Author: darthfool
Date: Sat Apr  7 08:06:14 2007
New Revision: 16664

URL: http://svn.gna.org/viewcvs/wesnoth?rev=16664&view=rev
Log:
fixes to correctly handle AI that tries to move a unit without knowing what is 
in its path

Modified:
    trunk/src/ai.cpp
    trunk/src/ai_dfool.cpp
    trunk/src/ai_interface.hpp
    trunk/src/pathfind.cpp
    trunk/src/pathfind.hpp
    trunk/src/unit.cpp
    trunk/src/unit.hpp

Modified: trunk/src/ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.cpp?rev=16664&r1=16663&r2=16664&view=diff
==============================================================================
--- trunk/src/ai.cpp (original)
+++ trunk/src/ai.cpp Sat Apr  7 08:06:14 2007
@@ -374,10 +374,11 @@
                std::map<location,paths>& possible_moves)
 {
        LOG_AI << "ai_interface::move_unit " << from << " -> " << to << '\n';
+
        //stop the user from issuing any commands while the unit is moving
        const events::command_disabler disable_commands;
 
-       wassert(info_.units.find(to) == info_.units.end() || from == to);
+       //wassert(info_.units.find(to) == info_.units.end() || from == to);
 
        info_.disp.select_hex(from);
        info_.disp.update_display();
@@ -412,12 +413,23 @@
                        }
                }
 
