1 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include <sigc++/functors/mem_fun.h>
21 #include "vectoredunitlist.h"
22 #include "vectoredunit.h"
24 #include "xmlhelper.h"
28 std::string VectoredUnitlist::d_tag = "vectoredunitlist";
29 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
32 VectoredUnitlist* VectoredUnitlist::s_instance = 0;
34 VectoredUnitlist* VectoredUnitlist::getInstance()
37 s_instance = new VectoredUnitlist();
42 VectoredUnitlist* VectoredUnitlist::getInstance(XML_Helper* helper)
47 s_instance = new VectoredUnitlist(helper);
51 void VectoredUnitlist::deleteInstance()
59 VectoredUnitlist::VectoredUnitlist()
63 VectoredUnitlist::~VectoredUnitlist()
65 for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
69 VectoredUnitlist::VectoredUnitlist(XML_Helper* helper)
71 helper->registerTag(VectoredUnit::d_tag, sigc::mem_fun(this, &VectoredUnitlist::load));
72 helper->registerTag(ArmyProdBase::d_tag, sigc::mem_fun(this, &VectoredUnitlist::load));
75 bool VectoredUnitlist::save(XML_Helper* helper) const
79 retval &= helper->openTag(VectoredUnitlist::d_tag);
81 for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
82 retval &= (*it)->save(helper);
84 retval &= helper->closeTag();
89 bool VectoredUnitlist::load(std::string tag, XML_Helper* helper)
91 if (tag == VectoredUnit::d_tag)
93 VectoredUnit *r = new VectoredUnit(helper);
98 if (tag == ArmyProdBase::d_tag)
100 VectoredUnit *vectoredunit = back();
101 vectoredunit->setArmy(new ArmyProdBase (helper));
108 void VectoredUnitlist::nextTurn(Player* p)
110 debug("next_turn(" <<p->getName() <<")");
112 for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
114 City *c = GameMap::getCity((*it)->getPos());
117 if (c->getOwner() == p)
120 else //must be a standard
124 for (VectoredUnitlist::iterator it = begin(); it != end();)
126 if ((*it)->getDuration() <= 0)
135 bool VectoredUnitlist::removeVectoredUnitsGoingTo(Vector<int> pos)
138 printf ("trying to remove vectored units going to %d,%d\n", pos.x, pos.y);
139 for (VectoredUnitlist::iterator it = begin(); it != end();)
141 if ((*it)->getDestination() == pos)
150 for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
152 if ((*it)->getDestination() == pos)
154 printf ("crap. it's still there!!\n");
162 bool VectoredUnitlist::removeVectoredUnitsComingFrom(Vector<int> pos)
165 for (VectoredUnitlist::iterator it = begin(); it != end();)
167 if ((*it)->getPos() == pos)
178 bool VectoredUnitlist::removeVectoredUnitsGoingTo(City *c)
183 for (VectoredUnitlist::iterator it = begin(); it != end();)
185 if (c->contains((*it)->getDestination()))
194 if (counter != count)
197 for (VectoredUnitlist::iterator it = begin(); it != end();)
199 if (c->contains((*it)->getDestination()))
201 printf ("crap! we found another one on the second try\n");
211 printf ("got another %d\n", counter);
218 bool VectoredUnitlist::removeVectoredUnitsComingFrom(City *c)
221 for (VectoredUnitlist::iterator it = begin(); it != end();)
223 if (c->contains((*it)->getPos()))
234 void VectoredUnitlist::getVectoredUnitsGoingTo(City *c, std::list<VectoredUnit*>& vectored) const
236 for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
238 if (c->contains((*it)->getDestination()))
240 vectored.push_back(*it);
244 void VectoredUnitlist::getVectoredUnitsGoingTo(Vector<int> pos, std::list<VectoredUnit*>& vectored) const
246 for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
248 if ((*it)->getDestination() == pos)
250 vectored.push_back(*it);
254 void VectoredUnitlist::getVectoredUnitsComingFrom(Vector<int> pos, std::list<VectoredUnit*>& vectored) const
256 for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
258 if ((*it)->getPos() == pos)
260 vectored.push_back(*it);
265 guint32 VectoredUnitlist::getNumberOfVectoredUnitsGoingTo(Vector<int> pos) const
268 for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
270 if ((*it)->getDestination() == pos)
278 bool VectoredUnitlist::changeDestination(City *c, Vector<int> new_dest)
281 for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
283 if (c->contains((*it)->getPos()))
285 assert (c->getOwner() == (*it)->getOwner());
286 (*it)->setDestination(new_dest);
293 VectoredUnitlist::iterator VectoredUnitlist::flErase(iterator object)
296 return erase (object);
299 void VectoredUnitlist::changeOwnership(Player *old_owner, Player *new_owner)
301 for (iterator it = begin(); it != end(); it++)
302 if ((*it)->getOwner() == old_owner)
303 (*it)->setOwner(new_owner);