]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map.cpp
Properly remove SAO when worldedges are overtaken (#5889)
[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         u64 curr_time = porting::getTimeMs();
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::saoPositionOverLimit(const v3f &p)
1382 {
1383         return getMapgenParams()->saoPosOverLimit(p);
1384 }
1385
1386 bool ServerMap::blockpos_over_mapgen_limit(v3s16 p)
1387 {
1388         const s16 mapgen_limit_bp = rangelim(
1389                 getMapgenParams()->mapgen_limit, 0, MAX_MAP_GENERATION_LIMIT) /
1390                 MAP_BLOCKSIZE;
1391         return p.X < -mapgen_limit_bp ||
1392                 p.X >  mapgen_limit_bp ||
1393                 p.Y < -mapgen_limit_bp ||
1394                 p.Y >  mapgen_limit_bp ||
1395                 p.Z < -mapgen_limit_bp ||
1396                 p.Z >  mapgen_limit_bp;
1397 }
1398
1399 bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data)
1400 {
1401         s16 csize = getMapgenParams()->chunksize;
1402         v3s16 bpmin = EmergeManager::getContainingChunk(blockpos, csize);
1403         v3s16 bpmax = bpmin + v3s16(1, 1, 1) * (csize - 1);
1404
1405         bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1406         EMERGE_DBG_OUT("initBlockMake(): " PP(bpmin) " - " PP(bpmax));
1407
1408         v3s16 extra_borders(1, 1, 1);
1409         v3s16 full_bpmin = bpmin - extra_borders;
1410         v3s16 full_bpmax = bpmax + extra_borders;
1411
1412         // Do nothing if not inside mapgen limits (+-1 because of neighbors)
1413         if (blockpos_over_mapgen_limit(full_bpmin) ||
1414                         blockpos_over_mapgen_limit(full_bpmax))
1415                 return false;
1416
1417         data->seed = getSeed();
1418         data->blockpos_min = bpmin;
1419         data->blockpos_max = bpmax;
1420         data->blockpos_requested = blockpos;
1421         data->nodedef = m_nodedef;
1422
1423         /*
1424                 Create the whole area of this and the neighboring blocks
1425         */
1426         for (s16 x = full_bpmin.X; x <= full_bpmax.X; x++)
1427         for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++) {
1428                 v2s16 sectorpos(x, z);
1429                 // Sector metadata is loaded from disk if not already loaded.
1430                 ServerMapSector *sector = createSector(sectorpos);
1431                 FATAL_ERROR_IF(sector == NULL, "createSector() failed");
1432
1433                 for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) {
1434                         v3s16 p(x, y, z);
1435
1436                         MapBlock *block = emergeBlock(p, false);
1437                         if (block == NULL) {
1438                                 block = createBlock(p);
1439
1440                                 // Block gets sunlight if this is true.
1441                                 // Refer to the map generator heuristics.
1442                                 bool ug = m_emerge->isBlockUnderground(p);
1443                                 block->setIsUnderground(ug);
1444                         }
1445                 }
1446         }
1447
1448         /*
1449                 Now we have a big empty area.
1450
1451                 Make a ManualMapVoxelManipulator that contains this and the
1452                 neighboring blocks
1453         */
1454
1455         data->vmanip = new MMVManip(this);
1456         data->vmanip->initialEmerge(full_bpmin, full_bpmax);
1457
1458         // Note: we may need this again at some point.
1459 #if 0
1460         // Ensure none of the blocks to be generated were marked as
1461         // containing CONTENT_IGNORE
1462         for (s16 z = blockpos_min.Z; z <= blockpos_max.Z; z++) {
1463                 for (s16 y = blockpos_min.Y; y <= blockpos_max.Y; y++) {
1464                         for (s16 x = blockpos_min.X; x <= blockpos_max.X; x++) {
1465                                 core::map<v3s16, u8>::Node *n;
1466                                 n = data->vmanip->m_loaded_blocks.find(v3s16(x, y, z));
1467                                 if (n == NULL)
1468                                         continue;
1469                                 u8 flags = n->getValue();
1470                                 flags &= ~VMANIP_BLOCK_CONTAINS_CIGNORE;
1471                                 n->setValue(flags);
1472                         }
1473                 }
1474         }
1475 #endif
1476
1477         // Data is ready now.
1478         return true;
1479 }
1480
1481 void ServerMap::finishBlockMake(BlockMakeData *data,
1482         std::map<v3s16, MapBlock*> *changed_blocks)
1483 {
1484         v3s16 bpmin = data->blockpos_min;
1485         v3s16 bpmax = data->blockpos_max;
1486
1487         v3s16 extra_borders(1, 1, 1);
1488
1489         bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1490         EMERGE_DBG_OUT("finishBlockMake(): " PP(bpmin) " - " PP(bpmax));
1491
1492         /*
1493                 Blit generated stuff to map
1494                 NOTE: blitBackAll adds nearly everything to changed_blocks
1495         */
1496         data->vmanip->blitBackAll(changed_blocks);
1497
1498         EMERGE_DBG_OUT("finishBlockMake: changed_blocks.size()="
1499                 << changed_blocks->size());
1500
1501         /*
1502                 Copy transforming liquid information
1503         */
1504         while (data->transforming_liquid.size()) {
1505                 m_transforming_liquid.push_back(data->transforming_liquid.front());
1506                 data->transforming_liquid.pop_front();
1507         }
1508
1509         for (std::map<v3s16, MapBlock *>::iterator
1510                         it = changed_blocks->begin();
1511                         it != changed_blocks->end(); ++it) {
1512                 MapBlock *block = it->second;
1513                 if (!block)
1514                         continue;
1515                 /*
1516                         Update day/night difference cache of the MapBlocks
1517                 */
1518                 block->expireDayNightDiff();
1519                 /*
1520                         Set block as modified
1521                 */
1522                 block->raiseModified(MOD_STATE_WRITE_NEEDED,
1523                         MOD_REASON_EXPIRE_DAYNIGHTDIFF);
1524         }
1525
1526         /*
1527                 Set central blocks as generated
1528         */
1529         for (s16 x = bpmin.X; x <= bpmax.X; x++)
1530         for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
1531         for (s16 y = bpmin.Y; y <= bpmax.Y; y++) {
1532                 MapBlock *block = getBlockNoCreateNoEx(v3s16(x, y, z));
1533                 if (!block)
1534                         continue;
1535
1536                 block->setGenerated(true);
1537         }
1538
1539         /*
1540                 Save changed parts of map
1541                 NOTE: Will be saved later.
1542         */
1543         //save(MOD_STATE_WRITE_AT_UNLOAD);
1544 }
1545
1546 ServerMapSector *ServerMap::createSector(v2s16 p2d)
1547 {
1548         DSTACKF("%s: p2d=(%d,%d)",
1549                         FUNCTION_NAME,
1550                         p2d.X, p2d.Y);
1551
1552         /*
1553                 Check if it exists already in memory
1554         */
1555         ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1556         if(sector != NULL)
1557                 return sector;
1558
1559         /*
1560                 Try to load it from disk (with blocks)
1561         */
1562         //if(loadSectorFull(p2d) == true)
1563
1564         /*
1565                 Try to load metadata from disk
1566         */
1567 #if 0
1568         if(loadSectorMeta(p2d) == true)
1569         {
1570                 ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1571                 if(sector == NULL)
1572                 {
1573                         infostream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
1574                         throw InvalidPositionException("");
1575                 }
1576                 return sector;
1577         }
1578 #endif
1579
1580         /*
1581                 Do not create over max mapgen limit
1582         */
1583         const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
1584         if (p2d.X < -max_limit_bp ||
1585                         p2d.X >  max_limit_bp ||
1586                         p2d.Y < -max_limit_bp ||
1587                         p2d.Y >  max_limit_bp)
1588                 throw InvalidPositionException("createSector(): pos. over max mapgen limit");
1589
1590         /*
1591                 Generate blank sector
1592         */
1593
1594         sector = new ServerMapSector(this, p2d, m_gamedef);
1595
1596         // Sector position on map in nodes
1597         //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
1598
1599         /*
1600                 Insert to container
1601         */
1602         m_sectors[p2d] = sector;
1603
1604         return sector;
1605 }
1606
1607 #if 0
1608 /*
1609         This is a quick-hand function for calling makeBlock().
1610 */
1611 MapBlock * ServerMap::generateBlock(
1612                 v3s16 p,
1613                 std::map<v3s16, MapBlock*> &modified_blocks
1614 )
1615 {
1616         DSTACKF("%s: p=(%d,%d,%d)", FUNCTION_NAME, p.X, p.Y, p.Z);
1617
1618         /*infostream<<"generateBlock(): "
1619                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1620                         <<std::endl;*/
1621
1622         bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
1623
1624         TimeTaker timer("generateBlock");
1625
1626         //MapBlock *block = original_dummy;
1627
1628         v2s16 p2d(p.X, p.Z);
1629         v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
1630
1631         /*
1632                 Do not generate over-limit
1633         */
1634         if(blockpos_over_limit(p))
1635         {
1636                 infostream<<FUNCTION_NAME<<": Block position over limit"<<std::endl;
1637                 throw InvalidPositionException("generateBlock(): pos. over limit");
1638         }
1639
1640         /*
1641                 Create block make data
1642         */
1643         BlockMakeData data;
1644         initBlockMake(&data, p);
1645
1646         /*
1647                 Generate block
1648         */
1649         {
1650                 TimeTaker t("mapgen::make_block()");
1651                 mapgen->makeChunk(&data);
1652                 //mapgen::make_block(&data);
1653
1654                 if(enable_mapgen_debug_info == false)
1655                         t.stop(true); // Hide output
1656         }
1657
1658         /*
1659                 Blit data back on map, update lighting, add mobs and whatever this does
1660         */
1661         finishBlockMake(&data, modified_blocks);
1662
1663         /*
1664                 Get central block
1665         */
1666         MapBlock *block = getBlockNoCreateNoEx(p);
1667
1668 #if 0
1669         /*
1670                 Check result
1671         */
1672         if(block)
1673         {
1674                 bool erroneus_content = false;
1675                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1676                 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1677                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1678                 {
1679                         v3s16 p(x0,y0,z0);
1680                         MapNode n = block->getNode(p);
1681                         if(n.getContent() == CONTENT_IGNORE)
1682                         {
1683                                 infostream<<"CONTENT_IGNORE at "
1684                                                 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1685                                                 <<std::endl;
1686                                 erroneus_content = true;
1687                                 assert(0);
1688                         }
1689                 }
1690                 if(erroneus_content)
1691                 {
1692                         assert(0);
1693                 }
1694         }
1695 #endif
1696
1697 #if 0
1698         /*
1699                 Generate a completely empty block
1700         */
1701         if(block)
1702         {
1703                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1704                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1705                 {
1706                         for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1707                         {
1708                                 MapNode n;
1709                                 n.setContent(CONTENT_AIR);
1710                                 block->setNode(v3s16(x0,y0,z0), n);
1711                         }
1712                 }
1713         }
1714 #endif
1715
1716         if(enable_mapgen_debug_info == false)
1717                 timer.stop(true); // Hide output
1718
1719         return block;
1720 }
1721 #endif
1722
1723 MapBlock * ServerMap::createBlock(v3s16 p)
1724 {
1725         DSTACKF("%s: p=(%d,%d,%d)",
1726                         FUNCTION_NAME, p.X, p.Y, p.Z);
1727
1728         /*
1729                 Do not create over max mapgen limit
1730         */
1731         if (blockpos_over_max_limit(p))
1732                 throw InvalidPositionException("createBlock(): pos. over max mapgen limit");
1733
1734         v2s16 p2d(p.X, p.Z);
1735         s16 block_y = p.Y;
1736         /*
1737                 This will create or load a sector if not found in memory.
1738                 If block exists on disk, it will be loaded.
1739
1740                 NOTE: On old save formats, this will be slow, as it generates
1741                       lighting on blocks for them.
1742         */
1743         ServerMapSector *sector;
1744         try {
1745                 sector = (ServerMapSector*)createSector(p2d);
1746                 assert(sector->getId() == MAPSECTOR_SERVER);
1747         }
1748         catch(InvalidPositionException &e)
1749         {
1750                 infostream<<"createBlock: createSector() failed"<<std::endl;
1751                 throw e;
1752         }
1753         /*
1754                 NOTE: This should not be done, or at least the exception
1755                 should not be passed on as std::exception, because it
1756                 won't be catched at all.
1757         */
1758         /*catch(std::exception &e)
1759         {
1760                 infostream<<"createBlock: createSector() failed: "
1761                                 <<e.what()<<std::endl;
1762                 throw e;
1763         }*/
1764
1765         /*
1766                 Try to get a block from the sector
1767         */
1768
1769         MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
1770         if(block)
1771         {
1772                 if(block->isDummy())
1773                         block->unDummify();
1774                 return block;
1775         }
1776         // Create blank
1777         block = sector->createBlankBlock(block_y);
1778
1779         return block;
1780 }
1781
1782 MapBlock * ServerMap::emergeBlock(v3s16 p, bool create_blank)
1783 {
1784         DSTACKF("%s: p=(%d,%d,%d), create_blank=%d",
1785                         FUNCTION_NAME,
1786                         p.X, p.Y, p.Z, create_blank);
1787
1788         {
1789                 MapBlock *block = getBlockNoCreateNoEx(p);
1790                 if(block && block->isDummy() == false)
1791                         return block;
1792         }
1793
1794         {
1795                 MapBlock *block = loadBlock(p);
1796                 if(block)
1797                         return block;
1798         }
1799
1800         if (create_blank) {
1801                 ServerMapSector *sector = createSector(v2s16(p.X, p.Z));
1802                 MapBlock *block = sector->createBlankBlock(p.Y);
1803
1804                 return block;
1805         }
1806
1807 #if 0
1808         if(allow_generate)
1809         {
1810                 std::map<v3s16, MapBlock*> modified_blocks;
1811                 MapBlock *block = generateBlock(p, modified_blocks);
1812                 if(block)
1813                 {
1814                         MapEditEvent event;
1815                         event.type = MEET_OTHER;
1816                         event.p = p;
1817
1818                         // Copy modified_blocks to event
1819                         for(std::map<v3s16, MapBlock*>::iterator
1820                                         i = modified_blocks.begin();
1821                                         i != modified_blocks.end(); ++i)
1822                         {
1823                                 event.modified_blocks.insert(i->first);
1824                         }
1825
1826                         // Queue event
1827                         dispatchEvent(&event);
1828
1829                         return block;
1830                 }
1831         }
1832 #endif
1833
1834         return NULL;
1835 }
1836
1837 MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
1838 {
1839         MapBlock *block = getBlockNoCreateNoEx(p3d);
1840         if (block == NULL)
1841                 m_emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, p3d, false);
1842
1843         return block;
1844 }
1845
1846 // N.B.  This requires no synchronization, since data will not be modified unless
1847 // the VoxelManipulator being updated belongs to the same thread.
1848 void ServerMap::updateVManip(v3s16 pos)
1849 {
1850         Mapgen *mg = m_emerge->getCurrentMapgen();
1851         if (!mg)
1852                 return;
1853
1854         MMVManip *vm = mg->vm;
1855         if (!vm)
1856                 return;
1857
1858         if (!vm->m_area.contains(pos))
1859                 return;
1860
1861         s32 idx = vm->m_area.index(pos);
1862         vm->m_data[idx] = getNodeNoEx(pos);
1863         vm->m_flags[idx] &= ~VOXELFLAG_NO_DATA;
1864
1865         vm->m_is_dirty = true;
1866 }
1867
1868 s16 ServerMap::findGroundLevel(v2s16 p2d)
1869 {
1870 #if 0
1871         /*
1872                 Uh, just do something random...
1873         */
1874         // Find existing map from top to down
1875         s16 max=63;
1876         s16 min=-64;
1877         v3s16 p(p2d.X, max, p2d.Y);
1878         for(; p.Y>min; p.Y--)
1879         {
1880                 MapNode n = getNodeNoEx(p);
1881                 if(n.getContent() != CONTENT_IGNORE)
1882                         break;
1883         }
1884         if(p.Y == min)
1885                 goto plan_b;
1886         // If this node is not air, go to plan b
1887         if(getNodeNoEx(p).getContent() != CONTENT_AIR)
1888                 goto plan_b;
1889         // Search existing walkable and return it
1890         for(; p.Y>min; p.Y--)
1891         {
1892                 MapNode n = getNodeNoEx(p);
1893                 if(content_walkable(n.d) && n.getContent() != CONTENT_IGNORE)
1894                         return p.Y;
1895         }
1896
1897         // Move to plan b
1898 plan_b:
1899 #endif
1900
1901         /*
1902                 Determine from map generator noise functions
1903         */
1904
1905         s16 level = m_emerge->getGroundLevelAtPoint(p2d);
1906         return level;
1907
1908         //double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
1909         //return (s16)level;
1910 }
1911
1912 bool ServerMap::loadFromFolders() {
1913         if (!dbase->initialized() &&
1914                         !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
1915                 return true;
1916         return false;
1917 }
1918
1919 void ServerMap::createDirs(std::string path)
1920 {
1921         if(fs::CreateAllDirs(path) == false)
1922         {
1923                 m_dout<<"ServerMap: Failed to create directory "
1924                                 <<"\""<<path<<"\""<<std::endl;
1925                 throw BaseException("ServerMap failed to create directory");
1926         }
1927 }
1928
1929 std::string ServerMap::getSectorDir(v2s16 pos, int layout)
1930 {
1931         char cc[9];
1932         switch(layout)
1933         {
1934                 case 1:
1935                         snprintf(cc, 9, "%.4x%.4x",
1936                                 (unsigned int) pos.X & 0xffff,
1937                                 (unsigned int) pos.Y & 0xffff);
1938
1939                         return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
1940                 case 2:
1941                         snprintf(cc, 9, (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
1942                                 (unsigned int) pos.X & 0xfff,
1943                                 (unsigned int) pos.Y & 0xfff);
1944
1945                         return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc;
1946                 default:
1947                         assert(false);
1948                         return "";
1949         }
1950 }
1951
1952 v2s16 ServerMap::getSectorPos(const std::string &dirname)
1953 {
1954         unsigned int x = 0, y = 0;
1955         int r;
1956         std::string component;
1957         fs::RemoveLastPathComponent(dirname, &component, 1);
1958         if(component.size() == 8)
1959         {
1960                 // Old layout
1961                 r = sscanf(component.c_str(), "%4x%4x", &x, &y);
1962         }
1963         else if(component.size() == 3)
1964         {
1965                 // New layout
1966                 fs::RemoveLastPathComponent(dirname, &component, 2);
1967                 r = sscanf(component.c_str(), (std::string("%3x") + DIR_DELIM + "%3x").c_str(), &x, &y);
1968                 // Sign-extend the 12 bit values up to 16 bits...
1969                 if(x & 0x800) x |= 0xF000;
1970                 if(y & 0x800) y |= 0xF000;
1971         }
1972         else
1973         {
1974                 r = -1;
1975         }
1976
1977         FATAL_ERROR_IF(r != 2, "getSectorPos()");
1978         v2s16 pos((s16)x, (s16)y);
1979         return pos;
1980 }
1981
1982 v3s16 ServerMap::getBlockPos(const std::string &sectordir, const std::string &blockfile)
1983 {
1984         v2s16 p2d = getSectorPos(sectordir);
1985
1986         if(blockfile.size() != 4){
1987                 throw InvalidFilenameException("Invalid block filename");
1988         }
1989         unsigned int y;
1990         int r = sscanf(blockfile.c_str(), "%4x", &y);
1991         if(r != 1)
1992                 throw InvalidFilenameException("Invalid block filename");
1993         return v3s16(p2d.X, y, p2d.Y);
1994 }
1995
1996 std::string ServerMap::getBlockFilename(v3s16 p)
1997 {
1998         char cc[5];
1999         snprintf(cc, 5, "%.4x", (unsigned int)p.Y&0xffff);
2000         return cc;
2001 }
2002
2003 void ServerMap::save(ModifiedState save_level)
2004 {
2005         DSTACK(FUNCTION_NAME);
2006         if(m_map_saving_enabled == false) {
2007                 warningstream<<"Not saving map, saving disabled."<<std::endl;
2008                 return;
2009         }
2010
2011         if(save_level == MOD_STATE_CLEAN)
2012                 infostream<<"ServerMap: Saving whole map, this can take time."
2013                                 <<std::endl;
2014
2015         if (m_map_metadata_changed || save_level == MOD_STATE_CLEAN) {
2016                 if (settings_mgr.saveMapMeta())
2017                         m_map_metadata_changed = false;
2018         }
2019
2020         // Profile modified reasons
2021         Profiler modprofiler;
2022
2023         u32 sector_meta_count = 0;
2024         u32 block_count = 0;
2025         u32 block_count_all = 0; // Number of blocks in memory
2026
2027         // Don't do anything with sqlite unless something is really saved
2028         bool save_started = false;
2029
2030         for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
2031                 i != m_sectors.end(); ++i) {
2032                 ServerMapSector *sector = (ServerMapSector*)i->second;
2033                 assert(sector->getId() == MAPSECTOR_SERVER);
2034
2035                 if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
2036                         saveSectorMeta(sector);
2037                         sector_meta_count++;
2038                 }
2039
2040                 MapBlockVect blocks;
2041                 sector->getBlocks(blocks);
2042
2043                 for(MapBlockVect::iterator j = blocks.begin();
2044                         j != blocks.end(); ++j) {
2045                         MapBlock *block = *j;
2046
2047                         block_count_all++;
2048
2049                         if(block->getModified() >= (u32)save_level) {
2050                                 // Lazy beginSave()
2051                                 if(!save_started) {
2052                                         beginSave();
2053                                         save_started = true;
2054                                 }
2055
2056                                 modprofiler.add(block->getModifiedReasonString(), 1);
2057
2058                                 saveBlock(block);
2059                                 block_count++;
2060
2061                                 /*infostream<<"ServerMap: Written block ("
2062                                                 <<block->getPos().X<<","
2063                                                 <<block->getPos().Y<<","
2064                                                 <<block->getPos().Z<<")"
2065                                                 <<std::endl;*/
2066                         }
2067                 }
2068         }
2069
2070         if(save_started)
2071                 endSave();
2072
2073         /*
2074                 Only print if something happened or saved whole map
2075         */
2076         if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
2077                         || block_count != 0) {
2078                 infostream<<"ServerMap: Written: "
2079                                 <<sector_meta_count<<" sector metadata files, "
2080                                 <<block_count<<" block files"
2081                                 <<", "<<block_count_all<<" blocks in memory."
2082                                 <<std::endl;
2083                 PrintInfo(infostream); // ServerMap/ClientMap:
2084                 infostream<<"Blocks modified by: "<<std::endl;
2085                 modprofiler.print(infostream);
2086         }
2087 }
2088
2089 void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
2090 {
2091         if (loadFromFolders()) {
2092                 errorstream << "Map::listAllLoadableBlocks(): Result will be missing "
2093                                 << "all blocks that are stored in flat files." << std::endl;
2094         }
2095         dbase->listAllLoadableBlocks(dst);
2096 }
2097
2098 void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
2099 {
2100         for(std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
2101                 si != m_sectors.end(); ++si)
2102         {
2103                 MapSector *sector = si->second;
2104
2105                 MapBlockVect blocks;
2106                 sector->getBlocks(blocks);
2107
2108                 for(MapBlockVect::iterator i = blocks.begin();
2109                                 i != blocks.end(); ++i) {
2110                         v3s16 p = (*i)->getPos();
2111                         dst.push_back(p);
2112                 }
2113         }
2114 }
2115
2116 void ServerMap::saveSectorMeta(ServerMapSector *sector)
2117 {
2118         DSTACK(FUNCTION_NAME);
2119         // Format used for writing
2120         u8 version = SER_FMT_VER_HIGHEST_WRITE;
2121         // Get destination
2122         v2s16 pos = sector->getPos();
2123         std::string dir = getSectorDir(pos);
2124         createDirs(dir);
2125
2126         std::string fullpath = dir + DIR_DELIM + "meta";
2127         std::ostringstream ss(std::ios_base::binary);
2128
2129         sector->serialize(ss, version);
2130
2131         if(!fs::safeWriteToFile(fullpath, ss.str()))
2132                 throw FileNotGoodException("Cannot write sector metafile");
2133
2134         sector->differs_from_disk = false;
2135 }
2136
2137 MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
2138 {
2139         DSTACK(FUNCTION_NAME);
2140         // Get destination
2141         v2s16 p2d = getSectorPos(sectordir);
2142
2143         ServerMapSector *sector = NULL;
2144
2145         std::string fullpath = sectordir + DIR_DELIM + "meta";
2146         std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2147         if(is.good() == false)
2148         {
2149                 // If the directory exists anyway, it probably is in some old
2150                 // format. Just go ahead and create the sector.
2151                 if(fs::PathExists(sectordir))
2152                 {
2153                         /*infostream<<"ServerMap::loadSectorMeta(): Sector metafile "
2154                                         <<fullpath<<" doesn't exist but directory does."
2155                                         <<" Continuing with a sector with no metadata."
2156                                         <<std::endl;*/
2157                         sector = new ServerMapSector(this, p2d, m_gamedef);
2158                         m_sectors[p2d] = sector;
2159                 }
2160                 else
2161                 {
2162                         throw FileNotGoodException("Cannot open sector metafile");
2163                 }
2164         }
2165         else
2166         {
2167                 sector = ServerMapSector::deSerialize
2168                                 (is, this, p2d, m_sectors, m_gamedef);
2169                 if(save_after_load)
2170                         saveSectorMeta(sector);
2171         }
2172
2173         sector->differs_from_disk = false;
2174
2175         return sector;
2176 }
2177
2178 bool ServerMap::loadSectorMeta(v2s16 p2d)
2179 {
2180         DSTACK(FUNCTION_NAME);
2181
2182         // The directory layout we're going to load from.
2183         //  1 - original sectors/xxxxzzzz/
2184         //  2 - new sectors2/xxx/zzz/
2185         //  If we load from anything but the latest structure, we will
2186         //  immediately save to the new one, and remove the old.
2187         int loadlayout = 1;
2188         std::string sectordir1 = getSectorDir(p2d, 1);
2189         std::string sectordir;
2190         if(fs::PathExists(sectordir1))
2191         {
2192                 sectordir = sectordir1;
2193         }
2194         else
2195         {
2196                 loadlayout = 2;
2197                 sectordir = getSectorDir(p2d, 2);
2198         }
2199
2200         try{
2201                 loadSectorMeta(sectordir, loadlayout != 2);
2202         }
2203         catch(InvalidFilenameException &e)
2204         {
2205                 return false;
2206         }
2207         catch(FileNotGoodException &e)
2208         {
2209                 return false;
2210         }
2211         catch(std::exception &e)
2212         {
2213                 return false;
2214         }
2215
2216         return true;
2217 }
2218
2219 #if 0
2220 bool ServerMap::loadSectorFull(v2s16 p2d)
2221 {
2222         DSTACK(FUNCTION_NAME);
2223
2224         MapSector *sector = NULL;
2225
2226         // The directory layout we're going to load from.
2227         //  1 - original sectors/xxxxzzzz/
2228         //  2 - new sectors2/xxx/zzz/
2229         //  If we load from anything but the latest structure, we will
2230         //  immediately save to the new one, and remove the old.
2231         int loadlayout = 1;
2232         std::string sectordir1 = getSectorDir(p2d, 1);
2233         std::string sectordir;
2234         if(fs::PathExists(sectordir1))
2235         {
2236                 sectordir = sectordir1;
2237         }
2238         else
2239         {
2240                 loadlayout = 2;
2241                 sectordir = getSectorDir(p2d, 2);
2242         }
2243
2244         try{
2245                 sector = loadSectorMeta(sectordir, loadlayout != 2);
2246         }
2247         catch(InvalidFilenameException &e)
2248         {
2249                 return false;
2250         }
2251         catch(FileNotGoodException &e)
2252         {
2253                 return false;
2254         }
2255         catch(std::exception &e)
2256         {
2257                 return false;
2258         }
2259
2260         /*
2261                 Load blocks
2262         */
2263         std::vector<fs::DirListNode> list2 = fs::GetDirListing
2264                         (sectordir);
2265         std::vector<fs::DirListNode>::iterator i2;
2266         for(i2=list2.begin(); i2!=list2.end(); i2++)
2267         {
2268                 // We want files
2269                 if(i2->dir)
2270                         continue;
2271                 try{
2272                         loadBlock(sectordir, i2->name, sector, loadlayout != 2);
2273                 }
2274                 catch(InvalidFilenameException &e)
2275                 {
2276                         // This catches unknown crap in directory
2277                 }
2278         }
2279
2280         if(loadlayout != 2)
2281         {
2282                 infostream<<"Sector converted to new layout - deleting "<<
2283                         sectordir1<<std::endl;
2284                 fs::RecursiveDelete(sectordir1);
2285         }
2286
2287         return true;
2288 }
2289 #endif
2290
2291 MapDatabase *ServerMap::createDatabase(
2292         const std::string &name,
2293         const std::string &savedir,
2294         Settings &conf)
2295 {
2296         if (name == "sqlite3")
2297                 return new MapDatabaseSQLite3(savedir);
2298         if (name == "dummy")
2299                 return new Database_Dummy();
2300         #if USE_LEVELDB
2301         else if (name == "leveldb")
2302                 return new Database_LevelDB(savedir);
2303         #endif
2304         #if USE_REDIS
2305         else if (name == "redis")
2306                 return new Database_Redis(conf);
2307         #endif
2308         #if USE_POSTGRESQL
2309         else if (name == "postgresql") {
2310                 std::string connect_string = "";
2311                 conf.getNoEx("pgsql_connection", connect_string);
2312                 return new MapDatabasePostgreSQL(connect_string);
2313         }
2314         #endif
2315         else
2316                 throw BaseException(std::string("Database backend ") + name + " not supported.");
2317 }
2318
2319 void ServerMap::beginSave()
2320 {
2321         dbase->beginSave();
2322 }
2323
2324 void ServerMap::endSave()
2325 {
2326         dbase->endSave();
2327 }
2328
2329 bool ServerMap::saveBlock(MapBlock *block)
2330 {
2331         return saveBlock(block, dbase);
2332 }
2333
2334 bool ServerMap::saveBlock(MapBlock *block, MapDatabase *db)
2335 {
2336         v3s16 p3d = block->getPos();
2337
2338         // Dummy blocks are not written
2339         if (block->isDummy()) {
2340                 warningstream << "saveBlock: Not writing dummy block "
2341                         << PP(p3d) << std::endl;
2342                 return true;
2343         }
2344
2345         // Format used for writing
2346         u8 version = SER_FMT_VER_HIGHEST_WRITE;
2347
2348         /*
2349                 [0] u8 serialization version
2350                 [1] data
2351         */
2352         std::ostringstream o(std::ios_base::binary);
2353         o.write((char*) &version, 1);
2354         block->serialize(o, version, true);
2355
2356         std::string data = o.str();
2357         bool ret = db->saveBlock(p3d, data);
2358         if (ret) {
2359                 // We just wrote it to the disk so clear modified flag
2360                 block->resetModified();
2361         }
2362         return ret;
2363 }
2364
2365 void ServerMap::loadBlock(const std::string &sectordir, const std::string &blockfile,
2366                 MapSector *sector, bool save_after_load)
2367 {
2368         DSTACK(FUNCTION_NAME);
2369
2370         std::string fullpath = sectordir + DIR_DELIM + blockfile;
2371         try {
2372                 std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2373                 if (!is.good())
2374                         throw FileNotGoodException("Cannot open block file");
2375
2376                 v3s16 p3d = getBlockPos(sectordir, blockfile);
2377                 v2s16 p2d(p3d.X, p3d.Z);
2378
2379                 assert(sector->getPos() == p2d);
2380
2381                 u8 version = SER_FMT_VER_INVALID;
2382                 is.read((char*)&version, 1);
2383
2384                 if(is.fail())
2385                         throw SerializationError("ServerMap::loadBlock(): Failed"
2386                                         " to read MapBlock version");
2387
2388                 /*u32 block_size = MapBlock::serializedLength(version);
2389                 SharedBuffer<u8> data(block_size);
2390                 is.read((char*)*data, block_size);*/
2391
2392                 // This will always return a sector because we're the server
2393                 //MapSector *sector = emergeSector(p2d);
2394
2395                 MapBlock *block = NULL;
2396                 bool created_new = false;
2397                 block = sector->getBlockNoCreateNoEx(p3d.Y);
2398                 if(block == NULL)
2399                 {
2400                         block = sector->createBlankBlockNoInsert(p3d.Y);
2401                         created_new = true;
2402                 }
2403
2404                 // Read basic data
2405                 block->deSerialize(is, version, true);
2406
2407                 // If it's a new block, insert it to the map
2408                 if (created_new) {
2409                         sector->insertBlock(block);
2410                         ReflowScan scanner(this, m_emerge->ndef);
2411                         scanner.scan(block, &m_transforming_liquid);
2412                 }
2413
2414                 /*
2415                         Save blocks loaded in old format in new format
2416                 */
2417
2418                 if(version < SER_FMT_VER_HIGHEST_WRITE || save_after_load)
2419                 {
2420                         saveBlock(block);
2421
2422                         // Should be in database now, so delete the old file
2423                         fs::RecursiveDelete(fullpath);
2424                 }
2425
2426                 // We just loaded it from the disk, so it's up-to-date.
2427                 block->resetModified();
2428
2429         }
2430         catch(SerializationError &e)
2431         {
2432                 warningstream<<"Invalid block data on disk "
2433                                 <<"fullpath="<<fullpath
2434                                 <<" (SerializationError). "
2435                                 <<"what()="<<e.what()
2436                                 <<std::endl;
2437                                 // Ignoring. A new one will be generated.
2438                 abort();
2439
2440                 // TODO: Backup file; name is in fullpath.
2441         }
2442 }
2443
2444 void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load)
2445 {
2446         DSTACK(FUNCTION_NAME);
2447
2448         try {
2449                 std::istringstream is(*blob, std::ios_base::binary);
2450
2451                 u8 version = SER_FMT_VER_INVALID;
2452                 is.read((char*)&version, 1);
2453
2454                 if(is.fail())
2455                         throw SerializationError("ServerMap::loadBlock(): Failed"
2456                                         " to read MapBlock version");
2457
2458                 MapBlock *block = NULL;
2459                 bool created_new = false;
2460                 block = sector->getBlockNoCreateNoEx(p3d.Y);
2461                 if(block == NULL)
2462                 {
2463                         block = sector->createBlankBlockNoInsert(p3d.Y);
2464                         created_new = true;
2465                 }
2466
2467                 // Read basic data
2468                 block->deSerialize(is, version, true);
2469
2470                 // If it's a new block, insert it to the map
2471                 if (created_new) {
2472                         sector->insertBlock(block);
2473                         ReflowScan scanner(this, m_emerge->ndef);
2474                         scanner.scan(block, &m_transforming_liquid);
2475                 }
2476
2477                 /*
2478                         Save blocks loaded in old format in new format
2479                 */
2480
2481                 //if(version < SER_FMT_VER_HIGHEST_READ || save_after_load)
2482                 // Only save if asked to; no need to update version
2483                 if(save_after_load)
2484                         saveBlock(block);
2485
2486                 // We just loaded it from, so it's up-to-date.
2487                 block->resetModified();
2488         }
2489         catch(SerializationError &e)
2490         {
2491                 errorstream<<"Invalid block data in database"
2492                                 <<" ("<<p3d.X<<","<<p3d.Y<<","<<p3d.Z<<")"
2493                                 <<" (SerializationError): "<<e.what()<<std::endl;
2494
2495                 // TODO: Block should be marked as invalid in memory so that it is
2496                 // not touched but the game can run
2497
2498                 if(g_settings->getBool("ignore_world_load_errors")){
2499                         errorstream<<"Ignoring block load error. Duck and cover! "
2500                                         <<"(ignore_world_load_errors)"<<std::endl;
2501                 } else {
2502                         throw SerializationError("Invalid block data in database");
2503                 }
2504         }
2505 }
2506
2507 MapBlock* ServerMap::loadBlock(v3s16 blockpos)
2508 {
2509         DSTACK(FUNCTION_NAME);
2510
2511         bool created_new = (getBlockNoCreateNoEx(blockpos) == NULL);
2512
2513         v2s16 p2d(blockpos.X, blockpos.Z);
2514
2515         std::string ret;
2516         dbase->loadBlock(blockpos, &ret);
2517         if (ret != "") {
2518                 loadBlock(&ret, blockpos, createSector(p2d), false);
2519         } else {
2520                 // Not found in database, try the files
2521
2522                 // The directory layout we're going to load from.
2523                 //  1 - original sectors/xxxxzzzz/
2524                 //  2 - new sectors2/xxx/zzz/
2525                 //  If we load from anything but the latest structure, we will
2526                 //  immediately save to the new one, and remove the old.
2527                 int loadlayout = 1;
2528                 std::string sectordir1 = getSectorDir(p2d, 1);
2529                 std::string sectordir;
2530                 if (fs::PathExists(sectordir1)) {
2531                         sectordir = sectordir1;
2532                 } else {
2533                         loadlayout = 2;
2534                         sectordir = getSectorDir(p2d, 2);
2535                 }
2536
2537                 /*
2538                 Make sure sector is loaded
2539                  */
2540
2541                 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2542                 if (sector == NULL) {
2543                         try {
2544                                 sector = loadSectorMeta(sectordir, loadlayout != 2);
2545                         } catch(InvalidFilenameException &e) {
2546                                 return NULL;
2547                         } catch(FileNotGoodException &e) {
2548                                 return NULL;
2549                         } catch(std::exception &e) {
2550                                 return NULL;
2551                         }
2552                 }
2553
2554
2555                 /*
2556                 Make sure file exists
2557                  */
2558
2559                 std::string blockfilename = getBlockFilename(blockpos);
2560                 if (fs::PathExists(sectordir + DIR_DELIM + blockfilename) == false)
2561                         return NULL;
2562
2563                 /*
2564                 Load block and save it to the database
2565                  */
2566                 loadBlock(sectordir, blockfilename, sector, true);
2567         }
2568         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2569         if (created_new && (block != NULL)) {
2570                 std::map<v3s16, MapBlock*> modified_blocks;
2571                 // Fix lighting if necessary
2572                 voxalgo::update_block_border_lighting(this, block, modified_blocks);
2573                 if (!modified_blocks.empty()) {
2574                         //Modified lighting, send event
2575                         MapEditEvent event;
2576                         event.type = MEET_OTHER;
2577                         std::map<v3s16, MapBlock *>::iterator it;
2578                         for (it = modified_blocks.begin();
2579                                         it != modified_blocks.end(); ++it)
2580                                 event.modified_blocks.insert(it->first);
2581                         dispatchEvent(&event);
2582                 }
2583         }
2584         return block;
2585 }
2586
2587 bool ServerMap::deleteBlock(v3s16 blockpos)
2588 {
2589         if (!dbase->deleteBlock(blockpos))
2590                 return false;
2591
2592         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2593         if (block) {
2594                 v2s16 p2d(blockpos.X, blockpos.Z);
2595                 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2596                 if (!sector)
2597                         return false;
2598                 sector->deleteBlock(block);
2599         }
2600
2601         return true;
2602 }
2603
2604 void ServerMap::PrintInfo(std::ostream &out)
2605 {
2606         out<<"ServerMap: ";
2607 }
2608
2609 bool ServerMap::repairBlockLight(v3s16 blockpos,
2610         std::map<v3s16, MapBlock *> *modified_blocks)
2611 {
2612         MapBlock *block = emergeBlock(blockpos, false);
2613         if (!block || !block->isGenerated())
2614                 return false;
2615         voxalgo::repair_block_light(this, block, modified_blocks);
2616         return true;
2617 }
2618
2619 MMVManip::MMVManip(Map *map):
2620                 VoxelManipulator(),
2621                 m_is_dirty(false),
2622                 m_create_area(false),
2623                 m_map(map)
2624 {
2625 }
2626
2627 MMVManip::~MMVManip()
2628 {
2629 }
2630
2631 void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
2632         bool load_if_inexistent)
2633 {
2634         TimeTaker timer1("initialEmerge", &emerge_time);
2635
2636         // Units of these are MapBlocks
2637         v3s16 p_min = blockpos_min;
2638         v3s16 p_max = blockpos_max;
2639
2640         VoxelArea block_area_nodes
2641                         (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2642
2643         u32 size_MB = block_area_nodes.getVolume()*4/1000000;
2644         if(size_MB >= 1)
2645         {
2646                 infostream<<"initialEmerge: area: ";
2647                 block_area_nodes.print(infostream);
2648                 infostream<<" ("<<size_MB<<"MB)";
2649                 infostream<<std::endl;
2650         }
2651
2652         addArea(block_area_nodes);
2653
2654         for(s32 z=p_min.Z; z<=p_max.Z; z++)
2655         for(s32 y=p_min.Y; y<=p_max.Y; y++)
2656         for(s32 x=p_min.X; x<=p_max.X; x++)
2657         {
2658                 u8 flags = 0;
2659                 MapBlock *block;
2660                 v3s16 p(x,y,z);
2661                 std::map<v3s16, u8>::iterator n;
2662                 n = m_loaded_blocks.find(p);
2663                 if(n != m_loaded_blocks.end())
2664                         continue;
2665
2666                 bool block_data_inexistent = false;
2667                 try
2668                 {
2669                         TimeTaker timer1("emerge load", &emerge_load_time);
2670
2671                         block = m_map->getBlockNoCreate(p);
2672                         if(block->isDummy())
2673                                 block_data_inexistent = true;
2674                         else
2675                                 block->copyTo(*this);
2676                 }
2677                 catch(InvalidPositionException &e)
2678                 {
2679                         block_data_inexistent = true;
2680                 }
2681
2682                 if(block_data_inexistent)
2683                 {
2684
2685                         if (load_if_inexistent && !blockpos_over_max_limit(p)) {
2686                                 ServerMap *svrmap = (ServerMap *)m_map;
2687                                 block = svrmap->emergeBlock(p, false);
2688                                 if (block == NULL)
2689                                         block = svrmap->createBlock(p);
2690                                 block->copyTo(*this);
2691                         } else {
2692                                 flags |= VMANIP_BLOCK_DATA_INEXIST;
2693
2694                                 /*
2695                                         Mark area inexistent
2696                                 */
2697                                 VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2698                                 // Fill with VOXELFLAG_NO_DATA
2699                                 for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
2700                                 for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
2701                                 {
2702                                         s32 i = m_area.index(a.MinEdge.X,y,z);
2703                                         memset(&m_flags[i], VOXELFLAG_NO_DATA, MAP_BLOCKSIZE);
2704                                 }
2705                         }
2706                 }
2707                 /*else if (block->getNode(0, 0, 0).getContent() == CONTENT_IGNORE)
2708                 {
2709                         // Mark that block was loaded as blank
2710                         flags |= VMANIP_BLOCK_CONTAINS_CIGNORE;
2711                 }*/
2712
2713                 m_loaded_blocks[p] = flags;
2714         }
2715
2716         m_is_dirty = false;
2717 }
2718
2719 void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
2720         bool overwrite_generated)
2721 {
2722         if(m_area.getExtent() == v3s16(0,0,0))
2723                 return;
2724
2725         /*
2726                 Copy data of all blocks
2727         */
2728         for(std::map<v3s16, u8>::iterator
2729                         i = m_loaded_blocks.begin();
2730                         i != m_loaded_blocks.end(); ++i)
2731         {
2732                 v3s16 p = i->first;
2733                 MapBlock *block = m_map->getBlockNoCreateNoEx(p);
2734                 bool existed = !(i->second & VMANIP_BLOCK_DATA_INEXIST);
2735                 if ((existed == false) || (block == NULL) ||
2736                         (overwrite_generated == false && block->isGenerated() == true))
2737                         continue;
2738
2739                 block->copyFrom(*this);
2740
2741                 if(modified_blocks)
2742                         (*modified_blocks)[p] = block;
2743         }
2744 }
2745
2746 //END