Add possibility to save ai variables to specified unit

This commit is contained in:
Bartek Waresiak 2008-10-13 20:37:16 +00:00
parent 25037b6171
commit 42fa02ba54
3 changed files with 29 additions and 6 deletions

View File

@ -528,24 +528,29 @@ private:
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 @@ bool formula_ai::execute_variant(const variant& var, bool commandline)
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();

View File

@ -1745,6 +1745,9 @@ void unit::write(config& cfg) const
}
void unit::add_formula_var(std::string str, variant var) {
formula_vars_->add(str, var);
}
const surface unit::still_image(bool scaled) const
{

View File

@ -288,6 +288,7 @@ public:
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_; }