ai/formula/callable_objects.hpp

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2009 - 2012 by Bartosz Waresiak <dragonking@o2.pl>
00003    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published by
00007    the Free Software Foundation; either version 2 of the License, or
00008    (at your option) any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 #ifndef FORMULA_AI_CALLABLE_OBJECTS_HPP_INCLUDED
00016 #define FORMULA_AI_CALLABLE_OBJECTS_HPP_INCLUDED
00017 
00018 #include "../game_info.hpp"
00019 #include "../../actions.hpp"
00020 #include "../../callable_objects.hpp"
00021 #include "../../formula.hpp"
00022 #include "../../formula_callable.hpp"
00023 
00024 namespace ai {
00025     class formula_ai;
00026 }
00027 
00028 namespace game_logic {
00029 
00030 class attack_map_callable : public formula_callable {
00031 public:
00032     typedef std::multimap<map_location, map_location> move_map;
00033     attack_map_callable(const ai::formula_ai& ai, const move_map& srcdst, const unit_map& units)
00034         : srcdst_(srcdst), units_(units), ai_(ai)
00035     {}
00036 private:
00037     const move_map& srcdst_;
00038     const unit_map& units_;
00039     const ai::formula_ai& ai_;
00040 
00041     variant get_value(const std::string& key) const;
00042 
00043     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00044 
00045     /* add to vars all attacks on enemy units around <attack_position> tile. attacker_location is tile where unit is currently standing. It's moved to attack_position first and then performs attack.*/
00046     void collect_possible_attacks(std::vector<variant>& vars, map_location attacker_location, map_location attack_position) const;
00047 };
00048 
00049 class attack_callable : public formula_callable {
00050     map_location move_from_, src_, dst_;
00051     battle_context bc_;
00052     variant get_value(const std::string& key) const;
00053 
00054     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00055 public:
00056     attack_callable(const map_location& move_from,
00057                 const map_location& src, const map_location& dst, int weapon);
00058 
00059     const map_location& move_from() const { return move_from_; }
00060     const map_location& src() const { return src_; }
00061     const map_location& dst() const { return dst_; }
00062     int weapon() const { return bc_.get_attacker_stats().attack_num; }
00063     int defender_weapon() const { return bc_.get_defender_stats().attack_num; }
00064 
00065     /** Compare two attacks in deterministic way or compare pointers
00066      * (nondeterministic in consequent game runs) if method argument is not
00067      * attack_callable */
00068     int do_compare(const game_logic::formula_callable* callable) const;
00069 };
00070 
00071 
00072 class move_callable : public game_logic::formula_callable {
00073     map_location src_, dst_;
00074     variant get_value(const std::string& key) const {
00075         if(key == "src") {
00076             return variant(new location_callable(src_));
00077         } else if(key == "dst") {
00078             return variant(new location_callable(dst_));
00079         } else {
00080             return variant();
00081         }
00082     }
00083     void get_inputs(std::vector<game_logic::formula_input>* inputs) const {
00084         inputs->push_back(game_logic::formula_input("src", game_logic::FORMULA_READ_ONLY));
00085         inputs->push_back(game_logic::formula_input("dst", game_logic::FORMULA_READ_ONLY));
00086     }
00087 
00088     int do_compare(const formula_callable* callable) const;
00089 public:
00090     move_callable(const map_location& src, const map_location& dst) :
00091       src_(src), dst_(dst)
00092     {
00093         type_ = MOVE_C;
00094     }
00095 
00096     const map_location& src() const { return src_; }
00097     const map_location& dst() const { return dst_; }
00098 };
00099 
00100 
00101 class move_partial_callable : public game_logic::formula_callable {
00102     map_location src_, dst_;
00103     variant get_value(const std::string& key) const {
00104         if(key == "src") {
00105             return variant(new location_callable(src_));
00106         } else if(key == "dst") {
00107             return variant(new location_callable(dst_));
00108         } else {
00109             return variant();
00110         }
00111     }
00112     void get_inputs(std::vector<game_logic::formula_input>* inputs) const {
00113         inputs->push_back(game_logic::formula_input("src", game_logic::FORMULA_READ_ONLY));
00114         inputs->push_back(game_logic::formula_input("dst", game_logic::FORMULA_READ_ONLY));
00115     }
00116 
00117     int do_compare(const formula_callable* callable) const;
00118 public:
00119     move_partial_callable(const map_location& src, const map_location& dst) :
00120       src_(src), dst_(dst)
00121     {
00122         type_ = MOVE_PARTIAL_C;
00123     }
00124 
00125     const map_location& src() const { return src_; }
00126     const map_location& dst() const { return dst_; }
00127 };
00128 
00129 
00130 
00131 class recall_callable : public formula_callable {
00132     map_location loc_;
00133     std::string id_;
00134 
00135     variant get_value(const std::string& key) const;
00136 
00137     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00138 public:
00139     recall_callable(const map_location& loc, const std::string& id)
00140       : loc_(loc), id_(id)
00141     {}
00142 
00143     const map_location& loc() const { return loc_; }
00144     const std::string& id() const { return id_; }
00145 };
00146 
00147 
00148 class recruit_callable : public formula_callable {
00149     map_location loc_;
00150     std::string type_;
00151 
00152     variant get_value(const std::string& key) const;
00153 
00154     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00155 public:
00156     recruit_callable(const map_location& loc, const std::string& type)
00157       : loc_(loc), type_(type)
00158     {}
00159 
00160     const map_location& loc() const { return loc_; }
00161     const std::string& type() const { return type_; }
00162 };
00163 
00164 
00165 class set_var_callable : public formula_callable {
00166     std::string key_;
00167     variant value_;
00168     variant get_value(const std::string& key) const;
00169 
00170     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00171 public:
00172     set_var_callable(const std::string& key, const variant& value)
00173       : key_(key), value_(value)
00174     {}
00175 
00176     const std::string& key() const { return key_; }
00177     variant value() const { return value_; }
00178 };
00179 
00180 
00181 class set_unit_var_callable : public formula_callable {
00182     std::string key_;
00183     variant value_;
00184     map_location loc_;
00185     variant get_value(const std::string& key) const;
00186 
00187     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00188 public:
00189     set_unit_var_callable(const std::string& key, const variant& value, const map_location& loc)
00190       : key_(key), value_(value), loc_(loc)
00191     {}
00192 
00193     const std::string& key() const { return key_; }
00194     variant value() const { return value_; }
00195     const map_location loc() const { return loc_; }
00196 };
00197 
00198 class fallback_callable : public formula_callable {
00199     std::string key_;
00200     variant get_value(const std::string& /*key*/) const { return variant(); }
00201 public:
00202     explicit fallback_callable(const std::string& key) : key_(key) {
00203     }
00204 
00205     const std::string& key() const { return key_; }
00206 };
00207 
00208 class safe_call_callable : public formula_callable {
00209     variant main_;
00210     variant backup_;
00211     expression_ptr backup_formula_;
00212     variant get_value(const std::string& key) const;
00213 
00214     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00215 public:
00216     safe_call_callable(const variant& main, const expression_ptr& backup)
00217         : main_(main)
00218         , backup_()
00219         , backup_formula_(backup)
00220     {}
00221 
00222     const variant& get_main() const { return main_; }
00223     const expression_ptr& get_backup() const { return backup_formula_; }
00224 
00225     void set_backup_result(const variant& v) {
00226         backup_ = v;
00227     }
00228 };
00229 
00230 
00231 class safe_call_result : public formula_callable {
00232     const formula_callable* failed_callable_;
00233     const map_location current_unit_location_;
00234     const int status_;
00235 
00236     variant get_value(const std::string& key) const;
00237 
00238     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00239 
00240 public:
00241     safe_call_result(const formula_callable* callable, int status,
00242                 const map_location& loc = map_location() )
00243       : failed_callable_(callable), current_unit_location_(loc), status_(status)
00244     {}
00245 };
00246 
00247 
00248 class move_map_callable : public game_logic::formula_callable {
00249     typedef std::multimap<map_location, map_location> move_map;
00250     const move_map& srcdst_;
00251     const move_map& dstsrc_;
00252         const unit_map& units_;
00253 
00254     variant get_value(const std::string& key) const;
00255     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00256 public:
00257     move_map_callable(const move_map& srcdst, const move_map& dstsrc, const unit_map& units)
00258       : srcdst_(srcdst), dstsrc_(dstsrc), units_(units)
00259     {
00260         type_ = MOVE_MAP_C;
00261     }
00262 
00263     const move_map& srcdst() const { return srcdst_; }
00264     const move_map& dstsrc() const { return dstsrc_; }
00265 };
00266 
00267 class position_callable : public formula_callable {
00268     //unit_map units_;
00269     int chance_;
00270     variant get_value(const std::string& key) const;
00271 
00272     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00273 public:
00274     position_callable(/*unit_map* units,*/ int chance) :
00275         //units_(),
00276         chance_(chance)
00277     {
00278         //units->swap(units_);
00279     }
00280 
00281     struct move_map_backup {
00282         move_map_backup() :
00283             srcdst(),
00284             dstsrc(),
00285             full_srcdst(),
00286             full_dstsrc(),
00287             enemy_srcdst(),
00288             enemy_dstsrc(),
00289             attacks_cache()
00290         {
00291         }
00292 
00293         ai::move_map srcdst, dstsrc, full_srcdst, full_dstsrc, enemy_srcdst, enemy_dstsrc;
00294         variant attacks_cache;
00295     };
00296 };
00297 
00298 
00299 class outcome_callable : public formula_callable {
00300     std::vector<variant> hitLeft_, prob_, status_;
00301     variant get_value(const std::string& key) const;
00302 
00303     void get_inputs(std::vector<game_logic::formula_input>* inputs) const;
00304 public:
00305     outcome_callable(       const std::vector<variant>& hitLeft,
00306                     const std::vector<variant>& prob,
00307                     const std::vector<variant>& status)
00308       : hitLeft_(hitLeft), prob_(prob), status_(status)
00309     {
00310     }
00311 
00312     const std::vector<variant>& hitLeft() const { return hitLeft_; }
00313     const std::vector<variant>& prob() const { return prob_; }
00314     const std::vector<variant>& status() const { return status_; }
00315 };
00316 
00317 }
00318 
00319 #endif  /* FORMULA_AI_CALLABLE_OBJECTS_HPP_INCLUDED */
00320 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Tue May 22 2012 01:03:37 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs