3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "mapsector.h"
25 #include "voxelalgorithms.h"
27 #include "serialization.h"
28 #include "nodemetadata.h"
34 #include "util/directiontables.h"
35 #include "util/basic_macros.h"
36 #include "rollback_interface.h"
37 #include "environment.h"
38 #include "reflowscan.h"
40 #include "mapgen_v6.h"
45 #include "database-dummy.h"
46 #include "database-sqlite3.h"
47 #include "script/scripting_server.h"
51 #include "database-leveldb.h"
54 #include "database-redis.h"
57 #include "database-postgresql.h"
65 Map::Map(std::ostream &dout, IGameDef *gamedef):
69 m_nodedef(gamedef->ndef()),
70 m_transforming_liquid_loop_count_multiplier(1.0f),
71 m_unprocessed_count(0),
72 m_inc_trending_up_start_time(0),
73 m_queue_size_timer_started(false)
82 for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
83 i != m_sectors.end(); ++i)
89 void Map::addEventReceiver(MapEventReceiver *event_receiver)
91 m_event_receivers.insert(event_receiver);
94 void Map::removeEventReceiver(MapEventReceiver *event_receiver)
96 m_event_receivers.erase(event_receiver);
99 void Map::dispatchEvent(MapEditEvent *event)
101 for(std::set<MapEventReceiver*>::iterator
102 i = m_event_receivers.begin();
103 i != m_event_receivers.end(); ++i)
105 (*i)->onMapEditEvent(event);
109 MapSector * Map::getSectorNoGenerateNoExNoLock(v2s16 p)
111 if(m_sector_cache != NULL && p == m_sector_cache_p){
112 MapSector * sector = m_sector_cache;
116 std::map<v2s16, MapSector*>::iterator n = m_sectors.find(p);
118 if(n == m_sectors.end())
121 MapSector *sector = n->second;
123 // Cache the last result
124 m_sector_cache_p = p;
125 m_sector_cache = sector;
130 MapSector * Map::getSectorNoGenerateNoEx(v2s16 p)
132 return getSectorNoGenerateNoExNoLock(p);
135 MapSector * Map::getSectorNoGenerate(v2s16 p)
137 MapSector *sector = getSectorNoGenerateNoEx(p);
139 throw InvalidPositionException();
144 MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
146 v2s16 p2d(p3d.X, p3d.Z);
147 MapSector * sector = getSectorNoGenerateNoEx(p2d);
150 MapBlock *block = sector->getBlockNoCreateNoEx(p3d.Y);
154 MapBlock * Map::getBlockNoCreate(v3s16 p3d)
156 MapBlock *block = getBlockNoCreateNoEx(p3d);
158 throw InvalidPositionException();
162 bool Map::isNodeUnderground(v3s16 p)
164 v3s16 blockpos = getNodeBlockPos(p);
166 MapBlock * block = getBlockNoCreate(blockpos);
167 return block->getIsUnderground();
169 catch(InvalidPositionException &e)
175 bool Map::isValidPosition(v3s16 p)
177 v3s16 blockpos = getNodeBlockPos(p);
178 MapBlock *block = getBlockNoCreateNoEx(blockpos);
179 return (block != NULL);
182 // Returns a CONTENT_IGNORE node if not found
183 MapNode Map::getNodeNoEx(v3s16 p, bool *is_valid_position)
185 v3s16 blockpos = getNodeBlockPos(p);
186 MapBlock *block = getBlockNoCreateNoEx(blockpos);
188 if (is_valid_position != NULL)
189 *is_valid_position = false;
190 return MapNode(CONTENT_IGNORE);
193 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
195 MapNode node = block->getNodeNoCheck(relpos, &is_valid_p);
196 if (is_valid_position != NULL)
197 *is_valid_position = is_valid_p;
203 // throws InvalidPositionException if not found
204 // TODO: Now this is deprecated, getNodeNoEx should be renamed
205 MapNode Map::getNode(v3s16 p)
207 v3s16 blockpos = getNodeBlockPos(p);
208 MapBlock *block = getBlockNoCreateNoEx(blockpos);
210 throw InvalidPositionException();
211 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
212 bool is_valid_position;
213 MapNode node = block->getNodeNoCheck(relpos, &is_valid_position);
214 if (!is_valid_position)
215 throw InvalidPositionException();
220 // throws InvalidPositionException if not found
221 void Map::setNode(v3s16 p, MapNode & n)
223 v3s16 blockpos = getNodeBlockPos(p);
224 MapBlock *block = getBlockNoCreate(blockpos);
225 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
226 // Never allow placing CONTENT_IGNORE, it fucks up stuff
227 if(n.getContent() == CONTENT_IGNORE){
229 errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE"
230 <<" while trying to replace \""
231 <<m_nodedef->get(block->getNodeNoCheck(relpos, &temp_bool)).name
232 <<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
233 debug_stacks_print_to(infostream);
236 block->setNodeNoCheck(relpos, n);
239 void Map::addNodeAndUpdate(v3s16 p, MapNode n,
240 std::map<v3s16, MapBlock*> &modified_blocks,
241 bool remove_metadata)
243 // Collect old node for rollback
244 RollbackNode rollback_oldnode(this, p, m_gamedef);
246 // This is needed for updating the lighting
247 MapNode oldnode = getNodeNoEx(p);
249 // Remove node metadata
250 if (remove_metadata) {
251 removeNodeMetadata(p);
254 // Set the node on the map
255 // Ignore light (because calling voxalgo::update_lighting_nodes)
256 n.setLight(LIGHTBANK_DAY, 0, m_nodedef);
257 n.setLight(LIGHTBANK_NIGHT, 0, m_nodedef);
261 std::vector<std::pair<v3s16, MapNode> > oldnodes;
262 oldnodes.push_back(std::pair<v3s16, MapNode>(p, oldnode));
263 voxalgo::update_lighting_nodes(this, oldnodes, modified_blocks);
265 for(std::map<v3s16, MapBlock*>::iterator
266 i = modified_blocks.begin();
267 i != modified_blocks.end(); ++i)
269 i->second->expireDayNightDiff();
272 // Report for rollback
273 if(m_gamedef->rollback())
275 RollbackNode rollback_newnode(this, p, m_gamedef);
276 RollbackAction action;
277 action.setSetNode(p, rollback_oldnode, rollback_newnode);
278 m_gamedef->rollback()->reportAction(action);
282 Add neighboring liquid nodes and this node to transform queue.
283 (it's vital for the node itself to get updated last, if it was removed.)
286 v3s16(0,0,1), // back
288 v3s16(1,0,0), // right
289 v3s16(0,0,-1), // front
290 v3s16(0,-1,0), // bottom
291 v3s16(-1,0,0), // left
292 v3s16(0,0,0), // self
294 for(u16 i=0; i<7; i++)
296 v3s16 p2 = p + dirs[i];
298 bool is_valid_position;
299 MapNode n2 = getNodeNoEx(p2, &is_valid_position);
300 if(is_valid_position &&
301 (m_nodedef->get(n2).isLiquid() ||
302 n2.getContent() == CONTENT_AIR))
303 m_transforming_liquid.push_back(p2);
307 void Map::removeNodeAndUpdate(v3s16 p,
308 std::map<v3s16, MapBlock*> &modified_blocks)
310 addNodeAndUpdate(p, MapNode(CONTENT_AIR), modified_blocks, true);
313 bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
316 event.type = remove_metadata ? MEET_ADDNODE : MEET_SWAPNODE;
320 bool succeeded = true;
322 std::map<v3s16, MapBlock*> modified_blocks;
323 addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
325 // Copy modified_blocks to event
326 for(std::map<v3s16, MapBlock*>::iterator
327 i = modified_blocks.begin();
328 i != modified_blocks.end(); ++i)
330 event.modified_blocks.insert(i->first);
333 catch(InvalidPositionException &e){
337 dispatchEvent(&event);
342 bool Map::removeNodeWithEvent(v3s16 p)
345 event.type = MEET_REMOVENODE;
348 bool succeeded = true;
350 std::map<v3s16, MapBlock*> modified_blocks;
351 removeNodeAndUpdate(p, modified_blocks);
353 // Copy modified_blocks to event
354 for(std::map<v3s16, MapBlock*>::iterator
355 i = modified_blocks.begin();
356 i != modified_blocks.end(); ++i)
358 event.modified_blocks.insert(i->first);
361 catch(InvalidPositionException &e){
365 dispatchEvent(&event);
370 bool Map::getDayNightDiff(v3s16 blockpos)
373 v3s16 p = blockpos + v3s16(0,0,0);
374 MapBlock *b = getBlockNoCreate(p);
375 if(b->getDayNightDiff())
378 catch(InvalidPositionException &e){}
381 v3s16 p = blockpos + v3s16(-1,0,0);
382 MapBlock *b = getBlockNoCreate(p);
383 if(b->getDayNightDiff())
386 catch(InvalidPositionException &e){}
388 v3s16 p = blockpos + v3s16(0,-1,0);
389 MapBlock *b = getBlockNoCreate(p);
390 if(b->getDayNightDiff())
393 catch(InvalidPositionException &e){}
395 v3s16 p = blockpos + v3s16(0,0,-1);
396 MapBlock *b = getBlockNoCreate(p);
397 if(b->getDayNightDiff())
400 catch(InvalidPositionException &e){}
403 v3s16 p = blockpos + v3s16(1,0,0);
404 MapBlock *b = getBlockNoCreate(p);
405 if(b->getDayNightDiff())
408 catch(InvalidPositionException &e){}
410 v3s16 p = blockpos + v3s16(0,1,0);
411 MapBlock *b = getBlockNoCreate(p);
412 if(b->getDayNightDiff())
415 catch(InvalidPositionException &e){}
417 v3s16 p = blockpos + v3s16(0,0,1);
418 MapBlock *b = getBlockNoCreate(p);
419 if(b->getDayNightDiff())
422 catch(InvalidPositionException &e){}
427 struct TimeOrderedMapBlock {
431 TimeOrderedMapBlock(MapSector *sect, MapBlock *block) :
436 bool operator<(const TimeOrderedMapBlock &b) const
438 return block->getUsageTimer() < b.block->getUsageTimer();
445 void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
446 std::vector<v3s16> *unloaded_blocks)
448 bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
450 // Profile modified reasons
451 Profiler modprofiler;
453 std::vector<v2s16> sector_deletion_queue;
454 u32 deleted_blocks_count = 0;
455 u32 saved_blocks_count = 0;
456 u32 block_count_all = 0;
460 // If there is no practical limit, we spare creation of mapblock_queue
461 if (max_loaded_blocks == U32_MAX) {
462 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
463 si != m_sectors.end(); ++si) {
464 MapSector *sector = si->second;
466 bool all_blocks_deleted = true;
469 sector->getBlocks(blocks);
471 for (MapBlockVect::iterator i = blocks.begin();
472 i != blocks.end(); ++i) {
473 MapBlock *block = (*i);
475 block->incrementUsageTimer(dtime);
477 if (block->refGet() == 0
478 && block->getUsageTimer() > unload_timeout) {
479 v3s16 p = block->getPos();
482 if (block->getModified() != MOD_STATE_CLEAN
483 && save_before_unloading) {
484 modprofiler.add(block->getModifiedReasonString(), 1);
485 if (!saveBlock(block))
487 saved_blocks_count++;
490 // Delete from memory
491 sector->deleteBlock(block);
494 unloaded_blocks->push_back(p);
496 deleted_blocks_count++;
498 all_blocks_deleted = false;
503 if (all_blocks_deleted) {
504 sector_deletion_queue.push_back(si->first);
508 std::priority_queue<TimeOrderedMapBlock> mapblock_queue;
509 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
510 si != m_sectors.end(); ++si) {
511 MapSector *sector = si->second;
514 sector->getBlocks(blocks);
516 for(MapBlockVect::iterator i = blocks.begin();
517 i != blocks.end(); ++i) {
518 MapBlock *block = (*i);
520 block->incrementUsageTimer(dtime);
521 mapblock_queue.push(TimeOrderedMapBlock(sector, block));
524 block_count_all = mapblock_queue.size();
525 // Delete old blocks, and blocks over the limit from the memory
526 while (!mapblock_queue.empty() && (mapblock_queue.size() > max_loaded_blocks
527 || mapblock_queue.top().block->getUsageTimer() > unload_timeout)) {
528 TimeOrderedMapBlock b = mapblock_queue.top();
529 mapblock_queue.pop();
531 MapBlock *block = b.block;
533 if (block->refGet() != 0)
536 v3s16 p = block->getPos();
539 if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
540 modprofiler.add(block->getModifiedReasonString(), 1);
541 if (!saveBlock(block))
543 saved_blocks_count++;
546 // Delete from memory
547 b.sect->deleteBlock(block);
550 unloaded_blocks->push_back(p);
552 deleted_blocks_count++;
555 // Delete empty sectors
556 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
557 si != m_sectors.end(); ++si) {
558 if (si->second->empty()) {
559 sector_deletion_queue.push_back(si->first);
565 // Finally delete the empty sectors
566 deleteSectors(sector_deletion_queue);
568 if(deleted_blocks_count != 0)
570 PrintInfo(infostream); // ServerMap/ClientMap:
571 infostream<<"Unloaded "<<deleted_blocks_count
572 <<" blocks from memory";
573 if(save_before_unloading)
574 infostream<<", of which "<<saved_blocks_count<<" were written";
575 infostream<<", "<<block_count_all<<" blocks in memory";
576 infostream<<"."<<std::endl;
577 if(saved_blocks_count != 0){
578 PrintInfo(infostream); // ServerMap/ClientMap:
579 infostream<<"Blocks modified by: "<<std::endl;
580 modprofiler.print(infostream);
585 void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks)
587 timerUpdate(0.0, -1.0, 0, unloaded_blocks);
590 void Map::deleteSectors(std::vector<v2s16> §orList)
592 for(std::vector<v2s16>::iterator j = sectorList.begin();
593 j != sectorList.end(); ++j) {
594 MapSector *sector = m_sectors[*j];
595 // If sector is in sector cache, remove it from there
596 if(m_sector_cache == sector)
597 m_sector_cache = NULL;
598 // Remove from map and delete
604 void Map::PrintInfo(std::ostream &out)
609 #define WATER_DROP_BOOST 4
616 struct NodeNeighbor {
626 NodeNeighbor(const MapNode &node, NeighborType n_type, v3s16 pos)
633 void Map::transforming_liquid_add(v3s16 p) {
634 m_transforming_liquid.push_back(p);
637 s32 Map::transforming_liquid_size() {
638 return m_transforming_liquid.size();
641 void Map::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
642 ServerEnvironment *env)
644 DSTACK(FUNCTION_NAME);
645 //TimeTaker timer("transformLiquids()");
648 u32 initial_size = m_transforming_liquid.size();
650 /*if(initial_size != 0)
651 infostream<<"transformLiquids(): initial_size="<<initial_size<<std::endl;*/
653 // list of nodes that due to viscosity have not reached their max level height
654 std::deque<v3s16> must_reflow;
656 std::vector<std::pair<v3s16, MapNode> > changed_nodes;
658 u32 liquid_loop_max = g_settings->getS32("liquid_loop_max");
659 u32 loop_max = liquid_loop_max;
663 /* If liquid_loop_max is not keeping up with the queue size increase
664 * loop_max up to a maximum of liquid_loop_max * dedicated_server_step.
666 if (m_transforming_liquid.size() > loop_max * 2) {
668 float server_step = g_settings->getFloat("dedicated_server_step");
669 if (m_transforming_liquid_loop_count_multiplier - 1.0 < server_step)
670 m_transforming_liquid_loop_count_multiplier *= 1.0 + server_step / 10;
672 m_transforming_liquid_loop_count_multiplier = 1.0;
675 loop_max *= m_transforming_liquid_loop_count_multiplier;
678 while (m_transforming_liquid.size() != 0)
680 // This should be done here so that it is done when continue is used
681 if (loopcount >= initial_size || loopcount >= loop_max)
686 Get a queued transforming liquid node
688 v3s16 p0 = m_transforming_liquid.front();
689 m_transforming_liquid.pop_front();
691 MapNode n0 = getNodeNoEx(p0);
694 Collect information about current node
696 s8 liquid_level = -1;
697 // The liquid node which will be placed there if
698 // the liquid flows into this node.
699 content_t liquid_kind = CONTENT_IGNORE;
700 // The node which will be placed there if liquid
701 // can't flow into this node.
702 content_t floodable_node = CONTENT_AIR;
703 const ContentFeatures &cf = m_nodedef->get(n0);
704 LiquidType liquid_type = cf.liquid_type;
705 switch (liquid_type) {
707 liquid_level = LIQUID_LEVEL_SOURCE;
708 liquid_kind = m_nodedef->getId(cf.liquid_alternative_flowing);
711 liquid_level = (n0.param2 & LIQUID_LEVEL_MASK);
712 liquid_kind = n0.getContent();
715 // if this node is 'floodable', it *could* be transformed
716 // into a liquid, otherwise, continue with the next node.
719 floodable_node = n0.getContent();
720 liquid_kind = CONTENT_AIR;
725 Collect information about the environment
727 const v3s16 *dirs = g_6dirs;
728 NodeNeighbor sources[6]; // surrounding sources
730 NodeNeighbor flows[6]; // surrounding flowing liquid nodes
732 NodeNeighbor airs[6]; // surrounding air
734 NodeNeighbor neutrals[6]; // nodes that are solid or another kind of liquid
735 int num_neutrals = 0;
736 bool flowing_down = false;
737 bool ignored_sources = false;
738 for (u16 i = 0; i < 6; i++) {
739 NeighborType nt = NEIGHBOR_SAME_LEVEL;
748 v3s16 npos = p0 + dirs[i];
749 NodeNeighbor nb(getNodeNoEx(npos), nt, npos);
750 const ContentFeatures &cfnb = m_nodedef->get(nb.n);
751 switch (m_nodedef->get(nb.n.getContent()).liquid_type) {
753 if (cfnb.floodable) {
754 airs[num_airs++] = nb;
755 // if the current node is a water source the neighbor
756 // should be enqueded for transformation regardless of whether the
757 // current node changes or not.
758 if (nb.t != NEIGHBOR_UPPER && liquid_type != LIQUID_NONE)
759 m_transforming_liquid.push_back(npos);
760 // if the current node happens to be a flowing node, it will start to flow down here.
761 if (nb.t == NEIGHBOR_LOWER)
764 neutrals[num_neutrals++] = nb;
765 if (nb.n.getContent() == CONTENT_IGNORE) {
766 // If node below is ignore prevent water from
767 // spreading outwards and otherwise prevent from
768 // flowing away as ignore node might be the source
769 if (nb.t == NEIGHBOR_LOWER)
772 ignored_sources = true;
777 // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
778 if (liquid_kind == CONTENT_AIR)
779 liquid_kind = m_nodedef->getId(cfnb.liquid_alternative_flowing);
780 if (m_nodedef->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
781 neutrals[num_neutrals++] = nb;
783 // Do not count bottom source, it will screw things up
785 sources[num_sources++] = nb;
789 // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
790 if (liquid_kind == CONTENT_AIR)
791 liquid_kind = m_nodedef->getId(cfnb.liquid_alternative_flowing);
792 if (m_nodedef->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
793 neutrals[num_neutrals++] = nb;
795 flows[num_flows++] = nb;
796 if (nb.t == NEIGHBOR_LOWER)
804 decide on the type (and possibly level) of the current node
806 content_t new_node_content;
807 s8 new_node_level = -1;
808 s8 max_node_level = -1;
810 u8 range = m_nodedef->get(liquid_kind).liquid_range;
811 if (range > LIQUID_LEVEL_MAX + 1)
812 range = LIQUID_LEVEL_MAX + 1;
814 if ((num_sources >= 2 && m_nodedef->get(liquid_kind).liquid_renewable) || liquid_type == LIQUID_SOURCE) {
815 // liquid_kind will be set to either the flowing alternative of the node (if it's a liquid)
816 // or the flowing alternative of the first of the surrounding sources (if it's air), so
817 // it's perfectly safe to use liquid_kind here to determine the new node content.
818 new_node_content = m_nodedef->getId(m_nodedef->get(liquid_kind).liquid_alternative_source);
819 } else if (num_sources >= 1 && sources[0].t != NEIGHBOR_LOWER) {
820 // liquid_kind is set properly, see above
821 max_node_level = new_node_level = LIQUID_LEVEL_MAX;
822 if (new_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
823 new_node_content = liquid_kind;
825 new_node_content = floodable_node;
826 } else if (ignored_sources && liquid_level >= 0) {
827 // Maybe there are neighbouring sources that aren't loaded yet
828 // so prevent flowing away.
829 new_node_level = liquid_level;
830 new_node_content = liquid_kind;
832 // no surrounding sources, so get the maximum level that can flow into this node
833 for (u16 i = 0; i < num_flows; i++) {
834 u8 nb_liquid_level = (flows[i].n.param2 & LIQUID_LEVEL_MASK);
835 switch (flows[i].t) {
837 if (nb_liquid_level + WATER_DROP_BOOST > max_node_level) {
838 max_node_level = LIQUID_LEVEL_MAX;
839 if (nb_liquid_level + WATER_DROP_BOOST < LIQUID_LEVEL_MAX)
840 max_node_level = nb_liquid_level + WATER_DROP_BOOST;
841 } else if (nb_liquid_level > max_node_level) {
842 max_node_level = nb_liquid_level;
847 case NEIGHBOR_SAME_LEVEL:
848 if ((flows[i].n.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK &&
849 nb_liquid_level > 0 && nb_liquid_level - 1 > max_node_level)
850 max_node_level = nb_liquid_level - 1;
855 u8 viscosity = m_nodedef->get(liquid_kind).liquid_viscosity;
856 if (viscosity > 1 && max_node_level != liquid_level) {
857 // amount to gain, limited by viscosity
858 // must be at least 1 in absolute value
859 s8 level_inc = max_node_level - liquid_level;
860 if (level_inc < -viscosity || level_inc > viscosity)
861 new_node_level = liquid_level + level_inc/viscosity;
862 else if (level_inc < 0)
863 new_node_level = liquid_level - 1;
864 else if (level_inc > 0)
865 new_node_level = liquid_level + 1;
866 if (new_node_level != max_node_level)
867 must_reflow.push_back(p0);
869 new_node_level = max_node_level;
872 if (max_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
873 new_node_content = liquid_kind;
875 new_node_content = floodable_node;
880 check if anything has changed. if not, just continue with the next node.
882 if (new_node_content == n0.getContent() &&
883 (m_nodedef->get(n0.getContent()).liquid_type != LIQUID_FLOWING ||
884 ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level &&
885 ((n0.param2 & LIQUID_FLOW_DOWN_MASK) == LIQUID_FLOW_DOWN_MASK)
891 update the current node
894 //bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
895 if (m_nodedef->get(new_node_content).liquid_type == LIQUID_FLOWING) {
896 // set level to last 3 bits, flowing down bit to 4th bit
897 n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK);
899 // set the liquid level and flow bit to 0
900 n0.param2 = ~(LIQUID_LEVEL_MASK | LIQUID_FLOW_DOWN_MASK);
904 n0.setContent(new_node_content);
906 // on_flood() the node
907 if (floodable_node != CONTENT_AIR) {
908 if (env->getScriptIface()->node_on_flood(p0, n00, n0))
912 // Ignore light (because calling voxalgo::update_lighting_nodes)
913 n0.setLight(LIGHTBANK_DAY, 0, m_nodedef);
914 n0.setLight(LIGHTBANK_NIGHT, 0, m_nodedef);
916 // Find out whether there is a suspect for this action
918 if (m_gamedef->rollback())
919 suspect = m_gamedef->rollback()->getSuspect(p0, 83, 1);
921 if (m_gamedef->rollback() && !suspect.empty()) {
923 RollbackScopeActor rollback_scope(m_gamedef->rollback(), suspect, true);
924 // Get old node for rollback
925 RollbackNode rollback_oldnode(this, p0, m_gamedef);
929 RollbackNode rollback_newnode(this, p0, m_gamedef);
930 RollbackAction action;
931 action.setSetNode(p0, rollback_oldnode, rollback_newnode);
932 m_gamedef->rollback()->reportAction(action);
938 v3s16 blockpos = getNodeBlockPos(p0);
939 MapBlock *block = getBlockNoCreateNoEx(blockpos);
941 modified_blocks[blockpos] = block;
942 changed_nodes.push_back(std::pair<v3s16, MapNode>(p0, n00));
946 enqueue neighbors for update if neccessary
948 switch (m_nodedef->get(n0.getContent()).liquid_type) {
951 // make sure source flows into all neighboring nodes
952 for (u16 i = 0; i < num_flows; i++)
953 if (flows[i].t != NEIGHBOR_UPPER)
954 m_transforming_liquid.push_back(flows[i].p);
955 for (u16 i = 0; i < num_airs; i++)
956 if (airs[i].t != NEIGHBOR_UPPER)
957 m_transforming_liquid.push_back(airs[i].p);
960 // this flow has turned to air; neighboring flows might need to do the same
961 for (u16 i = 0; i < num_flows; i++)
962 m_transforming_liquid.push_back(flows[i].p);
966 //infostream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
968 for (std::deque<v3s16>::iterator iter = must_reflow.begin(); iter != must_reflow.end(); ++iter)
969 m_transforming_liquid.push_back(*iter);
971 voxalgo::update_lighting_nodes(this, changed_nodes, modified_blocks);
974 /* ----------------------------------------------------------------------
975 * Manage the queue so that it does not grow indefinately
977 u16 time_until_purge = g_settings->getU16("liquid_queue_purge_time");
979 if (time_until_purge == 0)
980 return; // Feature disabled
982 time_until_purge *= 1000; // seconds -> milliseconds
984 u32 curr_time = porting::getTime(PRECISION_MILLI);
985 u32 prev_unprocessed = m_unprocessed_count;
986 m_unprocessed_count = m_transforming_liquid.size();
988 // if unprocessed block count is decreasing or stable
989 if (m_unprocessed_count <= prev_unprocessed) {
990 m_queue_size_timer_started = false;
992 if (!m_queue_size_timer_started)
993 m_inc_trending_up_start_time = curr_time;
994 m_queue_size_timer_started = true;
997 // Account for curr_time overflowing
998 if (m_queue_size_timer_started && m_inc_trending_up_start_time > curr_time)
999 m_queue_size_timer_started = false;
1001 /* If the queue has been growing for more than liquid_queue_purge_time seconds
1002 * and the number of unprocessed blocks is still > liquid_loop_max then we
1003 * cannot keep up; dump the oldest blocks from the queue so that the queue
1004 * has liquid_loop_max items in it
1006 if (m_queue_size_timer_started
1007 && curr_time - m_inc_trending_up_start_time > time_until_purge
1008 && m_unprocessed_count > liquid_loop_max) {
1010 size_t dump_qty = m_unprocessed_count - liquid_loop_max;
1012 infostream << "transformLiquids(): DUMPING " << dump_qty
1013 << " blocks from the queue" << std::endl;
1016 m_transforming_liquid.pop_front();
1018 m_queue_size_timer_started = false; // optimistically assume we can keep up now
1019 m_unprocessed_count = m_transforming_liquid.size();
1023 std::vector<v3s16> Map::findNodesWithMetadata(v3s16 p1, v3s16 p2)
1025 std::vector<v3s16> positions_with_meta;
1027 sortBoxVerticies(p1, p2);
1028 v3s16 bpmin = getNodeBlockPos(p1);
1029 v3s16 bpmax = getNodeBlockPos(p2);
1031 VoxelArea area(p1, p2);
1033 for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
1034 for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
1035 for (s16 x = bpmin.X; x <= bpmax.X; x++) {
1036 v3s16 blockpos(x, y, z);
1038 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1040 verbosestream << "Map::getNodeMetadata(): Need to emerge "
1041 << PP(blockpos) << std::endl;
1042 block = emergeBlock(blockpos, false);
1045 infostream << "WARNING: Map::getNodeMetadata(): Block not found"
1050 v3s16 p_base = blockpos * MAP_BLOCKSIZE;
1051 std::vector<v3s16> keys = block->m_node_metadata.getAllKeys();
1052 for (size_t i = 0; i != keys.size(); i++) {
1053 v3s16 p(keys[i] + p_base);
1054 if (!area.contains(p))
1057 positions_with_meta.push_back(p);
1061 return positions_with_meta;
1064 NodeMetadata *Map::getNodeMetadata(v3s16 p)
1066 v3s16 blockpos = getNodeBlockPos(p);
1067 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1068 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1070 infostream<<"Map::getNodeMetadata(): Need to emerge "
1071 <<PP(blockpos)<<std::endl;
1072 block = emergeBlock(blockpos, false);
1075 warningstream<<"Map::getNodeMetadata(): Block not found"
1079 NodeMetadata *meta = block->m_node_metadata.get(p_rel);
1083 bool Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
1085 v3s16 blockpos = getNodeBlockPos(p);
1086 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1087 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1089 infostream<<"Map::setNodeMetadata(): Need to emerge "
1090 <<PP(blockpos)<<std::endl;
1091 block = emergeBlock(blockpos, false);
1094 warningstream<<"Map::setNodeMetadata(): Block not found"
1098 block->m_node_metadata.set(p_rel, meta);
1102 void Map::removeNodeMetadata(v3s16 p)
1104 v3s16 blockpos = getNodeBlockPos(p);
1105 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1106 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1109 warningstream<<"Map::removeNodeMetadata(): Block not found"
1113 block->m_node_metadata.remove(p_rel);
1116 NodeTimer Map::getNodeTimer(v3s16 p)
1118 v3s16 blockpos = getNodeBlockPos(p);
1119 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1120 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1122 infostream<<"Map::getNodeTimer(): Need to emerge "
1123 <<PP(blockpos)<<std::endl;
1124 block = emergeBlock(blockpos, false);
1127 warningstream<<"Map::getNodeTimer(): Block not found"
1131 NodeTimer t = block->m_node_timers.get(p_rel);
1132 NodeTimer nt(t.timeout, t.elapsed, p);
1136 void Map::setNodeTimer(const NodeTimer &t)
1138 v3s16 p = t.position;
1139 v3s16 blockpos = getNodeBlockPos(p);
1140 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1141 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1143 infostream<<"Map::setNodeTimer(): Need to emerge "
1144 <<PP(blockpos)<<std::endl;
1145 block = emergeBlock(blockpos, false);
1148 warningstream<<"Map::setNodeTimer(): Block not found"
1152 NodeTimer nt(t.timeout, t.elapsed, p_rel);
1153 block->m_node_timers.set(nt);
1156 void Map::removeNodeTimer(v3s16 p)
1158 v3s16 blockpos = getNodeBlockPos(p);
1159 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1160 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1163 warningstream<<"Map::removeNodeTimer(): Block not found"
1167 block->m_node_timers.remove(p_rel);
1170 bool Map::isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac,
1171 float start_off, float end_off, u32 needed_count)
1173 float d0 = (float)BS * p0.getDistanceFrom(p1);
1175 v3f uf = v3f(u0.X, u0.Y, u0.Z) * BS;
1177 v3f p0f = v3f(p0.X, p0.Y, p0.Z) * BS;
1179 for(float s=start_off; s<d0+end_off; s+=step){
1180 v3f pf = p0f + uf * s;
1181 v3s16 p = floatToInt(pf, BS);
1182 MapNode n = getNodeNoEx(p);
1183 const ContentFeatures &f = m_nodedef->get(n);
1184 if(f.drawtype == NDT_NORMAL){
1185 // not transparent, see ContentFeature::updateTextures
1187 if(count >= needed_count)
1195 bool Map::isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes) {
1196 v3s16 cpn = block->getPos() * MAP_BLOCKSIZE;
1197 cpn += v3s16(MAP_BLOCKSIZE / 2, MAP_BLOCKSIZE / 2, MAP_BLOCKSIZE / 2);
1198 float step = BS * 1;
1199 float stepfac = 1.1;
1200 float startoff = BS * 1;
1201 // The occlusion search of 'isOccluded()' must stop short of the target
1202 // point by distance 'endoff' (end offset) to not enter the target mapblock.
1203 // For the 8 mapblock corners 'endoff' must therefore be the maximum diagonal
1204 // of a mapblock, because we must consider all view angles.
1205 // sqrt(1^2 + 1^2 + 1^2) = 1.732
1206 float endoff = -BS * MAP_BLOCKSIZE * 1.732050807569;
1207 v3s16 spn = cam_pos_nodes;
1208 s16 bs2 = MAP_BLOCKSIZE / 2 + 1;
1209 // to reduce the likelihood of falsely occluded blocks
1210 // require at least two solid blocks
1211 // this is a HACK, we should think of a more precise algorithm
1212 u32 needed_count = 2;
1215 // For the central point of the mapblock 'endoff' can be halved
1216 isOccluded(spn, cpn,
1217 step, stepfac, startoff, endoff / 2.0f, needed_count) &&
1218 isOccluded(spn, cpn + v3s16(bs2,bs2,bs2),
1219 step, stepfac, startoff, endoff, needed_count) &&
1220 isOccluded(spn, cpn + v3s16(bs2,bs2,-bs2),
1221 step, stepfac, startoff, endoff, needed_count) &&
1222 isOccluded(spn, cpn + v3s16(bs2,-bs2,bs2),
1223 step, stepfac, startoff, endoff, needed_count) &&
1224 isOccluded(spn, cpn + v3s16(bs2,-bs2,-bs2),
1225 step, stepfac, startoff, endoff, needed_count) &&
1226 isOccluded(spn, cpn + v3s16(-bs2,bs2,bs2),
1227 step, stepfac, startoff, endoff, needed_count) &&
1228 isOccluded(spn, cpn + v3s16(-bs2,bs2,-bs2),
1229 step, stepfac, startoff, endoff, needed_count) &&
1230 isOccluded(spn, cpn + v3s16(-bs2,-bs2,bs2),
1231 step, stepfac, startoff, endoff, needed_count) &&
1232 isOccluded(spn, cpn + v3s16(-bs2,-bs2,-bs2),
1233 step, stepfac, startoff, endoff, needed_count));
1239 ServerMap::ServerMap(const std::string &savedir, IGameDef *gamedef,
1240 EmergeManager *emerge):
1241 Map(dout_server, gamedef),
1242 settings_mgr(g_settings, savedir + DIR_DELIM + "map_meta.txt"),
1244 m_map_metadata_changed(true)
1246 verbosestream<<FUNCTION_NAME<<std::endl;
1248 // Tell the EmergeManager about our MapSettingsManager
1249 emerge->map_settings_mgr = &settings_mgr;
1252 Try to load map; if not found, create a new one.
1255 // Determine which database backend to use
1256 std::string conf_path = savedir + DIR_DELIM + "world.mt";
1258 bool succeeded = conf.readConfigFile(conf_path.c_str());
1259 if (!succeeded || !conf.exists("backend")) {
1260 // fall back to sqlite3
1261 conf.set("backend", "sqlite3");
1263 std::string backend = conf.get("backend");
1264 dbase = createDatabase(backend, savedir, conf);
1266 if (!conf.updateConfigFile(conf_path.c_str()))
1267 errorstream << "ServerMap::ServerMap(): Failed to update world.mt!" << std::endl;
1269 m_savedir = savedir;
1270 m_map_saving_enabled = false;
1274 // If directory exists, check contents and load if possible
1275 if(fs::PathExists(m_savedir))
1277 // If directory is empty, it is safe to save into it.
1278 if(fs::GetDirListing(m_savedir).size() == 0)
1280 infostream<<"ServerMap: Empty save directory is valid."
1282 m_map_saving_enabled = true;
1287 if (settings_mgr.loadMapMeta()) {
1288 infostream << "ServerMap: Metadata loaded from "
1289 << savedir << std::endl;
1291 infostream << "ServerMap: Metadata could not be loaded "
1292 "from " << savedir << ", assuming valid save "
1293 "directory." << std::endl;
1296 m_map_saving_enabled = true;
1297 // Map loaded, not creating new one
1301 // If directory doesn't exist, it is safe to save to it
1303 m_map_saving_enabled = true;
1306 catch(std::exception &e)
1308 warningstream<<"ServerMap: Failed to load map from "<<savedir
1309 <<", exception: "<<e.what()<<std::endl;
1310 infostream<<"Please remove the map or fix it."<<std::endl;
1311 warningstream<<"Map saving will be disabled."<<std::endl;
1314 infostream<<"Initializing new map."<<std::endl;
1316 // Create zero sector
1317 emergeSector(v2s16(0,0));
1319 // Initially write whole map
1320 save(MOD_STATE_CLEAN);
1323 ServerMap::~ServerMap()
1325 verbosestream<<FUNCTION_NAME<<std::endl;
1329 if(m_map_saving_enabled)
1331 // Save only changed parts
1332 save(MOD_STATE_WRITE_AT_UNLOAD);
1333 infostream<<"ServerMap: Saved map to "<<m_savedir<<std::endl;
1337 infostream<<"ServerMap: Map not saved"<<std::endl;
1340 catch(std::exception &e)
1342 infostream<<"ServerMap: Failed to save map to "<<m_savedir
1343 <<", exception: "<<e.what()<<std::endl;
1347 Close database if it was opened
1355 core::map<v2s16, MapChunk*>::Iterator i = m_chunks.getIterator();
1356 for(; i.atEnd() == false; i++)
1358 MapChunk *chunk = i.getNode()->getValue();
1364 MapgenParams *ServerMap::getMapgenParams()
1366 // getMapgenParams() should only ever be called after Server is initialized
1367 assert(settings_mgr.mapgen_params != NULL);
1368 return settings_mgr.mapgen_params;
1371 u64 ServerMap::getSeed()
1373 return getMapgenParams()->seed;
1376 s16 ServerMap::getWaterLevel()
1378 return getMapgenParams()->water_level;
1381 bool ServerMap::blockpos_over_mapgen_limit(v3s16 p)
1383 const s16 mapgen_limit_bp = rangelim(
1384 getMapgenParams()->mapgen_limit, 0, MAX_MAP_GENERATION_LIMIT) /
1386 return p.X < -mapgen_limit_bp ||
1387 p.X > mapgen_limit_bp ||
1388 p.Y < -mapgen_limit_bp ||
1389 p.Y > mapgen_limit_bp ||
1390 p.Z < -mapgen_limit_bp ||
1391 p.Z > mapgen_limit_bp;
1394 bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data)
1396 s16 csize = getMapgenParams()->chunksize;
1397 v3s16 bpmin = EmergeManager::getContainingChunk(blockpos, csize);
1398 v3s16 bpmax = bpmin + v3s16(1, 1, 1) * (csize - 1);
1400 bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1401 EMERGE_DBG_OUT("initBlockMake(): " PP(bpmin) " - " PP(bpmax));
1403 v3s16 extra_borders(1, 1, 1);
1404 v3s16 full_bpmin = bpmin - extra_borders;
1405 v3s16 full_bpmax = bpmax + extra_borders;
1407 // Do nothing if not inside mapgen limits (+-1 because of neighbors)
1408 if (blockpos_over_mapgen_limit(full_bpmin) ||
1409 blockpos_over_mapgen_limit(full_bpmax))
1412 data->seed = getSeed();
1413 data->blockpos_min = bpmin;
1414 data->blockpos_max = bpmax;
1415 data->blockpos_requested = blockpos;
1416 data->nodedef = m_nodedef;
1419 Create the whole area of this and the neighboring blocks
1421 for (s16 x = full_bpmin.X; x <= full_bpmax.X; x++)
1422 for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++) {
1423 v2s16 sectorpos(x, z);
1424 // Sector metadata is loaded from disk if not already loaded.
1425 ServerMapSector *sector = createSector(sectorpos);
1426 FATAL_ERROR_IF(sector == NULL, "createSector() failed");
1428 for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) {
1431 MapBlock *block = emergeBlock(p, false);
1432 if (block == NULL) {
1433 block = createBlock(p);
1435 // Block gets sunlight if this is true.
1436 // Refer to the map generator heuristics.
1437 bool ug = m_emerge->isBlockUnderground(p);
1438 block->setIsUnderground(ug);
1444 Now we have a big empty area.
1446 Make a ManualMapVoxelManipulator that contains this and the
1450 data->vmanip = new MMVManip(this);
1451 data->vmanip->initialEmerge(full_bpmin, full_bpmax);
1453 // Note: we may need this again at some point.
1455 // Ensure none of the blocks to be generated were marked as
1456 // containing CONTENT_IGNORE
1457 for (s16 z = blockpos_min.Z; z <= blockpos_max.Z; z++) {
1458 for (s16 y = blockpos_min.Y; y <= blockpos_max.Y; y++) {
1459 for (s16 x = blockpos_min.X; x <= blockpos_max.X; x++) {
1460 core::map<v3s16, u8>::Node *n;
1461 n = data->vmanip->m_loaded_blocks.find(v3s16(x, y, z));
1464 u8 flags = n->getValue();
1465 flags &= ~VMANIP_BLOCK_CONTAINS_CIGNORE;
1472 // Data is ready now.
1476 void ServerMap::finishBlockMake(BlockMakeData *data,
1477 std::map<v3s16, MapBlock*> *changed_blocks)
1479 v3s16 bpmin = data->blockpos_min;
1480 v3s16 bpmax = data->blockpos_max;
1482 v3s16 extra_borders(1, 1, 1);
1484 bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1485 EMERGE_DBG_OUT("finishBlockMake(): " PP(bpmin) " - " PP(bpmax));
1488 Blit generated stuff to map
1489 NOTE: blitBackAll adds nearly everything to changed_blocks
1491 data->vmanip->blitBackAll(changed_blocks);
1493 EMERGE_DBG_OUT("finishBlockMake: changed_blocks.size()="
1494 << changed_blocks->size());
1497 Copy transforming liquid information
1499 while (data->transforming_liquid.size()) {
1500 m_transforming_liquid.push_back(data->transforming_liquid.front());
1501 data->transforming_liquid.pop_front();
1504 for (std::map<v3s16, MapBlock *>::iterator
1505 it = changed_blocks->begin();
1506 it != changed_blocks->end(); ++it) {
1507 MapBlock *block = it->second;
1511 Update day/night difference cache of the MapBlocks
1513 block->expireDayNightDiff();
1515 Set block as modified
1517 block->raiseModified(MOD_STATE_WRITE_NEEDED,
1518 MOD_REASON_EXPIRE_DAYNIGHTDIFF);
1522 Set central blocks as generated
1524 for (s16 x = bpmin.X; x <= bpmax.X; x++)
1525 for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
1526 for (s16 y = bpmin.Y; y <= bpmax.Y; y++) {
1527 MapBlock *block = getBlockNoCreateNoEx(v3s16(x, y, z));
1531 block->setGenerated(true);
1535 Save changed parts of map
1536 NOTE: Will be saved later.
1538 //save(MOD_STATE_WRITE_AT_UNLOAD);
1541 ServerMapSector *ServerMap::createSector(v2s16 p2d)
1543 DSTACKF("%s: p2d=(%d,%d)",
1548 Check if it exists already in memory
1550 ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1555 Try to load it from disk (with blocks)
1557 //if(loadSectorFull(p2d) == true)
1560 Try to load metadata from disk
1563 if(loadSectorMeta(p2d) == true)
1565 ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1568 infostream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
1569 throw InvalidPositionException("");
1576 Do not create over max mapgen limit
1578 const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
1579 if (p2d.X < -max_limit_bp ||
1580 p2d.X > max_limit_bp ||
1581 p2d.Y < -max_limit_bp ||
1582 p2d.Y > max_limit_bp)
1583 throw InvalidPositionException("createSector(): pos. over max mapgen limit");
1586 Generate blank sector
1589 sector = new ServerMapSector(this, p2d, m_gamedef);
1591 // Sector position on map in nodes
1592 //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
1597 m_sectors[p2d] = sector;
1604 This is a quick-hand function for calling makeBlock().
1606 MapBlock * ServerMap::generateBlock(
1608 std::map<v3s16, MapBlock*> &modified_blocks
1611 DSTACKF("%s: p=(%d,%d,%d)", FUNCTION_NAME, p.X, p.Y, p.Z);
1613 /*infostream<<"generateBlock(): "
1614 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1617 bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
1619 TimeTaker timer("generateBlock");
1621 //MapBlock *block = original_dummy;
1623 v2s16 p2d(p.X, p.Z);
1624 v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
1627 Do not generate over-limit
1629 if(blockpos_over_limit(p))
1631 infostream<<FUNCTION_NAME<<": Block position over limit"<<std::endl;
1632 throw InvalidPositionException("generateBlock(): pos. over limit");
1636 Create block make data
1639 initBlockMake(&data, p);
1645 TimeTaker t("mapgen::make_block()");
1646 mapgen->makeChunk(&data);
1647 //mapgen::make_block(&data);
1649 if(enable_mapgen_debug_info == false)
1650 t.stop(true); // Hide output
1654 Blit data back on map, update lighting, add mobs and whatever this does
1656 finishBlockMake(&data, modified_blocks);
1661 MapBlock *block = getBlockNoCreateNoEx(p);
1669 bool erroneus_content = false;
1670 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1671 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1672 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1675 MapNode n = block->getNode(p);
1676 if(n.getContent() == CONTENT_IGNORE)
1678 infostream<<"CONTENT_IGNORE at "
1679 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1681 erroneus_content = true;
1685 if(erroneus_content)
1694 Generate a completely empty block
1698 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1699 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1701 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1704 n.setContent(CONTENT_AIR);
1705 block->setNode(v3s16(x0,y0,z0), n);
1711 if(enable_mapgen_debug_info == false)
1712 timer.stop(true); // Hide output
1718 MapBlock * ServerMap::createBlock(v3s16 p)
1720 DSTACKF("%s: p=(%d,%d,%d)",
1721 FUNCTION_NAME, p.X, p.Y, p.Z);
1724 Do not create over max mapgen limit
1726 if (blockpos_over_max_limit(p))
1727 throw InvalidPositionException("createBlock(): pos. over max mapgen limit");
1729 v2s16 p2d(p.X, p.Z);
1732 This will create or load a sector if not found in memory.
1733 If block exists on disk, it will be loaded.
1735 NOTE: On old save formats, this will be slow, as it generates
1736 lighting on blocks for them.
1738 ServerMapSector *sector;
1740 sector = (ServerMapSector*)createSector(p2d);
1741 assert(sector->getId() == MAPSECTOR_SERVER);
1743 catch(InvalidPositionException &e)
1745 infostream<<"createBlock: createSector() failed"<<std::endl;
1749 NOTE: This should not be done, or at least the exception
1750 should not be passed on as std::exception, because it
1751 won't be catched at all.
1753 /*catch(std::exception &e)
1755 infostream<<"createBlock: createSector() failed: "
1756 <<e.what()<<std::endl;
1761 Try to get a block from the sector
1764 MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
1767 if(block->isDummy())
1772 block = sector->createBlankBlock(block_y);
1777 MapBlock * ServerMap::emergeBlock(v3s16 p, bool create_blank)
1779 DSTACKF("%s: p=(%d,%d,%d), create_blank=%d",
1781 p.X, p.Y, p.Z, create_blank);
1784 MapBlock *block = getBlockNoCreateNoEx(p);
1785 if(block && block->isDummy() == false)
1790 MapBlock *block = loadBlock(p);
1796 ServerMapSector *sector = createSector(v2s16(p.X, p.Z));
1797 MapBlock *block = sector->createBlankBlock(p.Y);
1805 std::map<v3s16, MapBlock*> modified_blocks;
1806 MapBlock *block = generateBlock(p, modified_blocks);
1810 event.type = MEET_OTHER;
1813 // Copy modified_blocks to event
1814 for(std::map<v3s16, MapBlock*>::iterator
1815 i = modified_blocks.begin();
1816 i != modified_blocks.end(); ++i)
1818 event.modified_blocks.insert(i->first);
1822 dispatchEvent(&event);
1832 MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
1834 MapBlock *block = getBlockNoCreateNoEx(p3d);
1836 m_emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, p3d, false);
1841 void ServerMap::prepareBlock(MapBlock *block) {
1844 // N.B. This requires no synchronization, since data will not be modified unless
1845 // the VoxelManipulator being updated belongs to the same thread.
1846 void ServerMap::updateVManip(v3s16 pos)
1848 Mapgen *mg = m_emerge->getCurrentMapgen();
1852 MMVManip *vm = mg->vm;
1856 if (!vm->m_area.contains(pos))
1859 s32 idx = vm->m_area.index(pos);
1860 vm->m_data[idx] = getNodeNoEx(pos);
1861 vm->m_flags[idx] &= ~VOXELFLAG_NO_DATA;
1863 vm->m_is_dirty = true;
1866 s16 ServerMap::findGroundLevel(v2s16 p2d)
1870 Uh, just do something random...
1872 // Find existing map from top to down
1875 v3s16 p(p2d.X, max, p2d.Y);
1876 for(; p.Y>min; p.Y--)
1878 MapNode n = getNodeNoEx(p);
1879 if(n.getContent() != CONTENT_IGNORE)
1884 // If this node is not air, go to plan b
1885 if(getNodeNoEx(p).getContent() != CONTENT_AIR)
1887 // Search existing walkable and return it
1888 for(; p.Y>min; p.Y--)
1890 MapNode n = getNodeNoEx(p);
1891 if(content_walkable(n.d) && n.getContent() != CONTENT_IGNORE)
1900 Determine from map generator noise functions
1903 s16 level = m_emerge->getGroundLevelAtPoint(p2d);
1906 //double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
1907 //return (s16)level;
1910 bool ServerMap::loadFromFolders() {
1911 if (!dbase->initialized() &&
1912 !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
1917 void ServerMap::createDirs(std::string path)
1919 if(fs::CreateAllDirs(path) == false)
1921 m_dout<<"ServerMap: Failed to create directory "
1922 <<"\""<<path<<"\""<<std::endl;
1923 throw BaseException("ServerMap failed to create directory");
1927 std::string ServerMap::getSectorDir(v2s16 pos, int layout)
1933 snprintf(cc, 9, "%.4x%.4x",
1934 (unsigned int) pos.X & 0xffff,
1935 (unsigned int) pos.Y & 0xffff);
1937 return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
1939 snprintf(cc, 9, (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
1940 (unsigned int) pos.X & 0xfff,
1941 (unsigned int) pos.Y & 0xfff);
1943 return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc;
1950 v2s16 ServerMap::getSectorPos(const std::string &dirname)
1952 unsigned int x = 0, y = 0;
1954 std::string component;
1955 fs::RemoveLastPathComponent(dirname, &component, 1);
1956 if(component.size() == 8)
1959 r = sscanf(component.c_str(), "%4x%4x", &x, &y);
1961 else if(component.size() == 3)
1964 fs::RemoveLastPathComponent(dirname, &component, 2);
1965 r = sscanf(component.c_str(), (std::string("%3x") + DIR_DELIM + "%3x").c_str(), &x, &y);
1966 // Sign-extend the 12 bit values up to 16 bits...
1967 if(x & 0x800) x |= 0xF000;
1968 if(y & 0x800) y |= 0xF000;
1975 FATAL_ERROR_IF(r != 2, "getSectorPos()");
1976 v2s16 pos((s16)x, (s16)y);
1980 v3s16 ServerMap::getBlockPos(const std::string §ordir, const std::string &blockfile)
1982 v2s16 p2d = getSectorPos(sectordir);
1984 if(blockfile.size() != 4){
1985 throw InvalidFilenameException("Invalid block filename");
1988 int r = sscanf(blockfile.c_str(), "%4x", &y);
1990 throw InvalidFilenameException("Invalid block filename");
1991 return v3s16(p2d.X, y, p2d.Y);
1994 std::string ServerMap::getBlockFilename(v3s16 p)
1997 snprintf(cc, 5, "%.4x", (unsigned int)p.Y&0xffff);
2001 void ServerMap::save(ModifiedState save_level)
2003 DSTACK(FUNCTION_NAME);
2004 if(m_map_saving_enabled == false) {
2005 warningstream<<"Not saving map, saving disabled."<<std::endl;
2009 if(save_level == MOD_STATE_CLEAN)
2010 infostream<<"ServerMap: Saving whole map, this can take time."
2013 if (m_map_metadata_changed || save_level == MOD_STATE_CLEAN) {
2014 if (settings_mgr.saveMapMeta())
2015 m_map_metadata_changed = false;
2018 // Profile modified reasons
2019 Profiler modprofiler;
2021 u32 sector_meta_count = 0;
2022 u32 block_count = 0;
2023 u32 block_count_all = 0; // Number of blocks in memory
2025 // Don't do anything with sqlite unless something is really saved
2026 bool save_started = false;
2028 for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
2029 i != m_sectors.end(); ++i) {
2030 ServerMapSector *sector = (ServerMapSector*)i->second;
2031 assert(sector->getId() == MAPSECTOR_SERVER);
2033 if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
2034 saveSectorMeta(sector);
2035 sector_meta_count++;
2038 MapBlockVect blocks;
2039 sector->getBlocks(blocks);
2041 for(MapBlockVect::iterator j = blocks.begin();
2042 j != blocks.end(); ++j) {
2043 MapBlock *block = *j;
2047 if(block->getModified() >= (u32)save_level) {
2051 save_started = true;
2054 modprofiler.add(block->getModifiedReasonString(), 1);
2059 /*infostream<<"ServerMap: Written block ("
2060 <<block->getPos().X<<","
2061 <<block->getPos().Y<<","
2062 <<block->getPos().Z<<")"
2072 Only print if something happened or saved whole map
2074 if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
2075 || block_count != 0) {
2076 infostream<<"ServerMap: Written: "
2077 <<sector_meta_count<<" sector metadata files, "
2078 <<block_count<<" block files"
2079 <<", "<<block_count_all<<" blocks in memory."
2081 PrintInfo(infostream); // ServerMap/ClientMap:
2082 infostream<<"Blocks modified by: "<<std::endl;
2083 modprofiler.print(infostream);
2087 void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
2089 if (loadFromFolders()) {
2090 errorstream << "Map::listAllLoadableBlocks(): Result will be missing "
2091 << "all blocks that are stored in flat files." << std::endl;
2093 dbase->listAllLoadableBlocks(dst);
2096 void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
2098 for(std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
2099 si != m_sectors.end(); ++si)
2101 MapSector *sector = si->second;
2103 MapBlockVect blocks;
2104 sector->getBlocks(blocks);
2106 for(MapBlockVect::iterator i = blocks.begin();
2107 i != blocks.end(); ++i) {
2108 v3s16 p = (*i)->getPos();
2114 void ServerMap::saveSectorMeta(ServerMapSector *sector)
2116 DSTACK(FUNCTION_NAME);
2117 // Format used for writing
2118 u8 version = SER_FMT_VER_HIGHEST_WRITE;
2120 v2s16 pos = sector->getPos();
2121 std::string dir = getSectorDir(pos);
2124 std::string fullpath = dir + DIR_DELIM + "meta";
2125 std::ostringstream ss(std::ios_base::binary);
2127 sector->serialize(ss, version);
2129 if(!fs::safeWriteToFile(fullpath, ss.str()))
2130 throw FileNotGoodException("Cannot write sector metafile");
2132 sector->differs_from_disk = false;
2135 MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
2137 DSTACK(FUNCTION_NAME);
2139 v2s16 p2d = getSectorPos(sectordir);
2141 ServerMapSector *sector = NULL;
2143 std::string fullpath = sectordir + DIR_DELIM + "meta";
2144 std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2145 if(is.good() == false)
2147 // If the directory exists anyway, it probably is in some old
2148 // format. Just go ahead and create the sector.
2149 if(fs::PathExists(sectordir))
2151 /*infostream<<"ServerMap::loadSectorMeta(): Sector metafile "
2152 <<fullpath<<" doesn't exist but directory does."
2153 <<" Continuing with a sector with no metadata."
2155 sector = new ServerMapSector(this, p2d, m_gamedef);
2156 m_sectors[p2d] = sector;
2160 throw FileNotGoodException("Cannot open sector metafile");
2165 sector = ServerMapSector::deSerialize
2166 (is, this, p2d, m_sectors, m_gamedef);
2168 saveSectorMeta(sector);
2171 sector->differs_from_disk = false;
2176 bool ServerMap::loadSectorMeta(v2s16 p2d)
2178 DSTACK(FUNCTION_NAME);
2180 // The directory layout we're going to load from.
2181 // 1 - original sectors/xxxxzzzz/
2182 // 2 - new sectors2/xxx/zzz/
2183 // If we load from anything but the latest structure, we will
2184 // immediately save to the new one, and remove the old.
2186 std::string sectordir1 = getSectorDir(p2d, 1);
2187 std::string sectordir;
2188 if(fs::PathExists(sectordir1))
2190 sectordir = sectordir1;
2195 sectordir = getSectorDir(p2d, 2);
2199 loadSectorMeta(sectordir, loadlayout != 2);
2201 catch(InvalidFilenameException &e)
2205 catch(FileNotGoodException &e)
2209 catch(std::exception &e)
2218 bool ServerMap::loadSectorFull(v2s16 p2d)
2220 DSTACK(FUNCTION_NAME);
2222 MapSector *sector = NULL;
2224 // The directory layout we're going to load from.
2225 // 1 - original sectors/xxxxzzzz/
2226 // 2 - new sectors2/xxx/zzz/
2227 // If we load from anything but the latest structure, we will
2228 // immediately save to the new one, and remove the old.
2230 std::string sectordir1 = getSectorDir(p2d, 1);
2231 std::string sectordir;
2232 if(fs::PathExists(sectordir1))
2234 sectordir = sectordir1;
2239 sectordir = getSectorDir(p2d, 2);
2243 sector = loadSectorMeta(sectordir, loadlayout != 2);
2245 catch(InvalidFilenameException &e)
2249 catch(FileNotGoodException &e)
2253 catch(std::exception &e)
2261 std::vector<fs::DirListNode> list2 = fs::GetDirListing
2263 std::vector<fs::DirListNode>::iterator i2;
2264 for(i2=list2.begin(); i2!=list2.end(); i2++)
2270 loadBlock(sectordir, i2->name, sector, loadlayout != 2);
2272 catch(InvalidFilenameException &e)
2274 // This catches unknown crap in directory
2280 infostream<<"Sector converted to new layout - deleting "<<
2281 sectordir1<<std::endl;
2282 fs::RecursiveDelete(sectordir1);
2289 MapDatabase *ServerMap::createDatabase(
2290 const std::string &name,
2291 const std::string &savedir,
2294 if (name == "sqlite3")
2295 return new MapDatabaseSQLite3(savedir);
2296 if (name == "dummy")
2297 return new Database_Dummy();
2299 else if (name == "leveldb")
2300 return new Database_LevelDB(savedir);
2303 else if (name == "redis")
2304 return new Database_Redis(conf);
2307 else if (name == "postgresql") {
2308 std::string connect_string = "";
2309 conf.getNoEx("pgsql_connection", connect_string);
2310 return new MapDatabasePostgreSQL(connect_string);
2314 throw BaseException(std::string("Database backend ") + name + " not supported.");
2317 void ServerMap::beginSave()
2322 void ServerMap::endSave()
2327 bool ServerMap::saveBlock(MapBlock *block)
2329 return saveBlock(block, dbase);
2332 bool ServerMap::saveBlock(MapBlock *block, MapDatabase *db)
2334 v3s16 p3d = block->getPos();
2336 // Dummy blocks are not written
2337 if (block->isDummy()) {
2338 warningstream << "saveBlock: Not writing dummy block "
2339 << PP(p3d) << std::endl;
2343 // Format used for writing
2344 u8 version = SER_FMT_VER_HIGHEST_WRITE;
2347 [0] u8 serialization version
2350 std::ostringstream o(std::ios_base::binary);
2351 o.write((char*) &version, 1);
2352 block->serialize(o, version, true);
2354 std::string data = o.str();
2355 bool ret = db->saveBlock(p3d, data);
2357 // We just wrote it to the disk so clear modified flag
2358 block->resetModified();
2363 void ServerMap::loadBlock(const std::string §ordir, const std::string &blockfile,
2364 MapSector *sector, bool save_after_load)
2366 DSTACK(FUNCTION_NAME);
2368 std::string fullpath = sectordir + DIR_DELIM + blockfile;
2370 std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2372 throw FileNotGoodException("Cannot open block file");
2374 v3s16 p3d = getBlockPos(sectordir, blockfile);
2375 v2s16 p2d(p3d.X, p3d.Z);
2377 assert(sector->getPos() == p2d);
2379 u8 version = SER_FMT_VER_INVALID;
2380 is.read((char*)&version, 1);
2383 throw SerializationError("ServerMap::loadBlock(): Failed"
2384 " to read MapBlock version");
2386 /*u32 block_size = MapBlock::serializedLength(version);
2387 SharedBuffer<u8> data(block_size);
2388 is.read((char*)*data, block_size);*/
2390 // This will always return a sector because we're the server
2391 //MapSector *sector = emergeSector(p2d);
2393 MapBlock *block = NULL;
2394 bool created_new = false;
2395 block = sector->getBlockNoCreateNoEx(p3d.Y);
2398 block = sector->createBlankBlockNoInsert(p3d.Y);
2403 block->deSerialize(is, version, true);
2405 // If it's a new block, insert it to the map
2407 sector->insertBlock(block);
2408 ReflowScan scanner(this, m_emerge->ndef);
2409 scanner.scan(block, &m_transforming_liquid);
2413 Save blocks loaded in old format in new format
2416 if(version < SER_FMT_VER_HIGHEST_WRITE || save_after_load)
2420 // Should be in database now, so delete the old file
2421 fs::RecursiveDelete(fullpath);
2424 // We just loaded it from the disk, so it's up-to-date.
2425 block->resetModified();
2428 catch(SerializationError &e)
2430 warningstream<<"Invalid block data on disk "
2431 <<"fullpath="<<fullpath
2432 <<" (SerializationError). "
2433 <<"what()="<<e.what()
2435 // Ignoring. A new one will be generated.
2438 // TODO: Backup file; name is in fullpath.
2442 void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load)
2444 DSTACK(FUNCTION_NAME);
2447 std::istringstream is(*blob, std::ios_base::binary);
2449 u8 version = SER_FMT_VER_INVALID;
2450 is.read((char*)&version, 1);
2453 throw SerializationError("ServerMap::loadBlock(): Failed"
2454 " to read MapBlock version");
2456 MapBlock *block = NULL;
2457 bool created_new = false;
2458 block = sector->getBlockNoCreateNoEx(p3d.Y);
2461 block = sector->createBlankBlockNoInsert(p3d.Y);
2466 block->deSerialize(is, version, true);
2468 // If it's a new block, insert it to the map
2470 sector->insertBlock(block);
2471 ReflowScan scanner(this, m_emerge->ndef);
2472 scanner.scan(block, &m_transforming_liquid);
2476 Save blocks loaded in old format in new format
2479 //if(version < SER_FMT_VER_HIGHEST_READ || save_after_load)
2480 // Only save if asked to; no need to update version
2484 // We just loaded it from, so it's up-to-date.
2485 block->resetModified();
2487 catch(SerializationError &e)
2489 errorstream<<"Invalid block data in database"
2490 <<" ("<<p3d.X<<","<<p3d.Y<<","<<p3d.Z<<")"
2491 <<" (SerializationError): "<<e.what()<<std::endl;
2493 // TODO: Block should be marked as invalid in memory so that it is
2494 // not touched but the game can run
2496 if(g_settings->getBool("ignore_world_load_errors")){
2497 errorstream<<"Ignoring block load error. Duck and cover! "
2498 <<"(ignore_world_load_errors)"<<std::endl;
2500 throw SerializationError("Invalid block data in database");
2505 MapBlock* ServerMap::loadBlock(v3s16 blockpos)
2507 DSTACK(FUNCTION_NAME);
2509 bool created_new = (getBlockNoCreateNoEx(blockpos) == NULL);
2511 v2s16 p2d(blockpos.X, blockpos.Z);
2514 dbase->loadBlock(blockpos, &ret);
2516 loadBlock(&ret, blockpos, createSector(p2d), false);
2518 // Not found in database, try the files
2520 // The directory layout we're going to load from.
2521 // 1 - original sectors/xxxxzzzz/
2522 // 2 - new sectors2/xxx/zzz/
2523 // If we load from anything but the latest structure, we will
2524 // immediately save to the new one, and remove the old.
2526 std::string sectordir1 = getSectorDir(p2d, 1);
2527 std::string sectordir;
2528 if (fs::PathExists(sectordir1)) {
2529 sectordir = sectordir1;
2532 sectordir = getSectorDir(p2d, 2);
2536 Make sure sector is loaded
2539 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2540 if (sector == NULL) {
2542 sector = loadSectorMeta(sectordir, loadlayout != 2);
2543 } catch(InvalidFilenameException &e) {
2545 } catch(FileNotGoodException &e) {
2547 } catch(std::exception &e) {
2554 Make sure file exists
2557 std::string blockfilename = getBlockFilename(blockpos);
2558 if (fs::PathExists(sectordir + DIR_DELIM + blockfilename) == false)
2562 Load block and save it to the database
2564 loadBlock(sectordir, blockfilename, sector, true);
2566 MapBlock *block = getBlockNoCreateNoEx(blockpos);
2567 if (created_new && (block != NULL)) {
2568 std::map<v3s16, MapBlock*> modified_blocks;
2569 // Fix lighting if necessary
2570 voxalgo::update_block_border_lighting(this, block, modified_blocks);
2571 if (!modified_blocks.empty()) {
2572 //Modified lighting, send event
2574 event.type = MEET_OTHER;
2575 std::map<v3s16, MapBlock *>::iterator it;
2576 for (it = modified_blocks.begin();
2577 it != modified_blocks.end(); ++it)
2578 event.modified_blocks.insert(it->first);
2579 dispatchEvent(&event);
2585 bool ServerMap::deleteBlock(v3s16 blockpos)
2587 if (!dbase->deleteBlock(blockpos))
2590 MapBlock *block = getBlockNoCreateNoEx(blockpos);
2592 v2s16 p2d(blockpos.X, blockpos.Z);
2593 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2596 sector->deleteBlock(block);
2602 void ServerMap::PrintInfo(std::ostream &out)
2607 bool ServerMap::repairBlockLight(v3s16 blockpos,
2608 std::map<v3s16, MapBlock *> *modified_blocks)
2610 MapBlock *block = emergeBlock(blockpos, false);
2611 if (!block || !block->isGenerated())
2613 voxalgo::repair_block_light(this, block, modified_blocks);
2617 MMVManip::MMVManip(Map *map):
2620 m_create_area(false),
2625 MMVManip::~MMVManip()
2629 void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
2630 bool load_if_inexistent)
2632 TimeTaker timer1("initialEmerge", &emerge_time);
2634 // Units of these are MapBlocks
2635 v3s16 p_min = blockpos_min;
2636 v3s16 p_max = blockpos_max;
2638 VoxelArea block_area_nodes
2639 (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2641 u32 size_MB = block_area_nodes.getVolume()*4/1000000;
2644 infostream<<"initialEmerge: area: ";
2645 block_area_nodes.print(infostream);
2646 infostream<<" ("<<size_MB<<"MB)";
2647 infostream<<std::endl;
2650 addArea(block_area_nodes);
2652 for(s32 z=p_min.Z; z<=p_max.Z; z++)
2653 for(s32 y=p_min.Y; y<=p_max.Y; y++)
2654 for(s32 x=p_min.X; x<=p_max.X; x++)
2659 std::map<v3s16, u8>::iterator n;
2660 n = m_loaded_blocks.find(p);
2661 if(n != m_loaded_blocks.end())
2664 bool block_data_inexistent = false;
2667 TimeTaker timer1("emerge load", &emerge_load_time);
2669 block = m_map->getBlockNoCreate(p);
2670 if(block->isDummy())
2671 block_data_inexistent = true;
2673 block->copyTo(*this);
2675 catch(InvalidPositionException &e)
2677 block_data_inexistent = true;
2680 if(block_data_inexistent)
2683 if (load_if_inexistent && !blockpos_over_max_limit(p)) {
2684 ServerMap *svrmap = (ServerMap *)m_map;
2685 block = svrmap->emergeBlock(p, false);
2687 block = svrmap->createBlock(p);
2688 block->copyTo(*this);
2690 flags |= VMANIP_BLOCK_DATA_INEXIST;
2693 Mark area inexistent
2695 VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2696 // Fill with VOXELFLAG_NO_DATA
2697 for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
2698 for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
2700 s32 i = m_area.index(a.MinEdge.X,y,z);
2701 memset(&m_flags[i], VOXELFLAG_NO_DATA, MAP_BLOCKSIZE);
2705 /*else if (block->getNode(0, 0, 0).getContent() == CONTENT_IGNORE)
2707 // Mark that block was loaded as blank
2708 flags |= VMANIP_BLOCK_CONTAINS_CIGNORE;
2711 m_loaded_blocks[p] = flags;
2717 void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
2718 bool overwrite_generated)
2720 if(m_area.getExtent() == v3s16(0,0,0))
2724 Copy data of all blocks
2726 for(std::map<v3s16, u8>::iterator
2727 i = m_loaded_blocks.begin();
2728 i != m_loaded_blocks.end(); ++i)
2731 MapBlock *block = m_map->getBlockNoCreateNoEx(p);
2732 bool existed = !(i->second & VMANIP_BLOCK_DATA_INEXIST);
2733 if ((existed == false) || (block == NULL) ||
2734 (overwrite_generated == false && block->isGenerated() == true))
2737 block->copyFrom(*this);
2740 (*modified_blocks)[p] = block;