File: maptile.cpp

package info (click to toggle)
lordsawar 0.3.2%2Bfrogknows-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,716 kB
  • sloc: cpp: 103,193; sh: 4,965; xml: 2,220; makefile: 1,371
file content (264 lines) | stat: -rw-r--r-- 6,992 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (C) 2003 Michael Bartl
// Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
// Copyright (C) 2007-2010, 2014, 2015, 2017 Ben Asselstine
// Copyright (C) 2008 Ole Laursen
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Library General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
//  02110-1301, USA.

#include "maptile.h"
#include <iostream>
#include "tileset.h"
#include "MapBackpack.h"
#include "stacktile.h"
#include "army.h"
#include "GameMap.h"

Maptile::Maptile()
        :Movable(Vector<int>(-1,-1)), d_index(0), d_building(NONE)
{
    d_tileStyle = NULL;
    d_stacktile = NULL;
    d_backpack = NULL;
}

Maptile::Maptile(int x, int y, guint32 index)
    :Movable (Vector<int>(x, y)), d_index(index), d_building(NONE)
{
    d_tileStyle = NULL;
    d_stacktile = NULL;
    d_backpack = NULL;
}

Maptile::Maptile(int x, int y, Tile::Type type)
    : Movable (Vector<int>(x, y)), d_index(GameMap::getTileset()->lookupIndexByType (type)), d_building(NONE)
{
    d_tileStyle = NULL;
    d_stacktile = NULL;
    d_backpack = NULL;
}

Maptile::~Maptile()
{
  if (d_backpack)
    delete d_backpack;
  if (d_stacktile)
    delete d_stacktile;
}

Gdk::RGBA Maptile::getColor() const
{
  Tileset *ts = GameMap::getTileset();
  return (*ts)[d_index]->getSmallTile()->getColor();
}

SmallTile::Pattern Maptile::getPattern() const
{
  Tileset *ts = GameMap::getTileset();
  return (*ts)[d_index]->getSmallTile()->getPattern();
}

Gdk::RGBA Maptile::getSecondColor() const
{
  Tileset *ts = GameMap::getTileset();
  return (*ts)[d_index]->getSmallTile()->getSecondColor();
}

Gdk::RGBA Maptile::getThirdColor() const
{
  Tileset *ts = GameMap::getTileset();
  return (*ts)[d_index]->getSmallTile()->getThirdColor();
}

MapBackpack *Maptile::getBackpack()
{
  if (!d_backpack)
    d_backpack = new MapBackpack (getPos());
  return d_backpack;
}

StackTile *Maptile::getStacks()
{
  if (!d_stacktile)
    d_stacktile = new StackTile (getPos());
  return d_stacktile;
}

Tile::Type Maptile::getType() const
{
  return (*GameMap::getTileset())[d_index]->getType();
}

guint32 Maptile::getMoves() const
{
    if (d_building == Maptile::CITY)
        return 1;
    else if (d_building == Maptile::ROAD)
        return 1;
    else if (d_building == Maptile::BRIDGE)
        return 1;

    if ((*GameMap::getTileset())[d_index]->getType() == Tile::WATER)
      {
	// if we're sailing and we're not on shore, then we move faster
	if (d_tileStyle->getType() == TileStyle::INNERMIDDLECENTER)
	  return (*GameMap::getTileset())[d_index]->getMoves() / 2;
      }
    return (*GameMap::getTileset())[d_index]->getMoves();
}

void Maptile::setIndex(guint32 index)
{
  Tileset *ts = GameMap::getTileset();
  Tile *tile = (*ts)[index];
  if (!tile)
    return;
  d_index = index;
}

bool Maptile::isCityTerrain()
{
  if (getBuilding() == Maptile::CITY || getBuilding() == Maptile::RUIN ||
      getBuilding() == Maptile::TEMPLE)
    return true;
  return false;
}

bool Maptile::isRoadTerrain()
{
  if (getBuilding() == Maptile::ROAD || getBuilding() == Maptile::BRIDGE)
    return true;
  return false;
}

