]> git.localhorst.tv Git - l2e.git/commitdiff
randomized monsters' attack target selection
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 22 Aug 2012 19:45:10 +0000 (21:45 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 22 Aug 2012 19:45:10 +0000 (21:45 +0200)
src/battle/BattleState.cpp
src/battle/BattleState.h
src/main.cpp

index fe1b309a3a65b36543de7d8585290f6503b9139f..fe821fc416ad6999a30e314858f9ca84da1ab885 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <algorithm>
 #include <cassert>
+#include <cstdlib>
 #include <stdexcept>
 
 using app::Application;
@@ -33,6 +34,7 @@ using geometry::Point;
 using geometry::Vector;
 using graphics::Menu;
 
+using std::rand;
 using std::vector;
 
 namespace battle {
@@ -220,26 +222,43 @@ bool BattleState::AttacksFinished() const {
 }
 
 void BattleState::CalculateDamage() {
+       if (CurrentAttack().isMonster) {
+               DecideMonsterAttack(MonsterAt(CurrentAttack().index));
+       }
        AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice());
        if (ac.GetType() == AttackChoice::DEFEND) return;
+       TargetSelection &ts(ac.Selection());
 
+       // TODO: this only evaluates SWORD type attacks
        if (CurrentAttack().isMonster) {
                const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
-               // TODO: run monster's attack script
-               ac.SetType(AttackChoice::SWORD);
-               ac.Selection().SelectSingle();
-               ac.Selection().SelectHeroes();
-               for (int i(0); i < NumHeroes(); ++i) {
-                       if (HeroAt(i).Health() > 0) {
-                               const Stats &defenderStats(HeroAt(i).GetStats());
-                               Uint16 damage(CalculateDamage(attackerStats, defenderStats));
-                               ac.Selection().SetBad(0, damage);
-                               break;
+               if (ts.TargetsEnemies()) {
+                       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);
+                                       }
+                               }
                        }
                }
        } else {
                const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
-               TargetSelection &ts(ac.Selection());
                bool hitSome(false);
                if (ts.TargetsEnemies()) {
                        for (int i(0); i < MaxMonsters(); ++i) {
@@ -289,6 +308,21 @@ void BattleState::CalculateDamage() {
        }
 }
 
+void BattleState::DecideMonsterAttack(Monster &m) {
+       AttackChoice &ac(m.GetAttackChoice());
+       TargetSelection &ts(ac.Selection());
+       ac.Reset();
+       // TODO: run monster's attack script
+       int target(rand() % NumHeroes());
+       while (!HeroPositionOccupied(target)) {
+               target = rand() % NumHeroes();
+       }
+       ac.SetType(AttackChoice::SWORD);
+       ts.SelectHeroes();
+       ts.SetSingle();
+       ts.Select(target);
+}
+
 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 d22d6894d314bb44f98223bce046ae7edc313a76..4fd9a15165696399e2100190c51b77b7c1ff44b3 100644 (file)
@@ -150,6 +150,7 @@ public:
 private:
        void LoadInventory();
 
+       void DecideMonsterAttack(Monster &);
        Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
 
 private:
index cf6e78bbf6c9596235b82ecc5d18001881c68bb6..71a29e58292b0f889661cd691db808d250226d19 100644 (file)
@@ -30,6 +30,8 @@
 #include "sdl/InitScreen.h"
 #include "sdl/InitSDL.h"
 
+#include <cstdlib>
+#include <ctime>
 #include <exception>
 #include <iostream>
 #include <SDL.h>
@@ -70,6 +72,8 @@ int main(int argc, char **argv) {
 
        const int framerate = 33;
 
+//     std::srand(std::time(0));
+
        try {
                InitSDL sdl;
                InitImage image(IMG_INIT_PNG);