Author: suokko
Date: Mon Sep  1 14:52:49 2008
New Revision: 29164

URL: http://svn.gna.org/viewcvs/wesnoth?rev=29164&view=rev
Log:
Fixed crash when ai moves units next to level 0 hiden unit (bug #12252)

Modified:
    trunk/changelog
    trunk/src/ai.cpp
    trunk/src/unit.cpp
    trunk/src/unit.hpp

Modified: trunk/changelog
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/changelog?rev=29164&r1=29163&r2=29164&view=diff
==============================================================================
--- trunk/changelog (original)
+++ trunk/changelog Mon Sep  1 14:52:49 2008
@@ -55,6 +55,7 @@
    * Fixed OOS bug when giving control and having move in undo stack.
    * Fixed [modify_turns] not updating $turn_number when current= was
      used.
+   * Fixed crash when ai moves units next to level 0 hiden unit (bug #12252)
    * Fixed a few inconsistencies related to scenarios which are not at
      turn 1 at the beginning (namely start autosave detection and initial
      triggering of a matching "turn *" event).

Modified: trunk/src/ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ai.cpp?rev=29164&r1=29163&r2=29164&view=diff
==============================================================================
--- trunk/src/ai.cpp (original)
+++ trunk/src/ai.cpp Mon Sep  1 14:52:49 2008
@@ -538,15 +538,15 @@
                                    LOG_AI << "\tresetting to " << from << " -> 
" << to << '\n';
                        }
 
-                       if(steps.size()>1) { // First step is starting hex
-                         unit_map::const_iterator 
utest=info_.units.find(*(steps.begin()+1));
+                       if(steps.size()) { // First step is starting hex
+                         unit_map::const_iterator 
utest=info_.units.find(*(steps.begin()));
                          if(utest != info_.units.end() && 
current_team().is_enemy(utest->second.side())){
                            LOG_STREAM(err, ai) << "AI tried to move onto 
existing enemy unit at"<<*(steps.begin())<<"\n";
                            //                      return(from);
                          }
 
                                // Check if there are any invisible units that 
we uncover
-                               for(std::vector<location>::iterator i = 
steps.begin()+1; i != steps.end(); ++i) {
+                               for(std::vector<location>::iterator i = 
steps.begin(); i != steps.end(); ++i) {
                                        location adj[6];
                                        get_adjacent_tiles(*i,adj);
 
@@ -559,10 +559,12 @@
 
                                                // 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()
+                                               // If level 0 is invisible it 
ambush us too
+                                               if (u != info_.units.end() && 
(u->second.emits_zoc() || u->second.invisible(adj[n], info_.units, info_.teams))
                                                        && 
current_team().is_enemy(u->second.side())) {
                                                        if 
(u->second.invisible(adj[n], info_.units, info_.teams)) {
                                                                to = *i;
+                                                               
u->second.ambush(adj[n]);
                                                                
steps.erase(i,steps.end());
                                                                break;
                                                        } else {

Modified: trunk/src/unit.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=29164&r1=29163&r2=29164&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Mon Sep  1 14:52:49 2008
@@ -3000,6 +3000,18 @@
        max_experience_ = std::max<int>(1, (max_experience_ * exp_accel + 
50)/100);
 }
 
+void unit::ambush(const gamemap::location& loc) const
+{
+       invisibility_cache_.find(loc);
+       std::vector<const unit *>::iterator itor =
+       std::find(units_with_cache.begin(), units_with_cache.end(), this);
+
+       if(itor != units_with_cache.end()) {
+               units_with_cache.erase(itor);
+       }
+
+}
+
 bool unit::invisible(const gamemap::location& loc,
                const unit_map& units,const std::vector<team>& teams, bool 
see_all) const
 {

Modified: trunk/src/unit.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.hpp?rev=29164&r1=29163&r2=29164&view=diff
==============================================================================
--- trunk/src/unit.hpp (original)
+++ trunk/src/unit.hpp Mon Sep  1 14:52:49 2008
@@ -302,6 +302,12 @@
        bool invisible(const gamemap::location& loc,
                const unit_map& units,const std::vector<team>& teams, bool 
see_all=true) const;
 
+       /**
+        * Make sure that invisibility cache is revalidate
+        * after ambush;
+        **/
+       void ambush(const gamemap::location& loc) const;
+
        unit_race::GENDER generate_gender(const unit_type& type, bool gen, 
game_state* state = 0);
        std::string image_mods() const;
 


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

Reply via email to