Author: dragonking
Date: Mon Oct 13 22:37:16 2008
New Revision: 30144
URL: http://svn.gna.org/viewcvs/wesnoth?rev=30144&view=rev
Log:
Add possibility to save ai variables to specified unit
Modified:
trunk/src/formula_ai.cpp
trunk/src/unit.cpp
trunk/src/unit.hpp
Modified: trunk/src/formula_ai.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formula_ai.cpp?rev=30144&r1=30143&r2=30144&view=diff
==============================================================================
--- trunk/src/formula_ai.cpp (original)
+++ trunk/src/formula_ai.cpp Mon Oct 13 22:37:16 2008
@@ -528,24 +528,29 @@
class set_var_callable : public formula_callable {
std::string key_;
variant value_;
+ gamemap::location loc_;
variant get_value(const std::string& /*key*/) const { return variant();
}
public:
- set_var_callable(const std::string& key, const variant& value)
- : key_(key), value_(value)
+ set_var_callable(const std::string& key, const variant& value, const
gamemap::location loc)
+ : key_(key), value_(value), loc_(loc)
{}
const std::string& key() const { return key_; }
variant value() const { return value_; }
+ const gamemap::location loc() const { return loc_; }
};
class set_var_function : public function_expression {
public:
explicit set_var_function(const args_list& args)
- : function_expression("set_var", args, 2, 2)
+ : function_expression("set_var", args, 2, 3)
{}
private:
variant execute(const formula_callable& variables) const {
- return variant(new
set_var_callable(args()[0]->evaluate(variables).as_string(),
args()[1]->evaluate(variables)));
+ if( args().size() == 2 ) {
+ return variant(new
set_var_callable(args()[0]->evaluate(variables).as_string(),
args()[1]->evaluate(variables), gamemap::location::null_location ));
+ }
+ return variant(new
set_var_callable(args()[0]->evaluate(variables).as_string(),
args()[1]->evaluate(variables),
convert_variant<location_callable>(args()[2]->evaluate(variables))->loc()));
}
};
@@ -1696,8 +1701,22 @@
made_move = true;
}
} else if(set_var_command) {
- std::cerr << "setting var: " << set_var_command->key()
<< " -> " << set_var_command->value().to_debug_string() << "\n";
- vars_.add(set_var_command->key(),
set_var_command->value());
+ if (set_var_command->loc() ==
gamemap::location::null_location) {
+ std::cerr << "setting var: " <<
set_var_command->key() << " -> " << set_var_command->value().to_debug_string()
<< "\n";
+ vars_.add(set_var_command->key(),
set_var_command->value());
+ } else {
+ std::cerr << "setting unit var: " <<
set_var_command->key() << " -> " << set_var_command->value().to_debug_string()
<< "\n";
+ unit_map::iterator unit =
units_.find(set_var_command->loc());
+ if(unit != units_.end()) {
+ if( unit->second.side() ==
get_info().team_num ) {
+
unit->second.add_formula_var(set_var_command->key(), set_var_command->value());
+ }
+ } else {
+ std::ostringstream str;
+ str << "set_var function: expected unit
at location (" << (set_var_command->loc().x+1) << "," <<
(set_var_command->loc().y+1) << ")";
+ throw formula_error( str.str(), "", "",
0);
+ }
+ }
made_move = true;
} else if(i->is_string() && i->as_string() == "recruit") {
do_recruitment();
Modified: trunk/src/unit.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=30144&r1=30143&r2=30144&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Mon Oct 13 22:37:16 2008
@@ -1745,6 +1745,9 @@
}
+void unit::add_formula_var(std::string str, variant var) {
+ formula_vars_->add(str, var);
+}
const surface unit::still_image(bool scaled) const
{
Modified: trunk/src/unit.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.hpp?rev=30144&r1=30143&r2=30144&view=diff
==============================================================================
--- trunk/src/unit.hpp (original)
+++ trunk/src/unit.hpp Mon Oct 13 22:37:16 2008
@@ -288,6 +288,7 @@
bool abilities_affects_adjacent() const;
const game_logic::map_formula_callable_ptr& formula_vars() const {
return formula_vars_; }
+ void add_formula_var(std::string str, variant var);
bool has_formula() const { return !unit_formula_.empty(); }
bool has_loop_formula() const { return !unit_loop_formula_.empty(); }
const std::string& get_formula() const { return unit_formula_; }
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits