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