initial commit, lordsawar source, slightly modified
[lordsawar] / src / Backpack.cpp
1 //  Copyright (C) 2008 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 <iostream>
19 #include <sstream>
20 #include <sigc++/functors/mem_fun.h>
21
22 #include "Backpack.h"
23
24 #include "xmlhelper.h"
25
26 #include "Item.h"
27
28 std::string Backpack::d_tag = "backpack";
29
30 using namespace std;
31
32 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<flush;}
33 #define debug(x)
34
35
36 Backpack::Backpack()
37 {
38     debug("Backpack()");
39 }
40
41 Backpack::Backpack(XML_Helper* helper)
42 {
43   helper->registerTag(Item::d_tag, sigc::mem_fun(this, &Backpack::loadItem));
44 }
45
46 Backpack::Backpack(const Backpack& backpack)
47 {
48   for (const_iterator it = backpack.begin(); it != backpack.end(); it++)
49     push_back(new Item(**it));
50 }
51
52 Backpack::~Backpack()
53 {
54   for (iterator it = begin(); it != end(); it++)
55     delete (*it);
56 }
57
58 bool Backpack::saveData(XML_Helper* helper) const
59 {
60   bool retval = true;
61   for (const_iterator it = begin(); it != end(); it++)
62     retval &= (*it)->save(helper);
63   return true;
64 }
65
66 bool Backpack::save(XML_Helper* helper) const
67 {
68     bool retval = true;
69
70     retval &= helper->openTag(Backpack::d_tag);
71     retval &= saveData(helper);
72     retval &= helper->closeTag();
73
74     return retval;
75 }
76
77 bool Backpack::loadItem(std::string tag, XML_Helper* helper)
78 {
79   if (tag == Backpack::d_tag)
80     return true;
81
82   if (tag == Item::d_tag)
83     {
84       Item* item = new Item(helper);
85       push_back(item);
86       return true;
87     }
88
89   return false;
90 }
91
92 guint32 Backpack::countStrengthBonuses()
93 {
94   guint32 bonus = 0;
95   for (iterator it = begin(); it != end(); it++)
96     {
97       if ((*it)->getBonus(Item::ADD1STR))
98         bonus += 1;
99       if ((*it)->getBonus(Item::ADD2STR))
100         bonus += 2;
101       if ((*it)->getBonus(Item::ADD3STR))
102         bonus += 3;
103     }
104   return bonus;
105 }
106
107 guint32 Backpack::countStackStrengthBonuses()
108 {
109   guint32 bonus = 0;
110   for (iterator it = begin(); it != end(); it++)
111     {
112       if ((*it)->getBonus(Item::ADD1STACK))
113         bonus += 1;
114       if ((*it)->getBonus(Item::ADD2STACK))
115         bonus += 2;
116       if ((*it)->getBonus(Item::ADD3STACK))
117         bonus += 3;
118     }
119   return bonus;
120 }
121
122
123 guint32 Backpack::countGoldBonuses()
124 {
125   guint32 bonus = 0;
126   for (iterator it = begin(); it != end(); it++)
127     {
128       if ((*it)->getBonus(Item::ADD2GOLDPERCITY))
129         bonus += 2;
130       if ((*it)->getBonus(Item::ADD3GOLDPERCITY))
131         bonus += 3;
132       if ((*it)->getBonus(Item::ADD4GOLDPERCITY))
133         bonus += 4;
134       if ((*it)->getBonus(Item::ADD5GOLDPERCITY))
135         bonus += 5;
136     }
137   return bonus;
138 }
139
140 guint32 Backpack::countMovementDoublers()
141 {
142   guint32 bonus = 0;
143   for (iterator it = begin(); it != end(); it++)
144     if ((*it)->getBonus(Item::DOUBLEMOVESTACK))
145       bonus++;
146   return bonus;
147 }
148
149 guint32 Backpack::countStackFlightGivers()
150 {
151   guint32 bonus = 0;
152   for (iterator it = begin(); it != end(); it++)
153     if ((*it)->getBonus(Item::FLYSTACK))
154       bonus++;
155   return bonus;
156 }
157
158 guint32 Backpack::countPlantableItems()
159 {
160   guint32 count = 0;
161   for (iterator it = begin(); it != end(); it++)
162     if ((*it)->isPlantable())
163       count++;
164   return count;
165 }
166
167 Item *Backpack::getPlantableItem(Player *player)
168 {
169   for (iterator it = begin(); it != end(); it++)
170     if ((*it)->isPlantable() && (*it)->getPlantableOwner() == player)
171       return *it;
172   return NULL;
173 }
174         
175 Item *Backpack::getItemById(guint32 id)
176 {
177   for (iterator it = begin(); it != end(); it++)
178     if ((*it)->getId() == id)
179       return *it;
180   return NULL;
181 }
182
183 bool Backpack::addToBackpack(Item* item, int position)
184 {
185   iterator it = begin();
186   for (; position > 0; position--, it++);
187   insert(it, item);
188   return true;
189 }
190
191 bool Backpack::addToBackpack(Item* item)
192 {
193   iterator it = end();
194   insert(it, item);
195   return true;
196 }
197
198 bool Backpack::removeFromBackpack(Item* item)
199 {
200   for (iterator it = begin(); it != end(); it++)
201     if ((*it) == item)
202       {
203         //FIXME: delete the item?
204         erase(it);
205         return true;
206       }
207
208   return false;
209 }
210         
211 void Backpack::removeAllFromBackpack()
212 {
213   while (!empty())
214     removeFromBackpack(front());
215 }
216         
217 void Backpack::add(Backpack *backpack)
218 {
219   for (Backpack::iterator it = backpack->begin(); it != backpack->end(); it++)
220     addToBackpack(new Item(**it));
221 }
222 // End of file