1 // Copyright (C) 2003 Michael Bartl
2 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2007-2010, 2014, 2015, 2017 Ben Asselstine
4 // Copyright (C) 2008 Ole Laursen
5 //
6 //  This program is free software; you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation; either version 3 of the License, or
9 //  (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU Library General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 //  02110-1301, USA.
20 
21 #include "maptile.h"
22 #include <iostream>
23 #include "tileset.h"
24 #include "MapBackpack.h"
25 #include "stacktile.h"
26 #include "army.h"
27 #include "GameMap.h"
28 
Maptile()29 Maptile::Maptile()
30         :Movable(Vector<int>(-1,-1)), d_index(0), d_building(NONE)
31 {
32     d_tileStyle = NULL;
33     d_stacktile = NULL;
34     d_backpack = NULL;
35 }
36 
Maptile(int x,int y,guint32 index)37 Maptile::Maptile(int x, int y, guint32 index)
38     :Movable (Vector<int>(x, y)), d_index(index), d_building(NONE)
39 {
40     d_tileStyle = NULL;
41     d_stacktile = NULL;
42     d_backpack = NULL;
43 }
44 
Maptile(int x,int y,Tile::Type type)45 Maptile::Maptile(int x, int y, Tile::Type type)
46     : Movable (Vector<int>(x, y)), d_index(GameMap::getTileset()->lookupIndexByType (type)), d_building(NONE)
47 {
48     d_tileStyle = NULL;
49     d_stacktile = NULL;
50     d_backpack = NULL;
51 }
52 
~Maptile()53 Maptile::~Maptile()
54 {
55   if (d_backpack)
56     delete d_backpack;
57   if (d_stacktile)
58     delete d_stacktile;
59 }
60 
getColor() const61 Gdk::RGBA Maptile::getColor() const
62 {
63   Tileset *ts = GameMap::getTileset();
64   return (*ts)[d_index]->getSmallTile()->getColor();
65 }
66 
getPattern() const67 SmallTile::Pattern Maptile::getPattern() const
68 {
69   Tileset *ts = GameMap::getTileset();
70   return (*ts)[d_index]->getSmallTile()->getPattern();
71 }
72 
getSecondColor() const73 Gdk::RGBA Maptile::getSecondColor() const
74 {
75   Tileset *ts = GameMap::getTileset();
76   return (*ts)[d_index]->getSmallTile()->getSecondColor();
77 }
78 
getThirdColor() const79 Gdk::RGBA Maptile::getThirdColor() const
80 {
81   Tileset *ts = GameMap::getTileset();
82   return (*ts)[d_index]->getSmallTile()->getThirdColor();
83 }
84 
getBackpack()85 MapBackpack *Maptile::getBackpack()
86 {
87   if (!d_backpack)
88     d_backpack = new MapBackpack (getPos());
89   return d_backpack;
90 }
91 
getStacks()92 StackTile *Maptile::getStacks()
93 {
94   if (!d_stacktile)
95     d_stacktile = new StackTile (getPos());
96   return d_stacktile;
97 }
98 
getType() const99 Tile::Type Maptile::getType() const
100 {
101   return (*GameMap::getTileset())[d_index]->getType();
102 }
103 
getMoves() const104 guint32 Maptile::getMoves() const
105 {
106     if (d_building == Maptile::CITY)
107         return 1;
108     else if (d_building == Maptile::ROAD)
109         return 1;
110     else if (d_building == Maptile::BRIDGE)
111         return 1;
112 
113     if ((*GameMap::getTileset())[d_index]->getType() == Tile::WATER)
114       {
115 	// if we're sailing and we're not on shore, then we move faster
116 	if (d_tileStyle->getType() == TileStyle::INNERMIDDLECENTER)
117 	  return (*GameMap::getTileset())[d_index]->getMoves() / 2;
118       }
119     return (*GameMap::getTileset())[d_index]->getMoves();
120 }
121 
setIndex(guint32 index)122 void Maptile::setIndex(guint32 index)
123 {
124   Tileset *ts = GameMap::getTileset();
125   Tile *tile = (*ts)[index];
126   if (!tile)
127     return;
128   d_index = index;
129 }
130 
isCityTerrain()131 bool Maptile::isCityTerrain()
132 {
133   if (getBuilding() == Maptile::CITY || getBuilding() == Maptile::RUIN ||
134       getBuilding() == Maptile::TEMPLE)
135     return true;
136   return false;
137 }
138 
isRoadTerrain()139 bool Maptile::isRoadTerrain()
140 {
141   if (getBuilding() == Maptile::ROAD || getBuilding() == Maptile::BRIDGE)
142     return true;
143   return false;
144 }
145 
isOpenTerrain()146 bool Maptile::isOpenTerrain()
147 {
148   if (isCityTerrain())
149     return false;
150   if ((getType() == Tile::HILLS || getType() == Tile::FOREST ||
151       getType() == Tile::MOUNTAIN) && getBuilding() == Maptile::NONE)
152     return false;
153   /* swamp and water are open terrain too */
154   if ((getType() == Tile::GRASS || getType() == Tile::SWAMP ||
155        getType() == Tile::WATER) || getBuilding() != Maptile::BRIDGE)
156     return true;
157   return false;
158 }
159 
isHillyTerrain()160 bool Maptile::isHillyTerrain()
161 {
162   if (isCityTerrain())
163     return false;
164   if ((getType() == Tile::HILLS || getType() == Tile::MOUNTAIN))
165     return true;
166   return false;
167 }
168 
hasLandBuilding() const169 bool Maptile::hasLandBuilding() const
170 {
171   switch (d_building)
172     {
173     case Maptile::NONE:
174       return false;
175       break;
176     case Maptile::CITY:
177     case Maptile::RUIN:
178     case Maptile::TEMPLE:
179     case Maptile::SIGNPOST:
180     case Maptile::ROAD:
181     case Maptile::STONE:
182       return true;
183       break;
184     case Maptile::PORT:
185     case Maptile::BRIDGE:
186       return false;
187       break;
188     }
189   return false;
190 }
191 
hasWaterBuilding() const192 bool Maptile::hasWaterBuilding() const
193 {
194   switch (d_building)
195     {
196     case Maptile::NONE:
197       return false;
198       break;
199     case Maptile::CITY:
200     case Maptile::RUIN:
201     case Maptile::TEMPLE:
202     case Maptile::SIGNPOST:
203     case Maptile::ROAD:
204     case Maptile::STONE:
205       return false;
206       break;
207     case Maptile::PORT:
208     case Maptile::BRIDGE:
209       return true;
210       break;
211     }
212   return false;
213 }
214 
buildingFromString(Glib::ustring str)215 Maptile::Building Maptile::buildingFromString(Glib::ustring str)
216 {
217   if (str.size() > 0 && isdigit(str.c_str()[0]))
218     return Maptile::Building(atoi(str.c_str()));
219   if (str == "Maptile::NONE") return Maptile::NONE;
220   else if (str == "Maptile::CITY") return Maptile::CITY;
221   else if (str == "Maptile::RUIN") return Maptile::RUIN;
222   else if (str == "Maptile::TEMPLE") return Maptile::TEMPLE;
223   else if (str == "Maptile::SIGNPOST") return Maptile::SIGNPOST;
224   else if (str == "Maptile::ROAD") return Maptile::ROAD;
225   else if (str == "Maptile::PORT") return Maptile::PORT;
226   else if (str == "Maptile::BRIDGE") return Maptile::BRIDGE;
227   else if (str == "Maptile::STONE") return Maptile::STONE;
228   return Maptile::NONE;
229 }
230 
buildingToString(const Maptile::Building bldg)231 Glib::ustring Maptile::buildingToString(const Maptile::Building bldg)
232 {
233   switch (bldg)
234     {
235     case Maptile::NONE: return "Maptile::NONE";
236     case Maptile::CITY: return "Maptile::CITY";
237     case Maptile::RUIN: return "Maptile::RUIN";
238     case Maptile::TEMPLE: return "Maptile::TEMPLE";
239     case Maptile::SIGNPOST: return "Maptile::SIGNPOST";
240     case Maptile::ROAD: return "Maptile::ROAD";
241     case Maptile::PORT: return "Maptile::PORT";
242     case Maptile::BRIDGE: return "Maptile::BRIDGE";
243     case Maptile::STONE: return "Maptile::STONE";
244     }
245   return "Maptile::NONE";
246 }
247 
buildingToFriendlyName(const guint32 bldg)248 Glib::ustring Maptile::buildingToFriendlyName(const guint32 bldg)
249 {
250   switch (Building(bldg))
251     {
252     case Maptile::NONE: return _("None");
253     case Maptile::CITY: return _("City");
254     case Maptile::RUIN: return _("Ruin");
255     case Maptile::TEMPLE: return _("Temple");
256     case Maptile::SIGNPOST: return _("Signpost");
257     case Maptile::ROAD: return _("Road");
258     case Maptile::PORT: return _("Port");
259     case Maptile::BRIDGE: return _("Bridge");
260     case Maptile::STONE: return _("Stone");
261     }
262   return _("None");
263 }
264 // End of file
265