Author: dragonking
Date: Tue Sep 16 20:41:30 2008
New Revision: 29493

URL: http://svn.gna.org/viewcvs/wesnoth?rev=29493&view=rev
Log:
Fixed bug #11916

Modified:
    trunk/src/formula_ai.cpp

Modified: trunk/src/formula_ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.cpp?rev=29493&r1=29492&r2=29493&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Tue Sep 16 20:41:30 2008
@@ -1499,9 +1499,33 @@
                prepare_move();
                if(move) {
                        LOG_AI << "MOVE: " << move->src().x << "," << 
move->src().y << " -> " << move->dst().x << "," << move->dst().y << "\n";
-                       unit_map::iterator i = units_.find(move->src());
-                       if( (possible_moves_.count(move->src()) > 0) && 
(i->second.movement_left() != 0) ) {
-                               move_unit(move->src(), move->dst(), 
possible_moves_);
+                       unit_map::iterator unit_it = units_.find(move->src());
+                       if( (possible_moves_.count(move->src()) > 0) && 
(unit_it->second.movement_left() != 0) ) {
+                               std::map<gamemap::location,paths>::iterator 
path = possible_moves_.find(move->src());
+
+                               gamemap::location destination = move->dst();
+
+                               //check if destination is within unit's reach
+                               if( path->second.routes.count(move->dst()) == 
0) {
+                                       //destination is too far, check where 
unit can go
+                                       shortest_path_calculator 
calc(unit_it->second, current_team(), units_, get_info().teams, get_info().map);
+                                       paths::route route = 
a_star_search(move->src(), move->dst(), 1000.0, &calc,
+                                               get_info().map.w(), 
get_info().map.h());
+
+                                       int movement = 
unit_it->second.movement_left();
+                                       
+                                       for 
(std::vector<gamemap::location>::const_iterator loc_iter = route.steps.begin() 
+ 1 ; loc_iter !=route.steps.end(); ++loc_iter) {
+                                               const int move_cost = 
unit_it->second.movement_cost(get_info().map[*(loc_iter+1)]);
+                               
+                                               if ( move_cost > movement ) {
+                                                       break;
+                                               }
+                                               destination = *loc_iter;
+                                               movement -= move_cost;
+                                       }
+                               }
+
+                               move_unit(move->src(), destination, 
possible_moves_);
                                made_move = true;
                        }
                } else if(attack) {


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

Reply via email to