]> git.localhorst.tv Git - l2e.git/commitdiff
added simple damage calculation formula
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 19 Aug 2012 19:12:08 +0000 (21:12 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 19 Aug 2012 19:21:30 +0000 (21:21 +0200)
Debug/src/battle/subdir.mk
Release/src/battle/subdir.mk
src/battle/BattleState.cpp
src/battle/BattleState.h
src/battle/Hero.cpp
src/battle/Hero.h
src/battle/Monster.cpp
src/battle/Monster.h
src/battle/Stats.cpp [new file with mode: 0644]
src/battle/Stats.h [new file with mode: 0644]
src/main.cpp

index df33d4e449215a08782b92531d598ab83a76283f..c31d03d23e1c6fd9ee8acded4e8c62d4152777f0 100644 (file)
@@ -14,6 +14,7 @@ CPP_SRCS += \
 ../src/battle/NumberAnimation.cpp \
 ../src/battle/PartyLayout.cpp \
 ../src/battle/SmallHeroTag.cpp \
+../src/battle/Stats.cpp \
 ../src/battle/TargetSelection.cpp 
 
 OBJS += \
@@ -27,6 +28,7 @@ OBJS += \
 ./src/battle/NumberAnimation.o \
 ./src/battle/PartyLayout.o \
 ./src/battle/SmallHeroTag.o \
+./src/battle/Stats.o \
 ./src/battle/TargetSelection.o 
 
 CPP_DEPS += \
@@ -40,6 +42,7 @@ CPP_DEPS += \
 ./src/battle/NumberAnimation.d \
 ./src/battle/PartyLayout.d \
 ./src/battle/SmallHeroTag.d \
+./src/battle/Stats.d \
 ./src/battle/TargetSelection.d 
 
 
index 776e8603cc4c6f057eba8e1c58a0448795b5c509..dc5e485185d2636fb27368c3e20a1a1b52fb8d99 100644 (file)
@@ -14,6 +14,7 @@ CPP_SRCS += \
 ../src/battle/NumberAnimation.cpp \
 ../src/battle/PartyLayout.cpp \
 ../src/battle/SmallHeroTag.cpp \
+../src/battle/Stats.cpp \
 ../src/battle/TargetSelection.cpp 
 
 OBJS += \
@@ -27,6 +28,7 @@ OBJS += \
 ./src/battle/NumberAnimation.o \
 ./src/battle/PartyLayout.o \
 ./src/battle/SmallHeroTag.o \
+./src/battle/Stats.o \
 ./src/battle/TargetSelection.o 
 
 CPP_DEPS += \
@@ -40,6 +42,7 @@ CPP_DEPS += \
 ./src/battle/NumberAnimation.d \
 ./src/battle/PartyLayout.d \
 ./src/battle/SmallHeroTag.d \
+./src/battle/Stats.d \
 ./src/battle/TargetSelection.d 
 
 
index 3dea5b4c5f40c9cad6f88ce6055df1d90ae0b84e..e72fbf94fc6c17b2a441881ed38de540d7fa9539 100644 (file)
@@ -254,8 +254,8 @@ class OrderCompare {
        public:
                OrderCompare(BattleState *battle) : battle(battle) { }
                bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) {
-                       int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).Agility() : battle->HeroAt(lhs.index).Agility());
-                       int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).Agility() : battle->HeroAt(rhs.index).Agility());
+                       int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility());
+                       int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility());
                        return lagl > ragl;
                }
        private:
