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
56 NODEMOD_CHANGECONTENT, //param is content id
57 NODEMOD_CRACK // param is crack progression
66 enum NodeModType type;
72 NODECONTAINER_ID_MAPBLOCK,
73 NODECONTAINER_ID_MAPSECTOR,
75 NODECONTAINER_ID_MAPBLOCKCACHE,
76 NODECONTAINER_ID_VOXELMANIPULATOR,
82 virtual bool isValidPosition(v3s16 p) = 0;
83 virtual MapNode getNode(v3s16 p) = 0;
84 virtual void setNode(v3s16 p, MapNode & n) = 0;
85 virtual u16 nodeContainerId() const = 0;
88 class MapBlock : public NodeContainer
93 This used by Server's block creation stuff for not sending
94 blocks that are waiting a lighting update.
96 If true, the block needs some work by the one who set this
99 While true, nobody else should touch the block.
101 //bool is_incomplete;
106 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false):
110 is_underground(false),
111 m_mesh_expired(false),
113 //is_incomplete(false)
125 JMutexAutoLock lock(mesh_mutex);
138 virtual u16 nodeContainerId() const
140 return NODECONTAINER_ID_MAPBLOCK;
143 NodeContainer * getParent()
150 return (data == NULL);
159 bool getChangedFlag()
164 void resetChangedFlag()
169 void setChangedFlag()
174 void setMeshExpired(bool expired)
176 m_mesh_expired = expired;
179 bool getMeshExpired()
181 return m_mesh_expired;
189 v3s16 getPosRelative()
191 return m_pos * MAP_BLOCKSIZE;
194 bool getIsUnderground()
196 return is_underground;
199 void setIsUnderground(bool a_is_underground)
201 is_underground = a_is_underground;
205 core::aabbox3d<s16> getBox()
207 return core::aabbox3d<s16>(getPosRelative(),
209 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
217 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
218 data = new MapNode[l];
219 for(u32 i=0; i<l; i++){
225 bool isValidPosition(v3s16 p)
229 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
230 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
231 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
235 Regular MapNode get-setters
238 MapNode getNode(s16 x, s16 y, s16 z)
241 throw InvalidPositionException();
242 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
243 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
244 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
245 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
248 MapNode getNode(v3s16 p)
250 return getNode(p.X, p.Y, p.Z);
253 void setNode(s16 x, s16 y, s16 z, MapNode & n)
256 throw InvalidPositionException();
257 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
258 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
259 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
260 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
264 void setNode(v3s16 p, MapNode & n)
266 setNode(p.X, p.Y, p.Z, n);
270 Non-checking variants of the above
273 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
276 throw InvalidPositionException();
277 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
280 MapNode getNodeNoCheck(v3s16 p)
282 return getNodeNoCheck(p.X, p.Y, p.Z);
285 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
288 throw InvalidPositionException();
289 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
293 void setNodeNoCheck(v3s16 p, MapNode & n)
295 setNodeNoCheck(p.X, p.Y, p.Z, n);
299 These functions consult the parent container if the position
300 is not valid on this MapBlock.
302 bool isValidPositionParent(v3s16 p);
303 MapNode getNodeParent(v3s16 p);
304 void setNodeParent(v3s16 p, MapNode & n);
306 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
308 for(u16 z=0; z<d; z++)
309 for(u16 y=0; y<h; y++)
310 for(u16 x=0; x<w; x++)
311 setNode(x0+x, y0+y, z0+z, node);
314 static FastFace * makeFastFace(TileSpec tile, u8 light, v3f p,
315 v3s16 dir, v3f scale, v3f posRelative_f);
317 u8 getFaceLight(u32 daylight_factor, v3s16 p, v3s16 face_dir);
319 TileSpec getNodeTile(v3s16 p, v3s16 face_dir);
320 u8 getNodeContent(v3s16 p);
324 translate_dir: unit vector with only one of x, y or z
325 face_dir: unit vector with only one of x, y or z
327 void updateFastFaceRow(
333 core::list<FastFace*> &dest);
335 void updateMesh(u32 daylight_factor);
337 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
339 // Copies data to VoxelManipulator to getPosRelative()
340 void copyTo(VoxelManipulator &dst);
346 void serializeObjects(std::ostream &os, u8 version)
348 m_objects.serialize(os, version);
350 // If smgr!=NULL, new objects are added to the scene
351 void updateObjects(std::istream &is, u8 version,
352 scene::ISceneManager *smgr)
354 m_objects.update(is, version, smgr);
364 void addObject(MapBlockObject *object)
365 throw(ContainerFullException, AlreadyExistsException)
367 m_objects.add(object);
371 void removeObject(s16 id)
373 m_objects.remove(id);
377 MapBlockObject * getObject(s16 id)
379 return m_objects.get(id);
381 JMutexAutoLock * getObjectLock()
383 return m_objects.getLock();
385 void stepObjects(float dtime, bool server)
387 m_objects.step(dtime, server);
392 /*void wrapObject(MapBlockObject *object)
394 m_objects.wrapObject(object);
399 // origin is relative to block
400 void getObjects(v3f origin, f32 max_d,
401 core::array<DistanceSortedObject> &dest)
403 m_objects.getObjects(origin, max_d, dest);
406 /*void getPseudoObjects(v3f origin, f32 max_d,
407 core::array<DistanceSortedObject> &dest);*/
411 return m_objects.getCount();
415 Methods for setting temporary modifications to nodes for
418 void setTempMod(v3s16 p, NodeMod mod)
420 m_temp_mods[p] = mod;
422 void clearTempMod(v3s16 p)
424 if(m_temp_mods.find(p))
425 m_temp_mods.remove(p);
436 // Doesn't write version by itself
437 void serialize(std::ostream &os, u8 version);
439 void deSerialize(std::istream &is, u8 version);
444 Used only internally, because changes can't be tracked
447 MapNode & getNodeRef(s16 x, s16 y, s16 z)
449 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
450 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
451 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
452 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
454 MapNode & getNodeRef(v3s16 &p)
456 return getNodeRef(p.X, p.Y, p.Z);
460 NodeContainer *m_parent;
461 // Position in blocks on parent
464 If NULL, block is a dummy block.
465 Dummy blocks are used for caching not-found-on-disk blocks.
469 - On the client, this is used for checking whether to
470 recalculate the face cache. (Is it anymore?)
471 - On the server, this is used for telling whether the
472 block has been changed from the one on disk.
476 Used for some initial lighting stuff.
477 At least /has been/ used. 8)
483 MapBlockObjectList m_objects;
485 // Temporary modifications to nodes
486 // These are only used when drawing
487 core::map<v3s16, NodeMod> m_temp_mods;
490 inline bool blockpos_over_limit(v3s16 p)
493 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
494 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
495 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
496 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
497 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
498 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
502 Returns the position of the block where the node is located
504 inline v3s16 getNodeBlockPos(v3s16 p)
506 return getContainerPos(p, MAP_BLOCKSIZE);
509 inline v2s16 getNodeSectorPos(v2s16 p)
511 return getContainerPos(p, MAP_BLOCKSIZE);
514 inline s16 getNodeBlockY(s16 y)
516 return getContainerPos(y, MAP_BLOCKSIZE);