AI refactoring: ai_game_info -> ai::game_info

This commit is contained in:
Iurii Chernyi 2009-06-04 19:36:53 +00:00
parent 9f89996e9d
commit ec19193f4e
10 changed files with 68 additions and 63 deletions

View File

@ -19,7 +19,7 @@
/**
* A small explanation about what's going on here:
* Each action has access to two ai_game_info objects
* Each action has access to two game_info objects
* First is 'info' - real information
* Second is 'subjective info' - AIs perception of what's going on
* So, when we check_before action, we use 'subjective info' and don't
@ -48,6 +48,8 @@ static lg::log_domain log_ai_actions("ai/actions");
#define WRN_AI_ACTIONS LOG_STREAM(warn, log_ai_actions)
#define ERR_AI_ACTIONS LOG_STREAM(err, log_ai_actions)
using namespace ai;
// =======================================================================
// AI ACTIONS
// =======================================================================
@ -127,13 +129,13 @@ bool ai_action_result::is_execution() const
return is_execution_;
}
ai_game_info& ai_action_result::get_info() const
game_info& ai_action_result::get_info() const
{
return ai::manager::get_active_ai_info_for_side(get_side());
return manager::get_active_ai_info_for_side(get_side());
}
ai_game_info& ai_action_result::get_subjective_info() const
game_info& ai_action_result::get_subjective_info() const
{
return get_info();
}
@ -145,12 +147,12 @@ bool ai_action_result::using_subjective_info() const
}
team& ai_action_result::get_my_team(ai_game_info& info) const
team& ai_action_result::get_my_team(game_info& info) const
{
return info.teams[side_-1];
}
const team& ai_action_result::get_my_team(const ai_game_info& info) const
const team& ai_action_result::get_my_team(const game_info& info) const
{
return info.teams[side_-1];
}
@ -252,8 +254,8 @@ bool ai_move_result::test_route(const unit &un, const team &my_team, const unit_
void ai_move_result::do_check_before()
{
DBG_AI_ACTIONS << " check_before " << *this << std::endl;
const ai_game_info& s_info = get_subjective_info();
const ai_game_info& info = get_info();
const game_info& s_info = get_subjective_info();
const game_info& info = get_info();
const unit_map& s_units = s_info.units;
const unit_map& units = info.units;
@ -305,7 +307,7 @@ void ai_move_result::do_execute()
DBG_AI_ACTIONS << " execute "<< *this << std::endl;
assert(is_success());
ai_game_info& info = get_info();
game_info& info = get_info();
move_unit(
/*game_display* disp*/ NULL,
@ -416,8 +418,8 @@ bool ai_recruit_result::test_suitable_recruit_location(const gamemap &map, const
void ai_recruit_result::do_check_before()
{
DBG_AI_ACTIONS << " check_before " << *this << std::endl;
const ai_game_info& s_info = get_subjective_info();
const ai_game_info& info = get_info();
const game_info& s_info = get_subjective_info();
const game_info& info = get_info();
const unit_map& s_units = s_info.units;
const unit_map& units = info.units;
@ -486,7 +488,7 @@ void ai_recruit_result::do_check_before()
void ai_recruit_result::do_check_after()
{
const ai_game_info& info = get_info();
const game_info& info = get_info();
const gamemap& map = info.map;
if (!map.on_board(recruit_location_)){
set_error(AI_ACTION_FAILURE);
@ -523,7 +525,7 @@ void ai_recruit_result::do_execute()
{
DBG_AI_ACTIONS << " execute: " << *this << std::endl;
assert(is_success());
ai_game_info& info = get_info();
game_info& info = get_info();
// We have to add the recruit command now, because when the unit
// is created it has to have the recruit command in the recorder
// to be able to put random numbers into to generate unit traits.
@ -540,7 +542,7 @@ void ai_recruit_result::do_execute()
get_my_team(info).spend_gold(u->second.cost());
// Confirm the transaction - i.e. don't undo recruitment
replay_guard.confirm_transaction();
ai::manager::raise_unit_recruited();
manager::raise_unit_recruited();
} else {
set_error(AI_ACTION_FAILURE);
}
@ -589,8 +591,8 @@ const unit *ai_stopunit_result::get_unit(const unit_map &units, bool)
void ai_stopunit_result::do_check_before()
{
DBG_AI_ACTIONS << " check_before " << *this << std::endl;
const ai_game_info& s_info = get_subjective_info();
const ai_game_info& info = get_info();
const game_info& s_info = get_subjective_info();
const game_info& info = get_info();
const unit_map& s_units = s_info.units;
const unit_map& units = info.units;
@ -605,7 +607,7 @@ void ai_stopunit_result::do_check_before()
void ai_stopunit_result::do_check_after()
{
const ai_game_info& info = get_info();
const game_info& info = get_info();
unit_map::const_iterator un = info.units.find(unit_location_);
if (un==info.units.end()){
set_error(AI_ACTION_FAILURE);
@ -641,7 +643,7 @@ void ai_stopunit_result::do_execute()
{
DBG_AI_ACTIONS << " execute: " << *this << std::endl;
assert(is_success());
const ai_game_info& info = get_info();
const game_info& info = get_info();
unit_map::iterator un = info.units.find(unit_location_);
if (remove_movement_){
un->second.set_movement(0);

View File

@ -75,19 +75,19 @@ protected:
int get_side() const { return side_; }
/* return real information about the game state */
ai_game_info& get_info() const;
ai::game_info& get_info() const;
/* return subjective information about the game state */
ai_game_info& get_subjective_info() const;
ai::game_info& get_subjective_info() const;
/* are we using the subjective info ? */
bool using_subjective_info() const;
/* get the team object corresponding to current side */
team& get_my_team(ai_game_info& info) const;
team& get_my_team(ai::game_info& info) const;
/* get the team object corresponding to current side */
const team& get_my_team(const ai_game_info& info) const;
const team& get_my_team(const ai::game_info& info) const;
/* set error code */
void set_error(int error_code);

View File

@ -275,7 +275,7 @@ manager::~manager()
manager::AI_map_of_stacks manager::ai_map_;
ai_game_info *manager::ai_info_;
game_info *manager::ai_info_;
events::generic_event manager::user_interact_("ai_user_interact");
events::generic_event manager::unit_recruited_("ai_unit_recruited");
events::generic_event manager::unit_moved_("ai_unit_moved");
@ -284,12 +284,12 @@ int manager::last_interact_ = 0;
int manager::num_interact_ = 0;
void manager::set_ai_info(const ai_game_info& i)
void manager::set_ai_info(const game_info& i)
{
if (ai_info_!=NULL){
clear_ai_info();
}
ai_info_ = new ai_game_info(i);
ai_info_ = new game_info(i);
ai_registry::init();
}
@ -694,13 +694,13 @@ const std::string& manager::get_active_ai_algorithm_type_for_side( int side )
}
ai_game_info& manager::get_active_ai_info_for_side( int /*side*/ )
game_info& manager::get_active_ai_info_for_side( int /*side*/ )
{
return *ai_info_;
}
ai_game_info& manager::get_ai_info()
game_info& manager::get_ai_info()
{
return *ai_info_;
}

View File

@ -153,7 +153,7 @@ public:
* Sets AI information.
* @param info ai_information to be set.
*/
static void set_ai_info(const ai_game_info& info);
static void set_ai_info(const game_info& info);
/**
@ -237,7 +237,7 @@ public:
* Adds active AI for specified @a side from @a file.
* @note Running this command may invalidate references previously returned
* by manager. AI is not initialized at this point.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param file file name, follows the usual WML convention.
* @param replace should new ai replace the current ai or 'be placed on top of it'.
* @return true if successful.
@ -249,7 +249,7 @@ public:
* Adds active AI for specified @a side from @a cfg.
* @note Running this command may invalidate references previously returned
* by manager. AI is not initialized at this point.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param cfg the config from which all ai parameters are to be read.
* @param replace should new ai replace the current ai or 'be placed on top of it'.
* @return true if successful.
@ -261,7 +261,7 @@ public:
* Adds active AI for specified @a side from parameters.
* @note Running this command may invalidate references previously returned
* by manager. AI is not initialized at this point.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param ai_algorithm_type type of AI algorithm to create.
* @param replace should new ai replace the current ai or 'be placed on top of it'.
* @return true if successful.
@ -295,7 +295,7 @@ public:
* Removes top-level AI from @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
*/
static void remove_ai_for_side( int side );
@ -304,7 +304,7 @@ public:
* Removes all AIs from @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
*/
static void remove_all_ais_for_side( int side );
@ -327,7 +327,7 @@ public:
* Gets AI parameters for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @return a reference to active AI parameters.
* @note This reference may become invalid after specific manager operations.
*/
@ -338,7 +338,7 @@ public:
* Gets effective AI parameters for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @return a reference to active AI effective parameters.
* @note this reference may become invalid after specific manager operations.
*/
@ -349,7 +349,7 @@ public:
* Get global AI parameters for active AI of the @a given side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @return a reference to active ai global parameters.
* @note This reference may become invalid after specific manager operations.
*/
@ -361,21 +361,21 @@ public:
* @param side side number (1-based).
* @return a reference to active AI info.
*/
static ai_game_info& get_active_ai_info_for_side( int side );
static game_info& get_active_ai_info_for_side( int side );
/**
* Gets global AI-game info
* @return a reference to the AI-game info.
*/
static ai_game_info& get_ai_info();
static game_info& get_ai_info();
/**
* Gets AI memory for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @return a reference to active AI memory.
* @note This reference may become invalid after specific manager operations.
*/
@ -386,7 +386,7 @@ public:
* Gets AI algorithm type for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @return a reference to active AI algorithm_type.
* @note This reference may become invalid after specific manager operations.
*/
@ -401,7 +401,7 @@ public:
* Sets AI parameters for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param ai_parameters AI parameters to be set.
*/
static void set_active_ai_parameters_for_side( int side, const std::vector<config>& ai_parameters );
@ -411,7 +411,7 @@ public:
* Sets effective AI parameters for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param ai_effective_parameters AI effective parameters to be set.
* @deprecated Added only for bug-for-bug compatibility with side.cpp.
* Will be refactored away.
@ -423,7 +423,7 @@ public:
* Sets global AI parameters for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param ai_global_parameters AI global parameters to be set.
* @deprecated Added only for bug-for-bug compatibility with side.cpp.
* Will be refactored away.
@ -435,7 +435,7 @@ public:
* Sets AI memory for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param ai_memory AI memory to be set.
* @deprecated Added only for bug-for-bug compatibility with side.cpp.
* Will be refactored away.
@ -447,7 +447,7 @@ public:
* Sets AI algorithm type for active AI of the given @a side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param ai_algorithm_type AI algorithm type to be set.
*/
static void set_active_ai_algorithm_type_for_side( int side, const std::string& ai_algorithm_type );
@ -459,7 +459,7 @@ public:
/**
* Plays a turn for the specified side using its active AI.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @param event_observer controller which will observe events produced by the AI.
*/
static void play_turn(int side, events::observer* event_observer);
@ -471,7 +471,7 @@ private:
static AI_map_of_stacks ai_map_;
static std::deque< ai_command_history_item > history_;
static long history_item_counter_;
static ai_game_info *ai_info_;
static game_info *ai_info_;
static events::generic_event user_interact_;
static events::generic_event unit_recruited_;
@ -545,7 +545,7 @@ private:
* Gets active AI for specified side.
* @note Running this command may invalidate references previously returned
* by manager.
* @param side side number (1-based, as in ai_game_info).
* @param side side number (1-based, as in game_info).
* @return a reference to the active AI.
* @note This reference may become invalid after specific manager operations.
*/

View File

@ -201,12 +201,12 @@ bool readwrite_context_impl::recruit(const std::string& unit_name, map_location
}
const ai_game_info& readonly_context_impl::get_info() const{
const game_info& readonly_context_impl::get_info() const{
return manager::get_active_ai_info_for_side(get_side());
}
ai_game_info& readwrite_context_impl::get_info_w(){
game_info& readwrite_context_impl::get_info_w(){
return manager::get_active_ai_info_for_side(get_side());
}

View File

@ -137,7 +137,7 @@ public:
move_map& dstsrc, bool enemy, bool assume_full_movement=false,
const std::set<map_location>* remove_destinations=NULL,
bool see_all=false) const = 0;
const virtual ai_game_info& get_info() const = 0;
const virtual game_info& get_info() const = 0;
virtual void raise_user_interact() const = 0;
};
@ -160,7 +160,7 @@ public:
virtual void raise_unit_recruited() const = 0;
virtual void raise_unit_moved() const = 0;
virtual void raise_enemy_attacked() const = 0;
virtual ai_game_info& get_info_w() = 0;
virtual game_info& get_info_w() = 0;
};
class ai_default_context;
@ -285,7 +285,7 @@ public:
target_->calculate_moves(units, possible_moves, srcdst, dstsrc, enemy, assume_full_movement, remove_destinations, see_all);
}
const virtual ai_game_info& get_info() const
const virtual game_info& get_info() const
{
return target_->get_info();
}
@ -398,7 +398,7 @@ public:
}
virtual ai_game_info& get_info_w()
virtual game_info& get_info_w()
{
return target_->get_info_w();
}
@ -582,7 +582,7 @@ public:
bool see_all=false) const;
const virtual ai_game_info& get_info() const;
const virtual game_info& get_info() const;
/**
* Function which should be called frequently to allow the user to interact
@ -737,7 +737,7 @@ public:
* Functions to retrieve the 'info' object.
* Used by derived classes to discover all necessary game information.
*/
virtual ai_game_info& get_info_w();
virtual game_info& get_info_w();
virtual int get_recursion_count() const;

View File

@ -19,6 +19,8 @@
#include "game_info.hpp"
namespace ai {
// =======================================================================
//
// =======================================================================
} //end of namespace ai

View File

@ -32,6 +32,7 @@ class gamemap;
* decisions.
*/
namespace ai {
typedef int side_number;
/** The standard way in which a map of possible moves is recorded. */
@ -39,13 +40,11 @@ typedef std::multimap<map_location,map_location> move_map;
/** The standard way in which a map of possible movement routes to location is recorded*/
typedef std::map<map_location,paths> moves_map;
} //of namespace ai
class ai_game_info {
class game_info {
public:
ai_game_info(game_display& disp, gamemap& map, unit_map& units,
game_info(game_display& disp, gamemap& map, unit_map& units,
std::vector<team>& teams, gamestatus& state, class game_state& game_state)
: disp(disp), map(map), units(units), teams(teams),
state(state), game_state_(game_state)
@ -70,4 +69,6 @@ public:
class game_state& game_state_;
};
} //of namespace ai
#endif

View File

@ -37,7 +37,7 @@ void ai_testing::log_turn_end(unsigned int side)
void ai_testing::log_turn(const char* msg, unsigned int side)
{
ai_game_info& i = ai::manager::get_ai_info();
ai::game_info& i = ai::manager::get_ai_info();
assert(side>=1);
team& current_team = i.teams[side-1];
@ -70,7 +70,7 @@ void ai_testing::log_victory(std::vector<unsigned int> winners)
void ai_testing::log_game_start()
{
ai_game_info& i = ai::manager::get_ai_info();
ai::game_info& i = ai::manager::get_ai_info();
for (std::vector<team>::const_iterator tm = i.teams.begin(); tm != i.teams.end(); ++tm) {
int side = tm-i.teams.begin()+1;
LOG_AI_TESTING << "AI_IDENTIFIER"<<side<<": " << tm->ai_algorithm_identifier() <<std::endl;

View File

@ -66,7 +66,7 @@ playsingle_controller::playsingle_controller(const config& level,
browse_ = linger_ = true;
}
ai_game_info ai_info(*gui_,map_,units_,teams_,status_, gamestate_);
ai::game_info ai_info(*gui_,map_,units_,teams_,status_, gamestate_);
ai::manager::set_ai_info(ai_info);
ai::manager::add_observer(this) ;
}