]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapsector.cpp
Fix my name.
[dragonfireclient.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 "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         for(std::map<s16, MapBlock*>::iterator i = m_blocks.begin();
49                 i != m_blocks.end(); ++i)
50         {
51                 delete i->second;
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         std::map<s16, MapBlock*>::iterator n = m_blocks.find(y);
68         if(n == m_blocks.end())
69         {
70                 block = NULL;
71         }
72         // If block exists, return it
73         else{
74                 block = n->second;
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[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[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.erase(block_y);
134
135         // Delete
136         delete block;
137 }
138
139 void MapSector::getBlocks(std::list<MapBlock*> &dest)
140 {
141         for(std::map<s16, MapBlock*>::iterator bi = m_blocks.begin();
142                 bi != m_blocks.end(); ++bi)
143         {
144                 dest.push_back(bi->second);
145         }
146 }
147
148 /*
149         ServerMapSector
150 */
151
152 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
153                 MapSector(parent, pos, gamedef)
154 {
155 }
156
157 ServerMapSector::~ServerMapSector()
158 {
159 }
160
161 void ServerMapSector::serialize(std::ostream &os, u8 version)
162 {
163         if(!ser_ver_supported(version))
164                 throw VersionMismatchException("ERROR: MapSector format not supported");
165         
166         /*
167                 [0] u8 serialization version
168                 + heightmap data
169         */
170         
171         // Server has both of these, no need to support not having them.
172         //assert(m_objects != NULL);
173
174         // Write version
175         os.write((char*)&version, 1);
176         
177         /*
178                 Add stuff here, if needed
179         */
180
181 }
182
183 ServerMapSector* ServerMapSector::deSerialize(
184                 std::istream &is,
185                 Map *parent,
186                 v2s16 p2d,
187                 std::map<v2s16, MapSector*> & sectors,
188                 IGameDef *gamedef
189         )
190 {
191         /*
192                 [0] u8 serialization version
193                 + heightmap data
194         */
195
196         /*
197                 Read stuff
198         */
199         
200         // Read version
201         u8 version = SER_FMT_VER_INVALID;
202         is.read((char*)&version, 1);
203         
204         if(!ser_ver_supported(version))
205                 throw VersionMismatchException("ERROR: MapSector format not supported");
206         
207         /*
208                 Add necessary reading stuff here
209         */
210         
211         /*
212                 Get or create sector
213         */
214
215         ServerMapSector *sector = NULL;
216
217         std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
218
219         if(n != sectors.end())
220         {
221                 dstream<<"WARNING: deSerializing existent sectors not supported "
222                                 "at the moment, because code hasn't been tested."
223                                 <<std::endl;
224
225                 MapSector *sector = n->second;
226                 assert(sector->getId() == MAPSECTOR_SERVER);
227                 return (ServerMapSector*)sector;
228         }
229         else
230         {
231                 sector = new ServerMapSector(parent, p2d, gamedef);
232                 sectors[p2d] = sector;
233         }
234
235         /*
236                 Set stuff in sector
237         */
238
239         // Nothing here
240
241         return sector;
242 }
243
244 #ifndef SERVER
245 /*
246         ClientMapSector
247 */
248
249 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
250                 MapSector(parent, pos, gamedef)
251 {
252 }
253
254 ClientMapSector::~ClientMapSector()
255 {
256 }
257
258 #endif // !SERVER
259
260 //END