]> git.localhorst.tv Git - l2e.git/blob - src/common/Hero.cpp
moved upgrade process to battle class
[l2e.git] / src / common / Hero.cpp
1 #include "Hero.h"
2
3 #include "Item.h"
4 #include "Spell.h"
5 #include "Upgrade.h"
6 #include "../graphics/Animation.h"
7 #include "../graphics/Sprite.h"
8 #include "../loader/Interpreter.h"
9 #include "../loader/TypeDescription.h"
10 #include "../map/Entity.h"
11
12 #include <cstring>
13
14 using graphics::Animation;
15 using graphics::Sprite;
16 using loader::FieldDescription;
17 using loader::Interpreter;
18 using loader::TypeDescription;
19 using map::Entity;
20 using std::memset;
21 using std::vector;
22
23
24 namespace common {
25
26 Hero::Hero()
27 : name(0)
28
29 , maxHealth(0)
30 , health(0)
31 , maxMana(0)
32 , mana(0)
33 , ip(0)
34
35 , level(0)
36 , experience(0)
37 , levelLadder(0)
38 , numLevels(0)
39
40 , useMask(0)
41
42 , battleSprite(0)
43 , meleeAnimation(0)
44 , attackAnimation(0)
45 , spellAnimation(0) {
46         memset(equipment, 0, sizeof(equipment));
47 }
48
49
50 void Hero::SubtractHealth(int amount) {
51         if (amount > Health()) {
52                 health = 0;
53         } else {
54                 health -= amount;
55                 int ipGain(amount * 255 / health);
56                 if (ip + ipGain > 255) {
57                         ip = 255;
58                 } else {
59                         ip += ipGain;
60                 }
61         }
62 }
63
64
65 int Hero::NextLevel() const {
66         int levelOffset(Level() - 1);
67         if (levelOffset < numLevels) {
68                 return levelLadder[levelOffset] - Experience();
69         } else {
70                 return 0;
71         }
72 }
73
74 void Hero::AddExperience(int exp, vector<Upgrade> &info) {
75         if (level > numLevels) {
76                 // don't award any experience if at highest level
77                 info.push_back(Upgrade(
78                                 name, Upgrade::LEVEL_NEXT, NextLevel()));
79                 return;
80         }
81         int remain = exp;
82         while (remain >= NextLevel()) {
83                 int added = NextLevel();
84                 experience += added;
85                 remain -= added;
86                 ++level;
87
88                 info.push_back(Upgrade(name, Upgrade::LEVEL_UP, level));
89
90                 // TODO: upgrade attributes and push info
91
92                 if (level > numLevels) {
93                         info.push_back(Upgrade(
94                                         name, Upgrade::LEVEL_NEXT, NextLevel()));
95                         return;
96                 }
97         }
98         experience += remain;
99         info.push_back(Upgrade(
100                         name, Upgrade::LEVEL_NEXT, NextLevel()));
101 }
102
103
104 bool Hero::CanEquip(const Item &item) const {
105         return useMask & item.HeroMask();
106 }
107
108 bool Hero::CanInvoke(const Spell &spell) const {
109         return useMask & spell.HeroMask();
110 }
111
112
113 void Hero::CreateTypeDescription() {
114         Hero h;
115
116         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Hero"));
117         td.SetConstructor(&Construct);
118         td.SetSize(sizeof(Hero));
119
120         td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), Interpreter::STRING_ID).SetReferenced());
121
122         td.AddField("maxHealth", FieldDescription(((char *)&h.maxHealth) - ((char *)&h), Interpreter::NUMBER_ID));
123         td.AddField("health", FieldDescription(((char *)&h.health) - ((char *)&h), Interpreter::NUMBER_ID));
124         td.AddField("maxMana", FieldDescription(((char *)&h.maxMana) - ((char *)&h), Interpreter::NUMBER_ID));
125         td.AddField("mana", FieldDescription(((char *)&h.mana) - ((char *)&h), Interpreter::NUMBER_ID));
126         td.AddField("ip", FieldDescription(((char *)&h.ip) - ((char *)&h), Interpreter::NUMBER_ID));
127
128         td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), Stats::TYPE_ID));
129
130         td.AddField("level", FieldDescription(((char *)&h.level) - ((char *)&h), Interpreter::NUMBER_ID));
131         td.AddField("ladder", FieldDescription(((char *)&h.levelLadder) - ((char *)&h), Interpreter::NUMBER_ID).SetAggregate());
132
133         td.AddField("useMask", FieldDescription(((char *)&h.useMask) - ((char *)&h), Interpreter::NUMBER_ID));
134
135         td.AddField("battleSprite", FieldDescription(((char *)&h.battleSprite) - ((char *)&h), Sprite::TYPE_ID).SetReferenced().SetDescription("the sprite used for battle scenes"));
136         td.AddField("attackAnimation", FieldDescription(((char *)&h.attackAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for physical attacks"));
137         td.AddField("spellAnimation", FieldDescription(((char *)&h.spellAnimation) - ((char *)&h), Animation::TYPE_ID).SetReferenced().SetDescription("the animation played for magical attacks"));
138         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"));
139
140         td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), Entity::TYPE_ID).SetDescription("the entity representing the hero on maps"));
141 }
142
143 void Hero::Construct(void *data) {
144         new (data) Hero;
145 }
146
147 }