Skip to content

Commit

Permalink
extent can_bool to resistance and leadership and using 'type_changed_…
Browse files Browse the repository at this point in the history
…by_special' in replacement to type for check type changed by damage

The addition of 'type_changed_by_special' is a temporary solution to allow wml authors to be able to check types modified by damage ([filter_opponent)) in specials without systematically risking infinite recursion. The introduction in wesnoth 1.19 of [filter_specials] in [filter_attack/weapon] should make it obsolete.
  • Loading branch information
newfrenchy83 committed Nov 2, 2023
1 parent 8a55eec commit 5be5479
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
8 changes: 8 additions & 0 deletions data/schema/filters/weapon.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@
{FILTER_BOOLEAN_OPS weapon}
[/tag]

[tag]
name="$filter_weapon_by_special"
max=0
super="$filter_weapon"
{SIMPLE_KEY modified_type string_list}
{FILTER_BOOLEAN_OPS weapon}
[/tag]

8 changes: 4 additions & 4 deletions data/schema/units/abilities.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
name="resistance"
max=infinite
super="units/unit_type/abilities/~value~"
{FILTER_TAG "filter_weapon" weapon ()}
{FILTER_TAG "filter_second_weapon" weapon ()}
{FILTER_TAG "filter_weapon" weapon_by_special ()}
{FILTER_TAG "filter_second_weapon" weapon_by_special ()}
[/tag]
[tag]
name="leadership"
Expand All @@ -95,8 +95,8 @@
{SIMPLE_KEY sub f_int}
{SIMPLE_KEY multiply f_int}
{SIMPLE_KEY divide f_int}
{FILTER_TAG "filter_weapon" weapon ()}
{FILTER_TAG "filter_second_weapon" weapon ()}
{FILTER_TAG "filter_weapon" weapon_by_special ()}
{FILTER_TAG "filter_second_weapon" weapon_by_special ()}
[/tag]
[tag]
name="illuminates"
Expand Down
8 changes: 4 additions & 4 deletions data/schema/units/specials.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

{FILTER_TAG "filter_adjacent" adjacent ()}
{FILTER_TAG "filter_adjacent_location" adjacent_location ()}
{FILTER_TAG "filter_self" unit {FILTER_TAG "filter_weapon" weapon ()}}
{FILTER_TAG "filter_opponent" unit {FILTER_TAG "filter_weapon" weapon ()}}
{FILTER_TAG "filter_attacker" unit {FILTER_TAG "filter_weapon" weapon ()}}
{FILTER_TAG "filter_defender" unit {FILTER_TAG "filter_weapon" weapon ()}}
{FILTER_TAG "filter_self" unit {FILTER_TAG "filter_weapon" weapon_by_special ()}}
{FILTER_TAG "filter_opponent" unit {FILTER_TAG "filter_weapon" weapon_by_special ()}}
{FILTER_TAG "filter_attacker" unit {FILTER_TAG "filter_weapon" weapon_by_special ()}}
{FILTER_TAG "filter_defender" unit {FILTER_TAG "filter_weapon" weapon_by_special ()}}
{WML_MERGE_KEYS}
[/tag]
# A few specials inheriting from ~generic~ are included here so that unit abilities can then inherit from them.
Expand Down
2 changes: 1 addition & 1 deletion src/units/abilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ bool unit::ability_affects_weapon(const config& cfg, const_attack_ptr weapon, bo
if(!weapon) {
return false;
}
return weapon->matches_filter(filter);
return weapon->matches_filter(filter, true);
}

bool unit::has_ability_type(const std::string& ability) const
Expand Down
16 changes: 8 additions & 8 deletions src/units/attack_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ std::string attack_type::accuracy_parry_description() const
* Returns whether or not *this matches the given @a filter, ignoring the
* complexities introduced by [and], [or], and [not].
*/
static bool matches_simple_filter(const attack_type & attack, const config & filter, bool can_loop)
static bool matches_simple_filter(const attack_type & attack, const config & filter, bool no_check)
{
const std::vector<std::string>& filter_range = utils::split(filter["range"]);
const std::string& filter_damage = filter["damage"];
Expand Down Expand Up @@ -144,8 +144,8 @@ static bool matches_simple_filter(const attack_type & attack, const config & fil
if ( !filter_name.empty() && std::find(filter_name.begin(), filter_name.end(), attack.id()) == filter_name.end() )
return false;

if ( !filter_type.empty()){
if(can_loop){
if (!filter_type.empty()){
if(no_check){
if (std::find(filter_type.begin(), filter_type.end(), attack.type()) == filter_type.end() ){
return false;
}
Expand Down Expand Up @@ -255,25 +255,25 @@ static bool matches_simple_filter(const attack_type & attack, const config & fil
/**
* Returns whether or not *this matches the given @a filter.
*/
bool attack_type::matches_filter(const config& filter, bool can_loop) const
bool attack_type::matches_filter(const config& filter, bool no_check) const
{
// Handle the basic filter.
bool matches = matches_simple_filter(*this, filter, can_loop);
bool matches = matches_simple_filter(*this, filter, no_check);

// Handle [and], [or], and [not] with in-order precedence
for (const config::any_child condition : filter.all_children_range() )
{
// Handle [and]
if ( condition.key == "and" )
matches = matches && matches_filter(condition.cfg, can_loop);
matches = matches && matches_filter(condition.cfg, no_check);

// Handle [or]
else if ( condition.key == "or" )
matches = matches || matches_filter(condition.cfg, can_loop);
matches = matches || matches_filter(condition.cfg, no_check);

// Handle [not]
else if ( condition.key == "not" )
matches = matches && !matches_filter(condition.cfg, can_loop);
matches = matches && !matches_filter(condition.cfg, no_check);
}

return matches;
Expand Down
2 changes: 1 addition & 1 deletion src/units/attack_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class attack_type : public std::enable_shared_from_this<attack_type>

// In unit_types.cpp:

bool matches_filter(const config& filter, bool can_loop = false) const;
bool matches_filter(const config& filter, bool no_check = false) const;
bool apply_modification(const config& cfg);
bool describe_modification(const config& cfg,std::string* description);

Expand Down

0 comments on commit 5be5479

Please sign in to comment.