Skip to content

Commit

Permalink
AI: allow list of ids for leader_ignores_keep aspect
Browse files Browse the repository at this point in the history
In addition to 'yes' and 'no', comma separated lists of leader ids are now also accepted as values for this aspect. This allows setting the behavior only for specific leaders.
  • Loading branch information
mattsc committed Dec 31, 2019
1 parent c2f9645 commit 4fd17e9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
26 changes: 24 additions & 2 deletions src/ai/contexts.cpp
Expand Up @@ -712,12 +712,12 @@ config readonly_context_impl::get_leader_goal() const
}


bool readonly_context_impl::get_leader_ignores_keep() const
std::string readonly_context_impl::get_leader_ignores_keep() const
{
if (leader_ignores_keep_) {
return leader_ignores_keep_->get();
}
return false;
return std::string();
}


Expand Down Expand Up @@ -1277,4 +1277,26 @@ bool readonly_context_impl::is_active(const std::string &time_of_day, const std:
return true;
}

bool readonly_context_impl::applies_to_leader(const std::string &aspect_value, const std::string &id) const
{
if(aspect_value == "yes") {
return true;
}
if(aspect_value == "no") {
return false;
}
std::vector<std::string> aspect_ids = utils::split(aspect_value);
for(std::vector<std::string>::const_iterator aspect_id = aspect_ids.begin(); aspect_id != aspect_ids.end() ; ++aspect_id ) {
if(*aspect_id == id) {
return true;
}
}
return false;
}

bool readonly_context_impl::is_keep_ignoring_leader(const std::string &id) const
{
return applies_to_leader(leader_ignores_keep_->get(), id);
}

} //of namespace ai
19 changes: 15 additions & 4 deletions src/ai/contexts.hpp
Expand Up @@ -276,7 +276,7 @@ class readonly_context : public virtual side_context {
virtual config get_leader_goal() const = 0;


virtual bool get_leader_ignores_keep() const = 0;
virtual std::string get_leader_ignores_keep() const = 0;


virtual double get_leader_value() const = 0;
Expand Down Expand Up @@ -330,6 +330,8 @@ class readonly_context : public virtual side_context {

virtual bool is_active(const std::string &time_of_day, const std::string &turns) const = 0;

virtual bool is_keep_ignoring_leader(const std::string &id) const = 0;

virtual bool is_dst_src_valid_lua() const = 0;

virtual bool is_dst_src_enemy_valid_lua() const = 0;
Expand Down Expand Up @@ -752,7 +754,7 @@ class readonly_context_proxy : public virtual readonly_context, public virtual s
}


virtual bool get_leader_ignores_keep() const override
virtual std::string get_leader_ignores_keep() const override
{
return target_->get_leader_ignores_keep();
}
Expand Down Expand Up @@ -866,6 +868,11 @@ class readonly_context_proxy : public virtual readonly_context, public virtual s
return target_->is_active(time_of_day, turns);
}

virtual bool is_keep_ignoring_leader(const std::string &id) const override
{
return target_->is_keep_ignoring_leader(id);
}

