1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#ifndef RME_BASE_MAP_H_
19#define RME_BASE_MAP_H_
20
21#include "main.h"
22#include "position.h"
23#include "filehandle.h"
24#include "map_allocator.h"
25#include "tile.h"
26
27// Class declarations
28class QTreeNode;
29class BaseMap;
30class MapIterator;
31class Floor;
32class QTreeNode;
33class TileLocation;
34
35class MapIterator {
36public:
37 MapIterator(BaseMap* _map = nullptr);
38 ~MapIterator();
39 MapIterator(const MapIterator& other);
40
41 TileLocation* operator*();
42 TileLocation* operator->();
43 MapIterator& operator++();
44 MapIterator operator++(int);
45 bool operator==(const MapIterator& other) const {
46 if (other.local_z != local_z) {
47 return false;
48 }
49 if (other.local_i != local_i) {
50 return false;
51 }
52 if (other.nodestack == nodestack) {
53 return true;
54 }
55 if (other.current_tile == current_tile) {
56 return true;
57 }
58 return false;
59 }
60 bool operator!=(const MapIterator& other) const {
61 return !(other == *this);
62 }
63
64 struct NodeIndex {
65 NodeIndex(QTreeNode* _node) :
66 index(0), node(_node) { }
67 NodeIndex(const NodeIndex& other) :
68 index(other.index), node(other.node) { }
69 int index;
70 QTreeNode* node;
71
72 bool operator==(const NodeIndex& n) const {
73 return n.node == node && n.index == index;
74 }
75 };
76
77private:
78 std::vector<NodeIndex> nodestack;
79 int local_i, local_z;
80 TileLocation* current_tile;
81 BaseMap* map;
82
83 friend class BaseMap;
84};
85
86class BaseMap {
87public:
88 BaseMap();
89 virtual ~BaseMap();
90
91 // This doesn't destroy the map structure, just clears it, if param is true, delete all tiles too.
92 void clear(bool del = true);
93 MapIterator begin();
94 MapIterator end();
95 uint64_t size() const {
96 return tilecount;
97 }
98
99 // these functions take a position and returns a tile on the map
100 Tile* createTile(int x, int y, int z);
101 Tile* getTile(int x, int y, int z);
102 Tile* getTile(const Position& pos);
103 Tile* getOrCreateTile(const Position& pos);
104 const Tile* getTile(int x, int y, int z) const;
105 const Tile* getTile(const Position& pos) const;
106 TileLocation* getTileL(int x, int y, int z);
107 TileLocation* getTileL(const Position& pos);
108 TileLocation* createTileL(int x, int y, int z);
109 TileLocation* createTileL(const Position& pos);
110 const TileLocation* getTileL(int x, int y, int z) const;
111 const TileLocation* getTileL(const Position& pos) const;
112
113 // Get a Quad Tree Leaf from the map
114 QTreeNode* getLeaf(int x, int y) {
115 return root.getLeaf(x, y);
116 }
117 QTreeNode* createLeaf(int x, int y) {
118 return root.getLeafForce(x, y);
119 }
120
121 // Assigns a tile, it might seem pointless to provide position, but it is not, as the passed tile may be nullptr
122 void setTile(int _x, int _y, int _z, Tile* newtile, bool remove = false);
123 void setTile(const Position& pos, Tile* newtile, bool remove = false) {
124 setTile(pos.x, pos.y, pos.z, newtile, remove);
125 }
126 void setTile(Tile* newtile, bool remove = false) {
127 setTile(newtile->getX(), newtile->getY(), newtile->getZ(), newtile, remove);
128 }
129 // Replaces a tile and returns the old one
130 Tile* swapTile(int _x, int _y, int _z, Tile* newtile);
131 Tile* swapTile(const Position& pos, Tile* newtile) {
132 return swapTile(pos.x, pos.y, pos.z, newtile);
133 }
134
135 // Clears the visiblity according to the mask passed
136 void clearVisible(uint32_t mask);
137
138 uint64_t getTileCount() const {
139 return tilecount;
140 }
141
142public:
143 MapAllocator allocator;
144
145protected:
146 uint64_t tilecount;
147
148 QTreeNode root; // The Quad Tree root
149
150 friend class QTreeNode;
151};
152
153inline Tile* BaseMap::getTile(int x, int y, int z) {
154 TileLocation* l = getTileL(x, y, z);
155 return l ? l->get() : nullptr;
156}
157
158inline Tile* BaseMap::getTile(const Position& pos) {
159 TileLocation* l = getTileL(pos);
160 return l ? l->get() : nullptr;
161}
162
163inline const Tile* BaseMap::getTile(int x, int y, int z) const {
164 const TileLocation* l = getTileL(x, y, z);
165 return l ? l->get() : nullptr;
166}
167
168inline const Tile* BaseMap::getTile(const Position& pos) const {
169 const TileLocation* l = getTileL(pos);
170 return l ? l->get() : nullptr;
171}
172
173#endif
174