Performance fix + SAO factorization
[oweals/minetest.git] / src / mapsector.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "mapsector.h"
21 #include "exceptions.h"
22 #include "mapblock.h"
23 #include "serialization.h"
24
25 MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
26                 differs_from_disk(false),
27                 m_parent(parent),
28                 m_pos(pos),
29                 m_gamedef(gamedef),
30                 m_block_cache(NULL)
31 {
32 }
33
34 MapSector::~MapSector()
35 {
36         deleteBlocks();
37 }
38
39 void MapSector::deleteBlocks()
40 {
41         // Clear cache
42         m_block_cache = NULL;
43
44         // Delete all
45         for (UNORDERED_MAP<s16, MapBlock*>::iterator i = m_blocks.begin();
46                         i != m_blocks.end(); ++i) {
47                 delete i->second;
48         }
49
50         // Clear container
51         m_blocks.clear();
52 }
53
54 MapBlock * MapSector::getBlockBuffered(s16 y)
55 {
56         MapBlock *block;
57
58         if (m_block_cache != NULL && y == m_block_cache_y) {
59                 return m_block_cache;
60         }
61
62         // If block doesn't exist, return NULL
63         UNORDERED_MAP<s16, MapBlock*>::iterator n = m_blocks.find(y);
64         block = (n != m_blocks.end() ? n->second : NULL);
65
66         // Cache the last result
67         m_block_cache_y = y;
68         m_block_cache = block;
69
70         return block;
71 }
72
73 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
74 {
75         return getBlockBuffered(y);
76 }
77
78 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
79 {
80         assert(getBlockBuffered(y) == NULL);    // Pre-condition
81
82         v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
83
84         MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
85
86         return block;
87 }
88
89 MapBlock * MapSector::createBlankBlock(s16 y)
90 {
91         MapBlock *block = createBlankBlockNoInsert(y);
92
93         m_blocks[y] = block;
94
95         return block;
96 }
97
98 void MapSector::insertBlock(MapBlock *block)
99 {
100         s16 block_y = block->getPos().Y;
101
102         MapBlock *block2 = getBlockBuffered(block_y);
103         if(block2 != NULL){
104                 throw AlreadyExistsException("Block already exists");
105         }
106
107         v2s16 p2d(block->getPos().X, block->getPos().Z);
108         assert(p2d == m_pos);
109
110         // Insert into container
111         m_blocks[block_y] = block;
112 }
113
114 void MapSector::deleteBlock(MapBlock *block)
115 {
116         s16 block_y = block->getPos().Y;
117
118         // Clear from cache
119         m_block_cache = NULL;
120
121         // Remove from container
122         m_blocks.erase(block_y);
123
124         // Delete
125         delete block;
126 }
127
128 void MapSector::getBlocks(MapBlockVect &dest)
129 {
130         for (UNORDERED_MAP<s16, MapBlock*>::iterator bi = m_blocks.begin();
131                 bi != m_blocks.end(); ++bi) {
132                 dest.push_back(bi->second);
133         }
134 }
135
136 /*
137         ServerMapSector
138 */
139
140 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
141                 MapSector(parent, pos, gamedef)
142 {
143 }
144
145 ServerMapSector::~ServerMapSector()
146 {
147 }
148
149 void ServerMapSector::serialize(std::ostream &os, u8 version)
150 {
151         if(!ser_ver_supported(version))
152                 throw VersionMismatchException("ERROR: MapSector format not supported");
153
154         /*
155                 [0] u8 serialization version
156                 + heightmap data
157         */
158
159         // Server has both of these, no need to support not having them.
160         //assert(m_objects != NULL);
161
162         // Write version
163         os.write((char*)&version, 1);
164
165         /*
166                 Add stuff here, if needed
167         */
168
169 }
170
171 ServerMapSector* ServerMapSector::deSerialize(
172                 std::istream &is,
173                 Map *parent,
174                 v2s16 p2d,
175                 std::map<v2s16, MapSector*> & sectors,
176                 IGameDef *gamedef
177         )
178 {
179         /*
180                 [0] u8 serialization version
181                 + heightmap data
182         */
183
184         /*
185                 Read stuff
186         */
187
188         // Read version
189         u8 version = SER_FMT_VER_INVALID;
190         is.read((char*)&version, 1);
191
192         if(!ser_ver_supported(version))
193                 throw VersionMismatchException("ERROR: MapSector format not supported");
194
195         /*
196                 Add necessary reading stuff here
197         */
198
199         /*
200                 Get or create sector
201         */
202
203         ServerMapSector *sector = NULL;
204
205         std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
206
207         if(n != sectors.end())
208         {
209                 warningstream<<"deSerializing existent sectors not supported "
210                                 "at the moment, because code hasn't been tested."
211                                 <<std::endl;
212
213                 MapSector *sector = n->second;
214                 assert(sector->getId() == MAPSECTOR_SERVER);
215                 return (ServerMapSector*)sector;
216         }
217         else
218         {
219                 sector = new ServerMapSector(parent, p2d, gamedef);
220                 sectors[p2d] = sector;
221         }
222
223         /*
224                 Set stuff in sector
225         */
226
227         // Nothing here
228
229         return sector;
230 }
231
232 #ifndef SERVER
233 /*
234         ClientMapSector
235 */
236
237 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
238                 MapSector(parent, pos, gamedef)
239 {
240 }
241
242 ClientMapSector::~ClientMapSector()
243 {
244 }
245
246 #endif // !SERVER
247
248 //END