1 // Copyright (C) 2008 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
20 #include <sigc++/functors/mem_fun.h>
24 #include "xmlhelper.h"
28 std::string Backpack::d_tag = "backpack";
32 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<flush;}
41 Backpack::Backpack(XML_Helper* helper)
43 helper->registerTag(Item::d_tag, sigc::mem_fun(this, &Backpack::loadItem));
46 Backpack::Backpack(const Backpack& backpack)
48 for (const_iterator it = backpack.begin(); it != backpack.end(); it++)
49 push_back(new Item(**it));
54 for (iterator it = begin(); it != end(); it++)
58 bool Backpack::saveData(XML_Helper* helper) const
61 for (const_iterator it = begin(); it != end(); it++)
62 retval &= (*it)->save(helper);
66 bool Backpack::save(XML_Helper* helper) const
70 retval &= helper->openTag(Backpack::d_tag);
71 retval &= saveData(helper);
72 retval &= helper->closeTag();
77 bool Backpack::loadItem(std::string tag, XML_Helper* helper)
79 if (tag == Backpack::d_tag)
82 if (tag == Item::d_tag)
84 Item* item = new Item(helper);
92 guint32 Backpack::countStrengthBonuses()
95 for (iterator it = begin(); it != end(); it++)
97 if ((*it)->getBonus(Item::ADD1STR))
99 if ((*it)->getBonus(Item::ADD2STR))
101 if ((*it)->getBonus(Item::ADD3STR))
107 guint32 Backpack::countStackStrengthBonuses()
110 for (iterator it = begin(); it != end(); it++)
112 if ((*it)->getBonus(Item::ADD1STACK))
114 if ((*it)->getBonus(Item::ADD2STACK))
116 if ((*it)->getBonus(Item::ADD3STACK))
123 guint32 Backpack::countGoldBonuses()
126 for (iterator it = begin(); it != end(); it++)
128 if ((*it)->getBonus(Item::ADD2GOLDPERCITY))
130 if ((*it)->getBonus(Item::ADD3GOLDPERCITY))
132 if ((*it)->getBonus(Item::ADD4GOLDPERCITY))
134 if ((*it)->getBonus(Item::ADD5GOLDPERCITY))
140 guint32 Backpack::countMovementDoublers()
143 for (iterator it = begin(); it != end(); it++)
144 if ((*it)->getBonus(Item::DOUBLEMOVESTACK))
149 guint32 Backpack::countStackFlightGivers()
152 for (iterator it = begin(); it != end(); it++)
153 if ((*it)->getBonus(Item::FLYSTACK))
158 guint32 Backpack::countPlantableItems()
161 for (iterator it = begin(); it != end(); it++)
162 if ((*it)->isPlantable())
167 Item *Backpack::getPlantableItem(Player *player)
169 for (iterator it = begin(); it != end(); it++)
170 if ((*it)->isPlantable() && (*it)->getPlantableOwner() == player)
175 Item *Backpack::getItemById(guint32 id)
177 for (iterator it = begin(); it != end(); it++)
178 if ((*it)->getId() == id)
183 bool Backpack::addToBackpack(Item* item, int position)
185 iterator it = begin();
186 for (; position > 0; position--, it++);
191 bool Backpack::addToBackpack(Item* item)
198 bool Backpack::removeFromBackpack(Item* item)
200 for (iterator it = begin(); it != end(); it++)
203 //FIXME: delete the item?
211 void Backpack::removeAllFromBackpack()
214 removeFromBackpack(front());
217 void Backpack::add(Backpack *backpack)
219 for (Backpack::iterator it = backpack->begin(); it != backpack->end(); it++)
220 addToBackpack(new Item(**it));