-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmap.h
298 lines (239 loc) · 8.58 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
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef FS_MAP_H_E3953D57C058461F856F5221D359DAFA
#define FS_MAP_H_E3953D57C058461F856F5221D359DAFA
#include "position.h"
#include "item.h"
#include "fileloader.h"
#include "tools.h"
#include "tile.h"
#include "town.h"
#include "house.h"
#include "spawn.h"
class Creature;
class Player;
class Game;
class Tile;
class Map;
static constexpr int32_t MAP_MAX_LAYERS = 16;
struct FindPathParams;
struct AStarNode {
AStarNode* parent;
int_fast32_t f, g, c;
uint16_t x, y;
};
static constexpr int32_t MAX_NODES = 512;
static constexpr int32_t MAP_NORMALWALKCOST = 10;
static constexpr int32_t MAP_DIAGONALWALKCOST = 25;
class AStarNodes
{
public:
AStarNodes(uint32_t x, uint32_t y, int_fast32_t extraCost);
bool createOpenNode(AStarNode* parent, uint32_t x, uint32_t y, int_fast32_t f, int_fast32_t heuristic, int_fast32_t extraCost);
AStarNode* getBestNode();
void closeNode(AStarNode* node);
void openNode(AStarNode* node);
int32_t getClosedNodes() const;
AStarNode* getNodeByPosition(uint32_t x, uint32_t y);
static inline int_fast32_t getMapWalkCost(AStarNode* node, const Position& neighborPos);
static inline int_fast32_t getTileWalkCost(const Creature& creature, const Tile* tile);
private:
#if defined(__SSE2__)
alignas(16) uint32_t nodesTable[MAX_NODES];
alignas(16) int32_t calculatedNodes[MAX_NODES];
AStarNode nodes[MAX_NODES];
#else
AStarNode nodes[MAX_NODES];
uint32_t nodesTable[MAX_NODES];
#endif
int32_t closedNodes;
int32_t curNode;
bool openNodes[MAX_NODES];
};
using SpectatorCache = std::map<Position, SpectatorVector>;
static constexpr int32_t FLOOR_BITS = 3;
static constexpr int32_t FLOOR_SIZE = (1 << FLOOR_BITS);
static constexpr int32_t FLOOR_MASK = (FLOOR_SIZE - 1);
struct Floor {
constexpr Floor() = default;
~Floor();
// non-copyable
Floor(const Floor&) = delete;
Floor& operator=(const Floor&) = delete;
Tile* tiles[FLOOR_SIZE][FLOOR_SIZE] = {};
};
class FrozenPathingConditionCall;
class QTreeLeafNode;
class QTreeNode
{
public:
constexpr QTreeNode() = default;
virtual ~QTreeNode();
// non-copyable
QTreeNode(const QTreeNode&) = delete;
QTreeNode& operator=(const QTreeNode&) = delete;
bool isLeaf() const {
return leaf;
}
QTreeLeafNode* getLeaf(uint32_t x, uint32_t y);
template<typename Leaf, typename Node>
static Leaf getLeafStatic(Node node, uint32_t x, uint32_t y)
{
do {
node = node->child[((x & 0x8000) >> 15) | ((y & 0x8000) >> 14)];
if (!node) {
return nullptr;
}
x <<= 1;
y <<= 1;
} while (!node->leaf);
return static_cast<Leaf>(node);
}
QTreeLeafNode* createLeaf(uint32_t x, uint32_t y, uint32_t level);
protected:
bool leaf = false;
private:
QTreeNode* child[4] = {};
friend class Map;
};
class QTreeLeafNode final : public QTreeNode
{
public:
QTreeLeafNode() { leaf = true; newLeaf = true; }
~QTreeLeafNode();
// non-copyable
QTreeLeafNode(const QTreeLeafNode&) = delete;
QTreeLeafNode& operator=(const QTreeLeafNode&) = delete;
Floor* createFloor(uint32_t z);
Floor* getFloor(uint8_t z) const {
return array[z];
}
void addCreature(Creature* c);
void removeCreature(Creature* c);
private:
static bool newLeaf;
QTreeLeafNode* leafS = nullptr;
QTreeLeafNode* leafE = nullptr;
Floor* array[MAP_MAX_LAYERS] = {};
CreatureVector creature_list;
CreatureVector player_list;
friend class Map;
friend class QTreeNode;
};
/**
* Map class.
* Holds all the actual map-data
*/
class Map
{
public:
static constexpr int32_t maxViewportX = 11; //min value: maxClientViewportX + 1
static constexpr int32_t maxViewportY = 11; //min value: maxClientViewportY + 1
static constexpr int32_t maxClientViewportX = 8;
static constexpr int32_t maxClientViewportY = 6;
uint32_t clean() const;
/**
* Load a map.
* \returns true if the map was loaded successfully
*/
bool loadMap(const std::string& identifier, bool loadHouses);
/**
* Save a map.
* \returns true if the map was saved successfully
*/
static bool save();
/**
* Get a single tile.
* \returns A pointer to that tile.
*/
Tile* getTile(uint16_t x, uint16_t y, uint8_t z) const;
Tile* getTile(const Position& pos) const {
return getTile(pos.x, pos.y, pos.z);
}
/**
* Set a single tile.
*/
void setTile(uint16_t x, uint16_t y, uint8_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 centerPos 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);
void moveCreature(Creature& creature, Tile& newTile, bool forceTeleport = false);
void getSpectators(SpectatorVector& spectators, const Position& centerPos, bool multifloor = false, bool onlyPlayers = false,
int32_t minRangeX = 0, int32_t maxRangeX = 0,
int32_t minRangeY = 0, int32_t maxRangeY = 0);
void clearSpectatorCache(bool clearPlayer);
void clearPlayersSpectatorCache();
/**
* 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, SightLines_t lineOfSight = SightLine_CheckSightLine,
int32_t rangex = Map::maxClientViewportX, int32_t rangey = Map::maxClientViewportY) const;
/**
* 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;
const Tile* canWalkTo(const Creature& creature, const Position& pos) const;
bool getPathMatching(const Creature& creature, const Position& targetPos, std::vector<Direction>& dirList,
const FrozenPathingConditionCall& pathCondition, const FindPathParams& fpp) const;
bool getPathMatchingCond(const Creature& creature, const Position& targetPos, std::vector<Direction>& dirList,
const FrozenPathingConditionCall& pathCondition, const FindPathParams& fpp) const;
std::map<std::string, Position> waypoints;
QTreeLeafNode* getQTNode(uint16_t x, uint16_t y) {
return QTreeNode::getLeafStatic<QTreeLeafNode*, QTreeNode*>(&root, x, y);
}
Spawns spawns;
Towns towns;
Houses houses;
private:
SpectatorCache spectatorCache;
SpectatorCache playersSpectatorCache;
QTreeNode root;
std::string spawnfile;
std::string housefile;
uint32_t width = 0;
uint32_t height = 0;
// Actually scans the map for spectators
void getSpectatorsInternal(SpectatorVector& spectators, const Position& centerPos,
int32_t minRangeX, int32_t maxRangeX,
int32_t minRangeY, int32_t maxRangeY,
int32_t minRangeZ, int32_t maxRangeZ, bool onlyPlayers) const;
friend class Game;
friend class IOMap;
};
#endif