Skip to content

Commit

Permalink
Merge branch 'refactor_unit_filter'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jul 3, 2014
2 parents ee2f981 + f40b446 commit cee6874
Show file tree
Hide file tree
Showing 44 changed files with 902 additions and 587 deletions.
6 changes: 4 additions & 2 deletions data/test/scenarios/break_replay_with_lua_random.cfg
Expand Up @@ -71,7 +71,8 @@
code =<<
wesnoth.set_variable("rnd_num", math.random(200))
>>
[/lua])}
[/lua]
)}

{TEST_BREAK_REPLAY "fixed_lua_random_replay_with_sync_choice"
([lua]
Expand All @@ -82,4 +83,5 @@
end)
wesnoth.set_variable("rnd_num", result.value)
>>
[/lua])}
[/lua]
)}
3 changes: 2 additions & 1 deletion data/test/scenarios/event_handlers_in_events.cfg
Expand Up @@ -189,7 +189,8 @@
name=post_advance
{VARIABLE pass_test 1}
[/my_event]
[/variables])}
[/variables]
)}
[store_unit]
[filter]
x=1
Expand Down
3 changes: 2 additions & 1 deletion data/test/scenarios/move_skip_sighted.cfg
Expand Up @@ -61,7 +61,8 @@
id=bob
x={STOP_X}
y={STOP_Y}
[/have_unit])}
[/have_unit]
)}
[/event]
[/test]
#enddef
Expand Down
6 changes: 4 additions & 2 deletions data/test/scenarios/test_unit_map.cfg
Expand Up @@ -9,14 +9,16 @@
[have_unit]
x,y=9,5
[/have_unit]
[/not])}
[/not]
)}
#enddef

#define ASSERT_YES_9_5
{ASSERT (
[have_unit]
x,y=9,5
[/have_unit])}
[/have_unit]
)}
#enddef


Expand Down
6 changes: 4 additions & 2 deletions data/test/scenarios/units_offmap_goto_recall.cfg
Expand Up @@ -20,13 +20,15 @@
side = 1
search_recall_list = no
[/have_unit]
[/not])}
[/not]
)}
{RETURN (
[have_unit]
id = Charlie
canrecruit = no
side = 1
search_recall_list = yes
[/have_unit])}
[/have_unit]
)}
[/event]
)}
2 changes: 2 additions & 0 deletions projectfiles/CodeBlocks/wesnoth.cbp
Expand Up @@ -1039,6 +1039,8 @@
<Unit filename="..\..\src\unit_display.hpp" />
<Unit filename="..\..\src\unit_drawer.cpp" />
<Unit filename="..\..\src\unit_drawer.hpp" />
<Unit filename="..\..\src\unit_filter.cpp" />
<Unit filename="..\..\src\unit_filter.hpp" />
<Unit filename="..\..\src\unit_formula_manager.cpp" />
<Unit filename="..\..\src\unit_formula_manager.hpp" />
<Unit filename="..\..\src\unit_frame.cpp" />
Expand Down
8 changes: 8 additions & 0 deletions projectfiles/VC9/wesnoth.vcproj
Expand Up @@ -21192,6 +21192,14 @@
RelativePath="..\..\src\unit_drawer.hpp"
>
</File>
<File
RelativePath="..\..\src\unit_filter.cpp"
>
</File>
<File
RelativePath="..\..\src\unit_filter.hpp"
>
</File>
<File
RelativePath="..\..\src\unit_formula_manager.cpp"
>
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -897,6 +897,7 @@ set(wesnoth-main_SRC
unit_animation_component.cpp
unit_display.cpp
unit_drawer.cpp
unit_filter.cpp
unit_formula_manager.cpp
unit_frame.cpp
unit_helper.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -529,6 +529,7 @@ wesnoth_sources = Split("""
unit_animation_component.cpp
unit_display.cpp
unit_drawer.cpp
unit_filter.cpp
unit_formula_manager.cpp
unit_frame.cpp
unit_helper.cpp
Expand Down
10 changes: 7 additions & 3 deletions src/actions/create.cpp
Expand Up @@ -25,6 +25,7 @@

#include "../config.hpp"
#include "../config_assign.hpp"
#include "../filter_context.hpp"
#include "../game_board.hpp"
#include "../game_display.hpp"
#include "../game_events/pump.hpp"
Expand All @@ -44,6 +45,7 @@
#include "../team.hpp"
#include "../unit.hpp"
#include "../unit_display.hpp"
#include "../unit_filter.hpp"
#include "../variable.hpp"
#include "../whiteboard/manager.hpp"

Expand Down Expand Up @@ -429,7 +431,8 @@ namespace { // Helpers for get_recalls()
// Only units that match the leader's recall filter are valid.
scoped_recall_unit this_unit("this_unit", save_id, leader_team.recall_list().find_index(recall_unit.id()));

if ( recall_unit.matches_filter(vconfig(leader->recall_filter()), map_location::null_location()) )
const unit_filter ufilt(vconfig(leader->recall_filter()), resources::filter_con);
if ( ufilt(recall_unit, map_location::null_location()) )
{
result.push_back(recall_unit_ptr);
if ( already_added != NULL )
Expand Down Expand Up @@ -527,8 +530,9 @@ namespace { // Helpers for check_recall_location()
team& recall_team = (*resources::teams)[recaller.side()-1];
scoped_recall_unit this_unit("this_unit", recall_team.save_id(),
recall_team.recall_list().find_index(recall_unit.id()));
if ( !recall_unit.matches_filter(vconfig(recaller.recall_filter()),
map_location::null_location()) )

const unit_filter ufilt(vconfig(recaller.recall_filter()), resources::filter_con);
if ( !ufilt(recall_unit, map_location::null_location()) )
return RECRUIT_NO_ABLE_LEADER;

// Make sure the unit is on a keep.
Expand Down
13 changes: 9 additions & 4 deletions src/ai/composite/goal.cpp
Expand Up @@ -24,6 +24,8 @@
#include "ai/lua/core.hpp"
#include "ai/lua/lua_object.hpp"
#include "ai/manager.hpp"
#include "filter_context.hpp"
#include "game_board.hpp"
#include "log.hpp"
#include "map_location.hpp"
#include "resources.hpp"
Expand All @@ -32,6 +34,7 @@
#include "terrain_filter.hpp"
#include "unit.hpp"
#include "unit_map.hpp"
#include "unit_filter.hpp"
#include "wml_exception.hpp"

#include <boost/lexical_cast.hpp>
Expand Down Expand Up @@ -132,8 +135,9 @@ void target_unit_goal::add_targets(std::back_insert_iterator< std::vector< targe
if (!criteria) return;

//find the enemy leaders and explicit targets
const unit_filter ufilt(vconfig(criteria), resources::filter_con);
BOOST_FOREACH(const unit &u, *resources::units) {
if (u.matches_filter(vconfig(criteria), u.get_location())) {
if (ufilt( u )) {
LOG_AI_GOAL << "found explicit target unit at ... " << u.get_location() << " with value: " << value() << "\n";
*target_list = target(u.get_location(), value(), target::EXPLICIT);
}
Expand Down Expand Up @@ -163,7 +167,7 @@ void target_location_goal::on_create()
}
const config &criteria = cfg_.child("criteria");
if (criteria) {
filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),*resources::units));
filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),resources::filter_con));
}
}

Expand Down Expand Up @@ -219,7 +223,7 @@ void protect_goal::on_create()
}
const config &criteria = cfg_.child("criteria");
if (criteria) {
filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),*resources::units));
filter_ptr_ = boost::shared_ptr<terrain_filter>(new terrain_filter(vconfig(criteria),resources::filter_con));
}


Expand Down Expand Up @@ -256,13 +260,14 @@ void protect_goal::add_targets(std::back_insert_iterator< std::vector< target >

std::set<map_location> items;
if (protect_unit_) {
const unit_filter ufilt(vconfig(criteria), resources::filter_con);
BOOST_FOREACH(const unit &u, units)
{
if (protect_only_own_unit_ && u.side() != get_side()) {
continue;
}
//TODO: we will protect hidden units, by not testing for invisibility to current side
if (u.matches_filter(vconfig(criteria), u.get_location())) {
if (ufilt(u)) {
DBG_AI_GOAL << "side " << get_side() << ": in " << goal_type << ": " << u.get_location() << " should be protected\n";
items.insert(u.get_location());
}
Expand Down
6 changes: 3 additions & 3 deletions src/ai/composite/value_translator.hpp
Expand Up @@ -179,10 +179,10 @@ class config_value_translator<terrain_filter> {
static terrain_filter cfg_to_value(const config &cfg)
{
if (const config &v = cfg.child("value")) {
return terrain_filter(vconfig(v), *resources::units);
return terrain_filter(vconfig(v), resources::filter_con);
}
static config c("not");
return terrain_filter(vconfig(c),*resources::units);
return terrain_filter(vconfig(c),resources::filter_con);
}

static void cfg_to_value(const config &cfg, terrain_filter &value)
Expand Down Expand Up @@ -442,7 +442,7 @@ class variant_value_translator<terrain_filter> {
static terrain_filter variant_to_value(const variant &var)
{
static config c("not");
terrain_filter value(vconfig(c),*resources::units);
terrain_filter value(vconfig(c),resources::filter_con);
variant_to_value(var,value);
return value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ai/contexts.cpp
Expand Up @@ -608,7 +608,7 @@ const terrain_filter& readonly_context_impl::get_avoid() const
}
config cfg;
cfg.add_child("not");
static terrain_filter tf(vconfig(cfg),*resources::units);
static terrain_filter tf(vconfig(cfg),resources::filter_con);
return tf;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ai/lua/lua_object.hpp
Expand Up @@ -138,7 +138,7 @@ inline boost::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua
boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config());
boost::shared_ptr<vconfig> vcfg = boost::shared_ptr<vconfig>(new vconfig(*cfg));
luaW_tovconfig(L, n, *vcfg);
boost::shared_ptr<terrain_filter> tf = boost::shared_ptr<terrain_filter>(new terrain_filter(*vcfg, *resources::units));
boost::shared_ptr<terrain_filter> tf = boost::shared_ptr<terrain_filter>(new terrain_filter(*vcfg, resources::filter_con));
return tf;
}

Expand Down
10 changes: 6 additions & 4 deletions src/ai/recruitment/recruitment.cpp
Expand Up @@ -25,6 +25,7 @@
#include "../manager.hpp"
#include "../../actions/attack.hpp"
#include "../../attack_prediction.hpp"
#include "../../filter_context.hpp"
#include "../../game_board.hpp"
#include "../../game_display.hpp"
#include "../../log.hpp"
Expand All @@ -35,6 +36,7 @@
#include "../../resources.hpp"
#include "../../team.hpp"
#include "../../tod_manager.hpp"
#include "../../unit_filter.hpp"
#include "../../unit_map.hpp"
#include "../../unit_types.hpp"
#include "../../util.hpp"
Expand Down Expand Up @@ -250,8 +252,8 @@ void recruitment::execute() {
// we'll check if we can do a recall instead of a recruitment.
BOOST_FOREACH(const unit_const_ptr & recall, current_team().recall_list()) {
// Check if this leader is allowed to recall this unit.
vconfig filter = vconfig(leader->recall_filter());
if (!recall->matches_filter(filter, map_location::null_location())) {
const unit_filter ufilt( vconfig(leader->recall_filter()), resources::filter_con);
if (!ufilt(*recall, map_location::null_location())) {
continue;
}
data.recruits.insert(recall->type_id());
Expand Down Expand Up @@ -475,8 +477,8 @@ const std::string* recruitment::get_appropriate_recall(const std::string& type,
continue;
}
// Check if this leader is allowed to recall this unit.
vconfig filter = vconfig(leader_data.leader->recall_filter());
if (!recall_unit->matches_filter(filter, map_location::null_location())) {
const unit_filter ufilt(vconfig(leader_data.leader->recall_filter()), resources::filter_con);
if (!ufilt(*recall_unit, map_location::null_location())) {
LOG_AI_RECRUITMENT << "Refused recall because of filter: " << recall_unit->id() << "\n";
continue;
}
Expand Down
39 changes: 21 additions & 18 deletions src/ai/testing/aspect_attacks.cpp
Expand Up @@ -29,6 +29,7 @@
#include "../../resources.hpp"
#include "../../unit.hpp"
#include "../../pathfind/pathfind.hpp"
#include "../../unit_filter.hpp"

namespace ai {

Expand Down Expand Up @@ -73,9 +74,10 @@ boost::shared_ptr<attacks_vector> aspect_attacks::analyze_targets() const
unit_map& units_ = *resources::units;

std::vector<map_location> unit_locs;
const unit_filter filt_own(vconfig(filter_own_), resources::filter_con);
for(unit_map::const_iterator i = units_.begin(); i != units_.end(); ++i) {
if (i->side() == get_side() && i->attacks_left() && !(i->can_recruit() && get_passive_leader())) {
if (!i->matches_filter(vconfig(filter_own_), i->get_location())) {
if (!filt_own(*i)) {
continue;
}
unit_locs.push_back(i->get_location());
Expand All @@ -91,25 +93,26 @@ boost::shared_ptr<attacks_vector> aspect_attacks::analyze_targets() const

unit_stats_cache().clear();

const unit_filter filt_en(vconfig(filter_enemy_), resources::filter_con);
for(unit_map::const_iterator j = units_.begin(); j != units_.end(); ++j) {

// Attack anyone who is on the enemy side,
// and who is not invisible or petrified.
if (current_team().is_enemy(j->side()) && !j->incapacitated() &&
!j->invisible(j->get_location()))
{
if (!j->matches_filter(vconfig(filter_enemy_), j->get_location())) {
continue;
}
map_location adjacent[6];
get_adjacent_tiles(j->get_location(), adjacent);
attack_analysis analysis;
analysis.target = j->get_location();
analysis.vulnerability = 0.0;
analysis.support = 0.0;
do_attack_analysis(j->get_location(), srcdst, dstsrc,
fullmove_srcdst, fullmove_dstsrc, enemy_srcdst, enemy_dstsrc,
adjacent,used_locations,unit_locs,*res,analysis, current_team());
// Attack anyone who is on the enemy side,
// and who is not invisible or petrified.
if (current_team().is_enemy(j->side()) && !j->incapacitated() &&
!j->invisible(j->get_location()))
{
if (!filt_en( *j)) {
continue;
}
map_location adjacent[6];
get_adjacent_tiles(j->get_location(), adjacent);
attack_analysis analysis;
analysis.target = j->get_location();
analysis.vulnerability = 0.0;
analysis.support = 0.0;
do_attack_analysis(j->get_location(), srcdst, dstsrc,
fullmove_srcdst, fullmove_dstsrc, enemy_srcdst, enemy_dstsrc,
adjacent,used_locations,unit_locs,*res,analysis, current_team());
}
}
return res;
Expand Down
10 changes: 10 additions & 0 deletions src/display.cpp
Expand Up @@ -416,6 +416,16 @@ const time_of_day & display::get_time_of_day(const map_location& /*loc*/) const
return tod;
}

/**
* Display objects don't hold a tod maanger, instead game_display objects do. If the base version of this method is called,
* try to get it from resources and use an assert to check for failure.
*/
const tod_manager & display::get_tod_man() const
{
assert(resources::tod_manager);
return *resources::tod_manager;
}

void display::update_tod() {
const time_of_day& tod = get_time_of_day();
tod_color col = color_adjust_ + tod.color;
Expand Down
4 changes: 3 additions & 1 deletion src/display.hpp
Expand Up @@ -49,6 +49,7 @@ namespace wb {

#include "animated.hpp"
#include "display_context.hpp"
#include "filter_context.hpp"
#include "font.hpp"
#include "image.hpp" //only needed for enums (!)
#include "key.hpp"
Expand All @@ -71,7 +72,7 @@ namespace wb {

class gamemap;

class display
class display : public filter_context
{
public:
display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::manager> wb,
Expand Down Expand Up @@ -164,6 +165,7 @@ class display

void change_display_context(const display_context * dc);
const display_context & get_disp_context() const { return *dc_; }
virtual const tod_manager & get_tod_man() const; //!< This is implemented properly in game_display. The display:: impl could be pure virtual here but we decide not to.

void reset_halo_manager();
void reset_halo_manager(halo::manager & hm);
Expand Down

0 comments on commit cee6874

Please sign in to comment.