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