-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.cpp
214 lines (185 loc) · 5.24 KB
/
Map.cpp
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
/*
* Map.cpp
* TractionEdge
*
* Created by Steven Hamilton on 20/11/09.
* Copyright 2009 scorch.org. All rights reserved.
*
*/
#include <fstream>
#include <iostream>
#include <string>
#include "defines.h"
#include "Map.h"
#include "MapGenerator.h"
#include "tinyxml.h"
#include "Utility.h"
#define TIXML_USE_STL
Map::Map()
{
creatureSchema["human"]=HUMAN;
creatureSchema["slither"]=SLITHER;
creatureSchema["gimp"]=GIMP;
parameterMap["squadsize"]=2;
parameterMap["visibility"]=8;
}
Map::~Map()
{
}
void Map::loadMap(int mapNumber)
{
Utility tool;
objectId=WORLD.getObjectId();
//std::cout << "Map objectId = " << objectId << std::endl;
//initialise map
//loadMapTerrain(mapNumber);
loadRandomTerrain();
//set fixed start zone. Move this into .tmx properties!
startx1=1;
starty1=5;
startx2=4;
starty2=20;
}
void Map::loadMapParameters(int mapNumber)
{
//YES I know this is repeated above!
//load map parameter file
Utility tool;
std::string paramURL = "maps/map"+tool.stringFromInt(mapNumber)+".xml";
TiXmlDocument paramFile(paramURL.c_str());
bool loadOkay = paramFile.LoadFile();
if (loadOkay)
{
std::cout << "Map.cpp:loaded " << paramURL << std::endl;
}
else
{
std::cout << "Map.cpp:failed to load " << paramURL << std::endl;
exit(0);
}
//set paramaters
TiXmlHandle paramHandle(¶mFile);
for(TiXmlElement * itemNode=paramHandle.FirstChild("parameters").FirstChild().ToElement(); itemNode; itemNode=itemNode->NextSiblingElement() )
{
parameterMap[itemNode->Value()]=tool.stringToInt(itemNode->GetText());
//std::cout << "Setting " << itemNode->Value() << " to " << parameterMap[itemNode->Value()] << std::endl;
}
//process texts
TiXmlElement * introTextNode=paramHandle.FirstChild("introtext").ToElement();
introText.append(introTextNode->GetText());
TiXmlElement * resultTextNode=paramHandle.FirstChild("resulttext").ToElement();
resultText.append(resultTextNode->GetText());
}
void Map::loadRandomTerrain()
{
MapGenerator mapGen;
map = mapGen.getMap(LANDSCAPE_FARM);
}
void Map::loadMapTerrain(int level)
{
Utility tool;
map.clear();
map.resize(MAPHEIGHT);
for (int i=0; i < MAPHEIGHT; i++)
map[i].resize(MAPWIDTH);
//std::cout << "BING" << std::endl;
//load map tmx file
std::string mapURL = "maps/map"+tool.stringFromInt(level)+".tmx";
TiXmlDocument mapFile(mapURL.c_str());
bool loadOkay = mapFile.LoadFile();
if (loadOkay)
{
std::cout << "loaded " << mapURL << std::endl; // defined later in the tutorial
}
else
{
std::cout << "failed to load " << mapURL << std::endl;
exit(0);
}
TiXmlHandle mapHandle(&mapFile);
TiXmlElement * tileElement=mapHandle.FirstChild("map").FirstChild("layer").FirstChild("data").FirstChild("tile").ToElement();
for (int y=0;y < map.size(); y++)
for (int x=0;x < MAPWIDTH; x++){
if (tileElement){
//std::cout << "gid id=" << tileElement->Attribute("gid") << std::endl;
int terrainId=tool.stringToInt(tileElement->Attribute("gid"));
Terrain terrain(terrainId-1);
terrain.isVisible=false;
map[y][x] = terrain;
// std::cout << map[y][x].gfxId;
tileElement=tileElement->NextSiblingElement();
}
}
}
void Map::replaceMapTerrain(std::vector<int> &gids)
{
//replaces map terrains. Usually used by Saver.cpp for loading
std::vector<int>::iterator it;
it=gids.begin();
for (int y=0;y < map.size(); y++){
for (int x=0;x < MAPWIDTH; x++){
//std::cout << "gid id=" << tileElement->Attribute("gid") << std::endl;
Terrain terrain((*it));
terrain.isVisible=false;
map[y][x] = terrain;
it++;
//std::cout << map[y][x].gfxId;
}
}
}
bool Map::isTileTranslucent(int x, int y)
{
//if tile is height 1 or less then we can see over it.
if (map[y][x].isTranslucent) return true;
else return false;
}
bool Map::isTileVisible(Position position)
{
if (map[position.y][position.x].isVisible) return true;
else return false;
}
bool Map::setTileVisibility(Position position, bool flag)
{
map[position.y][position.x].isVisible = flag;
if (flag) map[position.y][position.x].wasVisible = flag;
}
bool Map::isTerrainPassable(Position position)
{
if (map[position.y][position.x].height<=1) return true;
return false;
}
void Map::setTilePassable(Position position, bool flag)
{
map[position.y][position.x].isPassable = flag;
}
int Map::getMoveCost(Position position)
{
return map[position.y][position.x].moveCost;
}
int Map::getHeight(Position position)
{
return map[position.y][position.x].height;
}
int Map::damage(Position position, int damage)
{
int resultantDamage = damage;
Terrain* tile=&map[position.y][position.x];
if (tile->height>0) {
tile->currentHitPoints-=damage;
if (tile->currentHitPoints<=0) destroyTerrain(position);
}
//std::cout << "Tile damage: " << damage << std::endl;
return resultantDamage;
}
void Map::destroyTerrain(Position position)
{
Utility tool;
Terrain & tile=map[position.y][position.x];
Terrain deadTerrain(tile.deadTerrain);
//std::cout << "Replacing tile " << tile.name << " with " << tool.stringFromInt(tile.deadTerrain) << std::endl;
map[position.y][position.x] = deadTerrain;
}
std::string Map::terrainNameAt(Position position)
{
return map[position.y][position.x].name;
}