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