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
18 #include "vectoredunit.h"
21 #include <xmlhelper.h>
23 #include "armysetlist.h"
24 #include "playerlist.h"
29 #include "MapBackpack.h"
31 std::string VectoredUnit::d_tag = "vectoredunit";
33 VectoredUnit::VectoredUnit(Vector<int> pos, Vector<int> dest, ArmyProdBase *army, int duration, Player *player)
34 :Ownable(player), LocationBox(pos), d_destination(dest),
38 d_army = new ArmyProdBase(*army);
43 VectoredUnit::VectoredUnit(const VectoredUnit& v)
44 :Ownable(v), LocationBox(v), d_destination(v.d_destination),
45 d_duration(v.d_duration)
48 d_army = new ArmyProdBase(*v.d_army);
53 VectoredUnit::VectoredUnit(XML_Helper* helper)
54 :Ownable(helper), LocationBox(helper), d_army(NULL)
56 helper->getData(d_duration, "duration");
57 helper->getData(d_destination.x, "dest_x");
58 helper->getData(d_destination.y, "dest_y");
59 //army is loaded via callback in vectoredunitlist
62 VectoredUnit::~VectoredUnit()
68 bool VectoredUnit::save(XML_Helper* helper) const
71 std::string name = "";
73 retval &= helper->openTag(VectoredUnit::d_tag);
74 retval &= helper->saveData("x", getPos().x);
75 retval &= helper->saveData("y", getPos().y);
76 retval &= helper->saveData("name", name);
77 retval &= helper->saveData("duration", d_duration);
78 retval &= helper->saveData("dest_x", d_destination.x);
79 retval &= helper->saveData("dest_y", d_destination.y);
81 retval &= helper->saveData("owner", d_owner->getId());
83 retval &= helper->saveData("owner", -1);
84 retval &= d_army->save(helper);
85 retval &= helper->closeTag();
90 Army *VectoredUnit::armyArrives() const
93 // drop it in the destination city!
94 dest = GameMap::getCity(d_destination);
97 if (d_destination == Vector<int>(-1,-1))
99 printf ("destination is -1,-1??? why?\n");
102 printf ("uhh... no city at %d,%d?\n", d_destination.x, d_destination.y);
103 Maptile *tile = GameMap::getInstance()->getTile(d_destination);
106 if (tile->getBackpack()->getPlantedItem(d_owner))
108 //army arrives on a planted standard
109 Army *a = new Army(*d_army, d_owner);
110 LocationBox loc = LocationBox(d_destination);
118 if (!dest->isBurnt() && dest->getOwner() == d_owner)
120 //army arrives in a city
121 Army *a = new Army(*d_army, d_owner);
125 printf ("destination city is owned by `%s', but the vectored unit is owned by `%s'\n", dest->getOwner()->getName().c_str(), d_owner->getName().c_str());
130 bool VectoredUnit::nextTurn()
134 return d_owner->vectoredUnitArrives(this);