bool Maptile::isOpenTerrain()
{
  if (isCityTerrain())
    return false;
  if ((getType() == Tile::HILLS || getType() == Tile::FOREST ||
      getType() == Tile::MOUNTAIN) && getBuilding() == Maptile::NONE)
    return false;
  /* swamp and water are open terrain too */
  if ((getType() == Tile::GRASS || getType() == Tile::SWAMP ||
       getType() == Tile::WATER) || getBuilding() != Maptile::BRIDGE)
    return true;
  return false;
}

bool Maptile::isHillyTerrain()
{
  if (isCityTerrain())
    return false;
  if ((getType() == Tile::HILLS || getType() == Tile::MOUNTAIN))
    return true;
  return false;
}

bool Maptile::hasLandBuilding() const
{
  switch (d_building)
    {
    case Maptile::NONE:
      return false;
      break;
    case Maptile::CITY:
    case Maptile::RUIN:
    case Maptile::TEMPLE:
    case Maptile::SIGNPOST:
    case Maptile::ROAD:
    case Maptile::STONE:
      return true;
      break;
    case Maptile::PORT:
    case Maptile::BRIDGE:
      return false;
      break;
    }
  return false;
}

bool Maptile::hasWaterBuilding() const
{
  switch (d_building)
    {
    case Maptile::NONE:
      return false;
      break;
    case Maptile::CITY:
    case Maptile::RUIN:
    case Maptile::TEMPLE:
    case Maptile::SIGNPOST:
    case Maptile::ROAD:
    case Maptile::STONE:
      return false;
      break;
    case Maptile::PORT:
    case Maptile::BRIDGE:
      return true;
      break;
    }
  return false;
}

Maptile::Building Maptile::buildingFromString(Glib::ustring str)
{
  if (str.size() > 0 && isdigit(str.c_str()[0]))
    return Maptile::Building(atoi(str.c_str()));
  if (str == "Maptile::NONE") return Maptile::NONE;
  else if (str == "Maptile::CITY") return Maptile::CITY;
  else if (str == "Maptile::RUIN") return Maptile::RUIN;
  else if (str == "Maptile::TEMPLE") return Maptile::TEMPLE;
  else if (str == "Maptile::SIGNPOST") return Maptile::SIGNPOST;
  else if (str == "Maptile::ROAD") return Maptile::ROAD;
  else if (str == "Maptile::PORT") return Maptile::PORT;
  else if (str == "Maptile::BRIDGE") return Maptile::BRIDGE;
  else if (str == "Maptile::STONE") return Maptile::STONE;
  return Maptile::NONE;
}

Glib::ustring Maptile::buildingToString(const Maptile::Building bldg)
{
  switch (bldg)
    {
    case Maptile::NONE: return "Maptile::NONE";
    case Maptile::CITY: return "Maptile::CITY";
    case Maptile::RUIN: return "Maptile::RUIN";
    case Maptile::TEMPLE: return "Maptile::TEMPLE";
    case Maptile::SIGNPOST: return "Maptile::SIGNPOST";
    case Maptile::ROAD: return "Maptile::ROAD";
    case Maptile::PORT: return "Maptile::PORT";
    case Maptile::BRIDGE: return "Maptile::BRIDGE";
    case Maptile::STONE: return "Maptile::STONE";
    }
  return "Maptile::NONE";
}

Glib::ustring Maptile::buildingToFriendlyName(const guint32 bldg)
{
  switch (Building(bldg))
    {
    case Maptile::NONE: return _("None");
    case Maptile::CITY: return _("City");
    case Maptile::RUIN: return _("Ruin");
    case Maptile::TEMPLE: return _("Temple");
    case Maptile::SIGNPOST: return _("Signpost");
    case Maptile::ROAD: return _("Road");
    case Maptile::PORT: return _("Port");
    case Maptile::BRIDGE: return _("Bridge");
    case Maptile::STONE: return _("Stone");
    }
  return _("None");
}
// End of file