Switch the license to be LGPLv2/later, with small parts still remaining as GPLv2...
[oweals/minetest.git] / src / mapsector.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 "jmutexautolock.h"
22 #ifndef SERVER
23 #include "client.h"
24 #endif
25 #include "exceptions.h"
26 #include "mapblock.h"
27
28 MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
29                 differs_from_disk(false),
30                 m_parent(parent),
31                 m_pos(pos),
32                 m_gamedef(gamedef),
33                 m_block_cache(NULL)
34 {
35 }
36
37 MapSector::~MapSector()
38 {
39         deleteBlocks();
40 }
41
42 void MapSector::deleteBlocks()
43 {
44         // Clear cache
45         m_block_cache = NULL;
46
47         // Delete all
48         core::map<s16, MapBlock*>::Iterator i = m_blocks.getIterator();
49         for(; i.atEnd() == false; i++)
50         {
51                 delete i.getNode()->getValue();
52         }
53
54         // Clear container
55         m_blocks.clear();
56 }
57
58 MapBlock * MapSector::getBlockBuffered(s16 y)
59 {
60         MapBlock *block;
61
62         if(m_block_cache != NULL && y == m_block_cache_y){
63                 return m_block_cache;
64         }
65         
66         // If block doesn't exist, return NULL
67         core::map<s16, MapBlock*>::Node *n = m_blocks.find(y);
68         if(n == NULL)
69         {
70                 block = NULL;
71         }
72         // If block exists, return it
73         else{
74                 block = n->getValue();
75         }
76         
77         // Cache the last result
78         m_block_cache_y = y;
79         m_block_cache = block;
80         
81         return block;
82 }
83
84 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
85 {
86         return getBlockBuffered(y);
87 }
88
89 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
90 {
91         assert(getBlockBuffered(y) == NULL);
92
93         v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
94         
95         MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
96         
97         return block;
98 }
99
100 MapBlock * MapSector::createBlankBlock(s16 y)
101 {
102         MapBlock *block = createBlankBlockNoInsert(y);
103         
104         m_blocks.insert(y, block);
105
106         return block;
107 }
108
109 void MapSector::insertBlock(MapBlock *block)
110 {
111         s16 block_y = block->getPos().Y;
112
113         MapBlock *block2 = getBlockBuffered(block_y);
114         if(block2 != NULL){
115                 throw AlreadyExistsException("Block already exists");
116         }
117
118         v2s16 p2d(block->getPos().X, block->getPos().Z);
119         assert(p2d == m_pos);
120         
121         // Insert into container
122         m_blocks.insert(block_y, block);
123 }
124
125 void MapSector::deleteBlock(MapBlock *block)
126 {
127         s16 block_y = block->getPos().Y;
128
129         // Clear from cache
130         m_block_cache = NULL;
131         
132         // Remove from container
133         m_blocks.remove(block_y);
134
135         // Delete
136         delete block;
137 }
138
139 void MapSector::getBlocks(core::list<MapBlock*> &dest)
140 {
141         core::list<MapBlock*> ref_list;
142
143         core::map<s16, MapBlock*>::Iterator bi;
144
145         bi = m_blocks.getIterator();
146         for(; bi.atEnd() == false; bi++)
147         {
148                 MapBlock *b = bi.getNode()->getValue();
149                 dest.push_back(b);
150         }
151 }
152
153 /*
154         ServerMapSector
155 */
156
157 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
158                 MapSector(parent, pos, gamedef)
159 {
160 }
161
162 ServerMapSector::~ServerMapSector()
163 {
164 }
165
166 void ServerMapSector::serialize(std::ostream &os, u8 version)
167 {
168         if(!ser_ver_supported(version))
169                 throw VersionMismatchException("ERROR: MapSector format not supported");
170         
171         /*
172                 [0] u8 serialization version
173                 + heightmap data
174         */
175         
176         // Server has both of these, no need to support not having them.
177         //assert(m_objects != NULL);
178
179         // Write version
180         os.write((char*)&version, 1);
181         
182         /*
183                 Add stuff here, if needed
184         */
185
186 }
187
188 ServerMapSector* ServerMapSector::deSerialize(
189                 std::istream &is,
190                 Map *parent,
191                 v2s16 p2d,
192                 core::map<v2s16, MapSector*> & sectors,
193                 IGameDef *gamedef
194         )
195 {
196         /*
197                 [0] u8 serialization version
198                 + heightmap data
199         */
200
201         /*
202                 Read stuff
203         */
204         
205         // Read version
206         u8 version = SER_FMT_VER_INVALID;
207         is.read((char*)&version, 1);
208         
209         if(!ser_ver_supported(version))
210                 throw VersionMismatchException("ERROR: MapSector format not supported");
211         
212         /*
213                 Add necessary reading stuff here
214         */
215         
216         /*
217                 Get or create sector
218         */
219
220         ServerMapSector *sector = NULL;
221
222         core::map<v2s16, MapSector*>::Node *n = sectors.find(p2d);
223
224         if(n != NULL)
225         {
226                 dstream<<"WARNING: deSerializing existent sectors not supported "
227                                 "at the moment, because code hasn't been tested."
228                                 <<std::endl;
229
230                 MapSector *sector = n->getValue();
231                 assert(sector->getId() == MAPSECTOR_SERVER);
232                 return (ServerMapSector*)sector;
233         }
234         else
235         {
236                 sector = new ServerMapSector(parent, p2d, gamedef);
237                 sectors.insert(p2d, sector);
238         }
239
240         /*
241                 Set stuff in sector
242         */
243
244         // Nothing here
245
246         return sector;
247 }
248
249 #ifndef SERVER
250 /*
251         ClientMapSector
252 */
253
254 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
255                 MapSector(parent, pos, gamedef)
256 {
257 }
258
259 ClientMapSector::~ClientMapSector()
260 {
261 }
262
263 #endif // !SERVER
264
265 //END