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,
47 virtual bool isValidPosition(v3s16 p) = 0;
48 virtual MapNode getNode(v3s16 p) = 0;
49 virtual void setNode(v3s16 p, MapNode & n) = 0;
50 virtual u16 nodeContainerId() const = 0;
53 class MapBlock : public NodeContainer
57 NodeContainer *m_parent;
58 // Position in blocks on parent
61 If NULL, block is a dummy block.
62 Dummy blocks are used for caching not-found-on-disk blocks.
66 - On the client, this is used for checking whether to
67 recalculate the face cache. (Is it anymore?)
68 - On the server, this is used for telling whether the
69 block has been changed from the one on disk.
73 Used for some initial lighting stuff.
74 At least /has been/ used. 8)
78 MapBlockObjectList m_objects;
83 This used by Server's block creation stuff for not sending
84 blocks that are waiting a lighting update.
86 If true, the block needs some work by the one who set this
89 While true, nobody else should touch the block.
96 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false):
100 is_underground(false),
102 //is_incomplete(false)
114 JMutexAutoLock lock(mesh_mutex);
127 virtual u16 nodeContainerId() const
129 return NODECONTAINER_ID_MAPBLOCK;
132 NodeContainer * getParent()
139 return (data == NULL);
148 bool getChangedFlag()
153 void resetChangedFlag()
158 void setChangedFlag()
168 v3s16 getPosRelative()
170 return m_pos * MAP_BLOCKSIZE;
173 bool getIsUnderground()
175 return is_underground;
178 void setIsUnderground(bool a_is_underground)
180 is_underground = a_is_underground;
184 core::aabbox3d<s16> getBox()
186 return core::aabbox3d<s16>(getPosRelative(),
188 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
196 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
197 data = new MapNode[l];
198 for(u32 i=0; i<l; i++){
204 bool isValidPosition(v3s16 p)
208 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
209 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
210 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
214 Regular MapNode get-setters
217 MapNode getNode(s16 x, s16 y, s16 z)
220 throw InvalidPositionException();
221 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
222 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
223 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
224 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
227 MapNode getNode(v3s16 p)
229 return getNode(p.X, p.Y, p.Z);
232 void setNode(s16 x, s16 y, s16 z, MapNode & n)
235 throw InvalidPositionException();
236 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
237 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
238 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
239 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
243 void setNode(v3s16 p, MapNode & n)
245 setNode(p.X, p.Y, p.Z, n);
249 These functions consult the parent container if the position
250 is not valid on this MapBlock.
252 bool isValidPositionParent(v3s16 p);
253 MapNode getNodeParent(v3s16 p);
254 void setNodeParent(v3s16 p, MapNode & n);
256 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
258 for(u16 z=0; z<d; z++)
259 for(u16 y=0; y<h; y++)
260 for(u16 x=0; x<w; x++)
261 setNode(x0+x, y0+y, z0+z, node);
264 static FastFace * makeFastFace(u8 material, u8 light, v3f p,
265 v3f dir, v3f scale, v3f posRelative_f);
267 u8 getFaceLight(v3s16 p, v3s16 face_dir);
270 Gets node material from any place relative to block.
271 Returns MATERIAL_AIR if doesn't exist.
273 u8 getNodeMaterial(v3s16 p);
277 translate_dir: unit vector with only one of x, y or z
278 face_dir: unit vector with only one of x, y or z
280 void updateFastFaceRow(v3s16 startpos,
284 core::list<FastFace*> &dest);
288 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
290 // Doesn't write version by itself
291 void serialize(std::ostream &os, u8 version);
293 void deSerialize(std::istream &is, u8 version);
295 void serializeObjects(std::ostream &os, u8 version)
297 m_objects.serialize(os, version);
299 // If smgr!=NULL, new objects are added to the scene
300 void updateObjects(std::istream &is, u8 version,
301 scene::ISceneManager *smgr)
303 m_objects.update(is, version, smgr);
313 void addObject(MapBlockObject *object)
314 throw(ContainerFullException, AlreadyExistsException)
316 m_objects.add(object);
320 void removeObject(s16 id)
322 m_objects.remove(id);
326 MapBlockObject * getObject(s16 id)
328 return m_objects.get(id);
330 JMutexAutoLock * getObjectLock()
332 return m_objects.getLock();
334 void stepObjects(float dtime, bool server)
336 m_objects.step(dtime, server);
341 /*void wrapObject(MapBlockObject *object)
343 m_objects.wrapObject(object);
348 // origin is relative to block
349 void getObjects(v3f origin, f32 max_d,
350 core::array<DistanceSortedObject> &dest)
352 m_objects.getObjects(origin, max_d, dest);
358 Used only internally, because changes can't be tracked
361 MapNode & getNodeRef(s16 x, s16 y, s16 z)
363 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
364 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
365 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
366 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
368 MapNode & getNodeRef(v3s16 &p)
370 return getNodeRef(p.X, p.Y, p.Z);
374 inline bool blockpos_over_limit(v3s16 p)
377 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
378 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
379 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
380 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
381 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
382 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
386 Returns the position of the block where the node is located
388 inline v3s16 getNodeBlockPos(v3s16 p)
390 return getContainerPos(p, MAP_BLOCKSIZE);
393 inline v2s16 getNodeSectorPos(v2s16 p)
395 return getContainerPos(p, MAP_BLOCKSIZE);
398 inline s16 getNodeBlockY(s16 y)
400 return getContainerPos(y, MAP_BLOCKSIZE);