-               if(rt != p.routes.end()) {
+               if(rt != p.routes.end()) {                
                        u_it->second.set_movement(rt->second.move_left);
 
                        std::vector<location> steps = rt->second.steps;
 
+                       while(steps.empty() == false && (!(info_.units.find(to) 
== info_.units.end() || from == to))){
+                                   LOG_AI << "AI attempting illegal move.  
Attempting to move onto existing unit\n";
+                                   LOG_AI << "\t" << 
info_.units.find(to)->second.underlying_description() <<" already on " << to << 
"\n";
+                                   LOG_AI <<"\tremoving 
"<<*(steps.end()-1)<<"\n";
+                                   to = *(steps.end()-1);
+                                   steps.pop_back();
+                                   LOG_AI << "\tresetting to " << from << " -> 
" << to << '\n';
+       
+                       }
+
                        if(steps.empty() == false) {
+
                                //check if there are any invisible units that 
we uncover
                                for(std::vector<location>::iterator i = 
steps.begin()+1; i != steps.end(); ++i) {
                                        location adj[6];
@@ -428,6 +440,8 @@
                                                //see if there is an enemy unit 
next to this tile.  If it's invisible,
                                                //we need to stop: we're 
ambushed.  If it's not, we must be a skirmisher,
                                                //otherwise AI wouldn't try.
+
+                                            //or would it?  If it doesn't 
cheat, it might...
                                                const unit_map::const_iterator 
u = info_.units.find(adj[n]);
                                                if (u != info_.units.end() && 
u->second.emits_zoc()
                                                        && 
current_team().is_enemy(u->second.side())) {
@@ -438,8 +452,17 @@
                                                        } else {
                                                          if 
(!u_it->second.get_ability_bool("skirmisher",*i)){
                                                            LOG_STREAM(err, ai) 
<< "AI tried to skirmish with non-skirmisher\n";
+                                                           LOG_AI << 
"\tresetting destination from " <<to;
                                                            to = *i;
+                                                           LOG_AI << " to " << 
to;
                                                            
steps.erase(i,steps.end());
+                                                           while(steps.empty() 
== false && (!(info_.units.find(to) == info_.units.end() || from == to))){
+                                                                to = 
*(steps.end()-1);
+                                                                
steps.pop_back();
+                                                                LOG_AI << 
"\tresetting to " << from << " -> " << to << '\n';
+                                                           }
+
+                                                           break;
                                                          }
                                                        }
                                                }
@@ -624,9 +647,12 @@
 
 void ai_interface::calculate_moves(const 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::const_iterator un_it = info_.units.begin(); un_it != 
info_.units.end(); ++un_it) {
+            const std::set<gamemap::location>* remove_destinations, 
+               bool see_all
+          )
+{
+
+       for(unit_map::const_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
@@ -635,17 +661,15 @@
                   !enemy && assume_full_movement && 
current_team().is_enemy(un_it->second.side())) {
                        continue;
                }
-
                //discount incapacitated units
                if(un_it->second.incapacitated()) {
                        continue;
                }
 
                //we can't see where invisible enemy units might move
-               if(enemy && 
un_it->second.invisible(un_it->first,info_.units,info_.teams)) {
+               if(enemy && 
un_it->second.invisible(un_it->first,units,info_.teams) && !see_all) {
                        continue;
                }
-
                //if it's an enemy unit, reset its moves while we do the 
calculations
                unit* held_unit = const_cast<unit*>(&(un_it->second));
                const unit_movement_resetter move_resetter(*held_unit,enemy || 
assume_full_movement);
@@ -656,11 +680,11 @@
                        srcdst.insert(trivial_mv);
                        dstsrc.insert(trivial_mv);
                }
-
                const bool teleports = 
un_it->second.get_ability_bool("teleport",un_it->first);
                res.insert(std::pair<gamemap::location,paths>(
-                               
un_it->first,paths(info_.map,info_.state,info_.gameinfo,info_.units,
-                                           
un_it->first,info_.teams,false,teleports,current_team())));
+                               
un_it->first,paths(info_.map,info_.state,info_.gameinfo,units,
+                                        
un_it->first,info_.teams,false,teleports,
+                                                                       
current_team(),0,see_all)));
        }
 
        for(std::map<location,paths>::iterator m = res.begin(); m != res.end(); 
++m) {
@@ -692,7 +716,7 @@
                                continue;
                        }
 
-                       if(src != dst && info_.units.find(dst) == 
info_.units.end()) {
+                       if(src != dst && units.find(dst) == units.end()) {
                                
srcdst.insert(std::pair<location,location>(src,dst));
                                
dstsrc.insert(std::pair<location,location>(dst,src));
                        }

Modified: trunk/src/ai_dfool.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_dfool.cpp?rev=16664&r1=16663&r2=16664&view=diff
==============================================================================
--- trunk/src/ai_dfool.cpp (original)
+++ trunk/src/ai_dfool.cpp Sat Apr  7 08:06:14 2007
@@ -1,5 +1,7 @@
 #include "global.hpp"
 #include "ai_dfool.hpp"
+
+#include <set>
 
 namespace dfool {
   void dfool_ai::play_turn(){
@@ -207,10 +209,10 @@
        move_map srcdst, dstsrc;
        unit_map known_units;
        
-       unit_memory_.known_map(known_units,0);
-
-       calculate_moves(known_units,possible_moves,srcdst,dstsrc,false);
-       
+       //      unit_memory_.known_map(known_units, get_info().state.turn());
+       unit_memory_.known_map(known_units, 0);
+
+       
calculate_moves(known_units,possible_moves,srcdst,dstsrc,false,false,NULL,true);
        int closest_distance = -1;
        std::pair<location,location> closest_move;
 
@@ -264,7 +266,7 @@
   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;
+    size_t i,j;
     for(i=0; i < ids_.size();i++){
       if(unit_id == ids_[i]){break;}
     }
@@ -280,8 +282,18 @@
       units_[i]=u;
       turns_[i]=t;
       locations_[i]=l;
-    }
-
+    }    
+
+    //remove units that are co-located units 
+    std::set<size_t> remove_list;
+    for(j=0; j < ids_.size();j++){
+      if(j!=i && locations_[j] == locations_[i]){
+         remove_list.insert(j);
+      }
+    }
+    for(std::set<size_t>::const_iterator 
k=remove_list.begin();k!=remove_list.end();k++){
+      remove_unit_sighting(ids_[*k]);
+    }
   }
 
   void unit_memory::remove_unit_sighting(std::string id){
@@ -330,12 +342,15 @@
     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){
+          //      std::cout<<"turn_used: "<< t <<"\n";
+        //      std::cout<<"turn: "<< turns_[i] <<"\n";
          turn_used[l] = t;
          if(t != 0){
            u.replace(new std::pair<gamemap::location,unit>(l,units_[i]));
          }else{
+           std::cout<<"id: "<< ids_[i] <<"\n";
+
            u.add(new std::pair<gamemap::location,unit>(l,units_[i]));
          }
       }

Modified: trunk/src/ai_interface.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai_interface.hpp?rev=16664&r1=16663&r2=16664&view=diff
==============================================================================
--- trunk/src/ai_interface.hpp (original)
+++ trunk/src/ai_interface.hpp Sat Apr  7 08:06:14 2007
@@ -130,7 +130,7 @@
   /// A more fundamental version of calculate_possible_moves which allows the 
   /// use of a speculative unit map
   void calculate_moves(const 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);
+          const std::set<location>* remove_destinations=NULL, bool 
see_all=false);
 
        ///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

