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"
34 #include "nodemetadata.h"
35 #include "staticobject.h"
37 #define BLOCK_TIMESTAMP_UNDEFINED 0xffffffff
39 // Named by looking towards z+
52 video::S3DVertex vertices[4]; // Precalculated vertices
58 NODEMOD_CHANGECONTENT, //param is content id
59 NODEMOD_CRACK // param is crack progression
64 NodeMod(enum NodeModType a_type=NODEMOD_NONE, u16 a_param=0)
69 bool operator==(const NodeMod &other)
71 return (type == other.type && param == other.param);
73 enum NodeModType type;
81 returns true if the mod was different last time
83 bool set(v3s16 p, const NodeMod &mod)
85 // See if old is different, cancel if it is not different.
86 core::map<v3s16, NodeMod>::Node *n = m_mods.find(p);
89 NodeMod old = n->getValue();
97 m_mods.insert(p, mod);
102 // Returns true if there was one
103 bool get(v3s16 p, NodeMod *mod)
105 core::map<v3s16, NodeMod>::Node *n;
110 *mod = n->getValue();
124 if(m_mods.size() == 0)
129 void copy(NodeModMap &dest)
133 for(core::map<v3s16, NodeMod>::Iterator
134 i = m_mods.getIterator();
135 i.atEnd() == false; i++)
137 dest.m_mods.insert(i.getNode()->getKey(), i.getNode()->getValue());
142 core::map<v3s16, NodeMod> m_mods;
147 NODECONTAINER_ID_MAPBLOCK,
148 NODECONTAINER_ID_MAPSECTOR,
149 NODECONTAINER_ID_MAP,
150 NODECONTAINER_ID_MAPBLOCKCACHE,
151 NODECONTAINER_ID_VOXELMANIPULATOR,
157 virtual bool isValidPosition(v3s16 p) = 0;
158 virtual MapNode getNode(v3s16 p) = 0;
159 virtual void setNode(v3s16 p, MapNode & n) = 0;
160 virtual u16 nodeContainerId() const = 0;
162 MapNode getNodeNoEx(v3s16 p)
167 catch(InvalidPositionException &e){
168 return MapNode(CONTENT_IGNORE);
183 u32 m_daynight_ratio;
184 NodeModMap m_temp_mods;
185 VoxelManipulator m_vmanip;
189 Copy central data directly from block, and other data from
192 void fill(u32 daynight_ratio, MapBlock *block);
195 scene::SMesh* makeMapBlockMesh(MeshMakeData *data);
199 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
206 class MapBlock : public NodeContainer
209 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
212 virtual u16 nodeContainerId() const
214 return NODECONTAINER_ID_MAPBLOCK;
217 NodeContainer * getParent()
226 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
227 data = new MapNode[l];
228 for(u32 i=0; i<l; i++){
240 return (data == NULL);
249 This is called internally or externally after the block is
250 modified, so that the block is saved and possibly not deleted from
253 void setChangedFlag()
257 void resetChangedFlag()
261 bool getChangedFlag()
266 bool getIsUnderground()
268 return is_underground;
270 void setIsUnderground(bool a_is_underground)
272 is_underground = a_is_underground;
277 void setMeshExpired(bool expired)
279 m_mesh_expired = expired;
282 bool getMeshExpired()
284 return m_mesh_expired;
288 void setLightingExpired(bool expired)
290 m_lighting_expired = expired;
293 bool getLightingExpired()
295 return m_lighting_expired;
298 /*bool isFullyGenerated()
300 return !m_not_fully_generated;
302 void setFullyGenerated(bool b)
305 m_not_fully_generated = !b;
310 if(m_lighting_expired)
326 v3s16 getPosRelative()
328 return m_pos * MAP_BLOCKSIZE;
331 core::aabbox3d<s16> getBox()
333 return core::aabbox3d<s16>(getPosRelative(),
335 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
340 Regular MapNode get-setters
343 bool isValidPosition(v3s16 p)
347 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
348 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
349 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
352 MapNode getNode(s16 x, s16 y, s16 z)
355 throw InvalidPositionException();
356 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
357 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
358 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
359 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
362 MapNode getNode(v3s16 p)
364 return getNode(p.X, p.Y, p.Z);
367 MapNode getNodeNoEx(v3s16 p)
370 return getNode(p.X, p.Y, p.Z);
371 }catch(InvalidPositionException &e){
372 return MapNode(CONTENT_IGNORE);
376 void setNode(s16 x, s16 y, s16 z, MapNode & n)
379 throw InvalidPositionException();
380 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
381 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
382 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
383 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
387 void setNode(v3s16 p, MapNode & n)
389 setNode(p.X, p.Y, p.Z, n);
393 Non-checking variants of the above
396 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
399 throw InvalidPositionException();
400 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
403 MapNode getNodeNoCheck(v3s16 p)
405 return getNodeNoCheck(p.X, p.Y, p.Z);
408 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
411 throw InvalidPositionException();
412 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
416 void setNodeNoCheck(v3s16 p, MapNode & n)
418 setNodeNoCheck(p.X, p.Y, p.Z, n);
422 These functions consult the parent container if the position
423 is not valid on this MapBlock.
425 bool isValidPositionParent(v3s16 p);
426 MapNode getNodeParent(v3s16 p);
427 void setNodeParent(v3s16 p, MapNode & n);
428 MapNode getNodeParentNoEx(v3s16 p);
430 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
432 for(u16 z=0; z<d; z++)
433 for(u16 y=0; y<h; y++)
434 for(u16 x=0; x<w; x++)
435 setNode(x0+x, y0+y, z0+z, node);
439 Graphics-related methods
442 /*// A quick version with nodes passed as parameters
443 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
445 /*// A more convenient version
446 u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
448 return getFaceLight(daynight_ratio,
449 getNodeParentNoEx(p),
450 getNodeParentNoEx(p + face_dir),
453 u8 getFaceLight2(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
455 return getFaceLight(daynight_ratio,
456 getNodeParentNoEx(p),
457 getNodeParentNoEx(p + face_dir),
461 #ifndef SERVER // Only on client
465 Thread-safely updates the whole mesh of the mapblock.
466 NOTE: Prefer generating the mesh separately and then using
469 void updateMesh(u32 daynight_ratio);
471 // Replace the mesh with a new one
472 void replaceMesh(scene::SMesh *mesh_new);
475 // See comments in mapblock.cpp
476 bool propagateSunlight(core::map<v3s16, bool> & light_sources,
477 bool remove_light=false, bool *black_air_left=NULL,
478 bool grow_grass=false);
480 // Copies data to VoxelManipulator to getPosRelative()
481 void copyTo(VoxelManipulator &dst);
482 // Copies data from VoxelManipulator getPosRelative()
483 void copyFrom(VoxelManipulator &dst);
490 void serializeObjects(std::ostream &os, u8 version)
492 m_objects.serialize(os, version);
494 // If smgr!=NULL, new objects are added to the scene
495 void updateObjects(std::istream &is, u8 version,
496 scene::ISceneManager *smgr, u32 daynight_ratio)
498 m_objects.update(is, version, smgr, daynight_ratio);
508 void addObject(MapBlockObject *object)
509 throw(ContainerFullException, AlreadyExistsException)
511 m_objects.add(object);
515 void removeObject(s16 id)
517 m_objects.remove(id);
521 MapBlockObject * getObject(s16 id)
523 return m_objects.get(id);
525 JMutexAutoLock * getObjectLock()
527 return m_objects.getLock();
531 Moves objects, deletes objects and spawns new objects
533 void stepObjects(float dtime, bool server, u32 daynight_ratio);
535 // origin is relative to block
536 void getObjects(v3f origin, f32 max_d,
537 core::array<DistanceSortedObject> &dest)
539 m_objects.getObjects(origin, max_d, dest);
544 return m_objects.getCount();
547 #ifndef SERVER // Only on client
549 Methods for setting temporary modifications to nodes for
552 returns true if the mod was different last time
554 bool setTempMod(v3s16 p, const NodeMod &mod)
556 /*dstream<<"setTempMod called on block"
557 <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
558 <<", mod.type="<<mod.type
559 <<", mod.param="<<mod.param
561 JMutexAutoLock lock(m_temp_mods_mutex);
563 return m_temp_mods.set(p, mod);
565 // Returns true if there was one
566 bool getTempMod(v3s16 p, NodeMod *mod)
568 JMutexAutoLock lock(m_temp_mods_mutex);
570 return m_temp_mods.get(p, mod);
572 bool clearTempMod(v3s16 p)
574 JMutexAutoLock lock(m_temp_mods_mutex);
576 return m_temp_mods.clear(p);
580 JMutexAutoLock lock(m_temp_mods_mutex);
582 return m_temp_mods.clear();
584 void copyTempMods(NodeModMap &dst)
586 JMutexAutoLock lock(m_temp_mods_mutex);
587 m_temp_mods.copy(dst);
592 Update day-night lighting difference flag.
594 Sets m_day_night_differs to appropriate value.
596 These methods don't care about neighboring blocks.
597 It means that to know if a block really doesn't need a mesh
598 update between day and night, the neighboring blocks have
599 to be taken into account. Use Map::dayNightDiffed().
601 void updateDayNightDiff();
603 bool dayNightDiffed()
605 return m_day_night_differs;
613 Tries to measure ground level.
618 0...MAP_BLOCKSIZE-1 = ground level
620 s16 getGroundLevel(v2s16 p2d);
623 Timestamp (see m_timestamp)
624 NOTE: BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
626 void setTimestamp(u32 time)
640 // These don't write or read version by itself
641 void serialize(std::ostream &os, u8 version);
642 void deSerialize(std::istream &is, u8 version);
643 // Used after the basic ones when writing on disk (serverside)
644 void serializeDiskExtra(std::ostream &os, u8 version);
645 void deSerializeDiskExtra(std::istream &is, u8 version);
653 Used only internally, because changes can't be tracked
656 MapNode & getNodeRef(s16 x, s16 y, s16 z)
659 throw InvalidPositionException();
660 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
661 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
662 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
663 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
665 MapNode & getNodeRef(v3s16 &p)
667 return getNodeRef(p.X, p.Y, p.Z);
672 Public member variables
675 #ifndef SERVER // Only on client
680 NodeMetadataList m_node_metadata;
681 StaticObjectList m_static_objects;
685 Private member variables
688 // NOTE: Lots of things rely on this being the Map
689 NodeContainer *m_parent;
690 // Position in blocks on parent
694 If NULL, block is a dummy block.
695 Dummy blocks are used for caching not-found-on-disk blocks.
700 - On the server, this is used for telling whether the
701 block has been changed from the one on disk.
702 - On the client, this is used for nothing.
707 When propagating sunlight and the above block doesn't exist,
708 sunlight is assumed if this is false.
710 In practice this is set to true if the block is completely
711 undeground with nothing visible above the ground except
717 Set to true if changes has been made that make the old lighting
718 values wrong but the lighting hasn't been actually updated.
720 If this is false, lighting is exactly right.
721 If this is true, lighting might be wrong or right.
723 bool m_lighting_expired;
725 // Whether day and night lighting differs
726 bool m_day_night_differs;
729 MapBlockObjectList m_objects;
731 #ifndef SERVER // Only on client
733 Set to true if the mesh has been ordered to be updated
734 sometime in the background.
735 In practice this is set when the day/night lighting switches.
739 // Temporary modifications to nodes
740 // These are only used when drawing
741 NodeModMap m_temp_mods;
742 JMutex m_temp_mods_mutex;
746 When block is removed from active blocks, this is set to gametime.
747 Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
752 inline bool blockpos_over_limit(v3s16 p)
755 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
756 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
757 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
758 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
759 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
760 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
764 Returns the position of the block where the node is located
766 inline v3s16 getNodeBlockPos(v3s16 p)
768 return getContainerPos(p, MAP_BLOCKSIZE);
771 inline v2s16 getNodeSectorPos(v2s16 p)
773 return getContainerPos(p, MAP_BLOCKSIZE);
776 inline s16 getNodeBlockY(s16 y)
778 return getContainerPos(y, MAP_BLOCKSIZE);