|
5#

楼主 |
发表于 2020-11-29 11:44:16
|
只看该作者
函数:internal_conditional_passed
- static bool internal_conditional_passed(const unit_map* units, const vconfig cond, bool& backwards_compat)
- {
- static std::vector<std::pair<int,int> > default_counts = utils::parse_ranges("1-99999");
- // 检查第一种允许的条件块:[have_unit]
- // If the if statement requires we have a certain unit,
- // then check for that.
- const vconfig::child_list& have_unit = cond.get_children("have_unit");
- backwards_compat = backwards_compat && have_unit.empty();
- for (vconfig::child_list::const_iterator u = have_unit.begin(); u != have_unit.end(); ++u) {
- if (units == NULL)
- return false;
- std::vector<std::pair<int,int> > counts = (*u).has_attribute("count")? utils::parse_ranges((*u)["count"]) : default_counts;
- int match_count = 0;
- unit_map::const_iterator itor;
- for (itor = units->begin(); itor != units->end(); ++itor) {
- if (itor->second.hitpoints() > 0 && game_events::unit_matches_filter(itor, *u)) {
- ++match_count;
- if (counts == default_counts) {
- // by default a single match is enough, so avoid extra work
- break;
- }
- }
- }
- if (!in_ranges(match_count, counts)) {
- return false;
- }
- }
- // 检查第三种允许的条件块:[have_location]
- // If the if statement requires we have a certain location,
- // then check for that.
- const vconfig::child_list& have_location = cond.get_children("have_location");
- backwards_compat = backwards_compat && have_location.empty();
- for (vconfig::child_list::const_iterator v = have_location.begin(); v != have_location.end(); ++v) {
- std::set<map_location> res;
- terrain_filter(*v, *units).get_locations(res);
- std::vector<std::pair<int,int> > counts = (*v).has_attribute("count")? utils::parse_ranges((*v)["count"]) : default_counts;
- if (!in_ranges<int>(res.size(), counts)) {
- return false;
- }
- }
- // 检查第三种允许的条件块:[variable]
- // Check against each variable statement,
- // to see if the variable matches the conditions or not.
- const vconfig::child_list& variables = cond.get_children("variable");
- backwards_compat = backwards_compat && variables.empty();
- foreach (const vconfig &values, variables) {
- const std::string name = values["name"];
- const std::string& value = resources::state_of_game->get_variable_const(name);
- const double num_value = atof(value.c_str());
- #define TEST_STR_ATTR(name, test) do { \
- if (values.has_attribute(name)) { \
- std::string attr_str = values[name].str(); \
- if (!(test)) return false; \
- } \
- } while (0)
- #define TEST_NUM_ATTR(name, test) do { \
- if (values.has_attribute(name)) { \
- double attr_num = atof(values[name].c_str()); \
- if (!(test)) return false; \
- } \
- } while (0)
- TEST_STR_ATTR("equals", value == attr_str);
- TEST_NUM_ATTR("numerical_equals", num_value == attr_num);
- TEST_STR_ATTR("not_equals", value != attr_str);
- TEST_NUM_ATTR("numerical_not_equals", num_value != attr_num);
- TEST_NUM_ATTR("greater_than", num_value > attr_num);
- TEST_NUM_ATTR("less_than", num_value < attr_num);
- TEST_NUM_ATTR("greater_than_equal_to", num_value >= attr_num);
- TEST_NUM_ATTR("less_than_equal_to", num_value <= attr_num);
- TEST_STR_ATTR("boolean_equals",
- utils::string_bool(value) == utils::string_bool(attr_str));
- TEST_STR_ATTR("boolean_not_equals",
- utils::string_bool(value) != utils::string_bool(attr_str));
- TEST_STR_ATTR("contains", value.find(attr_str) != std::string::npos);
- #undef TEST_STR_ATTR
- #undef TEST_NUM_ATTR
- }
- // 3/2/1种条件块都满足,返回true
- return true;
- }
复制代码 |
|