Added can_reach call to attack move evals to reduce amount of required evals.

Added target (enemy_unit in candidate_move class) as callable object
to eval formula.
This commit is contained in:
Douglas Hains 2008-08-07 05:19:47 +00:00
parent e2d0df696d
commit f394d863a8
2 changed files with 51 additions and 43 deletions

View file

@ -1810,7 +1810,8 @@ variant formula_ai::get_keeps() const
return keeps_cache_;
}
bool formula_ai::can_attack(const gamemap::location unit_loc, const gamemap::location enemy_loc) {
bool formula_ai::can_attack(const gamemap::location unit_loc,
const gamemap::location enemy_loc) const {
move_map::iterator i;
std::pair<move_map::iterator,
move_map::iterator> unit_moves;
@ -1823,3 +1824,47 @@ bool formula_ai::can_attack(const gamemap::location unit_loc, const gamemap::loc
}
return false;
}
void candidate_move::evaluate_move(const formula_ai* ai, unit_map& units,
int team_num) {
score_ = -1000;
if(type_ == "attack") {
for(unit_map::unit_iterator me = units.begin() ; me != units.end() ; ++me)
{
if( (me->second.side() == team_num) &&
(me->second.has_moved() == false) ) {
for(unit_map::unit_iterator target = units.begin() ; target != units.end() ; ++target) {
if( (target->second.side() != team_num) &&
(ai->can_attack(me->first, target->first)) ) {
game_logic::map_formula_callable callable((formula_callable*) ai);
callable.add_ref();
callable.add("me", variant(new unit_callable(*me)));
callable.add("target", variant(new unit_callable(*target)));
int res = (formula::evaluate(eval_, callable)).as_int();
if(res > score_) {
score_ = res;
action_unit_ = me;
enemy_unit_ = target;
}
}
}
}
}
} else {
for(unit_map::unit_iterator i = units.begin() ; i != units.end() ; ++i)
{
if( (i->second.side() == team_num) &&
(i->second.has_moved() == false) ) {
game_logic::map_formula_callable callable((formula_callable*) ai);
callable.add_ref();
callable.add("me", variant(new unit_callable(*i)));
int res = (formula::evaluate(eval_, callable)).as_int();
if(res > score_) {
score_ = res;
action_unit_ = i;
}
}
}
}
}

View file

@ -36,49 +36,11 @@ public:
eval_(eval),
move_(move),
score_(-1),
action_unit_()
action_unit_(),
enemy_unit_()
{};
void evaluate_move(const formula_callable* ai, unit_map& units, int team_num) {
score_ = -1000;
if(type_ == "attack") {
for(unit_map::unit_iterator me = units.begin() ; me != units.end() ; ++me)
{
if( (me->second.side() == team_num) &&
(me->second.has_moved() == false) ) {
for(unit_map::unit_iterator target = units.begin() ; target != units.end() ; ++target) {
if(target->second.side() != team_num) {
game_logic::map_formula_callable callable(ai);
callable.add_ref();
callable.add("me", variant(new unit_callable(*me)));
callable.add("target", variant(new unit_callable(*target)));
int res = (formula::evaluate(eval_, callable)).as_int();
if(res > score_) {
score_ = res;
action_unit_ = me;
// enemy_unit_ = target;
}
}
}
}
}
} else {
for(unit_map::unit_iterator i = units.begin() ; i != units.end() ; ++i)
{
if( (i->second.side() == team_num) &&
(i->second.has_moved() == false) ) {
game_logic::map_formula_callable callable(ai);
callable.add_ref();
callable.add("me", variant(new unit_callable(*i)));
int res = (formula::evaluate(eval_, callable)).as_int();
if(res > score_) {
score_ = res;
action_unit_ = i;
}
}
}
}
}
void evaluate_move(const formula_ai* ai, unit_map& units, int team_num);
int get_score() const {return score_;}
std::string get_type() const {return type_;}
@ -101,6 +63,7 @@ private:
const_formula_ptr move_;
int score_;
unit_map::unit_iterator action_unit_;
unit_map::unit_iterator enemy_unit_;
};
@ -181,7 +144,7 @@ public:
const variant& get_keeps_cache() const { return keeps_cache_; }
// Check if given unit loc can reach attack range of enemy loc
bool can_attack (const gamemap::location, const gamemap::location);
bool can_attack (const gamemap::location, const gamemap::location) const;
private:
void do_recruitment();