Skip to content

Commit

Permalink
simplify game_state construction
Browse files Browse the repository at this point in the history
instead of calling constructor and init you now only have to call the
constructor.

In order to make this work this commit removes the uses of
resources::gameboard/units during team construction. while doing that i
moved the unit_creator class to a new file.

This commit also removes the ticks parameter from game_startes
constructor and replaces it with a ticks() function in play_controller,
other classes were also changed to use the play_controller::ticks()
function.
  • Loading branch information
gfgtdf committed Sep 9, 2015
1 parent 99c75bf commit 20fafde
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 272 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -739,6 +739,7 @@ set(wesnoth-main_SRC
actions/undo_recall_action.cpp
actions/undo_recruit_action.cpp
actions/undo_update_shroud_action.cpp
actions/unit_creator.cpp
actions/vision.cpp
addon/client.cpp
addon/info.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -198,6 +198,7 @@ wesnoth_sources = Split("""
actions/undo_recall_action.cpp
actions/undo_recruit_action.cpp
actions/undo_update_shroud_action.cpp
actions/unit_creator.cpp
actions/vision.cpp
addon/client.cpp
addon/info.cpp
Expand Down
175 changes: 0 additions & 175 deletions src/actions/create.cpp
Expand Up @@ -59,183 +59,8 @@ static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define ERR_NG LOG_STREAM(err, log_engine)

unit_creator::unit_creator(team &tm, const map_location &start_pos)
: add_to_recall_(false),discover_(false),get_village_(false),invalidate_(false), rename_side_(false), show_(false), start_pos_(start_pos), team_(tm)
{
}


unit_creator& unit_creator::allow_show(bool b)
{
show_=b;
return *this;
}


unit_creator& unit_creator::allow_get_village(bool b)
{
get_village_=b;
return *this;
}


unit_creator& unit_creator::allow_rename_side(bool b)
{
rename_side_=b;
return *this;
}

unit_creator& unit_creator::allow_invalidate(bool b)
{
invalidate_=b;
return *this;
}


unit_creator& unit_creator::allow_discover(bool b)
{
discover_=b;
return *this;
}


unit_creator& unit_creator::allow_add_to_recall(bool b)
{
add_to_recall_=b;
return *this;
}


map_location unit_creator::find_location(const config &cfg, const unit* pass_check)
{

DBG_NG << "finding location for unit with id=["<<cfg["id"]<<"] placement=["<<cfg["placement"]<<"] x=["<<cfg["x"]<<"] y=["<<cfg["y"]<<"] for side " << team_.side() << "\n";

std::vector< std::string > placements = utils::split(cfg["placement"]);

placements.push_back("map");
placements.push_back("recall");

BOOST_FOREACH(const std::string& place, placements) {
map_location loc;

if ( place == "recall" ) {
return map_location::null_location();
}

else if ( place == "leader" || place == "leader_passable" ) {
unit_map::const_iterator leader = resources::units->find_leader(team_.side());
//todo: take 'leader in recall list' possibility into account
if (leader.valid()) {
loc = leader->get_location();
} else {
loc = start_pos_;
}
}

// "map", "map_passable", and "map_overwrite".
else if ( place == "map" || place.compare(0, 4, "map_") == 0 ) {
loc = map_location(cfg,resources::gamedata);
}

if(loc.valid() && resources::gameboard->map().on_board(loc)) {
const bool pass((place == "leader_passable") || (place == "map_passable"));
if ( place != "map_overwrite" ) {
loc = find_vacant_tile(loc, pathfind::VACANT_ANY,
pass ? pass_check : NULL);
}
if(loc.valid() && resources::gameboard->map().on_board(loc)) {
return loc;
}
}
}

return map_location::null_location();

}


void unit_creator::add_unit(const config &cfg, const vconfig* vcfg)
{
config temp_cfg(cfg);
temp_cfg["side"] = team_.side();
temp_cfg.remove_attribute("player_id");
temp_cfg.remove_attribute("faction_from_recruit");

const std::string& id =(cfg)["id"];
bool animate = temp_cfg["animate"].to_bool();
temp_cfg.remove_attribute("animate");

unit_ptr recall_list_element = team_.recall_list().find_if_matches_id(id);

if ( !recall_list_element ) {
//make the new unit
unit_ptr new_unit(new unit(temp_cfg, true, vcfg));
map_location loc = find_location(temp_cfg, new_unit.get());
if ( loc.valid() ) {
//add the new unit to map
resources::units->replace(loc, *new_unit);
LOG_NG << "inserting unit for side " << new_unit->side() << "\n";
post_create(loc,*(resources::units->find(loc)),animate);
}
else if ( add_to_recall_ ) {
//add to recall list
team_.recall_list().add(new_unit);
DBG_NG << "inserting unit with id=["<<id<<"] on recall list for side " << new_unit->side() << "\n";
preferences::encountered_units().insert(new_unit->type_id());
}
} else {
//get unit from recall list
map_location loc = find_location(temp_cfg, recall_list_element.get());
if ( loc.valid() ) {
resources::units->replace(loc, *recall_list_element);
LOG_NG << "inserting unit from recall list for side " << recall_list_element->side()<< " with id="<< id << "\n";
post_create(loc,*(resources::units->find(loc)),animate);
//if id is not empty, delete units with this ID from recall list
team_.recall_list().erase_if_matches_id( id);
}
else if ( add_to_recall_ ) {
LOG_NG << "wanted to insert unit on recall list, but recall list for side " << (cfg)["side"] << "already contains id=" <<id<<"\n";
return;
}
}
}


void unit_creator::post_create(const map_location &loc, const unit &new_unit, bool anim)
{

if (discover_) {
preferences::encountered_units().insert(new_unit.type_id());
}

bool show = show_ && (resources::screen !=NULL) && !resources::screen->fogged(loc);
bool animate = show && anim;

if (get_village_) {
if (resources::gameboard->map().is_village(loc)) {
actions::get_village(loc, new_unit.side());
}
}

if (resources::screen!=NULL) {

if (invalidate_ ) {
resources::screen->invalidate(loc);
}

if (animate) {
unit_display::unit_recruited(loc);
} else if (show) {
resources::screen->draw();
}
}
}


namespace actions {


const std::set<std::string> get_recruits(int side, const map_location &recruit_loc)
{
const team & current_team = (*resources::teams)[side -1];
Expand Down
40 changes: 2 additions & 38 deletions src/actions/create.hpp
Expand Up @@ -26,47 +26,11 @@ class team;
class unit_type;
class vconfig;

#include "unit_creator.hpp"

#include "../map_location.hpp"
#include "../unit_ptr.hpp"


class unit_creator {
public:
unit_creator(team &tm, const map_location &start_pos);
unit_creator& allow_show(bool b);
unit_creator& allow_get_village(bool b);
unit_creator& allow_rename_side(bool b);
unit_creator& allow_invalidate(bool b);
unit_creator& allow_discover(bool b);
unit_creator& allow_add_to_recall(bool b);

/**
* finds a suitable location for unit
* @retval map_location::null_location() if unit is to be put into recall list
* @retval valid on-board map location otherwise
*/
map_location find_location(const config &cfg, const unit* pass_check=NULL);


/**
* adds a unit on map without firing any events (so, usable during team construction in gamestatus)
*/
void add_unit(const config &cfg, const vconfig* vcfg = NULL);

private:
void post_create(const map_location &loc, const unit &new_unit, bool anim);

bool add_to_recall_;
bool discover_;
bool get_village_;
bool invalidate_;
bool rename_side_;
bool show_;
const map_location start_pos_;
team &team_;

};

namespace actions {

/// The possible results of finding a location for recruiting (or recalling).
Expand Down

0 comments on commit 20fafde

Please sign in to comment.