Modified: trunk/src/pathfind.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/pathfind.cpp?rev=16664&r1=16663&r2=16664&view=diff
==============================================================================
--- trunk/src/pathfind.cpp (original)
+++ trunk/src/pathfind.cpp Sat Apr  7 08:06:14 2007
@@ -322,18 +322,19 @@
 bool enemy_zoc(gamemap const &map,
                unit_map const &units,
                std::vector<team> const &teams,
-               gamemap::location const &loc, team const &viewing_team, 
unsigned int side)
+               gamemap::location const &loc, team const &viewing_team, 
unsigned int side, bool see_all)
 {
        gamemap::location locs[6];
        const team &current_team = teams[side-1];
        get_adjacent_tiles(loc,locs);
        for(int i = 0; i != 6; ++i) {
-               const unit_map::const_iterator it
-                       = find_visible_unit(units, locs[i], map, teams, 
viewing_team);
-               if (it != units.end() && it->second.side() != side &&
-                   current_team.is_enemy(it->second.side()) && 
it->second.emits_zoc()) {
-                       return true;
-               }
+         unit_map::const_iterator it;       
+         it = find_visible_unit(units, locs[i], map, teams, 
viewing_team,see_all);
+
+         if (it != units.end() && it->second.side() != side &&
+                current_team.is_enemy(it->second.side()) && 
it->second.emits_zoc()) {
+           return true;
+         }
        }
 
        return false;
@@ -348,7 +349,7 @@
                std::map<gamemap::location,paths::route>& routes,
                std::vector<team> const &teams,
                bool force_ignore_zocs, bool allow_teleport, int turns_left,
-           bool starting_pos, const team &viewing_team)
+                 bool starting_pos, const team &viewing_team, bool see_all)
        {
                if(size_t(u.side()-1) >= teams.size()) {
                        return;
@@ -365,7 +366,7 @@
                if (allow_teleport && map.is_village(loc) &&
                    current_team.owns_village(loc) &&
                        (starting_pos || find_visible_unit(units, loc, map,
-                                                                               
           teams, viewing_team) == units.end())) {
+                                                                               
teams, viewing_team,see_all) == units.end())) {
                        const std::vector<gamemap::location>& villages = 
map.villages();
 
                        //if we are on a village, see all friendly villages 
that we can
@@ -390,7 +391,7 @@
 
                        //see if the tile is on top of an enemy unit
                        const unit_map::const_iterator unit_it =
-                               find_visible_unit(units, locs[i], map, teams, 
viewing_team);
+                         find_visible_unit(units, locs[i], map, teams, 
viewing_team,see_all);
 
                        if (unit_it != units.end() &&
                            current_team.is_enemy(unit_it->second.side()))
@@ -420,7 +421,7 @@
                                        continue;
 
                                const bool skirmisher = force_ignore_zocs | 
u.get_ability_bool("skirmisher",currentloc);
-                               const bool zoc = !skirmisher && 
enemy_zoc(map,units,teams,currentloc, viewing_team,u.side());
+                               const bool zoc = !skirmisher && 
enemy_zoc(map,units,teams,currentloc, viewing_team,u.side(),see_all);
                                paths::route new_route = routes[loc];
                                new_route.steps.push_back(loc);
 
@@ -431,7 +432,7 @@
                                if (new_route.move_left > 0) {
                                        find_routes(map, status, gamedata, 
units, u, currentloc,
                                                    zoc_move_left, routes, 
teams, force_ignore_zocs,
-                                                   allow_teleport, 
new_turns_left, false, viewing_team);
+                                                   allow_teleport, 
new_turns_left, false, viewing_team, see_all);
                                }
                        }
                }
