prevent premature exit from filtering before [or] blocks

This commit is contained in:
Patrick Parker 2007-07-16 07:25:40 +00:00
parent 415033d3a8
commit 9603a1b182
2 changed files with 56 additions and 47 deletions

View file

@ -154,14 +154,19 @@ game_state* get_state_of_game()
return state_of_game;
}
bool conditional_passed(const unit_map* units,
const vconfig cond)
bool internal_conditional_passed(const unit_map* units,
const vconfig cond, bool& backwards_compat)
{
const vconfig::child_list& have_unit = cond.get_children("have_unit");
const vconfig::child_list& have_location = cond.get_children("have_location");
const vconfig::child_list& variables = cond.get_children("variable");
backwards_compat = backwards_compat
&& have_unit.size() == 0
&& have_location.size() == 0
&& variables.size() == 0;
//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");
int tag_count = have_unit.size();
for(vconfig::child_list::const_iterator u = have_unit.begin(); u != have_unit.end(); ++u) {
if(units == NULL)
@ -181,8 +186,6 @@ bool conditional_passed(const unit_map* units,
//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");
tag_count += have_location.size();
for(vconfig::child_list::const_iterator v = have_location.begin(); v != have_location.end(); ++v) {
std::set<gamemap::location> res;
wassert(game_map != NULL && units != NULL && status_ptr != NULL);
@ -194,8 +197,6 @@ bool conditional_passed(const unit_map* units,
//check against each variable statement to see if the variable
//matches the conditions or not
const vconfig::child_list& variables = cond.get_children("variable");
tag_count += variables.size();
for(vconfig::child_list::const_iterator var = variables.begin(); var != variables.end(); ++var) {
const vconfig& values = *var;
@ -250,10 +251,17 @@ bool conditional_passed(const unit_map* units,
return false;
}
}
bool matches = true; //so far, so good
int or_count = 0;
return true;
}
bool conditional_passed(const unit_map* units,
const vconfig cond)
{
bool backwards_compat = utils::string_bool(cond["backwards_compat"],true);
bool matches = internal_conditional_passed(units, cond, backwards_compat);
//handle [and], [or], and [not] with in-order precedence
int or_count = 0;
config::all_children_iterator cond_i = cond.get_config().ordered_begin();
config::all_children_iterator cond_end = cond.get_config().ordered_end();
while(cond_i != cond_end)
@ -265,25 +273,24 @@ bool conditional_passed(const unit_map* units,
if(cond_name == "and")
{
matches = matches && conditional_passed(units, cond_filter);
++tag_count;
backwards_compat = false;
}
//handle [or]
else if(cond_name == "or")
{
matches = matches || conditional_passed(units, cond_filter);
++or_count;
++tag_count;
}
//handle [not]
else if(cond_name == "not")
{
matches = matches && !conditional_passed(units, cond_filter);
++tag_count;
backwards_compat = false;
}
++cond_i;
}
//check for deprecated [or] syntax
if(matches && or_count > 1 && tag_count == or_count && cond.get_config().values.size() == 0)
if(matches && or_count > 1 && backwards_compat)
{
lg::wml_error << "possible deprecated [or] syntax: now forcing re-interpretation\n";
//for now we will re-interpret it according to the old rules
@ -2312,14 +2319,13 @@ bool matches_special_filter(const config* cfg, const vconfig filter)
if(!cfg) {
return false;
}
bool matches = true;
if(filter["weapon"] != "") {
if(filter["weapon"] != (*cfg)["weapon"]) {
return false;
matches = false;
}
}
bool matches = true; //so far, so good
//handle [and], [or], and [not] with in-order precedence
config::all_children_iterator cond_i = filter.get_config().ordered_begin();
config::all_children_iterator cond_end = filter.get_config().ordered_end();

View file

@ -714,13 +714,42 @@ bool unit::has_ability_by_id(const std::string& ability) const
bool unit::matches_filter(const vconfig& cfg, const gamemap::location& loc, bool use_flat_tod) const
{
bool matches = true;
if(loc.valid()) {
wassert(units_ != NULL);
scoped_xy_unit auto_store("this_unit", loc.x, loc.y, *units_);
return internal_matches_filter(cfg, loc, use_flat_tod);
matches = internal_matches_filter(cfg, loc, use_flat_tod);
} else {
//if loc is invalid, then this is a recall list unit (already been scoped)
matches = internal_matches_filter(cfg, loc, use_flat_tod);
}
//if loc is invalid, then this is a recall list unit which should have already been scoped
return internal_matches_filter(cfg, loc, use_flat_tod);
//handle [and], [or], and [not] with in-order precedence
config::all_children_iterator cond = cfg.get_config().ordered_begin();
config::all_children_iterator cond_end = cfg.get_config().ordered_end();
while(cond != cond_end)
{
const std::string& cond_name = *((*cond).first);
const vconfig cond_filter(&(*((*cond).second)));
//handle [and]
if(cond_name == "and") {
matches = matches && matches_filter(cond_filter,loc,use_flat_tod);
}
//handle [or]
else if(cond_name == "or") {
matches = matches || matches_filter(cond_filter,loc,use_flat_tod);
}
//handle [not]
else if(cond_name == "not") {
matches = matches && !matches_filter(cond_filter,loc,use_flat_tod);
}
++cond;
}
return matches;
}
bool unit::internal_matches_filter(const vconfig& cfg, const gamemap::location& loc, bool use_flat_tod) const
@ -886,33 +915,7 @@ bool unit::internal_matches_filter(const vconfig& cfg, const gamemap::location&
}
}
}
bool matches = true; //so far, so good
//handle [and], [or], and [not] with in-order precedence
config::all_children_iterator cond = cfg.get_config().ordered_begin();
config::all_children_iterator cond_end = cfg.get_config().ordered_end();
while(cond != cond_end)
{
const std::string& cond_name = *((*cond).first);
const vconfig cond_filter(&(*((*cond).second)));
//handle [and]
if(cond_name == "and") {
matches = matches && internal_matches_filter(cond_filter,loc,use_flat_tod);
}
//handle [or]
else if(cond_name == "or") {
matches = matches || internal_matches_filter(cond_filter,loc,use_flat_tod);
}
//handle [not]
else if(cond_name == "not") {
matches = matches && !internal_matches_filter(cond_filter,loc,use_flat_tod);
}
++cond;
}
return matches;
return true;
}