Skip to content

Commit

Permalink
Merge branch 'move_temp_unit_structs_to_game_board'
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 3, 2014
2 parents 7f499f7 + 1ec273c commit 94b057f
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 130 deletions.
1 change: 1 addition & 0 deletions src/ai/formula/function_table.cpp
Expand Up @@ -24,6 +24,7 @@

#include "../../attack_prediction.hpp"
#include "../../filesystem.hpp"
#include "../../game_board.hpp"
#include "../../game_display.hpp"
#include "../../log.hpp"
#include "../../map_label.hpp"
Expand Down
110 changes: 110 additions & 0 deletions src/game_board.cpp
Expand Up @@ -214,3 +214,113 @@ void game_board::write_config(config & cfg) const {
//write the map
cfg["map_data"] = map_.write();
}

temporary_unit_placer::temporary_unit_placer(unit_map& m, const map_location& loc, unit& u)
: m_(m), loc_(loc), temp_(m_.extract(loc))
{
u.clone();
m_.add(loc, u);
}

temporary_unit_placer::temporary_unit_placer(game_board& b, const map_location& loc, unit& u)
: m_(b.units_), loc_(loc), temp_(m_.extract(loc))
{
u.clone();
m_.add(loc, u);
}

temporary_unit_placer::~temporary_unit_placer()
{
m_.erase(loc_);
if(temp_) {
m_.insert(temp_);
}
}

temporary_unit_remover::temporary_unit_remover(unit_map& m, const map_location& loc)
: m_(m), loc_(loc), temp_(m_.extract(loc))
{
}

temporary_unit_remover::temporary_unit_remover(game_board& b, const map_location& loc)
: m_(b.units_), loc_(loc), temp_(m_.extract(loc))
{
}

temporary_unit_remover::~temporary_unit_remover()
{
if(temp_) {
m_.insert(temp_);
}
}

/**
* Constructor
* This version will change the unit's current movement to @a new_moves while
* the unit is moved (and restored to its previous value upon this object's
* destruction).
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst, int new_moves)
: m_(m), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? NULL : m_.extract(dst))
{
std::pair<unit_map::iterator, bool> move_result = m_.move(src_, dst_);

// Set the movement.
if ( move_result.second )
{
old_moves_ = move_result.first->movement_left(true);
move_result.first->set_movement(new_moves);
}
}

temporary_unit_mover::temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst, int new_moves)
: m_(b.units_), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? NULL : m_.extract(dst))
{
std::pair<unit_map::iterator, bool> move_result = m_.move(src_, dst_);

// Set the movement.
if ( move_result.second )
{
old_moves_ = move_result.first->movement_left(true);
move_result.first->set_movement(new_moves);
}
}

/**
* Constructor
* This version does not change (nor restore) the unit's movement.
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst)
: m_(m), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? NULL : m_.extract(dst))
{
m_.move(src_, dst_);
}

temporary_unit_mover::temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst)
: m_(b.units_), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? NULL : m_.extract(dst))
{
m_.move(src_, dst_);
}

temporary_unit_mover::~temporary_unit_mover()
{
std::pair<unit_map::iterator, bool> move_result = m_.move(dst_, src_);

// Restore the movement?
if ( move_result.second && old_moves_ >= 0 )
move_result.first->set_movement(old_moves_);

// Restore the extracted unit?
if(temp_) {
m_.insert(temp_);
}
}

97 changes: 97 additions & 0 deletions src/game_board.hpp
Expand Up @@ -30,6 +30,20 @@ namespace events {
class mouse_handler;
}

/**
*
* Game board class.
*
* The purpose of this class is to encapsulate some of the core game logic, including the unit map,
* the list of teams, and the game map.
*
* This should eventually become part of the game state object IMO, which should be a child of play_controller.
*
* I also intend to move the pathfinding module to be housed within this class -- this way, we can implement a
* sound pathfinding data structure to speed up path computations for AI, without having to make "update event"
* code at all points in the engine which modify the relevant data.
*
**/

class game_board {

Expand All @@ -44,6 +58,24 @@ class game_board {
friend class playsingle_controller;
friend class playmp_controller;
friend class events::mouse_handler;

/**
* Temporary unit move structs:
*
* Probably don't remove these friends, this is actually fairly useful. These structs are used by:
* - AI
* - Whiteboard
* - I think certain wml actions
* For AI, the ai wants to move two units next to eachother so it can ask for attack calculations. This should not trigger
* pathfinding modifications, so the version that directly changes the unit map is probably preferable, although it should be
* refactored.
* For whiteboard and wml actions, we generally do want pathfinding to be updated, so use the game_board constructors which I
* have added to these structs instead.
*
**/
friend struct temporary_unit_placer;
friend struct temporary_unit_mover;
friend struct temporary_unit_remover;

public:

Expand Down Expand Up @@ -85,4 +117,69 @@ class game_board {
unit* get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false); //TODO: can this not return a pointer?
};

/**
* This object is used to temporary place a unit in the unit map, swapping out
* any unit that is already there. On destruction, it restores the unit map to
* its original.
*/
struct temporary_unit_placer
{
temporary_unit_placer(unit_map& m, const map_location& loc, unit& u);
temporary_unit_placer(game_board& m, const map_location& loc, unit& u);
virtual ~temporary_unit_placer();

private:
unit_map& m_;
const map_location loc_;
unit *temp_;
};

// Begin Temporary Unit Move Structs
// TODO: Fix up the implementations which use game_board

/**
* This object is used to temporary remove a unit from the unit map.
* On destruction, it restores the unit map to its original.
* unit_map iterators to this unit must not be accessed while the unit is temporarily
* removed, otherwise a collision will happen when trying to reinsert the unit.
*/
struct temporary_unit_remover
{
temporary_unit_remover(unit_map& m, const map_location& loc);
temporary_unit_remover(game_board& m, const map_location& loc);
virtual ~temporary_unit_remover();

private:
unit_map& m_;
const map_location loc_;
unit *temp_;
};


/**
* This object is used to temporary move a unit in the unit map, swapping out
* any unit that is already there. On destruction, it restores the unit map to
* its original.
*/
struct temporary_unit_mover
{
temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst, int new_moves);
temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst);
temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst, int new_moves);
temporary_unit_mover(game_board& b, const map_location& src,
const map_location& dst);
virtual ~temporary_unit_mover();

