]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapblock.h
* better player graphics
[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
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         u32 getTimestamp()
632         {
633                 return m_timestamp;
634         }
635
636         /*
637                 Serialization
638         */
639         
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);
646
647 private:
648         /*
649                 Private methods
650         */
651
652         /*
653                 Used only internally, because changes can't be tracked
654         */
655
656         MapNode & getNodeRef(s16 x, s16 y, s16 z)
657         {
658                 if(data == NULL)
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];
664         }
665         MapNode & getNodeRef(v3s16 &p)
666         {
667                 return getNodeRef(p.X, p.Y, p.Z);
668         }
669
670 public:
671         /*
672                 Public member variables
673         */
674
675 #ifndef SERVER // Only on client
676         scene::SMesh *mesh;
677         JMutex mesh_mutex;
678 #endif
679         
680         NodeMetadataList m_node_metadata;
681         StaticObjectList m_static_objects;
682         
683 private:
684         /*
685                 Private member variables
686         */
687
688         // NOTE: Lots of things rely on this being the Map
689         NodeContainer *m_parent;
690         // Position in blocks on parent
691         v3s16 m_pos;
692         
693         /*
694                 If NULL, block is a dummy block.
695                 Dummy blocks are used for caching not-found-on-disk blocks.
696         */
697         MapNode * data;
698
699         /*
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.
703         */
704         bool changed;
705
706         /*
707                 When propagating sunlight and the above block doesn't exist,
708                 sunlight is assumed if this is false.
709
710                 In practice this is set to true if the block is completely
711                 undeground with nothing visible above the ground except
712                 caves.
713         */
714         bool is_underground;
715
716         /*
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.
719
720                 If this is false, lighting is exactly right.
721                 If this is true, lighting might be wrong or right.
722         */
723         bool m_lighting_expired;
724         
725         // Whether day and night lighting differs
726         bool m_day_night_differs;
727         
728         // DEPRECATED
729         MapBlockObjectList m_objects;
730
731 #ifndef SERVER // Only on client
732         /*
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.
736         */
737         bool m_mesh_expired;
738         
739         // Temporary modifications to nodes
740         // These are only used when drawing
741         NodeModMap m_temp_mods;
742         JMutex m_temp_mods_mutex;
743 #endif
744         
745         /*
746                 When block is removed from active blocks, this is set to gametime.
747                 Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
748         */
749         u32 m_timestamp;
750 };
751
752 inline bool blockpos_over_limit(v3s16 p)
753 {
754         return
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);
761 }
762
763 /*
764         Returns the position of the block where the node is located
765 */
766 inline v3s16 getNodeBlockPos(v3s16 p)
767 {
768         return getContainerPos(p, MAP_BLOCKSIZE);
769 }
770
771 inline v2s16 getNodeSectorPos(v2s16 p)
772 {
773         return getContainerPos(p, MAP_BLOCKSIZE);
774 }
775
776 inline s16 getNodeBlockY(s16 y)
777 {
778         return getContainerPos(y, MAP_BLOCKSIZE);
779 }
780
781 #endif
782