mail[Wesnoth-commits] r24928 - in /trunk: data/ src/


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

Header


Content

Posted by dave on March 21, 2008 - 07:58:
Author: dave
Date: Fri Mar 21 07:58:19 2008
New Revision: 24928

URL: http://svn.gna.org/viewcvs/wesnoth?rev=24928&view=rev
Log:
some fixes to unit specific formulas

Modified:
    trunk/data/scenario-formula.cfg
    trunk/src/callable_objects.cpp
    trunk/src/callable_objects.hpp
    trunk/src/formula_ai.cpp
    trunk/src/formula_callable.hpp
    trunk/src/formula_function.cpp
    trunk/src/unit.cpp
    trunk/src/unit.hpp
    trunk/src/variant.cpp

Modified: trunk/data/scenario-formula.cfg
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/scenario-formula.cfg?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/data/scenario-formula.cfg (original)
+++ trunk/data/scenario-formula.cfg Fri Mar 21 07:58:19 2008
@@ -46,7 +46,7 @@
         canrecruit=1
         recruit=Dwarvish Guardsman,Dwarvish Fighter,Dwarvish 
Thunderer,Thief,Poacher,Footpad
         gold=100
-        #controller=human
+        controller=human
     [/side]
 
     [side]
@@ -58,27 +58,17 @@
         gold=100
 
         [unit]
-            x,y=4,23
+            x,y=8,5
             type="Orcish Archer"
             generate_description=yes
-               formula="recruit('Skeleton Archer')"    
+                       formula="if(attack, attack, move(me.loc, 
choose(unit_moves(me.loc), -distance_between(self, me.vars.guard_loc))))
+                       where attack = choose(filter(attacks, units = 
[me.loc] and distance_between(me.vars.guard_loc, target) <= 
me.vars.guard_radius), avg_damage_inflicted)"
                [ai_vars]
-                       number=2
-                       text="'some string'"
-                       list=[ 2, 3, 4]
+                       guard_radius=3
+                       guard_loc="loc(8,5)"
                [/ai_vars]
         [/unit]
-        [unit]
-            x,y=16,23
-            type="Orcish Archer"
-            generate_description=yes
-               formula="move(me.loc, loc(me.loc.x, me.loc.y - 1))"
-               [ai_vars]
-                       number=2
-                       text="'some string'"
-                       list=[ 2, 3, 4]
-               [/ai_vars]
-        [/unit]
+
         ai_algorithm=formula_ai
         [ai]
             [function]

Modified: trunk/src/callable_objects.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/callable_objects.cpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/callable_objects.cpp (original)
+++ trunk/src/callable_objects.cpp Fri Mar 21 07:58:19 2008
@@ -43,6 +43,13 @@
        }
 
        return loc_.y - other_loc.y;
+}
+
+void location_callable::serialize_to_string(std::string& str) const
+{
+       std::ostringstream s;
+       s << "loc(" << loc_.x << "," << loc_.y << ")";
+       str = s.str();
 }
 
 variant move_map_callable::get_value(const std::string& key) const
@@ -106,7 +113,11 @@
        } else if(key == "value") {
                return variant(u_.cost());
        } else if(key == "vars") {
-               return variant(u_.formula_vars());
+               if(u_.formula_vars()) {
+                       return variant(u_.formula_vars().get());
+               } else {
+                       return variant();
+               }
        } else {
                return variant();
        }

Modified: trunk/src/callable_objects.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/callable_objects.hpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/callable_objects.hpp (original)
+++ trunk/src/callable_objects.hpp Fri Mar 21 07:58:19 2008
@@ -54,6 +54,8 @@
        {}
 
        const gamemap::location& loc() const { return loc_; }
+
+       void serialize_to_string(std::string& str) const;
 };
 
 class move_callable : public game_logic::formula_callable {

Modified: trunk/src/formula_ai.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.cpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Fri Mar 21 07:58:19 2008
@@ -80,9 +80,9 @@
                const args_list& arguments = args();
                const expression_ptr& exp_p = arguments[0];
                variant my_variant = exp_p->evaluate(variables);
-               const location_callable* loc1 = 
convert_variant<location_callable>(args()[0]->evaluate(variables));
-               const location_callable* loc2 = 
convert_variant<location_callable>(args()[1]->evaluate(variables));
-               return variant(distance_between(loc1->loc(), loc2->loc()));
+               const gamemap::location loc1 = 
convert_variant<location_callable>(args()[0]->evaluate(variables))->loc();
+               const gamemap::location loc2 = 
convert_variant<location_callable>(args()[1]->evaluate(variables))->loc();
+               return variant(distance_between(loc1, loc2));
        }
 };
 
@@ -314,17 +314,6 @@
        }
 
        const formula_ai& ai_;
-};
-
-class loc_function : public function_expression {
-public:
-       explicit loc_function(const args_list& args)
-         : function_expression("loc", args, 2, 2)
-       {}
-private:
-       variant execute(const formula_callable& variables) const {
-               return variant(new 
location_callable(gamemap::location(args()[0]->evaluate(variables).as_int()-1,
 args()[1]->evaluate(variables).as_int()-1)));
-       }
 };
 
 class is_village_function : public function_expression {
@@ -493,8 +482,6 @@
                        return expression_ptr(new recruit_function(args));
                } else if(fn == "is_village") {
                        return expression_ptr(new is_village_function(args));
-               } else if(fn == "loc") {
-                       return expression_ptr(new loc_function(args));
                } else if(fn == "unit_at") {
                        return expression_ptr(new unit_at_function(args, 
ai_));
                } else if(fn == "unit_moves") {
@@ -628,6 +615,8 @@
 bool formula_ai::make_move(game_logic::const_formula_ptr formula_, const 
game_logic::formula_callable& variables)
 {
        if(!formula_) {
+               ai_interface* fallback = create_ai("", get_info());
+               fallback->play_turn();
                return false;
        }
 
@@ -653,6 +642,7 @@
                
                const move_callable* move = 
try_convert_variant<move_callable>(*i);
                const attack_callable* attack = 
try_convert_variant<attack_callable>(*i);
+               const ai::attack_analysis* attack_analysis = 
try_convert_variant<ai::attack_analysis>(*i);
                const recruit_callable* recruit_command = 
try_convert_variant<recruit_callable>(*i);
                const set_var_callable* set_var_command = 
try_convert_variant<set_var_callable>(*i);
                const fallback_callable* fallback_command = 
try_convert_variant<fallback_callable>(*i);
@@ -677,6 +667,29 @@
                        }
                        std::cerr << "ATTACK: " << attack->src() << " -> " << 
attack->dst() << " " << attack->weapon() << "\n";
                        attack_enemy(attack->src(), attack->dst(), 
attack->weapon(), attack->defender_weapon());
+                       made_move = true;
+               } 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);
+
+                       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,
+                                                 get_info().units, 
get_info().state,
+                                                                 
get_info().gameinfo,
+                                                 src, dst, -1, -1, 1.0, NULL,
+                                                                 
&get_info().units.find(src)->second);
+                               
attack_enemy(attack_analysis->movements.front().second,
+                                            attack_analysis->target,
+                                                        
bc.get_attacker_stats().attack_num,
+                                                        
bc.get_defender_stats().attack_num);
+                       }
                        made_move = true;
                } else if(recruit_command) {
                        std::cerr << "RECRUIT: '" << recruit_command->type() 
<< "'\n";

Modified: trunk/src/formula_callable.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_callable.hpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/formula_callable.hpp (original)
+++ trunk/src/formula_callable.hpp Fri Mar 21 07:58:19 2008
@@ -65,12 +65,21 @@
        }
 
        virtual void get_inputs(std::vector<formula_input>* inputs) const {};
+
+       void serialize(std::string& str) const {
+               serialize_to_string(str);
+       }
+
 protected:
        virtual ~formula_callable() {}
 
        virtual void set_value(const std::string& key, const variant& value);
        virtual int do_compare(const formula_callable* callable) const {
                return this < callable ? -1 : (this == callable ? 0 : 1);
+       }
+
+       virtual void serialize_to_string(std::string& str) const {
+               throw type_error("Tried to serialize type which cannot be 
serialized");
        }
 private:
        virtual variant get_value(const std::string& key) const = 0;

Modified: trunk/src/formula_function.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_function.cpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/formula_function.cpp (original)
+++ trunk/src/formula_function.cpp Fri Mar 21 07:58:19 2008
@@ -18,8 +18,10 @@
 #include <math.h>
 
 //#include "foreach.hpp"
+#include "callable_objects.hpp"
 #include "formula_callable.hpp"
 #include "formula_function.hpp"
+#include "map.hpp"
 
 #include "SDL.h"
 
@@ -277,7 +279,7 @@
                }
 
                if(max_index == -1) {
-                       return variant(0);
+                       return variant();
                } else {
                        return items[max_index];
                }
