]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map.cpp
Code modernization: src/m* (part 2)
[dragonfireclient.git] / src / map.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #include "map.h"
21 #include "mapsector.h"
22 #include "mapblock.h"
23 #include "filesys.h"
24 #include "voxel.h"
25 #include "voxelalgorithms.h"
26 #include "porting.h"
27 #include "serialization.h"
28 #include "nodemetadata.h"
29 #include "settings.h"
30 #include "log.h"
31 #include "profiler.h"
32 #include "nodedef.h"
33 #include "gamedef.h"
34 #include "util/directiontables.h"
35 #include "util/basic_macros.h"
36 #include "rollback_interface.h"
37 #include "environment.h"
38 #include "reflowscan.h"
39 #include "emerge.h"
40 #include "mapgen_v6.h"
41 #include "mg_biome.h"
42 #include "config.h"
43 #include "server.h"
44 #include "database.h"
45 #include "database-dummy.h"
46 #include "database-sqlite3.h"
47 #include "script/scripting_server.h"
48 #include <deque>
49 #include <queue>
50 #if USE_LEVELDB
51 #include "database-leveldb.h"
52 #endif
53 #if USE_REDIS
54 #include "database-redis.h"
55 #endif
56 #if USE_POSTGRESQL
57 #include "database-postgresql.h"
58 #endif
59
60
61 /*
62         Map
63 */
64
65 Map::Map(std::ostream &dout, IGameDef *gamedef):
66         m_dout(dout),
67         m_gamedef(gamedef),
68         m_nodedef(gamedef->ndef())
69 {
70 }
71
72 Map::~Map()
73 {
74         /*
75                 Free all MapSectors
76         */
77         for (auto &sector : m_sectors) {
78                 delete sector.second;
79         }
80 }
81
82 void Map::addEventReceiver(MapEventReceiver *event_receiver)
83 {
84         m_event_receivers.insert(event_receiver);
85 }
86
87 void Map::removeEventReceiver(MapEventReceiver *event_receiver)
88 {
89         m_event_receivers.erase(event_receiver);
90 }
91
92 void Map::dispatchEvent(MapEditEvent *event)
93 {
94         for (MapEventReceiver *event_receiver : m_event_receivers) {
95                 event_receiver->onMapEditEvent(event);
96         }
97 }
98
99 MapSector * Map::getSectorNoGenerateNoExNoLock(v2s16 p)
100 {
101         if(m_sector_cache != NULL && p == m_sector_cache_p){
102                 MapSector * sector = m_sector_cache;
103                 return sector;
104         }
105
106         std::map<v2s16, MapSector*>::iterator n = m_sectors.find(p);
107
108         if (n == m_sectors.end())
109                 return NULL;
110
111         MapSector *sector = n->second;
112
113         // Cache the last result
114         m_sector_cache_p = p;
115         m_sector_cache = sector;
116
117         return sector;
118 }
119
120 MapSector * Map::getSectorNoGenerateNoEx(v2s16 p)
121 {
122         return getSectorNoGenerateNoExNoLock(p);
123 }
124
125 MapSector * Map::getSectorNoGenerate(v2s16 p)
126 {
127         MapSector *sector = getSectorNoGenerateNoEx(p);
128         if(sector == NULL)
129                 throw InvalidPositionException();
130
131         return sector;
132 }
133
134 MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
135 {
136         v2s16 p2d(p3d.X, p3d.Z);
137         MapSector * sector = getSectorNoGenerateNoEx(p2d);
138         if(sector == NULL)
139                 return NULL;
140         MapBlock *block = sector->getBlockNoCreateNoEx(p3d.Y);
141         return block;
142 }
143
144 MapBlock * Map::getBlockNoCreate(v3s16 p3d)
145 {
146         MapBlock *block = getBlockNoCreateNoEx(p3d);
147         if(block == NULL)
148                 throw InvalidPositionException();
149         return block;
150 }
151
152 bool Map::isNodeUnderground(v3s16 p)
153 {
154         v3s16 blockpos = getNodeBlockPos(p);
155         try{
156                 MapBlock * block = getBlockNoCreate(blockpos);
157                 return block->getIsUnderground();
158         }
159         catch(InvalidPositionException &e)
160         {
161                 return false;
162         }
163 }
164
165 bool Map::isValidPosition(v3s16 p)
166 {
167         v3s16 blockpos = getNodeBlockPos(p);
168         MapBlock *block = getBlockNoCreateNoEx(blockpos);
169         return (block != NULL);
170 }
171
172 // Returns a CONTENT_IGNORE node if not found
173 MapNode Map::getNodeNoEx(v3s16 p, bool *is_valid_position)
174 {
175         v3s16 blockpos = getNodeBlockPos(p);
176         MapBlock *block = getBlockNoCreateNoEx(blockpos);
177         if (block == NULL) {
178                 if (is_valid_position != NULL)
179                         *is_valid_position = false;
180                 return {CONTENT_IGNORE};
181         }
182
183         v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
184         bool is_valid_p;
185         MapNode node = block->getNodeNoCheck(relpos, &is_valid_p);
186         if (is_valid_position != NULL)
187                 *is_valid_position = is_valid_p;
188         return node;
189 }
190
191 #if 0
192 // Deprecated
193 // throws InvalidPositionException if not found
194 // TODO: Now this is deprecated, getNodeNoEx should be renamed
195 MapNode Map::getNode(v3s16 p)
196 {
197         v3s16 blockpos = getNodeBlockPos(p);
198         MapBlock *block = getBlockNoCreateNoEx(blockpos);
199         if (block == NULL)
200                 throw InvalidPositionException();
201         v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
202         bool is_valid_position;
203         MapNode node = block->getNodeNoCheck(relpos, &is_valid_position);
204         if (!is_valid_position)
205                 throw InvalidPositionException();
206         return node;
207 }
208 #endif
209
210 // throws InvalidPositionException if not found
211 void Map::setNode(v3s16 p, MapNode & n)
212 {
213         v3s16 blockpos = getNodeBlockPos(p);
214         MapBlock *block = getBlockNoCreate(blockpos);
215         v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
216         // Never allow placing CONTENT_IGNORE, it fucks up stuff
217         if(n.getContent() == CONTENT_IGNORE){
218                 bool temp_bool;
219                 errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE"
220                                 <<" while trying to replace \""
221                                 <<m_nodedef->get(block->getNodeNoCheck(relpos, &temp_bool)).name
222                                 <<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
223                 debug_stacks_print_to(infostream);
224                 return;
225         }
226         block->setNodeNoCheck(relpos, n);
227 }
228
229 void Map::addNodeAndUpdate(v3s16 p, MapNode n,
230                 std::map<v3s16, MapBlock*> &modified_blocks,
231                 bool remove_metadata)
232 {
233         // Collect old node for rollback
234         RollbackNode rollback_oldnode(this, p, m_gamedef);
235
236         // This is needed for updating the lighting
237         MapNode oldnode = getNodeNoEx(p);
238
239         // Remove node metadata
240         if (remove_metadata) {
241                 removeNodeMetadata(p);
242         }
243
244         // Set the node on the map
245         // Ignore light (because calling voxalgo::update_lighting_nodes)
246         n.setLight(LIGHTBANK_DAY, 0, m_nodedef);
247         n.setLight(LIGHTBANK_NIGHT, 0, m_nodedef);
248         setNode(p, n);
249
250         // Update lighting
251         std::vector<std::pair<v3s16, MapNode> > oldnodes;
252         oldnodes.emplace_back(p, oldnode);
253         voxalgo::update_lighting_nodes(this, oldnodes, modified_blocks);
254
255         for (auto &modified_block : modified_blocks) {
256                 modified_block.second->expireDayNightDiff();
257         }
258
259         // Report for rollback
260         if(m_gamedef->rollback())
261         {
262                 RollbackNode rollback_newnode(this, p, m_gamedef);
263                 RollbackAction action;
264                 action.setSetNode(p, rollback_oldnode, rollback_newnode);
265                 m_gamedef->rollback()->reportAction(action);
266         }
267
268         /*
269                 Add neighboring liquid nodes and this node to transform queue.
270                 (it's vital for the node itself to get updated last, if it was removed.)
271          */
272
273         for (const v3s16 &dir : g_7dirs) {
274                 v3s16 p2 = p + dir;
275
276                 bool is_valid_position;
277                 MapNode n2 = getNodeNoEx(p2, &is_valid_position);
278                 if(is_valid_position &&
279                                 (m_nodedef->get(n2).isLiquid() ||
280                                 n2.getContent() == CONTENT_AIR))
281                         m_transforming_liquid.push_back(p2);
282         }
283 }
284
285 void Map::removeNodeAndUpdate(v3s16 p,
286                 std::map<v3s16, MapBlock*> &modified_blocks)
287 {
288         addNodeAndUpdate(p, MapNode(CONTENT_AIR), modified_blocks, true);
289 }
290
291 bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
292 {
293         MapEditEvent event;
294         event.type = remove_metadata ? MEET_ADDNODE : MEET_SWAPNODE;
295         event.p = p;
296         event.n = n;
297
298         bool succeeded = true;
299         try{
300                 std::map<v3s16, MapBlock*> modified_blocks;
301                 addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
302
303                 // Copy modified_blocks to event
304                 for (auto &modified_block : modified_blocks) {
305                         event.modified_blocks.insert(modified_block.first);
306                 }
307         }
308         catch(InvalidPositionException &e){
309                 succeeded = false;
310         }
311
312         dispatchEvent(&event);
313
314         return succeeded;
315 }
316
317 bool Map::removeNodeWithEvent(v3s16 p)
318 {
319         MapEditEvent event;
320         event.type = MEET_REMOVENODE;
321         event.p = p;
322
323         bool succeeded = true;
324         try{
325                 std::map<v3s16, MapBlock*> modified_blocks;
326                 removeNodeAndUpdate(p, modified_blocks);
327
328                 // Copy modified_blocks to event
329                 for (auto &modified_block : modified_blocks) {
330                         event.modified_blocks.insert(modified_block.first);
331                 }
332         }
333         catch(InvalidPositionException &e){
334                 succeeded = false;
335         }
336
337         dispatchEvent(&event);
338
339         return succeeded;
340 }
341
342 struct TimeOrderedMapBlock {
343         MapSector *sect;
344         MapBlock *block;
345
346         TimeOrderedMapBlock(MapSector *sect, MapBlock *block) :
347                 sect(sect),
348                 block(block)
349         {}
350
351         bool operator<(const TimeOrderedMapBlock &b) const
352         {
353                 return block->getUsageTimer() < b.block->getUsageTimer();
354         };
355 };
356
357 /*
358         Updates usage timers
359 */
360 void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
361                 std::vector<v3s16> *unloaded_blocks)
362 {
363         bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
364
365         // Profile modified reasons
366         Profiler modprofiler;
367
368         std::vector<v2s16> sector_deletion_queue;
369         u32 deleted_blocks_count = 0;
370         u32 saved_blocks_count = 0;
371         u32 block_count_all = 0;
372
373         beginSave();
374
375         // If there is no practical limit, we spare creation of mapblock_queue
376         if (max_loaded_blocks == U32_MAX) {
377                 for (auto &sector_it : m_sectors) {
378                         MapSector *sector = sector_it.second;
379
380                         bool all_blocks_deleted = true;
381
382                         MapBlockVect blocks;
383                         sector->getBlocks(blocks);
384
385                         for (MapBlock *block : blocks) {
386                                 block->incrementUsageTimer(dtime);
387
388                                 if (block->refGet() == 0
389                                                 && block->getUsageTimer() > unload_timeout) {
390                                         v3s16 p = block->getPos();
391
392                                         // Save if modified
393                                         if (block->getModified() != MOD_STATE_CLEAN
394                                                         && save_before_unloading) {
395                                                 modprofiler.add(block->getModifiedReasonString(), 1);
396                                                 if (!saveBlock(block))
397                                                         continue;
398                                                 saved_blocks_count++;
399                                         }
400
401                                         // Delete from memory
402                                         sector->deleteBlock(block);
403
404                                         if (unloaded_blocks)
405                                                 unloaded_blocks->push_back(p);
406
407                                         deleted_blocks_count++;
408                                 } else {
409                                         all_blocks_deleted = false;
410                                         block_count_all++;
411                                 }
412                         }
413
414                         if (all_blocks_deleted) {
415                                 sector_deletion_queue.push_back(sector_it.first);
416                         }
417                 }
418         } else {
419                 std::priority_queue<TimeOrderedMapBlock> mapblock_queue;
420                 for (auto &sector_it : m_sectors) {
421                         MapSector *sector = sector_it.second;
422
423                         MapBlockVect blocks;
424                         sector->getBlocks(blocks);
425
426                         for (MapBlock *block : blocks) {
427                                 block->incrementUsageTimer(dtime);
428                                 mapblock_queue.push(TimeOrderedMapBlock(sector, block));
429                         }
430                 }
431                 block_count_all = mapblock_queue.size();
432                 // Delete old blocks, and blocks over the limit from the memory
433                 while (!mapblock_queue.empty() && (mapblock_queue.size() > max_loaded_blocks
434                                 || mapblock_queue.top().block->getUsageTimer() > unload_timeout)) {
435                         TimeOrderedMapBlock b = mapblock_queue.top();
436                         mapblock_queue.pop();
437
438                         MapBlock *block = b.block;
439
440                         if (block->refGet() != 0)
441                                 continue;
442
443                         v3s16 p = block->getPos();
444
445                         // Save if modified
446                         if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
447                                 modprofiler.add(block->getModifiedReasonString(), 1);
448                                 if (!saveBlock(block))
449                                         continue;
450                                 saved_blocks_count++;
451                         }
452
453                         // Delete from memory
454                         b.sect->deleteBlock(block);
455
456                         if (unloaded_blocks)
457                                 unloaded_blocks->push_back(p);
458
459                         deleted_blocks_count++;
460                         block_count_all--;
461                 }
462                 // Delete empty sectors
463                 for (auto &sector_it : m_sectors) {
464                         if (sector_it.second->empty()) {
465                                 sector_deletion_queue.push_back(sector_it.first);
466                         }
467                 }
468         }
469         endSave();
470
471         // Finally delete the empty sectors
472         deleteSectors(sector_deletion_queue);
473
474         if(deleted_blocks_count != 0)
475         {
476                 PrintInfo(infostream); // ServerMap/ClientMap:
477                 infostream<<"Unloaded "<<deleted_blocks_count
478                                 <<" blocks from memory";
479                 if(save_before_unloading)
480                         infostream<<", of which "<<saved_blocks_count<<" were written";
481                 infostream<<", "<<block_count_all<<" blocks in memory";
482                 infostream<<"."<<std::endl;
483                 if(saved_blocks_count != 0){
484                         PrintInfo(infostream); // ServerMap/ClientMap:
485                         infostream<<"Blocks modified by: "<<std::endl;
486                         modprofiler.print(infostream);
487                 }
488         }
489 }
490
491 void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks)
492 {
493         timerUpdate(0.0, -1.0, 0, unloaded_blocks);
494 }
495
496 void Map::deleteSectors(std::vector<v2s16> &sectorList)
497 {
498         for (v2s16 j : sectorList) {
499                 MapSector *sector = m_sectors[j];
500                 // If sector is in sector cache, remove it from there
501                 if(m_sector_cache == sector)
502                         m_sector_cache = NULL;
503                 // Remove from map and delete
504                 m_sectors.erase(j);
505                 delete sector;
506         }
507 }
508
509 void Map::PrintInfo(std::ostream &out)
510 {
511         out<<"Map: ";
512 }
513
514 #define WATER_DROP_BOOST 4
515
516 enum NeighborType {
517         NEIGHBOR_UPPER,
518         NEIGHBOR_SAME_LEVEL,
519         NEIGHBOR_LOWER
520 };
521 struct NodeNeighbor {
522         MapNode n;
523         NeighborType t;
524         v3s16 p;
525         bool l; //can liquid
526
527         NodeNeighbor()
528                 : n(CONTENT_AIR)
529         { }
530
531         NodeNeighbor(const MapNode &node, NeighborType n_type, v3s16 pos)
532                 : n(node),
533                   t(n_type),
534                   p(pos)
535         { }
536 };
537
538 void Map::transforming_liquid_add(v3s16 p) {
539         m_transforming_liquid.push_back(p);
540 }
541
542 s32 Map::transforming_liquid_size() {
543         return m_transforming_liquid.size();
544 }
545
546 void Map::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
547                 ServerEnvironment *env)
548 {
549         DSTACK(FUNCTION_NAME);
550         //TimeTaker timer("transformLiquids()");
551
552         u32 loopcount = 0;
553         u32 initial_size = m_transforming_liquid.size();
554
555         /*if(initial_size != 0)
556                 infostream<<"transformLiquids(): initial_size="<<initial_size<<std::endl;*/
557
558         // list of nodes that due to viscosity have not reached their max level height
559         std::deque<v3s16> must_reflow;
560
561         std::vector<std::pair<v3s16, MapNode> > changed_nodes;
562
563         u32 liquid_loop_max = g_settings->getS32("liquid_loop_max");
564         u32 loop_max = liquid_loop_max;
565
566 #if 0
567
568         /* If liquid_loop_max is not keeping up with the queue size increase
569          * loop_max up to a maximum of liquid_loop_max * dedicated_server_step.
570          */
571         if (m_transforming_liquid.size() > loop_max * 2) {
572                 // "Burst" mode
573                 float server_step = g_settings->getFloat("dedicated_server_step");
574                 if (m_transforming_liquid_loop_count_multiplier - 1.0 < server_step)
575                         m_transforming_liquid_loop_count_multiplier *= 1.0 + server_step / 10;
576         } else {
577                 m_transforming_liquid_loop_count_multiplier = 1.0;
578         }
579
580         loop_max *= m_transforming_liquid_loop_count_multiplier;
581 #endif
582
583         while (m_transforming_liquid.size() != 0)
584         {
585                 // This should be done here so that it is done when continue is used
586                 if (loopcount >= initial_size || loopcount >= loop_max)
587                         break;
588                 loopcount++;
589
590                 /*
591                         Get a queued transforming liquid node
592                 */
593                 v3s16 p0 = m_transforming_liquid.front();
594                 m_transforming_liquid.pop_front();
595
596                 MapNode n0 = getNodeNoEx(p0);
597
598                 /*
599                         Collect information about current node
600                  */
601                 s8 liquid_level = -1;
602                 // The liquid node which will be placed there if
603                 // the liquid flows into this node.
604                 content_t liquid_kind = CONTENT_IGNORE;
605                 // The node which will be placed there if liquid
606                 // can't flow into this node.
607                 content_t floodable_node = CONTENT_AIR;
608                 const ContentFeatures &cf = m_nodedef->get(n0);
609                 LiquidType liquid_type = cf.liquid_type;
610                 switch (liquid_type) {
611                         case LIQUID_SOURCE:
612                                 liquid_level = LIQUID_LEVEL_SOURCE;
613                                 liquid_kind = m_nodedef->getId(cf.liquid_alternative_flowing);
614                                 break;
615                         case LIQUID_FLOWING:
616                                 liquid_level = (n0.param2 & LIQUID_LEVEL_MASK);
617                                 liquid_kind = n0.getContent();
618                                 break;
619                         case LIQUID_NONE:
620                                 // if this node is 'floodable', it *could* be transformed
621                                 // into a liquid, otherwise, continue with the next node.
622                                 if (!cf.floodable)
623                                         continue;
624                                 floodable_node = n0.getContent();
625                                 liquid_kind = CONTENT_AIR;
626                                 break;
627                 }
628
629                 /*
630                         Collect information about the environment
631                  */
632                 const v3s16 *dirs = g_6dirs;
633                 NodeNeighbor sources[6]; // surrounding sources
634                 int num_sources = 0;
635                 NodeNeighbor flows[6]; // surrounding flowing liquid nodes
636                 int num_flows = 0;
637                 NodeNeighbor airs[6]; // surrounding air
638                 int num_airs = 0;
639                 NodeNeighbor neutrals[6]; // nodes that are solid or another kind of liquid
640                 int num_neutrals = 0;
641                 bool flowing_down = false;
642                 bool ignored_sources = false;
643                 for (u16 i = 0; i < 6; i++) {
644                         NeighborType nt = NEIGHBOR_SAME_LEVEL;
645                         switch (i) {
646                                 case 1:
647                                         nt = NEIGHBOR_UPPER;
648                                         break;
649                                 case 4:
650                                         nt = NEIGHBOR_LOWER;
651                                         break;
652                         }
653                         v3s16 npos = p0 + dirs[i];
654                         NodeNeighbor nb(getNodeNoEx(npos), nt, npos);
655                         const ContentFeatures &cfnb = m_nodedef->get(nb.n);
656                         switch (m_nodedef->get(nb.n.getContent()).liquid_type) {
657                                 case LIQUID_NONE:
658                                         if (cfnb.floodable) {
659                                                 airs[num_airs++] = nb;
660                                                 // if the current node is a water source the neighbor
661                                                 // should be enqueded for transformation regardless of whether the
662                                                 // current node changes or not.
663                                                 if (nb.t != NEIGHBOR_UPPER && liquid_type != LIQUID_NONE)
664                                                         m_transforming_liquid.push_back(npos);
665                                                 // if the current node happens to be a flowing node, it will start to flow down here.
666                                                 if (nb.t == NEIGHBOR_LOWER)
667                                                         flowing_down = true;
668                                         } else {
669                                                 neutrals[num_neutrals++] = nb;
670                                                 if (nb.n.getContent() == CONTENT_IGNORE) {
671                                                         // If node below is ignore prevent water from
672                                                         // spreading outwards and otherwise prevent from
673                                                         // flowing away as ignore node might be the source
674                                                         if (nb.t == NEIGHBOR_LOWER)
675                                                                 flowing_down = true;
676                                                         else
677                                                                 ignored_sources = true;
678                                                 }
679                                         }
680                                         break;
681                                 case LIQUID_SOURCE:
682                                         // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
683                                         if (liquid_kind == CONTENT_AIR)
684                                                 liquid_kind = m_nodedef->getId(cfnb.liquid_alternative_flowing);
685                                         if (m_nodedef->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
686                                                 neutrals[num_neutrals++] = nb;
687                                         } else {
688                                                 // Do not count bottom source, it will screw things up
689                                                 if(dirs[i].Y != -1)
690                                                         sources[num_sources++] = nb;
691                                         }
692                                         break;
693                                 case LIQUID_FLOWING:
694                                         // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
695                                         if (liquid_kind == CONTENT_AIR)
696                                                 liquid_kind = m_nodedef->getId(cfnb.liquid_alternative_flowing);
697                                         if (m_nodedef->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
698                                                 neutrals[num_neutrals++] = nb;
699                                         } else {
700                                                 flows[num_flows++] = nb;
701                                                 if (nb.t == NEIGHBOR_LOWER)
702                                                         flowing_down = true;
703                                         }
704                                         break;
705                         }
706                 }
707
708                 /*
709                         decide on the type (and possibly level) of the current node
710                  */
711                 content_t new_node_content;
712                 s8 new_node_level = -1;
713                 s8 max_node_level = -1;
714
715                 u8 range = m_nodedef->get(liquid_kind).liquid_range;
716                 if (range > LIQUID_LEVEL_MAX + 1)
717                         range = LIQUID_LEVEL_MAX + 1;
718
719                 if ((num_sources >= 2 && m_nodedef->get(liquid_kind).liquid_renewable) || liquid_type == LIQUID_SOURCE) {
720                         // liquid_kind will be set to either the flowing alternative of the node (if it's a liquid)
721                         // or the flowing alternative of the first of the surrounding sources (if it's air), so
722                         // it's perfectly safe to use liquid_kind here to determine the new node content.
723                         new_node_content = m_nodedef->getId(m_nodedef->get(liquid_kind).liquid_alternative_source);
724                 } else if (num_sources >= 1 && sources[0].t != NEIGHBOR_LOWER) {
725                         // liquid_kind is set properly, see above
726                         max_node_level = new_node_level = LIQUID_LEVEL_MAX;
727                         if (new_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
728                                 new_node_content = liquid_kind;
729                         else
730                                 new_node_content = floodable_node;
731                 } else if (ignored_sources && liquid_level >= 0) {
732                         // Maybe there are neighbouring sources that aren't loaded yet
733                         // so prevent flowing away.
734                         new_node_level = liquid_level;
735                         new_node_content = liquid_kind;
736                 } else {
737                         // no surrounding sources, so get the maximum level that can flow into this node
738                         for (u16 i = 0; i < num_flows; i++) {
739                                 u8 nb_liquid_level = (flows[i].n.param2 & LIQUID_LEVEL_MASK);
740                                 switch (flows[i].t) {
741                                         case NEIGHBOR_UPPER:
742                                                 if (nb_liquid_level + WATER_DROP_BOOST > max_node_level) {
743                                                         max_node_level = LIQUID_LEVEL_MAX;
744                                                         if (nb_liquid_level + WATER_DROP_BOOST < LIQUID_LEVEL_MAX)
745                                                                 max_node_level = nb_liquid_level + WATER_DROP_BOOST;
746                                                 } else if (nb_liquid_level > max_node_level) {
747                                                         max_node_level = nb_liquid_level;
748                                                 }
749                                                 break;
750                                         case NEIGHBOR_LOWER:
751                                                 break;
752                                         case NEIGHBOR_SAME_LEVEL:
753                                                 if ((flows[i].n.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK &&
754                                                                 nb_liquid_level > 0 && nb_liquid_level - 1 > max_node_level)
755                                                         max_node_level = nb_liquid_level - 1;
756                                                 break;
757                                 }
758                         }
759
760                         u8 viscosity = m_nodedef->get(liquid_kind).liquid_viscosity;
761                         if (viscosity > 1 && max_node_level != liquid_level) {
762                                 // amount to gain, limited by viscosity
763                                 // must be at least 1 in absolute value
764                                 s8 level_inc = max_node_level - liquid_level;
765                                 if (level_inc < -viscosity || level_inc > viscosity)
766                                         new_node_level = liquid_level + level_inc/viscosity;
767                                 else if (level_inc < 0)
768                                         new_node_level = liquid_level - 1;
769                                 else if (level_inc > 0)
770                                         new_node_level = liquid_level + 1;
771                                 if (new_node_level != max_node_level)
772                                         must_reflow.push_back(p0);
773                         } else {
774                                 new_node_level = max_node_level;
775                         }
776
777                         if (max_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
778                                 new_node_content = liquid_kind;
779                         else
780                                 new_node_content = floodable_node;
781
782                 }
783
784                 /*
785                         check if anything has changed. if not, just continue with the next node.
786                  */
787                 if (new_node_content == n0.getContent() &&
788                                 (m_nodedef->get(n0.getContent()).liquid_type != LIQUID_FLOWING ||
789                                 ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level &&
790                                 ((n0.param2 & LIQUID_FLOW_DOWN_MASK) == LIQUID_FLOW_DOWN_MASK)
791                                 == flowing_down)))
792                         continue;
793
794
795                 /*
796                         update the current node
797                  */
798                 MapNode n00 = n0;
799                 //bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
800                 if (m_nodedef->get(new_node_content).liquid_type == LIQUID_FLOWING) {
801                         // set level to last 3 bits, flowing down bit to 4th bit
802                         n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK);
803                 } else {
804                         // set the liquid level and flow bit to 0
805                         n0.param2 = ~(LIQUID_LEVEL_MASK | LIQUID_FLOW_DOWN_MASK);
806                 }
807
808                 // change the node.
809                 n0.setContent(new_node_content);
810
811                 // on_flood() the node
812                 if (floodable_node != CONTENT_AIR) {
813                         if (env->getScriptIface()->node_on_flood(p0, n00, n0))
814                                 continue;
815                 }
816
817                 // Ignore light (because calling voxalgo::update_lighting_nodes)
818                 n0.setLight(LIGHTBANK_DAY, 0, m_nodedef);
819                 n0.setLight(LIGHTBANK_NIGHT, 0, m_nodedef);
820
821                 // Find out whether there is a suspect for this action
822                 std::string suspect;
823                 if (m_gamedef->rollback())
824                         suspect = m_gamedef->rollback()->getSuspect(p0, 83, 1);
825
826                 if (m_gamedef->rollback() && !suspect.empty()) {
827                         // Blame suspect
828                         RollbackScopeActor rollback_scope(m_gamedef->rollback(), suspect, true);
829                         // Get old node for rollback
830                         RollbackNode rollback_oldnode(this, p0, m_gamedef);
831                         // Set node
832                         setNode(p0, n0);
833                         // Report
834                         RollbackNode rollback_newnode(this, p0, m_gamedef);
835                         RollbackAction action;
836                         action.setSetNode(p0, rollback_oldnode, rollback_newnode);
837                         m_gamedef->rollback()->reportAction(action);
838                 } else {
839                         // Set node
840                         setNode(p0, n0);
841                 }
842
843                 v3s16 blockpos = getNodeBlockPos(p0);
844                 MapBlock *block = getBlockNoCreateNoEx(blockpos);
845                 if (block != NULL) {
846                         modified_blocks[blockpos] =  block;
847                         changed_nodes.emplace_back(p0, n00);
848                 }
849
850                 /*
851                         enqueue neighbors for update if neccessary
852                  */
853                 switch (m_nodedef->get(n0.getContent()).liquid_type) {
854                         case LIQUID_SOURCE:
855                         case LIQUID_FLOWING:
856                                 // make sure source flows into all neighboring nodes
857                                 for (u16 i = 0; i < num_flows; i++)
858                                         if (flows[i].t != NEIGHBOR_UPPER)
859                                                 m_transforming_liquid.push_back(flows[i].p);
860                                 for (u16 i = 0; i < num_airs; i++)
861                                         if (airs[i].t != NEIGHBOR_UPPER)
862                                                 m_transforming_liquid.push_back(airs[i].p);
863                                 break;
864                         case LIQUID_NONE:
865                                 // this flow has turned to air; neighboring flows might need to do the same
866                                 for (u16 i = 0; i < num_flows; i++)
867                                         m_transforming_liquid.push_back(flows[i].p);
868                                 break;
869                 }
870         }
871         //infostream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
872
873         for (auto &iter : must_reflow)
874                 m_transforming_liquid.push_back(iter);
875
876         voxalgo::update_lighting_nodes(this, changed_nodes, modified_blocks);
877
878
879         /* ----------------------------------------------------------------------
880          * Manage the queue so that it does not grow indefinately
881          */
882         u16 time_until_purge = g_settings->getU16("liquid_queue_purge_time");
883
884         if (time_until_purge == 0)
885                 return; // Feature disabled
886
887         time_until_purge *= 1000;       // seconds -> milliseconds
888
889         u64 curr_time = porting::getTimeMs();
890         u32 prev_unprocessed = m_unprocessed_count;
891         m_unprocessed_count = m_transforming_liquid.size();
892
893         // if unprocessed block count is decreasing or stable
894         if (m_unprocessed_count <= prev_unprocessed) {
895                 m_queue_size_timer_started = false;
896         } else {
897                 if (!m_queue_size_timer_started)
898                         m_inc_trending_up_start_time = curr_time;
899                 m_queue_size_timer_started = true;
900         }
901
902         // Account for curr_time overflowing
903         if (m_queue_size_timer_started && m_inc_trending_up_start_time > curr_time)
904                 m_queue_size_timer_started = false;
905
906         /* If the queue has been growing for more than liquid_queue_purge_time seconds
907          * and the number of unprocessed blocks is still > liquid_loop_max then we
908          * cannot keep up; dump the oldest blocks from the queue so that the queue
909          * has liquid_loop_max items in it
910          */
911         if (m_queue_size_timer_started
912                         && curr_time - m_inc_trending_up_start_time > time_until_purge
913                         && m_unprocessed_count > liquid_loop_max) {
914
915                 size_t dump_qty = m_unprocessed_count - liquid_loop_max;
916
917                 infostream << "transformLiquids(): DUMPING " << dump_qty
918                            << " blocks from the queue" << std::endl;
919
920                 while (dump_qty--)
921                         m_transforming_liquid.pop_front();
922
923                 m_queue_size_timer_started = false; // optimistically assume we can keep up now
924                 m_unprocessed_count = m_transforming_liquid.size();
925         }
926 }
927
928 std::vector<v3s16> Map::findNodesWithMetadata(v3s16 p1, v3s16 p2)
929 {
930         std::vector<v3s16> positions_with_meta;
931
932         sortBoxVerticies(p1, p2);
933         v3s16 bpmin = getNodeBlockPos(p1);
934         v3s16 bpmax = getNodeBlockPos(p2);
935
936         VoxelArea area(p1, p2);
937
938         for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
939         for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
940         for (s16 x = bpmin.X; x <= bpmax.X; x++) {
941                 v3s16 blockpos(x, y, z);
942
943                 MapBlock *block = getBlockNoCreateNoEx(blockpos);
944                 if (!block) {
945                         verbosestream << "Map::getNodeMetadata(): Need to emerge "
946                                 << PP(blockpos) << std::endl;
947                         block = emergeBlock(blockpos, false);
948                 }
949                 if (!block) {
950                         infostream << "WARNING: Map::getNodeMetadata(): Block not found"
951                                 << std::endl;
952                         continue;
953                 }
954
955                 v3s16 p_base = blockpos * MAP_BLOCKSIZE;
956                 std::vector<v3s16> keys = block->m_node_metadata.getAllKeys();
957                 for (size_t i = 0; i != keys.size(); i++) {
958                         v3s16 p(keys[i] + p_base);
959                         if (!area.contains(p))
960                                 continue;
961
962                         positions_with_meta.push_back(p);
963                 }
964         }
965
966         return positions_with_meta;
967 }
968
969 NodeMetadata *Map::getNodeMetadata(v3s16 p)
970 {
971         v3s16 blockpos = getNodeBlockPos(p);
972         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
973         MapBlock *block = getBlockNoCreateNoEx(blockpos);
974         if(!block){
975                 infostream<<"Map::getNodeMetadata(): Need to emerge "
976                                 <<PP(blockpos)<<std::endl;
977                 block = emergeBlock(blockpos, false);
978         }
979         if(!block){
980                 warningstream<<"Map::getNodeMetadata(): Block not found"
981                                 <<std::endl;
982                 return NULL;
983         }
984         NodeMetadata *meta = block->m_node_metadata.get(p_rel);
985         return meta;
986 }
987
988 bool Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
989 {
990         v3s16 blockpos = getNodeBlockPos(p);
991         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
992         MapBlock *block = getBlockNoCreateNoEx(blockpos);
993         if(!block){
994                 infostream<<"Map::setNodeMetadata(): Need to emerge "
995                                 <<PP(blockpos)<<std::endl;
996                 block = emergeBlock(blockpos, false);
997         }
998         if(!block){
999                 warningstream<<"Map::setNodeMetadata(): Block not found"
1000                                 <<std::endl;
1001                 return false;
1002         }
1003         block->m_node_metadata.set(p_rel, meta);
1004         return true;
1005 }
1006
1007 void Map::removeNodeMetadata(v3s16 p)
1008 {
1009         v3s16 blockpos = getNodeBlockPos(p);
1010         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1011         MapBlock *block = getBlockNoCreateNoEx(blockpos);
1012         if(block == NULL)
1013         {
1014                 warningstream<<"Map::removeNodeMetadata(): Block not found"
1015                                 <<std::endl;
1016                 return;
1017         }
1018         block->m_node_metadata.remove(p_rel);
1019 }
1020
1021 NodeTimer Map::getNodeTimer(v3s16 p)
1022 {
1023         v3s16 blockpos = getNodeBlockPos(p);
1024         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1025         MapBlock *block = getBlockNoCreateNoEx(blockpos);
1026         if(!block){
1027                 infostream<<"Map::getNodeTimer(): Need to emerge "
1028                                 <<PP(blockpos)<<std::endl;
1029                 block = emergeBlock(blockpos, false);
1030         }
1031         if(!block){
1032                 warningstream<<"Map::getNodeTimer(): Block not found"
1033                                 <<std::endl;
1034                 return NodeTimer();
1035         }
1036         NodeTimer t = block->m_node_timers.get(p_rel);
1037         NodeTimer nt(t.timeout, t.elapsed, p);
1038         return nt;
1039 }
1040
1041 void Map::setNodeTimer(const NodeTimer &t)
1042 {
1043         v3s16 p = t.position;
1044         v3s16 blockpos = getNodeBlockPos(p);
1045         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1046         MapBlock *block = getBlockNoCreateNoEx(blockpos);
1047         if(!block){
1048                 infostream<<"Map::setNodeTimer(): Need to emerge "
1049                                 <<PP(blockpos)<<std::endl;
1050                 block = emergeBlock(blockpos, false);
1051         }
1052         if(!block){
1053                 warningstream<<"Map::setNodeTimer(): Block not found"
1054                                 <<std::endl;
1055                 return;
1056         }
1057         NodeTimer nt(t.timeout, t.elapsed, p_rel);
1058         block->m_node_timers.set(nt);
1059 }
1060
1061 void Map::removeNodeTimer(v3s16 p)
1062 {
1063         v3s16 blockpos = getNodeBlockPos(p);
1064         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1065         MapBlock *block = getBlockNoCreateNoEx(blockpos);
1066         if(block == NULL)
1067         {
1068                 warningstream<<"Map::removeNodeTimer(): Block not found"
1069                                 <<std::endl;
1070                 return;
1071         }
1072         block->m_node_timers.remove(p_rel);
1073 }
1074
1075 bool Map::isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac,
1076                 float start_off, float end_off, u32 needed_count)
1077 {
1078         float d0 = (float)BS * p0.getDistanceFrom(p1);
1079         v3s16 u0 = p1 - p0;
1080         v3f uf = v3f(u0.X, u0.Y, u0.Z) * BS;
1081         uf.normalize();
1082         v3f p0f = v3f(p0.X, p0.Y, p0.Z) * BS;
1083         u32 count = 0;
1084         for(float s=start_off; s<d0+end_off; s+=step){
1085                 v3f pf = p0f + uf * s;
1086                 v3s16 p = floatToInt(pf, BS);
1087                 MapNode n = getNodeNoEx(p);
1088                 const ContentFeatures &f = m_nodedef->get(n);
1089                 if(f.drawtype == NDT_NORMAL){
1090                         // not transparent, see ContentFeature::updateTextures
1091                         count++;
1092                         if(count >= needed_count)
1093                                 return true;
1094                 }
1095                 step *= stepfac;
1096         }
1097         return false;
1098 }
1099
1100 bool Map::isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes) {
1101         v3s16 cpn = block->getPos() * MAP_BLOCKSIZE;
1102         cpn += v3s16(MAP_BLOCKSIZE / 2, MAP_BLOCKSIZE / 2, MAP_BLOCKSIZE / 2);
1103         float step = BS * 1;
1104         float stepfac = 1.1;
1105         float startoff = BS * 1;
1106         // The occlusion search of 'isOccluded()' must stop short of the target
1107         // point by distance 'endoff' (end offset) to not enter the target mapblock.
1108         // For the 8 mapblock corners 'endoff' must therefore be the maximum diagonal
1109         // of a mapblock, because we must consider all view angles.
1110         // sqrt(1^2 + 1^2 + 1^2) = 1.732
1111         float endoff = -BS * MAP_BLOCKSIZE * 1.732050807569;
1112         v3s16 spn = cam_pos_nodes;
1113         s16 bs2 = MAP_BLOCKSIZE / 2 + 1;
1114         // to reduce the likelihood of falsely occluded blocks
1115         // require at least two solid blocks
1116         // this is a HACK, we should think of a more precise algorithm
1117         u32 needed_count = 2;
1118
1119         return (
1120                 // For the central point of the mapblock 'endoff' can be halved
1121                 isOccluded(spn, cpn,
1122                         step, stepfac, startoff, endoff / 2.0f, needed_count) &&
1123                 isOccluded(spn, cpn + v3s16(bs2,bs2,bs2),
1124                         step, stepfac, startoff, endoff, needed_count) &&
1125                 isOccluded(spn, cpn + v3s16(bs2,bs2,-bs2),
1126                         step, stepfac, startoff, endoff, needed_count) &&
1127                 isOccluded(spn, cpn + v3s16(bs2,-bs2,bs2),
1128                         step, stepfac, startoff, endoff, needed_count) &&
1129                 isOccluded(spn, cpn + v3s16(bs2,-bs2,-bs2),
1130                         step, stepfac, startoff, endoff, needed_count) &&
1131                 isOccluded(spn, cpn + v3s16(-bs2,bs2,bs2),
1132                         step, stepfac, startoff, endoff, needed_count) &&
1133                 isOccluded(spn, cpn + v3s16(-bs2,bs2,-bs2),
1134                         step, stepfac, startoff, endoff, needed_count) &&
1135                 isOccluded(spn, cpn + v3s16(-bs2,-bs2,bs2),
1136                         step, stepfac, startoff, endoff, needed_count) &&
1137                 isOccluded(spn, cpn + v3s16(-bs2,-bs2,-bs2),
1138                         step, stepfac, startoff, endoff, needed_count));
1139 }
1140
1141 /*
1142         ServerMap
1143 */
1144 ServerMap::ServerMap(const std::string &savedir, IGameDef *gamedef,
1145                 EmergeManager *emerge):
1146         Map(dout_server, gamedef),
1147         settings_mgr(g_settings, savedir + DIR_DELIM + "map_meta.txt"),
1148         m_emerge(emerge)
1149 {
1150         verbosestream<<FUNCTION_NAME<<std::endl;
1151
1152         // Tell the EmergeManager about our MapSettingsManager
1153         emerge->map_settings_mgr = &settings_mgr;
1154
1155         /*
1156                 Try to load map; if not found, create a new one.
1157         */
1158
1159         // Determine which database backend to use
1160         std::string conf_path = savedir + DIR_DELIM + "world.mt";
1161         Settings conf;
1162         bool succeeded = conf.readConfigFile(conf_path.c_str());
1163         if (!succeeded || !conf.exists("backend")) {
1164                 // fall back to sqlite3
1165                 conf.set("backend", "sqlite3");
1166         }
1167         std::string backend = conf.get("backend");
1168         dbase = createDatabase(backend, savedir, conf);
1169
1170         if (!conf.updateConfigFile(conf_path.c_str()))
1171                 errorstream << "ServerMap::ServerMap(): Failed to update world.mt!" << std::endl;
1172
1173         m_savedir = savedir;
1174         m_map_saving_enabled = false;
1175
1176         try {
1177                 // If directory exists, check contents and load if possible
1178                 if (fs::PathExists(m_savedir)) {
1179                         // If directory is empty, it is safe to save into it.
1180                         if (fs::GetDirListing(m_savedir).empty()) {
1181                                 infostream<<"ServerMap: Empty save directory is valid."
1182                                                 <<std::endl;
1183                                 m_map_saving_enabled = true;
1184                         }
1185                         else
1186                         {
1187
1188                                 if (settings_mgr.loadMapMeta()) {
1189                                         infostream << "ServerMap: Metadata loaded from "
1190                                                 << savedir << std::endl;
1191                                 } else {
1192                                         infostream << "ServerMap: Metadata could not be loaded "
1193                                                 "from " << savedir << ", assuming valid save "
1194                                                 "directory." << std::endl;
1195                                 }
1196
1197                                 m_map_saving_enabled = true;
1198                                 // Map loaded, not creating new one
1199                                 return;
1200                         }
1201                 }
1202                 // If directory doesn't exist, it is safe to save to it
1203                 else{
1204                         m_map_saving_enabled = true;
1205                 }
1206         }
1207         catch(std::exception &e)
1208         {
1209                 warningstream<<"ServerMap: Failed to load map from "<<savedir
1210                                 <<", exception: "<<e.what()<<std::endl;
1211                 infostream<<"Please remove the map or fix it."<<std::endl;
1212                 warningstream<<"Map saving will be disabled."<<std::endl;
1213         }
1214
1215         infostream<<"Initializing new map."<<std::endl;
1216
1217         // Create zero sector
1218         emergeSector(v2s16(0,0));
1219
1220         // Initially write whole map
1221         save(MOD_STATE_CLEAN);
1222 }
1223
1224 ServerMap::~ServerMap()
1225 {
1226         verbosestream<<FUNCTION_NAME<<std::endl;
1227
1228         try
1229         {
1230                 if(m_map_saving_enabled)
1231                 {
1232                         // Save only changed parts
1233                         save(MOD_STATE_WRITE_AT_UNLOAD);
1234                         infostream<<"ServerMap: Saved map to "<<m_savedir<<std::endl;
1235                 }
1236                 else
1237                 {
1238                         infostream<<"ServerMap: Map not saved"<<std::endl;
1239                 }
1240         }
1241         catch(std::exception &e)
1242         {
1243                 infostream<<"ServerMap: Failed to save map to "<<m_savedir
1244                                 <<", exception: "<<e.what()<<std::endl;
1245         }
1246
1247         /*
1248                 Close database if it was opened
1249         */
1250         delete dbase;
1251
1252 #if 0
1253         /*
1254                 Free all MapChunks
1255         */
1256         core::map<v2s16, MapChunk*>::Iterator i = m_chunks.getIterator();
1257         for(; i.atEnd() == false; i++)
1258         {
1259                 MapChunk *chunk = i.getNode()->getValue();
1260                 delete chunk;
1261         }
1262 #endif
1263 }
1264
1265 MapgenParams *ServerMap::getMapgenParams()
1266 {
1267         // getMapgenParams() should only ever be called after Server is initialized
1268         assert(settings_mgr.mapgen_params != NULL);
1269         return settings_mgr.mapgen_params;
1270 }
1271
1272 u64 ServerMap::getSeed()
1273 {
1274         return getMapgenParams()->seed;
1275 }
1276
1277 s16 ServerMap::getWaterLevel()
1278 {
1279         return getMapgenParams()->water_level;
1280 }
1281
1282 bool ServerMap::saoPositionOverLimit(const v3f &p)
1283 {
1284         return getMapgenParams()->saoPosOverLimit(p);
1285 }
1286
1287 bool ServerMap::blockpos_over_mapgen_limit(v3s16 p)
1288 {
1289         const s16 mapgen_limit_bp = rangelim(
1290                 getMapgenParams()->mapgen_limit, 0, MAX_MAP_GENERATION_LIMIT) /
1291                 MAP_BLOCKSIZE;
1292         return p.X < -mapgen_limit_bp ||
1293                 p.X >  mapgen_limit_bp ||
1294                 p.Y < -mapgen_limit_bp ||
1295                 p.Y >  mapgen_limit_bp ||
1296                 p.Z < -mapgen_limit_bp ||
1297                 p.Z >  mapgen_limit_bp;
1298 }
1299
1300 bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data)
1301 {
1302         s16 csize = getMapgenParams()->chunksize;
1303         v3s16 bpmin = EmergeManager::getContainingChunk(blockpos, csize);
1304         v3s16 bpmax = bpmin + v3s16(1, 1, 1) * (csize - 1);
1305
1306         bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1307         EMERGE_DBG_OUT("initBlockMake(): " PP(bpmin) " - " PP(bpmax));
1308
1309         v3s16 extra_borders(1, 1, 1);
1310         v3s16 full_bpmin = bpmin - extra_borders;
1311         v3s16 full_bpmax = bpmax + extra_borders;
1312
1313         // Do nothing if not inside mapgen limits (+-1 because of neighbors)
1314         if (blockpos_over_mapgen_limit(full_bpmin) ||
1315                         blockpos_over_mapgen_limit(full_bpmax))
1316                 return false;
1317
1318         data->seed = getSeed();
1319         data->blockpos_min = bpmin;
1320         data->blockpos_max = bpmax;
1321         data->blockpos_requested = blockpos;
1322         data->nodedef = m_nodedef;
1323
1324         /*
1325                 Create the whole area of this and the neighboring blocks
1326         */
1327         for (s16 x = full_bpmin.X; x <= full_bpmax.X; x++)
1328         for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++) {
1329                 v2s16 sectorpos(x, z);
1330                 // Sector metadata is loaded from disk if not already loaded.
1331                 ServerMapSector *sector = createSector(sectorpos);
1332                 FATAL_ERROR_IF(sector == NULL, "createSector() failed");
1333
1334                 for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) {
1335                         v3s16 p(x, y, z);
1336
1337                         MapBlock *block = emergeBlock(p, false);
1338                         if (block == NULL) {
1339                                 block = createBlock(p);
1340
1341                                 // Block gets sunlight if this is true.
1342                                 // Refer to the map generator heuristics.
1343                                 bool ug = m_emerge->isBlockUnderground(p);
1344                                 block->setIsUnderground(ug);
1345                         }
1346                 }
1347         }
1348
1349         /*
1350                 Now we have a big empty area.
1351
1352                 Make a ManualMapVoxelManipulator that contains this and the
1353                 neighboring blocks
1354         */
1355
1356         data->vmanip = new MMVManip(this);
1357         data->vmanip->initialEmerge(full_bpmin, full_bpmax);
1358
1359         // Note: we may need this again at some point.
1360 #if 0
1361         // Ensure none of the blocks to be generated were marked as
1362         // containing CONTENT_IGNORE
1363         for (s16 z = blockpos_min.Z; z <= blockpos_max.Z; z++) {
1364                 for (s16 y = blockpos_min.Y; y <= blockpos_max.Y; y++) {
1365                         for (s16 x = blockpos_min.X; x <= blockpos_max.X; x++) {
1366                                 core::map<v3s16, u8>::Node *n;
1367                                 n = data->vmanip->m_loaded_blocks.find(v3s16(x, y, z));
1368                                 if (n == NULL)
1369                                         continue;
1370                                 u8 flags = n->getValue();
1371                                 flags &= ~VMANIP_BLOCK_CONTAINS_CIGNORE;
1372                                 n->setValue(flags);
1373                         }
1374                 }
1375         }
1376 #endif
1377
1378         // Data is ready now.
1379         return true;
1380 }
1381
1382 void ServerMap::finishBlockMake(BlockMakeData *data,
1383         std::map<v3s16, MapBlock*> *changed_blocks)
1384 {
1385         v3s16 bpmin = data->blockpos_min;
1386         v3s16 bpmax = data->blockpos_max;
1387
1388         v3s16 extra_borders(1, 1, 1);
1389
1390         bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1391         EMERGE_DBG_OUT("finishBlockMake(): " PP(bpmin) " - " PP(bpmax));
1392
1393         /*
1394                 Blit generated stuff to map
1395                 NOTE: blitBackAll adds nearly everything to changed_blocks
1396         */
1397         data->vmanip->blitBackAll(changed_blocks);
1398
1399         EMERGE_DBG_OUT("finishBlockMake: changed_blocks.size()="
1400                 << changed_blocks->size());
1401
1402         /*
1403                 Copy transforming liquid information
1404         */
1405         while (data->transforming_liquid.size()) {
1406                 m_transforming_liquid.push_back(data->transforming_liquid.front());
1407                 data->transforming_liquid.pop_front();
1408         }
1409
1410         for (auto &changed_block : *changed_blocks) {
1411                 MapBlock *block = changed_block.second;
1412                 if (!block)
1413                         continue;
1414                 /*
1415                         Update day/night difference cache of the MapBlocks
1416                 */
1417                 block->expireDayNightDiff();
1418                 /*
1419                         Set block as modified
1420                 */
1421                 block->raiseModified(MOD_STATE_WRITE_NEEDED,
1422                         MOD_REASON_EXPIRE_DAYNIGHTDIFF);
1423         }
1424
1425         /*
1426                 Set central blocks as generated
1427         */
1428         for (s16 x = bpmin.X; x <= bpmax.X; x++)
1429         for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
1430         for (s16 y = bpmin.Y; y <= bpmax.Y; y++) {
1431                 MapBlock *block = getBlockNoCreateNoEx(v3s16(x, y, z));
1432                 if (!block)
1433                         continue;
1434
1435                 block->setGenerated(true);
1436         }
1437
1438         /*
1439                 Save changed parts of map
1440                 NOTE: Will be saved later.
1441         */
1442         //save(MOD_STATE_WRITE_AT_UNLOAD);
1443 }
1444
1445 ServerMapSector *ServerMap::createSector(v2s16 p2d)
1446 {
1447         DSTACKF("%s: p2d=(%d,%d)",
1448                         FUNCTION_NAME,
1449                         p2d.X, p2d.Y);
1450
1451         /*
1452                 Check if it exists already in memory
1453         */
1454         ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1455         if(sector != NULL)
1456                 return sector;
1457
1458         /*
1459                 Try to load it from disk (with blocks)
1460         */
1461         //if(loadSectorFull(p2d) == true)
1462
1463         /*
1464                 Try to load metadata from disk
1465         */
1466 #if 0
1467         if(loadSectorMeta(p2d) == true)
1468         {
1469                 ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1470                 if(sector == NULL)
1471                 {
1472                         infostream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
1473                         throw InvalidPositionException("");
1474                 }
1475                 return sector;
1476         }
1477 #endif
1478
1479         /*
1480                 Do not create over max mapgen limit
1481         */
1482         const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
1483         if (p2d.X < -max_limit_bp ||
1484                         p2d.X >  max_limit_bp ||
1485                         p2d.Y < -max_limit_bp ||
1486                         p2d.Y >  max_limit_bp)
1487                 throw InvalidPositionException("createSector(): pos. over max mapgen limit");
1488
1489         /*
1490                 Generate blank sector
1491         */
1492
1493         sector = new ServerMapSector(this, p2d, m_gamedef);
1494
1495         // Sector position on map in nodes
1496         //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
1497
1498         /*
1499                 Insert to container
1500         */
1501         m_sectors[p2d] = sector;
1502
1503         return sector;
1504 }
1505
1506 #if 0
1507 /*
1508         This is a quick-hand function for calling makeBlock().
1509 */
1510 MapBlock * ServerMap::generateBlock(
1511                 v3s16 p,
1512                 std::map<v3s16, MapBlock*> &modified_blocks
1513 )
1514 {
1515         DSTACKF("%s: p=(%d,%d,%d)", FUNCTION_NAME, p.X, p.Y, p.Z);
1516
1517         /*infostream<<"generateBlock(): "
1518                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1519                         <<std::endl;*/
1520
1521         bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
1522
1523         TimeTaker timer("generateBlock");
1524
1525         //MapBlock *block = original_dummy;
1526
1527         v2s16 p2d(p.X, p.Z);
1528         v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
1529
1530         /*
1531                 Do not generate over-limit
1532         */
1533         if(blockpos_over_limit(p))
1534         {
1535                 infostream<<FUNCTION_NAME<<": Block position over limit"<<std::endl;
1536                 throw InvalidPositionException("generateBlock(): pos. over limit");
1537         }
1538
1539         /*
1540                 Create block make data
1541         */
1542         BlockMakeData data;
1543         initBlockMake(&data, p);
1544
1545         /*
1546                 Generate block
1547         */
1548         {
1549                 TimeTaker t("mapgen::make_block()");
1550                 mapgen->makeChunk(&data);
1551                 //mapgen::make_block(&data);
1552
1553                 if(enable_mapgen_debug_info == false)
1554                         t.stop(true); // Hide output
1555         }
1556
1557         /*
1558                 Blit data back on map, update lighting, add mobs and whatever this does
1559         */
1560         finishBlockMake(&data, modified_blocks);
1561
1562         /*
1563                 Get central block
1564         */
1565         MapBlock *block = getBlockNoCreateNoEx(p);
1566
1567 #if 0
1568         /*
1569                 Check result
1570         */
1571         if(block)
1572         {
1573                 bool erroneus_content = false;
1574                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1575                 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1576                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1577                 {
1578                         v3s16 p(x0,y0,z0);
1579                         MapNode n = block->getNode(p);
1580                         if(n.getContent() == CONTENT_IGNORE)
1581                         {
1582                                 infostream<<"CONTENT_IGNORE at "
1583                                                 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1584                                                 <<std::endl;
1585                                 erroneus_content = true;
1586                                 assert(0);
1587                         }
1588                 }
1589                 if(erroneus_content)
1590                 {
1591                         assert(0);
1592                 }
1593         }
1594 #endif
1595
1596 #if 0
1597         /*
1598                 Generate a completely empty block
1599         */
1600         if(block)
1601         {
1602                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1603                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1604                 {
1605                         for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1606                         {
1607                                 MapNode n;
1608                                 n.setContent(CONTENT_AIR);
1609                                 block->setNode(v3s16(x0,y0,z0), n);
1610                         }
1611                 }
1612         }
1613 #endif
1614
1615         if(enable_mapgen_debug_info == false)
1616                 timer.stop(true); // Hide output
1617
1618         return block;
1619 }
1620 #endif
1621
1622 MapBlock * ServerMap::createBlock(v3s16 p)
1623 {
1624         DSTACKF("%s: p=(%d,%d,%d)",
1625                         FUNCTION_NAME, p.X, p.Y, p.Z);
1626
1627         /*
1628                 Do not create over max mapgen limit
1629         */
1630         if (blockpos_over_max_limit(p))
1631                 throw InvalidPositionException("createBlock(): pos. over max mapgen limit");
1632
1633         v2s16 p2d(p.X, p.Z);
1634         s16 block_y = p.Y;
1635         /*
1636                 This will create or load a sector if not found in memory.
1637                 If block exists on disk, it will be loaded.
1638
1639                 NOTE: On old save formats, this will be slow, as it generates
1640                       lighting on blocks for them.
1641         */
1642         ServerMapSector *sector;
1643         try {
1644                 sector = (ServerMapSector*)createSector(p2d);
1645                 assert(sector->getId() == MAPSECTOR_SERVER);
1646         }
1647         catch(InvalidPositionException &e)
1648         {
1649                 infostream<<"createBlock: createSector() failed"<<std::endl;
1650                 throw e;
1651         }
1652         /*
1653                 NOTE: This should not be done, or at least the exception
1654                 should not be passed on as std::exception, because it
1655                 won't be catched at all.
1656         */
1657         /*catch(std::exception &e)
1658         {
1659                 infostream<<"createBlock: createSector() failed: "
1660                                 <<e.what()<<std::endl;
1661                 throw e;
1662         }*/
1663
1664         /*
1665                 Try to get a block from the sector
1666         */
1667
1668         MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
1669         if(block)
1670         {
1671                 if(block->isDummy())
1672                         block->unDummify();
1673                 return block;
1674         }
1675         // Create blank
1676         block = sector->createBlankBlock(block_y);
1677
1678         return block;
1679 }
1680
1681 MapBlock * ServerMap::emergeBlock(v3s16 p, bool create_blank)
1682 {
1683         DSTACKF("%s: p=(%d,%d,%d), create_blank=%d",
1684                         FUNCTION_NAME,
1685                         p.X, p.Y, p.Z, create_blank);
1686
1687         {
1688                 MapBlock *block = getBlockNoCreateNoEx(p);
1689                 if (block && !block->isDummy())
1690                         return block;
1691         }
1692
1693         {
1694                 MapBlock *block = loadBlock(p);
1695                 if(block)
1696                         return block;
1697         }
1698
1699         if (create_blank) {
1700                 ServerMapSector *sector = createSector(v2s16(p.X, p.Z));
1701                 MapBlock *block = sector->createBlankBlock(p.Y);
1702
1703                 return block;
1704         }
1705
1706 #if 0
1707         if(allow_generate)
1708         {
1709                 std::map<v3s16, MapBlock*> modified_blocks;
1710                 MapBlock *block = generateBlock(p, modified_blocks);
1711                 if(block)
1712                 {
1713                         MapEditEvent event;
1714                         event.type = MEET_OTHER;
1715                         event.p = p;
1716
1717                         // Copy modified_blocks to event
1718                         for(std::map<v3s16, MapBlock*>::iterator
1719                                         i = modified_blocks.begin();
1720                                         i != modified_blocks.end(); ++i)
1721                         {
1722                                 event.modified_blocks.insert(i->first);
1723                         }
1724
1725                         // Queue event
1726                         dispatchEvent(&event);
1727
1728                         return block;
1729                 }
1730         }
1731 #endif
1732
1733         return NULL;
1734 }
1735
1736 MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
1737 {
1738         MapBlock *block = getBlockNoCreateNoEx(p3d);
1739         if (block == NULL)
1740                 m_emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, p3d, false);
1741
1742         return block;
1743 }
1744
1745 // N.B.  This requires no synchronization, since data will not be modified unless
1746 // the VoxelManipulator being updated belongs to the same thread.
1747 void ServerMap::updateVManip(v3s16 pos)
1748 {
1749         Mapgen *mg = m_emerge->getCurrentMapgen();
1750         if (!mg)
1751                 return;
1752
1753         MMVManip *vm = mg->vm;
1754         if (!vm)
1755                 return;
1756
1757         if (!vm->m_area.contains(pos))
1758                 return;
1759
1760         s32 idx = vm->m_area.index(pos);
1761         vm->m_data[idx] = getNodeNoEx(pos);
1762         vm->m_flags[idx] &= ~VOXELFLAG_NO_DATA;
1763
1764         vm->m_is_dirty = true;
1765 }
1766
1767 s16 ServerMap::findGroundLevel(v2s16 p2d)
1768 {
1769 #if 0
1770         /*
1771                 Uh, just do something random...
1772         */
1773         // Find existing map from top to down
1774         s16 max=63;
1775         s16 min=-64;
1776         v3s16 p(p2d.X, max, p2d.Y);
1777         for(; p.Y>min; p.Y--)
1778         {
1779                 MapNode n = getNodeNoEx(p);
1780                 if(n.getContent() != CONTENT_IGNORE)
1781                         break;
1782         }
1783         if(p.Y == min)
1784                 goto plan_b;
1785         // If this node is not air, go to plan b
1786         if(getNodeNoEx(p).getContent() != CONTENT_AIR)
1787                 goto plan_b;
1788         // Search existing walkable and return it
1789         for(; p.Y>min; p.Y--)
1790         {
1791                 MapNode n = getNodeNoEx(p);
1792                 if(content_walkable(n.d) && n.getContent() != CONTENT_IGNORE)
1793                         return p.Y;
1794         }
1795
1796         // Move to plan b
1797 plan_b:
1798 #endif
1799
1800         /*
1801                 Determine from map generator noise functions
1802         */
1803
1804         s16 level = m_emerge->getGroundLevelAtPoint(p2d);
1805         return level;
1806
1807         //double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
1808         //return (s16)level;
1809 }
1810
1811 bool ServerMap::loadFromFolders() {
1812         if (!dbase->initialized() &&
1813                         !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
1814                 return true;
1815         return false;
1816 }
1817
1818 void ServerMap::createDirs(std::string path)
1819 {
1820         if (!fs::CreateAllDirs(path)) {
1821                 m_dout<<"ServerMap: Failed to create directory "
1822                                 <<"\""<<path<<"\""<<std::endl;
1823                 throw BaseException("ServerMap failed to create directory");
1824         }
1825 }
1826
1827 std::string ServerMap::getSectorDir(v2s16 pos, int layout)
1828 {
1829         char cc[9];
1830         switch(layout)
1831         {
1832                 case 1:
1833                         snprintf(cc, 9, "%.4x%.4x",
1834                                 (unsigned int) pos.X & 0xffff,
1835                                 (unsigned int) pos.Y & 0xffff);
1836
1837                         return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
1838                 case 2:
1839                         snprintf(cc, 9, (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
1840                                 (unsigned int) pos.X & 0xfff,
1841                                 (unsigned int) pos.Y & 0xfff);
1842
1843                         return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc;
1844                 default:
1845                         assert(false);
1846                         return "";
1847         }
1848 }
1849
1850 v2s16 ServerMap::getSectorPos(const std::string &dirname)
1851 {
1852         unsigned int x = 0, y = 0;
1853         int r;
1854         std::string component;
1855         fs::RemoveLastPathComponent(dirname, &component, 1);
1856         if(component.size() == 8)
1857         {
1858                 // Old layout
1859                 r = sscanf(component.c_str(), "%4x%4x", &x, &y);
1860         }
1861         else if(component.size() == 3)
1862         {
1863                 // New layout
1864                 fs::RemoveLastPathComponent(dirname, &component, 2);
1865                 r = sscanf(component.c_str(), (std::string("%3x") + DIR_DELIM + "%3x").c_str(), &x, &y);
1866                 // Sign-extend the 12 bit values up to 16 bits...
1867                 if(x & 0x800) x |= 0xF000;
1868                 if(y & 0x800) y |= 0xF000;
1869         }
1870         else
1871         {
1872                 r = -1;
1873         }
1874
1875         FATAL_ERROR_IF(r != 2, "getSectorPos()");
1876         v2s16 pos((s16)x, (s16)y);
1877         return pos;
1878 }
1879
1880 v3s16 ServerMap::getBlockPos(const std::string &sectordir, const std::string &blockfile)
1881 {
1882         v2s16 p2d = getSectorPos(sectordir);
1883
1884         if(blockfile.size() != 4){
1885                 throw InvalidFilenameException("Invalid block filename");
1886         }
1887         unsigned int y;
1888         int r = sscanf(blockfile.c_str(), "%4x", &y);
1889         if(r != 1)
1890                 throw InvalidFilenameException("Invalid block filename");
1891         return v3s16(p2d.X, y, p2d.Y);
1892 }
1893
1894 std::string ServerMap::getBlockFilename(v3s16 p)
1895 {
1896         char cc[5];
1897         snprintf(cc, 5, "%.4x", (unsigned int)p.Y&0xffff);
1898         return cc;
1899 }
1900
1901 void ServerMap::save(ModifiedState save_level)
1902 {
1903         DSTACK(FUNCTION_NAME);
1904         if (!m_map_saving_enabled) {
1905                 warningstream<<"Not saving map, saving disabled."<<std::endl;
1906                 return;
1907         }
1908
1909         if(save_level == MOD_STATE_CLEAN)
1910                 infostream<<"ServerMap: Saving whole map, this can take time."
1911                                 <<std::endl;
1912
1913         if (m_map_metadata_changed || save_level == MOD_STATE_CLEAN) {
1914                 if (settings_mgr.saveMapMeta())
1915                         m_map_metadata_changed = false;
1916         }
1917
1918         // Profile modified reasons
1919         Profiler modprofiler;
1920
1921         u32 sector_meta_count = 0;
1922         u32 block_count = 0;
1923         u32 block_count_all = 0; // Number of blocks in memory
1924
1925         // Don't do anything with sqlite unless something is really saved
1926         bool save_started = false;
1927
1928         for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
1929                 i != m_sectors.end(); ++i) {
1930                 ServerMapSector *sector = (ServerMapSector*)i->second;
1931                 assert(sector->getId() == MAPSECTOR_SERVER);
1932
1933                 if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
1934                         saveSectorMeta(sector);
1935                         sector_meta_count++;
1936                 }
1937
1938                 MapBlockVect blocks;
1939                 sector->getBlocks(blocks);
1940
1941                 for (MapBlock *block : blocks) {
1942                         block_count_all++;
1943
1944                         if(block->getModified() >= (u32)save_level) {
1945                                 // Lazy beginSave()
1946                                 if(!save_started) {
1947                                         beginSave();
1948                                         save_started = true;
1949                                 }
1950
1951                                 modprofiler.add(block->getModifiedReasonString(), 1);
1952
1953                                 saveBlock(block);
1954                                 block_count++;
1955
1956                                 /*infostream<<"ServerMap: Written block ("
1957                                                 <<block->getPos().X<<","
1958                                                 <<block->getPos().Y<<","
1959                                                 <<block->getPos().Z<<")"
1960                                                 <<std::endl;*/
1961                         }
1962                 }
1963         }
1964
1965         if(save_started)
1966                 endSave();
1967
1968         /*
1969                 Only print if something happened or saved whole map
1970         */
1971         if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
1972                         || block_count != 0) {
1973                 infostream<<"ServerMap: Written: "
1974                                 <<sector_meta_count<<" sector metadata files, "
1975                                 <<block_count<<" block files"
1976                                 <<", "<<block_count_all<<" blocks in memory."
1977                                 <<std::endl;
1978                 PrintInfo(infostream); // ServerMap/ClientMap:
1979                 infostream<<"Blocks modified by: "<<std::endl;
1980                 modprofiler.print(infostream);
1981         }
1982 }
1983
1984 void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
1985 {
1986         if (loadFromFolders()) {
1987                 errorstream << "Map::listAllLoadableBlocks(): Result will be missing "
1988                                 << "all blocks that are stored in flat files." << std::endl;
1989         }
1990         dbase->listAllLoadableBlocks(dst);
1991 }
1992
1993 void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
1994 {
1995         for (auto &sector_it : m_sectors) {
1996                 MapSector *sector = sector_it.second;
1997
1998                 MapBlockVect blocks;
1999                 sector->getBlocks(blocks);
2000
2001                 for (MapBlock *block : blocks) {
2002                         v3s16 p = block->getPos();
2003                         dst.push_back(p);
2004                 }
2005         }
2006 }
2007
2008 void ServerMap::saveSectorMeta(ServerMapSector *sector)
2009 {
2010         DSTACK(FUNCTION_NAME);
2011         // Format used for writing
2012         u8 version = SER_FMT_VER_HIGHEST_WRITE;
2013         // Get destination
2014         v2s16 pos = sector->getPos();
2015         std::string dir = getSectorDir(pos);
2016         createDirs(dir);
2017
2018         std::string fullpath = dir + DIR_DELIM + "meta";
2019         std::ostringstream ss(std::ios_base::binary);
2020
2021         sector->serialize(ss, version);
2022
2023         if(!fs::safeWriteToFile(fullpath, ss.str()))
2024                 throw FileNotGoodException("Cannot write sector metafile");
2025
2026         sector->differs_from_disk = false;
2027 }
2028
2029 MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
2030 {
2031         DSTACK(FUNCTION_NAME);
2032         // Get destination
2033         v2s16 p2d = getSectorPos(sectordir);
2034
2035         ServerMapSector *sector = NULL;
2036
2037         std::string fullpath = sectordir + DIR_DELIM + "meta";
2038         std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2039         if (!is.good()) {
2040                 // If the directory exists anyway, it probably is in some old
2041                 // format. Just go ahead and create the sector.
2042                 if(fs::PathExists(sectordir))
2043                 {
2044                         /*infostream<<"ServerMap::loadSectorMeta(): Sector metafile "
2045                                         <<fullpath<<" doesn't exist but directory does."
2046                                         <<" Continuing with a sector with no metadata."
2047                                         <<std::endl;*/
2048                         sector = new ServerMapSector(this, p2d, m_gamedef);
2049                         m_sectors[p2d] = sector;
2050                 }
2051                 else
2052                 {
2053                         throw FileNotGoodException("Cannot open sector metafile");
2054                 }
2055         }
2056         else
2057         {
2058                 sector = ServerMapSector::deSerialize
2059                                 (is, this, p2d, m_sectors, m_gamedef);
2060                 if(save_after_load)
2061                         saveSectorMeta(sector);
2062         }
2063
2064         sector->differs_from_disk = false;
2065
2066         return sector;
2067 }
2068
2069 bool ServerMap::loadSectorMeta(v2s16 p2d)
2070 {
2071         DSTACK(FUNCTION_NAME);
2072
2073         // The directory layout we're going to load from.
2074         //  1 - original sectors/xxxxzzzz/
2075         //  2 - new sectors2/xxx/zzz/
2076         //  If we load from anything but the latest structure, we will
2077         //  immediately save to the new one, and remove the old.
2078         int loadlayout = 1;
2079         std::string sectordir1 = getSectorDir(p2d, 1);
2080         std::string sectordir;
2081         if(fs::PathExists(sectordir1))
2082         {
2083                 sectordir = sectordir1;
2084         }
2085         else
2086         {
2087                 loadlayout = 2;
2088                 sectordir = getSectorDir(p2d, 2);
2089         }
2090
2091         try{
2092                 loadSectorMeta(sectordir, loadlayout != 2);
2093         }
2094         catch(InvalidFilenameException &e)
2095         {
2096                 return false;
2097         }
2098         catch(FileNotGoodException &e)
2099         {
2100                 return false;
2101         }
2102         catch(std::exception &e)
2103         {
2104                 return false;
2105         }
2106
2107         return true;
2108 }
2109
2110 #if 0
2111 bool ServerMap::loadSectorFull(v2s16 p2d)
2112 {
2113         DSTACK(FUNCTION_NAME);
2114
2115         MapSector *sector = NULL;
2116
2117         // The directory layout we're going to load from.
2118         //  1 - original sectors/xxxxzzzz/
2119         //  2 - new sectors2/xxx/zzz/
2120         //  If we load from anything but the latest structure, we will
2121         //  immediately save to the new one, and remove the old.
2122         int loadlayout = 1;
2123         std::string sectordir1 = getSectorDir(p2d, 1);
2124         std::string sectordir;
2125         if(fs::PathExists(sectordir1))
2126         {
2127                 sectordir = sectordir1;
2128         }
2129         else
2130         {
2131                 loadlayout = 2;
2132                 sectordir = getSectorDir(p2d, 2);
2133         }
2134
2135         try{
2136                 sector = loadSectorMeta(sectordir, loadlayout != 2);
2137         }
2138         catch(InvalidFilenameException &e)
2139         {
2140                 return false;
2141         }
2142         catch(FileNotGoodException &e)
2143         {
2144                 return false;
2145         }
2146         catch(std::exception &e)
2147         {
2148                 return false;
2149         }
2150
2151         /*
2152                 Load blocks
2153         */
2154         std::vector<fs::DirListNode> list2 = fs::GetDirListing
2155                         (sectordir);
2156         std::vector<fs::DirListNode>::iterator i2;
2157         for(i2=list2.begin(); i2!=list2.end(); i2++)
2158         {
2159                 // We want files
2160                 if(i2->dir)
2161                         continue;
2162                 try{
2163                         loadBlock(sectordir, i2->name, sector, loadlayout != 2);
2164                 }
2165                 catch(InvalidFilenameException &e)
2166                 {
2167                         // This catches unknown crap in directory
2168                 }
2169         }
2170
2171         if(loadlayout != 2)
2172         {
2173                 infostream<<"Sector converted to new layout - deleting "<<
2174                         sectordir1<<std::endl;
2175                 fs::RecursiveDelete(sectordir1);
2176         }
2177
2178         return true;
2179 }
2180 #endif
2181
2182 MapDatabase *ServerMap::createDatabase(
2183         const std::string &name,
2184         const std::string &savedir,
2185         Settings &conf)
2186 {
2187         if (name == "sqlite3")
2188                 return new MapDatabaseSQLite3(savedir);
2189         if (name == "dummy")
2190                 return new Database_Dummy();
2191         #if USE_LEVELDB
2192         if (name == "leveldb")
2193                 return new Database_LevelDB(savedir);
2194         #endif
2195         #if USE_REDIS
2196         if (name == "redis")
2197                 return new Database_Redis(conf);
2198         #endif
2199         #if USE_POSTGRESQL
2200         if (name == "postgresql") {
2201                 std::string connect_string;
2202                 conf.getNoEx("pgsql_connection", connect_string);
2203                 return new MapDatabasePostgreSQL(connect_string);
2204         }
2205         #endif
2206
2207         throw BaseException(std::string("Database backend ") + name + " not supported.");
2208 }
2209
2210 void ServerMap::beginSave()
2211 {
2212         dbase->beginSave();
2213 }
2214
2215 void ServerMap::endSave()
2216 {
2217         dbase->endSave();
2218 }
2219
2220 bool ServerMap::saveBlock(MapBlock *block)
2221 {
2222         return saveBlock(block, dbase);
2223 }
2224
2225 bool ServerMap::saveBlock(MapBlock *block, MapDatabase *db)
2226 {
2227         v3s16 p3d = block->getPos();
2228
2229         // Dummy blocks are not written
2230         if (block->isDummy()) {
2231                 warningstream << "saveBlock: Not writing dummy block "
2232                         << PP(p3d) << std::endl;
2233                 return true;
2234         }
2235
2236         // Format used for writing
2237         u8 version = SER_FMT_VER_HIGHEST_WRITE;
2238
2239         /*
2240                 [0] u8 serialization version
2241                 [1] data
2242         */
2243         std::ostringstream o(std::ios_base::binary);
2244         o.write((char*) &version, 1);
2245         block->serialize(o, version, true);
2246
2247         std::string data = o.str();
2248         bool ret = db->saveBlock(p3d, data);
2249         if (ret) {
2250                 // We just wrote it to the disk so clear modified flag
2251                 block->resetModified();
2252         }
2253         return ret;
2254 }
2255
2256 void ServerMap::loadBlock(const std::string &sectordir, const std::string &blockfile,
2257                 MapSector *sector, bool save_after_load)
2258 {
2259         DSTACK(FUNCTION_NAME);
2260
2261         std::string fullpath = sectordir + DIR_DELIM + blockfile;
2262         try {
2263                 std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2264                 if (!is.good())
2265                         throw FileNotGoodException("Cannot open block file");
2266
2267                 v3s16 p3d = getBlockPos(sectordir, blockfile);
2268                 v2s16 p2d(p3d.X, p3d.Z);
2269
2270                 assert(sector->getPos() == p2d);
2271
2272                 u8 version = SER_FMT_VER_INVALID;
2273                 is.read((char*)&version, 1);
2274
2275                 if(is.fail())
2276                         throw SerializationError("ServerMap::loadBlock(): Failed"
2277                                         " to read MapBlock version");
2278
2279                 /*u32 block_size = MapBlock::serializedLength(version);
2280                 SharedBuffer<u8> data(block_size);
2281                 is.read((char*)*data, block_size);*/
2282
2283                 // This will always return a sector because we're the server
2284                 //MapSector *sector = emergeSector(p2d);
2285
2286                 MapBlock *block = NULL;
2287                 bool created_new = false;
2288                 block = sector->getBlockNoCreateNoEx(p3d.Y);
2289                 if(block == NULL)
2290                 {
2291                         block = sector->createBlankBlockNoInsert(p3d.Y);
2292                         created_new = true;
2293                 }
2294
2295                 // Read basic data
2296                 block->deSerialize(is, version, true);
2297
2298                 // If it's a new block, insert it to the map
2299                 if (created_new) {
2300                         sector->insertBlock(block);
2301                         ReflowScan scanner(this, m_emerge->ndef);
2302                         scanner.scan(block, &m_transforming_liquid);
2303                 }
2304
2305                 /*
2306                         Save blocks loaded in old format in new format
2307                 */
2308
2309                 if(version < SER_FMT_VER_HIGHEST_WRITE || save_after_load)
2310                 {
2311                         saveBlock(block);
2312
2313                         // Should be in database now, so delete the old file
2314                         fs::RecursiveDelete(fullpath);
2315                 }
2316
2317                 // We just loaded it from the disk, so it's up-to-date.
2318                 block->resetModified();
2319
2320         }
2321         catch(SerializationError &e)
2322         {
2323                 warningstream<<"Invalid block data on disk "
2324                                 <<"fullpath="<<fullpath
2325                                 <<" (SerializationError). "
2326                                 <<"what()="<<e.what()
2327                                 <<std::endl;
2328                                 // Ignoring. A new one will be generated.
2329                 abort();
2330
2331                 // TODO: Backup file; name is in fullpath.
2332         }
2333 }
2334
2335 void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load)
2336 {
2337         DSTACK(FUNCTION_NAME);
2338
2339         try {
2340                 std::istringstream is(*blob, std::ios_base::binary);
2341
2342                 u8 version = SER_FMT_VER_INVALID;
2343                 is.read((char*)&version, 1);
2344
2345                 if(is.fail())
2346                         throw SerializationError("ServerMap::loadBlock(): Failed"
2347                                         " to read MapBlock version");
2348
2349                 MapBlock *block = NULL;
2350                 bool created_new = false;
2351                 block = sector->getBlockNoCreateNoEx(p3d.Y);
2352                 if(block == NULL)
2353                 {
2354                         block = sector->createBlankBlockNoInsert(p3d.Y);
2355                         created_new = true;
2356                 }
2357
2358                 // Read basic data
2359                 block->deSerialize(is, version, true);
2360
2361                 // If it's a new block, insert it to the map
2362                 if (created_new) {
2363                         sector->insertBlock(block);
2364                         ReflowScan scanner(this, m_emerge->ndef);
2365                         scanner.scan(block, &m_transforming_liquid);
2366                 }
2367
2368                 /*
2369                         Save blocks loaded in old format in new format
2370                 */
2371
2372                 //if(version < SER_FMT_VER_HIGHEST_READ || save_after_load)
2373                 // Only save if asked to; no need to update version
2374                 if(save_after_load)
2375                         saveBlock(block);
2376
2377                 // We just loaded it from, so it's up-to-date.
2378                 block->resetModified();
2379         }
2380         catch(SerializationError &e)
2381         {
2382                 errorstream<<"Invalid block data in database"
2383                                 <<" ("<<p3d.X<<","<<p3d.Y<<","<<p3d.Z<<")"
2384                                 <<" (SerializationError): "<<e.what()<<std::endl;
2385
2386                 // TODO: Block should be marked as invalid in memory so that it is
2387                 // not touched but the game can run
2388
2389                 if(g_settings->getBool("ignore_world_load_errors")){
2390                         errorstream<<"Ignoring block load error. Duck and cover! "
2391                                         <<"(ignore_world_load_errors)"<<std::endl;
2392                 } else {
2393                         throw SerializationError("Invalid block data in database");
2394                 }
2395         }
2396 }
2397
2398 MapBlock* ServerMap::loadBlock(v3s16 blockpos)
2399 {
2400         DSTACK(FUNCTION_NAME);
2401
2402         bool created_new = (getBlockNoCreateNoEx(blockpos) == NULL);
2403
2404         v2s16 p2d(blockpos.X, blockpos.Z);
2405
2406         std::string ret;
2407         dbase->loadBlock(blockpos, &ret);
2408         if (!ret.empty()) {
2409                 loadBlock(&ret, blockpos, createSector(p2d), false);
2410         } else {
2411                 // Not found in database, try the files
2412
2413                 // The directory layout we're going to load from.
2414                 //  1 - original sectors/xxxxzzzz/
2415                 //  2 - new sectors2/xxx/zzz/
2416                 //  If we load from anything but the latest structure, we will
2417                 //  immediately save to the new one, and remove the old.
2418                 int loadlayout = 1;
2419                 std::string sectordir1 = getSectorDir(p2d, 1);
2420                 std::string sectordir;
2421                 if (fs::PathExists(sectordir1)) {
2422                         sectordir = sectordir1;
2423                 } else {
2424                         loadlayout = 2;
2425                         sectordir = getSectorDir(p2d, 2);
2426                 }
2427
2428                 /*
2429                 Make sure sector is loaded
2430                  */
2431
2432                 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2433                 if (sector == NULL) {
2434                         try {
2435                                 sector = loadSectorMeta(sectordir, loadlayout != 2);
2436                         } catch(InvalidFilenameException &e) {
2437                                 return NULL;
2438                         } catch(FileNotGoodException &e) {
2439                                 return NULL;
2440                         } catch(std::exception &e) {
2441                                 return NULL;
2442                         }
2443                 }
2444
2445
2446                 /*
2447                 Make sure file exists
2448                  */
2449
2450                 std::string blockfilename = getBlockFilename(blockpos);
2451                 if (!fs::PathExists(sectordir + DIR_DELIM + blockfilename))
2452                         return NULL;
2453
2454                 /*
2455                 Load block and save it to the database
2456                  */
2457                 loadBlock(sectordir, blockfilename, sector, true);
2458         }
2459         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2460         if (created_new && (block != NULL)) {
2461                 std::map<v3s16, MapBlock*> modified_blocks;
2462                 // Fix lighting if necessary
2463                 voxalgo::update_block_border_lighting(this, block, modified_blocks);
2464                 if (!modified_blocks.empty()) {
2465                         //Modified lighting, send event
2466                         MapEditEvent event;
2467                         event.type = MEET_OTHER;
2468                         std::map<v3s16, MapBlock *>::iterator it;
2469                         for (it = modified_blocks.begin();
2470                                         it != modified_blocks.end(); ++it)
2471                                 event.modified_blocks.insert(it->first);
2472                         dispatchEvent(&event);
2473                 }
2474         }
2475         return block;
2476 }
2477
2478 bool ServerMap::deleteBlock(v3s16 blockpos)
2479 {
2480         if (!dbase->deleteBlock(blockpos))
2481                 return false;
2482
2483         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2484         if (block) {
2485                 v2s16 p2d(blockpos.X, blockpos.Z);
2486                 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2487                 if (!sector)
2488                         return false;
2489                 sector->deleteBlock(block);
2490         }
2491
2492         return true;
2493 }
2494
2495 void ServerMap::PrintInfo(std::ostream &out)
2496 {
2497         out<<"ServerMap: ";
2498 }
2499
2500 bool ServerMap::repairBlockLight(v3s16 blockpos,
2501         std::map<v3s16, MapBlock *> *modified_blocks)
2502 {
2503         MapBlock *block = emergeBlock(blockpos, false);
2504         if (!block || !block->isGenerated())
2505                 return false;
2506         voxalgo::repair_block_light(this, block, modified_blocks);
2507         return true;
2508 }
2509
2510 MMVManip::MMVManip(Map *map):
2511                 VoxelManipulator(),
2512                 m_map(map)
2513 {
2514 }
2515
2516 void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
2517         bool load_if_inexistent)
2518 {
2519         TimeTaker timer1("initialEmerge", &emerge_time);
2520
2521         // Units of these are MapBlocks
2522         v3s16 p_min = blockpos_min;
2523         v3s16 p_max = blockpos_max;
2524
2525         VoxelArea block_area_nodes
2526                         (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2527
2528         u32 size_MB = block_area_nodes.getVolume()*4/1000000;
2529         if(size_MB >= 1)
2530         {
2531                 infostream<<"initialEmerge: area: ";
2532                 block_area_nodes.print(infostream);
2533                 infostream<<" ("<<size_MB<<"MB)";
2534                 infostream<<std::endl;
2535         }
2536
2537         addArea(block_area_nodes);
2538
2539         for(s32 z=p_min.Z; z<=p_max.Z; z++)
2540         for(s32 y=p_min.Y; y<=p_max.Y; y++)
2541         for(s32 x=p_min.X; x<=p_max.X; x++)
2542         {
2543                 u8 flags = 0;
2544                 MapBlock *block;
2545                 v3s16 p(x,y,z);
2546                 std::map<v3s16, u8>::iterator n;
2547                 n = m_loaded_blocks.find(p);
2548                 if(n != m_loaded_blocks.end())
2549                         continue;
2550
2551                 bool block_data_inexistent = false;
2552                 try
2553                 {
2554                         TimeTaker timer1("emerge load", &emerge_load_time);
2555
2556                         block = m_map->getBlockNoCreate(p);
2557                         if(block->isDummy())
2558                                 block_data_inexistent = true;
2559                         else
2560                                 block->copyTo(*this);
2561                 }
2562                 catch(InvalidPositionException &e)
2563                 {
2564                         block_data_inexistent = true;
2565                 }
2566
2567                 if(block_data_inexistent)
2568                 {
2569
2570                         if (load_if_inexistent && !blockpos_over_max_limit(p)) {
2571                                 ServerMap *svrmap = (ServerMap *)m_map;
2572                                 block = svrmap->emergeBlock(p, false);
2573                                 if (block == NULL)
2574                                         block = svrmap->createBlock(p);
2575                                 block->copyTo(*this);
2576                         } else {
2577                                 flags |= VMANIP_BLOCK_DATA_INEXIST;
2578
2579                                 /*
2580                                         Mark area inexistent
2581                                 */
2582                                 VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2583                                 // Fill with VOXELFLAG_NO_DATA
2584                                 for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
2585                                 for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
2586                                 {
2587                                         s32 i = m_area.index(a.MinEdge.X,y,z);
2588                                         memset(&m_flags[i], VOXELFLAG_NO_DATA, MAP_BLOCKSIZE);
2589                                 }
2590                         }
2591                 }
2592                 /*else if (block->getNode(0, 0, 0).getContent() == CONTENT_IGNORE)
2593                 {
2594                         // Mark that block was loaded as blank
2595                         flags |= VMANIP_BLOCK_CONTAINS_CIGNORE;
2596                 }*/
2597
2598                 m_loaded_blocks[p] = flags;
2599         }
2600
2601         m_is_dirty = false;
2602 }
2603
2604 void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
2605         bool overwrite_generated)
2606 {
2607         if(m_area.getExtent() == v3s16(0,0,0))
2608                 return;
2609
2610         /*
2611                 Copy data of all blocks
2612         */
2613         for (auto &loaded_block : m_loaded_blocks) {
2614                 v3s16 p = loaded_block.first;
2615                 MapBlock *block = m_map->getBlockNoCreateNoEx(p);
2616                 bool existed = !(loaded_block.second & VMANIP_BLOCK_DATA_INEXIST);
2617                 if (!existed || (block == NULL) ||
2618                         (!overwrite_generated && block->isGenerated()))
2619                         continue;
2620
2621                 block->copyFrom(*this);
2622
2623                 if(modified_blocks)
2624                         (*modified_blocks)[p] = block;
2625         }
2626 }
2627
2628 //END