1 // Copyright (C) 2001, 2003 Michael Bartl
2 // Copyright (C) 2002, 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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.
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.
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
21 #include "playerlist.h"
23 #include "rewardlist.h"
27 std::string Ruin::d_tag = "ruin";
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!")),
33 d_type(type), d_occupant(occupant), d_hidden(hidden), d_owner(owner),
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++)
42 Vector<int> pos = getPos() + Vector<int>(i, j);
43 GameMap::getInstance()->getTile(pos)->setBuilding(Maptile::RUIN);
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)
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)
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)
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)
74 helper->getData(ui, "owner");
75 if (ui != MAX_PLAYERS)
76 d_owner = Playerlist::getInstance()->getPlayer(ui);
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++)
87 Vector<int> pos = getPos() + Vector<int>(i, j);
88 GameMap::getInstance()->getTile(pos)->setBuilding(Maptile::RUIN);
100 bool Ruin::save(XML_Helper* helper) const
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);
116 retval &= helper->saveData("owner", d_owner->getId());
118 retval &= helper->saveData("owner", MAX_PLAYERS);
120 retval &= d_occupant->save(helper);
121 if (d_sage == false && d_reward)
122 retval &= d_reward->save(helper);
123 retval &= helper->closeTag();
128 bool Ruin::load(std::string tag, XML_Helper* helper)
130 if (tag == Reward::d_tag)
133 std::string type_str;
134 helper->getData(type_str, "type");
135 t = Reward::rewardTypeFromString(type_str);
139 d_reward = new Reward_Gold(helper); break;
141 d_reward = new Reward_Allies(helper); break;
143 d_reward = new Reward_Item(helper); break;
145 d_reward = new Reward_Ruin(helper); break;
147 d_reward = new Reward_Map(helper); break;
152 if (tag == Stack::d_tag)
154 Stack* s = new Stack(helper);
162 void Ruin::populateWithRandomReward()
165 if (getType() == Ruin::STRONGHOLD)
166 num = 1 + (rand() % 2);
170 setReward(new Reward_Gold(Reward_Gold::getRandomGoldPieces()));
173 const ArmyProto *a = Reward_Allies::randomArmyAlly();
174 setReward(new Reward_Allies(a, Reward_Allies::getRandomAmountOfAllies()));
178 Reward *reward = Rewardlist::getInstance()->popRandomItemReward();
181 else //no items left to give!
182 setReward(new Reward_Gold(Reward_Gold::getRandomGoldPieces()));
186 std::string Ruin::ruinTypeToString(const Ruin::Type type)
193 case Ruin::STRONGHOLD:
194 return "Ruin::STRONGHOLD";
200 Ruin::Type Ruin::ruinTypeFromString(const std::string str)
202 if (str.size() > 0 && isdigit(str.c_str()[0]))
203 return Ruin::Type(atoi(str.c_str()));
204 if (str == "Ruin::RUIN")
206 else if (str == "Ruin::STRONGHOLD")
207 return Ruin::STRONGHOLD;
211 Sage* Ruin::generateSage() const