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