Skip to content

Commit

Permalink
fix [filter_ability] issue wesnoth#7992
Browse files Browse the repository at this point in the history
  • Loading branch information
newfrenchy83 committed Oct 28, 2023
1 parent 829b74c commit 83eacbc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/units/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
#include "utils/config_filters.hpp"
#include "variable.hpp" // for vconfig, etc

#include <boost/dynamic_bitset_fwd.hpp>

#include <bitset>

#include <cassert> // for assert
#include <cstdlib> // for rand
#include <exception> // for exception
Expand Down Expand Up @@ -1441,7 +1445,7 @@ static bool matches_ability_filter(const config & cfg, const std::string& tag_na
if(!bool_matches_if_present(filter, cfg, "affect_enemies", false))
return false;

if(!bool_matches_if_present(filter, cfg, "cumulative", false))
if((filter.has_attribute("cumulative") && numerical_tags.count(tag_name) == 0 && tag_name != "berserk") || !bool_matches_if_present(filter, cfg, "cumulative", false))
return false;

const std::vector<std::string> filter_type = utils::split(filter["tag_name"]);
Expand All @@ -1450,17 +1454,23 @@ static bool matches_ability_filter(const config & cfg, const std::string& tag_na

if(!string_matches_if_present(filter, cfg, "id", ""))
return false;
if(filter.has_attribute("apply_to")){
bool matche_apply_to = (tag_name == "resistance") ? string_matches_if_present(filter, cfg, "apply_to", "") : string_matches_if_present(filter, cfg, "apply_to", "self");
if(abilities_tags.count(tag_name) != 0 || !matche_apply_to){
return false;
}
}

if(!string_matches_if_present(filter, cfg, "apply_to", "self"))
return false;

if(!string_matches_if_present(filter, cfg, "overwrite_specials", "none"))
if((filter.has_attribute("overwrite_specials") && numerical_tags.count(tag_name) == 0 && tag_name != "berserk" && tag_name != "plague") || !string_matches_if_present(filter, cfg, "overwrite_specials", "none"))
return false;

if(!string_matches_if_present(filter, cfg, "active_on", "both"))
if((filter.has_attribute("active_on") && abilities_tags.count(tag_name) != 0) || !string_matches_if_present(filter, cfg, "active_on", "both"))
return false;

if(!filter["value"].empty()){
if((filter.has_attribute("value") && numerical_tags.count(tag_name) == 0 && tag_name != "berserk" && tag_name != "tailwind")){
return false;
}
bool has_other_key = (!cfg["add"].empty() || !cfg["sub"].empty() || !cfg["multiply"].empty() || !cfg["divide"].empty());
if(!has_other_key){
if(tag_name == "drains"){
Expand All @@ -1483,16 +1493,16 @@ static bool matches_ability_filter(const config & cfg, const std::string& tag_na
}
}

if(!int_matches_if_present_or_negative(filter, cfg, "add", "sub"))
if((filter.has_attribute("add") && numerical_tags.count(tag_name) == 0) || !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.has_attribute("sub") && numerical_tags.count(tag_name) == 0) || !int_matches_if_present_or_negative(filter, cfg, "sub", "add"))
return false;

if(!double_matches_if_present(filter, cfg, "multiply"))
if((filter.has_attribute("multiply") && numerical_tags.count(tag_name) == 0) || !double_matches_if_present(filter, cfg, "multiply"))
return false;

if(!double_matches_if_present(filter, cfg, "divide"))
if((filter.has_attribute("divide") && numerical_tags.count(tag_name) == 0) || !double_matches_if_present(filter, cfg, "divide"))
return false;

// Passed all tests.
Expand Down
10 changes: 10 additions & 0 deletions src/utils/config_filters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#pragma once

#include "config.hpp"
#include <boost/dynamic_bitset_fwd.hpp>

#include <bitset>

/**
* Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.
Expand Down Expand Up @@ -69,4 +72,11 @@ bool string_matches_if_present(

bool bool_or_empty(const config& filter, const config& cfg, const std::string& attribute);

/**
* vector used for verify what attribute checked can be present in special/abilities.
*/
const std::set<std::string> abilities_tags{"tailwind", "hides", "skirmisher", "teleport", "leadership", "illuminates", "heals", "regenerate"};

const std::set<std::string> numerical_tags{"attacks", "damage", "chance_to_hit", "drains", "heal_on_hit", "resistance", "leadership", "illuminates", "heals", "regenerate"};

} // namespace utils::config_filters

0 comments on commit 83eacbc

Please sign in to comment.