// TODO: this only evaluates SWORD type attacks
if (CurrentAttack().isMonster) {
const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
- if (ts.TargetsMonsters()) {
- for (int i(0); i < MaxMonsters(); ++i) {
- if (ts.IsSelected(i)) {
- if (MonsterAt(i).Health() > 0) {
- const Stats &defenderStats(MonsterAt(i).GetStats());
- Uint16 damage(CalculateDamage(attackerStats, defenderStats));
- ts.SetBad(i, damage);
- } else {
- ts.Unselect(i);
- }
- }
- }
- } else {
- for (int i(0); i < NumHeroes(); ++i) {
- if (ts.IsSelected(i)) {
- if (HeroAt(i).Health() > 0) {
- const Stats &defenderStats(HeroAt(i).GetStats());
- Uint16 damage(CalculateDamage(attackerStats, defenderStats));
- ts.SetBad(i, damage);
- } else {
- ts.Unselect(i);
- }
- }
- }
- }
+ CalculateDamage(attackerStats, ts);
} else {
const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
- bool hitSome(false);
- if (ts.TargetsMonsters()) {
- for (int i(0); i < MaxMonsters(); ++i) {
- if (ts.IsSelected(i)) {
- if (MonsterAt(i).Health() > 0) {
- const Stats &defenderStats(MonsterAt(i).GetStats());
- Uint16 damage(CalculateDamage(attackerStats, defenderStats));
- ts.SetBad(i, damage);
- hitSome = true;
- } else {
- ts.Unselect(i);
- }
- }
- }
- if (hitSome) return;
- for (int i(0); i < MaxMonsters(); ++i) {
- if (MonsterAt(i).Health() > 0) {
- const Stats &defenderStats(MonsterAt(i).GetStats());
- Uint16 damage(CalculateDamage(attackerStats, defenderStats));
- ts.SetBad(i, damage);
- break;
- }
- }
- } else {
- for (int i(0); i < NumHeroes(); ++i) {
- if (ts.IsSelected(i)) {
- if (HeroAt(i).Health() > 0) {
- const Stats &defenderStats(HeroAt(i).GetStats());
- Uint16 damage(CalculateDamage(attackerStats, defenderStats));
- ts.SetBad(i, damage);
- hitSome = true;
- } else {
- ts.Unselect(i);
- }
- }
- }
- if (hitSome) return;
- for (int i(0); i < NumHeroes(); ++i) {
- if (HeroAt(i).Health() > 0) {
- const Stats &defenderStats(HeroAt(i).GetStats());
- Uint16 damage(CalculateDamage(attackerStats, defenderStats));
- ts.SetBad(i, damage);
- break;
- }
- }
- }
+ CalculateDamage(attackerStats, ts);
}
}
-void BattleState::DecideMonsterAttack(Monster &m) {
+void BattleState::DecideMonsterAttack(Monster &m) const {
AttackChoice &ac(m.GetAttackChoice());
TargetSelection &ts(ac.Selection());
ac.Reset();
ts.Select(target);
}
+void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const {
+ bool hitSome(false);
+ if (ts.TargetsMonsters()) {
+ for (int i(0); i < MaxMonsters(); ++i) {
+ if (ts.IsSelected(i)) {
+ if (MonsterAt(i).Health() > 0) {
+ const Stats &defenderStats(MonsterAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ hitSome = true;
+ } else {
+ ts.Unselect(i);
+ }
+ }
+ }
+ if (hitSome) return;
+ for (int i(0); i < MaxMonsters(); ++i) {
+ if (MonsterAt(i).Health() > 0) {
+ const Stats &defenderStats(MonsterAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ break;
+ }
+ }
+ } else {
+ for (int i(0); i < NumHeroes(); ++i) {
+ if (ts.IsSelected(i)) {
+ if (HeroAt(i).Health() > 0) {
+ const Stats &defenderStats(HeroAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ hitSome = true;
+ } else {
+ ts.Unselect(i);
+ }
+ }
+ }
+ if (hitSome) return;
+ for (int i(0); i < NumHeroes(); ++i) {
+ if (HeroAt(i).Health() > 0) {
+ const Stats &defenderStats(HeroAt(i).GetStats());
+ Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+ ts.SetBad(i, damage);
+ break;
+ }
+ }
+ }
+}
+
Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender) const {
// TODO: find out real formula and add some randomness
return attacker.Attack() / 2 - defender.Defense() / 4;