@@ -510,6 +512,17 @@
 private:
        variant execute(const formula_callable& variables) const {
                return variant(args()[0]->evaluate(variables).refcount());
+       }
+};
+
+class loc_function : public function_expression {
+public:
+       explicit loc_function(const args_list& args)
+         : function_expression("loc", args, 2, 2)
+       {}
+private:
+       variant execute(const formula_callable& variables) const {
+               return variant(new 
location_callable(gamemap::location(args()[0]->evaluate(variables).as_int()-1,
 args()[1]->evaluate(variables).as_int()-1)));
        }
 };
 
@@ -634,6 +647,7 @@
                FUNCTION(size);
                FUNCTION(null);
                FUNCTION(refcount);
+               FUNCTION(loc);
 #undef FUNCTION
        }
 

Modified: trunk/src/unit.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Fri Mar 21 07:58:19 2008
@@ -116,7 +116,7 @@
            alpha_(o.alpha_),
 
            unit_formula_(o.unit_formula_),
-           formula_vars_(o.formula_vars_),
+           formula_vars_(o.formula_vars_ ? new 
game_logic::map_formula_callable(*o.formula_vars_) : o.formula_vars_),
 
            recruits_(o.recruits_),
 
@@ -1325,15 +1325,17 @@
        const config* ai_vars = cfg.child("ai_vars");
        if (ai_vars)
        {               
-               formula_vars_.clear();
+               formula_vars_ = new game_logic::map_formula_callable;
                
                variant var;
                for(string_map::const_iterator i = ai_vars->values.begin(); i 
!= ai_vars->values.end(); ++i) 
                { 
                        var.serialize_from_string(i->second);
-                       formula_vars_.add(i->first, var);
-               }
-       }       
+                       formula_vars_->add(i->first, var);
+               }
+       } else {
+               formula_vars_ = game_logic::map_formula_callable_ptr();
+       }
        //remove ai_vars from private cfg 
        cfg_.clear_children("ai_vars");
 
@@ -1510,13 +1512,13 @@
                cfg["formula"] = unit_formula_;
 
 
-       if (!formula_vars_.empty())
+       if (formula_vars_ && formula_vars_->empty() == false)
        {
                cfg.add_child("ai_vars");
                config* ai_vars = cfg.child("ai_vars");
 
                std::string str;
-               for(game_logic::map_formula_callable::const_iterator i = 
formula_vars_.begin(); i != formula_vars_.end(); ++i) 
+               for(game_logic::map_formula_callable::const_iterator i = 
formula_vars_->begin(); i != formula_vars_->end(); ++i) 
                { 
                        i->second.serialize_to_string(str);
                        if (!str.empty())

Modified: trunk/src/unit.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.hpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/unit.hpp (original)
+++ trunk/src/unit.hpp Fri Mar 21 07:58:19 2008
@@ -259,7 +259,7 @@
        bool has_ability_type(const std::string& ability) const;
        bool abilities_affects_adjacent() const;
 
-       const game_logic::map_formula_callable* formula_vars() const { return 
&formula_vars_; }
+       const game_logic::map_formula_callable_ptr& formula_vars() const { 
return formula_vars_; }
        bool has_formula() const { return !unit_formula_.empty(); }
        const std::string& get_formula() const { return unit_formula_; }
 
@@ -330,7 +330,7 @@
        fixed_t alpha_;
 
        std::string unit_formula_;
-       game_logic::map_formula_callable formula_vars_;
+       game_logic::map_formula_callable_ptr formula_vars_;
 
        std::vector<std::string> recruits_;
 

Modified: trunk/src/variant.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/variant.cpp?rev=24928&r1=24927&r2=24928&view=diff
==============================================================================
--- trunk/src/variant.cpp (original)
+++ trunk/src/variant.cpp Fri Mar 21 07:58:19 2008
@@ -419,7 +419,7 @@
                str += boost::lexical_cast<std::string>(int_value_);
                break;
        case TYPE_CALLABLE:
-               std::cerr << "ERROR: attempt to serialize callable variant\n";
+               callable_->serialize(str);
                break;
        case TYPE_LIST: {
                str += "[";
@@ -447,7 +447,11 @@
 
 void variant::serialize_from_string(const std::string& str)
 {
-       *this = game_logic::formula(str).execute();
+       try {
+               *this = game_logic::formula(str).execute();
+       } catch(...) {
+               *this = variant(str);
+       }
 }
 
 int variant::refcount() const




Related Messages


Powered by MHonArc, Updated Fri Mar 21 10:22:46 2008