]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapblock.h
day/night working client side
[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 #define MAP_BLOCKSIZE 16
36
37 // Named by looking towards z+
38 enum{
39         FACE_BACK=0,
40         FACE_TOP,
41         FACE_RIGHT,
42         FACE_FRONT,
43         FACE_BOTTOM,
44         FACE_LEFT
45 };
46
47 struct FastFace
48 {
49         TileSpec tile;
50         video::S3DVertex vertices[4]; // Precalculated vertices
51 };
52
53 enum NodeModType
54 {
55         NODEMOD_NONE,
56         NODEMOD_CHANGECONTENT, //param is content id
57         NODEMOD_CRACK // param is crack progression
58 };
59
60 struct NodeMod
61 {
62         NodeMod()
63         {
64                 type = NODEMOD_NONE;
65         }
66         enum NodeModType type;
67         u16 param;
68 };
69
70 enum
71 {
72         NODECONTAINER_ID_MAPBLOCK,
73         NODECONTAINER_ID_MAPSECTOR,
74         NODECONTAINER_ID_MAP,
75         NODECONTAINER_ID_MAPBLOCKCACHE,
76         NODECONTAINER_ID_VOXELMANIPULATOR,
77 };
78
79 class NodeContainer
80 {
81 public:
82         virtual bool isValidPosition(v3s16 p) = 0;
83         virtual MapNode getNode(v3s16 p) = 0;
84         virtual void setNode(v3s16 p, MapNode & n) = 0;
85         virtual u16 nodeContainerId() const = 0;
86 };
87
88 class MapBlock : public NodeContainer
89 {
90 public:
91
92         //scene::SMesh *mesh[DAYNIGHT_CACHE_COUNT];
93         scene::SMesh *mesh;
94         JMutex mesh_mutex;
95
96         MapBlock(NodeContainer *parent, v3s16 pos, bool dummy=false);
97         ~MapBlock();
98         
99         virtual u16 nodeContainerId() const
100         {
101                 return NODECONTAINER_ID_MAPBLOCK;
102         }
103
104         NodeContainer * getParent()
105         {
106                 return m_parent;
107         }
108
109         bool isDummy()
110         {
111                 return (data == NULL);
112         }
113
114         void unDummify()
115         {
116                 assert(isDummy());
117                 reallocate();
118         }
119         
120         bool getChangedFlag()
121         {
122                 return changed;
123         }
124
125         void resetChangedFlag()
126         {
127                 changed = false;
128         }
129
130         void setChangedFlag()
131         {
132                 changed = true;
133         }
134
135         void setMeshExpired(bool expired)
136         {
137                 m_mesh_expired = expired;
138         }
139         
140         bool getMeshExpired()
141         {
142                 return m_mesh_expired;
143         }
144
145         v3s16 getPos()
146         {
147                 return m_pos;
148         }
149                 
150         v3s16 getPosRelative()
151         {
152                 return m_pos * MAP_BLOCKSIZE;
153         }
154                 
155         bool getIsUnderground()
156         {
157                 return is_underground;
158         }
159
160         void setIsUnderground(bool a_is_underground)
161         {
162                 is_underground = a_is_underground;
163                 setChangedFlag();
164         }
165
166         core::aabbox3d<s16> getBox()
167         {
168                 return core::aabbox3d<s16>(getPosRelative(),
169                                 getPosRelative()
170                                 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
171                                 - v3s16(1,1,1));
172         }
173         
174         void reallocate()
175         {
176                 if(data != NULL)
177                         delete[] data;
178                 u32 l = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
179                 data = new MapNode[l];
180                 for(u32 i=0; i<l; i++){
181                         data[i] = MapNode();
182                 }
183                 setChangedFlag();
184         }
185
186         bool isValidPosition(v3s16 p)
187         {
188                 if(data == NULL)
189                         return false;
190                 return (p.X >= 0 && p.X < MAP_BLOCKSIZE
191                                 && p.Y >= 0 && p.Y < MAP_BLOCKSIZE
192                                 && p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
193         }
194
195         /*
196                 Regular MapNode get-setters
197         */
198         
199         MapNode getNode(s16 x, s16 y, s16 z)
200         {
201                 if(data == NULL)
202                         throw InvalidPositionException();
203                 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
204                 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
205                 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
206                 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
207         }
208         
209         MapNode getNode(v3s16 p)
210         {
211                 return getNode(p.X, p.Y, p.Z);
212         }
213         
214         void setNode(s16 x, s16 y, s16 z, MapNode & n)
215         {
216                 if(data == NULL)
217                         throw InvalidPositionException();
218                 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
219                 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
220                 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
221                 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
222                 setChangedFlag();
223         }
224         
225         void setNode(v3s16 p, MapNode & n)
226         {
227                 setNode(p.X, p.Y, p.Z, n);
228         }
229
230         /*
231                 Non-checking variants of the above
232         */
233
234         MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
235         {
236                 if(data == NULL)
237                         throw InvalidPositionException();
238                 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
239         }
240         
241         MapNode getNodeNoCheck(v3s16 p)
242         {
243                 return getNodeNoCheck(p.X, p.Y, p.Z);
244         }
245         
246         void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
247         {
248                 if(data == NULL)
249                         throw InvalidPositionException();
250                 data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x] = n;
251                 setChangedFlag();
252         }
253         
254         void setNodeNoCheck(v3s16 p, MapNode & n)
255         {
256                 setNodeNoCheck(p.X, p.Y, p.Z, n);
257         }
258
259         /*
260                 These functions consult the parent container if the position
261                 is not valid on this MapBlock.
262         */
263         bool isValidPositionParent(v3s16 p);
264         MapNode getNodeParent(v3s16 p);
265         void setNodeParent(v3s16 p, MapNode & n);
266         MapNode getNodeParentNoEx(v3s16 p);
267
268         void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
269         {
270                 for(u16 z=0; z<d; z++)
271                         for(u16 y=0; y<h; y++)
272                                 for(u16 x=0; x<w; x++)
273                                         setNode(x0+x, y0+y, z0+z, node);
274         }
275
276         static void makeFastFace(TileSpec tile, u8 light, v3f p,
277                         v3s16 dir, v3f scale, v3f posRelative_f,
278                         core::array<FastFace> &dest);
279         
280         u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
281                         v3s16 face_dir);
282         
283         u8 getFaceLight(u32 daynight_ratio, v3s16 p, v3s16 face_dir)
284         {
285                 return getFaceLight(daynight_ratio,
286                                 getNodeParentNoEx(p),
287                                 getNodeParentNoEx(p + face_dir),
288                                 face_dir);
289         }
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
315         bool propagateSunlight(core::map<v3s16, bool> & light_sources);
316         
317         // Copies data to VoxelManipulator to getPosRelative()
318         void copyTo(VoxelManipulator &dst);
319
320         /*
321                 Object stuff
322         */
323         
324         void serializeObjects(std::ostream &os, u8 version)
325         {
326                 m_objects.serialize(os, version);
327         }
328         // If smgr!=NULL, new objects are added to the scene
329         void updateObjects(std::istream &is, u8 version,
330                         scene::ISceneManager *smgr)
331         {
332                 m_objects.update(is, version, smgr);
333
334                 setChangedFlag();
335         }
336         void clearObjects()
337         {
338                 m_objects.clear();
339
340                 setChangedFlag();
341         }
342         void addObject(MapBlockObject *object)
343                         throw(ContainerFullException, AlreadyExistsException)
344         {
345                 m_objects.add(object);
346
347                 setChangedFlag();
348         }
349         void removeObject(s16 id)
350         {
351                 m_objects.remove(id);
352
353                 setChangedFlag();
354         }
355         MapBlockObject * getObject(s16 id)
356         {
357                 return m_objects.get(id);
358         }
359         JMutexAutoLock * getObjectLock()
360         {
361                 return m_objects.getLock();
362         }
363         void stepObjects(float dtime, bool server)
364         {
365                 m_objects.step(dtime, server);
366
367                 setChangedFlag();
368         }
369
370         /*void wrapObject(MapBlockObject *object)
371         {
372                 m_objects.wrapObject(object);
373
374                 setChangedFlag();
375         }*/
376
377         // origin is relative to block
378         void getObjects(v3f origin, f32 max_d,
379                         core::array<DistanceSortedObject> &dest)
380         {
381                 m_objects.getObjects(origin, max_d, dest);
382         }
383
384         /*void getPseudoObjects(v3f origin, f32 max_d,
385                         core::array<DistanceSortedObject> &dest);*/
386
387         s32 getObjectCount()
388         {
389                 return m_objects.getCount();
390         }
391         
392         /*
393                 Methods for setting temporary modifications to nodes for
394                 drawing
395         */
396         void setTempMod(v3s16 p, NodeMod mod)
397         {
398                 m_temp_mods[p] = mod;
399         }
400         void clearTempMod(v3s16 p)
401         {
402                 if(m_temp_mods.find(p))
403                         m_temp_mods.remove(p);
404         }
405         void clearTempMods()
406         {
407                 m_temp_mods.clear();
408         }
409
410         /*
411                 Day-night lighting difference
412                 
413                 These methods don't care about neighboring blocks.
414                 It means that to know if a block really doesn't need a mesh
415                 update between day and night, the neighboring blocks have
416                 to be taken into account.
417         */
418         void updateDayNightDiff();
419
420         bool dayNightDiffed()
421         {
422                 return m_day_night_differs;
423         }
424
425         /*
426                 Serialization
427         */
428         
429         // Doesn't write version by itself
430         void serialize(std::ostream &os, u8 version);
431
432         void deSerialize(std::istream &is, u8 version);
433
434 private:
435
436         /*
437                 Used only internally, because changes can't be tracked
438         */
439
440         MapNode & getNodeRef(s16 x, s16 y, s16 z)
441         {
442                 if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
443                 if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
444                 if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
445                 return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
446         }
447         MapNode & getNodeRef(v3s16 &p)
448         {
449                 return getNodeRef(p.X, p.Y, p.Z);
450         }
451
452         
453         NodeContainer *m_parent;
454         // Position in blocks on parent
455         v3s16 m_pos;
456         /*
457                 If NULL, block is a dummy block.
458                 Dummy blocks are used for caching not-found-on-disk blocks.
459         */
460         MapNode * data;
461         /*
462                 - On the client, this is used for checking whether to
463                   recalculate the face cache. (Is it anymore?)
464                 - On the server, this is used for telling whether the
465                   block has been changed from the one on disk.
466         */
467         bool changed;
468         /*
469                 Used for some initial lighting stuff.
470                 At least /has been/ used. 8)
471         */
472         bool is_underground;
473
474         bool m_mesh_expired;
475         
476         // Whether day and night lighting differs
477         bool m_day_night_differs;
478         
479         MapBlockObjectList m_objects;
480         
481         // Temporary modifications to nodes
482         // These are only used when drawing
483         core::map<v3s16, NodeMod> m_temp_mods;
484 };
485
486 inline bool blockpos_over_limit(v3s16 p)
487 {
488         return
489           (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
490         || p.X >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
491         || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
492         || p.Y >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
493         || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
494         || p.Z >  MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
495 }
496
497 /*
498         Returns the position of the block where the node is located
499 */
500 inline v3s16 getNodeBlockPos(v3s16 p)
501 {
502         return getContainerPos(p, MAP_BLOCKSIZE);
503 }
504
505 inline v2s16 getNodeSectorPos(v2s16 p)
506 {
507         return getContainerPos(p, MAP_BLOCKSIZE);
508 }
509
510 inline s16 getNodeBlockY(s16 y)
511 {
512         return getContainerPos(y, MAP_BLOCKSIZE);
513 }
514
515 #endif
516