]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map.cpp
Liquid flow: Prevent water spreading on ignore
[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 "porting.h"
26 #include "serialization.h"
27 #include "nodemetadata.h"
28 #include "settings.h"
29 #include "log.h"
30 #include "profiler.h"
31 #include "nodedef.h"
32 #include "gamedef.h"
33 #include "util/directiontables.h"
34 #include "util/mathconstants.h"
35 #include "rollback_interface.h"
36 #include "environment.h"
37 #include "emerge.h"
38 #include "mapgen_v6.h"
39 #include "mg_biome.h"
40 #include "config.h"
41 #include "server.h"
42 #include "database.h"
43 #include "database-dummy.h"
44 #include "database-sqlite3.h"
45 #include <deque>
46 #include <queue>
47 #if USE_LEVELDB
48 #include "database-leveldb.h"
49 #endif
50 #if USE_REDIS
51 #include "database-redis.h"
52 #endif
53
54 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
55
56
57 /*
58         Map
59 */
60
61 Map::Map(std::ostream &dout, IGameDef *gamedef):
62         m_dout(dout),
63         m_gamedef(gamedef),
64         m_sector_cache(NULL),
65         m_transforming_liquid_loop_count_multiplier(1.0f),
66         m_unprocessed_count(0),
67         m_inc_trending_up_start_time(0),
68         m_queue_size_timer_started(false)
69 {
70 }
71
72 Map::~Map()
73 {
74         /*
75                 Free all MapSectors
76         */
77         for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
78                 i != m_sectors.end(); ++i)
79         {
80                 delete i->second;
81         }
82 }
83
84 void Map::addEventReceiver(MapEventReceiver *event_receiver)
85 {
86         m_event_receivers.insert(event_receiver);
87 }
88
89 void Map::removeEventReceiver(MapEventReceiver *event_receiver)
90 {
91         m_event_receivers.erase(event_receiver);
92 }
93
94 void Map::dispatchEvent(MapEditEvent *event)
95 {
96         for(std::set<MapEventReceiver*>::iterator
97                         i = m_event_receivers.begin();
98                         i != m_event_receivers.end(); ++i)
99         {
100                 (*i)->onMapEditEvent(event);
101         }
102 }
103
104 MapSector * Map::getSectorNoGenerateNoExNoLock(v2s16 p)
105 {
106         if(m_sector_cache != NULL && p == m_sector_cache_p){
107                 MapSector * sector = m_sector_cache;
108                 return sector;
109         }
110
111         std::map<v2s16, MapSector*>::iterator n = m_sectors.find(p);
112
113         if(n == m_sectors.end())
114                 return NULL;
115
116         MapSector *sector = n->second;
117
118         // Cache the last result
119         m_sector_cache_p = p;
120         m_sector_cache = sector;
121
122         return sector;
123 }
124
125 MapSector * Map::getSectorNoGenerateNoEx(v2s16 p)
126 {
127         return getSectorNoGenerateNoExNoLock(p);
128 }
129
130 MapSector * Map::getSectorNoGenerate(v2s16 p)
131 {
132         MapSector *sector = getSectorNoGenerateNoEx(p);
133         if(sector == NULL)
134                 throw InvalidPositionException();
135
136         return sector;
137 }
138
139 MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
140 {
141         v2s16 p2d(p3d.X, p3d.Z);
142         MapSector * sector = getSectorNoGenerateNoEx(p2d);
143         if(sector == NULL)
144                 return NULL;
145         MapBlock *block = sector->getBlockNoCreateNoEx(p3d.Y);
146         return block;
147 }
148
149 MapBlock * Map::getBlockNoCreate(v3s16 p3d)
150 {
151         MapBlock *block = getBlockNoCreateNoEx(p3d);
152         if(block == NULL)
153                 throw InvalidPositionException();
154         return block;
155 }
156
157 bool Map::isNodeUnderground(v3s16 p)
158 {
159         v3s16 blockpos = getNodeBlockPos(p);
160         try{
161                 MapBlock * block = getBlockNoCreate(blockpos);
162                 return block->getIsUnderground();
163         }
164         catch(InvalidPositionException &e)
165         {
166                 return false;
167         }
168 }
169
170 bool Map::isValidPosition(v3s16 p)
171 {
172         v3s16 blockpos = getNodeBlockPos(p);
173         MapBlock *block = getBlockNoCreate(blockpos);
174         return (block != NULL);
175 }
176
177 // Returns a CONTENT_IGNORE node if not found
178 MapNode Map::getNodeNoEx(v3s16 p, bool *is_valid_position)
179 {
180         v3s16 blockpos = getNodeBlockPos(p);
181         MapBlock *block = getBlockNoCreateNoEx(blockpos);
182         if (block == NULL) {
183                 if (is_valid_position != NULL)
184                         *is_valid_position = false;
185                 return MapNode(CONTENT_IGNORE);
186         }
187
188         v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
189         bool is_valid_p;
190         MapNode node = block->getNodeNoCheck(relpos, &is_valid_p);
191         if (is_valid_position != NULL)
192                 *is_valid_position = is_valid_p;
193         return node;
194 }
195
196 #if 0
197 // Deprecated
198 // throws InvalidPositionException if not found
199 // TODO: Now this is deprecated, getNodeNoEx should be renamed
200 MapNode Map::getNode(v3s16 p)
201 {
202         v3s16 blockpos = getNodeBlockPos(p);
203         MapBlock *block = getBlockNoCreateNoEx(blockpos);
204         if (block == NULL)
205                 throw InvalidPositionException();
206         v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
207         bool is_valid_position;
208         MapNode node = block->getNodeNoCheck(relpos, &is_valid_position);
209         if (!is_valid_position)
210                 throw InvalidPositionException();
211         return node;
212 }
213 #endif
214
215 // throws InvalidPositionException if not found
216 void Map::setNode(v3s16 p, MapNode & n)
217 {
218         v3s16 blockpos = getNodeBlockPos(p);
219         MapBlock *block = getBlockNoCreate(blockpos);
220         v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
221         // Never allow placing CONTENT_IGNORE, it fucks up stuff
222         if(n.getContent() == CONTENT_IGNORE){
223                 bool temp_bool;
224                 errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE"
225                                 <<" while trying to replace \""
226                                 <<m_gamedef->ndef()->get(block->getNodeNoCheck(relpos, &temp_bool)).name
227                                 <<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
228                 debug_stacks_print_to(infostream);
229                 return;
230         }
231         block->setNodeNoCheck(relpos, n);
232 }
233
234
235 /*
236         Goes recursively through the neighbours of the node.
237
238         Alters only transparent nodes.
239
240         If the lighting of the neighbour is lower than the lighting of
241         the node was (before changing it to 0 at the step before), the
242         lighting of the neighbour is set to 0 and then the same stuff
243         repeats for the neighbour.
244
245         The ending nodes of the routine are stored in light_sources.
246         This is useful when a light is removed. In such case, this
247         routine can be called for the light node and then again for
248         light_sources to re-light the area without the removed light.
249
250         values of from_nodes are lighting values.
251 */
252 void Map::unspreadLight(enum LightBank bank,
253                 std::map<v3s16, u8> & from_nodes,
254                 std::set<v3s16> & light_sources,
255                 std::map<v3s16, MapBlock*>  & modified_blocks)
256 {
257         INodeDefManager *nodemgr = m_gamedef->ndef();
258
259         v3s16 dirs[6] = {
260                 v3s16(0,0,1), // back
261                 v3s16(0,1,0), // top
262                 v3s16(1,0,0), // right
263                 v3s16(0,0,-1), // front
264                 v3s16(0,-1,0), // bottom
265                 v3s16(-1,0,0), // left
266         };
267
268         if(from_nodes.empty())
269                 return;
270
271         u32 blockchangecount = 0;
272
273         std::map<v3s16, u8> unlighted_nodes;
274
275         /*
276                 Initialize block cache
277         */
278         v3s16 blockpos_last;
279         MapBlock *block = NULL;
280         // Cache this a bit, too
281         bool block_checked_in_modified = false;
282
283         for(std::map<v3s16, u8>::iterator j = from_nodes.begin();
284                 j != from_nodes.end(); ++j)
285         {
286                 v3s16 pos = j->first;
287                 v3s16 blockpos = getNodeBlockPos(pos);
288
289                 // Only fetch a new block if the block position has changed
290                 try{
291                         if(block == NULL || blockpos != blockpos_last){
292                                 block = getBlockNoCreate(blockpos);
293                                 blockpos_last = blockpos;
294
295                                 block_checked_in_modified = false;
296                                 blockchangecount++;
297                         }
298                 }
299                 catch(InvalidPositionException &e)
300                 {
301                         continue;
302                 }
303
304                 if(block->isDummy())
305                         continue;
306
307                 // Calculate relative position in block
308                 //v3s16 relpos = pos - blockpos_last * MAP_BLOCKSIZE;
309
310                 // Get node straight from the block
311                 //MapNode n = block->getNode(relpos);
312
313                 u8 oldlight = j->second;
314
315                 // Loop through 6 neighbors
316                 for(u16 i=0; i<6; i++)
317                 {
318                         // Get the position of the neighbor node
319                         v3s16 n2pos = pos + dirs[i];
320
321                         // Get the block where the node is located
322                         v3s16 blockpos, relpos;
323                         getNodeBlockPosWithOffset(n2pos, blockpos, relpos);
324
325                         // Only fetch a new block if the block position has changed
326                         try {
327                                 if(block == NULL || blockpos != blockpos_last){
328                                         block = getBlockNoCreate(blockpos);
329                                         blockpos_last = blockpos;
330
331                                         block_checked_in_modified = false;
332                                         blockchangecount++;
333                                 }
334                         }
335                         catch(InvalidPositionException &e) {
336                                 continue;
337                         }
338
339                         // Get node straight from the block
340                         bool is_valid_position;
341                         MapNode n2 = block->getNode(relpos, &is_valid_position);
342                         if (!is_valid_position)
343                                 continue;
344
345                         bool changed = false;
346
347                         //TODO: Optimize output by optimizing light_sources?
348
349                         /*
350                                 If the neighbor is dimmer than what was specified
351                                 as oldlight (the light of the previous node)
352                         */
353                         if(n2.getLight(bank, nodemgr) < oldlight)
354                         {
355                                 /*
356                                         And the neighbor is transparent and it has some light
357                                 */
358                                 if(nodemgr->get(n2).light_propagates
359                                                 && n2.getLight(bank, nodemgr) != 0)
360                                 {
361                                         /*
362                                                 Set light to 0 and add to queue
363                                         */
364
365                                         u8 current_light = n2.getLight(bank, nodemgr);
366                                         n2.setLight(bank, 0, nodemgr);
367                                         block->setNode(relpos, n2);
368
369                                         unlighted_nodes[n2pos] = current_light;
370                                         changed = true;
371
372                                         /*
373                                                 Remove from light_sources if it is there
374                                                 NOTE: This doesn't happen nearly at all
375                                         */
376                                         /*if(light_sources.find(n2pos))
377                                         {
378                                                 infostream<<"Removed from light_sources"<<std::endl;
379                                                 light_sources.remove(n2pos);
380                                         }*/
381                                 }
382
383                                 /*// DEBUG
384                                 if(light_sources.find(n2pos) != NULL)
385                                         light_sources.remove(n2pos);*/
386                         }
387                         else{
388                                 light_sources.insert(n2pos);
389                         }
390
391                         // Add to modified_blocks
392                         if(changed == true && block_checked_in_modified == false)
393                         {
394                                 // If the block is not found in modified_blocks, add.
395                                 if(modified_blocks.find(blockpos) == modified_blocks.end())
396                                 {
397                                         modified_blocks[blockpos] = block;
398                                 }
399                                 block_checked_in_modified = true;
400                         }
401                 }
402         }
403
404         /*infostream<<"unspreadLight(): Changed block "
405         <<blockchangecount<<" times"
406         <<" for "<<from_nodes.size()<<" nodes"
407         <<std::endl;*/
408
409         if(!unlighted_nodes.empty())
410                 unspreadLight(bank, unlighted_nodes, light_sources, modified_blocks);
411 }
412
413 /*
414         A single-node wrapper of the above
415 */
416 void Map::unLightNeighbors(enum LightBank bank,
417                 v3s16 pos, u8 lightwas,
418                 std::set<v3s16> & light_sources,
419                 std::map<v3s16, MapBlock*>  & modified_blocks)
420 {
421         std::map<v3s16, u8> from_nodes;
422         from_nodes[pos] = lightwas;
423
424         unspreadLight(bank, from_nodes, light_sources, modified_blocks);
425 }
426
427 /*
428         Lights neighbors of from_nodes, collects all them and then
429         goes on recursively.
430 */
431 void Map::spreadLight(enum LightBank bank,
432                 std::set<v3s16> & from_nodes,
433                 std::map<v3s16, MapBlock*> & modified_blocks)
434 {
435         INodeDefManager *nodemgr = m_gamedef->ndef();
436
437         const v3s16 dirs[6] = {
438                 v3s16(0,0,1), // back
439                 v3s16(0,1,0), // top
440                 v3s16(1,0,0), // right
441                 v3s16(0,0,-1), // front
442                 v3s16(0,-1,0), // bottom
443                 v3s16(-1,0,0), // left
444         };
445
446         if(from_nodes.empty())
447                 return;
448
449         u32 blockchangecount = 0;
450
451         std::set<v3s16> lighted_nodes;
452
453         /*
454                 Initialize block cache
455         */
456         v3s16 blockpos_last;
457         MapBlock *block = NULL;
458                 // Cache this a bit, too
459         bool block_checked_in_modified = false;
460
461         for(std::set<v3s16>::iterator j = from_nodes.begin();
462                 j != from_nodes.end(); ++j)
463         {
464                 v3s16 pos = *j;
465                 v3s16 blockpos, relpos;
466
467                 getNodeBlockPosWithOffset(pos, blockpos, relpos);
468
469                 // Only fetch a new block if the block position has changed
470                 try {
471                         if(block == NULL || blockpos != blockpos_last){
472                                 block = getBlockNoCreate(blockpos);
473                                 blockpos_last = blockpos;
474
475                                 block_checked_in_modified = false;
476                                 blockchangecount++;
477                         }
478                 }
479                 catch(InvalidPositionException &e) {
480                         continue;
481                 }
482
483                 if(block->isDummy())
484                         continue;
485
486                 // Get node straight from the block
487                 bool is_valid_position;
488                 MapNode n = block->getNode(relpos, &is_valid_position);
489
490                 u8 oldlight = is_valid_position ? n.getLight(bank, nodemgr) : 0;
491                 u8 newlight = diminish_light(oldlight);
492
493                 // Loop through 6 neighbors
494                 for(u16 i=0; i<6; i++){
495                         // Get the position of the neighbor node
496                         v3s16 n2pos = pos + dirs[i];
497
498                         // Get the block where the node is located
499                         v3s16 blockpos, relpos;
500                         getNodeBlockPosWithOffset(n2pos, blockpos, relpos);
501
502                         // Only fetch a new block if the block position has changed
503                         try {
504                                 if(block == NULL || blockpos != blockpos_last){
505                                         block = getBlockNoCreate(blockpos);
506                                         blockpos_last = blockpos;
507
508                                         block_checked_in_modified = false;
509                                         blockchangecount++;
510                                 }
511                         }
512                         catch(InvalidPositionException &e) {
513                                 continue;
514                         }
515
516                         // Get node straight from the block
517                         MapNode n2 = block->getNode(relpos, &is_valid_position);
518                         if (!is_valid_position)
519                                 continue;
520
521                         bool changed = false;
522                         /*
523                                 If the neighbor is brighter than the current node,
524                                 add to list (it will light up this node on its turn)
525                         */
526                         if(n2.getLight(bank, nodemgr) > undiminish_light(oldlight))
527                         {
528                                 lighted_nodes.insert(n2pos);
529                                 changed = true;
530                         }
531                         /*
532                                 If the neighbor is dimmer than how much light this node
533                                 would spread on it, add to list
534                         */
535                         if(n2.getLight(bank, nodemgr) < newlight)
536                         {
537                                 if(nodemgr->get(n2).light_propagates)
538                                 {
539                                         n2.setLight(bank, newlight, nodemgr);
540                                         block->setNode(relpos, n2);
541                                         lighted_nodes.insert(n2pos);
542                                         changed = true;
543                                 }
544                         }
545
546                         // Add to modified_blocks
547                         if(changed == true && block_checked_in_modified == false)
548                         {
549                                 // If the block is not found in modified_blocks, add.
550                                 if(modified_blocks.find(blockpos) == modified_blocks.end())
551                                 {
552                                         modified_blocks[blockpos] = block;
553                                 }
554                                 block_checked_in_modified = true;
555                         }
556                 }
557         }
558
559         /*infostream<<"spreadLight(): Changed block "
560                         <<blockchangecount<<" times"
561                         <<" for "<<from_nodes.size()<<" nodes"
562                         <<std::endl;*/
563
564         if(!lighted_nodes.empty())
565                 spreadLight(bank, lighted_nodes, modified_blocks);
566 }
567
568 /*
569         A single-node source variation of the above.
570 */
571 void Map::lightNeighbors(enum LightBank bank,
572                 v3s16 pos,
573                 std::map<v3s16, MapBlock*> & modified_blocks)
574 {
575         std::set<v3s16> from_nodes;
576         from_nodes.insert(pos);
577         spreadLight(bank, from_nodes, modified_blocks);
578 }
579
580 v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p)
581 {
582         INodeDefManager *nodemgr = m_gamedef->ndef();
583
584         v3s16 dirs[6] = {
585                 v3s16(0,0,1), // back
586                 v3s16(0,1,0), // top
587                 v3s16(1,0,0), // right
588                 v3s16(0,0,-1), // front
589                 v3s16(0,-1,0), // bottom
590                 v3s16(-1,0,0), // left
591         };
592
593         u8 brightest_light = 0;
594         v3s16 brightest_pos(0,0,0);
595         bool found_something = false;
596
597         // Loop through 6 neighbors
598         for(u16 i=0; i<6; i++){
599                 // Get the position of the neighbor node
600                 v3s16 n2pos = p + dirs[i];
601                 MapNode n2;
602                 bool is_valid_position;
603                 n2 = getNodeNoEx(n2pos, &is_valid_position);
604                 if (!is_valid_position)
605                         continue;
606
607                 if(n2.getLight(bank, nodemgr) > brightest_light || found_something == false){
608                         brightest_light = n2.getLight(bank, nodemgr);
609                         brightest_pos = n2pos;
610                         found_something = true;
611                 }
612         }
613
614         if(found_something == false)
615                 throw InvalidPositionException();
616
617         return brightest_pos;
618 }
619
620 /*
621         Propagates sunlight down from a node.
622         Starting point gets sunlight.
623
624         Returns the lowest y value of where the sunlight went.
625
626         Mud is turned into grass in where the sunlight stops.
627 */
628 s16 Map::propagateSunlight(v3s16 start,
629                 std::map<v3s16, MapBlock*> & modified_blocks)
630 {
631         INodeDefManager *nodemgr = m_gamedef->ndef();
632
633         s16 y = start.Y;
634         for(; ; y--)
635         {
636                 v3s16 pos(start.X, y, start.Z);
637
638                 v3s16 blockpos = getNodeBlockPos(pos);
639                 MapBlock *block;
640                 try{
641                         block = getBlockNoCreate(blockpos);
642                 }
643                 catch(InvalidPositionException &e)
644                 {
645                         break;
646                 }
647
648                 v3s16 relpos = pos - blockpos*MAP_BLOCKSIZE;
649                 bool is_valid_position;
650                 MapNode n = block->getNode(relpos, &is_valid_position);
651                 if (!is_valid_position)
652                         break;
653
654                 if(nodemgr->get(n).sunlight_propagates)
655                 {
656                         n.setLight(LIGHTBANK_DAY, LIGHT_SUN, nodemgr);
657                         block->setNode(relpos, n);
658
659                         modified_blocks[blockpos] = block;
660                 }
661                 else
662                 {
663                         // Sunlight goes no further
664                         break;
665                 }
666         }
667         return y + 1;
668 }
669
670 void Map::updateLighting(enum LightBank bank,
671                 std::map<v3s16, MapBlock*> & a_blocks,
672                 std::map<v3s16, MapBlock*> & modified_blocks)
673 {
674         INodeDefManager *nodemgr = m_gamedef->ndef();
675
676         /*m_dout<<"Map::updateLighting(): "
677                         <<a_blocks.size()<<" blocks."<<std::endl;*/
678
679         //TimeTaker timer("updateLighting");
680
681         // For debugging
682         //bool debug=true;
683         //u32 count_was = modified_blocks.size();
684
685         //std::map<v3s16, MapBlock*> blocks_to_update;
686
687         std::set<v3s16> light_sources;
688
689         std::map<v3s16, u8> unlight_from;
690
691         int num_bottom_invalid = 0;
692
693         {
694         //TimeTaker t("first stuff");
695
696         for(std::map<v3s16, MapBlock*>::iterator i = a_blocks.begin();
697                 i != a_blocks.end(); ++i)
698         {
699                 MapBlock *block = i->second;
700
701                 for(;;)
702                 {
703                         // Don't bother with dummy blocks.
704                         if(block->isDummy())
705                                 break;
706
707                         v3s16 pos = block->getPos();
708                         v3s16 posnodes = block->getPosRelative();
709                         modified_blocks[pos] = block;
710                         //blocks_to_update[pos] = block;
711
712                         /*
713                                 Clear all light from block
714                         */
715                         for(s16 z=0; z<MAP_BLOCKSIZE; z++)
716                         for(s16 x=0; x<MAP_BLOCKSIZE; x++)
717                         for(s16 y=0; y<MAP_BLOCKSIZE; y++)
718                         {
719                                 v3s16 p(x,y,z);
720                                 bool is_valid_position;
721                                 MapNode n = block->getNode(p, &is_valid_position);
722                                 if (!is_valid_position) {
723                                         /* This would happen when dealing with a
724                                            dummy block.
725                                         */
726                                         infostream<<"updateLighting(): InvalidPositionException"
727                                                         <<std::endl;
728                                         continue;
729                                 }
730                                 u8 oldlight = n.getLight(bank, nodemgr);
731                                 n.setLight(bank, 0, nodemgr);
732                                 block->setNode(p, n);
733
734                                 // If node sources light, add to list
735                                 u8 source = nodemgr->get(n).light_source;
736                                 if(source != 0)
737                                         light_sources.insert(p + posnodes);
738
739                                 // Collect borders for unlighting
740                                 if((x==0 || x == MAP_BLOCKSIZE-1
741                                                 || y==0 || y == MAP_BLOCKSIZE-1
742                                                 || z==0 || z == MAP_BLOCKSIZE-1)
743                                                 && oldlight != 0)
744                                 {
745                                         v3s16 p_map = p + posnodes;
746                                         unlight_from[p_map] = oldlight;
747                                 }
748
749
750                         }
751
752                         if(bank == LIGHTBANK_DAY)
753                         {
754                                 bool bottom_valid = block->propagateSunlight(light_sources);
755
756                                 if(!bottom_valid)
757                                         num_bottom_invalid++;
758
759                                 // If bottom is valid, we're done.
760                                 if(bottom_valid)
761                                         break;
762                         }
763                         else if(bank == LIGHTBANK_NIGHT)
764                         {
765                                 // For night lighting, sunlight is not propagated
766                                 break;
767                         }
768                         else
769                         {
770                                 assert("Invalid lighting bank" == NULL);
771                         }
772
773                         /*infostream<<"Bottom for sunlight-propagated block ("
774                                         <<pos.X<<","<<pos.Y<<","<<pos.Z<<") not valid"
775                                         <<std::endl;*/
776
777                         // Bottom sunlight is not valid; get the block and loop to it
778
779                         pos.Y--;
780                         try{
781                                 block = getBlockNoCreate(pos);
782                         }
783                         catch(InvalidPositionException &e)
784                         {
785                                 FATAL_ERROR("Invalid position");
786                         }
787
788                 }
789         }
790
791         }
792
793         /*
794                 Enable this to disable proper lighting for speeding up map
795                 generation for testing or whatever
796         */
797 #if 0
798         //if(g_settings->get(""))
799         {
800                 core::map<v3s16, MapBlock*>::Iterator i;
801                 i = blocks_to_update.getIterator();
802                 for(; i.atEnd() == false; i++)
803                 {
804                         MapBlock *block = i.getNode()->getValue();
805                         v3s16 p = block->getPos();
806                         block->setLightingExpired(false);
807                 }
808                 return;
809         }
810 #endif
811
812 #if 1
813         {
814                 //TimeTaker timer("unspreadLight");
815                 unspreadLight(bank, unlight_from, light_sources, modified_blocks);
816         }
817
818         /*if(debug)
819         {
820                 u32 diff = modified_blocks.size() - count_was;
821                 count_was = modified_blocks.size();
822                 infostream<<"unspreadLight modified "<<diff<<std::endl;
823         }*/
824
825         {
826                 //TimeTaker timer("spreadLight");
827                 spreadLight(bank, light_sources, modified_blocks);
828         }
829
830         /*if(debug)
831         {
832                 u32 diff = modified_blocks.size() - count_was;
833                 count_was = modified_blocks.size();
834                 infostream<<"spreadLight modified "<<diff<<std::endl;
835         }*/
836 #endif
837
838 #if 0
839         {
840                 //MapVoxelManipulator vmanip(this);
841
842                 // Make a manual voxel manipulator and load all the blocks
843                 // that touch the requested blocks
844                 ManualMapVoxelManipulator vmanip(this);
845
846                 {
847                 //TimeTaker timer("initialEmerge");
848
849                 core::map<v3s16, MapBlock*>::Iterator i;
850                 i = blocks_to_update.getIterator();
851                 for(; i.atEnd() == false; i++)
852                 {
853                         MapBlock *block = i.getNode()->getValue();
854                         v3s16 p = block->getPos();
855
856                         // Add all surrounding blocks
857                         vmanip.initialEmerge(p - v3s16(1,1,1), p + v3s16(1,1,1));
858
859                         /*
860                                 Add all surrounding blocks that have up-to-date lighting
861                                 NOTE: This doesn't quite do the job (not everything
862                                           appropriate is lighted)
863                         */
864                         /*for(s16 z=-1; z<=1; z++)
865                         for(s16 y=-1; y<=1; y++)
866                         for(s16 x=-1; x<=1; x++)
867                         {
868                                 v3s16 p2 = p + v3s16(x,y,z);
869                                 MapBlock *block = getBlockNoCreateNoEx(p2);
870                                 if(block == NULL)
871                                         continue;
872                                 if(block->isDummy())
873                                         continue;
874                                 if(block->getLightingExpired())
875                                         continue;
876                                 vmanip.initialEmerge(p2, p2);
877                         }*/
878
879                         // Lighting of block will be updated completely
880                         block->setLightingExpired(false);
881                 }
882                 }
883
884                 {
885                         //TimeTaker timer("unSpreadLight");
886                         vmanip.unspreadLight(bank, unlight_from, light_sources, nodemgr);
887                 }
888                 {
889                         //TimeTaker timer("spreadLight");
890                         vmanip.spreadLight(bank, light_sources, nodemgr);
891                 }
892                 {
893                         //TimeTaker timer("blitBack");
894                         vmanip.blitBack(modified_blocks);
895                 }
896                 /*infostream<<"emerge_time="<<emerge_time<<std::endl;
897                 emerge_time = 0;*/
898         }
899 #endif
900
901         //m_dout<<"Done ("<<getTimestamp()<<")"<<std::endl;
902 }
903
904 void Map::updateLighting(std::map<v3s16, MapBlock*> & a_blocks,
905                 std::map<v3s16, MapBlock*> & modified_blocks)
906 {
907         updateLighting(LIGHTBANK_DAY, a_blocks, modified_blocks);
908         updateLighting(LIGHTBANK_NIGHT, a_blocks, modified_blocks);
909
910         /*
911                 Update information about whether day and night light differ
912         */
913         for(std::map<v3s16, MapBlock*>::iterator
914                         i = modified_blocks.begin();
915                         i != modified_blocks.end(); ++i)
916         {
917                 MapBlock *block = i->second;
918                 block->expireDayNightDiff();
919         }
920 }
921
922 /*
923 */
924 void Map::addNodeAndUpdate(v3s16 p, MapNode n,
925                 std::map<v3s16, MapBlock*> &modified_blocks,
926                 bool remove_metadata)
927 {
928         INodeDefManager *ndef = m_gamedef->ndef();
929
930         /*PrintInfo(m_dout);
931         m_dout<<"Map::addNodeAndUpdate(): p=("
932                         <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
933
934         /*
935                 From this node to nodes underneath:
936                 If lighting is sunlight (1.0), unlight neighbours and
937                 set lighting to 0.
938                 Else discontinue.
939         */
940
941         v3s16 toppos = p + v3s16(0,1,0);
942         //v3s16 bottompos = p + v3s16(0,-1,0);
943
944         bool node_under_sunlight = true;
945         std::set<v3s16> light_sources;
946
947         /*
948                 Collect old node for rollback
949         */
950         RollbackNode rollback_oldnode(this, p, m_gamedef);
951
952         /*
953                 If there is a node at top and it doesn't have sunlight,
954                 there has not been any sunlight going down.
955
956                 Otherwise there probably is.
957         */
958
959         bool is_valid_position;
960         MapNode topnode = getNodeNoEx(toppos, &is_valid_position);
961
962         if(is_valid_position && topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN)
963                 node_under_sunlight = false;
964
965         /*
966                 Remove all light that has come out of this node
967         */
968
969         enum LightBank banks[] =
970         {
971                 LIGHTBANK_DAY,
972                 LIGHTBANK_NIGHT
973         };
974         for(s32 i=0; i<2; i++)
975         {
976                 enum LightBank bank = banks[i];
977
978                 u8 lightwas = getNodeNoEx(p).getLight(bank, ndef);
979
980                 // Add the block of the added node to modified_blocks
981                 v3s16 blockpos = getNodeBlockPos(p);
982                 MapBlock * block = getBlockNoCreate(blockpos);
983                 assert(block != NULL);
984                 modified_blocks[blockpos] = block;
985
986                 assert(isValidPosition(p));
987
988                 // Unlight neighbours of node.
989                 // This means setting light of all consequent dimmer nodes
990                 // to 0.
991                 // This also collects the nodes at the border which will spread
992                 // light again into this.
993                 unLightNeighbors(bank, p, lightwas, light_sources, modified_blocks);
994
995                 n.setLight(bank, 0, ndef);
996         }
997
998         /*
999                 If node lets sunlight through and is under sunlight, it has
1000                 sunlight too.
1001         */
1002         if(node_under_sunlight && ndef->get(n).sunlight_propagates)
1003         {
1004                 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
1005         }
1006
1007         /*
1008                 Remove node metadata
1009         */
1010         if (remove_metadata) {
1011                 removeNodeMetadata(p);
1012         }
1013
1014         /*
1015                 Set the node on the map
1016         */
1017
1018         setNode(p, n);
1019
1020         /*
1021                 If node is under sunlight and doesn't let sunlight through,
1022                 take all sunlighted nodes under it and clear light from them
1023                 and from where the light has been spread.
1024                 TODO: This could be optimized by mass-unlighting instead
1025                           of looping
1026         */
1027         if(node_under_sunlight && !ndef->get(n).sunlight_propagates)
1028         {
1029                 s16 y = p.Y - 1;
1030                 for(;; y--){
1031                         //m_dout<<"y="<<y<<std::endl;
1032                         v3s16 n2pos(p.X, y, p.Z);
1033
1034                         MapNode n2;
1035
1036                         n2 = getNodeNoEx(n2pos, &is_valid_position);
1037                         if (!is_valid_position)
1038                                 break;
1039
1040                         if(n2.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
1041                         {
1042                                 unLightNeighbors(LIGHTBANK_DAY,
1043                                                 n2pos, n2.getLight(LIGHTBANK_DAY, ndef),
1044                                                 light_sources, modified_blocks);
1045                                 n2.setLight(LIGHTBANK_DAY, 0, ndef);
1046                                 setNode(n2pos, n2);
1047                         }
1048                         else
1049                                 break;
1050                 }
1051         }
1052
1053         for(s32 i=0; i<2; i++)
1054         {
1055                 enum LightBank bank = banks[i];
1056
1057                 /*
1058                         Spread light from all nodes that might be capable of doing so
1059                 */
1060                 spreadLight(bank, light_sources, modified_blocks);
1061         }
1062
1063         /*
1064                 Update information about whether day and night light differ
1065         */
1066         for(std::map<v3s16, MapBlock*>::iterator
1067                         i = modified_blocks.begin();
1068                         i != modified_blocks.end(); ++i)
1069         {
1070                 i->second->expireDayNightDiff();
1071         }
1072
1073         /*
1074                 Report for rollback
1075         */
1076         if(m_gamedef->rollback())
1077         {
1078                 RollbackNode rollback_newnode(this, p, m_gamedef);
1079                 RollbackAction action;
1080                 action.setSetNode(p, rollback_oldnode, rollback_newnode);
1081                 m_gamedef->rollback()->reportAction(action);
1082         }
1083
1084         /*
1085                 Add neighboring liquid nodes and the node itself if it is
1086                 liquid (=water node was added) to transform queue.
1087         */
1088         v3s16 dirs[7] = {
1089                 v3s16(0,0,0), // self
1090                 v3s16(0,0,1), // back
1091                 v3s16(0,1,0), // top
1092                 v3s16(1,0,0), // right
1093                 v3s16(0,0,-1), // front
1094                 v3s16(0,-1,0), // bottom
1095                 v3s16(-1,0,0), // left
1096         };
1097         for(u16 i=0; i<7; i++)
1098         {
1099                 v3s16 p2 = p + dirs[i];
1100
1101                 MapNode n2 = getNodeNoEx(p2, &is_valid_position);
1102                 if(is_valid_position
1103                                 && (ndef->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR))
1104                 {
1105                         m_transforming_liquid.push_back(p2);
1106                 }
1107         }
1108 }
1109
1110 /*
1111 */
1112 void Map::removeNodeAndUpdate(v3s16 p,
1113                 std::map<v3s16, MapBlock*> &modified_blocks)
1114 {
1115         INodeDefManager *ndef = m_gamedef->ndef();
1116
1117         /*PrintInfo(m_dout);
1118         m_dout<<"Map::removeNodeAndUpdate(): p=("
1119                         <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
1120
1121         bool node_under_sunlight = true;
1122
1123         v3s16 toppos = p + v3s16(0,1,0);
1124
1125         // Node will be replaced with this
1126         content_t replace_material = CONTENT_AIR;
1127
1128         /*
1129                 Collect old node for rollback
1130         */
1131         RollbackNode rollback_oldnode(this, p, m_gamedef);
1132
1133         /*
1134                 If there is a node at top and it doesn't have sunlight,
1135                 there will be no sunlight going down.
1136         */
1137         bool is_valid_position;
1138         MapNode topnode = getNodeNoEx(toppos, &is_valid_position);
1139
1140         if(is_valid_position && topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN)
1141                 node_under_sunlight = false;
1142
1143         std::set<v3s16> light_sources;
1144
1145         enum LightBank banks[] =
1146         {
1147                 LIGHTBANK_DAY,
1148                 LIGHTBANK_NIGHT
1149         };
1150         for(s32 i=0; i<2; i++)
1151         {
1152                 enum LightBank bank = banks[i];
1153
1154                 /*
1155                         Unlight neighbors (in case the node is a light source)
1156                 */
1157                 unLightNeighbors(bank, p,
1158                                 getNodeNoEx(p).getLight(bank, ndef),
1159                                 light_sources, modified_blocks);
1160         }
1161
1162         /*
1163                 Remove node metadata
1164         */
1165
1166         removeNodeMetadata(p);
1167
1168         /*
1169                 Remove the node.
1170                 This also clears the lighting.
1171         */
1172
1173         MapNode n(replace_material);
1174         setNode(p, n);
1175
1176         for(s32 i=0; i<2; i++)
1177         {
1178                 enum LightBank bank = banks[i];
1179
1180                 /*
1181                         Recalculate lighting
1182                 */
1183                 spreadLight(bank, light_sources, modified_blocks);
1184         }
1185
1186         // Add the block of the removed node to modified_blocks
1187         v3s16 blockpos = getNodeBlockPos(p);
1188         MapBlock * block = getBlockNoCreate(blockpos);
1189         assert(block != NULL);
1190         modified_blocks[blockpos] = block;
1191
1192         /*
1193                 If the removed node was under sunlight, propagate the
1194                 sunlight down from it and then light all neighbors
1195                 of the propagated blocks.
1196         */
1197         if(node_under_sunlight)
1198         {
1199                 s16 ybottom = propagateSunlight(p, modified_blocks);
1200                 /*m_dout<<"Node was under sunlight. "
1201                                 "Propagating sunlight";
1202                 m_dout<<" -> ybottom="<<ybottom<<std::endl;*/
1203                 s16 y = p.Y;
1204                 for(; y >= ybottom; y--)
1205                 {
1206                         v3s16 p2(p.X, y, p.Z);
1207                         /*m_dout<<"lighting neighbors of node ("
1208                                         <<p2.X<<","<<p2.Y<<","<<p2.Z<<")"
1209                                         <<std::endl;*/
1210                         lightNeighbors(LIGHTBANK_DAY, p2, modified_blocks);
1211                 }
1212         }
1213         else
1214         {
1215                 // Set the lighting of this node to 0
1216                 // TODO: Is this needed? Lighting is cleared up there already.
1217                 MapNode n = getNodeNoEx(p, &is_valid_position);
1218                 if (is_valid_position) {
1219                         n.setLight(LIGHTBANK_DAY, 0, ndef);
1220                         setNode(p, n);
1221                 } else {
1222                         FATAL_ERROR("Invalid position");
1223                 }
1224         }
1225
1226         for(s32 i=0; i<2; i++)
1227         {
1228                 enum LightBank bank = banks[i];
1229
1230                 // Get the brightest neighbour node and propagate light from it
1231                 v3s16 n2p = getBrightestNeighbour(bank, p);
1232                 try{
1233                         //MapNode n2 = getNode(n2p);
1234                         lightNeighbors(bank, n2p, modified_blocks);
1235                 }
1236                 catch(InvalidPositionException &e)
1237                 {
1238                 }
1239         }
1240
1241         /*
1242                 Update information about whether day and night light differ
1243         */
1244         for(std::map<v3s16, MapBlock*>::iterator
1245                         i = modified_blocks.begin();
1246                         i != modified_blocks.end(); ++i)
1247         {
1248                 i->second->expireDayNightDiff();
1249         }
1250
1251         /*
1252                 Report for rollback
1253         */
1254         if(m_gamedef->rollback())
1255         {
1256                 RollbackNode rollback_newnode(this, p, m_gamedef);
1257                 RollbackAction action;
1258                 action.setSetNode(p, rollback_oldnode, rollback_newnode);
1259                 m_gamedef->rollback()->reportAction(action);
1260         }
1261
1262         /*
1263                 Add neighboring liquid nodes and this node to transform queue.
1264                 (it's vital for the node itself to get updated last.)
1265         */
1266         v3s16 dirs[7] = {
1267                 v3s16(0,0,1), // back
1268                 v3s16(0,1,0), // top
1269                 v3s16(1,0,0), // right
1270                 v3s16(0,0,-1), // front
1271                 v3s16(0,-1,0), // bottom
1272                 v3s16(-1,0,0), // left
1273                 v3s16(0,0,0), // self
1274         };
1275         for(u16 i=0; i<7; i++)
1276         {
1277                 v3s16 p2 = p + dirs[i];
1278
1279                 bool is_position_valid;
1280                 MapNode n2 = getNodeNoEx(p2, &is_position_valid);
1281                 if (is_position_valid
1282                                 && (ndef->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR))
1283                 {
1284                         m_transforming_liquid.push_back(p2);
1285                 }
1286         }
1287 }
1288
1289 bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
1290 {
1291         MapEditEvent event;
1292         event.type = remove_metadata ? MEET_ADDNODE : MEET_SWAPNODE;
1293         event.p = p;
1294         event.n = n;
1295
1296         bool succeeded = true;
1297         try{
1298                 std::map<v3s16, MapBlock*> modified_blocks;
1299                 addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
1300
1301                 // Copy modified_blocks to event
1302                 for(std::map<v3s16, MapBlock*>::iterator
1303                                 i = modified_blocks.begin();
1304                                 i != modified_blocks.end(); ++i)
1305                 {
1306                         event.modified_blocks.insert(i->first);
1307                 }
1308         }
1309         catch(InvalidPositionException &e){
1310                 succeeded = false;
1311         }
1312
1313         dispatchEvent(&event);
1314
1315         return succeeded;
1316 }
1317
1318 bool Map::removeNodeWithEvent(v3s16 p)
1319 {
1320         MapEditEvent event;
1321         event.type = MEET_REMOVENODE;
1322         event.p = p;
1323
1324         bool succeeded = true;
1325         try{
1326                 std::map<v3s16, MapBlock*> modified_blocks;
1327                 removeNodeAndUpdate(p, modified_blocks);
1328
1329                 // Copy modified_blocks to event
1330                 for(std::map<v3s16, MapBlock*>::iterator
1331                                 i = modified_blocks.begin();
1332                                 i != modified_blocks.end(); ++i)
1333                 {
1334                         event.modified_blocks.insert(i->first);
1335                 }
1336         }
1337         catch(InvalidPositionException &e){
1338                 succeeded = false;
1339         }
1340
1341         dispatchEvent(&event);
1342
1343         return succeeded;
1344 }
1345
1346 bool Map::getDayNightDiff(v3s16 blockpos)
1347 {
1348         try{
1349                 v3s16 p = blockpos + v3s16(0,0,0);
1350                 MapBlock *b = getBlockNoCreate(p);
1351                 if(b->getDayNightDiff())
1352                         return true;
1353         }
1354         catch(InvalidPositionException &e){}
1355         // Leading edges
1356         try{
1357                 v3s16 p = blockpos + v3s16(-1,0,0);
1358                 MapBlock *b = getBlockNoCreate(p);
1359                 if(b->getDayNightDiff())
1360                         return true;
1361         }
1362         catch(InvalidPositionException &e){}
1363         try{
1364                 v3s16 p = blockpos + v3s16(0,-1,0);
1365                 MapBlock *b = getBlockNoCreate(p);
1366                 if(b->getDayNightDiff())
1367                         return true;
1368         }
1369         catch(InvalidPositionException &e){}
1370         try{
1371                 v3s16 p = blockpos + v3s16(0,0,-1);
1372                 MapBlock *b = getBlockNoCreate(p);
1373                 if(b->getDayNightDiff())
1374                         return true;
1375         }
1376         catch(InvalidPositionException &e){}
1377         // Trailing edges
1378         try{
1379                 v3s16 p = blockpos + v3s16(1,0,0);
1380                 MapBlock *b = getBlockNoCreate(p);
1381                 if(b->getDayNightDiff())
1382                         return true;
1383         }
1384         catch(InvalidPositionException &e){}
1385         try{
1386                 v3s16 p = blockpos + v3s16(0,1,0);
1387                 MapBlock *b = getBlockNoCreate(p);
1388                 if(b->getDayNightDiff())
1389                         return true;
1390         }
1391         catch(InvalidPositionException &e){}
1392         try{
1393                 v3s16 p = blockpos + v3s16(0,0,1);
1394                 MapBlock *b = getBlockNoCreate(p);
1395                 if(b->getDayNightDiff())
1396                         return true;
1397         }
1398         catch(InvalidPositionException &e){}
1399
1400         return false;
1401 }
1402
1403 struct TimeOrderedMapBlock {
1404         MapSector *sect;
1405         MapBlock *block;
1406
1407         TimeOrderedMapBlock(MapSector *sect, MapBlock *block) :
1408                 sect(sect),
1409                 block(block)
1410         {}
1411
1412         bool operator<(const TimeOrderedMapBlock &b) const
1413         {
1414                 return block->getUsageTimer() < b.block->getUsageTimer();
1415         };
1416 };
1417
1418 /*
1419         Updates usage timers
1420 */
1421 void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
1422                 std::vector<v3s16> *unloaded_blocks)
1423 {
1424         bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
1425
1426         // Profile modified reasons
1427         Profiler modprofiler;
1428
1429         std::vector<v2s16> sector_deletion_queue;
1430         u32 deleted_blocks_count = 0;
1431         u32 saved_blocks_count = 0;
1432         u32 block_count_all = 0;
1433
1434         beginSave();
1435
1436         // If there is no practical limit, we spare creation of mapblock_queue
1437         if (max_loaded_blocks == U32_MAX) {
1438                 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
1439                                 si != m_sectors.end(); ++si) {
1440                         MapSector *sector = si->second;
1441
1442                         bool all_blocks_deleted = true;
1443
1444                         MapBlockVect blocks;
1445                         sector->getBlocks(blocks);
1446
1447                         for (MapBlockVect::iterator i = blocks.begin();
1448                                         i != blocks.end(); ++i) {
1449                                 MapBlock *block = (*i);
1450
1451                                 block->incrementUsageTimer(dtime);
1452
1453                                 if (block->refGet() == 0
1454                                                 && block->getUsageTimer() > unload_timeout) {
1455                                         v3s16 p = block->getPos();
1456
1457                                         // Save if modified
1458                                         if (block->getModified() != MOD_STATE_CLEAN
1459                                                         && save_before_unloading) {
1460                                                 modprofiler.add(block->getModifiedReasonString(), 1);
1461                                                 if (!saveBlock(block))
1462                                                         continue;
1463                                                 saved_blocks_count++;
1464                                         }
1465
1466                                         // Delete from memory
1467                                         sector->deleteBlock(block);
1468
1469                                         if (unloaded_blocks)
1470                                                 unloaded_blocks->push_back(p);
1471
1472                                         deleted_blocks_count++;
1473                                 } else {
1474                                         all_blocks_deleted = false;
1475                                         block_count_all++;
1476                                 }
1477                         }
1478
1479                         if (all_blocks_deleted) {
1480                                 sector_deletion_queue.push_back(si->first);
1481                         }
1482                 }
1483         } else {
1484                 std::priority_queue<TimeOrderedMapBlock> mapblock_queue;
1485                 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
1486                                 si != m_sectors.end(); ++si) {
1487                         MapSector *sector = si->second;
1488
1489                         MapBlockVect blocks;
1490                         sector->getBlocks(blocks);
1491
1492                         for(MapBlockVect::iterator i = blocks.begin();
1493                                         i != blocks.end(); ++i) {
1494                                 MapBlock *block = (*i);
1495
1496                                 block->incrementUsageTimer(dtime);
1497                                 mapblock_queue.push(TimeOrderedMapBlock(sector, block));
1498                         }
1499                 }
1500                 block_count_all = mapblock_queue.size();
1501                 // Delete old blocks, and blocks over the limit from the memory
1502                 while (!mapblock_queue.empty() && (mapblock_queue.size() > max_loaded_blocks
1503                                 || mapblock_queue.top().block->getUsageTimer() > unload_timeout)) {
1504                         TimeOrderedMapBlock b = mapblock_queue.top();
1505                         mapblock_queue.pop();
1506
1507                         MapBlock *block = b.block;
1508
1509                         if (block->refGet() != 0)
1510                                 continue;
1511
1512                         v3s16 p = block->getPos();
1513
1514                         // Save if modified
1515                         if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
1516                                 modprofiler.add(block->getModifiedReasonString(), 1);
1517                                 if (!saveBlock(block))
1518                                         continue;
1519                                 saved_blocks_count++;
1520                         }
1521
1522                         // Delete from memory
1523                         b.sect->deleteBlock(block);
1524
1525                         if (unloaded_blocks)
1526                                 unloaded_blocks->push_back(p);
1527
1528                         deleted_blocks_count++;
1529                         block_count_all--;
1530                 }
1531                 // Delete empty sectors
1532                 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
1533                         si != m_sectors.end(); ++si) {
1534                         if (si->second->empty()) {
1535                                 sector_deletion_queue.push_back(si->first);
1536                         }
1537                 }
1538         }
1539         endSave();
1540
1541         // Finally delete the empty sectors
1542         deleteSectors(sector_deletion_queue);
1543
1544         if(deleted_blocks_count != 0)
1545         {
1546                 PrintInfo(infostream); // ServerMap/ClientMap:
1547                 infostream<<"Unloaded "<<deleted_blocks_count
1548                                 <<" blocks from memory";
1549                 if(save_before_unloading)
1550                         infostream<<", of which "<<saved_blocks_count<<" were written";
1551                 infostream<<", "<<block_count_all<<" blocks in memory";
1552                 infostream<<"."<<std::endl;
1553                 if(saved_blocks_count != 0){
1554                         PrintInfo(infostream); // ServerMap/ClientMap:
1555                         infostream<<"Blocks modified by: "<<std::endl;
1556                         modprofiler.print(infostream);
1557                 }
1558         }
1559 }
1560
1561 void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks)
1562 {
1563         timerUpdate(0.0, -1.0, 0, unloaded_blocks);
1564 }
1565
1566 void Map::deleteSectors(std::vector<v2s16> &sectorList)
1567 {
1568         for(std::vector<v2s16>::iterator j = sectorList.begin();
1569                 j != sectorList.end(); ++j) {
1570                 MapSector *sector = m_sectors[*j];
1571                 // If sector is in sector cache, remove it from there
1572                 if(m_sector_cache == sector)
1573                         m_sector_cache = NULL;
1574                 // Remove from map and delete
1575                 m_sectors.erase(*j);
1576                 delete sector;
1577         }
1578 }
1579
1580 void Map::PrintInfo(std::ostream &out)
1581 {
1582         out<<"Map: ";
1583 }
1584
1585 #define WATER_DROP_BOOST 4
1586
1587 enum NeighborType {
1588         NEIGHBOR_UPPER,
1589         NEIGHBOR_SAME_LEVEL,
1590         NEIGHBOR_LOWER
1591 };
1592 struct NodeNeighbor {
1593         MapNode n;
1594         NeighborType t;
1595         v3s16 p;
1596         bool l; //can liquid
1597
1598         NodeNeighbor()
1599                 : n(CONTENT_AIR)
1600         { }
1601
1602         NodeNeighbor(const MapNode &node, NeighborType n_type, v3s16 pos)
1603                 : n(node),
1604                   t(n_type),
1605                   p(pos)
1606         { }
1607 };
1608
1609 void Map::transforming_liquid_add(v3s16 p) {
1610         m_transforming_liquid.push_back(p);
1611 }
1612
1613 s32 Map::transforming_liquid_size() {
1614         return m_transforming_liquid.size();
1615 }
1616
1617 void Map::transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks)
1618 {
1619
1620         INodeDefManager *nodemgr = m_gamedef->ndef();
1621
1622         DSTACK(FUNCTION_NAME);
1623         //TimeTaker timer("transformLiquids()");
1624
1625         u32 loopcount = 0;
1626         u32 initial_size = m_transforming_liquid.size();
1627
1628         /*if(initial_size != 0)
1629                 infostream<<"transformLiquids(): initial_size="<<initial_size<<std::endl;*/
1630
1631         // list of nodes that due to viscosity have not reached their max level height
1632         std::deque<v3s16> must_reflow;
1633
1634         // List of MapBlocks that will require a lighting update (due to lava)
1635         std::map<v3s16, MapBlock*> lighting_modified_blocks;
1636
1637         u32 liquid_loop_max = g_settings->getS32("liquid_loop_max");
1638         u32 loop_max = liquid_loop_max;
1639
1640 #if 0
1641
1642         /* If liquid_loop_max is not keeping up with the queue size increase
1643          * loop_max up to a maximum of liquid_loop_max * dedicated_server_step.
1644          */
1645         if (m_transforming_liquid.size() > loop_max * 2) {
1646                 // "Burst" mode
1647                 float server_step = g_settings->getFloat("dedicated_server_step");
1648                 if (m_transforming_liquid_loop_count_multiplier - 1.0 < server_step)
1649                         m_transforming_liquid_loop_count_multiplier *= 1.0 + server_step / 10;
1650         } else {
1651                 m_transforming_liquid_loop_count_multiplier = 1.0;
1652         }
1653
1654         loop_max *= m_transforming_liquid_loop_count_multiplier;
1655 #endif
1656
1657         while (m_transforming_liquid.size() != 0)
1658         {
1659                 // This should be done here so that it is done when continue is used
1660                 if (loopcount >= initial_size || loopcount >= loop_max)
1661                         break;
1662                 loopcount++;
1663
1664                 /*
1665                         Get a queued transforming liquid node
1666                 */
1667                 v3s16 p0 = m_transforming_liquid.front();
1668                 m_transforming_liquid.pop_front();
1669
1670                 MapNode n0 = getNodeNoEx(p0);
1671
1672                 /*
1673                         Collect information about current node
1674                  */
1675                 s8 liquid_level = -1;
1676                 content_t liquid_kind = CONTENT_IGNORE;
1677                 content_t floodable_node = CONTENT_AIR;
1678                 ContentFeatures cf = nodemgr->get(n0);
1679                 LiquidType liquid_type = cf.liquid_type;
1680                 switch (liquid_type) {
1681                         case LIQUID_SOURCE:
1682                                 liquid_level = LIQUID_LEVEL_SOURCE;
1683                                 liquid_kind = nodemgr->getId(cf.liquid_alternative_flowing);
1684                                 break;
1685                         case LIQUID_FLOWING:
1686                                 liquid_level = (n0.param2 & LIQUID_LEVEL_MASK);
1687                                 liquid_kind = n0.getContent();
1688                                 break;
1689                         case LIQUID_NONE:
1690                                 // if this node is 'floodable', it *could* be transformed
1691                                 // into a liquid, otherwise, continue with the next node.
1692                                 if (!cf.floodable)
1693                                         continue;
1694                                 floodable_node = n0.getContent();
1695                                 liquid_kind = CONTENT_AIR;
1696                                 break;
1697                 }
1698
1699                 /*
1700                         Collect information about the environment
1701                  */
1702                 const v3s16 *dirs = g_6dirs;
1703                 NodeNeighbor sources[6]; // surrounding sources
1704                 int num_sources = 0;
1705                 NodeNeighbor flows[6]; // surrounding flowing liquid nodes
1706                 int num_flows = 0;
1707                 NodeNeighbor airs[6]; // surrounding air
1708                 int num_airs = 0;
1709                 NodeNeighbor neutrals[6]; // nodes that are solid or another kind of liquid
1710                 int num_neutrals = 0;
1711                 bool flowing_down = false;
1712                 for (u16 i = 0; i < 6; i++) {
1713                         NeighborType nt = NEIGHBOR_SAME_LEVEL;
1714                         switch (i) {
1715                                 case 1:
1716                                         nt = NEIGHBOR_UPPER;
1717                                         break;
1718                                 case 4:
1719                                         nt = NEIGHBOR_LOWER;
1720                                         break;
1721                         }
1722                         v3s16 npos = p0 + dirs[i];
1723                         NodeNeighbor nb(getNodeNoEx(npos), nt, npos);
1724                         ContentFeatures cfnb = nodemgr->get(nb.n);
1725                         switch (nodemgr->get(nb.n.getContent()).liquid_type) {
1726                                 case LIQUID_NONE:
1727                                         if (cfnb.floodable) {
1728                                                 airs[num_airs++] = nb;
1729                                                 // if the current node is a water source the neighbor
1730                                                 // should be enqueded for transformation regardless of whether the
1731                                                 // current node changes or not.
1732                                                 if (nb.t != NEIGHBOR_UPPER && liquid_type != LIQUID_NONE)
1733                                                         m_transforming_liquid.push_back(npos);
1734                                                 // if the current node happens to be a flowing node, it will start to flow down here.
1735                                                 if (nb.t == NEIGHBOR_LOWER)
1736                                                         flowing_down = true;
1737                                         } else {
1738                                                 neutrals[num_neutrals++] = nb;
1739                                                 // If neutral below is ignore prevent water spreading outwards
1740                                                 if (nb.t == NEIGHBOR_LOWER &&
1741                                                                 nb.n.getContent() == CONTENT_IGNORE)
1742                                                         flowing_down = true;
1743                                         }
1744                                         break;
1745                                 case LIQUID_SOURCE:
1746                                         // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
1747                                         if (liquid_kind == CONTENT_AIR)
1748                                                 liquid_kind = nodemgr->getId(cfnb.liquid_alternative_flowing);
1749                                         if (nodemgr->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
1750                                                 neutrals[num_neutrals++] = nb;
1751                                         } else {
1752                                                 // Do not count bottom source, it will screw things up
1753                                                 if(dirs[i].Y != -1)
1754                                                         sources[num_sources++] = nb;
1755                                         }
1756                                         break;
1757                                 case LIQUID_FLOWING:
1758                                         // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
1759                                         if (liquid_kind == CONTENT_AIR)
1760                                                 liquid_kind = nodemgr->getId(cfnb.liquid_alternative_flowing);
1761                                         if (nodemgr->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
1762                                                 neutrals[num_neutrals++] = nb;
1763                                         } else {
1764                                                 flows[num_flows++] = nb;
1765                                                 if (nb.t == NEIGHBOR_LOWER)
1766                                                         flowing_down = true;
1767                                         }
1768                                         break;
1769                         }
1770                 }
1771
1772                 /*
1773                         decide on the type (and possibly level) of the current node
1774                  */
1775                 content_t new_node_content;
1776                 s8 new_node_level = -1;
1777                 s8 max_node_level = -1;
1778
1779                 u8 range = nodemgr->get(liquid_kind).liquid_range;
1780                 if (range > LIQUID_LEVEL_MAX + 1)
1781                         range = LIQUID_LEVEL_MAX + 1;
1782
1783                 if ((num_sources >= 2 && nodemgr->get(liquid_kind).liquid_renewable) || liquid_type == LIQUID_SOURCE) {
1784                         // liquid_kind will be set to either the flowing alternative of the node (if it's a liquid)
1785                         // or the flowing alternative of the first of the surrounding sources (if it's air), so
1786                         // it's perfectly safe to use liquid_kind here to determine the new node content.
1787                         new_node_content = nodemgr->getId(nodemgr->get(liquid_kind).liquid_alternative_source);
1788                 } else if (num_sources >= 1 && sources[0].t != NEIGHBOR_LOWER) {
1789                         // liquid_kind is set properly, see above
1790                         max_node_level = new_node_level = LIQUID_LEVEL_MAX;
1791                         if (new_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
1792                                 new_node_content = liquid_kind;
1793                         else
1794                                 new_node_content = floodable_node;
1795                 } else {
1796                         // no surrounding sources, so get the maximum level that can flow into this node
1797                         for (u16 i = 0; i < num_flows; i++) {
1798                                 u8 nb_liquid_level = (flows[i].n.param2 & LIQUID_LEVEL_MASK);
1799                                 switch (flows[i].t) {
1800                                         case NEIGHBOR_UPPER:
1801                                                 if (nb_liquid_level + WATER_DROP_BOOST > max_node_level) {
1802                                                         max_node_level = LIQUID_LEVEL_MAX;
1803                                                         if (nb_liquid_level + WATER_DROP_BOOST < LIQUID_LEVEL_MAX)
1804                                                                 max_node_level = nb_liquid_level + WATER_DROP_BOOST;
1805                                                 } else if (nb_liquid_level > max_node_level) {
1806                                                         max_node_level = nb_liquid_level;
1807                                                 }
1808                                                 break;
1809                                         case NEIGHBOR_LOWER:
1810                                                 break;
1811                                         case NEIGHBOR_SAME_LEVEL:
1812                                                 if ((flows[i].n.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK &&
1813                                                                 nb_liquid_level > 0 && nb_liquid_level - 1 > max_node_level)
1814                                                         max_node_level = nb_liquid_level - 1;
1815                                                 break;
1816                                 }
1817                         }
1818
1819                         u8 viscosity = nodemgr->get(liquid_kind).liquid_viscosity;
1820                         if (viscosity > 1 && max_node_level != liquid_level) {
1821                                 // amount to gain, limited by viscosity
1822                                 // must be at least 1 in absolute value
1823                                 s8 level_inc = max_node_level - liquid_level;
1824                                 if (level_inc < -viscosity || level_inc > viscosity)
1825                                         new_node_level = liquid_level + level_inc/viscosity;
1826                                 else if (level_inc < 0)
1827                                         new_node_level = liquid_level - 1;
1828                                 else if (level_inc > 0)
1829                                         new_node_level = liquid_level + 1;
1830                                 if (new_node_level != max_node_level)
1831                                         must_reflow.push_back(p0);
1832                         } else {
1833                                 new_node_level = max_node_level;
1834                         }
1835
1836                         if (max_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
1837                                 new_node_content = liquid_kind;
1838                         else
1839                                 new_node_content = floodable_node;
1840
1841                 }
1842
1843                 /*
1844                         check if anything has changed. if not, just continue with the next node.
1845                  */
1846                 if (new_node_content == n0.getContent() &&
1847                                 (nodemgr->get(n0.getContent()).liquid_type != LIQUID_FLOWING ||
1848                                 ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level &&
1849                                 ((n0.param2 & LIQUID_FLOW_DOWN_MASK) == LIQUID_FLOW_DOWN_MASK)
1850                                 == flowing_down)))
1851                         continue;
1852
1853
1854                 /*
1855                         update the current node
1856                  */
1857                 MapNode n00 = n0;
1858                 //bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
1859                 if (nodemgr->get(new_node_content).liquid_type == LIQUID_FLOWING) {
1860                         // set level to last 3 bits, flowing down bit to 4th bit
1861                         n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK);
1862                 } else {
1863                         // set the liquid level and flow bit to 0
1864                         n0.param2 = ~(LIQUID_LEVEL_MASK | LIQUID_FLOW_DOWN_MASK);
1865                 }
1866                 n0.setContent(new_node_content);
1867
1868                 // Find out whether there is a suspect for this action
1869                 std::string suspect;
1870                 if (m_gamedef->rollback())
1871                         suspect = m_gamedef->rollback()->getSuspect(p0, 83, 1);
1872
1873                 if (m_gamedef->rollback() && !suspect.empty()) {
1874                         // Blame suspect
1875                         RollbackScopeActor rollback_scope(m_gamedef->rollback(), suspect, true);
1876                         // Get old node for rollback
1877                         RollbackNode rollback_oldnode(this, p0, m_gamedef);
1878                         // Set node
1879                         setNode(p0, n0);
1880                         // Report
1881                         RollbackNode rollback_newnode(this, p0, m_gamedef);
1882                         RollbackAction action;
1883                         action.setSetNode(p0, rollback_oldnode, rollback_newnode);
1884                         m_gamedef->rollback()->reportAction(action);
1885                 } else {
1886                         // Set node
1887                         setNode(p0, n0);
1888                 }
1889
1890                 v3s16 blockpos = getNodeBlockPos(p0);
1891                 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1892                 if (block != NULL) {
1893                         modified_blocks[blockpos] =  block;
1894                         // If new or old node emits light, MapBlock requires lighting update
1895                         if (nodemgr->get(n0).light_source != 0 ||
1896                                         nodemgr->get(n00).light_source != 0)
1897                                 lighting_modified_blocks[block->getPos()] = block;
1898                 }
1899
1900                 /*
1901                         enqueue neighbors for update if neccessary
1902                  */
1903                 switch (nodemgr->get(n0.getContent()).liquid_type) {
1904                         case LIQUID_SOURCE:
1905                         case LIQUID_FLOWING:
1906                                 // make sure source flows into all neighboring nodes
1907                                 for (u16 i = 0; i < num_flows; i++)
1908                                         if (flows[i].t != NEIGHBOR_UPPER)
1909                                                 m_transforming_liquid.push_back(flows[i].p);
1910                                 for (u16 i = 0; i < num_airs; i++)
1911                                         if (airs[i].t != NEIGHBOR_UPPER)
1912                                                 m_transforming_liquid.push_back(airs[i].p);
1913                                 break;
1914                         case LIQUID_NONE:
1915                                 // this flow has turned to air; neighboring flows might need to do the same
1916                                 for (u16 i = 0; i < num_flows; i++)
1917                                         m_transforming_liquid.push_back(flows[i].p);
1918                                 break;
1919                 }
1920         }
1921         //infostream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
1922
1923         for (std::deque<v3s16>::iterator iter = must_reflow.begin(); iter != must_reflow.end(); ++iter)
1924                 m_transforming_liquid.push_back(*iter);
1925
1926         updateLighting(lighting_modified_blocks, modified_blocks);
1927
1928
1929         /* ----------------------------------------------------------------------
1930          * Manage the queue so that it does not grow indefinately
1931          */
1932         u16 time_until_purge = g_settings->getU16("liquid_queue_purge_time");
1933
1934         if (time_until_purge == 0)
1935                 return; // Feature disabled
1936
1937         time_until_purge *= 1000;       // seconds -> milliseconds
1938
1939         u32 curr_time = getTime(PRECISION_MILLI);
1940         u32 prev_unprocessed = m_unprocessed_count;
1941         m_unprocessed_count = m_transforming_liquid.size();
1942
1943         // if unprocessed block count is decreasing or stable
1944         if (m_unprocessed_count <= prev_unprocessed) {
1945                 m_queue_size_timer_started = false;
1946         } else {
1947                 if (!m_queue_size_timer_started)
1948                         m_inc_trending_up_start_time = curr_time;
1949                 m_queue_size_timer_started = true;
1950         }
1951
1952         // Account for curr_time overflowing
1953         if (m_queue_size_timer_started && m_inc_trending_up_start_time > curr_time)
1954                 m_queue_size_timer_started = false;
1955
1956         /* If the queue has been growing for more than liquid_queue_purge_time seconds
1957          * and the number of unprocessed blocks is still > liquid_loop_max then we
1958          * cannot keep up; dump the oldest blocks from the queue so that the queue
1959          * has liquid_loop_max items in it
1960          */
1961         if (m_queue_size_timer_started
1962                         && curr_time - m_inc_trending_up_start_time > time_until_purge
1963                         && m_unprocessed_count > liquid_loop_max) {
1964
1965                 size_t dump_qty = m_unprocessed_count - liquid_loop_max;
1966
1967                 infostream << "transformLiquids(): DUMPING " << dump_qty
1968                            << " blocks from the queue" << std::endl;
1969
1970                 while (dump_qty--)
1971                         m_transforming_liquid.pop_front();
1972
1973                 m_queue_size_timer_started = false; // optimistically assume we can keep up now
1974                 m_unprocessed_count = m_transforming_liquid.size();
1975         }
1976 }
1977
1978 std::vector<v3s16> Map::findNodesWithMetadata(v3s16 p1, v3s16 p2)
1979 {
1980         std::vector<v3s16> positions_with_meta;
1981
1982         sortBoxVerticies(p1, p2);
1983         v3s16 bpmin = getNodeBlockPos(p1);
1984         v3s16 bpmax = getNodeBlockPos(p2);
1985
1986         VoxelArea area(p1, p2);
1987
1988         for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
1989         for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
1990         for (s16 x = bpmin.X; x <= bpmax.X; x++) {
1991                 v3s16 blockpos(x, y, z);
1992
1993                 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1994                 if (!block) {
1995                         verbosestream << "Map::getNodeMetadata(): Need to emerge "
1996                                 << PP(blockpos) << std::endl;
1997                         block = emergeBlock(blockpos, false);
1998                 }
1999                 if (!block) {
2000                         infostream << "WARNING: Map::getNodeMetadata(): Block not found"
2001                                 << std::endl;
2002                         continue;
2003                 }
2004
2005                 v3s16 p_base = blockpos * MAP_BLOCKSIZE;
2006                 std::vector<v3s16> keys = block->m_node_metadata.getAllKeys();
2007                 for (size_t i = 0; i != keys.size(); i++) {
2008                         v3s16 p(keys[i] + p_base);
2009                         if (!area.contains(p))
2010                                 continue;
2011
2012                         positions_with_meta.push_back(p);
2013                 }
2014         }
2015
2016         return positions_with_meta;
2017 }
2018
2019 NodeMetadata *Map::getNodeMetadata(v3s16 p)
2020 {
2021         v3s16 blockpos = getNodeBlockPos(p);
2022         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
2023         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2024         if(!block){
2025                 infostream<<"Map::getNodeMetadata(): Need to emerge "
2026                                 <<PP(blockpos)<<std::endl;
2027                 block = emergeBlock(blockpos, false);
2028         }
2029         if(!block){
2030                 warningstream<<"Map::getNodeMetadata(): Block not found"
2031                                 <<std::endl;
2032                 return NULL;
2033         }
2034         NodeMetadata *meta = block->m_node_metadata.get(p_rel);
2035         return meta;
2036 }
2037
2038 bool Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
2039 {
2040         v3s16 blockpos = getNodeBlockPos(p);
2041         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
2042         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2043         if(!block){
2044                 infostream<<"Map::setNodeMetadata(): Need to emerge "
2045                                 <<PP(blockpos)<<std::endl;
2046                 block = emergeBlock(blockpos, false);
2047         }
2048         if(!block){
2049                 warningstream<<"Map::setNodeMetadata(): Block not found"
2050                                 <<std::endl;
2051                 return false;
2052         }
2053         block->m_node_metadata.set(p_rel, meta);
2054         return true;
2055 }
2056
2057 void Map::removeNodeMetadata(v3s16 p)
2058 {
2059         v3s16 blockpos = getNodeBlockPos(p);
2060         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
2061         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2062         if(block == NULL)
2063         {
2064                 warningstream<<"Map::removeNodeMetadata(): Block not found"
2065                                 <<std::endl;
2066                 return;
2067         }
2068         block->m_node_metadata.remove(p_rel);
2069 }
2070
2071 NodeTimer Map::getNodeTimer(v3s16 p)
2072 {
2073         v3s16 blockpos = getNodeBlockPos(p);
2074         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
2075         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2076         if(!block){
2077                 infostream<<"Map::getNodeTimer(): Need to emerge "
2078                                 <<PP(blockpos)<<std::endl;
2079                 block = emergeBlock(blockpos, false);
2080         }
2081         if(!block){
2082                 warningstream<<"Map::getNodeTimer(): Block not found"
2083                                 <<std::endl;
2084                 return NodeTimer();
2085         }
2086         NodeTimer t = block->m_node_timers.get(p_rel);
2087         return t;
2088 }
2089
2090 void Map::setNodeTimer(v3s16 p, NodeTimer t)
2091 {
2092         v3s16 blockpos = getNodeBlockPos(p);
2093         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
2094         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2095         if(!block){
2096                 infostream<<"Map::setNodeTimer(): Need to emerge "
2097                                 <<PP(blockpos)<<std::endl;
2098                 block = emergeBlock(blockpos, false);
2099         }
2100         if(!block){
2101                 warningstream<<"Map::setNodeTimer(): Block not found"
2102                                 <<std::endl;
2103                 return;
2104         }
2105         block->m_node_timers.set(p_rel, t);
2106 }
2107
2108 void Map::removeNodeTimer(v3s16 p)
2109 {
2110         v3s16 blockpos = getNodeBlockPos(p);
2111         v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
2112         MapBlock *block = getBlockNoCreateNoEx(blockpos);
2113         if(block == NULL)
2114         {
2115                 warningstream<<"Map::removeNodeTimer(): Block not found"
2116                                 <<std::endl;
2117                 return;
2118         }
2119         block->m_node_timers.remove(p_rel);
2120 }
2121
2122 /*
2123         ServerMap
2124 */
2125 ServerMap::ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge):
2126         Map(dout_server, gamedef),
2127         m_emerge(emerge),
2128         m_map_metadata_changed(true)
2129 {
2130         verbosestream<<FUNCTION_NAME<<std::endl;
2131
2132         /*
2133                 Try to load map; if not found, create a new one.
2134         */
2135
2136         // Determine which database backend to use
2137         std::string conf_path = savedir + DIR_DELIM + "world.mt";
2138         Settings conf;
2139         bool succeeded = conf.readConfigFile(conf_path.c_str());
2140         if (!succeeded || !conf.exists("backend")) {
2141                 // fall back to sqlite3
2142                 conf.set("backend", "sqlite3");
2143         }
2144         std::string backend = conf.get("backend");
2145         dbase = createDatabase(backend, savedir, conf);
2146
2147         if (!conf.updateConfigFile(conf_path.c_str()))
2148                 errorstream << "ServerMap::ServerMap(): Failed to update world.mt!" << std::endl;
2149
2150         m_savedir = savedir;
2151         m_map_saving_enabled = false;
2152
2153         try
2154         {
2155                 // If directory exists, check contents and load if possible
2156                 if(fs::PathExists(m_savedir))
2157                 {
2158                         // If directory is empty, it is safe to save into it.
2159                         if(fs::GetDirListing(m_savedir).size() == 0)
2160                         {
2161                                 infostream<<"ServerMap: Empty save directory is valid."
2162                                                 <<std::endl;
2163                                 m_map_saving_enabled = true;
2164                         }
2165                         else
2166                         {
2167                                 try{
2168                                         // Load map metadata (seed, chunksize)
2169                                         loadMapMeta();
2170                                 }
2171                                 catch(SettingNotFoundException &e){
2172                                         infostream<<"ServerMap:  Some metadata not found."
2173                                                           <<" Using default settings."<<std::endl;
2174                                 }
2175                                 catch(FileNotGoodException &e){
2176                                         warningstream<<"Could not load map metadata"
2177                                                         //<<" Disabling chunk-based generator."
2178                                                         <<std::endl;
2179                                         //m_chunksize = 0;
2180                                 }
2181
2182                                 infostream<<"ServerMap: Successfully loaded map "
2183                                                 <<"metadata from "<<savedir
2184                                                 <<", assuming valid save directory."
2185                                                 <<" seed="<< m_emerge->params.seed <<"."
2186                                                 <<std::endl;
2187
2188                                 m_map_saving_enabled = true;
2189                                 // Map loaded, not creating new one
2190                                 return;
2191                         }
2192                 }
2193                 // If directory doesn't exist, it is safe to save to it
2194                 else{
2195                         m_map_saving_enabled = true;
2196                 }
2197         }
2198         catch(std::exception &e)
2199         {
2200                 warningstream<<"ServerMap: Failed to load map from "<<savedir
2201                                 <<", exception: "<<e.what()<<std::endl;
2202                 infostream<<"Please remove the map or fix it."<<std::endl;
2203                 warningstream<<"Map saving will be disabled."<<std::endl;
2204         }
2205
2206         infostream<<"Initializing new map."<<std::endl;
2207
2208         // Create zero sector
2209         emergeSector(v2s16(0,0));
2210
2211         // Initially write whole map
2212         save(MOD_STATE_CLEAN);
2213 }
2214
2215 ServerMap::~ServerMap()
2216 {
2217         verbosestream<<FUNCTION_NAME<<std::endl;
2218
2219         try
2220         {
2221                 if(m_map_saving_enabled)
2222                 {
2223                         // Save only changed parts
2224                         save(MOD_STATE_WRITE_AT_UNLOAD);
2225                         infostream<<"ServerMap: Saved map to "<<m_savedir<<std::endl;
2226                 }
2227                 else
2228                 {
2229                         infostream<<"ServerMap: Map not saved"<<std::endl;
2230                 }
2231         }
2232         catch(std::exception &e)
2233         {
2234                 infostream<<"ServerMap: Failed to save map to "<<m_savedir
2235                                 <<", exception: "<<e.what()<<std::endl;
2236         }
2237
2238         /*
2239                 Close database if it was opened
2240         */
2241         delete dbase;
2242
2243 #if 0
2244         /*
2245                 Free all MapChunks
2246         */
2247         core::map<v2s16, MapChunk*>::Iterator i = m_chunks.getIterator();
2248         for(; i.atEnd() == false; i++)
2249         {
2250                 MapChunk *chunk = i.getNode()->getValue();
2251                 delete chunk;
2252         }
2253 #endif
2254 }
2255
2256 u64 ServerMap::getSeed()
2257 {
2258         return m_emerge->params.seed;
2259 }
2260
2261 s16 ServerMap::getWaterLevel()
2262 {
2263         return m_emerge->params.water_level;
2264 }
2265
2266 bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data)
2267 {
2268         s16 csize = m_emerge->params.chunksize;
2269         v3s16 bpmin = EmergeManager::getContainingChunk(blockpos, csize);
2270         v3s16 bpmax = bpmin + v3s16(1, 1, 1) * (csize - 1);
2271
2272         bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
2273         EMERGE_DBG_OUT("initBlockMake(): " PP(bpmin) " - " PP(bpmax));
2274
2275         v3s16 extra_borders(1, 1, 1);
2276         v3s16 full_bpmin = bpmin - extra_borders;
2277         v3s16 full_bpmax = bpmax + extra_borders;
2278
2279         // Do nothing if not inside limits (+-1 because of neighbors)
2280         if (blockpos_over_limit(full_bpmin) ||
2281                 blockpos_over_limit(full_bpmax))
2282                 return false;
2283
2284         data->seed = m_emerge->params.seed;
2285         data->blockpos_min = bpmin;
2286         data->blockpos_max = bpmax;
2287         data->blockpos_requested = blockpos;
2288         data->nodedef = m_gamedef->ndef();
2289
2290         /*
2291                 Create the whole area of this and the neighboring blocks
2292         */
2293         for (s16 x = full_bpmin.X; x <= full_bpmax.X; x++)
2294         for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++) {
2295                 v2s16 sectorpos(x, z);
2296                 // Sector metadata is loaded from disk if not already loaded.
2297                 ServerMapSector *sector = createSector(sectorpos);
2298                 FATAL_ERROR_IF(sector == NULL, "createSector() failed");
2299
2300                 for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) {
2301                         v3s16 p(x, y, z);
2302
2303                         MapBlock *block = emergeBlock(p, false);
2304                         if (block == NULL) {
2305                                 block = createBlock(p);
2306
2307                                 // Block gets sunlight if this is true.
2308                                 // Refer to the map generator heuristics.
2309                                 bool ug = m_emerge->isBlockUnderground(p);
2310                                 block->setIsUnderground(ug);
2311                         }
2312                 }
2313         }
2314
2315         /*
2316                 Now we have a big empty area.
2317
2318                 Make a ManualMapVoxelManipulator that contains this and the
2319                 neighboring blocks
2320         */
2321
2322         data->vmanip = new MMVManip(this);
2323         data->vmanip->initialEmerge(full_bpmin, full_bpmax);
2324
2325         // Note: we may need this again at some point.
2326 #if 0
2327         // Ensure none of the blocks to be generated were marked as
2328         // containing CONTENT_IGNORE
2329         for (s16 z = blockpos_min.Z; z <= blockpos_max.Z; z++) {
2330                 for (s16 y = blockpos_min.Y; y <= blockpos_max.Y; y++) {
2331                         for (s16 x = blockpos_min.X; x <= blockpos_max.X; x++) {
2332                                 core::map<v3s16, u8>::Node *n;
2333                                 n = data->vmanip->m_loaded_blocks.find(v3s16(x, y, z));
2334                                 if (n == NULL)
2335                                         continue;
2336                                 u8 flags = n->getValue();
2337                                 flags &= ~VMANIP_BLOCK_CONTAINS_CIGNORE;
2338                                 n->setValue(flags);
2339                         }
2340                 }
2341         }
2342 #endif
2343
2344         // Data is ready now.
2345         return true;
2346 }
2347
2348 void ServerMap::finishBlockMake(BlockMakeData *data,
2349         std::map<v3s16, MapBlock*> *changed_blocks)
2350 {
2351         v3s16 bpmin = data->blockpos_min;
2352         v3s16 bpmax = data->blockpos_max;
2353
2354         v3s16 extra_borders(1, 1, 1);
2355         v3s16 full_bpmin = bpmin - extra_borders;
2356         v3s16 full_bpmax = bpmax + extra_borders;
2357
2358         bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
2359         EMERGE_DBG_OUT("finishBlockMake(): " PP(bpmin) " - " PP(bpmax));
2360
2361         /*
2362                 Set lighting to non-expired state in all of them.
2363                 This is cheating, but it is not fast enough if all of them
2364                 would actually be updated.
2365         */
2366         for (s16 x = full_bpmin.X; x <= full_bpmax.X; x++)
2367         for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++)
2368         for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) {
2369                 MapBlock *block = emergeBlock(v3s16(x, y, z), false);
2370                 if (!block)
2371                         continue;
2372
2373                 block->setLightingExpired(false);
2374         }
2375
2376         /*
2377                 Blit generated stuff to map
2378                 NOTE: blitBackAll adds nearly everything to changed_blocks
2379         */
2380         data->vmanip->blitBackAll(changed_blocks);
2381
2382         EMERGE_DBG_OUT("finishBlockMake: changed_blocks.size()="
2383                 << changed_blocks->size());
2384
2385         /*
2386                 Copy transforming liquid information
2387         */
2388         while (data->transforming_liquid.size()) {
2389                 m_transforming_liquid.push_back(data->transforming_liquid.front());
2390                 data->transforming_liquid.pop_front();
2391         }
2392
2393         for (std::map<v3s16, MapBlock *>::iterator
2394                         it = changed_blocks->begin();
2395                         it != changed_blocks->end(); ++it) {
2396                 MapBlock *block = it->second;
2397                 if (!block)
2398                         continue;
2399                 /*
2400                         Update day/night difference cache of the MapBlocks
2401                 */
2402                 block->expireDayNightDiff();
2403                 /*
2404                         Set block as modified
2405                 */
2406                 block->raiseModified(MOD_STATE_WRITE_NEEDED,
2407                         MOD_REASON_EXPIRE_DAYNIGHTDIFF);
2408         }
2409
2410         /*
2411                 Set central blocks as generated
2412         */
2413         for (s16 x = bpmin.X; x <= bpmax.X; x++)
2414         for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
2415         for (s16 y = bpmin.Y; y <= bpmax.Y; y++) {
2416                 MapBlock *block = getBlockNoCreateNoEx(v3s16(x, y, z));
2417                 if (!block)
2418                         continue;
2419
2420                 block->setGenerated(true);
2421         }
2422
2423         /*
2424                 Save changed parts of map
2425                 NOTE: Will be saved later.
2426         */
2427         //save(MOD_STATE_WRITE_AT_UNLOAD);
2428 }
2429
2430 ServerMapSector *ServerMap::createSector(v2s16 p2d)
2431 {
2432         DSTACKF("%s: p2d=(%d,%d)",
2433                         FUNCTION_NAME,
2434                         p2d.X, p2d.Y);
2435
2436         /*
2437                 Check if it exists already in memory
2438         */
2439         ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
2440         if(sector != NULL)
2441                 return sector;
2442
2443         /*
2444                 Try to load it from disk (with blocks)
2445         */
2446         //if(loadSectorFull(p2d) == true)
2447
2448         /*
2449                 Try to load metadata from disk
2450         */
2451 #if 0
2452         if(loadSectorMeta(p2d) == true)
2453         {
2454                 ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
2455                 if(sector == NULL)
2456                 {
2457                         infostream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
2458                         throw InvalidPositionException("");
2459                 }
2460                 return sector;
2461         }
2462 #endif
2463         /*
2464                 Do not create over-limit
2465         */
2466         const static u16 map_gen_limit = MYMIN(MAX_MAP_GENERATION_LIMIT,
2467                 g_settings->getU16("map_generation_limit"));
2468         if(p2d.X < -map_gen_limit / MAP_BLOCKSIZE
2469                         || p2d.X >  map_gen_limit / MAP_BLOCKSIZE
2470                         || p2d.Y < -map_gen_limit / MAP_BLOCKSIZE
2471                         || p2d.Y >  map_gen_limit / MAP_BLOCKSIZE)
2472                 throw InvalidPositionException("createSector(): pos. over limit");
2473
2474         /*
2475                 Generate blank sector
2476         */
2477
2478         sector = new ServerMapSector(this, p2d, m_gamedef);
2479
2480         // Sector position on map in nodes
2481         //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
2482
2483         /*
2484                 Insert to container
2485         */
2486         m_sectors[p2d] = sector;
2487
2488         return sector;
2489 }
2490
2491 #if 0
2492 /*
2493         This is a quick-hand function for calling makeBlock().
2494 */
2495 MapBlock * ServerMap::generateBlock(
2496                 v3s16 p,
2497                 std::map<v3s16, MapBlock*> &modified_blocks
2498 )
2499 {
2500         DSTACKF("%s: p=(%d,%d,%d)", FUNCTION_NAME, p.X, p.Y, p.Z);
2501
2502         /*infostream<<"generateBlock(): "
2503                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
2504                         <<std::endl;*/
2505
2506         bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
2507
2508         TimeTaker timer("generateBlock");
2509
2510         //MapBlock *block = original_dummy;
2511
2512         v2s16 p2d(p.X, p.Z);
2513         v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
2514
2515         /*
2516                 Do not generate over-limit
2517         */
2518         if(blockpos_over_limit(p))
2519         {
2520                 infostream<<FUNCTION_NAME<<": Block position over limit"<<std::endl;
2521                 throw InvalidPositionException("generateBlock(): pos. over limit");
2522         }
2523
2524         /*
2525                 Create block make data
2526         */
2527         BlockMakeData data;
2528         initBlockMake(&data, p);
2529
2530         /*
2531                 Generate block
2532         */
2533         {
2534                 TimeTaker t("mapgen::make_block()");
2535                 mapgen->makeChunk(&data);
2536                 //mapgen::make_block(&data);
2537
2538                 if(enable_mapgen_debug_info == false)
2539                         t.stop(true); // Hide output
2540         }
2541
2542         /*
2543                 Blit data back on map, update lighting, add mobs and whatever this does
2544         */
2545         finishBlockMake(&data, modified_blocks);
2546
2547         /*
2548                 Get central block
2549         */
2550         MapBlock *block = getBlockNoCreateNoEx(p);
2551
2552 #if 0
2553         /*
2554                 Check result
2555         */
2556         if(block)
2557         {
2558                 bool erroneus_content = false;
2559                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
2560                 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
2561                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
2562                 {
2563                         v3s16 p(x0,y0,z0);
2564                         MapNode n = block->getNode(p);
2565                         if(n.getContent() == CONTENT_IGNORE)
2566                         {
2567                                 infostream<<"CONTENT_IGNORE at "
2568                                                 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
2569                                                 <<std::endl;
2570                                 erroneus_content = true;
2571                                 assert(0);
2572                         }
2573                 }
2574                 if(erroneus_content)
2575                 {
2576                         assert(0);
2577                 }
2578         }
2579 #endif
2580
2581 #if 0
2582         /*
2583                 Generate a completely empty block
2584         */
2585         if(block)
2586         {
2587                 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
2588                 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
2589                 {
2590                         for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
2591                         {
2592                                 MapNode n;
2593                                 n.setContent(CONTENT_AIR);
2594                                 block->setNode(v3s16(x0,y0,z0), n);
2595                         }
2596                 }
2597         }
2598 #endif
2599
2600         if(enable_mapgen_debug_info == false)
2601                 timer.stop(true); // Hide output
2602
2603         return block;
2604 }
2605 #endif
2606
2607 MapBlock * ServerMap::createBlock(v3s16 p)
2608 {
2609         DSTACKF("%s: p=(%d,%d,%d)",
2610                         FUNCTION_NAME, p.X, p.Y, p.Z);
2611
2612         /*
2613                 Do not create over-limit
2614         */
2615         if (blockpos_over_limit(p))
2616                 throw InvalidPositionException("createBlock(): pos. over limit");
2617
2618         v2s16 p2d(p.X, p.Z);
2619         s16 block_y = p.Y;
2620         /*
2621                 This will create or load a sector if not found in memory.
2622                 If block exists on disk, it will be loaded.
2623
2624                 NOTE: On old save formats, this will be slow, as it generates
2625                       lighting on blocks for them.
2626         */
2627         ServerMapSector *sector;
2628         try {
2629                 sector = (ServerMapSector*)createSector(p2d);
2630                 assert(sector->getId() == MAPSECTOR_SERVER);
2631         }
2632         catch(InvalidPositionException &e)
2633         {
2634                 infostream<<"createBlock: createSector() failed"<<std::endl;
2635                 throw e;
2636         }
2637         /*
2638                 NOTE: This should not be done, or at least the exception
2639                 should not be passed on as std::exception, because it
2640                 won't be catched at all.
2641         */
2642         /*catch(std::exception &e)
2643         {
2644                 infostream<<"createBlock: createSector() failed: "
2645                                 <<e.what()<<std::endl;
2646                 throw e;
2647         }*/
2648
2649         /*
2650                 Try to get a block from the sector
2651         */
2652
2653         MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
2654         if(block)
2655         {
2656                 if(block->isDummy())
2657                         block->unDummify();
2658                 return block;
2659         }
2660         // Create blank
2661         block = sector->createBlankBlock(block_y);
2662
2663         return block;
2664 }
2665
2666 MapBlock * ServerMap::emergeBlock(v3s16 p, bool create_blank)
2667 {
2668         DSTACKF("%s: p=(%d,%d,%d), create_blank=%d",
2669                         FUNCTION_NAME,
2670                         p.X, p.Y, p.Z, create_blank);
2671
2672         {
2673                 MapBlock *block = getBlockNoCreateNoEx(p);
2674                 if(block && block->isDummy() == false)
2675                         return block;
2676         }
2677
2678         {
2679                 MapBlock *block = loadBlock(p);
2680                 if(block)
2681                         return block;
2682         }
2683
2684         if (create_blank) {
2685                 ServerMapSector *sector = createSector(v2s16(p.X, p.Z));
2686                 MapBlock *block = sector->createBlankBlock(p.Y);
2687
2688                 return block;
2689         }
2690
2691 #if 0
2692         if(allow_generate)
2693         {
2694                 std::map<v3s16, MapBlock*> modified_blocks;
2695                 MapBlock *block = generateBlock(p, modified_blocks);
2696                 if(block)
2697                 {
2698                         MapEditEvent event;
2699                         event.type = MEET_OTHER;
2700                         event.p = p;
2701
2702                         // Copy modified_blocks to event
2703                         for(std::map<v3s16, MapBlock*>::iterator
2704                                         i = modified_blocks.begin();
2705                                         i != modified_blocks.end(); ++i)
2706                         {
2707                                 event.modified_blocks.insert(i->first);
2708                         }
2709
2710                         // Queue event
2711                         dispatchEvent(&event);
2712
2713                         return block;
2714                 }
2715         }
2716 #endif
2717
2718         return NULL;
2719 }
2720
2721 MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
2722 {
2723         MapBlock *block = getBlockNoCreateNoEx(p3d);
2724         if (block == NULL)
2725                 m_emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, p3d, false);
2726
2727         return block;
2728 }
2729
2730 void ServerMap::prepareBlock(MapBlock *block) {
2731 }
2732
2733 // N.B.  This requires no synchronization, since data will not be modified unless
2734 // the VoxelManipulator being updated belongs to the same thread.
2735 void ServerMap::updateVManip(v3s16 pos)
2736 {
2737         Mapgen *mg = m_emerge->getCurrentMapgen();
2738         if (!mg)
2739                 return;
2740
2741         MMVManip *vm = mg->vm;
2742         if (!vm)
2743                 return;
2744
2745         if (!vm->m_area.contains(pos))
2746                 return;
2747
2748         s32 idx = vm->m_area.index(pos);
2749         vm->m_data[idx] = getNodeNoEx(pos);
2750         vm->m_flags[idx] &= ~VOXELFLAG_NO_DATA;
2751
2752         vm->m_is_dirty = true;
2753 }
2754
2755 s16 ServerMap::findGroundLevel(v2s16 p2d)
2756 {
2757 #if 0
2758         /*
2759                 Uh, just do something random...
2760         */
2761         // Find existing map from top to down
2762         s16 max=63;
2763         s16 min=-64;
2764         v3s16 p(p2d.X, max, p2d.Y);
2765         for(; p.Y>min; p.Y--)
2766         {
2767                 MapNode n = getNodeNoEx(p);
2768                 if(n.getContent() != CONTENT_IGNORE)
2769                         break;
2770         }
2771         if(p.Y == min)
2772                 goto plan_b;
2773         // If this node is not air, go to plan b
2774         if(getNodeNoEx(p).getContent() != CONTENT_AIR)
2775                 goto plan_b;
2776         // Search existing walkable and return it
2777         for(; p.Y>min; p.Y--)
2778         {
2779                 MapNode n = getNodeNoEx(p);
2780                 if(content_walkable(n.d) && n.getContent() != CONTENT_IGNORE)
2781                         return p.Y;
2782         }
2783
2784         // Move to plan b
2785 plan_b:
2786 #endif
2787
2788         /*
2789                 Determine from map generator noise functions
2790         */
2791
2792         s16 level = m_emerge->getGroundLevelAtPoint(p2d);
2793         return level;
2794
2795         //double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
2796         //return (s16)level;
2797 }
2798
2799 bool ServerMap::loadFromFolders() {
2800         if (!dbase->initialized() &&
2801                         !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
2802                 return true;
2803         return false;
2804 }
2805
2806 void ServerMap::createDirs(std::string path)
2807 {
2808         if(fs::CreateAllDirs(path) == false)
2809         {
2810                 m_dout<<"ServerMap: Failed to create directory "
2811                                 <<"\""<<path<<"\""<<std::endl;
2812                 throw BaseException("ServerMap failed to create directory");
2813         }
2814 }
2815
2816 std::string ServerMap::getSectorDir(v2s16 pos, int layout)
2817 {
2818         char cc[9];
2819         switch(layout)
2820         {
2821                 case 1:
2822                         snprintf(cc, 9, "%.4x%.4x",
2823                                 (unsigned int) pos.X & 0xffff,
2824                                 (unsigned int) pos.Y & 0xffff);
2825
2826                         return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
2827                 case 2:
2828                         snprintf(cc, 9, (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
2829                                 (unsigned int) pos.X & 0xfff,
2830                                 (unsigned int) pos.Y & 0xfff);
2831
2832                         return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc;
2833                 default:
2834                         assert(false);
2835                         return "";
2836         }
2837 }
2838
2839 v2s16 ServerMap::getSectorPos(std::string dirname)
2840 {
2841         unsigned int x = 0, y = 0;
2842         int r;
2843         std::string component;
2844         fs::RemoveLastPathComponent(dirname, &component, 1);
2845         if(component.size() == 8)
2846         {
2847                 // Old layout
2848                 r = sscanf(component.c_str(), "%4x%4x", &x, &y);
2849         }
2850         else if(component.size() == 3)
2851         {
2852                 // New layout
2853                 fs::RemoveLastPathComponent(dirname, &component, 2);
2854                 r = sscanf(component.c_str(), (std::string("%3x") + DIR_DELIM + "%3x").c_str(), &x, &y);
2855                 // Sign-extend the 12 bit values up to 16 bits...
2856                 if(x & 0x800) x |= 0xF000;
2857                 if(y & 0x800) y |= 0xF000;
2858         }
2859         else
2860         {
2861                 r = -1;
2862         }
2863
2864         FATAL_ERROR_IF(r != 2, "getSectorPos()");
2865         v2s16 pos((s16)x, (s16)y);
2866         return pos;
2867 }
2868
2869 v3s16 ServerMap::getBlockPos(std::string sectordir, std::string blockfile)
2870 {
2871         v2s16 p2d = getSectorPos(sectordir);
2872
2873         if(blockfile.size() != 4){
2874                 throw InvalidFilenameException("Invalid block filename");
2875         }
2876         unsigned int y;
2877         int r = sscanf(blockfile.c_str(), "%4x", &y);
2878         if(r != 1)
2879                 throw InvalidFilenameException("Invalid block filename");
2880         return v3s16(p2d.X, y, p2d.Y);
2881 }
2882
2883 std::string ServerMap::getBlockFilename(v3s16 p)
2884 {
2885         char cc[5];
2886         snprintf(cc, 5, "%.4x", (unsigned int)p.Y&0xffff);
2887         return cc;
2888 }
2889
2890 void ServerMap::save(ModifiedState save_level)
2891 {
2892         DSTACK(FUNCTION_NAME);
2893         if(m_map_saving_enabled == false) {
2894                 warningstream<<"Not saving map, saving disabled."<<std::endl;
2895                 return;
2896         }
2897
2898         if(save_level == MOD_STATE_CLEAN)
2899                 infostream<<"ServerMap: Saving whole map, this can take time."
2900                                 <<std::endl;
2901
2902         if(m_map_metadata_changed || save_level == MOD_STATE_CLEAN) {
2903                 saveMapMeta();
2904         }
2905
2906         // Profile modified reasons
2907         Profiler modprofiler;
2908
2909         u32 sector_meta_count = 0;
2910         u32 block_count = 0;
2911         u32 block_count_all = 0; // Number of blocks in memory
2912
2913         // Don't do anything with sqlite unless something is really saved
2914         bool save_started = false;
2915
2916         for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
2917                 i != m_sectors.end(); ++i) {
2918                 ServerMapSector *sector = (ServerMapSector*)i->second;
2919                 assert(sector->getId() == MAPSECTOR_SERVER);
2920
2921                 if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
2922                         saveSectorMeta(sector);
2923                         sector_meta_count++;
2924                 }
2925
2926                 MapBlockVect blocks;
2927                 sector->getBlocks(blocks);
2928
2929                 for(MapBlockVect::iterator j = blocks.begin();
2930                         j != blocks.end(); ++j) {
2931                         MapBlock *block = *j;
2932
2933                         block_count_all++;
2934
2935                         if(block->getModified() >= (u32)save_level) {
2936                                 // Lazy beginSave()
2937                                 if(!save_started) {
2938                                         beginSave();
2939                                         save_started = true;
2940                                 }
2941
2942                                 modprofiler.add(block->getModifiedReasonString(), 1);
2943
2944                                 saveBlock(block);
2945                                 block_count++;
2946
2947                                 /*infostream<<"ServerMap: Written block ("
2948                                                 <<block->getPos().X<<","
2949                                                 <<block->getPos().Y<<","
2950                                                 <<block->getPos().Z<<")"
2951                                                 <<std::endl;*/
2952                         }
2953                 }
2954         }
2955
2956         if(save_started)
2957                 endSave();
2958
2959         /*
2960                 Only print if something happened or saved whole map
2961         */
2962         if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
2963                         || block_count != 0) {
2964                 infostream<<"ServerMap: Written: "
2965                                 <<sector_meta_count<<" sector metadata files, "
2966                                 <<block_count<<" block files"
2967                                 <<", "<<block_count_all<<" blocks in memory."
2968                                 <<std::endl;
2969                 PrintInfo(infostream); // ServerMap/ClientMap:
2970                 infostream<<"Blocks modified by: "<<std::endl;
2971                 modprofiler.print(infostream);
2972         }
2973 }
2974
2975 void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
2976 {
2977         if (loadFromFolders()) {
2978                 errorstream << "Map::listAllLoadableBlocks(): Result will be missing "
2979                                 << "all blocks that are stored in flat files." << std::endl;
2980         }
2981         dbase->listAllLoadableBlocks(dst);
2982 }
2983
2984 void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
2985 {
2986         for(std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
2987                 si != m_sectors.end(); ++si)
2988         {
2989                 MapSector *sector = si->second;
2990
2991                 MapBlockVect blocks;
2992                 sector->getBlocks(blocks);
2993
2994                 for(MapBlockVect::iterator i = blocks.begin();
2995                                 i != blocks.end(); ++i) {
2996                         v3s16 p = (*i)->getPos();
2997                         dst.push_back(p);
2998                 }
2999         }
3000 }
3001
3002 void ServerMap::saveMapMeta()
3003 {
3004         DSTACK(FUNCTION_NAME);
3005
3006         createDirs(m_savedir);
3007
3008         std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
3009         std::ostringstream oss(std::ios_base::binary);
3010         Settings conf;
3011
3012         m_emerge->params.save(conf);
3013         conf.writeLines(oss);
3014
3015         oss << "[end_of_params]\n";
3016
3017         if(!fs::safeWriteToFile(fullpath, oss.str())) {
3018                 errorstream << "ServerMap::saveMapMeta(): "
3019                                 << "could not write " << fullpath << std::endl;
3020                 throw FileNotGoodException("Cannot save chunk metadata");
3021         }
3022
3023         m_map_metadata_changed = false;
3024 }
3025
3026 void ServerMap::loadMapMeta()
3027 {
3028         DSTACK(FUNCTION_NAME);
3029
3030         Settings conf;
3031         std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
3032
3033         std::ifstream is(fullpath.c_str(), std::ios_base::binary);
3034         if (!is.good()) {
3035                 errorstream << "ServerMap::loadMapMeta(): "
3036                         "could not open " << fullpath << std::endl;
3037                 throw FileNotGoodException("Cannot open map metadata");
3038         }
3039
3040         if (!conf.parseConfigLines(is, "[end_of_params]")) {
3041                 throw SerializationError("ServerMap::loadMapMeta(): "
3042                                 "[end_of_params] not found!");
3043         }
3044
3045         m_emerge->params.load(conf);
3046
3047         verbosestream << "ServerMap::loadMapMeta(): seed="
3048                 << m_emerge->params.seed << std::endl;
3049 }
3050
3051 void ServerMap::saveSectorMeta(ServerMapSector *sector)
3052 {
3053         DSTACK(FUNCTION_NAME);
3054         // Format used for writing
3055         u8 version = SER_FMT_VER_HIGHEST_WRITE;
3056         // Get destination
3057         v2s16 pos = sector->getPos();
3058         std::string dir = getSectorDir(pos);
3059         createDirs(dir);
3060
3061         std::string fullpath = dir + DIR_DELIM + "meta";
3062         std::ostringstream ss(std::ios_base::binary);
3063
3064         sector->serialize(ss, version);
3065
3066         if(!fs::safeWriteToFile(fullpath, ss.str()))
3067                 throw FileNotGoodException("Cannot write sector metafile");
3068
3069         sector->differs_from_disk = false;
3070 }
3071
3072 MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
3073 {
3074         DSTACK(FUNCTION_NAME);
3075         // Get destination
3076         v2s16 p2d = getSectorPos(sectordir);
3077
3078         ServerMapSector *sector = NULL;
3079
3080         std::string fullpath = sectordir + DIR_DELIM + "meta";
3081         std::ifstream is(fullpath.c_str(), std::ios_base::binary);
3082         if(is.good() == false)
3083         {
3084                 // If the directory exists anyway, it probably is in some old
3085                 // format. Just go ahead and create the sector.
3086                 if(fs::PathExists(sectordir))
3087                 {
3088                         /*infostream<<"ServerMap::loadSectorMeta(): Sector metafile "
3089                                         <<fullpath<<" doesn't exist but directory does."
3090                                         <<" Continuing with a sector with no metadata."
3091                                         <<std::endl;*/
3092                         sector = new ServerMapSector(this, p2d, m_gamedef);
3093                         m_sectors[p2d] = sector;
3094                 }
3095                 else
3096                 {
3097                         throw FileNotGoodException("Cannot open sector metafile");
3098                 }
3099         }
3100         else
3101         {
3102                 sector = ServerMapSector::deSerialize
3103                                 (is, this, p2d, m_sectors, m_gamedef);
3104                 if(save_after_load)
3105                         saveSectorMeta(sector);
3106         }
3107
3108         sector->differs_from_disk = false;
3109
3110         return sector;
3111 }
3112
3113 bool ServerMap::loadSectorMeta(v2s16 p2d)
3114 {
3115         DSTACK(FUNCTION_NAME);
3116
3117         // The directory layout we're going to load from.
3118         //  1 - original sectors/xxxxzzzz/
3119         //  2 - new sectors2/xxx/zzz/
3120         //  If we load from anything but the latest structure, we will
3121         //  immediately save to the new one, and remove the old.
3122         int loadlayout = 1;
3123         std::string sectordir1 = getSectorDir(p2d, 1);
3124         std::string sectordir;
3125         if(fs::PathExists(sectordir1))
3126         {
3127                 sectordir = sectordir1;
3128         }
3129         else
3130         {
3131                 loadlayout = 2;
3132                 sectordir = getSectorDir(p2d, 2);
3133         }
3134
3135         try{
3136                 loadSectorMeta(sectordir, loadlayout != 2);
3137         }
3138         catch(InvalidFilenameException &e)
3139         {
3140                 return false;
3141         }
3142         catch(FileNotGoodException &e)
3143         {
3144                 return false;
3145         }
3146         catch(std::exception &e)
3147         {
3148                 return false;
3149         }
3150
3151         return true;
3152 }
3153
3154 #if 0
3155 bool ServerMap::loadSectorFull(v2s16 p2d)
3156 {
3157         DSTACK(FUNCTION_NAME);
3158
3159         MapSector *sector = NULL;
3160
3161         // The directory layout we're going to load from.
3162         //  1 - original sectors/xxxxzzzz/
3163         //  2 - new sectors2/xxx/zzz/
3164         //  If we load from anything but the latest structure, we will
3165         //  immediately save to the new one, and remove the old.
3166         int loadlayout = 1;
3167         std::string sectordir1 = getSectorDir(p2d, 1);
3168         std::string sectordir;
3169         if(fs::PathExists(sectordir1))
3170         {
3171                 sectordir = sectordir1;
3172         }
3173         else
3174         {
3175                 loadlayout = 2;
3176                 sectordir = getSectorDir(p2d, 2);
3177         }
3178
3179         try{
3180                 sector = loadSectorMeta(sectordir, loadlayout != 2);
3181         }
3182         catch(InvalidFilenameException &e)
3183         {
3184                 return false;
3185         }
3186         catch(FileNotGoodException &e)
3187         {
3188                 return false;
3189         }
3190         catch(std::exception &e)
3191         {
3192                 return false;
3193         }
3194
3195         /*
3196                 Load blocks
3197         */
3198         std::vector<fs::DirListNode> list2 = fs::GetDirListing
3199                         (sectordir);
3200         std::vector<fs::DirListNode>::iterator i2;
3201         for(i2=list2.begin(); i2!=list2.end(); i2++)
3202         {
3203                 // We want files
3204                 if(i2->dir)
3205                         continue;
3206                 try{
3207                         loadBlock(sectordir, i2->name, sector, loadlayout != 2);
3208                 }
3209                 catch(InvalidFilenameException &e)
3210                 {
3211                         // This catches unknown crap in directory
3212                 }
3213         }
3214
3215         if(loadlayout != 2)
3216         {
3217                 infostream<<"Sector converted to new layout - deleting "<<
3218                         sectordir1<<std::endl;
3219                 fs::RecursiveDelete(sectordir1);
3220         }
3221
3222         return true;
3223 }
3224 #endif
3225
3226 Database *ServerMap::createDatabase(
3227         const std::string &name,
3228         const std::string &savedir,
3229         Settings &conf)
3230 {
3231         if (name == "sqlite3")
3232                 return new Database_SQLite3(savedir);
3233         if (name == "dummy")
3234                 return new Database_Dummy();
3235         #if USE_LEVELDB
3236         else if (name == "leveldb")
3237                 return new Database_LevelDB(savedir);
3238         #endif
3239         #if USE_REDIS
3240         else if (name == "redis")
3241                 return new Database_Redis(conf);
3242         #endif
3243         else
3244                 throw BaseException(std::string("Database backend ") + name + " not supported.");
3245 }
3246
3247 void ServerMap::beginSave()
3248 {
3249         dbase->beginSave();
3250 }
3251
3252 void ServerMap::endSave()
3253 {
3254         dbase->endSave();
3255 }
3256
3257 bool ServerMap::saveBlock(MapBlock *block)
3258 {
3259         return saveBlock(block, dbase);
3260 }
3261
3262 bool ServerMap::saveBlock(MapBlock *block, Database *db)
3263 {
3264         v3s16 p3d = block->getPos();
3265
3266         // Dummy blocks are not written
3267         if (block->isDummy()) {
3268                 warningstream << "saveBlock: Not writing dummy block "
3269                         << PP(p3d) << std::endl;
3270                 return true;
3271         }
3272
3273         // Format used for writing
3274         u8 version = SER_FMT_VER_HIGHEST_WRITE;
3275
3276         /*
3277                 [0] u8 serialization version
3278                 [1] data
3279         */
3280         std::ostringstream o(std::ios_base::binary);
3281         o.write((char*) &version, 1);
3282         block->serialize(o, version, true);
3283
3284         std::string data = o.str();
3285         bool ret = db->saveBlock(p3d, data);
3286         if (ret) {
3287                 // We just wrote it to the disk so clear modified flag
3288                 block->resetModified();
3289         }
3290         return ret;
3291 }
3292
3293 void ServerMap::loadBlock(std::string sectordir, std::string blockfile,
3294                 MapSector *sector, bool save_after_load)
3295 {
3296         DSTACK(FUNCTION_NAME);
3297
3298         std::string fullpath = sectordir + DIR_DELIM + blockfile;
3299         try {
3300
3301                 std::ifstream is(fullpath.c_str(), std::ios_base::binary);
3302                 if(is.good() == false)
3303                         throw FileNotGoodException("Cannot open block file");
3304
3305                 v3s16 p3d = getBlockPos(sectordir, blockfile);
3306                 v2s16 p2d(p3d.X, p3d.Z);
3307
3308                 assert(sector->getPos() == p2d);
3309
3310                 u8 version = SER_FMT_VER_INVALID;
3311                 is.read((char*)&version, 1);
3312
3313                 if(is.fail())
3314                         throw SerializationError("ServerMap::loadBlock(): Failed"
3315                                         " to read MapBlock version");
3316
3317                 /*u32 block_size = MapBlock::serializedLength(version);
3318                 SharedBuffer<u8> data(block_size);
3319                 is.read((char*)*data, block_size);*/
3320
3321                 // This will always return a sector because we're the server
3322                 //MapSector *sector = emergeSector(p2d);
3323
3324                 MapBlock *block = NULL;
3325                 bool created_new = false;
3326                 block = sector->getBlockNoCreateNoEx(p3d.Y);
3327                 if(block == NULL)
3328                 {
3329                         block = sector->createBlankBlockNoInsert(p3d.Y);
3330                         created_new = true;
3331                 }
3332
3333                 // Read basic data
3334                 block->deSerialize(is, version, true);
3335
3336                 // If it's a new block, insert it to the map
3337                 if(created_new)
3338                         sector->insertBlock(block);
3339
3340                 /*
3341                         Save blocks loaded in old format in new format
3342                 */
3343
3344                 if(version < SER_FMT_VER_HIGHEST_WRITE || save_after_load)
3345                 {
3346                         saveBlock(block);
3347
3348                         // Should be in database now, so delete the old file
3349                         fs::RecursiveDelete(fullpath);
3350                 }
3351
3352                 // We just loaded it from the disk, so it's up-to-date.
3353                 block->resetModified();
3354
3355         }
3356         catch(SerializationError &e)
3357         {
3358                 warningstream<<"Invalid block data on disk "
3359                                 <<"fullpath="<<fullpath
3360                                 <<" (SerializationError). "
3361                                 <<"what()="<<e.what()
3362                                 <<std::endl;
3363                                 // Ignoring. A new one will be generated.
3364                 abort();
3365
3366                 // TODO: Backup file; name is in fullpath.
3367         }
3368 }
3369
3370 void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load)
3371 {
3372         DSTACK(FUNCTION_NAME);
3373
3374         try {
3375                 std::istringstream is(*blob, std::ios_base::binary);
3376
3377                 u8 version = SER_FMT_VER_INVALID;
3378                 is.read((char*)&version, 1);
3379
3380                 if(is.fail())
3381                         throw SerializationError("ServerMap::loadBlock(): Failed"
3382                                         " to read MapBlock version");
3383
3384                 /*u32 block_size = MapBlock::serializedLength(version);
3385                 SharedBuffer<u8> data(block_size);
3386                 is.read((char*)*data, block_size);*/
3387
3388                 // This will always return a sector because we're the server
3389                 //MapSector *sector = emergeSector(p2d);
3390
3391                 MapBlock *block = NULL;
3392                 bool created_new = false;
3393                 block = sector->getBlockNoCreateNoEx(p3d.Y);
3394                 if(block == NULL)
3395                 {
3396                         block = sector->createBlankBlockNoInsert(p3d.Y);
3397                         created_new = true;
3398                 }
3399
3400                 // Read basic data
3401                 block->deSerialize(is, version, true);
3402
3403                 // If it's a new block, insert it to the map
3404                 if(created_new)
3405                         sector->insertBlock(block);
3406
3407                 /*
3408                         Save blocks loaded in old format in new format
3409                 */
3410
3411                 //if(version < SER_FMT_VER_HIGHEST_READ || save_after_load)
3412                 // Only save if asked to; no need to update version
3413                 if(save_after_load)
3414                         saveBlock(block);
3415
3416                 // We just loaded it from, so it's up-to-date.
3417                 block->resetModified();
3418
3419         }
3420         catch(SerializationError &e)
3421         {
3422                 errorstream<<"Invalid block data in database"
3423                                 <<" ("<<p3d.X<<","<<p3d.Y<<","<<p3d.Z<<")"
3424                                 <<" (SerializationError): "<<e.what()<<std::endl;
3425
3426                 // TODO: Block should be marked as invalid in memory so that it is
3427                 // not touched but the game can run
3428
3429                 if(g_settings->getBool("ignore_world_load_errors")){
3430                         errorstream<<"Ignoring block load error. Duck and cover! "
3431                                         <<"(ignore_world_load_errors)"<<std::endl;
3432                 } else {
3433                         throw SerializationError("Invalid block data in database");
3434                 }
3435         }
3436 }
3437
3438 MapBlock* ServerMap::loadBlock(v3s16 blockpos)
3439 {
3440         DSTACK(FUNCTION_NAME);
3441
3442         v2s16 p2d(blockpos.X, blockpos.Z);
3443
3444         std::string ret;
3445
3446         ret = dbase->loadBlock(blockpos);
3447         if (ret != "") {
3448                 loadBlock(&ret, blockpos, createSector(p2d), false);
3449                 return getBlockNoCreateNoEx(blockpos);
3450         }
3451         // Not found in database, try the files
3452
3453         // The directory layout we're going to load from.
3454         //  1 - original sectors/xxxxzzzz/
3455         //  2 - new sectors2/xxx/zzz/
3456         //  If we load from anything but the latest structure, we will
3457         //  immediately save to the new one, and remove the old.
3458         int loadlayout = 1;
3459         std::string sectordir1 = getSectorDir(p2d, 1);
3460         std::string sectordir;
3461         if(fs::PathExists(sectordir1))
3462         {
3463                 sectordir = sectordir1;
3464         }
3465         else
3466         {
3467                 loadlayout = 2;
3468                 sectordir = getSectorDir(p2d, 2);
3469         }
3470
3471         /*
3472                 Make sure sector is loaded
3473         */
3474
3475         MapSector *sector = getSectorNoGenerateNoEx(p2d);
3476         if(sector == NULL)
3477         {
3478                 try{
3479                         sector = loadSectorMeta(sectordir, loadlayout != 2);
3480                 }
3481                 catch(InvalidFilenameException &e)
3482                 {
3483                         return NULL;
3484                 }
3485                 catch(FileNotGoodException &e)
3486                 {
3487                         return NULL;
3488                 }
3489                 catch(std::exception &e)
3490                 {
3491                         return NULL;
3492                 }
3493         }
3494
3495         /*
3496                 Make sure file exists
3497         */
3498
3499         std::string blockfilename = getBlockFilename(blockpos);
3500         if(fs::PathExists(sectordir + DIR_DELIM + blockfilename) == false)
3501                 return NULL;
3502
3503         /*
3504                 Load block and save it to the database
3505         */
3506         loadBlock(sectordir, blockfilename, sector, true);
3507         return getBlockNoCreateNoEx(blockpos);
3508 }
3509
3510 bool ServerMap::deleteBlock(v3s16 blockpos)
3511 {
3512         if (!dbase->deleteBlock(blockpos))
3513                 return false;
3514
3515         MapBlock *block = getBlockNoCreateNoEx(blockpos);
3516         if (block) {
3517                 v2s16 p2d(blockpos.X, blockpos.Z);
3518                 MapSector *sector = getSectorNoGenerateNoEx(p2d);
3519                 if (!sector)
3520                         return false;
3521                 sector->deleteBlock(block);
3522         }
3523
3524         return true;
3525 }
3526
3527 void ServerMap::PrintInfo(std::ostream &out)
3528 {
3529         out<<"ServerMap: ";
3530 }
3531
3532 MMVManip::MMVManip(Map *map):
3533                 VoxelManipulator(),
3534                 m_is_dirty(false),
3535                 m_create_area(false),
3536                 m_map(map)
3537 {
3538 }
3539
3540 MMVManip::~MMVManip()
3541 {
3542 }
3543
3544 void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
3545         bool load_if_inexistent)
3546 {
3547         TimeTaker timer1("initialEmerge", &emerge_time);
3548
3549         // Units of these are MapBlocks
3550         v3s16 p_min = blockpos_min;
3551         v3s16 p_max = blockpos_max;
3552
3553         VoxelArea block_area_nodes
3554                         (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
3555
3556         u32 size_MB = block_area_nodes.getVolume()*4/1000000;
3557         if(size_MB >= 1)
3558         {
3559                 infostream<<"initialEmerge: area: ";
3560                 block_area_nodes.print(infostream);
3561                 infostream<<" ("<<size_MB<<"MB)";
3562                 infostream<<std::endl;
3563         }
3564
3565         addArea(block_area_nodes);
3566
3567         for(s32 z=p_min.Z; z<=p_max.Z; z++)
3568         for(s32 y=p_min.Y; y<=p_max.Y; y++)
3569         for(s32 x=p_min.X; x<=p_max.X; x++)
3570         {
3571                 u8 flags = 0;
3572                 MapBlock *block;
3573                 v3s16 p(x,y,z);
3574                 std::map<v3s16, u8>::iterator n;
3575                 n = m_loaded_blocks.find(p);
3576                 if(n != m_loaded_blocks.end())
3577                         continue;
3578
3579                 bool block_data_inexistent = false;
3580                 try
3581                 {
3582                         TimeTaker timer1("emerge load", &emerge_load_time);
3583
3584                         block = m_map->getBlockNoCreate(p);
3585                         if(block->isDummy())
3586                                 block_data_inexistent = true;
3587                         else
3588                                 block->copyTo(*this);
3589                 }
3590                 catch(InvalidPositionException &e)
3591                 {
3592                         block_data_inexistent = true;
3593                 }
3594
3595                 if(block_data_inexistent)
3596                 {
3597
3598                         if (load_if_inexistent) {
3599                                 ServerMap *svrmap = (ServerMap *)m_map;
3600                                 block = svrmap->emergeBlock(p, false);
3601                                 if (block == NULL)
3602                                         block = svrmap->createBlock(p);
3603                                 block->copyTo(*this);
3604                         } else {
3605                                 flags |= VMANIP_BLOCK_DATA_INEXIST;
3606
3607                                 /*
3608                                         Mark area inexistent
3609                                 */
3610                                 VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
3611                                 // Fill with VOXELFLAG_NO_DATA
3612                                 for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
3613                                 for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
3614                                 {
3615                                         s32 i = m_area.index(a.MinEdge.X,y,z);
3616                                         memset(&m_flags[i], VOXELFLAG_NO_DATA, MAP_BLOCKSIZE);
3617                                 }
3618                         }
3619                 }
3620                 /*else if (block->getNode(0, 0, 0).getContent() == CONTENT_IGNORE)
3621                 {
3622                         // Mark that block was loaded as blank
3623                         flags |= VMANIP_BLOCK_CONTAINS_CIGNORE;
3624                 }*/
3625
3626                 m_loaded_blocks[p] = flags;
3627         }
3628
3629         m_is_dirty = false;
3630 }
3631
3632 void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
3633         bool overwrite_generated)
3634 {
3635         if(m_area.getExtent() == v3s16(0,0,0))
3636                 return;
3637
3638         /*
3639                 Copy data of all blocks
3640         */
3641         for(std::map<v3s16, u8>::iterator
3642                         i = m_loaded_blocks.begin();
3643                         i != m_loaded_blocks.end(); ++i)
3644         {
3645                 v3s16 p = i->first;
3646                 MapBlock *block = m_map->getBlockNoCreateNoEx(p);
3647                 bool existed = !(i->second & VMANIP_BLOCK_DATA_INEXIST);
3648                 if ((existed == false) || (block == NULL) ||
3649                         (overwrite_generated == false && block->isGenerated() == true))
3650                         continue;
3651
3652                 block->copyFrom(*this);
3653
3654                 if(modified_blocks)
3655                         (*modified_blocks)[p] = block;
3656         }
3657 }
3658
3659 //END