Add support for unit loop formulas
This commit is contained in:
parent
ac84c24591
commit
5f8e84774a
3 changed files with 42 additions and 11 deletions
|
@ -1369,19 +1369,38 @@ void formula_ai::play_turn()
|
|||
|
||||
for(unit_map::unit_iterator i = units_.begin() ; i != units_.end() ; ++i)
|
||||
{
|
||||
if ( (i->second.side() == get_info().team_num) && i->second.has_formula() )
|
||||
if (i->second.side() == get_info().team_num)
|
||||
{
|
||||
try {
|
||||
game_logic::const_formula_ptr formula(new game_logic::formula(i->second.get_formula(), &function_table));
|
||||
game_logic::map_formula_callable callable(this);
|
||||
callable.add_ref();
|
||||
callable.add("me", variant(new unit_callable(*i)));
|
||||
make_move(formula, callable);
|
||||
if( i->second.has_formula() )
|
||||
{
|
||||
try {
|
||||
game_logic::const_formula_ptr formula(new game_logic::formula(i->second.get_formula(), &function_table));
|
||||
game_logic::map_formula_callable callable(this);
|
||||
callable.add_ref();
|
||||
callable.add("me", variant(new unit_callable(*i)));
|
||||
make_move(formula, callable);
|
||||
}
|
||||
catch(formula_error& e) {
|
||||
if(e.filename_ == "formula")
|
||||
e.line_ = 0;
|
||||
handle_exception( e, "Unit formula error for unit: '" + i->second.type_id() + "' standing at (" + boost::lexical_cast<std::string>(i->first.x+1) + "," + boost::lexical_cast<std::string>(i->first.y+1) + ")");
|
||||
}
|
||||
}
|
||||
catch(formula_error& e) {
|
||||
if(e.filename_ == "formula")
|
||||
e.line_ = 0;
|
||||
handle_exception( e, "Unit formula error for unit: '" + i->second.type_id() + "' standing at (" + boost::lexical_cast<std::string>(i->first.x+1) + "," + boost::lexical_cast<std::string>(i->first.y+1) + ")");
|
||||
|
||||
if( i->second.has_loop_formula() )
|
||||
{
|
||||
try {
|
||||
game_logic::const_formula_ptr loop_formula(new game_logic::formula(i->second.get_loop_formula(), &function_table));
|
||||
game_logic::map_formula_callable callable(this);
|
||||
callable.add_ref();
|
||||
callable.add("me", variant(new unit_callable(*i)));
|
||||
while ( make_move(loop_formula, callable) ) {}
|
||||
}
|
||||
catch(formula_error& e) {
|
||||
if(e.filename_ == "formula")
|
||||
e.line_ = 0;
|
||||
handle_exception( e, "Unit loop formula error for unit: '" + i->second.type_id() + "' standing at (" + boost::lexical_cast<std::string>(i->first.x+1) + "," + boost::lexical_cast<std::string>(i->first.y+1) + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,6 +116,7 @@ unit::unit(const unit& o):
|
|||
alpha_(o.alpha_),
|
||||
|
||||
unit_formula_(o.unit_formula_),
|
||||
unit_loop_formula_(o.unit_loop_formula_),
|
||||
formula_vars_(o.formula_vars_ ? new game_logic::map_formula_callable(*o.formula_vars_) : o.formula_vars_),
|
||||
|
||||
recruits_(o.recruits_),
|
||||
|
@ -199,6 +200,7 @@ unit::unit(unit_map* unitmap, const gamemap* map, const gamestatus* game_status,
|
|||
gender_(),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
formula_vars_(),
|
||||
recruits_(),
|
||||
movement_(0),
|
||||
|
@ -276,6 +278,7 @@ unit::unit(const config& cfg,bool use_traits) :
|
|||
gender_(),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
formula_vars_(),
|
||||
recruits_(),
|
||||
movement_(0),
|
||||
|
@ -381,6 +384,7 @@ unit::unit(unit_map* unitmap, const gamemap* map, const gamestatus* game_status,
|
|||
gender_(dummy_unit ? gender : generate_gender(*t,use_traits)),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
formula_vars_(),
|
||||
recruits_(),
|
||||
movement_(0),
|
||||
|
@ -479,6 +483,7 @@ unit::unit(const unit_type* t, int side, bool use_traits, bool dummy_unit,
|
|||
gender_(dummy_unit ? gender : generate_gender(*t,use_traits)),
|
||||
alpha_(),
|
||||
unit_formula_(),
|
||||
unit_loop_formula_(),
|
||||
formula_vars_(),
|
||||
recruits_(),
|
||||
movement_(0),
|
||||
|
@ -1474,6 +1479,7 @@ void unit::read(const config& cfg, bool use_traits, game_state* state)
|
|||
|
||||
//support for unit formulas and unit-specyfic variables in [ai_vars]
|
||||
unit_formula_ = cfg["formula"];
|
||||
unit_loop_formula_ = cfg["loop_formula"];
|
||||
|
||||
const config* ai_vars = cfg.child("ai_vars");
|
||||
if (ai_vars)
|
||||
|
@ -1642,6 +1648,9 @@ void unit::write(config& cfg) const
|
|||
if (has_formula())
|
||||
cfg["formula"] = unit_formula_;
|
||||
|
||||
if (has_loop_formula())
|
||||
cfg["loop_formula"] = unit_loop_formula_;
|
||||
|
||||
|
||||
if (formula_vars_ && formula_vars_->empty() == false)
|
||||
{
|
||||
|
|
|
@ -289,7 +289,9 @@ public:
|
|||
|
||||
const game_logic::map_formula_callable_ptr& formula_vars() const { return formula_vars_; }
|
||||
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_; }
|
||||
const std::string& get_loop_formula() const { return unit_loop_formula_; }
|
||||
|
||||
void reset_modifications();
|
||||
void backup_state();
|
||||
|
@ -376,6 +378,7 @@ private:
|
|||
fixed_t alpha_;
|
||||
|
||||
std::string unit_formula_;
|
||||
std::string unit_loop_formula_;
|
||||
game_logic::map_formula_callable_ptr formula_vars_;
|
||||
|
||||
std::vector<std::string> recruits_;
|
||||
|
|
Loading…
Add table
Reference in a new issue