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