mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-30 01:47:13 +00:00
AI Refactoring: moved ai_default into ai namespace
This commit is contained in:
parent
0f5c9a1d1c
commit
3cc9edcd15
@ -44,10 +44,17 @@ static lg::log_domain log_ai("ai/general");
|
|||||||
#define WRN_AI LOG_STREAM(warn, log_ai)
|
#define WRN_AI LOG_STREAM(warn, log_ai)
|
||||||
#define ERR_AI LOG_STREAM(err, log_ai)
|
#define ERR_AI LOG_STREAM(err, log_ai)
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(push)
|
||||||
|
//silence "inherits via dominance" warnings
|
||||||
|
#pragma warning(disable:4250)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace ai {
|
||||||
|
|
||||||
typedef util::array<map_location,6> adjacent_tiles_array;
|
typedef util::array<map_location,6> adjacent_tiles_array;
|
||||||
|
|
||||||
|
idle_ai::idle_ai(readwrite_context &context) : recursion_counter_(context.get_recursion_count())
|
||||||
idle_ai::idle_ai(ai::readwrite_context &context) : recursion_counter_(context.get_recursion_count())
|
|
||||||
{
|
{
|
||||||
init_readwrite_context_proxy(context);
|
init_readwrite_context_proxy(context);
|
||||||
}
|
}
|
||||||
@ -57,7 +64,7 @@ std::string idle_ai::describe_self()
|
|||||||
return "[idle_ai]";
|
return "[idle_ai]";
|
||||||
}
|
}
|
||||||
|
|
||||||
void idle_ai::switch_side(ai::side_number side)
|
void idle_ai::switch_side(side_number side)
|
||||||
{
|
{
|
||||||
set_side(side);
|
set_side(side);
|
||||||
}
|
}
|
||||||
@ -74,16 +81,11 @@ void idle_ai::play_turn()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#pragma warning(push)
|
|
||||||
//silence "inherits via dominance" warnings
|
|
||||||
#pragma warning(disable:4250)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** Sample ai, with simple strategy. */
|
/** Sample ai, with simple strategy. */
|
||||||
class sample_ai : public ai::readwrite_context_proxy, public ai::interface {
|
class sample_ai : public readwrite_context_proxy, public interface {
|
||||||
public:
|
public:
|
||||||
sample_ai(ai::readwrite_context &context)
|
sample_ai(readwrite_context &context)
|
||||||
: recursion_counter_(context.get_recursion_count()) {
|
: recursion_counter_(context.get_recursion_count()) {
|
||||||
init_readwrite_context_proxy(context);
|
init_readwrite_context_proxy(context);
|
||||||
}
|
}
|
||||||
@ -109,7 +111,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void do_attacks() {
|
void do_attacks() {
|
||||||
std::map<map_location,paths> possible_moves;
|
std::map<map_location,paths> possible_moves;
|
||||||
ai::move_map srcdst, dstsrc;
|
move_map srcdst, dstsrc;
|
||||||
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
|
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
|
||||||
|
|
||||||
for(unit_map::const_iterator i = get_info().units.begin(); i != get_info().units.end(); ++i) {
|
for(unit_map::const_iterator i = get_info().units.begin(); i != get_info().units.end(); ++i) {
|
||||||
@ -121,7 +123,7 @@ protected:
|
|||||||
std::pair<map_location,map_location> best_movement;
|
std::pair<map_location,map_location> best_movement;
|
||||||
|
|
||||||
for(size_t n = 0; n != 6; ++n) {
|
for(size_t n = 0; n != 6; ++n) {
|
||||||
typedef ai::move_map::const_iterator Itor;
|
typedef move_map::const_iterator Itor;
|
||||||
std::pair<Itor,Itor> range = dstsrc.equal_range(adjacent_tiles[n]);
|
std::pair<Itor,Itor> range = dstsrc.equal_range(adjacent_tiles[n]);
|
||||||
while(range.first != range.second) {
|
while(range.first != range.second) {
|
||||||
const map_location& dst = range.first->first;
|
const map_location& dst = range.first->first;
|
||||||
@ -159,10 +161,10 @@ protected:
|
|||||||
|
|
||||||
void get_villages() {
|
void get_villages() {
|
||||||
std::map<map_location,paths> possible_moves;
|
std::map<map_location,paths> possible_moves;
|
||||||
ai::move_map srcdst, dstsrc;
|
move_map srcdst, dstsrc;
|
||||||
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
|
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
|
||||||
|
|
||||||
for(ai::move_map::const_iterator i = dstsrc.begin(); i != dstsrc.end(); ++i) {
|
for(move_map::const_iterator i = dstsrc.begin(); i != dstsrc.end(); ++i) {
|
||||||
if(get_info().map.is_village(i->first) && current_team().owns_village(i->first) == false) {
|
if(get_info().map.is_village(i->first) && current_team().owns_village(i->first) == false) {
|
||||||
move_unit(i->second,i->first,possible_moves);
|
move_unit(i->second,i->first,possible_moves);
|
||||||
get_villages();
|
get_villages();
|
||||||
@ -183,13 +185,13 @@ protected:
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
std::map<map_location,paths> possible_moves;
|
std::map<map_location,paths> possible_moves;
|
||||||
ai::move_map srcdst, dstsrc;
|
move_map srcdst, dstsrc;
|
||||||
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
|
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
|
||||||
|
|
||||||
int closest_distance = -1;
|
int closest_distance = -1;
|
||||||
std::pair<map_location,map_location> closest_move;
|
std::pair<map_location,map_location> closest_move;
|
||||||
|
|
||||||
for(ai::move_map::const_iterator i = dstsrc.begin(); i != dstsrc.end(); ++i) {
|
for(move_map::const_iterator i = dstsrc.begin(); i != dstsrc.end(); ++i) {
|
||||||
const int distance = distance_between(i->first,leader->first);
|
const int distance = distance_between(i->first,leader->first);
|
||||||
if(closest_distance == -1 || distance < closest_distance) {
|
if(closest_distance == -1 || distance < closest_distance) {
|
||||||
closest_distance = distance;
|
closest_distance = distance;
|
||||||
@ -203,7 +205,7 @@ protected:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void switch_side(ai::side_number side){
|
void switch_side(side_number side){
|
||||||
set_side(side);
|
set_side(side);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +226,7 @@ protected:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
ai::recursion_counter recursion_counter_;
|
recursion_counter recursion_counter_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
@ -232,7 +234,7 @@ private:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
ai_default::ai_default(ai::default_ai_context &context) :
|
ai_default::ai_default(default_ai_context &context) :
|
||||||
game_logic::formula_callable(),
|
game_logic::formula_callable(),
|
||||||
recursion_counter_(context.get_recursion_count()),
|
recursion_counter_(context.get_recursion_count()),
|
||||||
defensive_position_cache_(),
|
defensive_position_cache_(),
|
||||||
@ -263,7 +265,7 @@ ai_default::ai_default(ai::default_ai_context &context) :
|
|||||||
ai_default::~ai_default(){
|
ai_default::~ai_default(){
|
||||||
}
|
}
|
||||||
|
|
||||||
void ai_default::switch_side(ai::side_number side){
|
void ai_default::switch_side(side_number side){
|
||||||
set_side(side);
|
set_side(side);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,7 +287,7 @@ void ai_default::new_turn()
|
|||||||
if (formula_ai_ != NULL){
|
if (formula_ai_ != NULL){
|
||||||
formula_ai_->new_turn();
|
formula_ai_->new_turn();
|
||||||
}
|
}
|
||||||
ai::interface::new_turn();
|
interface::new_turn();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ai_default::describe_self(){
|
std::string ai_default::describe_self(){
|
||||||
@ -414,10 +416,10 @@ bool ai_default::multistep_move_possible(const map_location& from,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
map_location ai_default::move_unit(map_location from, map_location to, ai::moves_map& possible_moves)
|
map_location ai_default::move_unit(map_location from, map_location to, moves_map& possible_moves)
|
||||||
{
|
{
|
||||||
ai::moves_map temp_possible_moves;
|
moves_map temp_possible_moves;
|
||||||
ai::moves_map* possible_moves_ptr = &possible_moves;
|
moves_map* possible_moves_ptr = &possible_moves;
|
||||||
|
|
||||||
const unit_map::const_iterator i = units_.find(from);
|
const unit_map::const_iterator i = units_.find(from);
|
||||||
if(i != units_.end() && i->second.can_recruit()) {
|
if(i != units_.end() && i->second.can_recruit()) {
|
||||||
@ -1490,11 +1492,11 @@ bool ai_default::do_recruitment()
|
|||||||
|
|
||||||
raise_user_interact();
|
raise_user_interact();
|
||||||
// Let formula ai to do recruiting first
|
// Let formula ai to do recruiting first
|
||||||
if (get_recursion_count()<ai::recursion_counter::MAX_COUNTER_VALUE)
|
if (get_recursion_count()<recursion_counter::MAX_COUNTER_VALUE)
|
||||||
{
|
{
|
||||||
if (!current_team().ai_parameters()["recruitment"].empty()){
|
if (!current_team().ai_parameters()["recruitment"].empty()){
|
||||||
if (!formula_ai_){
|
if (!formula_ai_){
|
||||||
formula_ai_ptr_ = (ai::manager::create_transient_ai(ai::manager::AI_TYPE_FORMULA_AI, this));
|
formula_ai_ptr_ = (manager::create_transient_ai(manager::AI_TYPE_FORMULA_AI, this));
|
||||||
formula_ai_ = static_cast<formula_ai*> (formula_ai_ptr_.get());
|
formula_ai_ = static_cast<formula_ai*> (formula_ai_ptr_.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2046,4 +2048,5 @@ void ai_default::attack_analysis::get_inputs(std::vector<game_logic::formula_inp
|
|||||||
inputs->push_back(formula_input("is_surrounded", FORMULA_READ_ONLY));
|
inputs->push_back(formula_input("is_surrounded", FORMULA_READ_ONLY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} //end of namespace ai
|
||||||
|
|
||||||
|
@ -35,30 +35,31 @@
|
|||||||
|
|
||||||
class formula_ai;
|
class formula_ai;
|
||||||
|
|
||||||
|
namespace ai {
|
||||||
|
|
||||||
/** A trivial ai that sits around doing absolutely nothing. */
|
/** A trivial ai that sits around doing absolutely nothing. */
|
||||||
class idle_ai : public ai::readwrite_context_proxy, public ai::interface {
|
class idle_ai : public readwrite_context_proxy, public interface {
|
||||||
public:
|
public:
|
||||||
idle_ai(ai::readwrite_context &context);
|
idle_ai(readwrite_context &context);
|
||||||
void play_turn();
|
void play_turn();
|
||||||
virtual std::string describe_self();
|
virtual std::string describe_self();
|
||||||
void switch_side(ai::side_number side);
|
void switch_side(side_number side);
|
||||||
int get_recursion_count() const;
|
int get_recursion_count() const;
|
||||||
private:
|
private:
|
||||||
ai::recursion_counter recursion_counter_;
|
recursion_counter recursion_counter_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ai_default : public virtual ai::default_ai_context_proxy, public ai::interface, public game_logic::formula_callable {
|
class ai_default : public virtual default_ai_context_proxy, public interface, public game_logic::formula_callable {
|
||||||
public:
|
public:
|
||||||
typedef ai::move_map move_map;
|
|
||||||
typedef map_location location;//will get rid of this later
|
typedef map_location location;//will get rid of this later
|
||||||
|
|
||||||
ai_default(ai::default_ai_context &context);
|
ai_default(default_ai_context &context);
|
||||||
virtual ~ai_default();
|
virtual ~ai_default();
|
||||||
|
|
||||||
virtual void play_turn();
|
virtual void play_turn();
|
||||||
virtual void new_turn();
|
virtual void new_turn();
|
||||||
virtual std::string describe_self();
|
virtual std::string describe_self();
|
||||||
void switch_side(ai::side_number side);
|
void switch_side(side_number side);
|
||||||
|
|
||||||
struct target {
|
struct target {
|
||||||
enum TYPE { VILLAGE, LEADER, EXPLICIT, THREAT, BATTLE_AID, MASS, SUPPORT };
|
enum TYPE { VILLAGE, LEADER, EXPLICIT, THREAT, BATTLE_AID, MASS, SUPPORT };
|
||||||
@ -103,7 +104,7 @@ public:
|
|||||||
/** get the recursion counter */
|
/** get the recursion counter */
|
||||||
int get_recursion_count() const;
|
int get_recursion_count() const;
|
||||||
private:
|
private:
|
||||||
ai::recursion_counter recursion_counter_;
|
recursion_counter recursion_counter_;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
@ -419,9 +420,11 @@ private:
|
|||||||
static const int min_recruiting_value_to_force_recruit = 28;
|
static const int min_recruiting_value_to_force_recruit = 28;
|
||||||
protected:
|
protected:
|
||||||
formula_ai *formula_ai_;
|
formula_ai *formula_ai_;
|
||||||
ai::ai_ptr formula_ai_ptr_;
|
ai_ptr formula_ai_ptr_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} //end of namespace ai
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
#endif
|
#endif
|
||||||
|
@ -30,6 +30,8 @@ static lg::log_domain log_ai("ai/attack");
|
|||||||
#define LOG_AI LOG_STREAM(info, log_ai)
|
#define LOG_AI LOG_STREAM(info, log_ai)
|
||||||
#define ERR_AI LOG_STREAM(err, log_ai)
|
#define ERR_AI LOG_STREAM(err, log_ai)
|
||||||
|
|
||||||
|
namespace ai {
|
||||||
|
|
||||||
const int max_positions = 10000;
|
const int max_positions = 10000;
|
||||||
|
|
||||||
/** Analyze possibility of attacking target on 'loc'. */
|
/** Analyze possibility of attacking target on 'loc'. */
|
||||||
@ -831,3 +833,5 @@ bool ai_default::desperate_attack(const map_location &loc)
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} //end of namespace ai
|
||||||
|
@ -34,13 +34,15 @@ static lg::log_domain log_ai("ai/move");
|
|||||||
#define DBG_AI LOG_STREAM(debug, log_ai)
|
#define DBG_AI LOG_STREAM(debug, log_ai)
|
||||||
#define ERR_AI LOG_STREAM(err, log_ai)
|
#define ERR_AI LOG_STREAM(err, log_ai)
|
||||||
|
|
||||||
|
namespace ai {
|
||||||
|
|
||||||
struct move_cost_calculator : cost_calculator
|
struct move_cost_calculator : cost_calculator
|
||||||
{
|
{
|
||||||
move_cost_calculator(const unit& u, const gamemap& map,
|
move_cost_calculator(const unit& u, const gamemap& map,
|
||||||
const unit_map& units,
|
const unit_map& units,
|
||||||
const map_location& loc,
|
const map_location& loc,
|
||||||
const ai::move_map& dstsrc,
|
const move_map& dstsrc,
|
||||||
const ai::move_map& enemy_dstsrc)
|
const move_map& enemy_dstsrc)
|
||||||
: unit_(u), map_(map), units_(units),
|
: unit_(u), map_(map), units_(units),
|
||||||
loc_(loc), dstsrc_(dstsrc), enemy_dstsrc_(enemy_dstsrc),
|
loc_(loc), dstsrc_(dstsrc), enemy_dstsrc_(enemy_dstsrc),
|
||||||
avoid_enemies_(u.usage() == "scout")
|
avoid_enemies_(u.usage() == "scout")
|
||||||
@ -88,7 +90,7 @@ private:
|
|||||||
const unit_map& units_;
|
const unit_map& units_;
|
||||||
// mutable std::map<t_translation::t_terrain,int> move_type_;
|
// mutable std::map<t_translation::t_terrain,int> move_type_;
|
||||||
const map_location loc_;
|
const map_location loc_;
|
||||||
const ai::move_map dstsrc_, enemy_dstsrc_;
|
const move_map dstsrc_, enemy_dstsrc_;
|
||||||
const bool avoid_enemies_;
|
const bool avoid_enemies_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -982,4 +984,5 @@ int ai_default::count_free_hexes_in_castle(const map_location& loc, std::set<map
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} //end of namespace ai
|
||||||
|
|
||||||
|
@ -31,6 +31,8 @@ static lg::log_domain log_ai("ai/village");
|
|||||||
#define LOG_AI LOG_STREAM(info, log_ai)
|
#define LOG_AI LOG_STREAM(info, log_ai)
|
||||||
#define WRN_AI LOG_STREAM(warn, log_ai)
|
#define WRN_AI LOG_STREAM(warn, log_ai)
|
||||||
|
|
||||||
|
namespace ai {
|
||||||
|
|
||||||
// Basic strategy
|
// Basic strategy
|
||||||
// 1. Store all our units that can move.
|
// 1. Store all our units that can move.
|
||||||
//
|
//
|
||||||
@ -931,6 +933,8 @@ static void dump_reachmap(treachmap& reachmap)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} //end of namespace ai
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// small helper rule to test the matching rules
|
// small helper rule to test the matching rules
|
||||||
// building rule
|
// building rule
|
||||||
|
@ -573,7 +573,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
variant execute(const formula_callable& variables) const {
|
variant execute(const formula_callable& variables) const {
|
||||||
variant attack = args()[0]->evaluate(variables);
|
variant attack = args()[0]->evaluate(variables);
|
||||||
ai_default::attack_analysis* analysis = convert_variant<ai_default::attack_analysis>(attack);
|
ai::ai_default::attack_analysis* analysis = convert_variant<ai::ai_default::attack_analysis>(attack);
|
||||||
unit_map units_with_moves(ai_.get_info().units);
|
unit_map units_with_moves(ai_.get_info().units);
|
||||||
typedef std::pair<map_location, map_location> mv;
|
typedef std::pair<map_location, map_location> mv;
|
||||||
foreach (const mv &m, analysis->movements) {
|
foreach (const mv &m, analysis->movements) {
|
||||||
@ -1097,7 +1097,7 @@ private:
|
|||||||
variant execute(const formula_callable& variables) const {
|
variant execute(const formula_callable& variables) const {
|
||||||
std::vector<variant> vars;
|
std::vector<variant> vars;
|
||||||
variant dstsrc_var = args()[0]->evaluate(variables);
|
variant dstsrc_var = args()[0]->evaluate(variables);
|
||||||
const ai_default::move_map& dstsrc = convert_variant<move_map_callable>(dstsrc_var)->dstsrc();
|
const ai::move_map& dstsrc = convert_variant<move_map_callable>(dstsrc_var)->dstsrc();
|
||||||
std::pair<ai::move_map::const_iterator,ai::move_map::const_iterator> range =
|
std::pair<ai::move_map::const_iterator,ai::move_map::const_iterator> range =
|
||||||
dstsrc.equal_range(convert_variant<location_callable>(args()[1]->evaluate(variables))->loc());
|
dstsrc.equal_range(convert_variant<location_callable>(args()[1]->evaluate(variables))->loc());
|
||||||
while(range.first != range.second) {
|
while(range.first != range.second) {
|
||||||
|
@ -70,7 +70,7 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class formula_ai : public ai_default {
|
class formula_ai : public ai::ai_default {
|
||||||
public:
|
public:
|
||||||
explicit formula_ai(ai::default_ai_context &context);
|
explicit formula_ai(ai::default_ai_context &context);
|
||||||
virtual ~formula_ai() {};
|
virtual ~formula_ai() {};
|
||||||
@ -78,7 +78,7 @@ public:
|
|||||||
virtual void new_turn();
|
virtual void new_turn();
|
||||||
virtual std::string describe_self();
|
virtual std::string describe_self();
|
||||||
|
|
||||||
using ai_default::move_map;
|
typedef ai::move_map move_map;
|
||||||
|
|
||||||
const move_map& srcdst() const { if(!move_maps_valid_) { prepare_move(); } return srcdst_; }
|
const move_map& srcdst() const { if(!move_maps_valid_) { prepare_move(); } return srcdst_; }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user