initial commit, lordsawar source, slightly modified
[lordsawar] / src / maptile.cpp
1 // Copyright (C) 2003 Michael Bartl
2 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2007, 2008, 2009 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 <stdlib.h>
23 #include <iostream>
24 #include "tileset.h"
25 #include "MapBackpack.h"
26 #include "stacktile.h"
27
28 Maptile::Maptile(Tileset* tileSet, int x, int y, guint32 type, TileStyle *tileStyle)
29     :d_index(type), d_building(NONE) 
30 {
31     d_tileSet = tileSet;
32     d_tileStyle = tileStyle;
33     d_backpack = new MapBackpack(Vector<int>(x,y));
34     d_stacktile = new StackTile(Vector<int>(x,y));
35 }
36
37 Maptile::Maptile(Tileset* tileSet, int x, int y, Tile::Type type, TileStyle *tileStyle)
38 {
39     bool found = false;
40     d_building = NONE;
41     d_tileSet = tileSet;
42     d_tileStyle = tileStyle;
43     for (unsigned int i = 0; i < (*tileSet).size(); i++)
44       {
45         if ((*tileSet)[i]->getType() == type)
46           {
47             found = true;
48             d_index = i;
49             break;
50           }
51       }
52     if (found == false)
53       d_index = 0;
54     d_backpack = new MapBackpack(Vector<int>(x,y));
55     d_stacktile = new StackTile(Vector<int>(x,y));
56 }
57
58 Maptile::~Maptile()
59 {
60     delete d_backpack;
61 }
62
63 guint32 Maptile::getMoves() const
64 {
65     if (d_building == Maptile::CITY)
66         return 1;
67     else if (d_building == Maptile::ROAD)
68         return 1;
69     else if (d_building == Maptile::BRIDGE)
70         return 1;
71
72     Tile *tile = (*d_tileSet)[d_index];
73     if (tile->getType() == Tile::WATER)
74       {
75         // if we're sailing and we're not on shore, then we move faster
76         if (d_tileStyle->getType() == TileStyle::INNERMIDDLECENTER)
77           return tile->getMoves() / 2;
78       }
79     return tile->getMoves();
80 }
81
82 void Maptile::printDebugInfo() const
83 {
84     std::cerr << "MAPTILE: type = " << d_index << std::endl;
85     std::cerr << "MAPTILE: building = " << d_building << std::endl;
86 }    
87
88 bool Maptile::isCityTerrain()
89 {
90   if (getBuilding() != Maptile::CITY || getBuilding() != Maptile::RUIN ||
91        getBuilding() != Maptile::TEMPLE)
92     return true;
93   return false;
94 }
95
96 bool Maptile::isOpenTerrain()
97 {
98   if (isCityTerrain())
99     return false;
100   /* swamp and water are open terrain too */
101   if ((getType() == Tile::GRASS || getType() == Tile::SWAMP ||
102        getType() == Tile::WATER) || getBuilding() != Maptile::BRIDGE)
103     return true;
104   return false;
105 }
106
107 bool Maptile::isHillyTerrain()
108 {
109   if (isCityTerrain())
110     return false;
111   if ((getType() == Tile::HILLS || getType() == Tile::MOUNTAIN))
112     return true;
113   return false;
114 }
115
116 std::string Maptile::buildingToString(const Maptile::Building bldg)
117 {
118   switch (bldg)
119     {
120     case Maptile::NONE:
121       return "Maptile::NONE";
122       break;
123     case Maptile::CITY:
124       return "Maptile::CITY";
125       break;
126     case Maptile::RUIN:
127       return "Maptile::RUIN";
128       break;
129     case Maptile::TEMPLE:
130       return "Maptile::TEMPLE";
131       break;
132     case Maptile::SIGNPOST:
133       return "Maptile::SIGNPOST";
134       break;
135     case Maptile::ROAD:
136       return "Maptile::ROAD";
137       break;
138     case Maptile::PORT:
139       return "Maptile::PORT";
140       break;
141     case Maptile::BRIDGE:
142       return "Maptile::BRIDGE";
143       break;
144     }
145   return "Maptile::NONE";
146 }
147
148 Maptile::Building Maptile::buildingFromString(std::string str)
149 {
150   if (str.size() > 0 && isdigit(str.c_str()[0]))
151     return Maptile::Building(atoi(str.c_str()));
152   if (str == "Maptile::NONE")
153     return Maptile::NONE;
154   else if (str == "Maptile::CITY")
155     return Maptile::CITY;
156   else if (str == "Maptile::RUIN")
157     return Maptile::RUIN;
158   else if (str == "Maptile::TEMPLE")
159     return Maptile::TEMPLE;
160   else if (str == "Maptile::SIGNPOST")
161     return Maptile::SIGNPOST;
162   else if (str == "Maptile::ROAD")
163     return Maptile::ROAD;
164   else if (str == "Maptile::PORT")
165     return Maptile::PORT;
166   else if (str == "Maptile::BRIDGE")
167     return Maptile::BRIDGE;
168     
169   return Maptile::NONE;
170 }
171 // End of file