From: Daniel Karbach Date: Wed, 22 Aug 2012 20:14:53 +0000 (+0200) Subject: extracted damage calculation once more X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c20927cf8ab9bb7526f641850c3997f14c66f06e;p=l2e.git extracted damage calculation once more --- diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 3f28f5c..ae88731 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -232,83 +232,14 @@ void BattleState::CalculateDamage() { // 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(); @@ -323,6 +254,55 @@ void BattleState::DecideMonsterAttack(Monster &m) { 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; diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 4fd9a15..a6ba398 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -150,7 +150,8 @@ public: private: void LoadInventory(); - void DecideMonsterAttack(Monster &); + void DecideMonsterAttack(Monster &) const; + void CalculateDamage(const Stats &attackerStats, TargetSelection &targets) const; Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const; private: