1 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2004, 2005 Andrea Paternesi
3 // Copyright (C) 2007, 2008 Ben Asselstine
4 // Copyright (C) 2008 Ole Laursen
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Library General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <sigc++/functors/mem_fun.h>
27 #include "stacklist.h"
28 #include "templelist.h"
29 #include "heroproto.h"
32 #include "xmlhelper.h"
33 #include "playerlist.h"
34 #include "QuestsManager.h"
36 std::string Hero::d_tag = "hero";
39 Hero::Hero(const HeroProto& a)
40 : Army (dynamic_cast<const ArmyProto&>(a)), d_name(a.getName()),
41 d_gender(Gender(a.getGender()))
44 d_backpack = new Backpack();
45 d_owner = Playerlist::getInstance()->getPlayer(a.getOwnerId());
49 : Army(h, h.d_owner), d_name(h.d_name), d_gender(h.d_gender)
51 d_backpack = new Backpack(*h.d_backpack);
54 Hero::Hero(XML_Helper* helper)
57 helper->getData(d_name, "name");
58 std::string gender_str;
59 if (!helper->getData(gender_str, "gender"))
62 d_gender = genderFromString(gender_str);
63 helper->registerTag(Backpack::d_tag,
64 sigc::mem_fun(*this, &Hero::loadBackpack));
73 bool Hero::save(XML_Helper* helper) const
76 std::list<Item*>::const_iterator it;
78 retval &= helper->openTag(Hero::d_tag);
80 retval &= helper->saveData("name", d_name);
81 std::string gender_str = genderToString(Hero::Gender(d_gender));
82 retval &= helper->saveData("gender", gender_str);
83 retval &= saveData(helper);
85 // Now save the backpack
86 retval &= d_backpack->save(helper);
88 retval &= helper->closeTag();
93 bool Hero::loadBackpack(std::string tag, XML_Helper* helper)
95 if (tag == Backpack::d_tag)
97 d_backpack = new Backpack(helper);
103 guint32 Hero::getStat(Stat stat, bool modified) const
106 guint32 value = Army::getStat(stat, modified);
111 // Add item bonuses that affect only this hero
112 if (stat == STRENGTH)
113 bonus += d_backpack->countStrengthBonuses();
115 return value + bonus;
118 guint32 Hero::calculateNaturalCommand()
121 guint32 strength = getStat(STRENGTH, true);
124 else if (strength > 6)
126 else if (strength > 3)
132 std::string Hero::genderToString(const Hero::Gender gender)
143 return "Hero::FEMALE";
146 return "Hero::FEMALE";
149 Hero::Gender Hero::genderFromString(const std::string str)
151 if (str.size() > 0 && isdigit(str.c_str()[0]))
152 return Hero::Gender(atoi(str.c_str()));
153 if (str == "Hero::MALE")
155 else if (str == "Hero::NONE")
157 else if (str == "Hero::FEMALE")
162 bool Hero::canGainLevel() const
164 return getXP() >= getXpNeededForNextLevel();
167 guint32 Hero::getXpNeededForNextLevel() const
169 return xp_per_level * getLevel();
172 int Hero::computeLevelGain(Stat stat) const
174 if (stat == MOVE_BONUS || stat == ARMY_BONUS || stat == SHIP)
190 int Hero::gainLevel(Stat stat)
195 if (stat == MOVE_BONUS || stat == ARMY_BONUS || stat == SHIP ||
196 stat == MOVES_MULTIPLIER)
202 int delta = computeLevelGain(stat);
207 if (d_strength > MAX_ARMY_STRENGTH)
208 d_strength = MAX_ARMY_STRENGTH;
214 d_max_moves += delta;
226 bool Hero::hasQuest() const
228 return QuestsManager::getInstance()->getHeroQuest(getId()) != NULL;