Skip to content

Commit

Permalink
AI: made use of unique_ptrs for action results
Browse files Browse the repository at this point in the history
I had to move the implementations of certain context-related functions into the cpp
file since the result types were incomplete in the header, which doesn't work with a
unique_ptr (you get errors about being unable to delete an incomplete type).
  • Loading branch information
Vultraz committed May 1, 2018
1 parent 9756847 commit 18b50f0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 61 deletions.
92 changes: 92 additions & 0 deletions src/ai/contexts.cpp
Expand Up @@ -70,6 +70,98 @@ static lg::log_domain log_ai("ai/general");
// =======================================================================
namespace ai
{
// =======================================================================
// READ-WRITE CONTEXT PROXY
// =======================================================================

attack_result_ptr readwrite_context_proxy::execute_attack_action(
const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon)
{
return target_->execute_attack_action(attacker_loc, defender_loc, attacker_weapon);
}

recall_result_ptr readwrite_context_proxy::execute_recall_action(const std::string& id,
const map_location& where,
const map_location& from)
{
return target_->execute_recall_action(id, where, from);
}

recruit_result_ptr readwrite_context_proxy::execute_recruit_action(const std::string& unit_name,
const map_location& where ,
const map_location& from)
{
return target_->execute_recruit_action(unit_name, where, from);
}

move_result_ptr readwrite_context_proxy::execute_move_action(const map_location& from,
const map_location& to,
bool remove_movement,
bool unreach_is_ok)
{
return target_->execute_move_action(from, to, remove_movement, unreach_is_ok);
}

stopunit_result_ptr readwrite_context_proxy::execute_stopunit_action(
const map_location& unit_location, bool remove_movement, bool remove_attacks)
{
return target_->execute_stopunit_action(unit_location, remove_movement, remove_attacks);
}

synced_command_result_ptr readwrite_context_proxy::execute_synced_command_action(
const std::string& lua_code, const map_location& location)
{
return target_->execute_synced_command_action(lua_code, location);
}

// =======================================================================
// READ-ONLY CONTEXT PROXY
// =======================================================================

attack_result_ptr readonly_context_proxy::check_attack_action(
const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon)
{
return target_->check_attack_action(attacker_loc, defender_loc, attacker_weapon);
}

recall_result_ptr readonly_context_proxy::check_recall_action(const std::string& id,
const map_location& where,
const map_location& from)
{
return target_->check_recall_action(id, where, from);
}

recruit_result_ptr readonly_context_proxy::check_recruit_action(const std::string& unit_name,
const map_location& where,
const map_location& from)
{
return target_->check_recruit_action(unit_name, where, from);
}

move_result_ptr readonly_context_proxy::check_move_action(const map_location& from,
const map_location& to,
bool remove_movement,
bool unreach_is_ok)
{
return target_->check_move_action(from, to, remove_movement, unreach_is_ok);
}

stopunit_result_ptr readonly_context_proxy::check_stopunit_action(
const map_location& unit_location, bool remove_movement, bool remove_attacks)
{
return target_->check_stopunit_action(unit_location, remove_movement, remove_attacks);
}

synced_command_result_ptr readonly_context_proxy::check_synced_command_action(
const std::string& lua_code, const map_location& location)
{
return target_->check_synced_command_action(lua_code, location);
}

// =======================================================================
// IMPLEMENTATIONS
// =======================================================================

int side_context_impl::get_recursion_count() const
{
return recursion_counter_.get_count();
Expand Down
60 changes: 12 additions & 48 deletions src/ai/contexts.hpp
Expand Up @@ -534,44 +534,26 @@ class readonly_context_proxy : public virtual readonly_context, public virtual s
}

virtual attack_result_ptr check_attack_action(
const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon) override
{
return target_->check_attack_action(attacker_loc, defender_loc, attacker_weapon);
}
const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon) override;

virtual move_result_ptr check_move_action(const map_location& from,
const map_location& to,
bool remove_movement = true,
bool unreach_is_ok = false) override
{
return target_->check_move_action(from, to, remove_movement, unreach_is_ok);
}
bool unreach_is_ok = false) override;

virtual recall_result_ptr check_recall_action(const std::string& id,
const map_location& where = map_location::null_location(),
const map_location& from = map_location::null_location()) override
{
return target_->check_recall_action(id, where, from);
}
const map_location& from = map_location::null_location()) override;

virtual recruit_result_ptr check_recruit_action(const std::string& unit_name,
const map_location& where = map_location::null_location(),
const map_location& from = map_location::null_location()) override
{
return target_->check_recruit_action(unit_name, where, from);
}
const map_location& from = map_location::null_location()) override;

virtual stopunit_result_ptr check_stopunit_action(
const map_location& unit_location, bool remove_movement = true, bool remove_attacks = false) override
{
return target_->check_stopunit_action(unit_location, remove_movement, remove_attacks);
}
const map_location& unit_location, bool remove_movement = true, bool remove_attacks = false) override;

virtual synced_command_result_ptr check_synced_command_action(
const std::string& lua_code, const map_location& location = map_location::null_location()) override
{
return target_->check_synced_command_action(lua_code, location);
}
const std::string& lua_code, const map_location& location = map_location::null_location()) override;

virtual void calculate_possible_moves(std::map<map_location, pathfind::paths>& possible_moves,
move_map& srcdst,
Expand Down Expand Up @@ -961,44 +943,26 @@ class readwrite_context_proxy : public virtual readwrite_context, public virtual
}

virtual attack_result_ptr execute_attack_action(
const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon) override
{
return target_->execute_attack_action(attacker_loc, defender_loc, attacker_weapon);
}
const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon) override;

virtual move_result_ptr execute_move_action(const map_location& from,
const map_location& to,
bool remove_movement = true,
bool unreach_is_ok = false) override
{
return target_->execute_move_action(from, to, remove_movement, unreach_is_ok);
}
bool unreach_is_ok = false) override;

virtual recall_result_ptr execute_recall_action(const std::string& id,
const map_location& where = map_location::null_location(),
const map_location& from = map_location::null_location()) override
{
return target_->execute_recall_action(id, where, from);
}
const map_location& from = map_location::null_location()) override;

virtual recruit_result_ptr execute_recruit_action(const std::string& unit_name,
const map_location& where = map_location::null_location(),
const map_location& from = map_location::null_location()) override
{
return target_->execute_recruit_action(unit_name, where, from);
}
const map_location& from = map_location::null_location()) override;

virtual stopunit_result_ptr execute_stopunit_action(
const map_location& unit_location, bool remove_movement = true, bool remove_attacks = false) override
{
return target_->execute_stopunit_action(unit_location, remove_movement, remove_attacks);
}
const map_location& unit_location, bool remove_movement = true, bool remove_attacks = false) override;

virtual synced_command_result_ptr execute_synced_command_action(
const std::string& lua_code, const map_location& location = map_location::null_location()) override
{
return target_->execute_synced_command_action(lua_code, location);
}
const std::string& lua_code, const map_location& location = map_location::null_location()) override;

virtual team& current_team_w() override
{
Expand Down
16 changes: 8 additions & 8 deletions src/ai/game_info.hpp
Expand Up @@ -77,14 +77,14 @@ class move_and_attack_result;
class stopunit_result;
class synced_command_result;

typedef std::shared_ptr<action_result> action_result_ptr;
typedef std::shared_ptr<attack_result> attack_result_ptr;
typedef std::shared_ptr<recall_result> recall_result_ptr;
typedef std::shared_ptr<recruit_result> recruit_result_ptr;
typedef std::shared_ptr<move_result> move_result_ptr;
typedef std::shared_ptr<move_and_attack_result> move_and_attack_result_ptr;
typedef std::shared_ptr<stopunit_result> stopunit_result_ptr;
typedef std::shared_ptr<synced_command_result> synced_command_result_ptr;
typedef std::unique_ptr<action_result> action_result_ptr;
typedef std::unique_ptr<attack_result> attack_result_ptr;
typedef std::unique_ptr<recall_result> recall_result_ptr;
typedef std::unique_ptr<recruit_result> recruit_result_ptr;
typedef std::unique_ptr<move_result> move_result_ptr;
typedef std::unique_ptr<move_and_attack_result> move_and_attack_result_ptr;
typedef std::unique_ptr<stopunit_result> stopunit_result_ptr;
typedef std::unique_ptr<synced_command_result> synced_command_result_ptr;

class aspect;
class candidate_action;
Expand Down
10 changes: 5 additions & 5 deletions src/ai/lua/core.cpp
Expand Up @@ -188,7 +188,7 @@ static int ai_move(lua_State* L, bool exec, bool remove_movement)
ai::move_result_ptr move_result =
ai::actions::execute_move_action(side, exec, from, to, remove_movement, unreach_is_ok);

return transform_ai_action(L, move_result);
return transform_ai_action(L, std::move(move_result));
}

static int cfun_ai_execute_move_full(lua_State* L)
Expand Down Expand Up @@ -236,7 +236,7 @@ static int ai_attack(lua_State* L, bool exec)
ai::attack_result_ptr attack_result =
ai::actions::execute_attack_action(side, exec, attacker, defender, attacker_weapon, aggression, advancements);

return transform_ai_action(L, attack_result);
return transform_ai_action(L, std::move(attack_result));
}

static int cfun_ai_execute_attack(lua_State* L)
Expand All @@ -257,7 +257,7 @@ static int ai_stopunit_select(lua_State* L, bool exec, bool remove_movement, boo
ai::stopunit_result_ptr stopunit_result =
ai::actions::execute_stopunit_action(side, exec, loc, remove_movement, remove_attacks);

return transform_ai_action(L, stopunit_result);
return transform_ai_action(L, std::move(stopunit_result));
}

static int cfun_ai_execute_stopunit_moves(lua_State* L)
Expand Down Expand Up @@ -295,7 +295,7 @@ static int ai_recruit(lua_State* L, bool exec)
ai::recruit_result_ptr recruit_result =
ai::actions::execute_recruit_action(side, exec, std::string(unit_name), where, from);

return transform_ai_action(L, recruit_result);
return transform_ai_action(L, std::move(recruit_result));
}

static int cfun_ai_execute_recruit(lua_State* L)
Expand Down Expand Up @@ -323,7 +323,7 @@ static int ai_recall(lua_State* L, bool exec)
ai::recall_result_ptr recall_result =
ai::actions::execute_recall_action(side, exec, std::string(unit_id), where, from);

return transform_ai_action(L, recall_result);
return transform_ai_action(L, std::move(recall_result));
}

static int cfun_ai_execute_recall(lua_State* L)
Expand Down

0 comments on commit 18b50f0

Please sign in to comment.