2 (c) 2010 Perttu Ahola <celeron55@gmail.com>
5 #ifndef MAPBLOCK_HEADER
6 #define MAPBLOCK_HEADER
9 #include <jmutexautolock.h>
12 #include "common_irrlicht.h"
14 #include "exceptions.h"
15 #include "serialization.h"
16 #include "constants.h"
17 #include "mapblockobject.h"
19 #define MAP_BLOCKSIZE 16
21 // Named by looking towards z+
34 video::S3DVertex vertices[4]; // Precalculated vertices
39 NODECONTAINER_ID_MAPBLOCK,
40 NODECONTAINER_ID_MAPSECTOR,
42 NODECONTAINER_ID_MAPBLOCKCACHE,
43 NODECONTAINER_ID_VOXELMANIPULATOR,
49 virtual bool isValidPosition(v3s16 p) = 0;
50 virtual MapNode getNode(v3s16 p) = 0;
51 virtual void setNode(v3s16 p, MapNode & n) = 0;
52 virtual u16 nodeContainerId() const = 0;
55 class MapBlock : public NodeContainer
59 NodeContainer *m_parent;
60 // Position in blocks on parent
63 If NULL, block is a dummy block.
64 Dummy blocks are used for caching not-found-on-disk blocks.
68 - On the client, this is used for checking whether to
69 recalculate the face cache. (Is it anymore?)
70 - On the server, this is used for telling whether the
71 block has been changed from the one on disk.
75 Used for some initial lighting stuff.
76 At least /has been/ used. 8)
80 MapBlockObjectList m_objects;
85 This used by Server's block creation stuff for not sending
86 blocks that are waiting a lighting update.
88 If true, the block needs some work by the one who set this
91 While true, nobody else should touch the block.
98 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false):
102 is_underground(false),
104 //is_incomplete(false)
116 JMutexAutoLock lock(mesh_mutex);
129 virtual u16 nodeContainerId() const
131 return NODECONTAINER_ID_MAPBLOCK;
134 NodeContainer * getParent()
141 return (data == NULL);
150 bool getChangedFlag()
155 void resetChangedFlag()
160 void setChangedFlag()
170 v3s16 getPosRelative()
172 return m_pos * MAP_BLOCKSIZE;
175 bool getIsUnderground()
177 return is_underground;
180 void setIsUnderground(bool a_is_underground)
182 is_underground = a_is_underground;
186 core::aabbox3d<s16> getBox()
188 return core::aabbox3d<s16>(getPosRelative(),
190 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
198 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
199 data = new MapNode[l];
200 for(u32 i=0; i<l; i++){
206 bool isValidPosition(v3s16 p)
210 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
211 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
212 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
216 Regular MapNode get-setters
219 MapNode getNode(s16 x, s16 y, s16 z)
222 throw InvalidPositionException();
223 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
224 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
225 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
226 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
229 MapNode getNode(v3s16 p)
231 return getNode(p.X, p.Y, p.Z);
234 void setNode(s16 x, s16 y, s16 z, MapNode & n)
237 throw InvalidPositionException();
238 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
239 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
240 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
241 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
245 void setNode(v3s16 p, MapNode & n)
247 setNode(p.X, p.Y, p.Z, n);
251 Non-checking variants of the above
254 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
257 throw InvalidPositionException();
258 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
261 MapNode getNodeNoCheck(v3s16 p)
263 return getNodeNoCheck(p.X, p.Y, p.Z);
266 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
269 throw InvalidPositionException();
270 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
274 void setNodeNoCheck(v3s16 p, MapNode & n)
276 setNodeNoCheck(p.X, p.Y, p.Z, n);
280 These functions consult the parent container if the position
281 is not valid on this MapBlock.
283 bool isValidPositionParent(v3s16 p);
284 MapNode getNodeParent(v3s16 p);
285 void setNodeParent(v3s16 p, MapNode & n);
287 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
289 for(u16 z=0; z<d; z++)
290 for(u16 y=0; y<h; y++)
291 for(u16 x=0; x<w; x++)
292 setNode(x0+x, y0+y, z0+z, node);
295 static FastFace * makeFastFace(u8 material, u8 light, v3f p,
296 v3f dir, v3f scale, v3f posRelative_f);
298 u8 getFaceLight(v3s16 p, v3s16 face_dir);
301 Gets node material from any place relative to block.
302 Returns MATERIAL_AIR if doesn't exist.
304 u8 getNodeMaterial(v3s16 p);
308 translate_dir: unit vector with only one of x, y or z
309 face_dir: unit vector with only one of x, y or z
311 void updateFastFaceRow(v3s16 startpos,
315 core::list<FastFace*> &dest);
319 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
321 // Doesn't write version by itself
322 void serialize(std::ostream &os, u8 version);
324 void deSerialize(std::istream &is, u8 version);
326 void serializeObjects(std::ostream &os, u8 version)
328 m_objects.serialize(os, version);
330 // If smgr!=NULL, new objects are added to the scene
331 void updateObjects(std::istream &is, u8 version,
332 scene::ISceneManager *smgr)
334 m_objects.update(is, version, smgr);
344 void addObject(MapBlockObject *object)
345 throw(ContainerFullException, AlreadyExistsException)
347 m_objects.add(object);
351 void removeObject(s16 id)
353 m_objects.remove(id);
357 MapBlockObject * getObject(s16 id)
359 return m_objects.get(id);
361 JMutexAutoLock * getObjectLock()
363 return m_objects.getLock();
365 void stepObjects(float dtime, bool server)
367 m_objects.step(dtime, server);
372 /*void wrapObject(MapBlockObject *object)
374 m_objects.wrapObject(object);
379 // origin is relative to block
380 void getObjects(v3f origin, f32 max_d,
381 core::array<DistanceSortedObject> &dest)
383 m_objects.getObjects(origin, max_d, dest);
388 return m_objects.getCount();
394 Used only internally, because changes can't be tracked
397 MapNode & getNodeRef(s16 x, s16 y, s16 z)
399 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
400 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
401 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
402 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
404 MapNode & getNodeRef(v3s16 &p)
406 return getNodeRef(p.X, p.Y, p.Z);
410 inline bool blockpos_over_limit(v3s16 p)
413 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
414 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
415 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
416 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
417 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
418 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
422 Returns the position of the block where the node is located
424 inline v3s16 getNodeBlockPos(v3s16 p)
426 return getContainerPos(p, MAP_BLOCKSIZE);
429 inline v2s16 getNodeSectorPos(v2s16 p)
431 return getContainerPos(p, MAP_BLOCKSIZE);
434 inline s16 getNodeBlockY(s16 y)
436 return getContainerPos(y, MAP_BLOCKSIZE);