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