@@ -289,19 +289,30 @@ void BattleState::NextAttack() {
 
 void BattleState::CalculateDamage() {
        if (CurrentAttack().isMonster) {
+               const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats());
                // TODO: run monster's attack script
                monsterAttacks[CurrentAttack().index].SetType(AttackChoice::SWORD);
                monsterAttacks[CurrentAttack().index].Selection().SelectSingle();
                monsterAttacks[CurrentAttack().index].Selection().SelectHeroes();
-               monsterAttacks[CurrentAttack().index].Selection().SetBad(0, 15);
+               for (int i(0); i < NumHeroes(); ++i) {
+                       if (HeroAt(i).Health() > 0) {
+                               const Stats &defenderStats(HeroAt(i).GetStats());
+                               Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+                               monsterAttacks[CurrentAttack().index].Selection().SetBad(0, damage);
+                               break;
+                       }
+               }
        } else {
+               const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats());
                TargetSelection &ts(AttackChoiceAt(CurrentAttack().index).Selection());
                bool hitSome(false);
                if (ts.TargetsEnemies()) {
                        for (int i(0); i < MaxMonsters(); ++i) {
                                if (ts.IsSelected(i)) {
                                        if (MonsterAt(i).Health() > 0) {
-                                               ts.SetBad(i, 15);
+                                               const Stats &defenderStats(MonsterAt(i).GetStats());
+                                               Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+                                               ts.SetBad(i, damage);
                                                hitSome = true;
                                        } else {
                                                ts.Unselect(i);
@@ -311,7 +322,9 @@ void BattleState::CalculateDamage() {
                        if (hitSome) return;
                        for (int i(0); i < MaxMonsters(); ++i) {
                                if (MonsterAt(i).Health() > 0) {
-                                       ts.SetBad(i, 15);
+                                       const Stats &defenderStats(MonsterAt(i).GetStats());
+                                       Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+                                       ts.SetBad(i, damage);
                                        break;
                                }
                        }
@@ -319,7 +332,9 @@ void BattleState::CalculateDamage() {
                        for (int i(0); i < NumHeroes(); ++i) {
                                if (ts.IsSelected(i)) {
                                        if (HeroAt(i).Health() > 0) {
-                                               ts.SetBad(i, 15);
+                                               const Stats &defenderStats(HeroAt(i).GetStats());
+                                               Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+                                               ts.SetBad(i, damage);
                                                hitSome = true;
                                        } else {
                                                ts.Unselect(i);
@@ -329,7 +344,9 @@ void BattleState::CalculateDamage() {
                        if (hitSome) return;
                        for (int i(0); i < NumHeroes(); ++i) {
                                if (HeroAt(i).Health() > 0) {
-                                       ts.SetBad(i, 15);
+                                       const Stats &defenderStats(HeroAt(i).GetStats());
+                                       Uint16 damage(CalculateDamage(attackerStats, defenderStats));
+                                       ts.SetBad(i, damage);
                                        break;
                                }
                        }
@@ -337,6 +354,11 @@ void BattleState::CalculateDamage() {
        }
 }
 
+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;
+}
+
 void BattleState::ApplyDamage() {
        if (attackCursor < 0) return;
        AttackChoice &ac(CurrentAttack().isMonster ? monsterAttacks[CurrentAttack().index] : AttackChoiceAt(CurrentAttack().index));
index 7d65c14a23d8a1d08890ac0d7c52a4512351287c..578098fa40f4d6c86c1ee41628be71ba231314fe 100644 (file)
@@ -41,6 +41,7 @@ namespace graphics {
 namespace battle {
 
 class PartyLayout;
+class Stats;
 
 class BattleState
 : public app::State {
@@ -157,6 +158,8 @@ private:
        void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
        void LoadInventory();
 
+       Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
+
 private:
        SDL_Surface *background;
        const PartyLayout *monstersLayout;
index ab3b23e9b9c396e023516e539f640501a76ff831..a3361c6045e0e368bfcbde718d51e80cfcde96ea 100644 (file)
@@ -29,13 +29,6 @@ Hero::Hero()
 , maxMana(0)
 , mana(0)
 
-, attack(0)
-, defense(0)
-, agility(0)
-, intelligence(0)
-, gut(0)
-, magicResistance(0)
-
 , level(0)
 , ip(0) {
 
@@ -51,6 +44,12 @@ void Hero::SubtractHealth(int amount) {
                health = 0;
        } else {
                health -= amount;
+               int ipGain(amount * 255 / health);
+               if (ip + ipGain > 255) {
+                       ip = 255;
+               } else {
+                       ip += ipGain;
+               }
        }
 }
 
index 7abfb68fb35ae5f8a0cd16e243678546cf58074e..5676cf93047e8c1d268086d92908d776aa1bc5c8 100644 (file)
@@ -8,6 +8,8 @@
 #ifndef BATTLE_HERO_H_
 #define BATTLE_HERO_H_
 
+#include "Stats.h"
+
 #include <vector>
 #include <SDL.h>
 
@@ -49,12 +51,8 @@ public:
        Uint8 IP() const { return ip; }
        int RelativeIP(int max) const { return IP() * max / MaxIP(); }
 
-       Uint16 Attack() const { return attack; }
-       Uint16 Defense() const { return defense; }
-       Uint16 Agility() const { return agility; }
-       Uint16 Intelligence() const { return intelligence; }
-       Uint16 Gut() const { return gut; }
-       Uint16 MagicResistance() const { return magicResistance; }
+       Stats &GetStats() { return stats; }
+       const Stats &GetStats() const { return stats; }
 
        common::Item *Weapon() { return weapon; }
        common::Item *Armor() { return armor; }
@@ -96,6 +94,8 @@ public:
        void SetMana(Uint16 m) { mana = m; }
        void SetIP(Uint8 i) { ip = i; }
 
+       void SetStats(const Stats &s) { stats = s; }
+
        void SetWeapon(common::Item *i) { weapon = i; }
        void SetArmor(common::Item *i) { armor = i; }
        void SetShield(common::Item *i) { shield = i; }
@@ -130,12 +130,7 @@ private:
        Uint16 maxHealth, health;
        Uint16 maxMana, mana;
 
-       Uint16 attack;
-       Uint16 defense;
-       Uint16 agility;
-       Uint16 intelligence;
-       Uint16 gut;
-       Uint16 magicResistance;
+       Stats stats;
 
        Uint8 level;
        Uint8 ip;
index 033614231ff0cea9ffec691b5c0dfcf307822901..ec179e488d3b5b0e19212204f0fff08a2585d9a1 100644 (file)
@@ -21,13 +21,6 @@ Monster::Monster()
 , maxMana(0)
 , mana(0)
 
-, attack(0)
-, defense(0)
-, agility(0)
-, intelligence(0)
-, gut(0)
-, magicResistance(0)
-
 , expReward(0)
 , goldReward(0)
 
index f79eaab3e1fc7e8dc807ed85b10cef677ed73f81..50a18076d357dff9fc9050caee4fd078c809c1ee 100644 (file)
@@ -8,6 +8,8 @@
 #ifndef BATTLE_MONSTER_H_
 #define BATTLE_MONSTER_H_
 
+#include "Stats.h"
+
 #include <SDL.h>
 
 namespace graphics { class Sprite; }
@@ -34,12 +36,8 @@ public:
        Uint16 Mana() const { return mana; }
        int RelativeMana(int max) const { return mana * max / maxMana; }
 
-       Uint16 Attack() const { return attack; }
-       Uint16 Defense() const { return defense; }
-       Uint16 Agility() const { return agility; }
-       Uint16 Intelligence() const { return intelligence; }
-       Uint16 Gut() const { return gut; }
-       Uint16 MagicResistance() const { return magicResistance; }
+       Stats &GetStats() { return stats; }
+       const Stats &GetStats() const { return stats; }
 
        Uint16 ExpReward() const { return expReward; }
        Uint16 GoldReward() const { return goldReward; }
@@ -54,8 +52,13 @@ public:
 public:
        void SetName(const char *n) { name = n; }
        void SetSprite(graphics::Sprite *s) { sprite = s; }
+       void SetLevel(Uint8 l) { level = l; }
        void SetMaxHealth(Uint16 m) { maxHealth = m; }
        void SetHealth(Uint16 h) { health = h; }
+       void SetMaxMana(Uint16 m) { maxMana = m; }
+       void SetMana(Uint16 m) { mana = m; }
+       void SetStats(const Stats &s) { stats = s; }
+       void SetReward(Uint16 exp, Uint16 gold) { expReward = exp; goldReward = gold; }
 
 private:
        const char *name;
@@ -67,12 +70,7 @@ private:
        Uint16 maxHealth, health;
        Uint16 maxMana, mana;
 
-       Uint16 attack;
-       Uint16 defense;
-       Uint16 agility;
-       Uint16 intelligence;
-       Uint16 gut;
-       Uint16 magicResistance;
+       Stats stats;
 
        Uint16 expReward, goldReward;
 
diff --git a/src/battle/Stats.cpp b/src/battle/Stats.cpp
new file mode 100644 (file)
index 0000000..ba15898
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Stats.cpp
+ *
+ *  Created on: Aug 19, 2012
+ *      Author: holy
+ */
+
+#include "Stats.h"
+
+namespace battle {
+
+Stats::Stats()
+: attack(0)
+, defense(0)
+, strength(0)
+, agility(0)
+, intelligence(0)
+, gut(0)
+, magicResistance(0) {
+
+}
+
+Stats::Stats(Uint16 attack, Uint16 defense, Uint16 strength, Uint16 agility, Uint16 intelligence, Uint8 gut, Uint16 magicResistance)
+: attack(attack)
+, defense(defense)
+, strength(strength)
+, agility(agility)
+, intelligence(intelligence)
+, gut(gut)
+, magicResistance(magicResistance) {
+
+}
+
+}
diff --git a/src/battle/Stats.h b/src/battle/Stats.h
new file mode 100644 (file)
index 0000000..b935e02
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Stats.h
+ *
+ *  Created on: Aug 19, 2012
+ *      Author: holy
+ */
+
+#ifndef BATTLE_STATS_H_
+#define BATTLE_STATS_H_
+
+#include <SDL.h>
+
+namespace battle {
+
+class Stats {
+
+public:
+       Stats();
+       Stats(Uint16 attack, Uint16 defense, Uint16 strength, Uint16 agility, Uint16 intelligence, Uint8 gut, Uint16 magicResistance);
+
+public:
+       Uint16 Attack() const { return attack; }
+       Uint16 Defense() const { return defense; }
+       Uint16 Strength() const { return strength; }
+       Uint16 Agility() const { return agility; }
+       Uint16 Intelligence() const { return intelligence; }
+       Uint8 Gut() const { return gut; }
+       Uint16 MagicResistance() const { return magicResistance; }
+
+private:
+       Uint16 attack;
+       Uint16 defense;
+       Uint16 strength;
+       Uint16 agility;
+       Uint16 intelligence;
+       Uint16 magicResistance;
+       Uint8 gut;
+
+};
+
+}
+
+#endif /* BATTLE_STATS_H_ */
index 841dc616f35cee60c181ee43324b29f197942ac1..987a985cc74083e76a29953a357b490ea12947fe 100644 (file)
@@ -12,6 +12,7 @@
 #include "battle/Monster.h"
 #include "battle/PartyLayout.h"
 #include "battle/Resources.h"
+#include "battle/Stats.h"
 #include "common/Ikari.h"
 #include "common/Inventory.h"
 #include "common/Item.h"
@@ -40,6 +41,7 @@ using battle::BattleState;
 using battle::Hero;
 using battle::Monster;
 using battle::PartyLayout;
+using battle::Stats;
 using common::Ikari;
 using common::Inventory;
 using common::Item;
@@ -89,10 +91,13 @@ int main(int argc, char **argv) {
                SDL_Surface *monsterImg(IMG_Load("test-data/monster.png"));
                Sprite dummySprite(monsterImg, 64, 64);
                Monster monster;
-               monster.SetName("Monster");
+               monster.SetName("Lizard");
                monster.SetSprite(&dummySprite);
-               monster.SetMaxHealth(10);
-               monster.SetHealth(10);
+               monster.SetLevel(1);
+               monster.SetMaxHealth(8);
+               monster.SetHealth(8);
+               monster.SetStats(Stats(14, 6, 6, 6, 6, 6, 6));
+               monster.SetReward(3, 5);
 
                SDL_Surface *maximImg(IMG_Load("test-data/maxim.png"));
                Sprite maximSprite(maximImg, 64, 64);
@@ -105,6 +110,7 @@ int main(int argc, char **argv) {
                maxim.SetMaxMana(20);
                maxim.SetMana(20);
                maxim.SetIP(0);
+               maxim.SetStats(Stats(28, 22, 28, 17, 14, 100, 10));
                ComplexAnimation maximAttackAnimation(&maximSprite, framerate);
                // TODO: cross check double frames; could be related to differences in framerates
                maximAttackAnimation.AddFrames(1, 0, Vector<int>(0,  0), 7); // TODO: maybe this could also be a pause before the battle animation
@@ -137,7 +143,8 @@ int main(int argc, char **argv) {
                selan.SetHealth(28);
                selan.SetMaxMana(23);
                selan.SetMana(23);
-               selan.SetIP(1);
+               selan.SetIP(0);
+               selan.SetStats(Stats(23, 21, 23, 19, 22, 80, 13));
                ComplexAnimation selanAttackAnimation(&selanSprite, framerate);
                selanAttackAnimation.AddFrames(1, 0, Vector<int>(4, 0), 2);
                selanAttackAnimation.AddFrame(1, 0, Vector<int>(8, 2));
@@ -171,7 +178,8 @@ int main(int argc, char **argv) {
                guy.SetHealth(38);
                guy.SetMaxMana(0);
                guy.SetMana(0);
-               guy.SetIP(254);
+               guy.SetIP(0);
+               guy.SetStats(Stats(38, 25, 38, 13, 8, 90, 8));
                ComplexAnimation guyAttackAnimation(&guySprite, framerate);
                guyAttackAnimation.AddFrames(1, 0, Vector<int>(-4, 0), 2);
                guyAttackAnimation.AddFrames(1, 0, Vector<int>(-8, 0), 2);
@@ -199,7 +207,8 @@ int main(int argc, char **argv) {
                dekar.SetHealth(38);
                dekar.SetMaxMana(0);
                dekar.SetMana(0);
-               dekar.SetIP(255);
+               dekar.SetIP(0);
+               dekar.SetStats(Stats(46, 29, 46, 13, 7, 100, 5));
                ComplexAnimation dekarAttackAnimation(&dekarSprite, framerate);
                dekarAttackAnimation.AddFrame(1, 0, Vector<int>(4, 0));
                dekarAttackAnimation.AddFrame(1, 0, Vector<int>(8, 2));