Skip to content

Commit

Permalink
repair detects non-existent default attribute value
Browse files Browse the repository at this point in the history
Fix wesnoth#7923 .when we want to detect an ability with value=0-20 and an ability with add=15 is present, this is detected because the system assigns a value of 0 when 'value' is not present, and so the system matches abilities that do not use 'value' or even no numerical value at all. it is therefore necessary to decide that the absence of an attribute is a non-correspondence to the criteria.
  • Loading branch information
newfrenchy83 committed Sep 29, 2023
1 parent e2bde1b commit aebaffe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
28 changes: 22 additions & 6 deletions src/units/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1482,19 +1482,35 @@ static bool matches_ability_filter(const config & cfg, const std::string& tag_na
if(!string_matches_if_present(filter, cfg, "active_on", "both"))
return false;

if(!int_matches_if_present(filter, cfg, "value"))
return false;
if(!filter["value"].empty()){
if(tag_name == "drains" || tag_name == "berserk" || tag_name != "heal_on_hit"){
int def = 0;
if(tag_name == "drains"){
def = 25;
}
if(tag_name == "berserk"){
def = 1;
}
if(!int_matches_if_present(filter, cfg, "value" , def)){
return false;
}
} else if(cfg["value"].empty()){
return false;
}
if(!int_matches_if_present(filter, cfg, "value"))
return false;
}

if(!int_matches_if_present_or_negative(filter, cfg, "add", "sub"))
if((!filter["add"].empty() && (cfg["add"].empty() && cfg["sub"].empty()) || !int_matches_if_present_or_negative(filter, cfg, "add", "sub")))
return false;

if(!int_matches_if_present_or_negative(filter, cfg, "sub", "add"))
if((!filter["sub"].empty() && (cfg["add"].empty() && cfg["sub"].empty()) || !int_matches_if_present_or_negative(filter, cfg, "sub", "add")))
return false;

if(!double_matches_if_present(filter, cfg, "multiply"))
if(!filter["multiply"].empty() && (cfg["multiply"].empty() || !double_matches_if_present(filter, cfg, "multiply")))
return false;

if(!double_matches_if_present(filter, cfg, "divide"))
if(!filter["divide"].empty() && (cfg["divide"].empty() || !double_matches_if_present(filter, cfg, "divide")))
return false;

if(!type_value_if_present(filter, cfg))
Expand Down
4 changes: 2 additions & 2 deletions src/utils/config_filters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ bool utils::config_filters::unsigned_matches_if_present(const config& filter, co
return in_ranges<int>(cfg[attribute].to_int(0), utils::parse_ranges_unsigned(filter[attribute].str()));
}

bool utils::config_filters::int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute)
bool utils::config_filters::int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, int def)
{
if(filter[attribute].empty()) {
return true;
}

return in_ranges<int>(cfg[attribute].to_int(0), utils::parse_ranges_int(filter[attribute].str()));
return in_ranges<int>(cfg[attribute].to_int(def), utils::parse_ranges_int(filter[attribute].str()));
}

bool utils::config_filters::int_matches_if_present_or_negative(
Expand Down
2 changes: 1 addition & 1 deletion src/utils/config_filters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace utils::config_filters
bool bool_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, bool def);

bool double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute);
bool int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute);
bool int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, int def = 0);

/**
* Restricts filters to only looking for values that are zero or more.
Expand Down

0 comments on commit aebaffe

Please sign in to comment.