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"
34 #define MAP_BLOCKSIZE 16
36 // Named by looking towards z+
49 video::S3DVertex vertices[4]; // Precalculated vertices
54 NODECONTAINER_ID_MAPBLOCK,
55 NODECONTAINER_ID_MAPSECTOR,
57 NODECONTAINER_ID_MAPBLOCKCACHE,
58 NODECONTAINER_ID_VOXELMANIPULATOR,
64 virtual bool isValidPosition(v3s16 p) = 0;
65 virtual MapNode getNode(v3s16 p) = 0;
66 virtual void setNode(v3s16 p, MapNode & n) = 0;
67 virtual u16 nodeContainerId() const = 0;
70 class MapBlock : public NodeContainer
74 NodeContainer *m_parent;
75 // Position in blocks on parent
78 If NULL, block is a dummy block.
79 Dummy blocks are used for caching not-found-on-disk blocks.
83 - On the client, this is used for checking whether to
84 recalculate the face cache. (Is it anymore?)
85 - On the server, this is used for telling whether the
86 block has been changed from the one on disk.
90 Used for some initial lighting stuff.
91 At least /has been/ used. 8)
95 MapBlockObjectList m_objects;
100 This used by Server's block creation stuff for not sending
101 blocks that are waiting a lighting update.
103 If true, the block needs some work by the one who set this
106 While true, nobody else should touch the block.
108 //bool is_incomplete;
113 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false):
117 is_underground(false),
119 //is_incomplete(false)
131 JMutexAutoLock lock(mesh_mutex);
144 virtual u16 nodeContainerId() const
146 return NODECONTAINER_ID_MAPBLOCK;
149 NodeContainer * getParent()
156 return (data == NULL);
165 bool getChangedFlag()
170 void resetChangedFlag()
175 void setChangedFlag()
185 v3s16 getPosRelative()
187 return m_pos * MAP_BLOCKSIZE;
190 bool getIsUnderground()
192 return is_underground;
195 void setIsUnderground(bool a_is_underground)
197 is_underground = a_is_underground;
201 core::aabbox3d<s16> getBox()
203 return core::aabbox3d<s16>(getPosRelative(),
205 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
213 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
214 data = new MapNode[l];
215 for(u32 i=0; i<l; i++){
221 bool isValidPosition(v3s16 p)
225 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
226 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
227 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
231 Regular MapNode get-setters
234 MapNode getNode(s16 x, s16 y, s16 z)
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 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
244 MapNode getNode(v3s16 p)
246 return getNode(p.X, p.Y, p.Z);
249 void setNode(s16 x, s16 y, s16 z, MapNode & n)
252 throw InvalidPositionException();
253 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
254 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
255 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
256 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
260 void setNode(v3s16 p, MapNode & n)
262 setNode(p.X, p.Y, p.Z, n);
266 Non-checking variants of the above
269 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
272 throw InvalidPositionException();
273 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
276 MapNode getNodeNoCheck(v3s16 p)
278 return getNodeNoCheck(p.X, p.Y, p.Z);
281 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
284 throw InvalidPositionException();
285 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
289 void setNodeNoCheck(v3s16 p, MapNode & n)
291 setNodeNoCheck(p.X, p.Y, p.Z, n);
295 These functions consult the parent container if the position
296 is not valid on this MapBlock.
298 bool isValidPositionParent(v3s16 p);
299 MapNode getNodeParent(v3s16 p);
300 void setNodeParent(v3s16 p, MapNode & n);
302 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
304 for(u16 z=0; z<d; z++)
305 for(u16 y=0; y<h; y++)
306 for(u16 x=0; x<w; x++)
307 setNode(x0+x, y0+y, z0+z, node);
310 static FastFace * makeFastFace(u8 material, u8 light, v3f p,
311 v3f dir, v3f scale, v3f posRelative_f);
313 u8 getFaceLight(v3s16 p, v3s16 face_dir);
316 Gets node material from any place relative to block.
317 Returns MATERIAL_AIR if doesn't exist.
319 u8 getNodeMaterial(v3s16 p);
323 translate_dir: unit vector with only one of x, y or z
324 face_dir: unit vector with only one of x, y or z
326 void updateFastFaceRow(v3s16 startpos,
330 core::list<FastFace*> &dest);
334 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
336 // Doesn't write version by itself
337 void serialize(std::ostream &os, u8 version);
339 void deSerialize(std::istream &is, u8 version);
341 void serializeObjects(std::ostream &os, u8 version)
343 m_objects.serialize(os, version);
345 // If smgr!=NULL, new objects are added to the scene
346 void updateObjects(std::istream &is, u8 version,
347 scene::ISceneManager *smgr)
349 m_objects.update(is, version, smgr);
359 void addObject(MapBlockObject *object)
360 throw(ContainerFullException, AlreadyExistsException)
362 m_objects.add(object);
366 void removeObject(s16 id)
368 m_objects.remove(id);
372 MapBlockObject * getObject(s16 id)
374 return m_objects.get(id);
376 JMutexAutoLock * getObjectLock()
378 return m_objects.getLock();
380 void stepObjects(float dtime, bool server)
382 m_objects.step(dtime, server);
387 /*void wrapObject(MapBlockObject *object)
389 m_objects.wrapObject(object);
394 // origin is relative to block
395 void getObjects(v3f origin, f32 max_d,
396 core::array<DistanceSortedObject> &dest)
398 m_objects.getObjects(origin, max_d, dest);
403 return m_objects.getCount();
409 Used only internally, because changes can't be tracked
412 MapNode & getNodeRef(s16 x, s16 y, s16 z)
414 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
415 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
416 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
417 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
419 MapNode & getNodeRef(v3s16 &p)
421 return getNodeRef(p.X, p.Y, p.Z);
425 inline bool blockpos_over_limit(v3s16 p)
428 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
429 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
430 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
431 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
432 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
433 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
437 Returns the position of the block where the node is located
439 inline v3s16 getNodeBlockPos(v3s16 p)
441 return getContainerPos(p, MAP_BLOCKSIZE);
444 inline v2s16 getNodeSectorPos(v2s16 p)
446 return getContainerPos(p, MAP_BLOCKSIZE);
449 inline s16 getNodeBlockY(s16 y)
451 return getContainerPos(y, MAP_BLOCKSIZE);