mail[Wesnoth-commits] r25087 - in /trunk/src: formula_ai.cpp formula_ai.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by dragonking on March 25, 2008 - 02:39:
Author: dragonking
Date: Tue Mar 25 02:39:17 2008
New Revision: 25087

URL: http://svn.gna.org/viewcvs/wesnoth?rev=25087&view=rev
Log:
Made formula command line more powerful: now funtions executed in it can 
affect game

Modified:
    trunk/src/formula_ai.cpp
    trunk/src/formula_ai.hpp

Modified: trunk/src/formula_ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.cpp?rev=25087&r1=25086&r2=25087&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Tue Mar 25 02:39:17 2008
@@ -612,7 +612,15 @@
        }
 
        game_logic::formula f(formula_str, &function_table);
-       const variant v = f.execute(*this);
+
+       game_logic::map_formula_callable callable(this);
+       callable.add_ref();
+
+       const variant v = f.execute(callable);  
+
+       if ( execute_variant(v, true ) )
+               return "Made move: " + v.to_debug_string();
+
        return v.to_debug_string();
 }
 
@@ -668,7 +676,13 @@
 
        std::cerr << "do move...\n";
        const variant var = formula_->execute(variables);
-       //const variant var = formula_->execute(*this);
+
+       return execute_variant(var);
+}
+
+//commandline=true when we evaluate formula from commandline, false 
otherwise (default)
+bool formula_ai::execute_variant(const variant& var, bool commandline)
+{
        std::vector<variant> vars;
        if(var.is_list()) {
                for(int n = 0; n != var.num_elements(); ++n) {
@@ -679,6 +693,7 @@
        }
 
        bool made_move = false;
+
        for(std::vector<variant>::const_iterator i = vars.begin(); i != 
vars.end(); ++i) {
                if(i->is_null()) {
                        continue;
@@ -693,7 +708,7 @@
 
                prepare_move();
                if(move) {
-                       std::cerr << "moving " << move->src().x << "," << 
move->src().y << " -> " << move->dst().x << "," << move->dst().y << "\n";
+                       std::cerr << "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_);
@@ -706,7 +721,7 @@
                                //ordered to move so it can get a different 
command.
                                continue;
                        }
-
+                       
                        if(attack->move_from() != attack->src()) {
                                move_unit(attack->move_from(), attack->src(), 
possible_moves_);
                        }
@@ -716,13 +731,29 @@
                } else if(attack_analysis) {
                        //If we get an attack analysis back we will do the 
first attack.
                        //Then the AI can get run again and re-choose.
-                       assert(attack_analysis->movements.empty() == false);
+                       assert(attack_analysis->movements.empty() == false);  
                  
+
+                       //make sure that unit which has to attack is at given 
position and is able to attack
+                       unit_map::const_iterator unit = 
units_.find(attack_analysis->movements.front().first);
+                       if ( ( unit == units_.end() ) || 
(unit->second.attacks_left() == 0) )
+                               continue;
+
+                       const gamemap::location& src = 
attack_analysis->movements.front().second;
+                       const gamemap::location& dst = 
attack_analysis->target;
+
+                       //now check if location to which we want to move is 
still unoccupied
+                       unit = units_.find(src);
+                       if ( unit != units_.end() )
+                               continue;
+
+                       //now check if target is still valid
+                       unit = units_.find(dst);
+                       if ( unit == units_.end() )
+                               continue;
 
                        move_unit(attack_analysis->movements.front().first,
                                  attack_analysis->movements.front().second,
                                          possible_moves_);
-                       const gamemap::location& src = 
attack_analysis->movements.front().second;
-                       const gamemap::location& dst = 
attack_analysis->target;
 
                        if(get_info().units.count(src)) {
                                battle_context bc(get_info().map, 
get_info().teams,
@@ -757,13 +788,15 @@
                        }
                        return false;
                } else {
-                       std::cerr << "UNRECOGNIZED MOVE: " << 
i->to_debug_string() << "\n";
-               }
-       }
-
-
+                       //this information is unneded when evaluating 
formulas form commandline
+                       if (!commandline)
+                               std::cerr << "UNRECOGNIZED MOVE: " << 
i->to_debug_string() << "\n";
+               }
+       }
+       
        return made_move;
 }
+
 
 void formula_ai::do_recruitment()
 {

Modified: trunk/src/formula_ai.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.hpp?rev=25087&r1=25086&r2=25087&view=diff
==============================================================================
--- trunk/src/formula_ai.hpp (original)
+++ trunk/src/formula_ai.hpp Tue Mar 25 02:39:17 2008
@@ -45,6 +45,7 @@
 private:
        void do_recruitment();
        bool make_move(game_logic::const_formula_ptr formula_, const 
game_logic::formula_callable& variables);
+       bool execute_variant(const variant& var, bool commandline=false);
        virtual variant get_value(const std::string& key) const;
        virtual void get_inputs(std::vector<game_logic::formula_input>* 
inputs) const;
        game_logic::const_formula_ptr recruit_formula_;




Related Messages


Powered by MHonArc, Updated Tue Mar 25 03:01:49 2008