virtual bool is_dst_src_valid_lua() const override
{
return target_->is_dst_src_valid_lua();
Expand Down Expand Up @@ -1351,7 +1358,7 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly
virtual config get_leader_goal() const override;


virtual bool get_leader_ignores_keep() const override;
virtual std::string get_leader_ignores_keep() const override;


virtual double get_leader_value() const override;
Expand Down Expand Up @@ -1404,6 +1411,8 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly

virtual bool is_active(const std::string &time_of_day, const std::string &turns) const override;

virtual bool is_keep_ignoring_leader(const std::string &id) const override;

virtual bool is_dst_src_valid_lua() const override;

virtual bool is_dst_src_enemy_valid_lua() const override;
Expand Down Expand Up @@ -1467,6 +1476,8 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly
template<typename T>
void add_known_aspect(const std::string &name, typesafe_aspect_ptr<T>& where);

bool applies_to_leader(const std::string &aspect_value, const std::string &id) const;

const config cfg_;

/**
Expand All @@ -1492,7 +1503,7 @@ class readonly_context_impl : public virtual side_context_proxy, public readonly
mutable keeps_cache keeps_;
typesafe_aspect_ptr<double> leader_aggression_;
typesafe_aspect_ptr<config> leader_goal_;
typesafe_aspect_ptr<bool> leader_ignores_keep_;
typesafe_aspect_ptr<std::string> leader_ignores_keep_;
typesafe_aspect_ptr<double> leader_value_;
mutable bool move_maps_enemy_valid_;
mutable bool move_maps_valid_;
Expand Down
4 changes: 2 additions & 2 deletions src/ai/default/ca.cpp
Expand Up @@ -407,7 +407,7 @@ move_leader_to_keep_phase::~move_leader_to_keep_phase()

double move_leader_to_keep_phase::evaluate()
{
if (get_leader_ignores_keep()) {
if (is_keep_ignoring_leader("")) {
return BAD_SCORE;
}
if (get_passive_leader() && !get_passive_leader_shares_keep()) {
Expand All @@ -434,7 +434,7 @@ double move_leader_to_keep_phase::evaluate()
int shortest_distance = 99999;

for (const unit_map::const_iterator& leader : leaders) {
if (leader->incapacitated() || leader->movement_left() == 0 || !is_allowed_unit(*leader)) {
if (leader->incapacitated() || leader->movement_left() == 0 || !is_allowed_unit(*leader) || is_keep_ignoring_leader(leader->id())) {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions src/ai/default/ca_move_to_targets.cpp
Expand Up @@ -280,7 +280,7 @@ std::pair<map_location,map_location> move_to_targets_phase::choose_move(std::vec

//take care of all the guardians first
for(u = units_.begin(); u != units_.end(); ++u) {
if (!(u->side() != get_side() || (u->can_recruit() && !get_leader_ignores_keep()) || u->movement_left() <= 0 || u->incapacitated())) {
if (!(u->side() != get_side() || (u->can_recruit() && !is_keep_ignoring_leader(u->id())) || u->movement_left() <= 0 || u->incapacitated())) {
if (u->get_state("guardian")) {
LOG_AI << u->type_id() << " is guardian, staying still\n";
return std::make_pair(u->get_location(), u->get_location());
Expand All @@ -290,7 +290,7 @@ std::pair<map_location,map_location> move_to_targets_phase::choose_move(std::vec

//now find the first eligible remaining unit
for(u = units_.begin(); u != units_.end(); ++u) {
if (!(u->side() != get_side() || (u->can_recruit() && !get_leader_ignores_keep()) || u->movement_left() <= 0 || u->incapacitated() || !is_allowed_unit(*u))) {
if (!(u->side() != get_side() || (u->can_recruit() && !is_keep_ignoring_leader(u->id())) || u->movement_left() <= 0 || u->incapacitated() || !is_allowed_unit(*u))) {
break;
}
}
Expand Down Expand Up @@ -391,7 +391,7 @@ std::pair<map_location,map_location> move_to_targets_phase::choose_move(std::vec
LOG_AI << "complex targeting...\n";
//now see if any other unit can put a better bid forward
for(++u; u != units_.end(); ++u) {
if (u->side() != get_side() || (u->can_recruit() && !get_leader_ignores_keep()) ||
if (u->side() != get_side() || (u->can_recruit() && !is_keep_ignoring_leader(u->id())) ||
u->movement_left() <= 0 || u->get_state("guardian") ||
u->incapacitated() || !is_allowed_unit(*u))
{
Expand Down Expand Up @@ -694,7 +694,7 @@ map_location move_to_targets_phase::form_group(const std::vector<map_location>&
++n;
} else {
const unit_map::const_iterator un = units_.find(j->second);
if(un == units_.end() || (un->can_recruit() && !get_leader_ignores_keep()) || un->movement_left() < un->total_movement()) {
if(un == units_.end() || (un->can_recruit() && !is_keep_ignoring_leader(un->id())) || un->movement_left() < un->total_movement()) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ai/lua/core.cpp
Expand Up @@ -438,8 +438,8 @@ static int cfun_ai_get_leader_goal(lua_State *L)
static int cfun_ai_get_leader_ignores_keep(lua_State *L)
{
DEPRECATED_ASPECT_MESSAGE("leader_ignores_keep");
bool leader_ignores_keep = get_readonly_context(L).get_leader_ignores_keep();
lua_pushboolean(L, leader_ignores_keep);
std::string leader_ignores_keep = get_readonly_context(L).get_leader_ignores_keep();
lua_pushstring(L, leader_ignores_keep.c_str());
return 1;
}

Expand Down
8 changes: 4 additions & 4 deletions src/ai/registry.cpp
Expand Up @@ -206,7 +206,7 @@ static register_aspect_factory< composite_aspect<double>>
static register_aspect_factory< composite_aspect<config>>
leader_goal__composite_aspect_factory("leader_goal*composite_aspect");

static register_aspect_factory< composite_aspect<bool>>
static register_aspect_factory< composite_aspect<std::string>>
leader_igores_keep__composite_aspect_factory("leader_ignores_keep*composite_aspect");

static register_aspect_factory< composite_aspect<double>>
Expand Down Expand Up @@ -277,7 +277,7 @@ static register_aspect_factory< standard_aspect<double>>
static register_aspect_factory< standard_aspect<config>>
leader_goal__standard_aspect_factory("leader_goal*standard_aspect");

static register_aspect_factory< standard_aspect<bool>>
static register_aspect_factory< standard_aspect<std::string>>
leader_ignores_keep__standard_aspect_factory("leader_ignores_keep*standard_aspect");

static register_aspect_factory< standard_aspect<double>>
Expand Down Expand Up @@ -352,7 +352,7 @@ static register_aspect_factory< standard_aspect<double>>
static register_aspect_factory< standard_aspect<config>>
leader_goal__standard_aspect_factory2("leader_goal*");

static register_aspect_factory< standard_aspect<bool>>
static register_aspect_factory< standard_aspect<std::string>>
leader_ignores_keep__standard_aspect_factory2("leader_ignores_keep*");

static register_aspect_factory< standard_aspect<double>>
Expand Down Expand Up @@ -423,7 +423,7 @@ static register_lua_aspect_factory< lua_aspect<double>>
static register_lua_aspect_factory< lua_aspect<config>>
leader_goal__lua_aspect_factory("leader_goal*lua_aspect");

static register_lua_aspect_factory< lua_aspect<bool>>
static register_lua_aspect_factory< lua_aspect<std::string>>
leader_ignores_keep__lua_aspect_factory("leader_ignores_keep*lua_aspect");

static register_lua_aspect_factory< lua_aspect<double>>
Expand Down

0 comments on commit 4fd17e9

Please sign in to comment.