From 18b50f06fb1af235c5cd494e6e55066f2e87cb88 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 1 May 2018 11:51:44 +1100 Subject: [PATCH] AI: made use of unique_ptrs for action results 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). --- src/ai/contexts.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++ src/ai/contexts.hpp | 60 ++++++----------------------- src/ai/game_info.hpp | 16 ++++---- src/ai/lua/core.cpp | 10 ++--- 4 files changed, 117 insertions(+), 61 deletions(-) diff --git a/src/ai/contexts.cpp b/src/ai/contexts.cpp index 17e387213df7..b70e0e35e6f4 100644 --- a/src/ai/contexts.cpp +++ b/src/ai/contexts.cpp @@ -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(); diff --git a/src/ai/contexts.hpp b/src/ai/contexts.hpp index 2906139111e3..f55d9afa2d29 100644 --- a/src/ai/contexts.hpp +++ b/src/ai/contexts.hpp @@ -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& possible_moves, move_map& srcdst, @@ -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 { diff --git a/src/ai/game_info.hpp b/src/ai/game_info.hpp index 044b22505a12..a96b773cdc75 100644 --- a/src/ai/game_info.hpp +++ b/src/ai/game_info.hpp @@ -77,14 +77,14 @@ class move_and_attack_result; class stopunit_result; class synced_command_result; -typedef std::shared_ptr action_result_ptr; -typedef std::shared_ptr attack_result_ptr; -typedef std::shared_ptr recall_result_ptr; -typedef std::shared_ptr recruit_result_ptr; -typedef std::shared_ptr move_result_ptr; -typedef std::shared_ptr move_and_attack_result_ptr; -typedef std::shared_ptr stopunit_result_ptr; -typedef std::shared_ptr synced_command_result_ptr; +typedef std::unique_ptr action_result_ptr; +typedef std::unique_ptr attack_result_ptr; +typedef std::unique_ptr recall_result_ptr; +typedef std::unique_ptr recruit_result_ptr; +typedef std::unique_ptr move_result_ptr; +typedef std::unique_ptr move_and_attack_result_ptr; +typedef std::unique_ptr stopunit_result_ptr; +typedef std::unique_ptr synced_command_result_ptr; class aspect; class candidate_action; diff --git a/src/ai/lua/core.cpp b/src/ai/lua/core.cpp index 26dceef59868..0d4f0331d638 100644 --- a/src/ai/lua/core.cpp +++ b/src/ai/lua/core.cpp @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)