forked from peonso/forgottenserver036pl1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.h
285 lines (230 loc) · 8.32 KB
/
map.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#ifndef __MAP__
#define __MAP__
#include "tools.h"
#include "fileloader.h"
#include "position.h"
#include "waypoints.h"
#include "tile.h"
class Creature;
class Player;
class Game;
class Tile;
class Map;
struct FindPathParams;
struct AStarNode
{
uint16_t x, y;
AStarNode* parent;
int32_t f, g, h;
};
using boost::shared_ptr;
#define MAP_MAX_LAYERS 16
#define MAX_NODES 512
#define GET_NODE_INDEX(a) (a - &nodes[0])
#define MAP_NORMALWALKCOST 10
#define MAP_DIAGONALWALKCOST 25
class AStarNodes
{
public:
AStarNodes();
virtual ~AStarNodes() {}
void openNode(AStarNode* node);
void closeNode(AStarNode* node);
uint32_t countOpenNodes();
uint32_t countClosedNodes();
AStarNode* getBestNode();
AStarNode* createOpenNode();
AStarNode* getNodeInList(uint16_t x, uint16_t y);
bool isInList(uint16_t x, uint16_t y);
int32_t getEstimatedDistance(uint16_t x, uint16_t y, uint16_t xGoal, uint16_t yGoal);
int32_t getMapWalkCost(const Creature* creature, AStarNode* node,
const Tile* neighbourTile, const Position& neighbourPos);
static int32_t getTileWalkCost(const Creature* creature, const Tile* tile);
private:
AStarNode nodes[MAX_NODES];
std::bitset<MAX_NODES> openNodes;
uint32_t curNode;
};
template<class T> class lessPointer: public std::binary_function<T*, T*, bool>
{
public:
bool operator()(T*& t1, T*& t2) {return *t1 < *t2;}
};
#define FLOOR_BITS 3
#define FLOOR_SIZE (1 << FLOOR_BITS)
#define FLOOR_MASK (FLOOR_SIZE - 1)
struct Floor
{
Floor();
Tile* tiles[FLOOR_SIZE][FLOOR_SIZE];
};
class FrozenPathingConditionCall;
class QTreeLeafNode;
class QTreeNode
{
public:
QTreeNode();
virtual ~QTreeNode();
bool isLeaf() const {return m_isLeaf;}
QTreeLeafNode* getLeaf(uint16_t x, uint16_t y);
static QTreeLeafNode* getLeafStatic(QTreeNode* root, uint16_t x, uint16_t y);
QTreeLeafNode* createLeaf(uint16_t x, uint16_t y, uint16_t level);
protected:
bool m_isLeaf;
QTreeNode* m_child[4];
friend class Map;
};
class QTreeLeafNode : public QTreeNode
{
public:
QTreeLeafNode();
virtual ~QTreeLeafNode();
Floor* createFloor(uint16_t z);
Floor* getFloor(uint16_t z){return m_array[z];}
QTreeLeafNode* stepSouth(){return m_leafS;}
QTreeLeafNode* stepEast(){return m_leafE;}
void addCreature(Creature* c);
void removeCreature(Creature* c);
protected:
static bool newLeaf;
QTreeLeafNode* m_leafS;
QTreeLeafNode* m_leafE;
Floor* m_array[MAP_MAX_LAYERS];
CreatureVector creatureList;
friend class Map;
friend class QTreeNode;
};
/**
* Map class.
* Holds all the actual map-data
*/
class Map
{
public:
Map();
virtual ~Map() {}
static const int32_t maxViewportX = 11; //min value: maxClientViewportX + 1
static const int32_t maxViewportY = 11; //min value: maxClientViewportY + 1
static const int32_t maxClientViewportX = 8;
static const int32_t maxClientViewportY = 6;
/**
* Load a map.
* \returns true if the map was loaded successfully
*/
bool loadMap(const std::string& identifier);
/**
* Save a map.
* \param identifier file/database to save to
* \returns true if the map was saved successfully
*/
bool saveMap();
/**
* Get a single tile.
* \returns A pointer to that tile.
*/
Tile* getTile(int32_t x, int32_t y, int32_t z);
Tile* getTile(const Position& pos) {return getTile(pos.x, pos.y, pos.z);}
/**
* Set a single tile.
* \param a tile to set for the position
*/
void setTile(uint16_t _x, uint16_t _y, uint16_t _z, Tile* newTile);
void setTile(const Position& pos, Tile* newTile) {setTile(pos.x, pos.y, pos.z, newTile);}
/**
* Place a creature on the map
* \param pos The position to place the creature
* \param creature Creature to place on the map
* \param extendedPos If true, the creature will in first-hand be placed 2 tiles away
* \param forceLogin If true, placing the creature will not fail becase of obstacles (creatures/chests)
*/
bool placeCreature(const Position& centerPos, Creature* creature, bool extendedPos = false, bool forceLogin = false);
/**
* Remove a creature from the map.
* \param c Creature pointer to the creature to remove
*/
bool removeCreature(Creature* c);
/**
* Checks if you can throw an object to that position
* \param fromPos from Source point
* \param toPos Destination point
* \param rangex maximum allowed range horizontially
* \param rangey maximum allowed range vertically
* \param checkLineOfSight checks if there is any blocking objects in the way
* \returns The result if you can throw there or not
*/
bool canThrowObjectTo(const Position& fromPos, const Position& toPos, bool checkLineOfSight = true,
int32_t rangex = Map::maxClientViewportX, int32_t rangey = Map::maxClientViewportY);
/**
* Checks if path is clear from fromPos to toPos
* Notice: This only checks a straight line if the path is clear, for path finding use getPathTo.
* \param fromPos from Source point
* \param toPos Destination point
* \param floorCheck if true then view is not clear if fromPos.z is not the same as toPos.z
* \returns The result if there is no obstacles
*/
bool isSightClear(const Position& fromPos, const Position& toPos, bool floorCheck) const;
bool checkSightLine(const Position& fromPos, const Position& toPos) const;
/**
* Get the path to a specific position on the map.
* \param creature The creature that wants a path
* \param destPos The position we want a path calculated to
* \param listDir contains a list of directions to the destination
* \param maxDist Maximum distance from our current position to search, default: -1 (no limit)
* \returns returns true if a path was found
*/
bool getPathTo(const Creature* creature, const Position& destPos,
std::list<Direction>& listDir, int32_t maxDist = -1);
bool getPathMatching(const Creature* creature, std::list<Direction>& dirList,
const FrozenPathingConditionCall& pathCondition, const FindPathParams& fpp);
QTreeLeafNode* getLeaf(uint16_t x, uint16_t y) {return root.getLeaf(x, y);}
const Tile* canWalkTo(const Creature* creature, const Position& pos);
Waypoints waypoints;
protected:
QTreeNode root;
uint32_t mapWidth, mapHeight;
std::string spawnfile, housefile;
StringVec descriptions;
SpectatorCache spectatorCache;
void clearSpectatorCache() {spectatorCache.clear();}
// Actually scans the map for spectators
void getSpectatorsInternal(SpectatorVec& list, const Position& centerPos, bool checkforduplicate,
int32_t minRangeX, int32_t maxRangeX, int32_t minRangeY, int32_t maxRangeY,
int32_t minRangeZ, int32_t maxRangeZ);
// Use this when a custom spectator vector is needed, this support many
// more parameters than the heavily cached version below.
void getSpectators(SpectatorVec& list, const Position& centerPos, bool checkforduplicate = false, bool multifloor = false,
int32_t minRangeX = 0, int32_t maxRangeX = 0, int32_t minRangeY = 0, int32_t maxRangeY = 0);
// The returned SpectatorVec is a temporary and should not be kept around
// Take special heed in that the vector will be destroyed if any function
// that calls clearSpectatorCache is called.
const SpectatorVec& getSpectators(const Position& centerPos);
friend class Game;
friend class IOMap;
};
inline void QTreeLeafNode::addCreature(Creature* c)
{
creatureList.push_back(c);
}
inline void QTreeLeafNode::removeCreature(Creature* c)
{
CreatureVector::iterator it = std::find(creatureList.begin(), creatureList.end(), c);
assert(it != creatureList.end());
creatureList.erase(it);
}
#endif