]> git.lizzy.rs Git - minetest.git/blob - src/mapblock.h
74666eb486de7539e9eecdf1e18eff5fab57ddc5
[minetest.git] / src / mapblock.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef MAPBLOCK_HEADER
21 #define MAPBLOCK_HEADER
22
23 #include <jmutex.h>
24 #include <jmutexautolock.h>
25 #include <exception>
26 #include "debug.h"
27 #include "common_irrlicht.h"
28 #include "mapnode.h"
29 #include "exceptions.h"
30 #include "serialization.h"
31 #include "constants.h"
32 #include "mapblockobject.h"
33 #include "voxel.h"
34 #include "nodemetadata.h"
35 #include "staticobject.h"
36
37 #define BLOCK_TIMESTAMP_UNDEFINED 0xffffffff
38
39 // Named by looking towards z+
40 enum{
41         FACE_BACK=0,
42         FACE_TOP,
43         FACE_RIGHT,
44         FACE_FRONT,
45         FACE_BOTTOM,
46         FACE_LEFT
47 };
48
49 struct FastFace
50 {
51         TileSpec tile;
52         video::S3DVertex vertices[4]; // Precalculated vertices
53 };
54
55 enum NodeModType
56 {
57         NODEMOD_NONE,
58         NODEMOD_CHANGECONTENT, //param is content id
59         NODEMOD_CRACK // param is crack progression
60 };
61
62 struct NodeMod
63 {
64         NodeMod(enum NodeModType a_type=NODEMOD_NONE, u16 a_param=0)
65         {
66                 type = a_type;
67                 param = a_param;
68         }
69         bool operator==(const NodeMod &other)
70         {
71                 return (type == other.type && param == other.param);
72         }
73         enum NodeModType type;
74         u16 param;
75 };
76
77 class NodeModMap
78 {
79 public:
80         /*
81                 returns true if the mod was different last time
82         */
83         bool set(v3s16 p, const NodeMod &mod)
84         {
85                 // See if old is different, cancel if it is not different.
86                 core::map<v3s16, NodeMod>::Node *n = m_mods.find(p);
87                 if(n)
88                 {
89                         NodeMod old = n->getValue();
90                         if(old == mod)
91                                 return false;
92
93                         n->setValue(mod);
94                 }
95                 else
96                 {
97                         m_mods.insert(p, mod);
98                 }
99                 
100                 return true;
101         }
102         // Returns true if there was one
103         bool get(v3s16 p, NodeMod *mod)
104         {
105                 core::map<v3s16, NodeMod>::Node *n;
106                 n = m_mods.find(p);
107                 if(n == NULL)
108                         return false;
109                 if(mod)
110                         *mod = n->getValue();
111                 return true;
112         }
113         bool clear(v3s16 p)
114         {
115                 if(m_mods.find(p))
116                 {
117                         m_mods.remove(p);
118                         return true;
119                 }
120                 return false;
121         }
122         bool clear()
123         {
124                 if(m_mods.size() == 0)
125                         return false;
126                 m_mods.clear();
127                 return true;
128         }
129         void copy(NodeModMap &dest)
130         {
131                 dest.m_mods.clear();
132
133                 for(core::map<v3s16, NodeMod>::Iterator
134                                 i = m_mods.getIterator();
135                                 i.atEnd() == false; i++)
136                 {
137                         dest.m_mods.insert(i.getNode()->getKey(), i.getNode()->getValue());
138                 }
139         }
140
141 private:
142         core::map<v3s16, NodeMod> m_mods;
143 };
144
145 enum
146 {
147         NODECONTAINER_ID_MAPBLOCK,
148         NODECONTAINER_ID_MAPSECTOR,
149         NODECONTAINER_ID_MAP,
150         NODECONTAINER_ID_MAPBLOCKCACHE,
151         NODECONTAINER_ID_VOXELMANIPULATOR,
152 };
153
154 class NodeContainer
155 {
156 public:
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;
161
162         MapNode getNodeNoEx(v3s16 p)
163         {
164                 try{
165                         return getNode(p);
166                 }
167                 catch(InvalidPositionException &e){
168                         return MapNode(CONTENT_IGNORE);
169                 }
170         }
171 };
172
173 /*
174         Mesh making stuff
175 */
176
177 class MapBlock;
178
179 #ifndef SERVER
180
181 struct MeshMakeData
182 {
183         u32 m_daynight_ratio;
184         NodeModMap m_temp_mods;
185         VoxelManipulator m_vmanip;
186         v3s16 m_blockpos;
187         
188         /*
189                 Copy central data directly from block, and other data from
190                 parent of block.
191         */
192         void fill(u32 daynight_ratio, MapBlock *block);
193 };
194
195 scene::SMesh* makeMapBlockMesh(MeshMakeData *data);
196
197 #endif
198
199 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
200                 v3s16 face_dir);
201
202 /*
203         MapBlock itself
204 */
205
206 class MapBlock : public NodeContainer
207 {
208 public:
209         MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
210         ~MapBlock();
211         
212         virtual u16 nodeContainerId() const
213         {
214                 return NODECONTAINER_ID_MAPBLOCK;
215         }
216         
217         NodeContainer * getParent()
218         {
219                 return m_parent;
220         }
221
222         void reallocate()
223         {
224                 if(data != NULL)
225                         delete[] data;
226                 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
227                 data = new MapNode[l];
228                 for(u32 i=0; i<l; i++){
229                         data[i] = MapNode();
230                 }
231                 setChangedFlag();
232         }
233
234         /*
235                 Flags
236         */
237
238         bool isDummy()
239         {
240                 return (data == NULL);
241         }
242         void unDummify()
243         {
244                 assert(isDummy());
245                 reallocate();
246         }
247         
248         /*
249                 This is called internally or externally after the block is
250                 modified, so that the block is saved and possibly not deleted from
251                 memory.
252         */
253         void setChangedFlag()
254         {
255                 changed = true;
256         }
257         void resetChangedFlag()
258         {
259                 changed = false;
260         }
261         bool getChangedFlag()
262         {
263                 return changed;
264         }
265
266         bool getIsUnderground()
267         {
268                 return is_underground;
269         }
270         void setIsUnderground(bool a_is_underground)
271         {
272                 is_underground = a_is_underground;
273                 setChangedFlag();
274         }
275
276 #ifndef SERVER
277         void setMeshExpired(bool expired)
278         {
279                 m_mesh_expired = expired;
280         }
281         
282         bool getMeshExpired()
283         {
284                 return m_mesh_expired;
285         }
286 #endif
287
288         void setLightingExpired(bool expired)
289         {
290                 m_lighting_expired = expired;
291                 setChangedFlag();
292         }
293         bool getLightingExpired()
294         {
295                 return m_lighting_expired;
296         }
297
298         /*bool isFullyGenerated()
299         {
300                 return !m_not_fully_generated;
301         }
302         void setFullyGenerated(bool b)
303         {
304                 setChangedFlag();
305                 m_not_fully_generated = !b;
306         }*/
307
308         bool isValid()
309         {
310                 if(m_lighting_expired)
311                         return false;
312                 if(data == NULL)
313                         return false;
314                 return true;
315         }
316
317         /*
318                 Position stuff
319         */
320
321         v3s16 getPos()
322         {
323                 return m_pos;
324         }
325                 
326         v3s16 getPosRelative()
327         {
328                 return m_pos * MAP_BLOCKSIZE;
329         }
330                 
331         core::aabbox3d<s16> getBox()
332         {
333                 return core::aabbox3d<s16>(getPosRelative(),
334                                 getPosRelative()
335                                 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
336                                 - v3s16(1,1,1));
337         }
338
339         /*
340                 Regular MapNode get-setters
341         */
342         
343         bool isValidPosition(v3s16 p)
344         {
345                 if(data == NULL)
346                         return false;
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);
350         }
351
352         MapNode getNode(s16 x, s16 y, s16 z)
353         {
354                 if(data == NULL)
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];
360         }
361         
362         MapNode getNode(v3s16 p)
363         {
364                 return getNode(p.X, p.Y, p.Z);
365         }
366         
367         MapNode getNodeNoEx(v3s16 p)
368         {
369                 try{
370                         return getNode(p.X, p.Y, p.Z);
371                 }catch(InvalidPositionException &e){
372                         return MapNode(CONTENT_IGNORE);
373                 }
374         }
375         
376         void setNode(s16 x, s16 y, s16 z, MapNode & n)
377         {
378                 if(data == NULL)
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;
384                 setChangedFlag();
385         }
386         
387         void setNode(v3s16 p, MapNode & n)
388         {
389                 setNode(p.X, p.Y, p.Z, n);
390         }
391
392         /*
393                 Non-checking variants of the above
394         */
395
396         MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
397         {
398                 if(data == NULL)
399                         throw InvalidPositionException();
400                 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
401         }
402         
403         MapNode getNodeNoCheck(v3s16 p)
404         {
405                 return getNodeNoCheck(p.X, p.Y, p.Z);
406         }
407         
408         void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
409         {
410                 if(data == NULL)
411                         throw InvalidPositionException();
412                 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
413                 setChangedFlag();
414         }
415         
416         void setNodeNoCheck(v3s16 p, MapNode & n)
417         {
418                 setNodeNoCheck(p.X, p.Y, p.Z, n);
419         }
420
421         /*
422                 These functions consult the parent container if the position
423                 is not valid on this MapBlock.
424         */
425         bool isValidPositionParent(v3s16 p);
426         MapNode getNodeParent(v3s16 p);
427         void setNodeParent(v3s16 p, MapNode & n);
428         MapNode getNodeParentNoEx(v3s16 p);
429
430         void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
431         {
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);
436         }
437
438         /*
439                 Graphics-related methods
440         */
441         
442         /*// A quick version with nodes passed as parameters
443         u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
444                         v3s16 face_dir);*/
445         /*// A more convenient version
446         u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
447         {
448                 return getFaceLight(daynight_ratio,
449                                 getNodeParentNoEx(p),
450                                 getNodeParentNoEx(p + face_dir),
451                                 face_dir);
452         }*/
453         u8 getFaceLight2(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
454         {
455                 return getFaceLight(daynight_ratio,
456                                 getNodeParentNoEx(p),
457                                 getNodeParentNoEx(p + face_dir),
458                                 face_dir);
459         }
460         
461 #ifndef SERVER // Only on client
462
463 #if 1
464         /*
465                 Thread-safely updates the whole mesh of the mapblock.
466                 NOTE: Prefer generating the mesh separately and then using
467                 replaceMesh().
468         */
469         void updateMesh(u32 daynight_ratio);
470 #endif
471         // Replace the mesh with a new one
472         void replaceMesh(scene::SMesh *mesh_new);
473 #endif
474         
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);
479         
480         // Copies data to VoxelManipulator to getPosRelative()
481         void copyTo(VoxelManipulator &dst);
482         // Copies data from VoxelManipulator getPosRelative()
483         void copyFrom(VoxelManipulator &dst);
484
485         /*
486                 MapBlockObject stuff
487                 DEPRECATED
488         */
489         
490         void serializeObjects(std::ostream &os, u8 version)
491         {
492                 m_objects.serialize(os, version);
493         }
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)
497         {
498                 m_objects.update(is, version, smgr, daynight_ratio);
499
500                 setChangedFlag();
501         }
502         void clearObjects()
503         {
504                 m_objects.clear();
505
506                 setChangedFlag();
507         }
508         void addObject(MapBlockObject *object)
509                         throw(ContainerFullException, AlreadyExistsException)
510         {
511                 m_objects.add(object);
512
513                 setChangedFlag();
514         }
515         void removeObject(s16 id)
516         {
517                 m_objects.remove(id);
518
519                 setChangedFlag();
520         }
521         MapBlockObject * getObject(s16 id)
522         {
523                 return m_objects.get(id);
524         }
525         JMutexAutoLock * getObjectLock()
526         {
527                 return m_objects.getLock();
528         }
529
530         /*
531                 Moves objects, deletes objects and spawns new objects
532         */
533         void stepObjects(float dtime, bool server, u32 daynight_ratio);
534
535         // origin is relative to block
536         void getObjects(v3f origin, f32 max_d,
537                         core::array<DistanceSortedObject> &dest)
538         {
539                 m_objects.getObjects(origin, max_d, dest);
540         }
541
542         s32 getObjectCount()
543         {
544                 return m_objects.getCount();
545         }
546
547 #ifndef SERVER // Only on client
548         /*
549                 Methods for setting temporary modifications to nodes for
550                 drawing
551
552                 returns true if the mod was different last time
553         */
554         bool setTempMod(v3s16 p, const NodeMod &mod)
555         {
556                 /*dstream<<"setTempMod called on block"
557                                 <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
558                                 <<", mod.type="<<mod.type
559                                 <<", mod.param="<<mod.param
560                                 <<std::endl;*/
561                 JMutexAutoLock lock(m_temp_mods_mutex);
562
563                 return m_temp_mods.set(p, mod);
564         }
565         // Returns true if there was one
566         bool getTempMod(v3s16 p, NodeMod *mod)
567         {
568                 JMutexAutoLock lock(m_temp_mods_mutex);
569
570                 return m_temp_mods.get(p, mod);
571         }
572         bool clearTempMod(v3s16 p)
573         {
574                 JMutexAutoLock lock(m_temp_mods_mutex);
575
576                 return m_temp_mods.clear(p);
577         }
578         bool clearTempMods()
579         {
580                 JMutexAutoLock lock(m_temp_mods_mutex);
581                 
582                 return m_temp_mods.clear();
583         }
584         void copyTempMods(NodeModMap &dst)
585         {
586                 JMutexAutoLock lock(m_temp_mods_mutex);
587                 m_temp_mods.copy(dst);
588         }
589 #endif
590
591         /*
592                 Update day-night lighting difference flag.
593                 
594                 Sets m_day_night_differs to appropriate value.
595                 
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().
600         */
601         void updateDayNightDiff();
602
603         bool dayNightDiffed()
604         {
605                 return m_day_night_differs;
606         }
607
608         /*
609                 Miscellaneous stuff
610         */
611         
612         /*
613                 Tries to measure ground level.
614                 Return value:
615                         -1 = only air
616                         -2 = only ground
617                         -3 = random fail
618                         0...MAP_BLOCKSIZE-1 = ground level
619         */
620         s16 getGroundLevel(v2s16 p2d);
621
622         /*
623                 Timestamp (see m_timestamp)
624                 NOTE: BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
625         */
626         void setTimestamp(u32 time)
627         {
628                 m_timestamp = time;
629                 setChangedFlag();
630         }
631         void setTimestampNoChangedFlag(u32 time)
632         {
633                 m_timestamp = time;
634         }
635         u32 getTimestamp()
636         {
637                 return m_timestamp;
638         }
639
640         /*
641                 Serialization
642         */
643         
644         // These don't write or read version by itself
645         void serialize(std::ostream &os, u8 version);
646         void deSerialize(std::istream &is, u8 version);
647         // Used after the basic ones when writing on disk (serverside)
648         void serializeDiskExtra(std::ostream &os, u8 version);
649         void deSerializeDiskExtra(std::istream &is, u8 version);
650
651 private:
652         /*
653                 Private methods
654         */
655
656         /*
657                 Used only internally, because changes can't be tracked
658         */
659
660         MapNode & getNodeRef(s16 x, s16 y, s16 z)
661         {
662                 if(data == NULL)
663                         throw InvalidPositionException();
664                 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
665                 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
666                 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
667                 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
668         }
669         MapNode & getNodeRef(v3s16 &p)
670         {
671                 return getNodeRef(p.X, p.Y, p.Z);
672         }
673
674 public:
675         /*
676                 Public member variables
677         */
678
679 #ifndef SERVER // Only on client
680         scene::SMesh *mesh;
681         JMutex mesh_mutex;
682 #endif
683         
684         NodeMetadataList m_node_metadata;
685         StaticObjectList m_static_objects;
686         
687 private:
688         /*
689                 Private member variables
690         */
691
692         // NOTE: Lots of things rely on this being the Map
693         NodeContainer *m_parent;
694         // Position in blocks on parent
695         v3s16 m_pos;
696         
697         /*
698                 If NULL, block is a dummy block.
699                 Dummy blocks are used for caching not-found-on-disk blocks.
700         */
701         MapNode * data;
702
703         /*
704                 - On the server, this is used for telling whether the
705                   block has been changed from the one on disk.
706                 - On the client, this is used for nothing.
707         */
708         bool changed;
709
710         /*
711                 When propagating sunlight and the above block doesn't exist,
712                 sunlight is assumed if this is false.
713
714                 In practice this is set to true if the block is completely
715                 undeground with nothing visible above the ground except
716                 caves.
717         */
718         bool is_underground;
719
720         /*
721                 Set to true if changes has been made that make the old lighting
722                 values wrong but the lighting hasn't been actually updated.
723
724                 If this is false, lighting is exactly right.
725                 If this is true, lighting might be wrong or right.
726         */
727         bool m_lighting_expired;
728         
729         // Whether day and night lighting differs
730         bool m_day_night_differs;
731         
732         // DEPRECATED
733         MapBlockObjectList m_objects;
734
735 #ifndef SERVER // Only on client
736         /*
737                 Set to true if the mesh has been ordered to be updated
738                 sometime in the background.
739                 In practice this is set when the day/night lighting switches.
740         */
741         bool m_mesh_expired;
742         
743         // Temporary modifications to nodes
744         // These are only used when drawing
745         NodeModMap m_temp_mods;
746         JMutex m_temp_mods_mutex;
747 #endif
748         
749         /*
750                 When block is removed from active blocks, this is set to gametime.
751                 Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
752         */
753         u32 m_timestamp;
754 };
755
756 inline bool blockpos_over_limit(v3s16 p)
757 {
758         return
759           (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
760         || p.X >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
761         || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
762         || p.Y >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
763         || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
764         || p.Z >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
765 }
766
767 /*
768         Returns the position of the block where the node is located
769 */
770 inline v3s16 getNodeBlockPos(v3s16 p)
771 {
772         return getContainerPos(p, MAP_BLOCKSIZE);
773 }
774
775 inline v2s16 getNodeSectorPos(v2s16 p)
776 {
777         return getContainerPos(p, MAP_BLOCKSIZE);
778 }
779
780 inline s16 getNodeBlockY(s16 y)
781 {
782         return getContainerPos(y, MAP_BLOCKSIZE);
783 }
784
785 #endif
786