Unit: added function to get current and max XP difference

See the function documentation for a explanation of its uses.
This commit is contained in:
Charles Dang 2017-05-29 18:43:08 +11:00
parent e46dfcce9a
commit 9faf177472
5 changed files with 21 additions and 9 deletions

View file

@ -287,7 +287,7 @@ unit_ptr get_advanced_unit(const unit &u, const std::string& advance_to)
" to: " + advance_to);
}
unit_ptr new_unit(new unit(u));
new_unit->set_experience(new_unit->experience() - new_unit->max_experience());
new_unit->set_experience(new_unit->experience_differential());
new_unit->advance_to(*new_type);
new_unit->heal_fully();
new_unit->set_state(unit::STATE_POISONED, false);
@ -305,7 +305,7 @@ unit_ptr get_advanced_unit(const unit &u, const std::string& advance_to)
unit_ptr get_amla_unit(const unit &u, const config &mod_option)
{
unit_ptr amla_unit(new unit(u));
amla_unit->set_experience(amla_unit->experience() - amla_unit->max_experience());
amla_unit->set_experience(amla_unit->experience_differential());
amla_unit->add_modification("advancement", mod_option);
return amla_unit;
}

View file

@ -180,7 +180,7 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,
double advance_prob = 0.0;
// The reward for advancing a unit is to get a 'negative' loss of that unit
if (!up->advances_to().empty()) {
int xp_for_advance = up->max_experience() - up->experience();
int xp_for_advance = up->experience_differential();
// See bug #6272... in some cases, unit already has got enough xp to advance,
// but hasn't (bug elsewhere?). Can cause divide by zero.
@ -235,7 +235,7 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,
}
if (!defend_it->advances_to().empty() &&
def_avg_experience >= defend_it->max_experience() - defend_it->experience()) {
def_avg_experience >= defend_it->experience_differential()) {
// It's likely to advance: only if we can kill with first blow.
chance_to_kill = first_chance_kill;
// Negative average damage (it will advance).

View file

@ -249,7 +249,7 @@ void helper_advance_unit(const map_location& loc){
ERR_AI_SIM_ACTIONS << "Simulating advancing to unknown unit type: " << advance_unit_typename;
assert(false && "simulating to unknown unit type");
}
advanced_unit->set_experience(advanced_unit->experience() - advanced_unit->max_experience());
advanced_unit->set_experience(advanced_unit->experience_differential());
advanced_unit->advance_to(*advanced_type);
advanced_unit->heal_fully();
advanced_unit->set_state(unit::STATE_POISONED, false);
@ -257,7 +257,7 @@ void helper_advance_unit(const map_location& loc){
advanced_unit->set_state(unit::STATE_PETRIFIED, false);
}else{
const config &mod_option = mod_options[advance_choice-options.size()];
advanced_unit->set_experience(advanced_unit->experience()-advanced_unit->max_experience());
advanced_unit->set_experience(advanced_unit->experience_differential());
advanced_unit->add_modification("advancement", mod_option);
}

View file

@ -1095,9 +1095,9 @@ color_t unit::xp_color() const
const color_t far_amla_color {139,0,237,0};
const color_t amla_color {170,0,255,0};
const bool near_advance = max_experience() - experience() <= game_config::kill_experience;
const bool mid_advance = max_experience() - experience() <= game_config::kill_experience*2;
const bool far_advance = max_experience() - experience() <= game_config::kill_experience*3;
const bool near_advance = static_cast<int>(experience_differential()) <= game_config::kill_experience;
const bool mid_advance = static_cast<int>(experience_differential()) <= game_config::kill_experience*2;
const bool far_advance = static_cast<int>(experience_differential()) <= game_config::kill_experience*3;
color_t color = normal_color;

View file

@ -409,6 +409,18 @@ public:
return max_experience_;
}
/**
* Absolute difference between current and max experience points.
*
* This function can serve two purposes:
* - If current XP < max XP, the result is the amount of XP needed to advance.
* - If current XP > max XP, the result is how much XP the unit will retain once they advance.
*/
unsigned int experience_differential() const
{
return std::abs(experience_ - max_experience_);
}
/** Sets the current experience point amount. */
void set_experience(int xp)
{