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),
112 //is_incomplete(false)
124 JMutexAutoLock lock(mesh_mutex);
137 virtual u16 nodeContainerId() const
139 return NODECONTAINER_ID_MAPBLOCK;
142 NodeContainer * getParent()
149 return (data == NULL);
158 bool getChangedFlag()
163 void resetChangedFlag()
168 void setChangedFlag()
178 v3s16 getPosRelative()
180 return m_pos * MAP_BLOCKSIZE;
183 bool getIsUnderground()
185 return is_underground;
188 void setIsUnderground(bool a_is_underground)
190 is_underground = a_is_underground;
194 core::aabbox3d<s16> getBox()
196 return core::aabbox3d<s16>(getPosRelative(),
198 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
206 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
207 data = new MapNode[l];
208 for(u32 i=0; i<l; i++){
214 bool isValidPosition(v3s16 p)
218 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
219 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
220 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
224 Regular MapNode get-setters
227 MapNode getNode(s16 x, s16 y, s16 z)
230 throw InvalidPositionException();
231 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
232 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
233 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
234 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
237 MapNode getNode(v3s16 p)
239 return getNode(p.X, p.Y, p.Z);
242 void setNode(s16 x, s16 y, s16 z, MapNode & n)
245 throw InvalidPositionException();
246 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
247 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
248 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
249 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
253 void setNode(v3s16 p, MapNode & n)
255 setNode(p.X, p.Y, p.Z, n);
259 Non-checking variants of the above
262 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
265 throw InvalidPositionException();
266 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
269 MapNode getNodeNoCheck(v3s16 p)
271 return getNodeNoCheck(p.X, p.Y, p.Z);
274 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
277 throw InvalidPositionException();
278 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
282 void setNodeNoCheck(v3s16 p, MapNode & n)
284 setNodeNoCheck(p.X, p.Y, p.Z, n);
288 These functions consult the parent container if the position
289 is not valid on this MapBlock.
291 bool isValidPositionParent(v3s16 p);
292 MapNode getNodeParent(v3s16 p);
293 void setNodeParent(v3s16 p, MapNode & n);
295 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
297 for(u16 z=0; z<d; z++)
298 for(u16 y=0; y<h; y++)
299 for(u16 x=0; x<w; x++)
300 setNode(x0+x, y0+y, z0+z, node);
303 static FastFace * makeFastFace(TileSpec tile, u8 light, v3f p,
304 v3s16 dir, v3f scale, v3f posRelative_f);
306 u8 getFaceLight(v3s16 p, v3s16 face_dir);
308 TileSpec getNodeTile(v3s16 p, v3s16 face_dir);
309 u8 getNodeContent(v3s16 p);
313 translate_dir: unit vector with only one of x, y or z
314 face_dir: unit vector with only one of x, y or z
316 void updateFastFaceRow(v3s16 startpos,
320 core::list<FastFace*> &dest);
324 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
326 // Copies data to VoxelManipulator to getPosRelative()
327 void copyTo(VoxelManipulator &dst);
333 void serializeObjects(std::ostream &os, u8 version)
335 m_objects.serialize(os, version);
337 // If smgr!=NULL, new objects are added to the scene
338 void updateObjects(std::istream &is, u8 version,
339 scene::ISceneManager *smgr)
341 m_objects.update(is, version, smgr);
351 void addObject(MapBlockObject *object)
352 throw(ContainerFullException, AlreadyExistsException)
354 m_objects.add(object);
358 void removeObject(s16 id)
360 m_objects.remove(id);
364 MapBlockObject * getObject(s16 id)
366 return m_objects.get(id);
368 JMutexAutoLock * getObjectLock()
370 return m_objects.getLock();
372 void stepObjects(float dtime, bool server)
374 m_objects.step(dtime, server);
379 /*void wrapObject(MapBlockObject *object)
381 m_objects.wrapObject(object);
386 // origin is relative to block
387 void getObjects(v3f origin, f32 max_d,
388 core::array<DistanceSortedObject> &dest)
390 m_objects.getObjects(origin, max_d, dest);
393 /*void getPseudoObjects(v3f origin, f32 max_d,
394 core::array<DistanceSortedObject> &dest);*/
398 return m_objects.getCount();
402 Methods for setting temporary modifications to nodes for
405 void setTempMod(v3s16 p, NodeMod mod)
407 m_temp_mods[p] = mod;
409 void clearTempMod(v3s16 p)
411 if(m_temp_mods.find(p))
412 m_temp_mods.remove(p);
423 // Doesn't write version by itself
424 void serialize(std::ostream &os, u8 version);
426 void deSerialize(std::istream &is, u8 version);
431 Used only internally, because changes can't be tracked
434 MapNode & getNodeRef(s16 x, s16 y, s16 z)
436 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
437 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
438 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
439 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
441 MapNode & getNodeRef(v3s16 &p)
443 return getNodeRef(p.X, p.Y, p.Z);
447 NodeContainer *m_parent;
448 // Position in blocks on parent
451 If NULL, block is a dummy block.
452 Dummy blocks are used for caching not-found-on-disk blocks.
456 - On the client, this is used for checking whether to
457 recalculate the face cache. (Is it anymore?)
458 - On the server, this is used for telling whether the
459 block has been changed from the one on disk.
463 Used for some initial lighting stuff.
464 At least /has been/ used. 8)
468 MapBlockObjectList m_objects;
470 // Temporary modifications to nodes
471 // These are only used when drawing
472 core::map<v3s16, NodeMod> m_temp_mods;
475 inline bool blockpos_over_limit(v3s16 p)
478 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
479 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
480 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
481 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
482 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
483 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
487 Returns the position of the block where the node is located
489 inline v3s16 getNodeBlockPos(v3s16 p)
491 return getContainerPos(p, MAP_BLOCKSIZE);
494 inline v2s16 getNodeSectorPos(v2s16 p)
496 return getContainerPos(p, MAP_BLOCKSIZE);
499 inline s16 getNodeBlockY(s16 y)
501 return getContainerPos(y, MAP_BLOCKSIZE);