3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #ifndef MAPBLOCK_HEADER
21 #define MAPBLOCK_HEADER
24 #include <jmutexautolock.h>
27 #include "common_irrlicht.h"
29 #include "exceptions.h"
30 #include "serialization.h"
31 #include "constants.h"
32 #include "mapblockobject.h"
35 #define MAP_BLOCKSIZE 16
37 // Named by looking towards z+
50 video::S3DVertex vertices[4]; // Precalculated vertices
55 NODECONTAINER_ID_MAPBLOCK,
56 NODECONTAINER_ID_MAPSECTOR,
58 NODECONTAINER_ID_MAPBLOCKCACHE,
59 NODECONTAINER_ID_VOXELMANIPULATOR,
65 virtual bool isValidPosition(v3s16 p) = 0;
66 virtual MapNode getNode(v3s16 p) = 0;
67 virtual void setNode(v3s16 p, MapNode & n) = 0;
68 virtual u16 nodeContainerId() const = 0;
71 class MapBlock : public NodeContainer
76 This used by Server's block creation stuff for not sending
77 blocks that are waiting a lighting update.
79 If true, the block needs some work by the one who set this
82 While true, nobody else should touch the block.
89 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false):
93 is_underground(false),
95 //is_incomplete(false)
107 JMutexAutoLock lock(mesh_mutex);
120 virtual u16 nodeContainerId() const
122 return NODECONTAINER_ID_MAPBLOCK;
125 NodeContainer * getParent()
132 return (data == NULL);
141 bool getChangedFlag()
146 void resetChangedFlag()
151 void setChangedFlag()
161 v3s16 getPosRelative()
163 return m_pos * MAP_BLOCKSIZE;
166 bool getIsUnderground()
168 return is_underground;
171 void setIsUnderground(bool a_is_underground)
173 is_underground = a_is_underground;
177 core::aabbox3d<s16> getBox()
179 return core::aabbox3d<s16>(getPosRelative(),
181 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
189 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
190 data = new MapNode[l];
191 for(u32 i=0; i<l; i++){
197 bool isValidPosition(v3s16 p)
201 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
202 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
203 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
207 Regular MapNode get-setters
210 MapNode getNode(s16 x, s16 y, s16 z)
213 throw InvalidPositionException();
214 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
215 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
216 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
217 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
220 MapNode getNode(v3s16 p)
222 return getNode(p.X, p.Y, p.Z);
225 void setNode(s16 x, s16 y, s16 z, MapNode & n)
228 throw InvalidPositionException();
229 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
230 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
231 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
232 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
236 void setNode(v3s16 p, MapNode & n)
238 setNode(p.X, p.Y, p.Z, n);
242 Non-checking variants of the above
245 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
248 throw InvalidPositionException();
249 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
252 MapNode getNodeNoCheck(v3s16 p)
254 return getNodeNoCheck(p.X, p.Y, p.Z);
257 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
260 throw InvalidPositionException();
261 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
265 void setNodeNoCheck(v3s16 p, MapNode & n)
267 setNodeNoCheck(p.X, p.Y, p.Z, n);
271 These functions consult the parent container if the position
272 is not valid on this MapBlock.
274 bool isValidPositionParent(v3s16 p);
275 MapNode getNodeParent(v3s16 p);
276 void setNodeParent(v3s16 p, MapNode & n);
278 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
280 for(u16 z=0; z<d; z++)
281 for(u16 y=0; y<h; y++)
282 for(u16 x=0; x<w; x++)
283 setNode(x0+x, y0+y, z0+z, node);
286 static FastFace * makeFastFace(u16 tile, u8 light, v3f p,
287 v3f dir, v3f scale, v3f posRelative_f);
289 u8 getFaceLight(v3s16 p, v3s16 face_dir);
291 u8 getNodeTile(v3s16 p);
295 translate_dir: unit vector with only one of x, y or z
296 face_dir: unit vector with only one of x, y or z
298 void updateFastFaceRow(v3s16 startpos,
302 core::list<FastFace*> &dest);
306 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
308 // Copies data to VoxelManipulator to getPosRelative()
309 void copyTo(VoxelManipulator &dst);
315 void serializeObjects(std::ostream &os, u8 version)
317 m_objects.serialize(os, version);
319 // If smgr!=NULL, new objects are added to the scene
320 void updateObjects(std::istream &is, u8 version,
321 scene::ISceneManager *smgr)
323 m_objects.update(is, version, smgr);
333 void addObject(MapBlockObject *object)
334 throw(ContainerFullException, AlreadyExistsException)
336 m_objects.add(object);
340 void removeObject(s16 id)
342 m_objects.remove(id);
346 MapBlockObject * getObject(s16 id)
348 return m_objects.get(id);
350 JMutexAutoLock * getObjectLock()
352 return m_objects.getLock();
354 void stepObjects(float dtime, bool server)
356 m_objects.step(dtime, server);
361 /*void wrapObject(MapBlockObject *object)
363 m_objects.wrapObject(object);
368 // origin is relative to block
369 void getObjects(v3f origin, f32 max_d,
370 core::array<DistanceSortedObject> &dest)
372 m_objects.getObjects(origin, max_d, dest);
375 /*void getPseudoObjects(v3f origin, f32 max_d,
376 core::array<DistanceSortedObject> &dest);*/
380 return m_objects.getCount();
387 // Doesn't write version by itself
388 void serialize(std::ostream &os, u8 version);
390 void deSerialize(std::istream &is, u8 version);
395 Used only internally, because changes can't be tracked
398 MapNode & getNodeRef(s16 x, s16 y, s16 z)
400 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
401 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
402 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
403 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
405 MapNode & getNodeRef(v3s16 &p)
407 return getNodeRef(p.X, p.Y, p.Z);
411 NodeContainer *m_parent;
412 // Position in blocks on parent
415 If NULL, block is a dummy block.
416 Dummy blocks are used for caching not-found-on-disk blocks.
420 - On the client, this is used for checking whether to
421 recalculate the face cache. (Is it anymore?)
422 - On the server, this is used for telling whether the
423 block has been changed from the one on disk.
427 Used for some initial lighting stuff.
428 At least /has been/ used. 8)
432 MapBlockObjectList m_objects;
436 inline bool blockpos_over_limit(v3s16 p)
439 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
440 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
441 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
442 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
443 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
444 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
448 Returns the position of the block where the node is located
450 inline v3s16 getNodeBlockPos(v3s16 p)
452 return getContainerPos(p, MAP_BLOCKSIZE);
455 inline v2s16 getNodeSectorPos(v2s16 p)
457 return getContainerPos(p, MAP_BLOCKSIZE);
460 inline s16 getNodeBlockY(s16 y)
462 return getContainerPos(y, MAP_BLOCKSIZE);