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
91 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
94 virtual u16 nodeContainerId() const
96 return NODECONTAINER_ID_MAPBLOCK;
99 NodeContainer * getParent()
106 return (data == NULL);
115 bool getChangedFlag()
120 void resetChangedFlag()
125 void setChangedFlag()
130 void setMeshExpired(bool expired)
132 m_mesh_expired = expired;
135 bool getMeshExpired()
137 return m_mesh_expired;
145 v3s16 getPosRelative()
147 return m_pos * MAP_BLOCKSIZE;
150 bool getIsUnderground()
152 return is_underground;
155 void setIsUnderground(bool a_is_underground)
157 is_underground = a_is_underground;
161 core::aabbox3d<s16> getBox()
163 return core::aabbox3d<s16>(getPosRelative(),
165 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
173 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
174 data = new MapNode[l];
175 for(u32 i=0; i<l; i++){
181 bool isValidPosition(v3s16 p)
185 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
186 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
187 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
191 Regular MapNode get-setters
194 MapNode getNode(s16 x, s16 y, s16 z)
197 throw InvalidPositionException();
198 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
199 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
200 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
201 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
204 MapNode getNode(v3s16 p)
206 return getNode(p.X, p.Y, p.Z);
209 void setNode(s16 x, s16 y, s16 z, MapNode & n)
212 throw InvalidPositionException();
213 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
214 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
215 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
216 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
220 void setNode(v3s16 p, MapNode & n)
222 setNode(p.X, p.Y, p.Z, n);
226 Non-checking variants of the above
229 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
232 throw InvalidPositionException();
233 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
236 MapNode getNodeNoCheck(v3s16 p)
238 return getNodeNoCheck(p.X, p.Y, p.Z);
241 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
244 throw InvalidPositionException();
245 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
249 void setNodeNoCheck(v3s16 p, MapNode & n)
251 setNodeNoCheck(p.X, p.Y, p.Z, n);
255 These functions consult the parent container if the position
256 is not valid on this MapBlock.
258 bool isValidPositionParent(v3s16 p);
259 MapNode getNodeParent(v3s16 p);
260 void setNodeParent(v3s16 p, MapNode & n);
261 MapNode getNodeParentNoEx(v3s16 p);
263 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
265 for(u16 z=0; z<d; z++)
266 for(u16 y=0; y<h; y++)
267 for(u16 x=0; x<w; x++)
268 setNode(x0+x, y0+y, z0+z, node);
271 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
274 u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
276 return getFaceLight(daynight_ratio,
277 getNodeParentNoEx(p),
278 getNodeParentNoEx(p + face_dir),
283 static void makeFastFace(TileSpec tile, u8 light, v3f p,
284 v3s16 dir, v3f scale, v3f posRelative_f,
285 core::array<FastFace> &dest);
287 TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir);
288 u8 getNodeContent(v3s16 p, MapNode mn);
292 translate_dir: unit vector with only one of x, y or z
293 face_dir: unit vector with only one of x, y or z
295 void updateFastFaceRow(
304 core::array<FastFace> &dest);
306 void updateMesh(u32 daynight_ratio);
307 /*void updateMesh(s32 daynight_i);
308 // Updates all DAYNIGHT_CACHE_COUNT meshes
309 void updateMeshes(s32 first_i=0);*/
312 bool propagateSunlight(core::map<v3s16, bool> & light_sources);
314 // Copies data to VoxelManipulator to getPosRelative()
315 void copyTo(VoxelManipulator &dst);
321 void serializeObjects(std::ostream &os, u8 version)
323 m_objects.serialize(os, version);
325 // If smgr!=NULL, new objects are added to the scene
326 void updateObjects(std::istream &is, u8 version,
327 scene::ISceneManager *smgr)
329 m_objects.update(is, version, smgr);
339 void addObject(MapBlockObject *object)
340 throw(ContainerFullException, AlreadyExistsException)
342 m_objects.add(object);
346 void removeObject(s16 id)
348 m_objects.remove(id);
352 MapBlockObject * getObject(s16 id)
354 return m_objects.get(id);
356 JMutexAutoLock * getObjectLock()
358 return m_objects.getLock();
360 void stepObjects(float dtime, bool server)
362 m_objects.step(dtime, server);
367 /*void wrapObject(MapBlockObject *object)
369 m_objects.wrapObject(object);
374 // origin is relative to block
375 void getObjects(v3f origin, f32 max_d,
376 core::array<DistanceSortedObject> &dest)
378 m_objects.getObjects(origin, max_d, dest);
381 /*void getPseudoObjects(v3f origin, f32 max_d,
382 core::array<DistanceSortedObject> &dest);*/
386 return m_objects.getCount();
391 Methods for setting temporary modifications to nodes for
394 void setTempMod(v3s16 p, NodeMod mod)
396 m_temp_mods[p] = mod;
398 void clearTempMod(v3s16 p)
400 if(m_temp_mods.find(p))
401 m_temp_mods.remove(p);
410 Day-night lighting difference
412 These methods don't care about neighboring blocks.
413 It means that to know if a block really doesn't need a mesh
414 update between day and night, the neighboring blocks have
415 to be taken into account.
417 void updateDayNightDiff();
419 bool dayNightDiffed()
421 return m_day_night_differs;
428 // Doesn't write version by itself
429 void serialize(std::ostream &os, u8 version);
431 void deSerialize(std::istream &is, u8 version);
434 Public member variables
438 //scene::SMesh *mesh[DAYNIGHT_CACHE_COUNT];
446 Used only internally, because changes can't be tracked
449 MapNode & getNodeRef(s16 x, s16 y, s16 z)
451 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
452 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
453 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
454 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
456 MapNode & getNodeRef(v3s16 &p)
458 return getNodeRef(p.X, p.Y, p.Z);
462 NodeContainer *m_parent;
463 // Position in blocks on parent
466 If NULL, block is a dummy block.
467 Dummy blocks are used for caching not-found-on-disk blocks.
471 - On the client, this is used for checking whether to
472 recalculate the face cache. (Is it anymore?)
473 - On the server, this is used for telling whether the
474 block has been changed from the one on disk.
478 Used for some initial lighting stuff.
479 At least /has been/ used. 8)
480 It's probably useless now.
484 // Whether day and night lighting differs
485 bool m_day_night_differs;
487 MapBlockObjectList m_objects;
492 // Temporary modifications to nodes
493 // These are only used when drawing
494 core::map<v3s16, NodeMod> m_temp_mods;
498 inline bool blockpos_over_limit(v3s16 p)
501 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
502 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
503 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
504 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
505 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
506 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
510 Returns the position of the block where the node is located
512 inline v3s16 getNodeBlockPos(v3s16 p)
514 return getContainerPos(p, MAP_BLOCKSIZE);
517 inline v2s16 getNodeSectorPos(v2s16 p)
519 return getContainerPos(p, MAP_BLOCKSIZE);
522 inline s16 getNodeBlockY(s16 y)
524 return getContainerPos(y, MAP_BLOCKSIZE);