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
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.
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.
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
22 #include <sigc++/functors/mem_fun.h>
25 #include "xmlhelper.h"
26 #include "playerlist.h"
31 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
34 std::string Ruinlist::d_tag = "ruinlist";
36 Ruinlist* Ruinlist::s_instance = 0;
38 Ruinlist* Ruinlist::getInstance()
41 s_instance = new Ruinlist();
46 Ruinlist* Ruinlist::getInstance(XML_Helper* helper)
51 s_instance = new Ruinlist(helper);
55 void Ruinlist::deleteInstance()
67 Ruinlist::Ruinlist(XML_Helper* helper)
69 helper->registerTag(Ruin::d_tag, sigc::mem_fun(this, &Ruinlist::load));
72 bool Ruinlist::save(XML_Helper* helper) const
76 retval &= helper->openTag(Ruinlist::d_tag);
78 for (const_iterator it = begin(); it != end(); it++)
79 retval &= (*it)->save(helper);
81 retval &= helper->closeTag();
86 bool Ruinlist::load(std::string tag, XML_Helper* helper)
88 // Shouldn't happen, but one never knows...
89 if (tag != Ruin::d_tag)
92 guint32 width = GameMap::getInstance()->getCityset()->getRuinTileWidth();
93 add(new Ruin(helper, width));
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));
104 static bool isHiddenAndNotOwnedByActivePlayer(void *r)
106 Ruin *ruin = ((Ruin *)r);
107 if (ruin->isHidden() == true &&
108 ruin->getOwner() != Playerlist::getInstance()->getActiveplayer())
113 static bool isFogged(void *r)
115 return ((Ruin*)r)->isVisible(Playerlist::getViewingplayer()) == false;
118 static bool isSearched(void *r)
120 return ((Ruin*)r)->isSearched();
123 Ruin* Ruinlist::getNearestUnsearchedRuin(const Vector<int>& pos) const
125 std::list<bool (*)(void *)> filters;
126 filters.push_back(isHiddenAndNotOwnedByActivePlayer);
127 filters.push_back(isSearched);
128 return getNearestObject(pos, &filters);
130 Ruin* Ruinlist::getNearestUnsearchedRuin(const Vector<int>& pos, int dist) const
132 Ruin *r = getNearestUnsearchedRuin(pos);
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)
141 Ruin* Ruinlist::getNearestRuin(const Vector<int>& pos) const
143 std::list<bool (*)(void *)> filters;
144 filters.push_back(isHiddenAndNotOwnedByActivePlayer);
145 return getNearestObject(pos, &filters);
148 Ruin* Ruinlist::getNearestRuin(const Vector<int>& pos, int dist) const
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)
157 Ruin* Ruinlist::getNearestVisibleRuin(const Vector<int>& pos) const
159 std::list<bool (*)(void *)> filters;
160 filters.push_back(isFogged);
161 filters.push_back(isHiddenAndNotOwnedByActivePlayer);
162 return getNearestObject(pos, &filters);
165 Ruin* Ruinlist::getNearestVisibleRuin(const Vector<int>& pos, int dist) const
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)
174 void Ruinlist::changeOwnership(Player *old_owner, Player *new_owner)
176 for (iterator it = begin(); it != end(); it++)
177 if ((*it)->getOwner() == old_owner)
178 (*it)->setOwner(new_owner);
181 guint32 Ruinlist::countUnexploredRuins(Player *owner) const
184 for (const_iterator it = begin(); it != end(); it++)
187 if (ruin->isHidden() == true && ruin->getOwner() != owner)
189 if (ruin->isSearched() == false)
195 guint32 Ruinlist::countExploredRuins(Player *owner) const
198 for (const_iterator it = begin(); it != end(); it++)
201 if (ruin->isHidden() == true && ruin->getOwner() != owner)
203 if (ruin->isSearched() == true && ruin->getOwner() == owner)