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"
50 #include "database-leveldb.h"
53 #include "database-redis.h"
56 #include "database-postgresql.h"
64 Map::Map(std::ostream &dout, IGameDef *gamedef):
68 m_nodedef(gamedef->ndef()),
69 m_transforming_liquid_loop_count_multiplier(1.0f),
70 m_unprocessed_count(0),
71 m_inc_trending_up_start_time(0),
72 m_queue_size_timer_started(false)
81 for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
82 i != m_sectors.end(); ++i)
88 void Map::addEventReceiver(MapEventReceiver *event_receiver)
90 m_event_receivers.insert(event_receiver);
93 void Map::removeEventReceiver(MapEventReceiver *event_receiver)
95 m_event_receivers.erase(event_receiver);
98 void Map::dispatchEvent(MapEditEvent *event)
100 for(std::set<MapEventReceiver*>::iterator
101 i = m_event_receivers.begin();
102 i != m_event_receivers.end(); ++i)
104 (*i)->onMapEditEvent(event);
108 MapSector * Map::getSectorNoGenerateNoExNoLock(v2s16 p)
110 if(m_sector_cache != NULL && p == m_sector_cache_p){
111 MapSector * sector = m_sector_cache;
115 std::map<v2s16, MapSector*>::iterator n = m_sectors.find(p);
117 if(n == m_sectors.end())
120 MapSector *sector = n->second;
122 // Cache the last result
123 m_sector_cache_p = p;
124 m_sector_cache = sector;
129 MapSector * Map::getSectorNoGenerateNoEx(v2s16 p)
131 return getSectorNoGenerateNoExNoLock(p);
134 MapSector * Map::getSectorNoGenerate(v2s16 p)
136 MapSector *sector = getSectorNoGenerateNoEx(p);
138 throw InvalidPositionException();
143 MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
145 v2s16 p2d(p3d.X, p3d.Z);
146 MapSector * sector = getSectorNoGenerateNoEx(p2d);
149 MapBlock *block = sector->getBlockNoCreateNoEx(p3d.Y);
153 MapBlock * Map::getBlockNoCreate(v3s16 p3d)
155 MapBlock *block = getBlockNoCreateNoEx(p3d);
157 throw InvalidPositionException();
161 bool Map::isNodeUnderground(v3s16 p)
163 v3s16 blockpos = getNodeBlockPos(p);
165 MapBlock * block = getBlockNoCreate(blockpos);
166 return block->getIsUnderground();
168 catch(InvalidPositionException &e)
174 bool Map::isValidPosition(v3s16 p)
176 v3s16 blockpos = getNodeBlockPos(p);
177 MapBlock *block = getBlockNoCreateNoEx(blockpos);
178 return (block != NULL);
181 // Returns a CONTENT_IGNORE node if not found
182 MapNode Map::getNodeNoEx(v3s16 p, bool *is_valid_position)
184 v3s16 blockpos = getNodeBlockPos(p);
185 MapBlock *block = getBlockNoCreateNoEx(blockpos);
187 if (is_valid_position != NULL)
188 *is_valid_position = false;
189 return MapNode(CONTENT_IGNORE);
192 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
194 MapNode node = block->getNodeNoCheck(relpos, &is_valid_p);
195 if (is_valid_position != NULL)
196 *is_valid_position = is_valid_p;
202 // throws InvalidPositionException if not found
203 // TODO: Now this is deprecated, getNodeNoEx should be renamed
204 MapNode Map::getNode(v3s16 p)
206 v3s16 blockpos = getNodeBlockPos(p);
207 MapBlock *block = getBlockNoCreateNoEx(blockpos);
209 throw InvalidPositionException();
210 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
211 bool is_valid_position;
212 MapNode node = block->getNodeNoCheck(relpos, &is_valid_position);
213 if (!is_valid_position)
214 throw InvalidPositionException();
219 // throws InvalidPositionException if not found
220 void Map::setNode(v3s16 p, MapNode & n)
222 v3s16 blockpos = getNodeBlockPos(p);
223 MapBlock *block = getBlockNoCreate(blockpos);
224 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
225 // Never allow placing CONTENT_IGNORE, it fucks up stuff
226 if(n.getContent() == CONTENT_IGNORE){
228 errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE"
229 <<" while trying to replace \""
230 <<m_nodedef->get(block->getNodeNoCheck(relpos, &temp_bool)).name
231 <<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
232 debug_stacks_print_to(infostream);
235 block->setNodeNoCheck(relpos, n);
238 void Map::addNodeAndUpdate(v3s16 p, MapNode n,
239 std::map<v3s16, MapBlock*> &modified_blocks,
240 bool remove_metadata)
242 // Collect old node for rollback
243 RollbackNode rollback_oldnode(this, p, m_gamedef);
245 // This is needed for updating the lighting
246 MapNode oldnode = getNodeNoEx(p);
248 // Remove node metadata
249 if (remove_metadata) {
250 removeNodeMetadata(p);
253 // Set the node on the map
254 // Ignore light (because calling voxalgo::update_lighting_nodes)
255 n.setLight(LIGHTBANK_DAY, 0, m_nodedef);
256 n.setLight(LIGHTBANK_NIGHT, 0, m_nodedef);
260 std::vector<std::pair<v3s16, MapNode> > oldnodes;
261 oldnodes.push_back(std::pair<v3s16, MapNode>(p, oldnode));
262 voxalgo::update_lighting_nodes(this, oldnodes, modified_blocks);
264 for(std::map<v3s16, MapBlock*>::iterator
265 i = modified_blocks.begin();
266 i != modified_blocks.end(); ++i)
268 i->second->expireDayNightDiff();
271 // Report for rollback
272 if(m_gamedef->rollback())
274 RollbackNode rollback_newnode(this, p, m_gamedef);
275 RollbackAction action;
276 action.setSetNode(p, rollback_oldnode, rollback_newnode);
277 m_gamedef->rollback()->reportAction(action);
281 Add neighboring liquid nodes and this node to transform queue.
282 (it's vital for the node itself to get updated last, if it was removed.)
285 v3s16(0,0,1), // back
287 v3s16(1,0,0), // right
288 v3s16(0,0,-1), // front
289 v3s16(0,-1,0), // bottom
290 v3s16(-1,0,0), // left
291 v3s16(0,0,0), // self
293 for(u16 i=0; i<7; i++)
295 v3s16 p2 = p + dirs[i];
297 bool is_valid_position;
298 MapNode n2 = getNodeNoEx(p2, &is_valid_position);
299 if(is_valid_position &&
300 (m_nodedef->get(n2).isLiquid() ||
301 n2.getContent() == CONTENT_AIR))
302 m_transforming_liquid.push_back(p2);
306 void Map::removeNodeAndUpdate(v3s16 p,
307 std::map<v3s16, MapBlock*> &modified_blocks)
309 addNodeAndUpdate(p, MapNode(CONTENT_AIR), modified_blocks, true);
312 bool Map::addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata)
315 event.type = remove_metadata ? MEET_ADDNODE : MEET_SWAPNODE;
319 bool succeeded = true;
321 std::map<v3s16, MapBlock*> modified_blocks;
322 addNodeAndUpdate(p, n, modified_blocks, remove_metadata);
324 // Copy modified_blocks to event
325 for(std::map<v3s16, MapBlock*>::iterator
326 i = modified_blocks.begin();
327 i != modified_blocks.end(); ++i)
329 event.modified_blocks.insert(i->first);
332 catch(InvalidPositionException &e){
336 dispatchEvent(&event);
341 bool Map::removeNodeWithEvent(v3s16 p)
344 event.type = MEET_REMOVENODE;
347 bool succeeded = true;
349 std::map<v3s16, MapBlock*> modified_blocks;
350 removeNodeAndUpdate(p, modified_blocks);
352 // Copy modified_blocks to event
353 for(std::map<v3s16, MapBlock*>::iterator
354 i = modified_blocks.begin();
355 i != modified_blocks.end(); ++i)
357 event.modified_blocks.insert(i->first);
360 catch(InvalidPositionException &e){
364 dispatchEvent(&event);
369 bool Map::getDayNightDiff(v3s16 blockpos)
372 v3s16 p = blockpos + v3s16(0,0,0);
373 MapBlock *b = getBlockNoCreate(p);
374 if(b->getDayNightDiff())
377 catch(InvalidPositionException &e){}
380 v3s16 p = blockpos + v3s16(-1,0,0);
381 MapBlock *b = getBlockNoCreate(p);
382 if(b->getDayNightDiff())
385 catch(InvalidPositionException &e){}
387 v3s16 p = blockpos + v3s16(0,-1,0);
388 MapBlock *b = getBlockNoCreate(p);
389 if(b->getDayNightDiff())
392 catch(InvalidPositionException &e){}
394 v3s16 p = blockpos + v3s16(0,0,-1);
395 MapBlock *b = getBlockNoCreate(p);
396 if(b->getDayNightDiff())
399 catch(InvalidPositionException &e){}
402 v3s16 p = blockpos + v3s16(1,0,0);
403 MapBlock *b = getBlockNoCreate(p);
404 if(b->getDayNightDiff())
407 catch(InvalidPositionException &e){}
409 v3s16 p = blockpos + v3s16(0,1,0);
410 MapBlock *b = getBlockNoCreate(p);
411 if(b->getDayNightDiff())
414 catch(InvalidPositionException &e){}
416 v3s16 p = blockpos + v3s16(0,0,1);
417 MapBlock *b = getBlockNoCreate(p);
418 if(b->getDayNightDiff())
421 catch(InvalidPositionException &e){}
426 struct TimeOrderedMapBlock {
430 TimeOrderedMapBlock(MapSector *sect, MapBlock *block) :
435 bool operator<(const TimeOrderedMapBlock &b) const
437 return block->getUsageTimer() < b.block->getUsageTimer();
444 void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
445 std::vector<v3s16> *unloaded_blocks)
447 bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
449 // Profile modified reasons
450 Profiler modprofiler;
452 std::vector<v2s16> sector_deletion_queue;
453 u32 deleted_blocks_count = 0;
454 u32 saved_blocks_count = 0;
455 u32 block_count_all = 0;
459 // If there is no practical limit, we spare creation of mapblock_queue
460 if (max_loaded_blocks == U32_MAX) {
461 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
462 si != m_sectors.end(); ++si) {
463 MapSector *sector = si->second;
465 bool all_blocks_deleted = true;
468 sector->getBlocks(blocks);
470 for (MapBlockVect::iterator i = blocks.begin();
471 i != blocks.end(); ++i) {
472 MapBlock *block = (*i);
474 block->incrementUsageTimer(dtime);
476 if (block->refGet() == 0
477 && block->getUsageTimer() > unload_timeout) {
478 v3s16 p = block->getPos();
481 if (block->getModified() != MOD_STATE_CLEAN
482 && save_before_unloading) {
483 modprofiler.add(block->getModifiedReasonString(), 1);
484 if (!saveBlock(block))
486 saved_blocks_count++;
489 // Delete from memory
490 sector->deleteBlock(block);
493 unloaded_blocks->push_back(p);
495 deleted_blocks_count++;
497 all_blocks_deleted = false;
502 if (all_blocks_deleted) {
503 sector_deletion_queue.push_back(si->first);
507 std::priority_queue<TimeOrderedMapBlock> mapblock_queue;
508 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
509 si != m_sectors.end(); ++si) {
510 MapSector *sector = si->second;
513 sector->getBlocks(blocks);
515 for(MapBlockVect::iterator i = blocks.begin();
516 i != blocks.end(); ++i) {
517 MapBlock *block = (*i);
519 block->incrementUsageTimer(dtime);
520 mapblock_queue.push(TimeOrderedMapBlock(sector, block));
523 block_count_all = mapblock_queue.size();
524 // Delete old blocks, and blocks over the limit from the memory
525 while (!mapblock_queue.empty() && (mapblock_queue.size() > max_loaded_blocks
526 || mapblock_queue.top().block->getUsageTimer() > unload_timeout)) {
527 TimeOrderedMapBlock b = mapblock_queue.top();
528 mapblock_queue.pop();
530 MapBlock *block = b.block;
532 if (block->refGet() != 0)
535 v3s16 p = block->getPos();
538 if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
539 modprofiler.add(block->getModifiedReasonString(), 1);
540 if (!saveBlock(block))
542 saved_blocks_count++;
545 // Delete from memory
546 b.sect->deleteBlock(block);
549 unloaded_blocks->push_back(p);
551 deleted_blocks_count++;
554 // Delete empty sectors
555 for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
556 si != m_sectors.end(); ++si) {
557 if (si->second->empty()) {
558 sector_deletion_queue.push_back(si->first);
564 // Finally delete the empty sectors
565 deleteSectors(sector_deletion_queue);
567 if(deleted_blocks_count != 0)
569 PrintInfo(infostream); // ServerMap/ClientMap:
570 infostream<<"Unloaded "<<deleted_blocks_count
571 <<" blocks from memory";
572 if(save_before_unloading)
573 infostream<<", of which "<<saved_blocks_count<<" were written";
574 infostream<<", "<<block_count_all<<" blocks in memory";
575 infostream<<"."<<std::endl;
576 if(saved_blocks_count != 0){
577 PrintInfo(infostream); // ServerMap/ClientMap:
578 infostream<<"Blocks modified by: "<<std::endl;
579 modprofiler.print(infostream);
584 void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks)
586 timerUpdate(0.0, -1.0, 0, unloaded_blocks);
589 void Map::deleteSectors(std::vector<v2s16> §orList)
591 for(std::vector<v2s16>::iterator j = sectorList.begin();
592 j != sectorList.end(); ++j) {
593 MapSector *sector = m_sectors[*j];
594 // If sector is in sector cache, remove it from there
595 if(m_sector_cache == sector)
596 m_sector_cache = NULL;
597 // Remove from map and delete
603 void Map::PrintInfo(std::ostream &out)
608 #define WATER_DROP_BOOST 4
615 struct NodeNeighbor {
625 NodeNeighbor(const MapNode &node, NeighborType n_type, v3s16 pos)
632 void Map::transforming_liquid_add(v3s16 p) {
633 m_transforming_liquid.push_back(p);
636 s32 Map::transforming_liquid_size() {
637 return m_transforming_liquid.size();
640 void Map::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks)
642 DSTACK(FUNCTION_NAME);
643 //TimeTaker timer("transformLiquids()");
646 u32 initial_size = m_transforming_liquid.size();
648 /*if(initial_size != 0)
649 infostream<<"transformLiquids(): initial_size="<<initial_size<<std::endl;*/
651 // list of nodes that due to viscosity have not reached their max level height
652 std::deque<v3s16> must_reflow;
654 std::vector<std::pair<v3s16, MapNode> > changed_nodes;
656 u32 liquid_loop_max = g_settings->getS32("liquid_loop_max");
657 u32 loop_max = liquid_loop_max;
661 /* If liquid_loop_max is not keeping up with the queue size increase
662 * loop_max up to a maximum of liquid_loop_max * dedicated_server_step.
664 if (m_transforming_liquid.size() > loop_max * 2) {
666 float server_step = g_settings->getFloat("dedicated_server_step");
667 if (m_transforming_liquid_loop_count_multiplier - 1.0 < server_step)
668 m_transforming_liquid_loop_count_multiplier *= 1.0 + server_step / 10;
670 m_transforming_liquid_loop_count_multiplier = 1.0;
673 loop_max *= m_transforming_liquid_loop_count_multiplier;
676 while (m_transforming_liquid.size() != 0)
678 // This should be done here so that it is done when continue is used
679 if (loopcount >= initial_size || loopcount >= loop_max)
684 Get a queued transforming liquid node
686 v3s16 p0 = m_transforming_liquid.front();
687 m_transforming_liquid.pop_front();
689 MapNode n0 = getNodeNoEx(p0);
692 Collect information about current node
694 s8 liquid_level = -1;
695 // The liquid node which will be placed there if
696 // the liquid flows into this node.
697 content_t liquid_kind = CONTENT_IGNORE;
698 // The node which will be placed there if liquid
699 // can't flow into this node.
700 content_t floodable_node = CONTENT_AIR;
701 const ContentFeatures &cf = m_nodedef->get(n0);
702 LiquidType liquid_type = cf.liquid_type;
703 switch (liquid_type) {
705 liquid_level = LIQUID_LEVEL_SOURCE;
706 liquid_kind = m_nodedef->getId(cf.liquid_alternative_flowing);
709 liquid_level = (n0.param2 & LIQUID_LEVEL_MASK);
710 liquid_kind = n0.getContent();
713 // if this node is 'floodable', it *could* be transformed
714 // into a liquid, otherwise, continue with the next node.
717 floodable_node = n0.getContent();
718 liquid_kind = CONTENT_AIR;
723 Collect information about the environment
725 const v3s16 *dirs = g_6dirs;
726 NodeNeighbor sources[6]; // surrounding sources
728 NodeNeighbor flows[6]; // surrounding flowing liquid nodes
730 NodeNeighbor airs[6]; // surrounding air
732 NodeNeighbor neutrals[6]; // nodes that are solid or another kind of liquid
733 int num_neutrals = 0;
734 bool flowing_down = false;
735 bool ignored_sources = false;
736 for (u16 i = 0; i < 6; i++) {
737 NeighborType nt = NEIGHBOR_SAME_LEVEL;
746 v3s16 npos = p0 + dirs[i];
747 NodeNeighbor nb(getNodeNoEx(npos), nt, npos);
748 const ContentFeatures &cfnb = m_nodedef->get(nb.n);
749 switch (m_nodedef->get(nb.n.getContent()).liquid_type) {
751 if (cfnb.floodable) {
752 airs[num_airs++] = nb;
753 // if the current node is a water source the neighbor
754 // should be enqueded for transformation regardless of whether the
755 // current node changes or not.
756 if (nb.t != NEIGHBOR_UPPER && liquid_type != LIQUID_NONE)
757 m_transforming_liquid.push_back(npos);
758 // if the current node happens to be a flowing node, it will start to flow down here.
759 if (nb.t == NEIGHBOR_LOWER)
762 neutrals[num_neutrals++] = nb;
763 if (nb.n.getContent() == CONTENT_IGNORE) {
764 // If node below is ignore prevent water from
765 // spreading outwards and otherwise prevent from
766 // flowing away as ignore node might be the source
767 if (nb.t == NEIGHBOR_LOWER)
770 ignored_sources = true;
775 // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
776 if (liquid_kind == CONTENT_AIR)
777 liquid_kind = m_nodedef->getId(cfnb.liquid_alternative_flowing);
778 if (m_nodedef->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
779 neutrals[num_neutrals++] = nb;
781 // Do not count bottom source, it will screw things up
783 sources[num_sources++] = nb;
787 // if this node is not (yet) of a liquid type, choose the first liquid type we encounter
788 if (liquid_kind == CONTENT_AIR)
789 liquid_kind = m_nodedef->getId(cfnb.liquid_alternative_flowing);
790 if (m_nodedef->getId(cfnb.liquid_alternative_flowing) != liquid_kind) {
791 neutrals[num_neutrals++] = nb;
793 flows[num_flows++] = nb;
794 if (nb.t == NEIGHBOR_LOWER)
802 decide on the type (and possibly level) of the current node
804 content_t new_node_content;
805 s8 new_node_level = -1;
806 s8 max_node_level = -1;
808 u8 range = m_nodedef->get(liquid_kind).liquid_range;
809 if (range > LIQUID_LEVEL_MAX + 1)
810 range = LIQUID_LEVEL_MAX + 1;
812 if ((num_sources >= 2 && m_nodedef->get(liquid_kind).liquid_renewable) || liquid_type == LIQUID_SOURCE) {
813 // liquid_kind will be set to either the flowing alternative of the node (if it's a liquid)
814 // or the flowing alternative of the first of the surrounding sources (if it's air), so
815 // it's perfectly safe to use liquid_kind here to determine the new node content.
816 new_node_content = m_nodedef->getId(m_nodedef->get(liquid_kind).liquid_alternative_source);
817 } else if (num_sources >= 1 && sources[0].t != NEIGHBOR_LOWER) {
818 // liquid_kind is set properly, see above
819 max_node_level = new_node_level = LIQUID_LEVEL_MAX;
820 if (new_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
821 new_node_content = liquid_kind;
823 new_node_content = floodable_node;
824 } else if (ignored_sources && liquid_level >= 0) {
825 // Maybe there are neighbouring sources that aren't loaded yet
826 // so prevent flowing away.
827 new_node_level = liquid_level;
828 new_node_content = liquid_kind;
830 // no surrounding sources, so get the maximum level that can flow into this node
831 for (u16 i = 0; i < num_flows; i++) {
832 u8 nb_liquid_level = (flows[i].n.param2 & LIQUID_LEVEL_MASK);
833 switch (flows[i].t) {
835 if (nb_liquid_level + WATER_DROP_BOOST > max_node_level) {
836 max_node_level = LIQUID_LEVEL_MAX;
837 if (nb_liquid_level + WATER_DROP_BOOST < LIQUID_LEVEL_MAX)
838 max_node_level = nb_liquid_level + WATER_DROP_BOOST;
839 } else if (nb_liquid_level > max_node_level) {
840 max_node_level = nb_liquid_level;
845 case NEIGHBOR_SAME_LEVEL:
846 if ((flows[i].n.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK &&
847 nb_liquid_level > 0 && nb_liquid_level - 1 > max_node_level)
848 max_node_level = nb_liquid_level - 1;
853 u8 viscosity = m_nodedef->get(liquid_kind).liquid_viscosity;
854 if (viscosity > 1 && max_node_level != liquid_level) {
855 // amount to gain, limited by viscosity
856 // must be at least 1 in absolute value
857 s8 level_inc = max_node_level - liquid_level;
858 if (level_inc < -viscosity || level_inc > viscosity)
859 new_node_level = liquid_level + level_inc/viscosity;
860 else if (level_inc < 0)
861 new_node_level = liquid_level - 1;
862 else if (level_inc > 0)
863 new_node_level = liquid_level + 1;
864 if (new_node_level != max_node_level)
865 must_reflow.push_back(p0);
867 new_node_level = max_node_level;
870 if (max_node_level >= (LIQUID_LEVEL_MAX + 1 - range))
871 new_node_content = liquid_kind;
873 new_node_content = floodable_node;
878 check if anything has changed. if not, just continue with the next node.
880 if (new_node_content == n0.getContent() &&
881 (m_nodedef->get(n0.getContent()).liquid_type != LIQUID_FLOWING ||
882 ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level &&
883 ((n0.param2 & LIQUID_FLOW_DOWN_MASK) == LIQUID_FLOW_DOWN_MASK)
889 update the current node
892 //bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK));
893 if (m_nodedef->get(new_node_content).liquid_type == LIQUID_FLOWING) {
894 // set level to last 3 bits, flowing down bit to 4th bit
895 n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK);
897 // set the liquid level and flow bit to 0
898 n0.param2 = ~(LIQUID_LEVEL_MASK | LIQUID_FLOW_DOWN_MASK);
900 n0.setContent(new_node_content);
902 // Ignore light (because calling voxalgo::update_lighting_nodes)
903 n0.setLight(LIGHTBANK_DAY, 0, m_nodedef);
904 n0.setLight(LIGHTBANK_NIGHT, 0, m_nodedef);
906 // Find out whether there is a suspect for this action
908 if (m_gamedef->rollback())
909 suspect = m_gamedef->rollback()->getSuspect(p0, 83, 1);
911 if (m_gamedef->rollback() && !suspect.empty()) {
913 RollbackScopeActor rollback_scope(m_gamedef->rollback(), suspect, true);
914 // Get old node for rollback
915 RollbackNode rollback_oldnode(this, p0, m_gamedef);
919 RollbackNode rollback_newnode(this, p0, m_gamedef);
920 RollbackAction action;
921 action.setSetNode(p0, rollback_oldnode, rollback_newnode);
922 m_gamedef->rollback()->reportAction(action);
928 v3s16 blockpos = getNodeBlockPos(p0);
929 MapBlock *block = getBlockNoCreateNoEx(blockpos);
931 modified_blocks[blockpos] = block;
932 changed_nodes.push_back(std::pair<v3s16, MapNode>(p0, n00));
936 enqueue neighbors for update if neccessary
938 switch (m_nodedef->get(n0.getContent()).liquid_type) {
941 // make sure source flows into all neighboring nodes
942 for (u16 i = 0; i < num_flows; i++)
943 if (flows[i].t != NEIGHBOR_UPPER)
944 m_transforming_liquid.push_back(flows[i].p);
945 for (u16 i = 0; i < num_airs; i++)
946 if (airs[i].t != NEIGHBOR_UPPER)
947 m_transforming_liquid.push_back(airs[i].p);
950 // this flow has turned to air; neighboring flows might need to do the same
951 for (u16 i = 0; i < num_flows; i++)
952 m_transforming_liquid.push_back(flows[i].p);
956 //infostream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
958 for (std::deque<v3s16>::iterator iter = must_reflow.begin(); iter != must_reflow.end(); ++iter)
959 m_transforming_liquid.push_back(*iter);
961 voxalgo::update_lighting_nodes(this, changed_nodes, modified_blocks);
964 /* ----------------------------------------------------------------------
965 * Manage the queue so that it does not grow indefinately
967 u16 time_until_purge = g_settings->getU16("liquid_queue_purge_time");
969 if (time_until_purge == 0)
970 return; // Feature disabled
972 time_until_purge *= 1000; // seconds -> milliseconds
974 u32 curr_time = getTime(PRECISION_MILLI);
975 u32 prev_unprocessed = m_unprocessed_count;
976 m_unprocessed_count = m_transforming_liquid.size();
978 // if unprocessed block count is decreasing or stable
979 if (m_unprocessed_count <= prev_unprocessed) {
980 m_queue_size_timer_started = false;
982 if (!m_queue_size_timer_started)
983 m_inc_trending_up_start_time = curr_time;
984 m_queue_size_timer_started = true;
987 // Account for curr_time overflowing
988 if (m_queue_size_timer_started && m_inc_trending_up_start_time > curr_time)
989 m_queue_size_timer_started = false;
991 /* If the queue has been growing for more than liquid_queue_purge_time seconds
992 * and the number of unprocessed blocks is still > liquid_loop_max then we
993 * cannot keep up; dump the oldest blocks from the queue so that the queue
994 * has liquid_loop_max items in it
996 if (m_queue_size_timer_started
997 && curr_time - m_inc_trending_up_start_time > time_until_purge
998 && m_unprocessed_count > liquid_loop_max) {
1000 size_t dump_qty = m_unprocessed_count - liquid_loop_max;
1002 infostream << "transformLiquids(): DUMPING " << dump_qty
1003 << " blocks from the queue" << std::endl;
1006 m_transforming_liquid.pop_front();
1008 m_queue_size_timer_started = false; // optimistically assume we can keep up now
1009 m_unprocessed_count = m_transforming_liquid.size();
1013 std::vector<v3s16> Map::findNodesWithMetadata(v3s16 p1, v3s16 p2)
1015 std::vector<v3s16> positions_with_meta;
1017 sortBoxVerticies(p1, p2);
1018 v3s16 bpmin = getNodeBlockPos(p1);
1019 v3s16 bpmax = getNodeBlockPos(p2);
1021 VoxelArea area(p1, p2);
1023 for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
1024 for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
1025 for (s16 x = bpmin.X; x <= bpmax.X; x++) {
1026 v3s16 blockpos(x, y, z);
1028 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1030 verbosestream << "Map::getNodeMetadata(): Need to emerge "
1031 << PP(blockpos) << std::endl;
1032 block = emergeBlock(blockpos, false);
1035 infostream << "WARNING: Map::getNodeMetadata(): Block not found"
1040 v3s16 p_base = blockpos * MAP_BLOCKSIZE;
1041 std::vector<v3s16> keys = block->m_node_metadata.getAllKeys();
1042 for (size_t i = 0; i != keys.size(); i++) {
1043 v3s16 p(keys[i] + p_base);
1044 if (!area.contains(p))
1047 positions_with_meta.push_back(p);
1051 return positions_with_meta;
1054 NodeMetadata *Map::getNodeMetadata(v3s16 p)
1056 v3s16 blockpos = getNodeBlockPos(p);
1057 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1058 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1060 infostream<<"Map::getNodeMetadata(): Need to emerge "
1061 <<PP(blockpos)<<std::endl;
1062 block = emergeBlock(blockpos, false);
1065 warningstream<<"Map::getNodeMetadata(): Block not found"
1069 NodeMetadata *meta = block->m_node_metadata.get(p_rel);
1073 bool Map::setNodeMetadata(v3s16 p, NodeMetadata *meta)
1075 v3s16 blockpos = getNodeBlockPos(p);
1076 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1077 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1079 infostream<<"Map::setNodeMetadata(): Need to emerge "
1080 <<PP(blockpos)<<std::endl;
1081 block = emergeBlock(blockpos, false);
1084 warningstream<<"Map::setNodeMetadata(): Block not found"
1088 block->m_node_metadata.set(p_rel, meta);
1092 void Map::removeNodeMetadata(v3s16 p)
1094 v3s16 blockpos = getNodeBlockPos(p);
1095 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1096 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1099 warningstream<<"Map::removeNodeMetadata(): Block not found"
1103 block->m_node_metadata.remove(p_rel);
1106 NodeTimer Map::getNodeTimer(v3s16 p)
1108 v3s16 blockpos = getNodeBlockPos(p);
1109 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1110 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1112 infostream<<"Map::getNodeTimer(): Need to emerge "
1113 <<PP(blockpos)<<std::endl;
1114 block = emergeBlock(blockpos, false);
1117 warningstream<<"Map::getNodeTimer(): Block not found"
1121 NodeTimer t = block->m_node_timers.get(p_rel);
1122 NodeTimer nt(t.timeout, t.elapsed, p);
1126 void Map::setNodeTimer(const NodeTimer &t)
1128 v3s16 p = t.position;
1129 v3s16 blockpos = getNodeBlockPos(p);
1130 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1131 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1133 infostream<<"Map::setNodeTimer(): Need to emerge "
1134 <<PP(blockpos)<<std::endl;
1135 block = emergeBlock(blockpos, false);
1138 warningstream<<"Map::setNodeTimer(): Block not found"
1142 NodeTimer nt(t.timeout, t.elapsed, p_rel);
1143 block->m_node_timers.set(nt);
1146 void Map::removeNodeTimer(v3s16 p)
1148 v3s16 blockpos = getNodeBlockPos(p);
1149 v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE;
1150 MapBlock *block = getBlockNoCreateNoEx(blockpos);
1153 warningstream<<"Map::removeNodeTimer(): Block not found"
1157 block->m_node_timers.remove(p_rel);
1163 ServerMap::ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge):
1164 Map(dout_server, gamedef),
1165 settings_mgr(g_settings, savedir + DIR_DELIM + "map_meta.txt"),
1167 m_map_metadata_changed(true)
1169 verbosestream<<FUNCTION_NAME<<std::endl;
1171 // Tell the EmergeManager about our MapSettingsManager
1172 emerge->map_settings_mgr = &settings_mgr;
1175 Try to load map; if not found, create a new one.
1178 // Determine which database backend to use
1179 std::string conf_path = savedir + DIR_DELIM + "world.mt";
1181 bool succeeded = conf.readConfigFile(conf_path.c_str());
1182 if (!succeeded || !conf.exists("backend")) {
1183 // fall back to sqlite3
1184 conf.set("backend", "sqlite3");
1186 std::string backend = conf.get("backend");
1187 dbase = createDatabase(backend, savedir, conf);
1189 if (!conf.updateConfigFile(conf_path.c_str()))
1190 errorstream << "ServerMap::ServerMap(): Failed to update world.mt!" << std::endl;
1192 m_savedir = savedir;
1193 m_map_saving_enabled = false;
1197 // If directory exists, check contents and load if possible
1198 if(fs::PathExists(m_savedir))
1200 // If directory is empty, it is safe to save into it.
1201 if(fs::GetDirListing(m_savedir).size() == 0)
1203 infostream<<"ServerMap: Empty save directory is valid."
1205 m_map_saving_enabled = true;
1210 if (settings_mgr.loadMapMeta()) {
1211 infostream << "ServerMap: Metadata loaded from "
1212 << savedir << std::endl;
1214 infostream << "ServerMap: Metadata could not be loaded "
1215 "from " << savedir << ", assuming valid save "
1216 "directory." << std::endl;
1219 m_map_saving_enabled = true;
1220 // Map loaded, not creating new one
1224 // If directory doesn't exist, it is safe to save to it
1226 m_map_saving_enabled = true;
1229 catch(std::exception &e)
1231 warningstream<<"ServerMap: Failed to load map from "<<savedir
1232 <<", exception: "<<e.what()<<std::endl;
1233 infostream<<"Please remove the map or fix it."<<std::endl;
1234 warningstream<<"Map saving will be disabled."<<std::endl;
1237 infostream<<"Initializing new map."<<std::endl;
1239 // Create zero sector
1240 emergeSector(v2s16(0,0));
1242 // Initially write whole map
1243 save(MOD_STATE_CLEAN);
1246 ServerMap::~ServerMap()
1248 verbosestream<<FUNCTION_NAME<<std::endl;
1252 if(m_map_saving_enabled)
1254 // Save only changed parts
1255 save(MOD_STATE_WRITE_AT_UNLOAD);
1256 infostream<<"ServerMap: Saved map to "<<m_savedir<<std::endl;
1260 infostream<<"ServerMap: Map not saved"<<std::endl;
1263 catch(std::exception &e)
1265 infostream<<"ServerMap: Failed to save map to "<<m_savedir
1266 <<", exception: "<<e.what()<<std::endl;
1270 Close database if it was opened
1278 core::map<v2s16, MapChunk*>::Iterator i = m_chunks.getIterator();
1279 for(; i.atEnd() == false; i++)
1281 MapChunk *chunk = i.getNode()->getValue();
1287 MapgenParams *ServerMap::getMapgenParams()
1289 // getMapgenParams() should only ever be called after Server is initialized
1290 assert(settings_mgr.mapgen_params != NULL);
1291 return settings_mgr.mapgen_params;
1294 u64 ServerMap::getSeed()
1296 return getMapgenParams()->seed;
1299 s16 ServerMap::getWaterLevel()
1301 return getMapgenParams()->water_level;
1304 bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data)
1306 s16 csize = getMapgenParams()->chunksize;
1307 v3s16 bpmin = EmergeManager::getContainingChunk(blockpos, csize);
1308 v3s16 bpmax = bpmin + v3s16(1, 1, 1) * (csize - 1);
1310 bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1311 EMERGE_DBG_OUT("initBlockMake(): " PP(bpmin) " - " PP(bpmax));
1313 v3s16 extra_borders(1, 1, 1);
1314 v3s16 full_bpmin = bpmin - extra_borders;
1315 v3s16 full_bpmax = bpmax + extra_borders;
1317 // Do nothing if not inside limits (+-1 because of neighbors)
1318 if (blockpos_over_limit(full_bpmin) ||
1319 blockpos_over_limit(full_bpmax))
1322 data->seed = getSeed();
1323 data->blockpos_min = bpmin;
1324 data->blockpos_max = bpmax;
1325 data->blockpos_requested = blockpos;
1326 data->nodedef = m_nodedef;
1329 Create the whole area of this and the neighboring blocks
1331 for (s16 x = full_bpmin.X; x <= full_bpmax.X; x++)
1332 for (s16 z = full_bpmin.Z; z <= full_bpmax.Z; z++) {
1333 v2s16 sectorpos(x, z);
1334 // Sector metadata is loaded from disk if not already loaded.
1335 ServerMapSector *sector = createSector(sectorpos);
1336 FATAL_ERROR_IF(sector == NULL, "createSector() failed");
1338 for (s16 y = full_bpmin.Y; y <= full_bpmax.Y; y++) {
1341 MapBlock *block = emergeBlock(p, false);
1342 if (block == NULL) {
1343 block = createBlock(p);
1345 // Block gets sunlight if this is true.
1346 // Refer to the map generator heuristics.
1347 bool ug = m_emerge->isBlockUnderground(p);
1348 block->setIsUnderground(ug);
1354 Now we have a big empty area.
1356 Make a ManualMapVoxelManipulator that contains this and the
1360 data->vmanip = new MMVManip(this);
1361 data->vmanip->initialEmerge(full_bpmin, full_bpmax);
1363 // Note: we may need this again at some point.
1365 // Ensure none of the blocks to be generated were marked as
1366 // containing CONTENT_IGNORE
1367 for (s16 z = blockpos_min.Z; z <= blockpos_max.Z; z++) {
1368 for (s16 y = blockpos_min.Y; y <= blockpos_max.Y; y++) {
1369 for (s16 x = blockpos_min.X; x <= blockpos_max.X; x++) {
1370 core::map<v3s16, u8>::Node *n;
1371 n = data->vmanip->m_loaded_blocks.find(v3s16(x, y, z));
1374 u8 flags = n->getValue();
1375 flags &= ~VMANIP_BLOCK_CONTAINS_CIGNORE;
1382 // Data is ready now.
1386 void ServerMap::finishBlockMake(BlockMakeData *data,
1387 std::map<v3s16, MapBlock*> *changed_blocks)
1389 v3s16 bpmin = data->blockpos_min;
1390 v3s16 bpmax = data->blockpos_max;
1392 v3s16 extra_borders(1, 1, 1);
1394 bool enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
1395 EMERGE_DBG_OUT("finishBlockMake(): " PP(bpmin) " - " PP(bpmax));
1398 Blit generated stuff to map
1399 NOTE: blitBackAll adds nearly everything to changed_blocks
1401 data->vmanip->blitBackAll(changed_blocks);
1403 EMERGE_DBG_OUT("finishBlockMake: changed_blocks.size()="
1404 << changed_blocks->size());
1407 Copy transforming liquid information
1409 while (data->transforming_liquid.size()) {
1410 m_transforming_liquid.push_back(data->transforming_liquid.front());
1411 data->transforming_liquid.pop_front();
1414 for (std::map<v3s16, MapBlock *>::iterator
1415 it = changed_blocks->begin();
1416 it != changed_blocks->end(); ++it) {
1417 MapBlock *block = it->second;
1421 Update day/night difference cache of the MapBlocks
1423 block->expireDayNightDiff();
1425 Set block as modified
1427 block->raiseModified(MOD_STATE_WRITE_NEEDED,
1428 MOD_REASON_EXPIRE_DAYNIGHTDIFF);
1432 Set central blocks as generated
1434 for (s16 x = bpmin.X; x <= bpmax.X; x++)
1435 for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
1436 for (s16 y = bpmin.Y; y <= bpmax.Y; y++) {
1437 MapBlock *block = getBlockNoCreateNoEx(v3s16(x, y, z));
1441 block->setGenerated(true);
1445 Save changed parts of map
1446 NOTE: Will be saved later.
1448 //save(MOD_STATE_WRITE_AT_UNLOAD);
1451 ServerMapSector *ServerMap::createSector(v2s16 p2d)
1453 DSTACKF("%s: p2d=(%d,%d)",
1458 Check if it exists already in memory
1460 ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1465 Try to load it from disk (with blocks)
1467 //if(loadSectorFull(p2d) == true)
1470 Try to load metadata from disk
1473 if(loadSectorMeta(p2d) == true)
1475 ServerMapSector *sector = (ServerMapSector*)getSectorNoGenerateNoEx(p2d);
1478 infostream<<"ServerMap::createSector(): loadSectorFull didn't make a sector"<<std::endl;
1479 throw InvalidPositionException("");
1486 Do not create over-limit.
1487 We are checking for any nodes of the mapblocks of the sector being beyond the limit.
1488 A sector is a vertical column of mapblocks, so sectorpos is like a 2D blockpos.
1490 At the negative limit we are checking for
1491 block minimum nodepos < -mapgenlimit.
1492 At the positive limit we are checking for
1493 block maximum nodepos > mapgenlimit.
1495 Block minimum nodepos = blockpos * mapblocksize.
1496 Block maximum nodepos = (blockpos + 1) * mapblocksize - 1.
1498 const u16 map_gen_limit = MYMIN(MAX_MAP_GENERATION_LIMIT,
1499 g_settings->getU16("map_generation_limit"));
1500 if (p2d.X * MAP_BLOCKSIZE < -map_gen_limit
1501 || (p2d.X + 1) * MAP_BLOCKSIZE - 1 > map_gen_limit
1502 || p2d.Y * MAP_BLOCKSIZE < -map_gen_limit
1503 || (p2d.Y + 1) * MAP_BLOCKSIZE - 1 > map_gen_limit)
1504 throw InvalidPositionException("createSector(): pos. over limit");
1507 Generate blank sector
1510 sector = new ServerMapSector(this, p2d, m_gamedef);
1512 // Sector position on map in nodes
1513 //v2s16 nodepos2d = p2d * MAP_BLOCKSIZE;
1518 m_sectors[p2d] = sector;
1525 This is a quick-hand function for calling makeBlock().
1527 MapBlock * ServerMap::generateBlock(
1529 std::map<v3s16, MapBlock*> &modified_blocks
1532 DSTACKF("%s: p=(%d,%d,%d)", FUNCTION_NAME, p.X, p.Y, p.Z);
1534 /*infostream<<"generateBlock(): "
1535 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1538 bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
1540 TimeTaker timer("generateBlock");
1542 //MapBlock *block = original_dummy;
1544 v2s16 p2d(p.X, p.Z);
1545 v2s16 p2d_nodes = p2d * MAP_BLOCKSIZE;
1548 Do not generate over-limit
1550 if(blockpos_over_limit(p))
1552 infostream<<FUNCTION_NAME<<": Block position over limit"<<std::endl;
1553 throw InvalidPositionException("generateBlock(): pos. over limit");
1557 Create block make data
1560 initBlockMake(&data, p);
1566 TimeTaker t("mapgen::make_block()");
1567 mapgen->makeChunk(&data);
1568 //mapgen::make_block(&data);
1570 if(enable_mapgen_debug_info == false)
1571 t.stop(true); // Hide output
1575 Blit data back on map, update lighting, add mobs and whatever this does
1577 finishBlockMake(&data, modified_blocks);
1582 MapBlock *block = getBlockNoCreateNoEx(p);
1590 bool erroneus_content = false;
1591 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1592 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1593 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1596 MapNode n = block->getNode(p);
1597 if(n.getContent() == CONTENT_IGNORE)
1599 infostream<<"CONTENT_IGNORE at "
1600 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
1602 erroneus_content = true;
1606 if(erroneus_content)
1615 Generate a completely empty block
1619 for(s16 z0=0; z0<MAP_BLOCKSIZE; z0++)
1620 for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
1622 for(s16 y0=0; y0<MAP_BLOCKSIZE; y0++)
1625 n.setContent(CONTENT_AIR);
1626 block->setNode(v3s16(x0,y0,z0), n);
1632 if(enable_mapgen_debug_info == false)
1633 timer.stop(true); // Hide output
1639 MapBlock * ServerMap::createBlock(v3s16 p)
1641 DSTACKF("%s: p=(%d,%d,%d)",
1642 FUNCTION_NAME, p.X, p.Y, p.Z);
1645 Do not create over-limit
1647 if (blockpos_over_limit(p))
1648 throw InvalidPositionException("createBlock(): pos. over limit");
1650 v2s16 p2d(p.X, p.Z);
1653 This will create or load a sector if not found in memory.
1654 If block exists on disk, it will be loaded.
1656 NOTE: On old save formats, this will be slow, as it generates
1657 lighting on blocks for them.
1659 ServerMapSector *sector;
1661 sector = (ServerMapSector*)createSector(p2d);
1662 assert(sector->getId() == MAPSECTOR_SERVER);
1664 catch(InvalidPositionException &e)
1666 infostream<<"createBlock: createSector() failed"<<std::endl;
1670 NOTE: This should not be done, or at least the exception
1671 should not be passed on as std::exception, because it
1672 won't be catched at all.
1674 /*catch(std::exception &e)
1676 infostream<<"createBlock: createSector() failed: "
1677 <<e.what()<<std::endl;
1682 Try to get a block from the sector
1685 MapBlock *block = sector->getBlockNoCreateNoEx(block_y);
1688 if(block->isDummy())
1693 block = sector->createBlankBlock(block_y);
1698 MapBlock * ServerMap::emergeBlock(v3s16 p, bool create_blank)
1700 DSTACKF("%s: p=(%d,%d,%d), create_blank=%d",
1702 p.X, p.Y, p.Z, create_blank);
1705 MapBlock *block = getBlockNoCreateNoEx(p);
1706 if(block && block->isDummy() == false)
1711 MapBlock *block = loadBlock(p);
1717 ServerMapSector *sector = createSector(v2s16(p.X, p.Z));
1718 MapBlock *block = sector->createBlankBlock(p.Y);
1726 std::map<v3s16, MapBlock*> modified_blocks;
1727 MapBlock *block = generateBlock(p, modified_blocks);
1731 event.type = MEET_OTHER;
1734 // Copy modified_blocks to event
1735 for(std::map<v3s16, MapBlock*>::iterator
1736 i = modified_blocks.begin();
1737 i != modified_blocks.end(); ++i)
1739 event.modified_blocks.insert(i->first);
1743 dispatchEvent(&event);
1753 MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
1755 MapBlock *block = getBlockNoCreateNoEx(p3d);
1757 m_emerge->enqueueBlockEmerge(PEER_ID_INEXISTENT, p3d, false);
1762 void ServerMap::prepareBlock(MapBlock *block) {
1765 // N.B. This requires no synchronization, since data will not be modified unless
1766 // the VoxelManipulator being updated belongs to the same thread.
1767 void ServerMap::updateVManip(v3s16 pos)
1769 Mapgen *mg = m_emerge->getCurrentMapgen();
1773 MMVManip *vm = mg->vm;
1777 if (!vm->m_area.contains(pos))
1780 s32 idx = vm->m_area.index(pos);
1781 vm->m_data[idx] = getNodeNoEx(pos);
1782 vm->m_flags[idx] &= ~VOXELFLAG_NO_DATA;
1784 vm->m_is_dirty = true;
1787 s16 ServerMap::findGroundLevel(v2s16 p2d)
1791 Uh, just do something random...
1793 // Find existing map from top to down
1796 v3s16 p(p2d.X, max, p2d.Y);
1797 for(; p.Y>min; p.Y--)
1799 MapNode n = getNodeNoEx(p);
1800 if(n.getContent() != CONTENT_IGNORE)
1805 // If this node is not air, go to plan b
1806 if(getNodeNoEx(p).getContent() != CONTENT_AIR)
1808 // Search existing walkable and return it
1809 for(; p.Y>min; p.Y--)
1811 MapNode n = getNodeNoEx(p);
1812 if(content_walkable(n.d) && n.getContent() != CONTENT_IGNORE)
1821 Determine from map generator noise functions
1824 s16 level = m_emerge->getGroundLevelAtPoint(p2d);
1827 //double level = base_rock_level_2d(m_seed, p2d) + AVERAGE_MUD_AMOUNT;
1828 //return (s16)level;
1831 bool ServerMap::loadFromFolders() {
1832 if (!dbase->initialized() &&
1833 !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
1838 void ServerMap::createDirs(std::string path)
1840 if(fs::CreateAllDirs(path) == false)
1842 m_dout<<"ServerMap: Failed to create directory "
1843 <<"\""<<path<<"\""<<std::endl;
1844 throw BaseException("ServerMap failed to create directory");
1848 std::string ServerMap::getSectorDir(v2s16 pos, int layout)
1854 snprintf(cc, 9, "%.4x%.4x",
1855 (unsigned int) pos.X & 0xffff,
1856 (unsigned int) pos.Y & 0xffff);
1858 return m_savedir + DIR_DELIM + "sectors" + DIR_DELIM + cc;
1860 snprintf(cc, 9, (std::string("%.3x") + DIR_DELIM + "%.3x").c_str(),
1861 (unsigned int) pos.X & 0xfff,
1862 (unsigned int) pos.Y & 0xfff);
1864 return m_savedir + DIR_DELIM + "sectors2" + DIR_DELIM + cc;
1871 v2s16 ServerMap::getSectorPos(std::string dirname)
1873 unsigned int x = 0, y = 0;
1875 std::string component;
1876 fs::RemoveLastPathComponent(dirname, &component, 1);
1877 if(component.size() == 8)
1880 r = sscanf(component.c_str(), "%4x%4x", &x, &y);
1882 else if(component.size() == 3)
1885 fs::RemoveLastPathComponent(dirname, &component, 2);
1886 r = sscanf(component.c_str(), (std::string("%3x") + DIR_DELIM + "%3x").c_str(), &x, &y);
1887 // Sign-extend the 12 bit values up to 16 bits...
1888 if(x & 0x800) x |= 0xF000;
1889 if(y & 0x800) y |= 0xF000;
1896 FATAL_ERROR_IF(r != 2, "getSectorPos()");
1897 v2s16 pos((s16)x, (s16)y);
1901 v3s16 ServerMap::getBlockPos(std::string sectordir, std::string blockfile)
1903 v2s16 p2d = getSectorPos(sectordir);
1905 if(blockfile.size() != 4){
1906 throw InvalidFilenameException("Invalid block filename");
1909 int r = sscanf(blockfile.c_str(), "%4x", &y);
1911 throw InvalidFilenameException("Invalid block filename");
1912 return v3s16(p2d.X, y, p2d.Y);
1915 std::string ServerMap::getBlockFilename(v3s16 p)
1918 snprintf(cc, 5, "%.4x", (unsigned int)p.Y&0xffff);
1922 void ServerMap::save(ModifiedState save_level)
1924 DSTACK(FUNCTION_NAME);
1925 if(m_map_saving_enabled == false) {
1926 warningstream<<"Not saving map, saving disabled."<<std::endl;
1930 if(save_level == MOD_STATE_CLEAN)
1931 infostream<<"ServerMap: Saving whole map, this can take time."
1934 if (m_map_metadata_changed || save_level == MOD_STATE_CLEAN) {
1935 if (settings_mgr.saveMapMeta())
1936 m_map_metadata_changed = false;
1939 // Profile modified reasons
1940 Profiler modprofiler;
1942 u32 sector_meta_count = 0;
1943 u32 block_count = 0;
1944 u32 block_count_all = 0; // Number of blocks in memory
1946 // Don't do anything with sqlite unless something is really saved
1947 bool save_started = false;
1949 for(std::map<v2s16, MapSector*>::iterator i = m_sectors.begin();
1950 i != m_sectors.end(); ++i) {
1951 ServerMapSector *sector = (ServerMapSector*)i->second;
1952 assert(sector->getId() == MAPSECTOR_SERVER);
1954 if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) {
1955 saveSectorMeta(sector);
1956 sector_meta_count++;
1959 MapBlockVect blocks;
1960 sector->getBlocks(blocks);
1962 for(MapBlockVect::iterator j = blocks.begin();
1963 j != blocks.end(); ++j) {
1964 MapBlock *block = *j;
1968 if(block->getModified() >= (u32)save_level) {
1972 save_started = true;
1975 modprofiler.add(block->getModifiedReasonString(), 1);
1980 /*infostream<<"ServerMap: Written block ("
1981 <<block->getPos().X<<","
1982 <<block->getPos().Y<<","
1983 <<block->getPos().Z<<")"
1993 Only print if something happened or saved whole map
1995 if(save_level == MOD_STATE_CLEAN || sector_meta_count != 0
1996 || block_count != 0) {
1997 infostream<<"ServerMap: Written: "
1998 <<sector_meta_count<<" sector metadata files, "
1999 <<block_count<<" block files"
2000 <<", "<<block_count_all<<" blocks in memory."
2002 PrintInfo(infostream); // ServerMap/ClientMap:
2003 infostream<<"Blocks modified by: "<<std::endl;
2004 modprofiler.print(infostream);
2008 void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
2010 if (loadFromFolders()) {
2011 errorstream << "Map::listAllLoadableBlocks(): Result will be missing "
2012 << "all blocks that are stored in flat files." << std::endl;
2014 dbase->listAllLoadableBlocks(dst);
2017 void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
2019 for(std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
2020 si != m_sectors.end(); ++si)
2022 MapSector *sector = si->second;
2024 MapBlockVect blocks;
2025 sector->getBlocks(blocks);
2027 for(MapBlockVect::iterator i = blocks.begin();
2028 i != blocks.end(); ++i) {
2029 v3s16 p = (*i)->getPos();
2035 void ServerMap::saveSectorMeta(ServerMapSector *sector)
2037 DSTACK(FUNCTION_NAME);
2038 // Format used for writing
2039 u8 version = SER_FMT_VER_HIGHEST_WRITE;
2041 v2s16 pos = sector->getPos();
2042 std::string dir = getSectorDir(pos);
2045 std::string fullpath = dir + DIR_DELIM + "meta";
2046 std::ostringstream ss(std::ios_base::binary);
2048 sector->serialize(ss, version);
2050 if(!fs::safeWriteToFile(fullpath, ss.str()))
2051 throw FileNotGoodException("Cannot write sector metafile");
2053 sector->differs_from_disk = false;
2056 MapSector* ServerMap::loadSectorMeta(std::string sectordir, bool save_after_load)
2058 DSTACK(FUNCTION_NAME);
2060 v2s16 p2d = getSectorPos(sectordir);
2062 ServerMapSector *sector = NULL;
2064 std::string fullpath = sectordir + DIR_DELIM + "meta";
2065 std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2066 if(is.good() == false)
2068 // If the directory exists anyway, it probably is in some old
2069 // format. Just go ahead and create the sector.
2070 if(fs::PathExists(sectordir))
2072 /*infostream<<"ServerMap::loadSectorMeta(): Sector metafile "
2073 <<fullpath<<" doesn't exist but directory does."
2074 <<" Continuing with a sector with no metadata."
2076 sector = new ServerMapSector(this, p2d, m_gamedef);
2077 m_sectors[p2d] = sector;
2081 throw FileNotGoodException("Cannot open sector metafile");
2086 sector = ServerMapSector::deSerialize
2087 (is, this, p2d, m_sectors, m_gamedef);
2089 saveSectorMeta(sector);
2092 sector->differs_from_disk = false;
2097 bool ServerMap::loadSectorMeta(v2s16 p2d)
2099 DSTACK(FUNCTION_NAME);
2101 // The directory layout we're going to load from.
2102 // 1 - original sectors/xxxxzzzz/
2103 // 2 - new sectors2/xxx/zzz/
2104 // If we load from anything but the latest structure, we will
2105 // immediately save to the new one, and remove the old.
2107 std::string sectordir1 = getSectorDir(p2d, 1);
2108 std::string sectordir;
2109 if(fs::PathExists(sectordir1))
2111 sectordir = sectordir1;
2116 sectordir = getSectorDir(p2d, 2);
2120 loadSectorMeta(sectordir, loadlayout != 2);
2122 catch(InvalidFilenameException &e)
2126 catch(FileNotGoodException &e)
2130 catch(std::exception &e)
2139 bool ServerMap::loadSectorFull(v2s16 p2d)
2141 DSTACK(FUNCTION_NAME);
2143 MapSector *sector = NULL;
2145 // The directory layout we're going to load from.
2146 // 1 - original sectors/xxxxzzzz/
2147 // 2 - new sectors2/xxx/zzz/
2148 // If we load from anything but the latest structure, we will
2149 // immediately save to the new one, and remove the old.
2151 std::string sectordir1 = getSectorDir(p2d, 1);
2152 std::string sectordir;
2153 if(fs::PathExists(sectordir1))
2155 sectordir = sectordir1;
2160 sectordir = getSectorDir(p2d, 2);
2164 sector = loadSectorMeta(sectordir, loadlayout != 2);
2166 catch(InvalidFilenameException &e)
2170 catch(FileNotGoodException &e)
2174 catch(std::exception &e)
2182 std::vector<fs::DirListNode> list2 = fs::GetDirListing
2184 std::vector<fs::DirListNode>::iterator i2;
2185 for(i2=list2.begin(); i2!=list2.end(); i2++)
2191 loadBlock(sectordir, i2->name, sector, loadlayout != 2);
2193 catch(InvalidFilenameException &e)
2195 // This catches unknown crap in directory
2201 infostream<<"Sector converted to new layout - deleting "<<
2202 sectordir1<<std::endl;
2203 fs::RecursiveDelete(sectordir1);
2210 Database *ServerMap::createDatabase(
2211 const std::string &name,
2212 const std::string &savedir,
2215 if (name == "sqlite3")
2216 return new Database_SQLite3(savedir);
2217 if (name == "dummy")
2218 return new Database_Dummy();
2220 else if (name == "leveldb")
2221 return new Database_LevelDB(savedir);
2224 else if (name == "redis")
2225 return new Database_Redis(conf);
2228 else if (name == "postgresql")
2229 return new Database_PostgreSQL(conf);
2232 throw BaseException(std::string("Database backend ") + name + " not supported.");
2235 void ServerMap::beginSave()
2240 void ServerMap::endSave()
2245 bool ServerMap::saveBlock(MapBlock *block)
2247 return saveBlock(block, dbase);
2250 bool ServerMap::saveBlock(MapBlock *block, Database *db)
2252 v3s16 p3d = block->getPos();
2254 // Dummy blocks are not written
2255 if (block->isDummy()) {
2256 warningstream << "saveBlock: Not writing dummy block "
2257 << PP(p3d) << std::endl;
2261 // Format used for writing
2262 u8 version = SER_FMT_VER_HIGHEST_WRITE;
2265 [0] u8 serialization version
2268 std::ostringstream o(std::ios_base::binary);
2269 o.write((char*) &version, 1);
2270 block->serialize(o, version, true);
2272 std::string data = o.str();
2273 bool ret = db->saveBlock(p3d, data);
2275 // We just wrote it to the disk so clear modified flag
2276 block->resetModified();
2281 void ServerMap::loadBlock(std::string sectordir, std::string blockfile,
2282 MapSector *sector, bool save_after_load)
2284 DSTACK(FUNCTION_NAME);
2286 std::string fullpath = sectordir + DIR_DELIM + blockfile;
2289 std::ifstream is(fullpath.c_str(), std::ios_base::binary);
2290 if(is.good() == false)
2291 throw FileNotGoodException("Cannot open block file");
2293 v3s16 p3d = getBlockPos(sectordir, blockfile);
2294 v2s16 p2d(p3d.X, p3d.Z);
2296 assert(sector->getPos() == p2d);
2298 u8 version = SER_FMT_VER_INVALID;
2299 is.read((char*)&version, 1);
2302 throw SerializationError("ServerMap::loadBlock(): Failed"
2303 " to read MapBlock version");
2305 /*u32 block_size = MapBlock::serializedLength(version);
2306 SharedBuffer<u8> data(block_size);
2307 is.read((char*)*data, block_size);*/
2309 // This will always return a sector because we're the server
2310 //MapSector *sector = emergeSector(p2d);
2312 MapBlock *block = NULL;
2313 bool created_new = false;
2314 block = sector->getBlockNoCreateNoEx(p3d.Y);
2317 block = sector->createBlankBlockNoInsert(p3d.Y);
2322 block->deSerialize(is, version, true);
2324 // If it's a new block, insert it to the map
2326 sector->insertBlock(block);
2327 ReflowScan scanner(this, m_emerge->ndef);
2328 scanner.scan(block, &m_transforming_liquid);
2332 Save blocks loaded in old format in new format
2335 if(version < SER_FMT_VER_HIGHEST_WRITE || save_after_load)
2339 // Should be in database now, so delete the old file
2340 fs::RecursiveDelete(fullpath);
2343 // We just loaded it from the disk, so it's up-to-date.
2344 block->resetModified();
2347 catch(SerializationError &e)
2349 warningstream<<"Invalid block data on disk "
2350 <<"fullpath="<<fullpath
2351 <<" (SerializationError). "
2352 <<"what()="<<e.what()
2354 // Ignoring. A new one will be generated.
2357 // TODO: Backup file; name is in fullpath.
2361 void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load)
2363 DSTACK(FUNCTION_NAME);
2366 std::istringstream is(*blob, std::ios_base::binary);
2368 u8 version = SER_FMT_VER_INVALID;
2369 is.read((char*)&version, 1);
2372 throw SerializationError("ServerMap::loadBlock(): Failed"
2373 " to read MapBlock version");
2375 /*u32 block_size = MapBlock::serializedLength(version);
2376 SharedBuffer<u8> data(block_size);
2377 is.read((char*)*data, block_size);*/
2379 // This will always return a sector because we're the server
2380 //MapSector *sector = emergeSector(p2d);
2382 MapBlock *block = NULL;
2383 bool created_new = false;
2384 block = sector->getBlockNoCreateNoEx(p3d.Y);
2387 block = sector->createBlankBlockNoInsert(p3d.Y);
2392 block->deSerialize(is, version, true);
2394 // If it's a new block, insert it to the map
2396 sector->insertBlock(block);
2397 ReflowScan scanner(this, m_emerge->ndef);
2398 scanner.scan(block, &m_transforming_liquid);
2402 Save blocks loaded in old format in new format
2405 //if(version < SER_FMT_VER_HIGHEST_READ || save_after_load)
2406 // Only save if asked to; no need to update version
2410 // We just loaded it from, so it's up-to-date.
2411 block->resetModified();
2413 catch(SerializationError &e)
2415 errorstream<<"Invalid block data in database"
2416 <<" ("<<p3d.X<<","<<p3d.Y<<","<<p3d.Z<<")"
2417 <<" (SerializationError): "<<e.what()<<std::endl;
2419 // TODO: Block should be marked as invalid in memory so that it is
2420 // not touched but the game can run
2422 if(g_settings->getBool("ignore_world_load_errors")){
2423 errorstream<<"Ignoring block load error. Duck and cover! "
2424 <<"(ignore_world_load_errors)"<<std::endl;
2426 throw SerializationError("Invalid block data in database");
2431 MapBlock* ServerMap::loadBlock(v3s16 blockpos)
2433 DSTACK(FUNCTION_NAME);
2435 bool created_new = (getBlockNoCreateNoEx(blockpos) == NULL);
2437 v2s16 p2d(blockpos.X, blockpos.Z);
2440 dbase->loadBlock(blockpos, &ret);
2442 loadBlock(&ret, blockpos, createSector(p2d), false);
2444 // Not found in database, try the files
2446 // The directory layout we're going to load from.
2447 // 1 - original sectors/xxxxzzzz/
2448 // 2 - new sectors2/xxx/zzz/
2449 // If we load from anything but the latest structure, we will
2450 // immediately save to the new one, and remove the old.
2452 std::string sectordir1 = getSectorDir(p2d, 1);
2453 std::string sectordir;
2454 if (fs::PathExists(sectordir1)) {
2455 sectordir = sectordir1;
2458 sectordir = getSectorDir(p2d, 2);
2462 Make sure sector is loaded
2465 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2466 if (sector == NULL) {
2468 sector = loadSectorMeta(sectordir, loadlayout != 2);
2469 } catch(InvalidFilenameException &e) {
2471 } catch(FileNotGoodException &e) {
2473 } catch(std::exception &e) {
2480 Make sure file exists
2483 std::string blockfilename = getBlockFilename(blockpos);
2484 if (fs::PathExists(sectordir + DIR_DELIM + blockfilename) == false)
2488 Load block and save it to the database
2490 loadBlock(sectordir, blockfilename, sector, true);
2492 MapBlock *block = getBlockNoCreateNoEx(blockpos);
2493 if (created_new && (block != NULL)) {
2494 std::map<v3s16, MapBlock*> modified_blocks;
2495 // Fix lighting if necessary
2496 voxalgo::update_block_border_lighting(this, block, modified_blocks);
2497 if (!modified_blocks.empty()) {
2498 //Modified lighting, send event
2500 event.type = MEET_OTHER;
2501 std::map<v3s16, MapBlock *>::iterator it;
2502 for (it = modified_blocks.begin();
2503 it != modified_blocks.end(); ++it)
2504 event.modified_blocks.insert(it->first);
2505 dispatchEvent(&event);
2511 bool ServerMap::deleteBlock(v3s16 blockpos)
2513 if (!dbase->deleteBlock(blockpos))
2516 MapBlock *block = getBlockNoCreateNoEx(blockpos);
2518 v2s16 p2d(blockpos.X, blockpos.Z);
2519 MapSector *sector = getSectorNoGenerateNoEx(p2d);
2522 sector->deleteBlock(block);
2528 void ServerMap::PrintInfo(std::ostream &out)
2533 MMVManip::MMVManip(Map *map):
2536 m_create_area(false),
2541 MMVManip::~MMVManip()
2545 void MMVManip::initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
2546 bool load_if_inexistent)
2548 TimeTaker timer1("initialEmerge", &emerge_time);
2550 // Units of these are MapBlocks
2551 v3s16 p_min = blockpos_min;
2552 v3s16 p_max = blockpos_max;
2554 VoxelArea block_area_nodes
2555 (p_min*MAP_BLOCKSIZE, (p_max+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2557 u32 size_MB = block_area_nodes.getVolume()*4/1000000;
2560 infostream<<"initialEmerge: area: ";
2561 block_area_nodes.print(infostream);
2562 infostream<<" ("<<size_MB<<"MB)";
2563 infostream<<std::endl;
2566 addArea(block_area_nodes);
2568 for(s32 z=p_min.Z; z<=p_max.Z; z++)
2569 for(s32 y=p_min.Y; y<=p_max.Y; y++)
2570 for(s32 x=p_min.X; x<=p_max.X; x++)
2575 std::map<v3s16, u8>::iterator n;
2576 n = m_loaded_blocks.find(p);
2577 if(n != m_loaded_blocks.end())
2580 bool block_data_inexistent = false;
2583 TimeTaker timer1("emerge load", &emerge_load_time);
2585 block = m_map->getBlockNoCreate(p);
2586 if(block->isDummy())
2587 block_data_inexistent = true;
2589 block->copyTo(*this);
2591 catch(InvalidPositionException &e)
2593 block_data_inexistent = true;
2596 if(block_data_inexistent)
2599 if (load_if_inexistent && !blockpos_over_limit(p)) {
2600 ServerMap *svrmap = (ServerMap *)m_map;
2601 block = svrmap->emergeBlock(p, false);
2603 block = svrmap->createBlock(p);
2604 block->copyTo(*this);
2606 flags |= VMANIP_BLOCK_DATA_INEXIST;
2609 Mark area inexistent
2611 VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
2612 // Fill with VOXELFLAG_NO_DATA
2613 for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
2614 for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
2616 s32 i = m_area.index(a.MinEdge.X,y,z);
2617 memset(&m_flags[i], VOXELFLAG_NO_DATA, MAP_BLOCKSIZE);
2621 /*else if (block->getNode(0, 0, 0).getContent() == CONTENT_IGNORE)
2623 // Mark that block was loaded as blank
2624 flags |= VMANIP_BLOCK_CONTAINS_CIGNORE;
2627 m_loaded_blocks[p] = flags;
2633 void MMVManip::blitBackAll(std::map<v3s16, MapBlock*> *modified_blocks,
2634 bool overwrite_generated)
2636 if(m_area.getExtent() == v3s16(0,0,0))
2640 Copy data of all blocks
2642 for(std::map<v3s16, u8>::iterator
2643 i = m_loaded_blocks.begin();
2644 i != m_loaded_blocks.end(); ++i)
2647 MapBlock *block = m_map->getBlockNoCreateNoEx(p);
2648 bool existed = !(i->second & VMANIP_BLOCK_DATA_INEXIST);
2649 if ((existed == false) || (block == NULL) ||
2650 (overwrite_generated == false && block->isGenerated() == true))
2653 block->copyFrom(*this);
2656 (*modified_blocks)[p] = block;