modifications to allow ai pathfinding based upon speculative unit maps

This commit is contained in:
John W. C. McNabb 2007-03-19 04:59:34 +00:00
parent e6a1f14f99
commit d860fe4187
4 changed files with 42 additions and 4 deletions

View file

@ -619,6 +619,14 @@ void ai::attack_enemy(const location& attacking_unit, const location& target,
void ai_interface::calculate_possible_moves(std::map<location,paths>& res, move_map& srcdst,
move_map& dstsrc, bool enemy, bool assume_full_movement,
const std::set<gamemap::location>* remove_destinations)
{
unit_map units=info_.units;
calculate_moves(units,res,srcdst,dstsrc,enemy,assume_full_movement,remove_destinations);
}
void ai_interface::calculate_moves(unit_map units, std::map<location,paths>& res, move_map& srcdst,
move_map& dstsrc, bool enemy, bool assume_full_movement,
const std::set<gamemap::location>* remove_destinations)
{
for(unit_map::iterator un_it = info_.units.begin(); un_it != info_.units.end(); ++un_it) {
//if we are looking for the movement of enemies, then this unit must be an enemy unit

View file

@ -27,7 +27,6 @@ namespace dfool {
unit_list all = all_units();
unit_list my_units=filter_units(side_filter, all,get_info().units);
unit_list v_units=visible_units();
// LOG_STREAM(info, ai)<<"My Units"<<std::endl;
// for(unit_list::iterator ui = all.begin(); ui != all.end(); ++ui) {
@ -40,7 +39,8 @@ namespace dfool {
// LOG_STREAM(info, ai)<<"\t\t\t"<<u->first.x<<","<<u->first.y<<std::endl;
// }
// }
LOG_STREAM(info, ai)<<"Visible Units"<<std::endl;
LOG_STREAM(info, ai)<<"Visible Units"<<std::endl;
for(unit_list::iterator ui = v_units.begin(); ui != v_units.end(); ++ui) {
unit_map::iterator u = unit(*ui,get_info().units);
if(u!=get_info().units.end()){
@ -48,7 +48,7 @@ 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());
}
}
@ -205,7 +205,11 @@ namespace dfool {
if(m->second.movement_left()){
std::map<location,paths> possible_moves;
move_map srcdst, dstsrc;
calculate_possible_moves(possible_moves,srcdst,dstsrc,false);
unit_map known_units;
unit_memory_.known_map(known_units,0);
calculate_moves(known_units,possible_moves,srcdst,dstsrc,false);
int closest_distance = -1;
std::pair<location,location> closest_move;
@ -320,4 +324,21 @@ namespace dfool {
// std::cout<<"ai write: "<<temp_unit["description"]<<"\n";
}
void unit_memory::known_map(unit_map& u, size_t turn){
size_t i;
std::map<gamemap::location,size_t> turn_used;
for(i=0;i<units_.size();i++){
gamemap::location l = locations_[i];
size_t t = turn_used[l];
// std::cout<<"turn: "<< t <<"\n";
if(turns_[i] >= turn && turns_[i] >= t){
turn_used[l] = t;
if(t != 0){
u.replace(new std::pair<gamemap::location,unit>(l,units_[i]));
}else{
u.add(new std::pair<gamemap::location,unit>(l,units_[i]));
}
}
}
}
}//end namespace dfool

View file

@ -33,6 +33,8 @@ namespace dfool {
void remove_unit_sighting(std::string id);
//void purge(int turn = -1); //clean outdated entries
void write(config& temp);
//create a map based upon units seen since turn
void known_map(unit_map& units, size_t turn=0);
private:
void write_element(int i, config& temp);
//could replace these with a single vector of memory elements
@ -58,7 +60,9 @@ namespace dfool {
unit_map::iterator unit(std::string unit_id, unit_map& um);
unit_memory unit_memory_;
};
}//end namespace dfool
#endif

View file

@ -127,6 +127,11 @@ protected:
void calculate_possible_moves(std::map<location,paths>& possible_moves, move_map& srcdst, move_map& dstsrc, bool enemy, bool assume_full_movement=false,
const std::set<location>* remove_destinations=NULL);
/// A more fundamental version of calculate_possible_moves which allows the
/// use of a speculative unit map
void calculate_moves(unit_map units, std::map<location,paths>& possible_moves, move_map& srcdst, move_map& dstsrc, bool enemy, bool assume_full_movement=false,
const std::set<location>* remove_destinations=NULL);
///this function is used to recruit a unit. It will recruit the unit with the given name,
///at the given location, or at an available location to recruit units if 'loc' is not
///a valid recruiting location.