private:
unit_map& m_;
const map_location src_;
const map_location dst_;
int old_moves_;
unit *temp_;
};


#endif
74 changes: 0 additions & 74 deletions src/unit.cpp
Expand Up @@ -3062,80 +3062,6 @@ team_data calculate_team_data(const team& tm, int side)
return res;
}

temporary_unit_placer::temporary_unit_placer(unit_map& m, const map_location& loc, unit& u)
: m_(m), loc_(loc), temp_(m.extract(loc))
{
u.clone();
m.add(loc, u);
}

temporary_unit_placer::~temporary_unit_placer()
{
m_.erase(loc_);
if(temp_) {
m_.insert(temp_);
}
}

temporary_unit_remover::temporary_unit_remover(unit_map& m, const map_location& loc)
: m_(m), loc_(loc), temp_(m.extract(loc))
{
}

temporary_unit_remover::~temporary_unit_remover()
{
if(temp_) {
m_.insert(temp_);
}
}

/**
* Constructor
* This version will change the unit's current movement to @a new_moves while
* the unit is moved (and restored to its previous value upon this object's
* destruction).
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst, int new_moves)
: m_(m), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? NULL : m.extract(dst))
{
std::pair<unit_map::iterator, bool> move_result = m.move(src_, dst_);

// Set the movement.
if ( move_result.second )
{
old_moves_ = move_result.first->movement_left(true);
move_result.first->set_movement(new_moves);
}
}

/**
* Constructor
* This version does not change (nor restore) the unit's movement.
*/
temporary_unit_mover::temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst)
: m_(m), src_(src), dst_(dst), old_moves_(-1),
temp_(src == dst ? NULL : m.extract(dst))
{
m.move(src_, dst_);
}

temporary_unit_mover::~temporary_unit_mover()
{
std::pair<unit_map::iterator, bool> move_result = m_.move(dst_, src_);

// Restore the movement?
if ( move_result.second && old_moves_ >= 0 )
move_result.first->set_movement(old_moves_);

// Restore the extracted unit?
if(temp_) {
m_.insert(temp_);
}
}

std::string unit::TC_image_mods() const{
std::stringstream modifier;
if(!flag_rgb_.empty()){
Expand Down
55 changes: 0 additions & 55 deletions src/unit.hpp
Expand Up @@ -596,61 +596,6 @@ struct team_data

team_data calculate_team_data(const class team& tm, int side);

/**
* This object is used to temporary place a unit in the unit map, swapping out
* any unit that is already there. On destruction, it restores the unit map to
* its original.
*/
struct temporary_unit_placer
{
temporary_unit_placer(unit_map& m, const map_location& loc, unit& u);
virtual ~temporary_unit_placer();

private:
unit_map& m_;
const map_location loc_;
unit *temp_;
};

/**
* This object is used to temporary remove a unit from the unit map.
* On destruction, it restores the unit map to its original.
* unit_map iterators to this unit must not be accessed while the unit is temporarily
* removed, otherwise a collision will happen when trying to reinsert the unit.
*/
struct temporary_unit_remover
{
temporary_unit_remover(unit_map& m, const map_location& loc);
virtual ~temporary_unit_remover();

private:
unit_map& m_;
const map_location loc_;
unit *temp_;
};


/**
* This object is used to temporary move a unit in the unit map, swapping out
* any unit that is already there. On destruction, it restores the unit map to
* its original.
*/
struct temporary_unit_mover
{
temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst, int new_moves);
temporary_unit_mover(unit_map& m, const map_location& src,
const map_location& dst);
virtual ~temporary_unit_mover();

private:
unit_map& m_;
const map_location src_;
const map_location dst_;
int old_moves_;
unit *temp_;
};

/**
* Gets a checksum for a unit.
*
Expand Down
1 change: 1 addition & 0 deletions src/whiteboard/action.cpp
Expand Up @@ -23,6 +23,7 @@
#include "recall.hpp"
#include "suppose_dead.hpp"

#include "game_board.hpp"
#include "resources.hpp"
#include "team.hpp"
#include "unit.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/whiteboard/attack.cpp
Expand Up @@ -22,6 +22,7 @@
#include "utility.hpp"

#include "arrow.hpp"
#include "game_board.hpp"
#include "play_controller.hpp"
#include "resources.hpp"
#include "unit.hpp"
Expand Down

0 comments on commit 94b057f

Please sign in to comment.