initial commit, lordsawar source, slightly modified
[lordsawar] / src / hero.h
1 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2007, 2008 Ben Asselstine
3 // Copyright (C) 2008 Ole Laursen
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Library General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
18 //  02110-1301, USA.
19
20 #ifndef HERO_H
21 #define HERO_H
22
23 #include <string>
24 #include <vector>
25 #include <list>
26
27 #include "army.h"
28 #include "player.h"
29
30 class Backpack;
31
32 //! A Hero is an Army unit capable of carrying items, going on quests and more.
33 /** 
34  * The Hero class is just like the Army class except that the Hero gets
35  * it's name, and gender set, and it's Army:Stat statistics are augmented by 
36  * Item objects that are carried in a d_backpack.
37  *
38  * Heroes emerge in a City usually along with some powerful allies for a 
39  * certain amount of gold pieces.
40  *
41  * Heroes are special because they can get a new Quest from a Temple, and
42  * search Ruin objects.  They can pick up and drop Item objects, and can
43  * also plant standards into the ground.
44  *
45  * Heroes are also unique in that they can increase in experience levels.
46  */
47 class Hero : public Army
48 {
49     public:
50
51         //! The xml tag of this object in a saved-game file.
52         static std::string d_tag; 
53
54         //! The different genders a Hero unit can have.
55         /**
56          * The purpose of this enumeration is to show the correct 
57          * recruitment picture for Hero units when they emerge in a 
58          * City, and the Player has to decide if they want it or not.
59          */
60         enum Gender {
61           //! The hero has no gender (Not used).
62           NONE = 0, 
63           //! The hero is male.
64           MALE = 1, 
65           //! The hero is female.
66           FEMALE = 2
67         };
68
69         /**
70          * Copies the prototype hero and creates a hero from it.
71          */
72         //! Default constructor.
73         Hero(const HeroProto& a);
74
75         /**
76          * This performs a deep copy, including the Hero's items.
77          */
78         //! Copy constructor.
79         Hero(Hero& h);
80
81         /** 
82          * @param helper   The opened saved-file to read the Hero from.
83          */
84         //! Load a Hero from an opened saved-game file.
85         Hero(XML_Helper* helper);
86
87         //! Destructor.
88         ~Hero();
89
90
91         // Get Methods
92
93         //! Return the name of this hero.
94         virtual std::string getName() const {return d_name;};
95
96         //! Return that this object is a hero.
97         bool isHero() const {return true;};
98
99         //! Return the gender of the hero.
100         guint32 getGender() const {return d_gender;}
101
102         /**
103          * Returns a stat of the hero.  See Army::Stat, and Army::getStat.
104          * 
105          * If modified is set to false, return the "raw", i.e. inherent
106          * value of the hero. Otherwise, all items are checked for a 
107          * bonus.
108          */
109         guint32 getStat(Army::Stat stat, bool modified = true) const;
110
111         //! Returns the backpack of the hero.
112         Backpack* getBackpack() {return d_backpack;}
113
114         
115         // Set Methods
116
117         void setName(std::string name) {d_name = name;};
118
119         //! Set the gender of the hero.
120         void setGender(Gender gender){d_gender = gender;}
121
122
123         // Methods that operate on class data and do not modify the class.
124
125         //! Saves the Hero to a saved-game file.
126         bool save(XML_Helper* helper) const;
127
128         //! Is this hero on a quest?
129         bool hasQuest() const;
130         
131         //! Return the natural command of the hero.
132         /**
133          * Natural command is used for bonus calculations during a Fight.
134          *
135          * @return A number that is added to the strength to other Army and
136          *         Hero units in the Stack. 
137          */
138         guint32 calculateNaturalCommand();
139
140
141         // Static methods
142
143         //! Convert a Hero::Gender string to an enumerated value.
144         static Hero::Gender genderFromString(const std::string str);
145
146         //! Convert a Hero::Gender enumerated value to a string.
147         static std::string genderToString(const Hero::Gender gender);
148
149         /** 
150          * Increase the Hero unit's level, and increase one of three stats;
151          * Stat::STRENGTH, Stat::MOVES, or Stat::SIGHT.
152          *
153          * @param stat     The stat to increase.
154          *
155          * @return How much the statistic increases or -1 upon error 
156          *         (e.g. because the XP is not enough).
157          */
158         //! Increase the Army's level, and increase a given stat.
159         int gainLevel(Stat stat);
160
161         /**
162          * Calculate how much a stat is increased because the Hero unit
163          * has increased it's level.
164          *
165          * @param stat  One of Stat::STRENGTH, Stat::MOVES, or Stat::SIGHT.
166          *
167          * @return The new value of the stat after it is increased.
168          */
169         //! Return how much the stat would be boosted by gaining a level.
170         int computeLevelGain(Stat stat) const;
171
172         //! Checks whether or not the Hero unit can advance a level.
173         bool canGainLevel() const;
174
175         //! Returns how many experience points the next level requires.
176         guint32 getXpNeededForNextLevel() const;
177
178     private:
179         
180         //! Callback for loading the backpack from a saved-game file.
181         bool loadBackpack(std::string tag, XML_Helper* helper);
182
183         //! The hero's backpack that holds any number of Item objects.
184         Backpack *d_backpack;
185
186         //! The name of the hero.
187         std::string d_name;
188
189         //! Gender of the hero
190         Hero::Gender d_gender;
191 };
192
193 #endif //HERO_H