initial commit, lordsawar source, slightly modified
[lordsawar] / src / ruin.cpp
1 // Copyright (C) 2001, 2003 Michael Bartl
2 // Copyright (C) 2002, 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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 #include "ruin.h"
21 #include "playerlist.h"
22 #include "GameMap.h"
23 #include "rewardlist.h"
24 #include <stdlib.h>
25 #include "Sage.h"
26
27 std::string Ruin::d_tag = "ruin";
28
29 Ruin::Ruin(Vector<int> pos, guint32 width, std::string name, int type, Stack* occupant, bool searched, bool hidden, Player *owner, bool sage)
30 :NamedLocation(pos, width, name,
31                 name + _(" is inhabited by monsters and full of treasure!")), 
32     d_searched(searched), 
33     d_type(type), d_occupant(occupant), d_hidden(hidden), d_owner(owner), 
34     d_sage(sage)
35 {
36     d_owner = NULL;
37     d_reward = NULL;
38     //mark the location as being occupied by a ruin on the map
39     for (unsigned int i = 0; i < getSize(); i++)
40       for (unsigned int j = 0; j < getSize(); j++)
41         {
42           Vector<int> pos = getPos() + Vector<int>(i, j);
43           GameMap::getInstance()->getTile(pos)->setBuilding(Maptile::RUIN);
44         }
45 }
46
47 Ruin::Ruin(const Ruin& ruin)
48     :NamedLocation(ruin), d_searched(ruin.d_searched), 
49     d_type(ruin.d_type), d_occupant(ruin.d_occupant), d_hidden(ruin.d_hidden), 
50     d_owner(ruin.d_owner), d_sage(ruin.d_sage), d_reward(ruin.d_reward)
51 {
52 }
53
54 Ruin::Ruin(const Ruin& ruin, Vector<int> pos)
55     :NamedLocation(ruin, pos), d_searched(ruin.d_searched), 
56     d_type(ruin.d_type), d_occupant(ruin.d_occupant), d_hidden(ruin.d_hidden), 
57     d_owner(ruin.d_owner), d_sage(ruin.d_sage), d_reward(ruin.d_reward)
58 {
59 }
60
61 Ruin::Ruin(XML_Helper* helper, guint32 width)
62     :NamedLocation(helper, width), d_type(0), d_occupant(0), 
63     d_hidden(0), d_owner(0), d_sage(0), d_reward(0)
64 {
65     guint32 ui;
66     std::string type_str;
67     helper->getData(type_str, "type");
68     d_type = ruinTypeFromString(type_str);
69     helper->getData(d_searched, "searched");
70     helper->getData(d_sage, "sage");
71     helper->getData(d_hidden, "hidden");
72     if (d_hidden || d_searched)
73       {
74         helper->getData(ui, "owner");
75         if (ui != MAX_PLAYERS)
76           d_owner = Playerlist::getInstance()->getPlayer(ui);
77         else
78           d_owner = NULL;
79       }
80     else
81       d_owner = NULL;
82
83     //mark the location as being occupied by a ruin on the map
84     for (unsigned int i = 0; i < getSize(); i++)
85       for (unsigned int j = 0; j < getSize(); j++)
86         {
87           Vector<int> pos = getPos() + Vector<int>(i, j);
88           GameMap::getInstance()->getTile(pos)->setBuilding(Maptile::RUIN);
89         }
90 }
91
92 Ruin::~Ruin()
93 {
94     if (d_reward)
95         delete d_reward;
96     if (d_occupant)
97         delete d_occupant;
98 }
99
100 bool Ruin::save(XML_Helper* helper) const
101 {
102   bool retval = true;
103
104   retval &= helper->openTag(Ruin::d_tag);
105   retval &= helper->saveData("id", d_id);
106   retval &= helper->saveData("x", getPos().x);
107   retval &= helper->saveData("y", getPos().y);
108   retval &= helper->saveData("name", getName(false));
109   retval &= helper->saveData("description", getDescription());
110   std::string type_str = ruinTypeToString(Ruin::Type(d_type));
111   retval &= helper->saveData("type", type_str);
112   retval &= helper->saveData("searched", d_searched);
113   retval &= helper->saveData("sage", d_sage);
114   retval &= helper->saveData("hidden", d_hidden);
115   if (d_owner != NULL)
116     retval &= helper->saveData("owner", d_owner->getId());
117   else
118     retval &= helper->saveData("owner", MAX_PLAYERS);
119   if (d_occupant)
120     retval &= d_occupant->save(helper);
121   if (d_sage == false && d_reward)
122     retval &= d_reward->save(helper);
123   retval &= helper->closeTag();
124
125   return retval;
126 }
127
128 bool Ruin::load(std::string tag, XML_Helper* helper)
129 {
130   if (tag == Reward::d_tag)
131     {
132         guint32 t;
133         std::string type_str;
134         helper->getData(type_str, "type");
135         t = Reward::rewardTypeFromString(type_str);
136         switch (t)
137           {
138           case  Reward::GOLD:
139             d_reward = new Reward_Gold(helper); break;
140           case  Reward::ALLIES:
141             d_reward = new Reward_Allies(helper); break;
142           case Reward::ITEM:
143             d_reward = new Reward_Item(helper); break;
144           case Reward::RUIN:
145             d_reward = new Reward_Ruin(helper); break;
146           case Reward::MAP:
147             d_reward = new Reward_Map(helper); break;
148           }
149         return true;
150     }
151
152   if (tag == Stack::d_tag)
153     {
154       Stack* s = new Stack(helper);
155       d_occupant = s;
156       return true;
157     }
158
159   return false;
160 }
161
162 void Ruin::populateWithRandomReward()
163 {
164   int num;
165   if (getType() == Ruin::STRONGHOLD)
166     num = 1 + (rand() % 2);
167   else
168     num = rand() % 3;
169   if (num == 0)
170     setReward(new Reward_Gold(Reward_Gold::getRandomGoldPieces()));
171   else if (num == 1)
172     {
173       const ArmyProto *a = Reward_Allies::randomArmyAlly();
174       setReward(new Reward_Allies(a, Reward_Allies::getRandomAmountOfAllies()));
175     }
176   else if (num == 2)
177     {
178       Reward *reward = Rewardlist::getInstance()->popRandomItemReward();
179       if (reward)
180         setReward(reward);
181       else //no items left to give!
182         setReward(new Reward_Gold(Reward_Gold::getRandomGoldPieces()));
183     }
184 }
185
186 std::string Ruin::ruinTypeToString(const Ruin::Type type)
187 {
188   switch (type)
189     {
190       case Ruin::RUIN:
191         return "Ruin::RUIN";
192         break;
193       case Ruin::STRONGHOLD:
194         return "Ruin::STRONGHOLD";
195         break;
196     }
197   return "Ruin::RUIN";
198 }
199
200 Ruin::Type Ruin::ruinTypeFromString(const std::string str)
201 {
202   if (str.size() > 0 && isdigit(str.c_str()[0]))
203     return Ruin::Type(atoi(str.c_str()));
204   if (str == "Ruin::RUIN")
205     return Ruin::RUIN;
206   else if (str == "Ruin::STRONGHOLD")
207     return Ruin::STRONGHOLD;
208   return Ruin::RUIN;
209 }
210
211 Sage* Ruin::generateSage() const
212 {
213   return new Sage();
214 }
215 // End of file