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
62 NodeMod(enum NodeModType a_type=NODEMOD_NONE, u16 a_param=0)
67 enum NodeModType type;
73 NODECONTAINER_ID_MAPBLOCK,
74 NODECONTAINER_ID_MAPSECTOR,
76 NODECONTAINER_ID_MAPBLOCKCACHE,
77 NODECONTAINER_ID_VOXELMANIPULATOR,
83 virtual bool isValidPosition(v3s16 p) = 0;
84 virtual MapNode getNode(v3s16 p) = 0;
85 virtual void setNode(v3s16 p, MapNode & n) = 0;
86 virtual u16 nodeContainerId() const = 0;
89 class MapBlock : public NodeContainer
92 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
95 virtual u16 nodeContainerId() const
97 return NODECONTAINER_ID_MAPBLOCK;
100 NodeContainer * getParent()
107 return (data == NULL);
116 bool getChangedFlag()
121 void resetChangedFlag()
126 void setChangedFlag()
131 void setMeshExpired(bool expired)
133 m_mesh_expired = expired;
136 bool getMeshExpired()
138 return m_mesh_expired;
146 v3s16 getPosRelative()
148 return m_pos * MAP_BLOCKSIZE;
151 bool getIsUnderground()
153 return is_underground;
156 void setIsUnderground(bool a_is_underground)
158 is_underground = a_is_underground;
162 core::aabbox3d<s16> getBox()
164 return core::aabbox3d<s16>(getPosRelative(),
166 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
174 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
175 data = new MapNode[l];
176 for(u32 i=0; i<l; i++){
182 bool isValidPosition(v3s16 p)
186 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
187 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
188 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
192 Regular MapNode get-setters
195 MapNode getNode(s16 x, s16 y, s16 z)
198 throw InvalidPositionException();
199 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
200 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
201 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
202 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
205 MapNode getNode(v3s16 p)
207 return getNode(p.X, p.Y, p.Z);
210 void setNode(s16 x, s16 y, s16 z, MapNode & n)
213 throw InvalidPositionException();
214 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
215 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
216 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
217 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
221 void setNode(v3s16 p, MapNode & n)
223 setNode(p.X, p.Y, p.Z, n);
227 Non-checking variants of the above
230 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
233 throw InvalidPositionException();
234 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
237 MapNode getNodeNoCheck(v3s16 p)
239 return getNodeNoCheck(p.X, p.Y, p.Z);
242 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
245 throw InvalidPositionException();
246 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
250 void setNodeNoCheck(v3s16 p, MapNode & n)
252 setNodeNoCheck(p.X, p.Y, p.Z, n);
256 These functions consult the parent container if the position
257 is not valid on this MapBlock.
259 bool isValidPositionParent(v3s16 p);
260 MapNode getNodeParent(v3s16 p);
261 void setNodeParent(v3s16 p, MapNode & n);
262 MapNode getNodeParentNoEx(v3s16 p);
264 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
266 for(u16 z=0; z<d; z++)
267 for(u16 y=0; y<h; y++)
268 for(u16 x=0; x<w; x++)
269 setNode(x0+x, y0+y, z0+z, node);
272 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
275 u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
277 return getFaceLight(daynight_ratio,
278 getNodeParentNoEx(p),
279 getNodeParentNoEx(p + face_dir),
285 static void makeFastFace(TileSpec tile, u8 light, v3f p,
286 v3s16 dir, v3f scale, v3f posRelative_f,
287 core::array<FastFace> &dest);
289 TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir);
290 u8 getNodeContent(v3s16 p, MapNode mn);
294 translate_dir: unit vector with only one of x, y or z
295 face_dir: unit vector with only one of x, y or z
297 void updateFastFaceRow(
306 core::array<FastFace> &dest);
308 void updateMesh(u32 daynight_ratio);
309 /*void updateMesh(s32 daynight_i);
310 // Updates all DAYNIGHT_CACHE_COUNT meshes
311 void updateMeshes(s32 first_i=0);*/
314 // See comments in mapblock.cpp
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, u32 daynight_ratio)
332 m_objects.update(is, version, smgr, daynight_ratio);
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();
365 Moves objects, deletes objects and spawns new objects
367 void stepObjects(float dtime, bool server, u32 daynight_ratio);
369 /*void wrapObject(MapBlockObject *object)
371 m_objects.wrapObject(object);
376 // origin is relative to block
377 void getObjects(v3f origin, f32 max_d,
378 core::array<DistanceSortedObject> &dest)
380 m_objects.getObjects(origin, max_d, dest);
383 /*void getPseudoObjects(v3f origin, f32 max_d,
384 core::array<DistanceSortedObject> &dest);*/
388 return m_objects.getCount();
393 Methods for setting temporary modifications to nodes for
396 void setTempMod(v3s16 p, NodeMod mod)
398 /*dstream<<"setTempMod called on block"
399 <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
400 <<", mod.type="<<mod.type
401 <<", mod.param="<<mod.param
403 JMutexAutoLock lock(m_temp_mods_mutex);
404 m_temp_mods[p] = mod;
406 // Returns true if there was one
407 bool getTempMod(v3s16 p, struct NodeMod *mod)
409 JMutexAutoLock lock(m_temp_mods_mutex);
410 core::map<v3s16, NodeMod>::Node *n;
411 n = m_temp_mods.find(p);
415 *mod = n->getValue();
418 void clearTempMod(v3s16 p)
420 JMutexAutoLock lock(m_temp_mods_mutex);
421 if(m_temp_mods.find(p))
422 m_temp_mods.remove(p);
426 JMutexAutoLock lock(m_temp_mods_mutex);
432 Day-night lighting difference
434 These methods don't care about neighboring blocks.
435 It means that to know if a block really doesn't need a mesh
436 update between day and night, the neighboring blocks have
437 to be taken into account. Use Map::dayNightDiffed().
439 void updateDayNightDiff();
441 bool dayNightDiffed()
443 return m_day_night_differs;
451 Tries to measure ground level.
456 0...MAP_BLOCKSIZE-1 = ground level
458 s16 getGroundLevel(v2s16 p2d);
464 // Doesn't write version by itself
465 void serialize(std::ostream &os, u8 version);
467 void deSerialize(std::istream &is, u8 version);
470 Public member variables
474 //scene::SMesh *mesh[DAYNIGHT_CACHE_COUNT];
482 Used only internally, because changes can't be tracked
485 MapNode & getNodeRef(s16 x, s16 y, s16 z)
488 throw InvalidPositionException();
489 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
490 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
491 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
492 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
494 MapNode & getNodeRef(v3s16 &p)
496 return getNodeRef(p.X, p.Y, p.Z);
500 NodeContainer *m_parent;
501 // Position in blocks on parent
504 If NULL, block is a dummy block.
505 Dummy blocks are used for caching not-found-on-disk blocks.
509 - On the client, this is used for checking whether to
510 recalculate the face cache. (Is it anymore?)
511 - On the server, this is used for telling whether the
512 block has been changed from the one on disk.
516 Used for some initial lighting stuff.
517 At least /has been/ used. 8)
518 It's probably useless now.
522 // Whether day and night lighting differs
523 bool m_day_night_differs;
525 MapBlockObjectList m_objects;
527 // Object spawning stuff
533 // Temporary modifications to nodes
534 // These are only used when drawing
535 core::map<v3s16, NodeMod> m_temp_mods;
536 JMutex m_temp_mods_mutex;
540 inline bool blockpos_over_limit(v3s16 p)
543 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
544 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
545 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
546 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
547 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
548 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
552 Returns the position of the block where the node is located
554 inline v3s16 getNodeBlockPos(v3s16 p)
556 return getContainerPos(p, MAP_BLOCKSIZE);
559 inline v2s16 getNodeSectorPos(v2s16 p)
561 return getContainerPos(p, MAP_BLOCKSIZE);
564 inline s16 getNodeBlockY(s16 y)
566 return getContainerPos(y, MAP_BLOCKSIZE);