1 //  Copyright (C) 2007, 2008, 2009, 2014, 2015 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 <xmlhelper.h>
20 
21 #include "armysetlist.h"
22 #include "playerlist.h"
23 #include "army.h"
24 #include "city.h"
25 #include "GameMap.h"
26 #include "action.h"
27 #include "MapBackpack.h"
28 #include "GameScenario.h"
29 
30 Glib::ustring VectoredUnit::d_tag = "vectoredunit";
31 
VectoredUnit(Vector<int> pos,Vector<int> dest,ArmyProdBase * army,int duration,Player * player)32 VectoredUnit::VectoredUnit(Vector<int> pos, Vector<int> dest, ArmyProdBase *army, int duration, Player *player)
33     :Ownable(player), LocationBox(pos), d_destination(dest),
34     d_duration(duration)
35 {
36   if (army)
37     d_army = new ArmyProdBase(*army);
38   else
39     d_army = NULL;
40 }
41 
VectoredUnit(const VectoredUnit & v)42 VectoredUnit::VectoredUnit(const VectoredUnit& v)
43     :Ownable(v), LocationBox(v), sigc::trackable(v),
44     d_destination(v.d_destination), d_duration(v.d_duration)
45 {
46   if (v.d_army)
47     d_army = new ArmyProdBase(*v.d_army);
48   else
49     d_army = NULL;
50 }
51 
VectoredUnit(XML_Helper * helper)52 VectoredUnit::VectoredUnit(XML_Helper* helper)
53     :Ownable(helper), LocationBox(helper), d_army(NULL)
54 {
55     helper->getData(d_duration, "duration");
56     helper->getData(d_destination.x, "dest_x");
57     helper->getData(d_destination.y, "dest_y");
58     //army is loaded via callback in vectoredunitlist
59 }
60 
~VectoredUnit()61 VectoredUnit::~VectoredUnit()
62 {
63   if (d_army)
64     delete d_army;
65 }
66 
save(XML_Helper * helper) const67 bool VectoredUnit::save(XML_Helper* helper) const
68 {
69     bool retval = true;
70     Glib::ustring name = "";
71 
72     retval &= helper->openTag(VectoredUnit::d_tag);
73     retval &= helper->saveData("x", getPos().x);
74     retval &= helper->saveData("y", getPos().y);
75     retval &= helper->saveData("name", name);
76     retval &= helper->saveData("duration", d_duration);
77     retval &= helper->saveData("dest_x", d_destination.x);
78     retval &= helper->saveData("dest_y", d_destination.y);
79     if (d_owner)
80         retval &= helper->saveData("owner", d_owner->getId());
81     else
82         retval &= helper->saveData("owner", -1);
83     retval &= d_army->save(helper);
84     retval &= helper->closeTag();
85 
86     return retval;
87 }
88 
armyArrives(Stack * & stack) const89 Army *VectoredUnit::armyArrives(Stack *& stack) const
90 {
91   City *dest;
92   // drop it in the destination city!
93   dest = GameMap::getCity(d_destination);
94   if (!dest)
95     {
96       if (d_destination == Vector<int>(-1,-1))
97 	{
98 	  printf ("destination is -1,-1??? why?\n");
99 	return NULL;
100 	}
101       printf ("uhh... no city at %d,%d?\n", d_destination.x, d_destination.y);
102       Maptile *tile = GameMap::getInstance()->getTile(d_destination);
103       if (tile)
104 	{
105 	  if (tile->getBackpack()->getPlantedItem(d_owner))
106 	    {
107 	      //army arrives on a planted standard
108 	      Army *a = new Army(*d_army, d_owner);
109 	      LocationBox loc = LocationBox(d_destination);
110               stack = GameMap::getInstance()->addArmy(d_destination, a);
111 	      return a;
112 	    }
113 	}
114     }
115   else
116     {
117       if (!dest->isBurnt() && dest->getOwner() == d_owner)
118 	{
119 	  //army arrives in a city
120 	  Army *a = new Army(*d_army, d_owner);
121           stack = GameMap::getInstance()->addArmy(d_destination, a);
122 	  return a;
123 	}
124       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());
125     }
126   return NULL;
127 }
128 
nextTurn()129 bool VectoredUnit::nextTurn()
130 {
131   d_duration--;
132   if (d_duration == 0)
133     return d_owner->vectoredUnitArrives(this);
134   return false;
135 }
136 
get_travel_turns(Vector<int> src,Vector<int> dest)137 int VectoredUnit::get_travel_turns (Vector<int> src, Vector<int> dest)
138 {
139   int turns = MAX_TURNS_FOR_VECTORING;
140   switch (GameScenario::s_vectoring_mode)
141     {
142     case GameParameters::VECTORING_ALWAYS_TWO_TURNS:
143       turns = MAX_TURNS_FOR_VECTORING;
144       break;
145     case GameParameters::VECTORING_VARIABLE_TURNS:
146       int d = dist(dest, src);
147       double r = (double) d /
148         (double)std::max(GameMap::getWidth(), GameMap::getHeight());
149       turns = (4 * r) + 1;
150       break;
151     }
152   return turns;
153 }
154 // End of file
155