1 // Copyright (C) 2000, 2001, 2003 Michael Bartl
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2002 Mark L. Amidon
4 // Copyright (C) 2005 Andrea Paternesi
5 // Copyright (C) 2006, 2007, 2008 Ben Asselstine
6 // Copyright (C) 2008 Ole Laursen
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Library General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "prodslotlist.h"
30 #include "armyprodbase.h"
32 #include "stacklist.h"
34 #include "playerlist.h"
35 #include "armysetlist.h"
38 #include "vectoredunitlist.h"
39 #include "vectoredunit.h"
44 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
47 ProdSlotlist::ProdSlotlist(guint32 numslots)
48 : d_active_production_slot(-1), d_duration(-1)
50 // Initialise armytypes
51 for (unsigned int i = 0; i < numslots; i++)
52 push_back(new ProdSlot());
56 ProdSlotlist::ProdSlotlist(XML_Helper* helper)
59 helper->getData(d_active_production_slot, "active_production_slot");
60 helper->getData(d_duration, "duration");
61 helper->registerTag(ProdSlot::d_tag,
62 sigc::mem_fun(this, &ProdSlotlist::load));
65 bool ProdSlotlist::load(std::string tag, XML_Helper *helper)
67 if (tag == ProdSlot::d_tag)
69 push_back(new ProdSlot(helper));
75 ProdSlotlist::ProdSlotlist(const ProdSlotlist& c)
76 :d_active_production_slot(c.d_active_production_slot),
77 d_duration(c.d_duration)
79 for (std::vector<ProdSlot*>::const_iterator it = c.begin();
84 ProdSlotlist::~ProdSlotlist()
88 bool ProdSlotlist::save(XML_Helper* helper) const
92 retval &= helper->saveData("active_production_slot",
93 d_active_production_slot);
94 retval &= helper->saveData("duration", d_duration);
96 for (unsigned int i = 0; i < size(); i++)
99 retval &= (*this)[i]->save(helper);
104 guint32 ProdSlotlist::getNoOfProductionBases() const
106 unsigned int max = 0;
107 for (unsigned int i = 0; i < getMaxNoOfProductionBases(); i++)
109 if (getProductionBase(i))
115 void ProdSlotlist::setActiveProductionSlot(int index)
119 d_active_production_slot = index;
124 // return on wrong data
125 if (((index >= (int)size())) ||
126 (index >= 0 && getArmytype(index) == -1))
129 d_active_production_slot = index;
130 const ArmyProdBase* a = getProductionBase(index);
132 // set the duration to produce this armytype
134 d_duration = a->getProduction();
137 int ProdSlotlist::getFreeSlot() const
141 debug(getName()<< " BASIC SLOTS=" << size())
142 for (unsigned int i = 0; i < size(); i++)
144 debug(getName()<< " Index Value=" << (*this)[i])
145 if ((*this)[i]->getArmyProdBase() == NULL)
155 bool ProdSlotlist::hasProductionBase(const ArmyProto * army) const
157 return hasProductionBase(army->getTypeId(), army->getArmyset());
160 void ProdSlotlist::addProductionBase(int idx, ArmyProdBase *army)
164 // try to find an unoccupied production slot. If there is none, pick
165 // the slot with the highest index.
166 for (unsigned int i = 0; i < size(); i++)
167 if ((*this)[i]->getArmyProdBase() == NULL)
178 if (idx >= (int)size())
181 if ((*this)[idx]->getArmyProdBase())
183 bool restore_production = false;
184 if (d_active_production_slot == idx)
185 restore_production = true;
186 removeProductionBase(idx);
187 (*this)[idx]->setArmyProdBase(army);
188 if (restore_production)
189 setActiveProductionSlot(idx);
192 (*this)[idx]->setArmyProdBase(army);
195 void ProdSlotlist::removeProductionBase(int idx)
197 if ((idx < 0) || (idx > (int)(getMaxNoOfProductionBases() - 1)))
200 if ((*this)[idx]->getArmyProdBase() != NULL)
201 (*this)[idx]->clear();
203 if (d_active_production_slot == idx)
204 setActiveProductionSlot(-1);
207 bool ProdSlotlist::hasProductionBase(int type, guint32 set) const
211 for (unsigned int i = 0; i < size(); i++)
213 if ((*this)[i]->getArmyProdBase() == NULL)
215 if ((*this)[i]->getArmyProdBase()->getTypeId() == (unsigned int) type)
222 int ProdSlotlist::getArmytype(int slot) const
227 if (slot >= (int)size())
229 if ((*this)[slot]->getArmyProdBase() == NULL)
231 return (*this)[slot]->getArmyProdBase()->getTypeId();
234 const ArmyProdBase * ProdSlotlist::getProductionBase(int slot) const
236 if (getArmytype(slot) == -1)
238 return (*this)[slot]->getArmyProdBase();
241 const ArmyProdBase *ProdSlotlist::getActiveProductionBase() const
243 return getProductionBase(d_active_production_slot);
246 const ArmyProdBase *ProdSlotlist::getProductionBaseBelongingTo(const Army *army) const
250 for (unsigned int i = 0; i < this->getMaxNoOfProductionBases(); i++)
252 const ArmyProdBase* armyprodbase = this->getProductionBase(i);
253 if (armyprodbase == NULL)
255 if (army->getArmyset() == armyprodbase->getArmyset() &&
256 army->getTypeId() == armyprodbase->getTypeId())