add a formula= filtering criteria to SUF,
...also remove is_ennemy and is_mine from the unit FAI type, since it caused unwanted dependency in unit_callable. The info should be available by other means (like a team_callable object)
This commit is contained in:
parent
a5845ccc06
commit
1027413680
6 changed files with 20 additions and 18 deletions
|
@ -2222,14 +2222,14 @@ variant ai_interface::get_value(const std::string& key) const
|
|||
} else if(key == "units") {
|
||||
std::vector<variant> vars;
|
||||
for(unit_map::const_iterator i = info_.units.begin(); i != info_.units.end(); ++i) {
|
||||
vars.push_back(variant(new unit_callable(*i, info_.teams[info_.team_num-1], info_.team_num)));
|
||||
vars.push_back(variant(new unit_callable(*i)));
|
||||
}
|
||||
return variant(&vars);
|
||||
} else if(key == "my_units") {
|
||||
std::vector<variant> vars;
|
||||
for(unit_map::const_iterator i = info_.units.begin(); i != info_.units.end(); ++i) {
|
||||
if(i->second.side() == info_.team_num) {
|
||||
vars.push_back(variant(new unit_callable(*i, info_.teams[info_.team_num-1], info_.team_num)));
|
||||
vars.push_back(variant(new unit_callable(*i)));
|
||||
}
|
||||
}
|
||||
return variant(&vars);
|
||||
|
@ -2237,7 +2237,7 @@ variant ai_interface::get_value(const std::string& key) const
|
|||
std::vector<variant> vars;
|
||||
for(unit_map::const_iterator i = info_.units.begin(); i != info_.units.end(); ++i) {
|
||||
if(info_.teams[info_.team_num-1].is_enemy(i->second.side())) {
|
||||
vars.push_back(variant(new unit_callable(*i, info_.teams[info_.team_num-1], info_.team_num)));
|
||||
vars.push_back(variant(new unit_callable(*i)));
|
||||
}
|
||||
}
|
||||
return variant(&vars);
|
||||
|
|
|
@ -106,10 +106,6 @@ variant unit_callable::get_value(const std::string& key) const
|
|||
return variant(u_.movement_left());
|
||||
} else if(key == "side") {
|
||||
return variant(u_.side());
|
||||
} else if(key == "is_enemy") {
|
||||
return variant(team_.is_enemy(u_.side()));
|
||||
} else if(key == "is_mine") {
|
||||
return variant(side_ == u_.side());
|
||||
} else if(key == "value") {
|
||||
return variant(u_.cost());
|
||||
} else if(key == "vars") {
|
||||
|
@ -139,8 +135,6 @@ void unit_callable::get_inputs(std::vector<game_logic::formula_input>* inputs) c
|
|||
inputs->push_back(game_logic::formula_input("total_movement", FORMULA_READ_ONLY));
|
||||
inputs->push_back(game_logic::formula_input("movement_left", FORMULA_READ_ONLY));
|
||||
inputs->push_back(game_logic::formula_input("side", FORMULA_READ_ONLY));
|
||||
inputs->push_back(game_logic::formula_input("is_enemy", FORMULA_READ_ONLY));
|
||||
inputs->push_back(game_logic::formula_input("is_mine", FORMULA_READ_ONLY));
|
||||
}
|
||||
|
||||
variant terrain_callable::get_value(const std::string& key) const
|
||||
|
|
|
@ -127,8 +127,8 @@ public:
|
|||
class unit_callable : public game_logic::formula_callable {
|
||||
public:
|
||||
typedef gamemap::location location;
|
||||
unit_callable(const std::pair<location, unit>& pair, const team& current_team, unsigned int side)
|
||||
: loc_(pair.first), u_(pair.second), team_(current_team), side_(side)
|
||||
unit_callable(const std::pair<location, unit>& pair)
|
||||
: loc_(pair.first), u_(pair.second)
|
||||
{}
|
||||
|
||||
const unit& get_unit() const { return u_; }
|
||||
|
@ -137,8 +137,6 @@ public:
|
|||
private:
|
||||
const location& loc_;
|
||||
const unit& u_;
|
||||
const team& team_;
|
||||
unsigned int side_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -209,7 +209,7 @@ private:
|
|||
while (un != end) {
|
||||
if (distance_between(loc, un->first) <= range) {
|
||||
if (un->second.side() != ai_.get_info().team_num) {
|
||||
vars.push_back(variant(new unit_callable(*un, ai_.current_team(), un->second.side())));
|
||||
vars.push_back(variant(new unit_callable(*un)));
|
||||
}
|
||||
}
|
||||
++un;
|
||||
|
@ -555,7 +555,7 @@ private:
|
|||
const location_callable* loc = convert_variant<location_callable>(args()[0]->evaluate(variables));
|
||||
const unit_map::const_iterator i = ai_.get_info().units.find(loc->loc());
|
||||
if(i != ai_.get_info().units.end()) {
|
||||
return variant(new unit_callable(*i, ai_.current_team(), ai_.get_info().team_num));
|
||||
return variant(new unit_callable(*i));
|
||||
} else {
|
||||
return variant();
|
||||
}
|
||||
|
@ -608,7 +608,7 @@ private:
|
|||
unit_map::const_iterator un = ai_.get_info().units.find(range.first->second);
|
||||
assert(un != ai_.get_info().units.end());
|
||||
const int side = un->second.side();
|
||||
vars.push_back(variant(new unit_callable(*un, ai_.get_info().teams[side-1], side)));
|
||||
vars.push_back(variant(new unit_callable(*un)));
|
||||
++range.first;
|
||||
}
|
||||
|
||||
|
@ -810,7 +810,7 @@ void formula_ai::play_turn()
|
|||
game_logic::map_formula_callable callable(this);
|
||||
unit_candidate_moves.push_back(formula);
|
||||
callable.add_ref();
|
||||
callable.add("me", variant(new unit_callable(*i, current_team(), get_info().team_num)));
|
||||
callable.add("me", variant(new unit_callable(*i)));
|
||||
make_move(formula, callable);
|
||||
}
|
||||
candidate_moves.insert(std::pair<const std::string, std::vector<game_logic::const_formula_ptr> >
|
||||
|
@ -1079,7 +1079,7 @@ variant formula_ai::get_value(const std::string& key) const
|
|||
return variant();
|
||||
}
|
||||
|
||||
return variant(new unit_callable(*i, current_team(), get_info().team_num));
|
||||
return variant(new unit_callable(*i));
|
||||
} else if(key == "vars") {
|
||||
return variant(&vars_);
|
||||
} else if(key == "keeps") {
|
||||
|
|
|
@ -38,6 +38,8 @@
|
|||
#include "sdl_utils.hpp"
|
||||
#include "terrain_filter.hpp"
|
||||
#include "variable.hpp"
|
||||
#include "callable_objects.hpp"
|
||||
#include "formula.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
|
@ -1136,6 +1138,13 @@ bool unit::internal_matches_filter(const vconfig& cfg, const gamemap::location&
|
|||
}
|
||||
}
|
||||
}
|
||||
if(cfg.has_attribute("formula")) {
|
||||
const unit_callable callable(std::pair<gamemap::location, unit>(loc,*this));
|
||||
const game_logic::formula form(cfg["formula"]);
|
||||
if(!form.execute(callable).as_bool()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -240,6 +240,7 @@ int unit_animation::matches(const game_display &disp,const gamemap::location& lo
|
|||
}
|
||||
std::vector<config>::const_iterator myitor;
|
||||
for(myitor = unit_filter_.begin(); myitor != unit_filter_.end(); myitor++) {
|
||||
printf("%s\n",my_unit->id().c_str());
|
||||
if(!my_unit->matches_filter(&(*myitor),loc)) return MATCH_FAIL;
|
||||
result++;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue