initial commit, lordsawar source, slightly modified
[lordsawar] / src / ruinlist.cpp
1 // Copyright (C) 2000, 2001 Michael Bartl
2 // Copyright (C) 2001, 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2004 John Farrell
4 // Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
5 // Copyright (C) 2007 Ole Laursen
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License as published by
9 //  the Free Software Foundation; either version 3 of the License, or
10 //  (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU Library General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
20 //  02110-1301, USA.
21
22 #include <sigc++/functors/mem_fun.h>
23
24 #include "ruinlist.h"
25 #include "xmlhelper.h"
26 #include "playerlist.h"
27 #include "reward.h"
28 #include "GameMap.h"
29 #include "cityset.h"
30
31 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
32 #define debug(x)
33
34 std::string Ruinlist::d_tag = "ruinlist";
35
36 Ruinlist* Ruinlist::s_instance = 0;
37
38 Ruinlist* Ruinlist::getInstance()
39 {
40     if (s_instance == 0)
41         s_instance = new Ruinlist();
42
43     return s_instance;
44 }
45
46 Ruinlist* Ruinlist::getInstance(XML_Helper* helper)
47 {
48     if (s_instance)
49         deleteInstance();
50
51     s_instance = new Ruinlist(helper);
52     return s_instance;
53 }
54
55 void Ruinlist::deleteInstance()
56 {
57     if (s_instance)
58         delete s_instance;
59
60     s_instance = 0;
61 }
62
63 Ruinlist::Ruinlist()
64 {
65 }
66
67 Ruinlist::Ruinlist(XML_Helper* helper)
68 {
69     helper->registerTag(Ruin::d_tag, sigc::mem_fun(this, &Ruinlist::load));
70 }
71
72 bool Ruinlist::save(XML_Helper* helper) const
73 {
74     bool retval = true;
75
76     retval &= helper->openTag(Ruinlist::d_tag);
77
78     for (const_iterator it = begin(); it != end(); it++)
79         retval &= (*it)->save(helper);
80     
81     retval &= helper->closeTag();
82
83     return retval;
84 }
85
86 bool Ruinlist::load(std::string tag, XML_Helper* helper)
87 {
88     // Shouldn't happen, but one never knows...
89     if (tag != Ruin::d_tag)
90         return false;
91
92     guint32 width = GameMap::getInstance()->getCityset()->getRuinTileWidth();
93     add(new Ruin(helper, width));
94
95     //! since the ruin has only now been copied to its final state, we need
96     //to register the callback for the occupants here.
97     helper->registerTag(Stack::d_tag, sigc::mem_fun(*begin(), &Ruin::load));
98     // same with rewards in ruins
99     helper->registerTag(Reward::d_tag, sigc::mem_fun(*begin(), &Ruin::load));
100
101     return true;
102 }
103
104 static bool isHiddenAndNotOwnedByActivePlayer(void *r)
105 {
106   Ruin *ruin = ((Ruin *)r);
107   if (ruin->isHidden() == true && 
108       ruin->getOwner() != Playerlist::getInstance()->getActiveplayer())
109     return true;
110   return false;
111 }
112
113 static bool isFogged(void *r)
114 {
115   return ((Ruin*)r)->isVisible(Playerlist::getViewingplayer()) == false;
116 }
117
118 static bool isSearched(void *r)
119 {
120   return ((Ruin*)r)->isSearched();
121 }
122
123 Ruin* Ruinlist::getNearestUnsearchedRuin(const Vector<int>& pos) const
124 {
125   std::list<bool (*)(void *)> filters;
126   filters.push_back(isHiddenAndNotOwnedByActivePlayer);
127   filters.push_back(isSearched);
128   return getNearestObject(pos, &filters);
129 }
130 Ruin* Ruinlist::getNearestUnsearchedRuin(const Vector<int>& pos, int dist) const
131 {
132   Ruin *r = getNearestUnsearchedRuin(pos);
133   if (!r)
134     return NULL;
135   if (r->getPos().x <= pos.x + dist && r->getPos().x >= pos.x - dist &&
136       r->getPos().y <= pos.y + dist && r->getPos().y >= pos.y - dist)
137     return r;
138   return NULL;
139 }
140
141 Ruin* Ruinlist::getNearestRuin(const Vector<int>& pos) const
142 {
143   std::list<bool (*)(void *)> filters;
144   filters.push_back(isHiddenAndNotOwnedByActivePlayer);
145   return getNearestObject(pos, &filters);
146 }
147
148 Ruin* Ruinlist::getNearestRuin(const Vector<int>& pos, int dist) const
149 {
150   Ruin *r = getNearestRuin(pos);
151   if (r->getPos().x <= pos.x + dist && r->getPos().x >= pos.x - dist &&
152       r->getPos().y <= pos.y + dist && r->getPos().y >= pos.y - dist)
153     return r;
154   return NULL;
155 }
156
157 Ruin* Ruinlist::getNearestVisibleRuin(const Vector<int>& pos) const
158 {
159   std::list<bool (*)(void *)> filters;
160   filters.push_back(isFogged);
161   filters.push_back(isHiddenAndNotOwnedByActivePlayer);
162   return getNearestObject(pos, &filters);
163 }
164
165 Ruin* Ruinlist::getNearestVisibleRuin(const Vector<int>& pos, int dist) const
166 {
167   Ruin *r = getNearestVisibleRuin(pos);
168   if (r->getPos().x <= pos.x + dist && r->getPos().x >= pos.x - dist &&
169       r->getPos().y <= pos.y + dist && r->getPos().y >= pos.y - dist)
170     return r;
171   return NULL;
172 }
173
174 void Ruinlist::changeOwnership(Player *old_owner, Player *new_owner)
175 {
176   for (iterator it = begin(); it != end(); it++)
177     if ((*it)->getOwner() == old_owner)
178       (*it)->setOwner(new_owner);
179 }
180
181 guint32 Ruinlist::countUnexploredRuins(Player *owner) const
182 {
183   guint32 count = 0;
184   for (const_iterator it = begin(); it != end(); it++)
185     {
186       Ruin *ruin = *it;
187       if (ruin->isHidden() == true && ruin->getOwner() != owner)
188         continue;
189       if (ruin->isSearched() == false)
190         count++;
191     }
192   return count;
193 }
194
195 guint32 Ruinlist::countExploredRuins(Player *owner) const
196 {
197   guint32 count = 0;
198   for (const_iterator it = begin(); it != end(); it++)
199     {
200       Ruin *ruin = *it;
201       if (ruin->isHidden() == true && ruin->getOwner() != owner)
202         continue;
203       if (ruin->isSearched() == true && ruin->getOwner() == owner)
204         count++;
205     }
206   return count;
207 }