initial commit, lordsawar source, slightly modified
[lordsawar] / src / vectoredunit.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 "vectoredunit.h"
19 #include <stdlib.h>
20 #include <string.h>
21 #include <xmlhelper.h>
22
23 #include "armysetlist.h"
24 #include "playerlist.h"
25 #include "army.h"
26 #include "city.h"
27 #include "GameMap.h"
28 #include "action.h"
29 #include "MapBackpack.h"
30
31 std::string VectoredUnit::d_tag = "vectoredunit";
32
33 VectoredUnit::VectoredUnit(Vector<int> pos, Vector<int> dest, ArmyProdBase *army, int duration, Player *player)
34     :Ownable(player), LocationBox(pos), d_destination(dest), 
35     d_duration(duration)
36 {
37   if (army)
38     d_army = new ArmyProdBase(*army);
39   else
40     d_army = NULL;
41 }
42
43 VectoredUnit::VectoredUnit(const VectoredUnit& v)
44     :Ownable(v), LocationBox(v), d_destination(v.d_destination),
45      d_duration(v.d_duration)
46 {
47   if (v.d_army)
48     d_army = new ArmyProdBase(*v.d_army);
49   else
50     d_army = NULL;
51 }
52
53 VectoredUnit::VectoredUnit(XML_Helper* helper)
54     :Ownable(helper), LocationBox(helper), d_army(NULL)
55 {
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
60 }
61
62 VectoredUnit::~VectoredUnit()
63 {
64   if (d_army)
65     delete d_army;
66 }
67
68 bool VectoredUnit::save(XML_Helper* helper) const
69 {
70     bool retval = true;
71     std::string name = "";
72
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);
80     if (d_owner)
81         retval &= helper->saveData("owner", d_owner->getId());
82     else
83         retval &= helper->saveData("owner", -1);
84     retval &= d_army->save(helper);
85     retval &= helper->closeTag();
86
87     return retval;
88 }
89
90 Army *VectoredUnit::armyArrives() const
91 {
92   City *dest;
93   // drop it in the destination city!
94   dest = GameMap::getCity(d_destination);
95   if (!dest)
96     {
97       if (d_destination == Vector<int>(-1,-1))
98         {
99           printf ("destination is -1,-1??? why?\n");
100         return NULL;
101         }
102       printf ("uhh... no city at %d,%d?\n", d_destination.x, d_destination.y);
103       Maptile *tile = GameMap::getInstance()->getTile(d_destination);
104       if (tile)
105         {
106           if (tile->getBackpack()->getPlantedItem(d_owner))
107             {
108               //army arrives on a planted standard
109               Army *a = new Army(*d_army, d_owner);
110               LocationBox loc = LocationBox(d_destination);
111               loc.addArmy(a);
112               return a;
113             }
114         }
115     }
116   else
117     {
118       if (!dest->isBurnt() && dest->getOwner() == d_owner)
119         {
120           //army arrives in a city
121           Army *a = new Army(*d_army, d_owner);
122           dest->addArmy(a);
123           return a;
124         }
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());
126     }
127   return NULL;
128 }
129
130 bool VectoredUnit::nextTurn()
131 {
132   d_duration--;
133   if (d_duration == 0)
134     return d_owner->vectoredUnitArrives(this);
135   return false;
136 }
137
138 // End of file