]> git.localhorst.tv Git - l2e.git/commitdiff
extracted damage calculation once more
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 22 Aug 2012 20:14:53 +0000 (22:14 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 22 Aug 2012 20:14:53 +0000 (22:14 +0200)
src/battle/BattleState.cpp
src/battle/BattleState.h

index 3f28f5cc43e1f74a2f4b9d650a867d7b8aa63961..ae88731fe0c9d06cde1edc265e6e868cb640dc2f 100644 (file)
@@ -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;
index 4fd9a15165696399e2100190c51b77b7c1ff44b3..a6ba39829d9f6de2718aace409980542d71b6ce5 100644 (file)
@@ -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: