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"
15 using graphics::Animation;
16 using graphics::Sprite;
17 using loader::FieldDescription;
18 using loader::Interpreter;
19 using loader::TypeDescription;
47 memset(equipment, 0, sizeof(equipment));
51 void Hero::SubtractHealth(int amount) {
52 if (amount > Health()) {
56 int ipGain(amount * 255 / health);
57 if (ip + ipGain > 255) {
66 int Hero::NextLevel() const {
67 int levelOffset(Level() - 1);
68 if (levelOffset < numLevels) {
69 return levelLadder[levelOffset].Experience() - Experience();
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()));
83 while (remain >= NextLevel()) {
84 const LevelUp &lup = levelLadder[level - 1];
85 int added = NextLevel();
89 maxHealth += lup.MaxHealth();
90 maxMana += lup.MaxMagic();
93 info.push_back(Upgrade(name, Upgrade::LEVEL_UP, level));
95 if (lup.MaxHealth() > 0) {
96 info.push_back(Upgrade(name, Upgrade::MAX_HEALTH, lup.MaxHealth()));
98 if (lup.MaxMagic() > 0) {
99 info.push_back(Upgrade(name, Upgrade::MAX_MAGIC, lup.MaxMagic()));
101 if (lup.Attack() > 0) {
102 info.push_back(Upgrade(name, Upgrade::ATTACK, lup.Attack()));
104 if (lup.Defense() > 0) {
105 info.push_back(Upgrade(name, Upgrade::DEFENSE, lup.Defense()));
107 if (lup.Strength() > 0) {
108 info.push_back(Upgrade(name, Upgrade::STRENGTH, lup.Strength()));
110 if (lup.Agility() > 0) {
111 info.push_back(Upgrade(name, Upgrade::AGILITY, lup.Agility()));
113 if (lup.Intelligence() > 0) {
114 info.push_back(Upgrade(name, Upgrade::INTELLIGENCE, lup.Intelligence()));
117 info.push_back(Upgrade(name, Upgrade::GUT, lup.Gut()));
119 if (lup.MagicResistance() > 0) {
120 info.push_back(Upgrade(name, Upgrade::MAGIC_RESISTANCE, lup.MagicResistance()));
123 if (level > numLevels) {
124 info.push_back(Upgrade(
125 name, Upgrade::LEVEL_NEXT, NextLevel()));
129 experience += remain;
130 info.push_back(Upgrade(
131 name, Upgrade::LEVEL_NEXT, NextLevel()));
135 bool Hero::CanEquip(const Item &item) const {
136 return useMask & item.HeroMask();
139 bool Hero::CanInvoke(const Spell &spell) const {
140 return useMask & spell.HeroMask();
144 void Hero::CreateTypeDescription() {
147 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Hero"));
148 td.SetConstructor(&Construct);
149 td.SetSize(sizeof(Hero));
151 td.AddField("name", FieldDescription(((char *)&h.name) - ((char *)&h), Interpreter::STRING_ID).SetReferenced());
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));
159 td.AddField("stats", FieldDescription(((char *)&h.stats) - ((char *)&h), Stats::TYPE_ID));
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());
164 td.AddField("useMask", FieldDescription(((char *)&h.useMask) - ((char *)&h), Interpreter::NUMBER_ID));
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"));
171 td.AddField("mapEntity", FieldDescription(((char *)&h.mapEntity) - ((char *)&h), Entity::TYPE_ID).SetDescription("the entity representing the hero on maps"));
174 void Hero::Construct(void *data) {