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