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