added unit location memory to ai_dfool
This commit is contained in:
parent
e310e2b2c6
commit
8211e46f91
3 changed files with 119 additions and 2 deletions
|
@ -6,7 +6,8 @@ namespace dfool {
|
|||
info info_ = get_info();
|
||||
int team_num=get_info().team_num;
|
||||
const config& parms = current_team().ai_parameters();
|
||||
config ai_mem=current_team().ai_memory();
|
||||
config ai_mem = current_team().ai_memory();
|
||||
|
||||
const config::child_list& orders = parms.get_children("order");
|
||||
LOG_STREAM(info, ai)<<"dfool side:"<<team_num<<" of "<<current_team().nteams()<<std::endl;
|
||||
|
||||
|
@ -47,6 +48,8 @@ namespace dfool {
|
|||
LOG_STREAM(info, ai)<<"\t\t"<<u->second.underlying_description()<<std::endl;
|
||||
// LOG_STREAM(info, ai)<<"\t\t\t"<<u->second.get_ai_special()<<std::endl;
|
||||
// LOG_STREAM(info, ai)<<"\t\t\t"<<u->first.x<<","<<u->first.y<<std::endl;
|
||||
|
||||
unit_memory_.add_unit_sighting(u->second, u->first, get_info().state.turn());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,6 +144,9 @@ namespace dfool {
|
|||
}
|
||||
}
|
||||
|
||||
unit_memory_.write(ai_mem);
|
||||
current_team().set_ai_memory(ai_mem);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -236,4 +242,82 @@ namespace dfool {
|
|||
// LOG_STREAM(info, ai)<<"nounit:"<<std::endl;
|
||||
return(um.end());
|
||||
}
|
||||
|
||||
unit_memory::unit_memory(const game_data& gamedata, const config& cfg){
|
||||
const config::child_list mem_list=cfg.get_children("unit_memory");
|
||||
for(config::child_list::const_iterator mem = mem_list.begin(); mem != mem_list.end(); ++mem) {
|
||||
config unit_cfg = *((*mem)->child("unit"));
|
||||
|
||||
unit u(gamedata , unit_cfg);
|
||||
|
||||
int t = atoi((**mem)["turn"].c_str());
|
||||
|
||||
gamemap::location l(atoi((**mem)["x"].c_str())-1,atoi((**mem)["y"].c_str())-1);
|
||||
add_unit_sighting(u,l,t);
|
||||
}
|
||||
}
|
||||
|
||||
void unit_memory::add_unit_sighting(unit u, gamemap::location l, size_t t){
|
||||
std::string unit_id= u.underlying_description();
|
||||
//check if this unit has already been seen
|
||||
size_t i;
|
||||
for(i=0; i < ids_.size();i++){
|
||||
if(unit_id == ids_[i]){break;}
|
||||
}
|
||||
|
||||
if(i == ids_.size()){
|
||||
//unit has not been seen
|
||||
units_.push_back(u);
|
||||
ids_.push_back(unit_id);
|
||||
turns_.push_back(t);
|
||||
locations_.push_back(l);
|
||||
}else{
|
||||
//update unit info
|
||||
units_[i]=u;
|
||||
turns_[i]=t;
|
||||
locations_[i]=l;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void unit_memory::remove_unit_sighting(std::string id){
|
||||
size_t i;
|
||||
for(i=0;i<ids_.size();i++){
|
||||
if(id == ids_[i]){break;}
|
||||
}
|
||||
|
||||
if(i == ids_.size()){
|
||||
//unit not in memory
|
||||
}else{
|
||||
//remove unit info
|
||||
units_.erase(units_.begin()+i);
|
||||
ids_.erase(ids_.begin()+i);
|
||||
locations_.erase(locations_.begin()+i);
|
||||
turns_.erase(turns_.begin()+i);
|
||||
}
|
||||
}
|
||||
|
||||
void unit_memory::write(config& temp){
|
||||
// std::cout<<"ai_write:\n";
|
||||
for(size_t i = 0; i < units_.size(); i++){
|
||||
config element;
|
||||
write_element(i, element);
|
||||
temp.add_child("unit_memory",element);
|
||||
}
|
||||
}
|
||||
|
||||
void unit_memory::write_element(int i, config &temp){
|
||||
config temp_unit;
|
||||
std::stringstream ts,xs,ys;
|
||||
ts << turns_[i];;
|
||||
temp["turn"] = ts.str();
|
||||
xs << locations_[i].x;
|
||||
temp["x"] = xs.str();
|
||||
ys << locations_[i].y;
|
||||
temp["y"] = ys.str();
|
||||
units_[i].write(temp_unit);
|
||||
temp.add_child("unit",temp_unit);
|
||||
// std::cout<<"ai write: "<<temp_unit["description"]<<"\n";
|
||||
}
|
||||
|
||||
}//end namespace dfool
|
||||
|
|
|
@ -13,19 +13,51 @@
|
|||
|
||||
namespace dfool {
|
||||
typedef std::vector<std::string> unit_list;
|
||||
|
||||
// class target {
|
||||
// public:
|
||||
// target(config& command, unit_history u_hist, info& ai_info);
|
||||
// double value(location loc, unit& u, unit_history u_hist, info ai_info);
|
||||
// private:
|
||||
// config unit_filter_;
|
||||
// config terrain_filter_;
|
||||
// std::string hex_val_;
|
||||
// std::string number;
|
||||
// std::string id;
|
||||
// };
|
||||
|
||||
class unit_memory{
|
||||
public:
|
||||
unit_memory(const game_data& gamedata, const config& cfg);
|
||||
void add_unit_sighting(unit u, gamemap::location l, size_t t);
|
||||
void remove_unit_sighting(std::string id);
|
||||
//void purge(int turn = -1); //clean outdated entries
|
||||
void write(config& temp);
|
||||
private:
|
||||
void write_element(int i, config& temp);
|
||||
//could replace these with a single vector of memory elements
|
||||
std::vector<unit> units_;
|
||||
std::vector<std::string> ids_;
|
||||
std::vector<size_t> turns_;
|
||||
std::vector<gamemap::location> locations_;
|
||||
};
|
||||
|
||||
//an ai that keeps track of what it has "seen", does not target units
|
||||
//that it has not "seen" and does not recruit based on unseen units.
|
||||
class dfool_ai : public ai_interface {
|
||||
public:
|
||||
dfool_ai(info& i) : ai_interface(i) {}
|
||||
dfool_ai(info& i) : ai_interface(i),unit_memory_(i.gameinfo , i.teams[i.team_num-1].ai_memory()){}
|
||||
void play_turn();
|
||||
private:
|
||||
// std::map<std::string,target> target_map_;
|
||||
unit_list all_units();
|
||||
unit_list visible_units();
|
||||
unit_list my_units();
|
||||
unit_list filter_units(const config& filter,unit_list& ul, unit_map& um);
|
||||
bool moveto(config::child_list::const_iterator o, unit_map::const_iterator m);
|
||||
unit_map::iterator unit(std::string unit_id, unit_map& um);
|
||||
|
||||
unit_memory unit_memory_;
|
||||
};
|
||||
|
||||
}//end namespace dfool
|
||||
|
|
|
@ -18,6 +18,7 @@ class gamemap;
|
|||
|
||||
#include "generic_event.hpp"
|
||||
#include "pathfind.hpp"
|
||||
#include "gamestatus.hpp"
|
||||
|
||||
class ai_interface {
|
||||
public:
|
||||
|
|
Loading…
Add table
Reference in a new issue