improvement to AI to recognize power of its allies when deciding whether to retreat

This commit is contained in:
uid68803 2004-02-04 19:04:50 +00:00
parent d07e6a721d
commit 09ffaa3c4a
3 changed files with 16 additions and 6 deletions

View file

@ -166,8 +166,12 @@ void ai::move_unit(const location& from, const location& to, std::map<location,p
void ai::calculate_possible_moves(std::map<location,paths>& res, move_map& srcdst, move_map& dstsrc, bool enemy, bool assume_full_movement)
{
for(std::map<gamemap::location,unit>::iterator un_it = units_.begin(); un_it != units_.end(); ++un_it) {
//if we are looking for the movement of enemies, then this unit must be an enemy unit
//if we are looking for movement of our own units, it must be on our side.
//if we are assuming full movement, then it may be a unit on our side, or allied
if(enemy && current_team().is_enemy(un_it->second.side()) == false ||
!enemy && un_it->second.side() != team_num_) {
!enemy && !assume_full_movement && un_it->second.side() != team_num_ ||
!enemy && assume_full_movement && current_team().is_enemy(un_it->second.side())) {
continue;
}
@ -469,9 +473,10 @@ bool ai::should_retreat(const gamemap::location& loc, const move_map& srcdst, co
bool ai::retreat_units(std::map<gamemap::location,paths>& possible_moves, const move_map& srcdst, const move_map& dstsrc, const move_map& enemy_srcdst, const move_map& enemy_dstsrc, unit_map::const_iterator leader)
{
//get versions of the move map that assume that all units are at full movement
std::map<gamemap::location,paths> dummy_possible_moves;
move_map fullmove_srcdst;
move_map fullmove_dstsrc;
calculate_possible_moves(possible_moves,fullmove_srcdst,fullmove_dstsrc,false,true);
calculate_possible_moves(dummy_possible_moves,fullmove_srcdst,fullmove_dstsrc,false,true);
for(unit_map::iterator i = units_.begin(); i != units_.end(); ++i) {
if(i->second.side() == team_num_ && i->second.movement_left() == i->second.total_movement()) {
@ -526,7 +531,7 @@ void ai::move_to_targets(std::map<gamemap::location,paths>& possible_moves, move
}
std::cerr << "choosing move...\n";
std::pair<location,location> move = choose_move(targets,dstsrc);
std::pair<location,location> move = choose_move(targets,dstsrc,enemy_srcdst,enemy_dstsrc);
for(std::vector<target>::const_iterator ittg = targets.begin(); ittg != targets.end(); ++ittg) {
assert(map_.on_board(ittg->loc));
}

View file

@ -133,7 +133,7 @@ private:
std::vector<target> find_targets(bool has_leader);
std::pair<location,location> choose_move(std::vector<target>& targets,const move_map& dstsrc);
std::pair<location,location> choose_move(std::vector<target>& targets,const move_map& dstsrc, const move_map& enemy_srcdst, const move_map& enemy_dstsrc);
display& disp_;
const gamemap& map_;

View file

@ -162,7 +162,7 @@ std::vector<ai::target> ai::find_targets(bool has_leader)
return targets;
}
std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<ai::target>& targets,const std::multimap<location,location>& dstsrc)
std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<target>& targets,const move_map& dstsrc, const move_map& enemy_srcdst, const move_map& enemy_dstsrc)
{
log_scope("choosing move");
@ -246,6 +246,11 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<ai::t
assert(map_.on_board(ittg->loc));
}
std::map<gamemap::location,paths> dummy_possible_moves;
move_map fullmove_srcdst;
move_map fullmove_dstsrc;
calculate_possible_moves(dummy_possible_moves,fullmove_srcdst,fullmove_dstsrc,false,true);
for(std::vector<location>::reverse_iterator ri =
best_route.steps.rbegin(); ri != best_route.steps.rend(); ++ri) {
@ -256,7 +261,7 @@ std::pair<gamemap::location,gamemap::location> ai::choose_move(std::vector<ai::t
typedef std::multimap<location,location>::const_iterator Itor;
std::pair<Itor,Itor> its = dstsrc.equal_range(*ri);
while(its.first != its.second) {
if(its.first->second == best->first) {
if(its.first->second == best->first && !should_retreat(its.first->first,fullmove_srcdst,fullmove_dstsrc,enemy_srcdst,enemy_dstsrc)) {
return std::pair<location,location>(its.first->second,its.first->first);
}