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