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 // Named by looking towards z+
48 video::S3DVertex vertices[4]; // Precalculated vertices
54 NODEMOD_CHANGECONTENT, //param is content id
55 NODEMOD_CRACK // param is crack progression
60 NodeMod(enum NodeModType a_type=NODEMOD_NONE, u16 a_param=0)
65 bool operator==(const NodeMod &other)
67 return (type == other.type && param == other.param);
69 enum NodeModType type;
75 NODECONTAINER_ID_MAPBLOCK,
76 NODECONTAINER_ID_MAPSECTOR,
78 NODECONTAINER_ID_MAPBLOCKCACHE,
79 NODECONTAINER_ID_VOXELMANIPULATOR,
85 virtual bool isValidPosition(v3s16 p) = 0;
86 virtual MapNode getNode(v3s16 p) = 0;
87 virtual void setNode(v3s16 p, MapNode & n) = 0;
88 virtual u16 nodeContainerId() const = 0;
91 class MapBlock : public NodeContainer
94 MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
97 virtual u16 nodeContainerId() const
99 return NODECONTAINER_ID_MAPBLOCK;
102 NodeContainer * getParent()
109 return (data == NULL);
118 bool getChangedFlag()
123 void resetChangedFlag()
128 void setChangedFlag()
133 void setMeshExpired(bool expired)
135 m_mesh_expired = expired;
138 bool getMeshExpired()
140 return m_mesh_expired;
148 v3s16 getPosRelative()
150 return m_pos * MAP_BLOCKSIZE;
153 bool getIsUnderground()
155 return is_underground;
158 void setIsUnderground(bool a_is_underground)
160 is_underground = a_is_underground;
164 core::aabbox3d<s16> getBox()
166 return core::aabbox3d<s16>(getPosRelative(),
168 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
176 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
177 data = new MapNode[l];
178 for(u32 i=0; i<l; i++){
184 bool isValidPosition(v3s16 p)
188 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
189 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
190 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
194 Regular MapNode get-setters
197 MapNode getNode(s16 x, s16 y, s16 z)
200 throw InvalidPositionException();
201 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
202 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
203 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
204 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
207 MapNode getNode(v3s16 p)
209 return getNode(p.X, p.Y, p.Z);
212 void setNode(s16 x, s16 y, s16 z, MapNode & n)
215 throw InvalidPositionException();
216 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
217 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
218 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
219 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
223 void setNode(v3s16 p, MapNode & n)
225 setNode(p.X, p.Y, p.Z, n);
229 Non-checking variants of the above
232 MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
235 throw InvalidPositionException();
236 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
239 MapNode getNodeNoCheck(v3s16 p)
241 return getNodeNoCheck(p.X, p.Y, p.Z);
244 void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
247 throw InvalidPositionException();
248 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
252 void setNodeNoCheck(v3s16 p, MapNode & n)
254 setNodeNoCheck(p.X, p.Y, p.Z, n);
258 These functions consult the parent container if the position
259 is not valid on this MapBlock.
261 bool isValidPositionParent(v3s16 p);
262 MapNode getNodeParent(v3s16 p);
263 void setNodeParent(v3s16 p, MapNode & n);
264 MapNode getNodeParentNoEx(v3s16 p);
266 void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
268 for(u16 z=0; z<d; z++)
269 for(u16 y=0; y<h; y++)
270 for(u16 x=0; x<w; x++)
271 setNode(x0+x, y0+y, z0+z, node);
274 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
277 u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
279 return getFaceLight(daynight_ratio,
280 getNodeParentNoEx(p),
281 getNodeParentNoEx(p + face_dir),
287 static void makeFastFace(TileSpec tile, u8 light, v3f p,
288 v3s16 dir, v3f scale, v3f posRelative_f,
289 core::array<FastFace> &dest);
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);*/
316 // See comments in mapblock.cpp
317 bool propagateSunlight(core::map<v3s16, bool> & light_sources,
318 bool remove_light=false, bool *black_air_left=NULL,
319 bool grow_grass=false);
321 // Copies data to VoxelManipulator to getPosRelative()
322 void copyTo(VoxelManipulator &dst);
328 void serializeObjects(std::ostream &os, u8 version)
330 m_objects.serialize(os, version);
332 // If smgr!=NULL, new objects are added to the scene
333 void updateObjects(std::istream &is, u8 version,
334 scene::ISceneManager *smgr, u32 daynight_ratio)
336 m_objects.update(is, version, smgr, daynight_ratio);
346 void addObject(MapBlockObject *object)
347 throw(ContainerFullException, AlreadyExistsException)
349 m_objects.add(object);
353 void removeObject(s16 id)
355 m_objects.remove(id);
359 MapBlockObject * getObject(s16 id)
361 return m_objects.get(id);
363 JMutexAutoLock * getObjectLock()
365 return m_objects.getLock();
369 Moves objects, deletes objects and spawns new objects
371 void stepObjects(float dtime, bool server, u32 daynight_ratio);
373 /*void wrapObject(MapBlockObject *object)
375 m_objects.wrapObject(object);
380 // origin is relative to block
381 void getObjects(v3f origin, f32 max_d,
382 core::array<DistanceSortedObject> &dest)
384 m_objects.getObjects(origin, max_d, dest);
387 /*void getPseudoObjects(v3f origin, f32 max_d,
388 core::array<DistanceSortedObject> &dest);*/
392 return m_objects.getCount();
397 Methods for setting temporary modifications to nodes for
400 returns true if the mod was different last time
402 bool setTempMod(v3s16 p, NodeMod mod)
404 /*dstream<<"setTempMod called on block"
405 <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
406 <<", mod.type="<<mod.type
407 <<", mod.param="<<mod.param
409 JMutexAutoLock lock(m_temp_mods_mutex);
411 // See if old is different, cancel if it is not different.
412 core::map<v3s16, NodeMod>::Node *n = m_temp_mods.find(p);
415 NodeMod old = n->getValue();
420 m_temp_mods[p] = mod;
423 // Returns true if there was one
424 bool getTempMod(v3s16 p, struct NodeMod *mod)
426 JMutexAutoLock lock(m_temp_mods_mutex);
427 core::map<v3s16, NodeMod>::Node *n;
428 n = m_temp_mods.find(p);
432 *mod = n->getValue();
435 bool clearTempMod(v3s16 p)
437 JMutexAutoLock lock(m_temp_mods_mutex);
438 if(m_temp_mods.find(p))
440 m_temp_mods.remove(p);
447 JMutexAutoLock lock(m_temp_mods_mutex);
448 if(m_temp_mods.size() == 0)
456 Day-night lighting difference
458 These methods don't care about neighboring blocks.
459 It means that to know if a block really doesn't need a mesh
460 update between day and night, the neighboring blocks have
461 to be taken into account. Use Map::dayNightDiffed().
463 void updateDayNightDiff();
465 bool dayNightDiffed()
467 return m_day_night_differs;
475 Tries to measure ground level.
480 0...MAP_BLOCKSIZE-1 = ground level
482 s16 getGroundLevel(v2s16 p2d);
488 // Doesn't write version by itself
489 void serialize(std::ostream &os, u8 version);
491 void deSerialize(std::istream &is, u8 version);
494 Public member variables
498 //scene::SMesh *mesh[DAYNIGHT_CACHE_COUNT];
506 Used only internally, because changes can't be tracked
509 MapNode & getNodeRef(s16 x, s16 y, s16 z)
512 throw InvalidPositionException();
513 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
514 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
515 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
516 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
518 MapNode & getNodeRef(v3s16 &p)
520 return getNodeRef(p.X, p.Y, p.Z);
524 NodeContainer *m_parent;
525 // Position in blocks on parent
528 If NULL, block is a dummy block.
529 Dummy blocks are used for caching not-found-on-disk blocks.
533 - On the client, this is used for checking whether to
534 recalculate the face cache. (Is it anymore?)
535 - On the server, this is used for telling whether the
536 block has been changed from the one on disk.
540 Used for some initial lighting stuff.
541 At least /has been/ used. 8)
542 It's probably useless now.
546 // Whether day and night lighting differs
547 bool m_day_night_differs;
549 MapBlockObjectList m_objects;
551 // Object spawning stuff
557 // Temporary modifications to nodes
558 // These are only used when drawing
559 core::map<v3s16, NodeMod> m_temp_mods;
560 JMutex m_temp_mods_mutex;
564 inline bool blockpos_over_limit(v3s16 p)
567 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
568 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
569 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
570 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
571 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
572 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
576 Returns the position of the block where the node is located
578 inline v3s16 getNodeBlockPos(v3s16 p)
580 return getContainerPos(p, MAP_BLOCKSIZE);
583 inline v2s16 getNodeSectorPos(v2s16 p)
585 return getContainerPos(p, MAP_BLOCKSIZE);
588 inline s16 getNodeBlockY(s16 y)
590 return getContainerPos(y, MAP_BLOCKSIZE);