diff --git a/data/test/scenarios/has_ally.cfg b/data/test/scenarios/has_ally.cfg new file mode 100644 index 00000000000..ac243d9dc2f --- /dev/null +++ b/data/test/scenarios/has_ally.cfg @@ -0,0 +1,116 @@ +# This test checks that the [has_ally] tag is working as expected. + +#define TEST_HAS_ALLY_SCEN ID EVENTS + [test] + name = "Unit Test {ID}" + map_data = "{test/maps/move_skip_sighted.map}" + turns = 3 + id = {ID} + random_start_time = no + + {DAWN} + + [side] + side=1 + controller=human + name = "Alice" + type = Elvish Archer + id=alice + fog=no + team_name=West + [/side] + [side] + side=2 + controller=human + name = "Bob" + type = Orcish Grunt + id=bob + fog=no + team_name=East + [/side] + [side] + side=3 + controller=human + name = "Dave" + type = Dwarvish Fighter + id=dave + fog=no + team_name=East + [/side] + [side] + side=4 + controller=human + name= "Charlie" + type = Chocobone + id=charlie + fog=no + team_name=West + [/side] + + {EVENTS} + [/test] +#enddef + +{TEST_HAS_ALLY_SCEN "has_ally" ( + [event] + name=start + {ASSERT ([have_unit] + id=dave + [filter_side] + [has_ally] + [has_unit] + id=bob + [/has_unit] + [/has_ally] + [/filter_side] + [/have_unit] + )} + {ASSERT ([have_unit] + id=dave + [filter_side] + [has_enemy] + [has_unit] + id=alice + [/has_unit] + [/has_enemy] + [/filter_side] + [/have_unit] + )} + {ASSERT ([have_unit] + id=dave + [filter_side] + [not] + [has_ally] + [has_unit] + id=steve + [/has_unit] + [/has_ally] + [/not] + [/filter_side] + [/have_unit] + )} + {ASSERT ([have_unit] + id=dave + [filter_side] + [has_enemy] + side=1,4 + [/has_enemy] + [/filter_side] + [/have_unit] + )} + {ASSERT ([not] + [have_unit] + id=dave + [filter_side] + [has_ally] + [has_unit] + id=charlie + [/has_unit] + [/has_ally] + [/filter_side] + [/have_unit] + [/not] + )} + {RETURN ([true][/true])} + [/event] +)} diff --git a/src/side_filter.cpp b/src/side_filter.cpp index 23155591b2d..f8f5abb61ea 100644 --- a/src/side_filter.cpp +++ b/src/side_filter.cpp @@ -188,6 +188,39 @@ bool side_filter::match_internal(const team &t) const } } + const vconfig& has_enemy = cfg_.child("has_enemy"); + if(!has_enemy.null()) { + if (!has_enemy_filter_) + has_enemy_filter_.reset(new side_filter(has_enemy, fc_)); + const std::vector& teams = has_enemy_filter_->get_teams(); + bool found = false; + BOOST_FOREACH(const int side, teams) { + if((fc_->get_disp_context().teams())[side - 1].is_enemy(t.side())) + { + found = true; + break; + } + } + if (!found) return false; + } + + const vconfig& has_ally = cfg_.child("has_ally"); + if(!has_ally.null()) { + if (!has_ally_filter_) + has_ally_filter_.reset(new side_filter(has_ally, fc_)); + const std::vector& teams = has_ally_filter_->get_teams(); + bool found = false; + BOOST_FOREACH(const int side, teams) { + if(!(fc_->get_disp_context().teams())[side - 1].is_enemy(t.side())) + { + found = true; + break; + } + } + if (!found) return false; + } + + const config::attribute_value cfg_controller = cfg_["controller"]; if (!cfg_controller.blank()) { diff --git a/src/side_filter.hpp b/src/side_filter.hpp index a6830375d26..1bd93b747c5 100644 --- a/src/side_filter.hpp +++ b/src/side_filter.hpp @@ -65,6 +65,8 @@ private: mutable boost::scoped_ptr ufilter_; mutable boost::scoped_ptr allied_filter_; mutable boost::scoped_ptr enemy_filter_; + mutable boost::scoped_ptr has_ally_filter_; + mutable boost::scoped_ptr has_enemy_filter_; }; #endif diff --git a/wml_test_schedule b/wml_test_schedule index d277aa2ac3c..d773c7ba8c1 100644 --- a/wml_test_schedule +++ b/wml_test_schedule @@ -80,6 +80,7 @@ 0 event_handlers_in_events_7 0 event_handlers_in_events_8 0 filter_vision +0 has_ally # # Pathfinding #