]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapblock.h
e4f93a031c4d61c72b80252f59394a30b38a95d8
[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         
320         // Copies data to VoxelManipulator to getPosRelative()
321         void copyTo(VoxelManipulator &dst);
322
323         /*
324                 Object stuff
325         */
326         
327         void serializeObjects(std::ostream &os, u8 version)
328         {
329                 m_objects.serialize(os, version);
330         }
331         // If smgr!=NULL, new objects are added to the scene
332         void updateObjects(std::istream &is, u8 version,
333                         scene::ISceneManager *smgr, u32 daynight_ratio)
334         {
335                 m_objects.update(is, version, smgr, daynight_ratio);
336
337                 setChangedFlag();
338         }
339         void clearObjects()
340         {
341                 m_objects.clear();
342
343                 setChangedFlag();
344         }
345         void addObject(MapBlockObject *object)
346                         throw(ContainerFullException, AlreadyExistsException)
347         {
348                 m_objects.add(object);
349
350                 setChangedFlag();
351         }
352         void removeObject(s16 id)
353         {
354                 m_objects.remove(id);
355
356                 setChangedFlag();
357         }
358         MapBlockObject * getObject(s16 id)
359         {
360                 return m_objects.get(id);
361         }
362         JMutexAutoLock * getObjectLock()
363         {
364                 return m_objects.getLock();
365         }
366
367         /*
368                 Moves objects, deletes objects and spawns new objects
369         */
370         void stepObjects(float dtime, bool server, u32 daynight_ratio);
371
372         /*void wrapObject(MapBlockObject *object)
373         {
374                 m_objects.wrapObject(object);
375
376                 setChangedFlag();
377         }*/
378
379         // origin is relative to block
380         void getObjects(v3f origin, f32 max_d,
381                         core::array<DistanceSortedObject> &dest)
382         {
383                 m_objects.getObjects(origin, max_d, dest);
384         }
385
386         /*void getPseudoObjects(v3f origin, f32 max_d,
387                         core::array<DistanceSortedObject> &dest);*/
388
389         s32 getObjectCount()
390         {
391                 return m_objects.getCount();
392         }
393
394 #ifndef SERVER
395         /*
396                 Methods for setting temporary modifications to nodes for
397                 drawing
398
399                 returns true if the mod was different last time
400         */
401         bool setTempMod(v3s16 p, NodeMod mod)
402         {
403                 /*dstream<<"setTempMod called on block"
404                                 <<" ("<<p.X<<","<<p.Y<<","<<p.Z<<")"
405                                 <<", mod.type="<<mod.type
406                                 <<", mod.param="<<mod.param
407                                 <<std::endl;*/
408                 JMutexAutoLock lock(m_temp_mods_mutex);
409
410                 // See if old is different, cancel if it is not different.
411                 core::map<v3s16, NodeMod>::Node *n = m_temp_mods.find(p);
412                 if(n)
413                 {
414                         NodeMod old = n->getValue();
415                         if(old == mod)
416                                 return false;
417                 }
418
419                 m_temp_mods[p] = mod;
420                 return true;
421         }
422         // Returns true if there was one
423         bool getTempMod(v3s16 p, struct NodeMod *mod)
424         {
425                 JMutexAutoLock lock(m_temp_mods_mutex);
426                 core::map<v3s16, NodeMod>::Node *n;
427                 n = m_temp_mods.find(p);
428                 if(n == NULL)
429                         return false;
430                 if(mod)
431                         *mod = n->getValue();
432                 return true;
433         }
434         bool clearTempMod(v3s16 p)
435         {
436                 JMutexAutoLock lock(m_temp_mods_mutex);
437                 if(m_temp_mods.find(p))
438                 {
439                         m_temp_mods.remove(p);
440                         return true;
441                 }
442                 return false;
443         }
444         bool clearTempMods()
445         {
446                 JMutexAutoLock lock(m_temp_mods_mutex);
447                 if(m_temp_mods.size() == 0)
448                         return false;
449                 m_temp_mods.clear();
450                 return true;
451         }
452 #endif
453
454         /*
455                 Day-night lighting difference
456                 
457                 These methods don't care about neighboring blocks.
458                 It means that to know if a block really doesn't need a mesh
459                 update between day and night, the neighboring blocks have
460                 to be taken into account. Use Map::dayNightDiffed().
461         */
462         void updateDayNightDiff();
463
464         bool dayNightDiffed()
465         {
466                 return m_day_night_differs;
467         }
468
469         /*
470                 Miscellaneous stuff
471         */
472         
473         /*
474                 Tries to measure ground level.
475                 Return value:
476                         -1 = only air
477                         -2 = only ground
478                         -3 = random fail
479                         0...MAP_BLOCKSIZE-1 = ground level
480         */
481         s16 getGroundLevel(v2s16 p2d);
482
483         /*
484                 Serialization
485         */
486         
487         // Doesn't write version by itself
488         void serialize(std::ostream &os, u8 version);
489
490         void deSerialize(std::istream &is, u8 version);
491
492         /*
493                 Public member variables
494         */
495
496 #ifndef SERVER
497         //scene::SMesh *mesh[DAYNIGHT_CACHE_COUNT];
498         scene::SMesh *mesh;
499         JMutex mesh_mutex;
500 #endif
501
502 private:
503
504         /*
505                 Used only internally, because changes can't be tracked
506         */
507
508         MapNode & getNodeRef(s16 x, s16 y, s16 z)
509         {
510                 if(data == NULL)
511                         throw InvalidPositionException();
512                 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
513                 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
514                 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
515                 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
516         }
517         MapNode & getNodeRef(v3s16 &p)
518         {
519                 return getNodeRef(p.X, p.Y, p.Z);
520         }
521
522         
523         NodeContainer *m_parent;
524         // Position in blocks on parent
525         v3s16 m_pos;
526         /*
527                 If NULL, block is a dummy block.
528                 Dummy blocks are used for caching not-found-on-disk blocks.
529         */
530         MapNode * data;
531         /*
532                 - On the client, this is used for checking whether to
533                   recalculate the face cache. (Is it anymore?)
534                 - On the server, this is used for telling whether the
535                   block has been changed from the one on disk.
536         */
537         bool changed;
538         /*
539                 Used for some initial lighting stuff.
540                 At least /has been/ used. 8)
541                 It's probably useless now.
542         */
543         bool is_underground;
544         
545         // Whether day and night lighting differs
546         bool m_day_night_differs;
547         
548         MapBlockObjectList m_objects;
549
550         // Object spawning stuff
551         float m_spawn_timer;
552         
553 #ifndef SERVER
554         bool m_mesh_expired;
555         
556         // Temporary modifications to nodes
557         // These are only used when drawing
558         core::map<v3s16, NodeMod> m_temp_mods;
559         JMutex m_temp_mods_mutex;
560 #endif
561 };
562
563 inline bool blockpos_over_limit(v3s16 p)
564 {
565         return
566           (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
567         || p.X >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
568         || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
569         || p.Y >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
570         || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
571         || p.Z >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
572 }
573
574 /*
575         Returns the position of the block where the node is located
576 */
577 inline v3s16 getNodeBlockPos(v3s16 p)
578 {
579         return getContainerPos(p, MAP_BLOCKSIZE);
580 }
581
582 inline v2s16 getNodeSectorPos(v2s16 p)
583 {
584         return getContainerPos(p, MAP_BLOCKSIZE);
585 }
586
587 inline s16 getNodeBlockY(s16 y)
588 {
589         return getContainerPos(y, MAP_BLOCKSIZE);
590 }
591
592 #endif
593