mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-25 11:57:19 +00:00
Manipulated unit states as booleans for improved performances.
Flag 'healable' needs some special casing: it is the only state in the game with a reverted semantic (unset when present).
This commit is contained in:
parent
987536f2c4
commit
030637836b
@ -689,24 +689,24 @@ battle_context::unit_stats::unit_stats(const unit &u, const map_location& u_loc,
|
||||
weapon->set_specials_context(*aloc, *dloc, units, attacking, opp_weapon);
|
||||
if (opp_weapon)
|
||||
opp_weapon->set_specials_context(*aloc, *dloc, units, !attacking, weapon);
|
||||
bool not_living = opp.get_state("not_living");
|
||||
slows = weapon->get_special_bool("slow");
|
||||
drains = weapon->get_special_bool("drains") && !utils::string_bool(opp.get_state("not_living"));
|
||||
drains = !not_living && weapon->get_special_bool("drains");
|
||||
petrifies = weapon->get_special_bool("petrifies");
|
||||
poisons = weapon->get_special_bool("poison") && utils::string_bool(opp.get_state("not_living")) != true && opp.get_state(unit::STATE_POISONED) != true;
|
||||
poisons = !not_living && weapon->get_special_bool("poison") && !opp.get_state(unit::STATE_POISONED);
|
||||
backstab_pos = is_attacker && backstab_check(u_loc, opp_loc, units, *resources::teams);
|
||||
rounds = weapon->get_specials("berserk").highest("value", 1).first;
|
||||
firststrike = weapon->get_special_bool("firststrike");
|
||||
|
||||
// Handle plague.
|
||||
unit_ability_list plague_specials = weapon->get_specials("plague");
|
||||
plagues = !plague_specials.empty() && opp.get_state("not_living") != "yes" &&
|
||||
plagues = !not_living && !plague_specials.empty() &&
|
||||
strcmp(opp.undead_variation().c_str(), "null") && !resources::game_map->is_village(opp_loc);
|
||||
|
||||
if (!plague_specials.empty()) {
|
||||
if((*plague_specials.cfgs.front().first)["type"] == "")
|
||||
if (plagues) {
|
||||
plague_type = (*plague_specials.cfgs.front().first)["type"];
|
||||
if (plague_type.empty())
|
||||
plague_type = u.type_id();
|
||||
else
|
||||
plague_type = (*plague_specials.cfgs.front().first)["type"];
|
||||
}
|
||||
|
||||
// Compute chance to hit.
|
||||
@ -1554,8 +1554,7 @@ void calculate_healing(int side, bool update_display)
|
||||
// We look for all allied units, then we see if our healer is near them.
|
||||
for (unit_map::iterator i = units.begin(); i != units.end(); ++i) {
|
||||
|
||||
if (!utils::string_bool(i->second.get_state("healable"),true) ||
|
||||
i->second.incapacitated())
|
||||
if (i->second.get_state("not_healable") || i->second.incapacitated())
|
||||
continue;
|
||||
|
||||
DBG_NG << "found healable unit at (" << i->first << ")\n";
|
||||
|
@ -374,7 +374,7 @@ std::pair<map_location,map_location> ai_default::choose_move(std::vector<target>
|
||||
}
|
||||
|
||||
//guardian units stay put
|
||||
if(utils::string_bool(u->second.get_state("guardian"))) {
|
||||
if (u->second.get_state("guardian")) {
|
||||
LOG_AI << u->second.type_id() << " is guardian, staying still\n";
|
||||
return std::pair<location,location>(u->first,u->first);
|
||||
}
|
||||
@ -467,8 +467,10 @@ std::pair<map_location,map_location> ai_default::choose_move(std::vector<target>
|
||||
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->second.side() != get_side() || u->second.can_recruit() ||
|
||||
u->second.movement_left() <= 0 || utils::string_bool(u->second.get_state("guardian")) || u->second.incapacitated()) {
|
||||
if (u->second.side() != get_side() || u->second.can_recruit() ||
|
||||
u->second.movement_left() <= 0 || u->second.get_state("guardian") ||
|
||||
u->second.incapacitated())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -350,7 +350,7 @@ void ai_default::find_villages(
|
||||
}
|
||||
|
||||
const unit_map::const_iterator u = units_.find(j->second);
|
||||
if(u == units_.end() || utils::string_bool(u->second.get_state("guardian"))) {
|
||||
if (u == units_.end() || u->second.get_state("guardian")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -1156,7 +1156,7 @@ void get_villages_phase::find_villages(
|
||||
}
|
||||
|
||||
const unit_map::const_iterator u = get_info().units.find(j->second);
|
||||
if(u == get_info().units.end() || utils::string_bool(u->second.get_state("guardian"))) {
|
||||
if (u == get_info().units.end() || u->second.get_state("guardian")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -291,7 +291,7 @@ std::pair<map_location,map_location> default_move_to_targets_phase::choose_move(
|
||||
}
|
||||
|
||||
//guardian units stay put
|
||||
if(utils::string_bool(u->second.get_state("guardian"))) {
|
||||
if (u->second.get_state("guardian")) {
|
||||
LOG_AI << u->second.type_id() << " is guardian, staying still\n";
|
||||
return std::pair<map_location,map_location>(u->first,u->first);
|
||||
}
|
||||
@ -384,8 +384,10 @@ std::pair<map_location,map_location> default_move_to_targets_phase::choose_move(
|
||||
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->second.side() != get_side() || u->second.can_recruit() ||
|
||||
u->second.movement_left() <= 0 || utils::string_bool(u->second.get_state("guardian")) || u->second.incapacitated()) {
|
||||
if (u->second.side() != get_side() || u->second.can_recruit() ||
|
||||
u->second.movement_left() <= 0 || u->second.get_state("guardian") ||
|
||||
u->second.incapacitated())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -291,7 +291,7 @@ std::pair<map_location,map_location> testing_move_to_targets_phase::choose_move(
|
||||
}
|
||||
|
||||
//guardian units stay put
|
||||
if(utils::string_bool(u->second.get_state("guardian"))) {
|
||||
if (u->second.get_state("guardian")) {
|
||||
LOG_AI << u->second.type_id() << " is guardian, staying still\n";
|
||||
return std::pair<map_location,map_location>(u->first,u->first);
|
||||
}
|
||||
@ -384,8 +384,10 @@ std::pair<map_location,map_location> testing_move_to_targets_phase::choose_move(
|
||||
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->second.side() != get_side() || u->second.can_recruit() ||
|
||||
u->second.movement_left() <= 0 || utils::string_bool(u->second.get_state("guardian")) || u->second.incapacitated()) {
|
||||
if (u->second.side() != get_side() || u->second.can_recruit() ||
|
||||
u->second.movement_left() <= 0 || u->second.get_state("guardian") ||
|
||||
u->second.incapacitated())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -170,10 +170,7 @@ variant unit_callable::get_value(const std::string& key) const
|
||||
} else if(key == "leader") {
|
||||
return variant(u_.can_recruit());
|
||||
} else if(key == "undead") {
|
||||
if ( utils::string_bool(u_.get_state("not_living")) )
|
||||
return variant( 1 );
|
||||
|
||||
return variant( 0 );
|
||||
return variant(u_.get_state("not_living") ? 1 : 0);
|
||||
} else if(key == "attacks") {
|
||||
const std::vector<attack_type>& att = u_.attacks();
|
||||
std::vector<variant> res;
|
||||
|
49
src/unit.cpp
49
src/unit.cpp
@ -414,12 +414,17 @@ unit::unit(unit_map* unitmap, const config& cfg,
|
||||
if (const config &status_flags = cfg.child("status"))
|
||||
{
|
||||
foreach (const config::attribute &st, status_flags.attribute_range()) {
|
||||
set_state(st.first,st.second);
|
||||
if (st.first == "healable") {
|
||||
if (!utils::string_bool(st.second, true))
|
||||
set_state("not_healable", true);
|
||||
} else if (utils::string_bool(st.second)) {
|
||||
set_state(st.first, true);
|
||||
}
|
||||
}
|
||||
cfg_.remove_child("status",0);
|
||||
}
|
||||
if(cfg["ai_special"] == "guardian") {
|
||||
set_state("guardian","yes");
|
||||
set_state("guardian", true);
|
||||
}
|
||||
|
||||
// Remove animations from private cfg, they're not needed there now
|
||||
@ -1037,7 +1042,11 @@ void unit::heal(int amount)
|
||||
|
||||
const std::map<std::string,std::string> unit::get_states() const
|
||||
{
|
||||
std::map<std::string,std::string> all_states = states_;
|
||||
std::map<std::string, std::string> all_states;
|
||||
foreach (std::string const &s, states_) {
|
||||
if (s == "not_healable") all_states["healable"] = "no";
|
||||
else all_states[s] = "yes";
|
||||
}
|
||||
for (std::map<std::string, state_t>::const_iterator i = known_boolean_state_names_.begin(),
|
||||
i_end = known_boolean_state_names_.end(); i != i_end; ++i)
|
||||
{
|
||||
@ -1049,17 +1058,13 @@ const std::map<std::string,std::string> unit::get_states() const
|
||||
return all_states;
|
||||
}
|
||||
|
||||
std::string unit::get_state(const std::string& state) const
|
||||
bool unit::get_state(const std::string &state) const
|
||||
{
|
||||
state_t known_boolean_state_id = get_known_boolean_state_id(state);
|
||||
if (known_boolean_state_id!=STATE_UNKNOWN){
|
||||
return get_state(known_boolean_state_id) ? "yes" : "";
|
||||
return get_state(known_boolean_state_id);
|
||||
}
|
||||
std::map<std::string,std::string>::const_iterator i = states_.find(state);
|
||||
if(i != states_.end()) {
|
||||
return i->second;
|
||||
}
|
||||
return "";
|
||||
return states_.find(state) != states_.end();
|
||||
}
|
||||
|
||||
void unit::set_state(state_t state, bool value)
|
||||
@ -1095,25 +1100,17 @@ std::map<std::string, unit::state_t> unit::get_known_boolean_state_names()
|
||||
return known_boolean_state_names_map;
|
||||
}
|
||||
|
||||
void unit::set_state(const std::string& state, const std::string& value)
|
||||
void unit::set_state(const std::string &state, bool value)
|
||||
{
|
||||
state_t known_boolean_state_id = get_known_boolean_state_id(state);
|
||||
if (value.empty()) {
|
||||
if (known_boolean_state_id!=STATE_UNKNOWN){
|
||||
set_state(known_boolean_state_id,false);
|
||||
return;
|
||||
}
|
||||
std::map<std::string,std::string>::iterator i = states_.find(state);
|
||||
if(i != states_.end()) {
|
||||
states_.erase(i);
|
||||
}
|
||||
} else {
|
||||
if (known_boolean_state_id!=STATE_UNKNOWN){
|
||||
set_state(known_boolean_state_id,utils::string_bool(value));
|
||||
return;
|
||||
}
|
||||
states_[state] = value;
|
||||
if (known_boolean_state_id != STATE_UNKNOWN) {
|
||||
set_state(known_boolean_state_id, value);
|
||||
return;
|
||||
}
|
||||
if (value)
|
||||
states_.insert(state);
|
||||
else
|
||||
states_.erase(state);
|
||||
}
|
||||
|
||||
|
||||
|
@ -157,8 +157,8 @@ public:
|
||||
void set_resting(bool rest) { resting_ = rest; }
|
||||
|
||||
const std::map<std::string,std::string> get_states() const;
|
||||
std::string get_state(const std::string& state) const;
|
||||
void set_state(const std::string& state, const std::string& value);
|
||||
bool get_state(const std::string& state) const;
|
||||
void set_state(const std::string &state, bool value);
|
||||
enum state_t { STATE_SLOWED = 0, STATE_POISONED, STATE_PETRIFIED,
|
||||
STATE_HIDDEN, STATE_NOT_MOVED, STATE_UNKNOWN = -1 };
|
||||
void set_state(state_t state, bool value);
|
||||
@ -400,7 +400,7 @@ private:
|
||||
int attacks_left_;
|
||||
int max_attacks_;
|
||||
|
||||
std::map<std::string,std::string> states_;
|
||||
std::set<std::string> states_;
|
||||
std::vector<bool> known_boolean_states_;
|
||||
static std::map<std::string, state_t> known_boolean_state_names_;
|
||||
config variables_;
|
||||
|
Loading…
x
Reference in New Issue
Block a user