initial commit, lordsawar source, slightly modified
[lordsawar] / src / vectoredunitlist.cpp
1 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
2 //
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.
7 //
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.
12 //
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 
16 //  02110-1301, USA.
17
18 #include <assert.h>
19 #include <sigc++/functors/mem_fun.h>
20
21 #include "vectoredunitlist.h"
22 #include "vectoredunit.h"
23 #include "city.h"
24 #include "xmlhelper.h"
25 #include "player.h"
26 #include "GameMap.h"
27
28 std::string VectoredUnitlist::d_tag = "vectoredunitlist";
29 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
30 #define debug(x)
31
32 VectoredUnitlist* VectoredUnitlist::s_instance = 0;
33
34 VectoredUnitlist* VectoredUnitlist::getInstance()
35 {
36     if (s_instance == 0)
37         s_instance = new VectoredUnitlist();
38
39     return s_instance;
40 }
41
42 VectoredUnitlist* VectoredUnitlist::getInstance(XML_Helper* helper)
43 {
44     if (s_instance)
45         deleteInstance();
46
47     s_instance = new VectoredUnitlist(helper);
48     return s_instance;
49 }
50
51 void VectoredUnitlist::deleteInstance()
52 {
53     if (s_instance)
54         delete s_instance;
55
56     s_instance = 0;
57 }
58
59 VectoredUnitlist::VectoredUnitlist()
60 {
61 }
62
63 VectoredUnitlist::~VectoredUnitlist()
64 {
65   for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
66     delete *it;
67 }
68
69 VectoredUnitlist::VectoredUnitlist(XML_Helper* helper)
70 {
71     helper->registerTag(VectoredUnit::d_tag, sigc::mem_fun(this, &VectoredUnitlist::load));
72     helper->registerTag(ArmyProdBase::d_tag, sigc::mem_fun(this, &VectoredUnitlist::load));
73 }
74
75 bool VectoredUnitlist::save(XML_Helper* helper) const
76 {
77     bool retval = true;
78
79     retval &= helper->openTag(VectoredUnitlist::d_tag);
80
81     for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
82         retval &= (*it)->save(helper);
83     
84     retval &= helper->closeTag();
85
86     return retval;
87 }
88
89 bool VectoredUnitlist::load(std::string tag, XML_Helper* helper)
90 {
91   if (tag == VectoredUnit::d_tag)
92     {
93       VectoredUnit *r = new VectoredUnit(helper);
94       push_back(r);
95       return true;
96     }
97
98   if (tag == ArmyProdBase::d_tag)
99     {
100       VectoredUnit *vectoredunit = back();
101       vectoredunit->setArmy(new ArmyProdBase (helper));
102       return true;
103     }
104
105     return false;
106 }
107
108 void VectoredUnitlist::nextTurn(Player* p)
109 {
110   debug("next_turn(" <<p->getName() <<")");
111
112   for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
113     {
114       City *c = GameMap::getCity((*it)->getPos());
115       if (c)
116         {
117           if (c->getOwner() == p)
118             (*it)->nextTurn();
119         }
120       else //must be a standard
121         (*it)->nextTurn();
122     }
123
124   for (VectoredUnitlist::iterator it = begin(); it != end();)
125     {
126       if ((*it)->getDuration() <= 0)
127         {
128           it = flErase(it);
129           continue;
130         }
131       it++;
132     }
133 }
134
135 bool VectoredUnitlist::removeVectoredUnitsGoingTo(Vector<int> pos)
136 {
137   bool found = false;
138   printf ("trying to remove vectored units going to %d,%d\n", pos.x, pos.y);
139   for (VectoredUnitlist::iterator it = begin(); it != end();)
140     {
141       if ((*it)->getDestination() == pos)
142         {
143           found = true;
144           it = flErase(it);
145           continue;
146         }
147       it++;
148     }
149   /*
150   for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
151     {
152       if ((*it)->getDestination() == pos)
153         {
154           printf ("crap.  it's still there!!\n");
155           exit(0);
156         }
157     }
158   */
159   return found;
160 }
161
162 bool VectoredUnitlist::removeVectoredUnitsComingFrom(Vector<int> pos)
163 {
164   bool found = false;
165   for (VectoredUnitlist::iterator it = begin(); it != end();)
166     {
167       if ((*it)->getPos() == pos)
168         {
169           found = true;
170           it = flErase(it);
171           continue;
172         }
173       it++;
174     }
175   return found;
176 }
177
178 bool VectoredUnitlist::removeVectoredUnitsGoingTo(City *c)
179 {
180   int count = 0;
181   int counter = 0;
182   bool found = false;
183   for (VectoredUnitlist::iterator it = begin(); it != end();)
184     {
185       if (c->contains((*it)->getDestination()))
186         {
187           found = true;
188           it = flErase(it);
189           counter++;
190           continue;
191         }
192       it++;
193     }
194   if (counter != count)
195     {
196       counter = 0;
197   for (VectoredUnitlist::iterator it = begin(); it != end();)
198     {
199       if (c->contains((*it)->getDestination()))
200         {
201           printf ("crap!  we found another one on the second try\n");
202           found = true;
203           it = flErase(it);
204           counter++;
205           continue;
206         }
207       it++;
208     }
209   if (counter)
210     {
211   printf ("got another %d\n", counter);
212     exit(0);
213     }
214     }
215   return found;
216 }
217
218 bool VectoredUnitlist::removeVectoredUnitsComingFrom(City *c)
219 {
220   bool found = false;
221   for (VectoredUnitlist::iterator it = begin(); it != end();)
222     {
223       if (c->contains((*it)->getPos()))
224         {
225           found = true;
226           it = flErase(it);
227           continue;
228         }
229       it++;
230     }
231   return found;
232 }
233
234 void VectoredUnitlist::getVectoredUnitsGoingTo(City *c, std::list<VectoredUnit*>& vectored) const
235 {
236   for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
237     {
238       if (c->contains((*it)->getDestination()))
239         {
240           vectored.push_back(*it);
241         }
242     }
243 }
244 void VectoredUnitlist::getVectoredUnitsGoingTo(Vector<int> pos, std::list<VectoredUnit*>& vectored) const
245 {
246   for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
247     {
248       if ((*it)->getDestination() == pos)
249         {
250           vectored.push_back(*it);
251         }
252     }
253 }
254 void VectoredUnitlist::getVectoredUnitsComingFrom(Vector<int> pos, std::list<VectoredUnit*>& vectored) const
255 {
256   for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
257     {
258       if ((*it)->getPos() == pos)
259         {
260           vectored.push_back(*it);
261         }
262     }
263 }
264
265 guint32 VectoredUnitlist::getNumberOfVectoredUnitsGoingTo(Vector<int> pos) const
266 {
267   guint32 count = 0;
268   for (VectoredUnitlist::const_iterator it = begin(); it != end(); it++)
269     {
270       if ((*it)->getDestination() == pos)
271         {
272           count++;
273         }
274     }
275   return count;
276 }
277
278 bool VectoredUnitlist::changeDestination(City *c, Vector<int> new_dest)
279 {
280   bool found = false;
281   for (VectoredUnitlist::iterator it = begin(); it != end(); it++)
282     {
283       if (c->contains((*it)->getPos()))
284         {
285           assert (c->getOwner() == (*it)->getOwner());
286           (*it)->setDestination(new_dest);
287           found = true;
288         }
289     }
290   return found;
291 }
292
293 VectoredUnitlist::iterator VectoredUnitlist::flErase(iterator object)
294 {
295   delete(*object);
296   return erase (object);
297 }
298
299 void VectoredUnitlist::changeOwnership(Player *old_owner, Player *new_owner)
300 {
301   for (iterator it = begin(); it != end(); it++)
302     if ((*it)->getOwner() == old_owner)
303       (*it)->setOwner(new_owner);
304 }