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
92 //scene::SMesh *mesh[DAYNIGHT_CACHE_COUNT];
96 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
99 virtual u16 nodeContainerId() const
101 return NODECONTAINER_ID_MAPBLOCK;
104 NodeContainer * getParent()
111 return (data == NULL);
120 bool getChangedFlag()
125 void resetChangedFlag()
130 void setChangedFlag()
135 void setMeshExpired(bool expired)
137 m_mesh_expired = expired;
140 bool getMeshExpired()
142 return m_mesh_expired;
150 v3s16 getPosRelative()
152 return m_pos * MAP_BLOCKSIZE;
155 bool getIsUnderground()
157 return is_underground;
160 void setIsUnderground(bool a_is_underground)
162 is_underground = a_is_underground;
166 core::aabbox3d<s16> getBox()
168 return core::aabbox3d<s16>(getPosRelative(),
170 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
178 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
179 data = new MapNode[l];
180 for(u32 i=0; i<l; i++){
186 bool isValidPosition(v3s16 p)
190 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
191 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
192 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
196 Regular MapNode get-setters
199 MapNode getNode(s16 x, s16 y, s16 z)
202 throw InvalidPositionException();
203 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
204 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
205 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
206 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
209 MapNode getNode(v3s16 p)
211 return getNode(p.X, p.Y, p.Z);
214 void setNode(s16 x, s16 y, s16 z, MapNode & n)
217 throw InvalidPositionException();
218 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
219 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
220 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
221 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
225 void setNode(v3s16 p, MapNode & n)
227 setNode(p.X, p.Y, p.Z, n);
231 Non-checking variants of the above
234 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
237 throw InvalidPositionException();
238 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
241 MapNode getNodeNoCheck(v3s16 p)
243 return getNodeNoCheck(p.X, p.Y, p.Z);
246 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
249 throw InvalidPositionException();
250 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
254 void setNodeNoCheck(v3s16 p, MapNode & n)
256 setNodeNoCheck(p.X, p.Y, p.Z, n);
260 These functions consult the parent container if the position
261 is not valid on this MapBlock.
263 bool isValidPositionParent(v3s16 p);
264 MapNode getNodeParent(v3s16 p);
265 void setNodeParent(v3s16 p, MapNode & n);
266 MapNode getNodeParentNoEx(v3s16 p);
268 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
270 for(u16 z=0; z<d; z++)
271 for(u16 y=0; y<h; y++)
272 for(u16 x=0; x<w; x++)
273 setNode(x0+x, y0+y, z0+z, node);
276 static void makeFastFace(TileSpec tile, u8 light, v3f p,
277 v3s16 dir, v3f scale, v3f posRelative_f,
278 core::array<FastFace> &dest);
280 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
283 u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
285 return getFaceLight(daynight_ratio,
286 getNodeParentNoEx(p),
287 getNodeParentNoEx(p + face_dir),
291 TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir);
292 u8 getNodeContent(v3s16 p, MapNode mn);
296 translate_dir: unit vector with only one of x, y or z
297 face_dir: unit vector with only one of x, y or z
299 void updateFastFaceRow(
308 core::array<FastFace> &dest);
310 void updateMesh(u32 daynight_ratio);
311 /*void updateMesh(s32 daynight_i);
312 // Updates all DAYNIGHT_CACHE_COUNT meshes
313 void updateMeshes(s32 first_i=0);*/
315 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
317 // Copies data to VoxelManipulator to getPosRelative()
318 void copyTo(VoxelManipulator &dst);
324 void serializeObjects(std::ostream &os, u8 version)
326 m_objects.serialize(os, version);
328 // If smgr!=NULL, new objects are added to the scene
329 void updateObjects(std::istream &is, u8 version,
330 scene::ISceneManager *smgr)
332 m_objects.update(is, version, smgr);
342 void addObject(MapBlockObject *object)
343 throw(ContainerFullException, AlreadyExistsException)
345 m_objects.add(object);
349 void removeObject(s16 id)
351 m_objects.remove(id);
355 MapBlockObject * getObject(s16 id)
357 return m_objects.get(id);
359 JMutexAutoLock * getObjectLock()
361 return m_objects.getLock();
363 void stepObjects(float dtime, bool server)
365 m_objects.step(dtime, server);
370 /*void wrapObject(MapBlockObject *object)
372 m_objects.wrapObject(object);
377 // origin is relative to block
378 void getObjects(v3f origin, f32 max_d,
379 core::array<DistanceSortedObject> &dest)
381 m_objects.getObjects(origin, max_d, dest);
384 /*void getPseudoObjects(v3f origin, f32 max_d,
385 core::array<DistanceSortedObject> &dest);*/
389 return m_objects.getCount();
393 Methods for setting temporary modifications to nodes for
396 void setTempMod(v3s16 p, NodeMod mod)
398 m_temp_mods[p] = mod;
400 void clearTempMod(v3s16 p)
402 if(m_temp_mods.find(p))
403 m_temp_mods.remove(p);
411 Day-night lighting difference
413 These methods don't care about neighboring blocks.
414 It means that to know if a block really doesn't need a mesh
415 update between day and night, the neighboring blocks have
416 to be taken into account.
418 void updateDayNightDiff();
420 bool dayNightDiffed()
422 return m_day_night_differs;
429 // Doesn't write version by itself
430 void serialize(std::ostream &os, u8 version);
432 void deSerialize(std::istream &is, u8 version);
437 Used only internally, because changes can't be tracked
440 MapNode & getNodeRef(s16 x, s16 y, s16 z)
442 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
443 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
444 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
445 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
447 MapNode & getNodeRef(v3s16 &p)
449 return getNodeRef(p.X, p.Y, p.Z);
453 NodeContainer *m_parent;
454 // Position in blocks on parent
457 If NULL, block is a dummy block.
458 Dummy blocks are used for caching not-found-on-disk blocks.
462 - On the client, this is used for checking whether to
463 recalculate the face cache. (Is it anymore?)
464 - On the server, this is used for telling whether the
465 block has been changed from the one on disk.
469 Used for some initial lighting stuff.
470 At least /has been/ used. 8)
476 // Whether day and night lighting differs
477 bool m_day_night_differs;
479 MapBlockObjectList m_objects;
481 // Temporary modifications to nodes
482 // These are only used when drawing
483 core::map<v3s16, NodeMod> m_temp_mods;
486 inline bool blockpos_over_limit(v3s16 p)
489 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
490 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
491 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
492 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
493 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
494 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
498 Returns the position of the block where the node is located
500 inline v3s16 getNodeBlockPos(v3s16 p)
502 return getContainerPos(p, MAP_BLOCKSIZE);
505 inline v2s16 getNodeSectorPos(v2s16 p)
507 return getContainerPos(p, MAP_BLOCKSIZE);
510 inline s16 getNodeBlockY(s16 y)
512 return getContainerPos(y, MAP_BLOCKSIZE);