Encapsulate poison and slow prob in calculations, provide average_hp calc.

This will be used by AI.
This commit is contained in:
Rusty Russell 2006-05-09 04:01:39 +00:00
parent 555c1f5182
commit 0f1337059c
2 changed files with 31 additions and 1 deletions

View file

@ -472,7 +472,7 @@ void prob_matrix::receive_blow_a(unsigned damage, unsigned slow_damage, double h
combatant::combatant(const battle_context::unit_stats &u)
: hp_dist(u.drains ? u.max_hp+1: u.hp+1),
untouched(1.0),
untouched(1.0), poisoned(u.is_poisoned ? 1.0 : 0.0), slowed(u.is_slowed ? 1.0 : 0.0),
u_(u),
hit_chances_(u.num_blows, u.chance_to_hit / 100.0)
{
@ -593,11 +593,32 @@ void combatant::fight(combatant &opp)
opp.hp_dist[i] = opp.summary[0][i] + opp.summary[1][i];
}
if (opp.u_.poisons && !u_.is_poisoned)
poisoned += untouched - hp_dist[u_.hp];
if (u_.poisons && !opp.u_.is_poisoned)
opp.poisoned += opp.untouched - opp.hp_dist[opp.u_.hp];
if (opp.u_.slows && !u_.is_slowed)
slowed += untouched - hp_dist[u_.hp];
if (u_.slows && !opp.u_.is_slowed)
opp.slowed += opp.untouched - opp.hp_dist[opp.u_.hp];
// FIXME: This is approximate: we could drain, then get hit.
untouched = hp_dist[u_.hp];
opp.untouched = opp.hp_dist[opp.u_.hp];
}
unsigned int combatant::average_hp() const
{
double total = 0, sum = 0;
for (unsigned int i = 0; i < hp_dist.size(); i++) {
sum += hp_dist[i];
total += hp_dist[i] * i;
}
return (unsigned int)((total / sum) + 0.5);
}
#if defined(BENCHMARK) || defined(CHECK)
// We create a significant number of nasty-to-calculate units, and
// test each one against the others.

View file

@ -23,6 +23,15 @@ struct combatant
// Resulting chance we were not hit by this opponent (important if it poisons)
double untouched;
// Resulting chance we are poisoned.
double poisoned;
// Resulting chance we are slowed.
double slowed;
// What's the average hp (weighted average of hp_dist).
unsigned int average_hp() const;
private:
// We must adjust for swarm after every combat.
void adjust_hitchance();