Skip to content

Commit

Permalink
Made use of std::tie for certain unit_map operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Feb 3, 2018
1 parent 13cacd1 commit 40da35d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
10 changes: 7 additions & 3 deletions src/actions/create.cpp
Expand Up @@ -626,10 +626,14 @@ place_recruit_result place_recruit(unit_ptr u, const map_location &recruit_locat
const map_location leader_loc = !show ? map_location::null_location() :
find_recruit_leader(u->side(), recruit_location, recruited_from);
u->set_location(recruit_location);

unit_map::unit_iterator new_unit_itor;
bool success = false;

// Add the unit to the board.
std::pair<unit_map::iterator, bool> add_result = resources::gameboard->units().insert(u);
assert(add_result.second);
unit_map::iterator & new_unit_itor = add_result.first;
std::tie(new_unit_itor, success) = resources::gameboard->units().insert(u);
assert(success);

map_location current_loc = recruit_location;

if (facing == map_location::NDIRECTIONS) {
Expand Down
17 changes: 9 additions & 8 deletions src/actions/move.cpp
Expand Up @@ -522,14 +522,15 @@ namespace { // Private helpers for move_unit()
// Invalidate before moving so we invalidate neighbor hexes if needed.
move_it_->anim_comp().invalidate(disp);

// Attempt actually moving.
// (Fails if *step_to is occupied).
std::pair<unit_map::iterator, bool> move_result =
resources::gameboard->units().move(*move_loc_, *step_to);
if ( move_result.second )
{
// Attempt actually moving. Fails if *step_to is occupied.
unit_map::unit_iterator unit_it;
bool success = false;

std::tie(unit_it, success) = resources::gameboard->units().move(*move_loc_, *step_to);

if(success) {
// Update the moving unit.
move_it_ = move_result.first;
move_it_ = unit_it;
move_it_->set_facing(step_from->get_relative_dir(*step_to));
// Disable bars. The expectation here is that the animation
// unit_mover::finish() will clean after us at a later point. Ugly,
Expand All @@ -546,7 +547,7 @@ namespace { // Private helpers for move_unit()
disp.redraw_minimap();
}

return move_result.second;
return success;
}


Expand Down
29 changes: 18 additions & 11 deletions src/ai/simulated_actions.cpp
Expand Up @@ -94,19 +94,24 @@ bool simulated_attack(const map_location& attacker_loc, const map_location& defe
return true;
}

bool simulated_move(int side, const map_location& from, const map_location& to, int steps, map_location& unit_location){
bool simulated_move(int side, const map_location& from, const map_location& to, int steps, map_location& unit_location)
{
LOG_AI_SIM_ACTIONS << "Simulated move" << std::endl;

unit_map::unit_iterator move_unit;
bool success = false;

// In simulation, AI should not know if there is a enemy's ambusher.
std::pair<unit_map::unit_iterator, bool> unit_move = resources::gameboard->units().move(from, to);
bool is_ok = unit_move.second;
if(!is_ok){
unit_location = to; // This happened because in some CAs like get_village_phase and move_leader_to_keep phase,
// if the destination is already occupied will not be checked before execute. Just silent
// errors in ai/actions and tell rca the game state isn't changed.
std::tie(move_unit, success) = resources::gameboard->units().move(from, to);

if(!success) {
// This happened because in some CAs like get_village_phase and move_leader_to_keep phase,
// if the destination is already occupied will not be checked before execute. Just silent
// errors in ai/actions and tell rca the game state isn't changed.
unit_location = to;
return false;
}
unit_map::unit_iterator move_unit = unit_move.first;

move_unit->set_movement(move_unit->movement_left()-steps); // Following original logic, remove_movement_ will be considered outside.

unit_location = move_unit->get_location(); // For check_after.
Expand Down Expand Up @@ -216,9 +221,11 @@ void helper_place_unit(const unit& u, const map_location& loc){
new_unit->heal_fully();
new_unit->set_location(loc);

std::pair<unit_map::iterator, bool> add_result = resources::gameboard->units().insert(new_unit);
assert(add_result.second);
unit_map::iterator& new_unit_itor = add_result.first;
unit_map::unit_iterator new_unit_itor;
bool success = false;

std::tie(new_unit_itor, success) = resources::gameboard->units().insert(new_unit);
assert(success);

if(resources::gameboard->map().is_village(loc)){
helper_check_village(loc, new_unit_itor->side());
Expand Down
10 changes: 7 additions & 3 deletions src/scripting/lua_unit.cpp
Expand Up @@ -70,10 +70,14 @@ unit_ptr lua_unit::get_shared()
bool lua_unit::put_map(const map_location &loc)
{
if (ptr) {
std::pair<unit_map::unit_iterator, bool> res = resources::gameboard->units().replace(loc, ptr);
if (res.second) {
unit_map::unit_iterator unit_it;
bool success = false;

std::tie(unit_it, success) = resources::gameboard->units().replace(loc, ptr);

if(success) {
ptr.reset();
uid = res.first->underlying_id();
uid = unit_it->underlying_id();
} else {
ERR_LUA << "Could not move unit " << ptr->underlying_id() << " onto map location " << loc << '\n';
return false;
Expand Down
10 changes: 7 additions & 3 deletions src/synced_commands.cpp
Expand Up @@ -483,8 +483,12 @@ SYNCED_COMMAND_HANDLER_FUNCTION(debug_create_unit, child, use_undo, /*show*/, e
// Create the unit.
unit_ptr created(new unit(*u_type, side_num, true, gender));
created->new_turn();

unit_map::unit_iterator unit_it;

// Add the unit to the board.
std::pair<unit_map::iterator, bool> add_result = resources::gameboard->units().replace(loc, created);
std::tie(unit_it, std::ignore) = resources::gameboard->units().replace(loc, created);

game_display::get_singleton()->invalidate_unit();
resources::game_events->pump().fire("unit_placed", loc);
unit_display::unit_recruited(loc);
Expand All @@ -498,8 +502,8 @@ SYNCED_COMMAND_HANDLER_FUNCTION(debug_create_unit, child, use_undo, /*show*/, e
actions::shroud_clearer clearer;
clearer.clear_unit(loc, *created);
clearer.fire_events();
if ( add_result.first.valid() ) // In case sighted events messed with the unit.
actions::actor_sighted(*add_result.first);
if (unit_it.valid() ) // In case sighted events messed with the unit.
actions::actor_sighted(*unit_it);

return true;
}
Expand Down

0 comments on commit 40da35d

Please sign in to comment.