@@ -444,7 +445,7 @@
              std::vector<team> const &teams,
             bool force_ignore_zoc,
              bool allow_teleport, const team &viewing_team,
-                        int additional_turns)
+                  int additional_turns, bool see_all)
 {
        const unit_map::const_iterator i = units.find(loc);
        if(i == units.end()) {
@@ -455,7 +456,7 @@
        routes[loc].move_left = i->second.movement_left();
        find_routes(map,status,gamedata,units,i->second,loc,
                i->second.movement_left(),routes,teams,force_ignore_zoc,
-               allow_teleport,additional_turns,true,viewing_team);
+               allow_teleport,additional_turns,true,viewing_team, see_all);
 }
 
 int route_turns_to_complete(unit const &u, gamemap const &map, paths::route 
&rt)

Modified: trunk/src/pathfind.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/pathfind.hpp?rev=16664&r1=16663&r2=16664&view=diff
==============================================================================
--- trunk/src/pathfind.hpp (original)
+++ trunk/src/pathfind.hpp Sat Apr  7 08:06:14 2007
@@ -58,7 +58,7 @@
 bool enemy_zoc(gamemap const &map,
                unit_map const &units,
                std::vector<team> const &teams, gamemap::location const &loc,
-               team const &viewing_team, unsigned int side);
+               team const &viewing_team, unsigned int side, bool 
see_all=false);
 
 struct cost_calculator
 {
@@ -85,7 +85,8 @@
              unit_map const &units,
              gamemap::location const &loc, std::vector<team> const &teams,
              bool force_ignore_zocs,bool allow_teleport,
-                 const team &viewing_team,int additional_turns = 0);
+                const team &viewing_team,int additional_turns = 0,
+                bool see_all = false);
 
        //structure which holds a single route between one location and another.
        struct route

Modified: trunk/src/unit.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=16664&r1=16663&r2=16664&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Sat Apr  7 08:06:14 2007
@@ -3095,10 +3095,11 @@
 unit_map::iterator find_visible_unit(unit_map& units,
                const gamemap::location loc,
                const gamemap& map,
-               const std::vector<team>& teams, const team& current_team)
+          const std::vector<team>& teams, const team& current_team,
+               bool see_all)
 {
        unit_map::iterator u = units.find(loc);
-       if(map.on_board(loc)){
+       if(map.on_board(loc) && !see_all){
                if(u != units.end()){
                        if(current_team.fogged(loc.x, loc.y)){
                                return units.end();
@@ -3115,10 +3116,11 @@
 unit_map::const_iterator find_visible_unit(const unit_map& units,
                const gamemap::location loc,
                const gamemap& map,
-               const std::vector<team>& teams, const team& current_team)
+               const std::vector<team>& teams, const team& current_team, 
+               bool see_all)
 {
        unit_map::const_iterator u = units.find(loc);
-       if(map.on_board(loc)){
+       if(map.on_board(loc) && !see_all){
                if(u != units.end()){
                        if(current_team.fogged(loc.x, loc.y)){
                                return units.end();

Modified: trunk/src/unit.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.hpp?rev=16664&r1=16663&r2=16664&view=diff
==============================================================================
--- trunk/src/unit.hpp (original)
+++ trunk/src/unit.hpp Sat Apr  7 08:06:14 2007
@@ -449,11 +449,13 @@
 unit_map::iterator find_visible_unit(unit_map& units,
                const gamemap::location loc,
                const gamemap& map,
-               const std::vector<team>& teams, const team& current_team);
+               const std::vector<team>& teams, const team& current_team, 
+               bool see_all=false);
 unit_map::const_iterator find_visible_unit(const unit_map& units,
                const gamemap::location loc,
                const gamemap& map,
-               const std::vector<team>& teams, const team& current_team);
+               const std::vector<team>& teams, const team& current_team,
+               bool see_all=false);
 
 struct team_data
 {


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to