]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapblock.h
Faster lighting at map generation time
[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
35 // Named by looking towards z+
36 enum{
37         FACE_BACK=0,
38         FACE_TOP,
39         FACE_RIGHT,
40         FACE_FRONT,
41         FACE_BOTTOM,
42         FACE_LEFT
43 };
44
45 struct FastFace
46 {
47         TileSpec tile;
48         video::S3DVertex vertices[4]; // Precalculated vertices
49 };
50
51 enum NodeModType
52 {
53         NODEMOD_NONE,
54         NODEMOD_CHANGECONTENT, //param is content id
55         NODEMOD_CRACK // param is crack progression
56 };
57
58 struct NodeMod
59 {
60         NodeMod(enum NodeModType a_type=NODEMOD_NONE, u16 a_param=0)
61         {
62                 type = a_type;
63                 param = a_param;
64         }
65         bool operator==(const NodeMod &other)
66         {
67                 return (type == other.type && param == other.param);
68         }
69         enum NodeModType type;
70         u16 param;
71 };
72
73 enum
74 {
75         NODECONTAINER_ID_MAPBLOCK,
76         NODECONTAINER_ID_MAPSECTOR,
77         NODECONTAINER_ID_MAP,
78         NODECONTAINER_ID_MAPBLOCKCACHE,
79         NODECONTAINER_ID_VOXELMANIPULATOR,
80 };
81
82 class NodeContainer
83 {
84 public:
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;
89 };
90
91 class MapBlock : public NodeContainer
92 {
93 public:
94         MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
95         ~MapBlock();
96         
97         virtual u16 nodeContainerId() const
98         {
99                 return NODECONTAINER_ID_MAPBLOCK;
100         }
101
102         NodeContainer * getParent()
103         {
104                 return m_parent;
105         }
106
107         bool isDummy()
108         {
109                 return (data == NULL);
110         }
111
112         void unDummify()
113         {
114                 assert(isDummy());
115                 reallocate();
116         }
117         
118         bool getChangedFlag()
119         {
120                 return changed;
121         }
122
123         void resetChangedFlag()
124         {
125                 changed = false;
126         }
127
128         void setChangedFlag()
129         {
130                 changed = true;
131         }
132 #ifndef SERVER
133         void setMeshExpired(bool expired)
134         {
135                 m_mesh_expired = expired;
136         }
137         
138         bool getMeshExpired()
139         {
140                 return m_mesh_expired;
141         }
142 #endif
143         v3s16 getPos()
144         {
145                 return m_pos;
146         }
147                 
148         v3s16 getPosRelative()
149         {
150                 return m_pos * MAP_BLOCKSIZE;
151         }
152                 
153         bool getIsUnderground()
154         {
155                 return is_underground;
156         }
157
158         void setIsUnderground(bool a_is_underground)
159         {
160                 is_underground = a_is_underground;
161                 setChangedFlag();
162         }
163
164         core::aabbox3d<s16> getBox()
165         {
166                 return core::aabbox3d<s16>(getPosRelative(),
167                                 getPosRelative()
168                                 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
169                                 - v3s16(1,1,1));
170         }
171         
172         void reallocate()
173         {
174                 if(data != NULL)
175                         delete[] data;
176                 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
177                 data = new MapNode[l];
178                 for(u32 i=0; i<l; i++){
179                         data[i] = MapNode();
180                 }
181                 setChangedFlag();
182         }
183
184         bool isValidPosition(v3s16 p)
185         {
186                 if(data == NULL)
187                         return false;
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);
191         }
192
193         /*
194                 Regular MapNode get-setters
195         */
196         
197         MapNode getNode(s16 x, s16 y, s16 z)
198         {
199                 if(data == NULL)
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];
205         }
206         
207         MapNode getNode(v3s16 p)
208         {
209                 return getNode(p.X, p.Y, p.Z);
210         }
211         
212         void setNode(s16 x, s16 y, s16 z, MapNode & n)
213         {
214                 if(data == NULL)
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;
220                 setChangedFlag();
221         }
222         
223         void setNode(v3s16 p, MapNode & n)
224         {
225                 setNode(p.X, p.Y, p.Z, n);
226         }
227
228         /*
229                 Non-checking variants of the above
230         */
231
232         MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
233         {
234                 if(data == NULL)
235                         throw InvalidPositionException();
236                 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
237         }
238         
239         MapNode getNodeNoCheck(v3s16 p)
240         {
241                 return getNodeNoCheck(p.X, p.Y, p.Z);
242         }
243         
244         void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
245         {
246                 if(data == NULL)
247                         throw InvalidPositionException();
248                 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
249                 setChangedFlag();
250         }
251         
252         void setNodeNoCheck(v3s16 p, MapNode & n)
253         {
254                 setNodeNoCheck(p.X, p.Y, p.Z, n);
255         }
256
257         /*
258                 These functions consult the parent container if the position
259                 is not valid on this MapBlock.
260         */
261         bool isValidPositionParent(v3s16 p);
262         MapNode getNodeParent(v3s16 p);
263         void setNodeParent(v3s16 p, MapNode & n);
264         MapNode getNodeParentNoEx(v3s16 p);
265
266         void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
267         {
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);
272         }
273
274         u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
275                         v3s16 face_dir);
276         
277         u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
278         {
279                 return getFaceLight(daynight_ratio,
280                                 getNodeParentNoEx(p),
281                                 getNodeParentNoEx(p + face_dir),
282                                 face_dir);
283         }
284         
285 #ifndef SERVER
286         // light = 0...255
287         static void makeFastFace(TileSpec tile, u8 light, v3f p,
288                         v3s16 dir, v3f scale, v3f posRelative_f,
289                         core::array<FastFace> &dest);
290         
291         TileSpec getNodeTile(MapNode mn, v3s16 p, v3s16 face_dir);
292         u8 getNodeContent(v3s16 p, MapNode mn);
293
294         /*
295                 startpos:
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
298         */
299         void updateFastFaceRow(
300                         u32 daynight_ratio,
301                         v3f posRelative_f,
302                         v3s16 startpos,
303                         u16 length,
304                         v3s16 translate_dir,
305                         v3f translate_dir_f,
306                         v3s16 face_dir,
307                         v3f face_dir_f,
308                         core::array<FastFace> &dest);
309
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);*/
314 #endif // !SERVER
315         
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);
320         
321         // Copies data to VoxelManipulator to getPosRelative()
322         void copyTo(VoxelManipulator &dst);
323
324         /*
325                 Object stuff
326         */
327         
328         void serializeObjects(std::ostream &os, u8 version)
329         {
330                 m_objects.serialize(os, version);
331         }
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)
335         {
336                 m_objects.update(is, version, smgr, daynight_ratio);
337
338                 setChangedFlag();
339         }
340         void clearObjects()
341         {
342                 m_objects.clear();
343
344                 setChangedFlag();
345         }
346         void addObject(MapBlockObject *object)
347                         throw(ContainerFullException, AlreadyExistsException)
348         {
349                 m_objects.add(object);
350
351                 setChangedFlag();
352         }
353         void removeObject(s16 id)
354         {
355                 m_objects.remove(id);
356
357                 setChangedFlag();
358         }
359         MapBlockObject * getObject(s16 id)
360         {
361                 return m_objects.get(id);
362         }
363         JMutexAutoLock * getObjectLock()
364         {
365                 return m_objects.getLock();
366         }
367
368         /*
369                 Moves objects, deletes objects and spawns new objects
370         */
371         void stepObjects(float dtime, bool server, u32 daynight_ratio);
372
373         /*void wrapObject(MapBlockObject *object)
374         {
375                 m_objects.wrapObject(object);
376
377                 setChangedFlag();
378         }*/
379
380         // origin is relative to block
381         void getObjects(v3f origin, f32 max_d,
382                         core::array<DistanceSortedObject> &dest)
383         {
384                 m_objects.getObjects(origin, max_d, dest);
385         }
386
387         /*void getPseudoObjects(v3f origin, f32 max_d,
388                         core::array<DistanceSortedObject> &dest);*/
389
390         s32 getObjectCount()
391         {
392                 return m_objects.getCount();
393         }
394
395 #ifndef SERVER
396         /*
397                 Methods for setting temporary modifications to nodes for
398                 drawing
399
400                 returns true if the mod was different last time
401         */
402         bool setTempMod(v3s16 p, NodeMod mod)
403         {
404                 /*dstream<<"setTempMod called on block"
405                                 <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
406                                 <<", mod.type="<<mod.type
407                                 <<", mod.param="<<mod.param
408                                 <<std::endl;*/
409                 JMutexAutoLock lock(m_temp_mods_mutex);
410
411                 // See if old is different, cancel if it is not different.
412                 core::map<v3s16, NodeMod>::Node *n = m_temp_mods.find(p);
413                 if(n)
414                 {
415                         NodeMod old = n->getValue();
416                         if(old == mod)
417                                 return false;
418                 }
419
420                 m_temp_mods[p] = mod;
421                 return true;
422         }
423         // Returns true if there was one
424         bool getTempMod(v3s16 p, struct NodeMod *mod)
425         {
426                 JMutexAutoLock lock(m_temp_mods_mutex);
427                 core::map<v3s16, NodeMod>::Node *n;
428                 n = m_temp_mods.find(p);
429                 if(n == NULL)
430                         return false;
431                 if(mod)
432                         *mod = n->getValue();
433                 return true;
434         }
435         bool clearTempMod(v3s16 p)
436         {
437                 JMutexAutoLock lock(m_temp_mods_mutex);
438                 if(m_temp_mods.find(p))
439                 {
440                         m_temp_mods.remove(p);
441                         return true;
442                 }
443                 return false;
444         }
445         bool clearTempMods()
446         {
447                 JMutexAutoLock lock(m_temp_mods_mutex);
448                 if(m_temp_mods.size() == 0)
449                         return false;
450                 m_temp_mods.clear();
451                 return true;
452         }
453 #endif
454
455         /*
456                 Day-night lighting difference
457                 
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().
462         */
463         void updateDayNightDiff();
464
465         bool dayNightDiffed()
466         {
467                 return m_day_night_differs;
468         }
469
470         /*
471                 Miscellaneous stuff
472         */
473         
474         /*
475                 Tries to measure ground level.
476                 Return value:
477                         -1 = only air
478                         -2 = only ground
479                         -3 = random fail
480                         0...MAP_BLOCKSIZE-1 = ground level
481         */
482         s16 getGroundLevel(v2s16 p2d);
483
484         /*
485                 Serialization
486         */
487         
488         // Doesn't write version by itself
489         void serialize(std::ostream &os, u8 version);
490
491         void deSerialize(std::istream &is, u8 version);
492
493         /*
494                 Public member variables
495         */
496
497 #ifndef SERVER
498         //scene::SMesh *mesh[DAYNIGHT_CACHE_COUNT];
499         scene::SMesh *mesh;
500         JMutex mesh_mutex;
501 #endif
502
503 private:
504
505         /*
506                 Used only internally, because changes can't be tracked
507         */
508
509         MapNode & getNodeRef(s16 x, s16 y, s16 z)
510         {
511                 if(data == NULL)
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];
517         }
518         MapNode & getNodeRef(v3s16 &p)
519         {
520                 return getNodeRef(p.X, p.Y, p.Z);
521         }
522
523         
524         NodeContainer *m_parent;
525         // Position in blocks on parent
526         v3s16 m_pos;
527         /*
528                 If NULL, block is a dummy block.
529                 Dummy blocks are used for caching not-found-on-disk blocks.
530         */
531         MapNode * data;
532         /*
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.
537         */
538         bool changed;
539         /*
540                 Used for some initial lighting stuff.
541                 At least /has been/ used. 8)
542                 It's probably useless now.
543         */
544         bool is_underground;
545         
546         // Whether day and night lighting differs
547         bool m_day_night_differs;
548         
549         MapBlockObjectList m_objects;
550
551         // Object spawning stuff
552         float m_spawn_timer;
553         
554 #ifndef SERVER
555         bool m_mesh_expired;
556         
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;
561 #endif
562 };
563
564 inline bool blockpos_over_limit(v3s16 p)
565 {
566         return
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);
573 }
574
575 /*
576         Returns the position of the block where the node is located
577 */
578 inline v3s16 getNodeBlockPos(v3s16 p)
579 {
580         return getContainerPos(p, MAP_BLOCKSIZE);
581 }
582
583 inline v2s16 getNodeSectorPos(v2s16 p)
584 {
585         return getContainerPos(p, MAP_BLOCKSIZE);
586 }
587
588 inline s16 getNodeBlockY(s16 y)
589 {
590         return getContainerPos(y, MAP_BLOCKSIZE);
591 }
592
593 #endif
594