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