]> git.localhorst.tv Git - l2e.git/blobdiff - src/common/Hero.cpp
put stat increments in level ladder
[l2e.git] / src / common / Hero.cpp
index 98b92ca2fdafbbef48bad6fa86216060b380bc75..79581569c4002335402d9e3117069221de5714c8 100644 (file)
@@ -1,16 +1,26 @@
-/*
- * Hero.cpp
- *
- *  Created on: Oct 7, 2012
- *      Author: holy
- */
-
 #include "Hero.h"
 
+#include "Item.h"
+#include "LevelUp.h"
+#include "Spell.h"
+#include "Upgrade.h"
+#include "../graphics/Animation.h"
+#include "../graphics/Sprite.h"
+#include "../loader/Interpreter.h"
 #include "../loader/TypeDescription.h"
+#include "../map/Entity.h"
+
+#include <cstring>
 
+using graphics::Animation;
+using graphics::Sprite;
 using loader::FieldDescription;
+using loader::Interpreter;
 using loader::TypeDescription;
+using map::Entity;
+using std::memset;
+using std::vector;
+
 
 namespace common {
 
@@ -24,21 +34,17 @@ Hero::Hero()
 , ip(0)
 
 , level(0)
+, experience(0)
+, levelLadder(0)
+, numLevels(0)
 
-, weapon(0)
-, armor(0)
-, shield(0)
-, helmet(0)
-, ring(0)
-, jewel(0)
+, useMask(0)
 
 , battleSprite(0)
 , meleeAnimation(0)
 , attackAnimation(0)
-, spellAnimation(0)
-
-, mapSprite(0) {
-
+, spellAnimation(0) {
+       memset(equipment, 0, sizeof(equipment));
 }
 
 
@@ -57,37 +63,112 @@ void Hero::SubtractHealth(int amount) {
 }
 
 
+int Hero::NextLevel() const {
+       int levelOffset(Level() - 1);
+       if (levelOffset < numLevels) {
+               return levelLadder[levelOffset].Experience() - Experience();
+       } else {
+               return 0;
+       }
+}
+
+void Hero::AddExperience(int exp, vector<Upgrade> &info) {
+       if (level > numLevels) {
+               // don't award any experience if at highest level
+               info.push_back(Upgrade(
+                               name, Upgrade::LEVEL_NEXT, NextLevel()));
+               return;
+       }
+       int remain = exp;
+       while (remain >= NextLevel()) {
+               const LevelUp &lup = levelLadder[level - 1];
+               int added = NextLevel();
+               experience += added;
+               remain -= added;
+               ++level;
+               maxHealth += lup.MaxHealth();
+               maxMana += lup.MaxMagic();
+               stats += lup;
+
+               info.push_back(Upgrade(name, Upgrade::LEVEL_UP, level));
+
+               if (lup.MaxHealth() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::MAX_HEALTH, lup.MaxHealth()));
+               }
+               if (lup.MaxMagic() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::MAX_MAGIC, lup.MaxMagic()));
+               }
+               if (lup.Attack() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::ATTACK, lup.Attack()));
+               }
+               if (lup.Defense() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::DEFENSE, lup.Defense()));
+               }
+               if (lup.Strength() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::STRENGTH, lup.Strength()));
+               }
+               if (lup.Agility() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::AGILITY, lup.Agility()));
+               }
+               if (lup.Intelligence() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::INTELLIGENCE, lup.Intelligence()));
+               }
+               if (lup.Gut() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::GUT, lup.Gut()));
+               }
+               if (lup.MagicResistance() > 0) {
+                       info.push_back(Upgrade(name, Upgrade::MAGIC_RESISTANCE, lup.MagicResistance()));
+               }
+
+               if (level > numLevels) {
+                       info.push_back(Upgrade(
+                                       name, Upgrade::LEVEL_NEXT, NextLevel()));
+                       return;
+               }
+       }
+       experience += remain;
+       info.push_back(Upgrade(
+                       name, Upgrade::LEVEL_NEXT, NextLevel()));
+}
+
+
+bool Hero::CanEquip(const Item &item) const {
+       return useMask & item.HeroMask();
+}
+
+bool Hero::CanInvoke(const Spell &spell) const {
+       return useMask & spell.HeroMask();
+}
+
+
 void Hero::CreateTypeDescription() {
        Hero h;
 
-       int animationId(TypeDescription::GetTypeId("Animation"));
-       int numberId(TypeDescription::GetTypeId("Number"));
-       int spriteId(TypeDescription::GetTypeId("Sprite"));
-       int statsId(TypeDescription::GetTypeId("Stats"));
-       int stringId(TypeDescription::GetTypeId("String"));
-
-       TypeDescription &td(TypeDescription::CreateOrGet("Hero"));
+       TypeDescription &td(TypeDescription::Create(TYPE_ID, "Hero"));
        td.SetConstructor(&Construct);
        td.SetSize(sizeof(Hero));
 
-       td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), stringId, true));
+       td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), Interpreter::STRING_ID).SetReferenced());
+
+       td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), Interpreter::NUMBER_ID));
+       td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), Interpreter::NUMBER_ID));
+       td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), Interpreter::NUMBER_ID));
+       td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), Interpreter::NUMBER_ID));
+       td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), Interpreter::NUMBER_ID));
 
-       td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), numberId, false));
-       td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), numberId, false));
-       td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), numberId, false));
-       td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), numberId, false));
-       td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), numberId, false));
+       td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), Stats::TYPE_ID));
 
-       td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), statsId, false));
+       td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), Interpreter::NUMBER_ID));
+       td.AddField("ladder", FieldDescription(((char *)&h.levelLadder) - ((char *)&h), LevelUp::TYPE_ID).SetAggregate());
 
-       td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), numberId, false));
+       td.AddField("useMask", FieldDescription(((char *)&h.useMask) - ((char *)&h), Interpreter::NUMBER_ID));
 
-       td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), spriteId, true));
-       td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), animationId, true));
-       td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), animationId, true));
-       td.AddField("meleeAnimation", FieldDescription(((char *)&h.meleeAnimation) - ((char *)&h), animationId, true));
+       td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), Sprite::TYPE_ID).SetReferenced().SetDescription("the sprite used for battle scenes"));
+       td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for physical attacks"));
+       td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for magical attacks"));
+       td.AddField("meleeAnimation", FieldDescription(((char *)&h.meleeAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played on attacked monsters when the hero has no weapon"));
 
-       td.AddField("mapSprite", FieldDescription(((char *)&h.mapSprite) - ((char *)&h), spriteId, true));
+       td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), Entity::TYPE_ID).SetDescription("the entity representing the hero on maps"));
 }
 
 void Hero::Construct(void *data) {