X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fmap.cpp;h=b3954019b84951b0691aa591d710532a10af3bd6;hb=569fca53089b7b7e87b02edd44e2ad47166f7af6;hp=2d661a52213d002023e96619f3cdc771dc54b069;hpb=67a6bc4ab5be607c4a9f9a349dee629e864c2d42;p=minetest.git diff --git a/src/map.cpp b/src/map.cpp index 2d661a522..b3954019b 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -21,19 +21,19 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapsector.h" #include "mapblock.h" #include "main.h" -#include "client.h" #include "filesys.h" #include "utility.h" #include "voxel.h" #include "porting.h" #include "mapgen.h" #include "nodemetadata.h" -#include "content_mapnode.h" -#ifndef SERVER -#include -#endif #include "settings.h" #include "log.h" +#include "profiler.h" +#include "nodedef.h" +#include "gamedef.h" + +#define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" /* SQLite format specification: @@ -56,8 +56,9 @@ with this program; if not, write to the Free Software Foundation, Inc., Map */ -Map::Map(std::ostream &dout): +Map::Map(std::ostream &dout, IGameDef *gamedef): m_dout(dout), + m_gamedef(gamedef), m_sector_cache(NULL) { /*m_sector_mutex.Init(); @@ -201,6 +202,15 @@ void Map::setNode(v3s16 p, MapNode & n) v3s16 blockpos = getNodeBlockPos(p); MapBlock *block = getBlockNoCreate(blockpos); v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; + // Never allow placing CONTENT_IGNORE, it fucks up stuff + if(n.getContent() == CONTENT_IGNORE){ + errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE" + <<" while trying to replace \"" + <ndef()->get(block->getNodeNoCheck(relpos)).name + <<"\" at "<setNodeNoCheck(relpos, n); } @@ -227,6 +237,8 @@ void Map::unspreadLight(enum LightBank bank, core::map & light_sources, core::map & modified_blocks) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + v3s16 dirs[6] = { v3s16(0,0,1), // back v3s16(0,1,0), // top @@ -323,19 +335,20 @@ void Map::unspreadLight(enum LightBank bank, If the neighbor is dimmer than what was specified as oldlight (the light of the previous node) */ - if(n2.getLight(bank) < oldlight) + if(n2.getLight(bank, nodemgr) < oldlight) { /* And the neighbor is transparent and it has some light */ - if(n2.light_propagates() && n2.getLight(bank) != 0) + if(nodemgr->get(n2).light_propagates + && n2.getLight(bank, nodemgr) != 0) { /* Set light to 0 and add to queue */ - u8 current_light = n2.getLight(bank); - n2.setLight(bank, 0); + u8 current_light = n2.getLight(bank, nodemgr); + n2.setLight(bank, 0, nodemgr); block->setNode(relpos, n2); unlighted_nodes.insert(n2pos, current_light); @@ -409,6 +422,8 @@ void Map::spreadLight(enum LightBank bank, core::map & from_nodes, core::map & modified_blocks) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + const v3s16 dirs[6] = { v3s16(0,0,1), // back v3s16(0,1,0), // top @@ -467,7 +482,7 @@ void Map::spreadLight(enum LightBank bank, // Get node straight from the block MapNode n = block->getNode(relpos); - u8 oldlight = n.getLight(bank); + u8 oldlight = n.getLight(bank, nodemgr); u8 newlight = diminish_light(oldlight); // Loop through 6 neighbors @@ -505,7 +520,7 @@ void Map::spreadLight(enum LightBank bank, If the neighbor is brighter than the current node, add to list (it will light up this node on its turn) */ - if(n2.getLight(bank) > undiminish_light(oldlight)) + if(n2.getLight(bank, nodemgr) > undiminish_light(oldlight)) { lighted_nodes.insert(n2pos, true); //lighted_nodes.push_back(n2pos); @@ -515,11 +530,11 @@ void Map::spreadLight(enum LightBank bank, If the neighbor is dimmer than how much light this node would spread on it, add to list */ - if(n2.getLight(bank) < newlight) + if(n2.getLight(bank, nodemgr) < newlight) { - if(n2.light_propagates()) + if(nodemgr->get(n2).light_propagates) { - n2.setLight(bank, newlight); + n2.setLight(bank, newlight, nodemgr); block->setNode(relpos, n2); lighted_nodes.insert(n2pos, true); //lighted_nodes.push_back(n2pos); @@ -568,6 +583,8 @@ void Map::lightNeighbors(enum LightBank bank, v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + v3s16 dirs[6] = { v3s16(0,0,1), // back v3s16(0,1,0), // top @@ -593,8 +610,8 @@ v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p) { continue; } - if(n2.getLight(bank) > brightest_light || found_something == false){ - brightest_light = n2.getLight(bank); + if(n2.getLight(bank, nodemgr) > brightest_light || found_something == false){ + brightest_light = n2.getLight(bank, nodemgr); brightest_pos = n2pos; found_something = true; } @@ -617,6 +634,8 @@ v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p) s16 Map::propagateSunlight(v3s16 start, core::map & modified_blocks) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + s16 y = start.Y; for(; ; y--) { @@ -635,23 +654,15 @@ s16 Map::propagateSunlight(v3s16 start, v3s16 relpos = pos - blockpos*MAP_BLOCKSIZE; MapNode n = block->getNode(relpos); - if(n.sunlight_propagates()) + if(nodemgr->get(n).sunlight_propagates) { - n.setLight(LIGHTBANK_DAY, LIGHT_SUN); + n.setLight(LIGHTBANK_DAY, LIGHT_SUN, nodemgr); block->setNode(relpos, n); modified_blocks.insert(blockpos, block); } else { - /*// Turn mud into grass - if(n.getContent() == CONTENT_MUD) - { - n.setContent(CONTENT_GRASS); - block->setNode(relpos, n); - modified_blocks.insert(blockpos, block); - }*/ - // Sunlight goes no further break; } @@ -663,6 +674,8 @@ void Map::updateLighting(enum LightBank bank, core::map & a_blocks, core::map & modified_blocks) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + /*m_dout<getNode(v3s16(x,y,z)); - u8 oldlight = n.getLight(bank); - n.setLight(bank, 0); + u8 oldlight = n.getLight(bank, nodemgr); + n.setLight(bank, 0, nodemgr); block->setNode(v3s16(x,y,z), n); // Collect borders for unlighting @@ -841,15 +854,15 @@ void Map::updateLighting(enum LightBank bank, for(s16 y=-1; y<=1; y++) for(s16 x=-1; x<=1; x++) { - v3s16 p(x,y,z); - MapBlock *block = getBlockNoCreateNoEx(p); + v3s16 p2 = p + v3s16(x,y,z); + MapBlock *block = getBlockNoCreateNoEx(p2); if(block == NULL) continue; if(block->isDummy()) continue; if(block->getLightingExpired()) continue; - vmanip.initialEmerge(p, p); + vmanip.initialEmerge(p2, p2); }*/ // Lighting of block will be updated completely @@ -858,11 +871,11 @@ void Map::updateLighting(enum LightBank bank, { //TimeTaker timer("unSpreadLight"); - vmanip.unspreadLight(bank, unlight_from, light_sources); + vmanip.unspreadLight(bank, unlight_from, light_sources, nodemgr); } { //TimeTaker timer("spreadLight"); - vmanip.spreadLight(bank, light_sources); + vmanip.spreadLight(bank, light_sources, nodemgr); } { //TimeTaker timer("blitBack"); @@ -896,8 +909,10 @@ void Map::updateLighting(core::map & a_blocks, /* */ void Map::addNodeAndUpdate(v3s16 p, MapNode n, - core::map &modified_blocks, std::string &player_name) + core::map &modified_blocks) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + /*PrintInfo(m_dout); m_dout<get(n).sunlight_propagates) { - n.setLight(LIGHTBANK_DAY, LIGHT_SUN); + n.setLight(LIGHTBANK_DAY, LIGHT_SUN, nodemgr); } /* @@ -1015,13 +997,16 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, /* Add intial metadata */ - - NodeMetadata *meta_proto = content_features(n).initial_metadata; - if(meta_proto) - { - NodeMetadata *meta = meta_proto->clone(); - meta->setOwner(player_name); - setNodeMetadata(p, meta); + + std::string metadata_name = nodemgr->get(n).metadata_name; + if(metadata_name != ""){ + NodeMetadata *meta = NodeMetadata::create(metadata_name, m_gamedef); + if(!meta){ + errorstream<<"Failed to create node metadata \"" + <get(n).sunlight_propagates) { s16 y = p.Y - 1; for(;; y--){ @@ -1047,12 +1032,12 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, break; } - if(n2.getLight(LIGHTBANK_DAY) == LIGHT_SUN) + if(n2.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN) { unLightNeighbors(LIGHTBANK_DAY, - n2pos, n2.getLight(LIGHTBANK_DAY), + n2pos, n2.getLight(LIGHTBANK_DAY, nodemgr), light_sources, modified_blocks); - n2.setLight(LIGHTBANK_DAY, 0); + n2.setLight(LIGHTBANK_DAY, 0, nodemgr); setNode(n2pos, n2); } else @@ -1102,7 +1087,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, v3s16 p2 = p + dirs[i]; MapNode n2 = getNode(p2); - if(content_liquid(n2.getContent()) || n2.getContent() == CONTENT_AIR) + if(nodemgr->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR) { m_transforming_liquid.push_back(p2); } @@ -1118,6 +1103,8 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, void Map::removeNodeAndUpdate(v3s16 p, core::map &modified_blocks) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + /*PrintInfo(m_dout); m_dout<get(n2).isLiquid() || n2.getContent() == CONTENT_AIR) { m_transforming_liquid.push_back(p2); } @@ -1297,8 +1284,7 @@ bool Map::addNodeWithEvent(v3s16 p, MapNode n) bool succeeded = true; try{ core::map modified_blocks; - std::string st = std::string(""); - addNodeAndUpdate(p, n, modified_blocks, st); + addNodeAndUpdate(p, n, modified_blocks); // Copy modified_blocks to event for(core::map::Iterator @@ -1410,9 +1396,13 @@ void Map::timerUpdate(float dtime, float unload_timeout, { bool save_before_unloading = (mapType() == MAPTYPE_SERVER); + // Profile modified reasons + Profiler modprofiler; + core::list sector_deletion_queue; u32 deleted_blocks_count = 0; u32 saved_blocks_count = 0; + u32 block_count_all = 0; core::map::Iterator si; @@ -1442,6 +1432,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, if(block->getModified() != MOD_STATE_CLEAN && save_before_unloading) { + modprofiler.add(block->getModifiedReason(), 1); saveBlock(block); saved_blocks_count++; } @@ -1457,6 +1448,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, else { all_blocks_deleted = false; + block_count_all++; } } @@ -1477,7 +1469,13 @@ void Map::timerUpdate(float dtime, float unload_timeout, <<" blocks from memory"; if(save_before_unloading) infostream<<", of which "< & modified_blocks) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + DSTACK(__FUNCTION_NAME); //TimeTaker timer("transformLiquids()"); @@ -1607,11 +1607,11 @@ void Map::transformLiquids(core::map & modified_blocks) */ s8 liquid_level = -1; u8 liquid_kind = CONTENT_IGNORE; - LiquidType liquid_type = content_features(n0.getContent()).liquid_type; + LiquidType liquid_type = nodemgr->get(n0).liquid_type; switch (liquid_type) { case LIQUID_SOURCE: liquid_level = LIQUID_LEVEL_SOURCE; - liquid_kind = content_features(n0.getContent()).liquid_alternative_flowing; + liquid_kind = nodemgr->getId(nodemgr->get(n0).liquid_alternative_flowing); break; case LIQUID_FLOWING: liquid_level = (n0.param2 & LIQUID_LEVEL_MASK); @@ -1651,7 +1651,7 @@ void Map::transformLiquids(core::map & modified_blocks) } v3s16 npos = p0 + dirs[i]; NodeNeighbor nb = {getNodeNoEx(npos), nt, npos}; - switch (content_features(nb.n.getContent()).liquid_type) { + switch (nodemgr->get(nb.n.getContent()).liquid_type) { case LIQUID_NONE: if (nb.n.getContent() == CONTENT_AIR) { airs[num_airs++] = nb; @@ -1671,18 +1671,20 @@ void Map::transformLiquids(core::map & modified_blocks) case LIQUID_SOURCE: // if this node is not (yet) of a liquid type, choose the first liquid type we encounter if (liquid_kind == CONTENT_AIR) - liquid_kind = content_features(nb.n.getContent()).liquid_alternative_flowing; - if (content_features(nb.n.getContent()).liquid_alternative_flowing !=liquid_kind) { + liquid_kind = nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing); + if (nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing) != liquid_kind) { neutrals[num_neutrals++] = nb; } else { - sources[num_sources++] = nb; + // Do not count bottom source, it will screw things up + if(dirs[i].Y != -1) + sources[num_sources++] = nb; } break; case LIQUID_FLOWING: // if this node is not (yet) of a liquid type, choose the first liquid type we encounter if (liquid_kind == CONTENT_AIR) - liquid_kind = content_features(nb.n.getContent()).liquid_alternative_flowing; - if (content_features(nb.n.getContent()).liquid_alternative_flowing != liquid_kind) { + liquid_kind = nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing); + if (nodemgr->getId(nodemgr->get(nb.n).liquid_alternative_flowing) != liquid_kind) { neutrals[num_neutrals++] = nb; } else { flows[num_flows++] = nb; @@ -1703,7 +1705,7 @@ void Map::transformLiquids(core::map & modified_blocks) // liquid_kind will be set to either the flowing alternative of the node (if it's a liquid) // or the flowing alternative of the first of the surrounding sources (if it's air), so // it's perfectly safe to use liquid_kind here to determine the new node content. - new_node_content = content_features(liquid_kind).liquid_alternative_source; + new_node_content = nodemgr->getId(nodemgr->get(liquid_kind).liquid_alternative_source); } else if (num_sources == 1 && sources[0].t != NEIGHBOR_LOWER) { // liquid_kind is set properly, see above new_node_content = liquid_kind; @@ -1732,7 +1734,7 @@ void Map::transformLiquids(core::map & modified_blocks) } } - u8 viscosity = content_features(liquid_kind).liquid_viscosity; + u8 viscosity = nodemgr->get(liquid_kind).liquid_viscosity; if (viscosity > 1 && max_node_level != liquid_level) { // amount to gain, limited by viscosity // must be at least 1 in absolute value @@ -1758,7 +1760,7 @@ void Map::transformLiquids(core::map & modified_blocks) /* check if anything has changed. if not, just continue with the next node. */ - if (new_node_content == n0.getContent() && (content_features(n0.getContent()).liquid_type != LIQUID_FLOWING || + if (new_node_content == n0.getContent() && (nodemgr->get(n0.getContent()).liquid_type != LIQUID_FLOWING || ((n0.param2 & LIQUID_LEVEL_MASK) == (u8)new_node_level && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) == LIQUID_FLOW_DOWN_MASK) == flowing_down))) @@ -1768,8 +1770,8 @@ void Map::transformLiquids(core::map & modified_blocks) /* update the current node */ - bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK)); - if (content_features(new_node_content).liquid_type == LIQUID_FLOWING) { + //bool flow_down_enabled = (flowing_down && ((n0.param2 & LIQUID_FLOW_DOWN_MASK) != LIQUID_FLOW_DOWN_MASK)); + if (nodemgr->get(new_node_content).liquid_type == LIQUID_FLOWING) { // set level to last 3 bits, flowing down bit to 4th bit n0.param2 = (flowing_down ? LIQUID_FLOW_DOWN_MASK : 0x00) | (new_node_level & LIQUID_LEVEL_MASK); } else { @@ -1783,14 +1785,14 @@ void Map::transformLiquids(core::map & modified_blocks) if(block != NULL) { modified_blocks.insert(blockpos, block); // If node emits light, MapBlock requires lighting update - if(content_features(n0).light_source != 0) + if(nodemgr->get(n0).light_source != 0) lighting_modified_blocks[block->getPos()] = block; } /* enqueue neighbors for update if neccessary */ - switch (content_features(n0.getContent()).liquid_type) { + switch (nodemgr->get(n0.getContent()).liquid_type) { case LIQUID_SOURCE: case LIQUID_FLOWING: // make sure source flows into all neighboring nodes @@ -1819,13 +1821,18 @@ NodeMetadata* Map::getNodeMetadata(v3s16 p) v3s16 blockpos = getNodeBlockPos(p); v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE; MapBlock *block = getBlockNoCreateNoEx(blockpos); - if(block == NULL) + if(!block){ + infostream<<"Map::getNodeMetadata(): Need to emerge " + <m_node_metadata.get(p_rel); + NodeMetadata *meta = block->m_node_metadata->get(p_rel); return meta; } @@ -1834,13 +1841,18 @@ void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta) v3s16 blockpos = getNodeBlockPos(p); v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE; MapBlock *block = getBlockNoCreateNoEx(blockpos); - if(block == NULL) + if(!block){ + infostream<<"Map::setNodeMetadata(): Need to emerge " + <m_node_metadata.set(p_rel, meta); + block->m_node_metadata->set(p_rel, meta); } void Map::removeNodeMetadata(v3s16 p) @@ -1854,7 +1866,7 @@ void Map::removeNodeMetadata(v3s16 p) <m_node_metadata.remove(p_rel); + block->m_node_metadata->remove(p_rel); } void Map::nodeMetadataStep(float dtime, @@ -1879,7 +1891,7 @@ void Map::nodeMetadataStep(float dtime, for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++) { MapBlock *block = *i; - bool changed = block->m_node_metadata.step(dtime); + bool changed = block->m_node_metadata->step(dtime); if(changed) changed_blocks[block->getPos()] = block; } @@ -1890,15 +1902,15 @@ void Map::nodeMetadataStep(float dtime, ServerMap */ -ServerMap::ServerMap(std::string savedir): - Map(dout_server), +ServerMap::ServerMap(std::string savedir, IGameDef *gamedef): + Map(dout_server, gamedef), m_seed(0), m_map_metadata_changed(true), m_database(NULL), m_database_read(NULL), m_database_write(NULL) { - infostream<<__FUNCTION_NAME<no_op = false; data->seed = m_seed; data->blockpos = blockpos; + data->nodedef = m_gamedef->ndef(); /* Create the whole area of this and the neighboring blocks @@ -2148,7 +2146,17 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data, /*infostream<<"Resulting vmanip:"<vmanip.print(infostream);*/ - + + // Make sure affected blocks are loaded + for(s16 x=-1; x<=1; x++) + for(s16 z=-1; z<=1; z++) + for(s16 y=-1; y<=1; y++) + { + v3s16 p(blockpos.X+x, blockpos.Y+y, blockpos.Z+z); + // Load from disk if not already in memory + emergeBlock(p, false); + } + /* Blit generated stuff to map NOTE: blitBackAll adds nearly everything to changed_blocks @@ -2287,7 +2295,8 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data, /* Set block as modified */ - block->raiseModified(MOD_STATE_WRITE_NEEDED); + block->raiseModified(MOD_STATE_WRITE_NEEDED, + "finishBlockMake updateDayNightDiff"); } /* @@ -2299,7 +2308,7 @@ MapBlock* ServerMap::finishBlockMake(mapgen::BlockMakeData *data, Save changed parts of map NOTE: Will be saved later. */ - //save(true); + //save(MOD_STATE_WRITE_AT_UNLOAD); /*infostream<<"finishBlockMake() done for ("<setNode(v3s16(x0,y0,z0), n); } } @@ -2610,152 +2616,6 @@ MapBlock * ServerMap::emergeBlock(v3s16 p, bool allow_generate) return NULL; } -#if 0 - /* - Do not generate over-limit - */ - if(p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE - || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE - || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE - || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE - || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE - || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE) - throw InvalidPositionException("emergeBlock(): pos. over limit"); - - v2s16 p2d(p.X, p.Z); - s16 block_y = p.Y; - /* - This will create or load a sector if not found in memory. - If block exists on disk, it will be loaded. - */ - ServerMapSector *sector; - try{ - sector = createSector(p2d); - //sector = emergeSector(p2d, changed_blocks); - } - catch(InvalidPositionException &e) - { - infostream<<"emergeBlock: createSector() failed: " - <getBlockNoCreateNoEx(block_y); - - // If not found, try loading from disk - if(block == NULL) - { - block = loadBlock(p); - } - - // Handle result - if(block == NULL) - { - does_not_exist = true; - } - else if(block->isDummy() == true) - { - does_not_exist = true; - } - else if(block->getLightingExpired()) - { - lighting_expired = true; - } - else - { - // Valid block - //infostream<<"emergeBlock(): Returning already valid block"<insertBlock(block); - } - // Done. - return block; - } - - //infostream<<"Not found on disk, generating."< making one"< light_sources; - bool black_air_left = false; - bool bottom_invalid = - block->propagateSunlight(light_sources, true, - &black_air_left); - - // If sunlight didn't reach everywhere and part of block is - // above ground, lighting has to be properly updated - //if(black_air_left && some_part_underground) - if(black_air_left) - { - lighting_invalidated_blocks[block->getPos()] = block; - } - - if(bottom_invalid) - { - lighting_invalidated_blocks[block->getPos()] = block; - } - } -#endif - - return block; -} -#endif - s16 ServerMap::findGroundLevel(v2s16 p2d) { #if 0 @@ -2812,7 +2672,7 @@ void ServerMap::createDatabase() { if(e == SQLITE_ABORT) throw FileNotGoodException("Could not create database structure"); else - infostream<<"Server: Database structure was created"; + infostream<<"ServerMap: Database structure was created"; } void ServerMap::verifyDatabase() { @@ -2854,7 +2714,13 @@ void ServerMap::verifyDatabase() { throw FileNotGoodException("Cannot prepare write statement"); } - infostream<<"Server: Database opened"<::Iterator i = m_sectors.getIterator(); for(; i.atEnd() == false; i++) { ServerMapSector *sector = (ServerMapSector*)i.getNode()->getValue(); assert(sector->getId() == MAPSECTOR_SERVER); - if(sector->differs_from_disk || only_changed == false) + if(sector->differs_from_disk || save_level == MOD_STATE_CLEAN) { saveSectorMeta(sector); sector_meta_count++; @@ -2988,16 +2859,22 @@ void ServerMap::save(bool only_changed) sector->getBlocks(blocks); core::list::Iterator j; - //sqlite3_exec(m_database, "BEGIN;", NULL, NULL, NULL); for(j=blocks.begin(); j!=blocks.end(); j++) { MapBlock *block = *j; block_count_all++; - if(block->getModified() >= MOD_STATE_WRITE_NEEDED - || only_changed == false) + if(block->getModified() >= save_level) { + // Lazy beginSave() + if(!save_started){ + beginSave(); + save_started = true; + } + + modprofiler.add(block->getModifiedReason(), 1); + saveBlock(block); block_count++; @@ -3007,15 +2884,15 @@ void ServerMap::save(bool only_changed) <getPos().Z<<")" <= 0) + return i % mod; + return mod - ((-i) % mod); +} + +v3s16 ServerMap::getIntegerAsBlock(sqlite3_int64 i) +{ + s32 x = unsignedToSigned(pythonmodulo(i, 4096), 2048); + i = (i - x) / 4096; + s32 y = unsignedToSigned(pythonmodulo(i, 4096), 2048); + i = (i - y) / 4096; + s32 z = unsignedToSigned(pythonmodulo(i, 4096), 2048); + return v3s16(x,y,z); +} + +void ServerMap::listAllLoadableBlocks(core::list &dst) +{ + if(loadFromFolders()){ + errorstream<<"Map::listAllLoadableBlocks(): Result will be missing " + <<"all blocks that are stored in flat files"<serialize(o, version); - - // Write extra data stored on disk - block->serializeDiskExtra(o, version); + block->serialize(o, version, true); // Write block to database @@ -3387,11 +3310,8 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSecto } // Read basic data - block->deSerialize(is, version); + block->deSerialize(is, version, true); - // Read extra data stored on disk - block->deSerializeDiskExtra(is, version); - // If it's a new block, insert it to the map if(created_new) sector->insertBlock(block); @@ -3457,10 +3377,7 @@ void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool } // Read basic data - block->deSerialize(is, version); - - // Read extra data stored on disk - block->deSerializeDiskExtra(is, version); + block->deSerialize(is, version, true); // If it's a new block, insert it to the map if(created_new) @@ -3470,10 +3387,10 @@ void ServerMap::loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool Save blocks loaded in old format in new format */ - if(version < SER_FMT_VER_HIGHEST || save_after_load) - { + //if(version < SER_FMT_VER_HIGHEST || save_after_load) + // Only save if asked to; no need to update version + if(save_after_load) saveBlock(block); - } // We just loaded it from, so it's up-to-date. block->resetModified(); @@ -3592,583 +3509,6 @@ void ServerMap::PrintInfo(std::ostream &out) out<<"ServerMap: "; } -#ifndef SERVER - -/* - ClientMap -*/ - -ClientMap::ClientMap( - Client *client, - MapDrawControl &control, - scene::ISceneNode* parent, - scene::ISceneManager* mgr, - s32 id -): - Map(dout_client), - scene::ISceneNode(parent, mgr, id), - m_client(client), - m_control(control), - m_camera_position(0,0,0), - m_camera_direction(0,0,1), - m_camera_fov(PI) -{ - m_camera_mutex.Init(); - assert(m_camera_mutex.IsInitialized()); - - m_box = core::aabbox3d(-BS*1000000,-BS*1000000,-BS*1000000, - BS*1000000,BS*1000000,BS*1000000); -} - -ClientMap::~ClientMap() -{ - /*JMutexAutoLock lock(mesh_mutex); - - if(mesh != NULL) - { - mesh->drop(); - mesh = NULL; - }*/ -} - -MapSector * ClientMap::emergeSector(v2s16 p2d) -{ - DSTACK(__FUNCTION_NAME); - // Check that it doesn't exist already - try{ - return getSectorNoGenerate(p2d); - } - catch(InvalidPositionException &e) - { - } - - // Create a sector - ClientMapSector *sector = new ClientMapSector(this, p2d); - - { - //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out - m_sectors.insert(p2d, sector); - } - - return sector; -} - -#if 0 -void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is) -{ - DSTACK(__FUNCTION_NAME); - ClientMapSector *sector = NULL; - - //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out - - core::map::Node *n = m_sectors.find(p2d); - - if(n != NULL) - { - sector = (ClientMapSector*)n->getValue(); - assert(sector->getId() == MAPSECTOR_CLIENT); - } - else - { - sector = new ClientMapSector(this, p2d); - { - //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out - m_sectors.insert(p2d, sector); - } - } - - sector->deSerialize(is); -} -#endif - -void ClientMap::OnRegisterSceneNode() -{ - if(IsVisible) - { - SceneManager->registerNodeForRendering(this, scene::ESNRP_SOLID); - SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT); - } - - ISceneNode::OnRegisterSceneNode(); -} - -void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) -{ - //m_dout<getDayNightRatio(); - - m_camera_mutex.Lock(); - v3f camera_position = m_camera_position; - v3f camera_direction = m_camera_direction; - f32 camera_fov = m_camera_fov; - m_camera_mutex.Unlock(); - - /* - Get all blocks and draw all visible ones - */ - - v3s16 cam_pos_nodes( - camera_position.X / BS, - camera_position.Y / BS, - camera_position.Z / BS); - - v3s16 box_nodes_d = m_control.wanted_range * v3s16(1,1,1); - - v3s16 p_nodes_min = cam_pos_nodes - box_nodes_d; - v3s16 p_nodes_max = cam_pos_nodes + box_nodes_d; - - // Take a fair amount as we will be dropping more out later - v3s16 p_blocks_min( - p_nodes_min.X / MAP_BLOCKSIZE - 2, - p_nodes_min.Y / MAP_BLOCKSIZE - 2, - p_nodes_min.Z / MAP_BLOCKSIZE - 2); - v3s16 p_blocks_max( - p_nodes_max.X / MAP_BLOCKSIZE + 1, - p_nodes_max.Y / MAP_BLOCKSIZE + 1, - p_nodes_max.Z / MAP_BLOCKSIZE + 1); - - u32 vertex_count = 0; - - // For limiting number of mesh updates per frame - u32 mesh_update_count = 0; - - u32 blocks_would_have_drawn = 0; - u32 blocks_drawn = 0; - - int timecheck_counter = 0; - core::map::Iterator si; - si = m_sectors.getIterator(); - for(; si.atEnd() == false; si++) - { - { - timecheck_counter++; - if(timecheck_counter > 50) - { - timecheck_counter = 0; - int time2 = time(0); - if(time2 > time1 + 4) - { - infostream<<"ClientMap::renderMap(): " - "Rendering takes ages, returning." - <getValue(); - v2s16 sp = sector->getPos(); - - if(m_control.range_all == false) - { - if(sp.X < p_blocks_min.X - || sp.X > p_blocks_max.X - || sp.Y < p_blocks_min.Z - || sp.Y > p_blocks_max.Z) - continue; - } - - core::list< MapBlock * > sectorblocks; - sector->getBlocks(sectorblocks); - - /* - Draw blocks - */ - - u32 sector_blocks_drawn = 0; - - core::list< MapBlock * >::Iterator i; - for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++) - { - MapBlock *block = *i; - - /* - Compare block position to camera position, skip - if not seen on display - */ - - float range = 100000 * BS; - if(m_control.range_all == false) - range = m_control.wanted_range * BS; - - float d = 0.0; - if(isBlockInSight(block->getPos(), camera_position, - camera_direction, camera_fov, - range, &d) == false) - { - continue; - } - - // Okay, this block will be drawn. Reset usage timer. - block->resetUsageTimer(); - - // This is ugly (spherical distance limit?) - /*if(m_control.range_all == false && - d - 0.5*BS*MAP_BLOCKSIZE > range) - continue;*/ - -#if 1 - /* - Update expired mesh (used for day/night change) - - It doesn't work exactly like it should now with the - tasked mesh update but whatever. - */ - - bool mesh_expired = false; - - { - JMutexAutoLock lock(block->mesh_mutex); - - mesh_expired = block->getMeshExpired(); - - // Mesh has not been expired and there is no mesh: - // block has no content - if(block->mesh == NULL && mesh_expired == false) - continue; - } - - f32 faraway = BS*50; - //f32 faraway = m_control.wanted_range * BS; - - /* - This has to be done with the mesh_mutex unlocked - */ - // Pretty random but this should work somewhat nicely - if(mesh_expired && ( - (mesh_update_count < 3 - && (d < faraway || mesh_update_count < 2) - ) - || - (m_control.range_all && mesh_update_count < 20) - ) - ) - /*if(mesh_expired && mesh_update_count < 6 - && (d < faraway || mesh_update_count < 3))*/ - { - mesh_update_count++; - - // Mesh has been expired: generate new mesh - //block->updateMesh(daynight_ratio); - m_client->addUpdateMeshTask(block->getPos()); - - mesh_expired = false; - } - -#endif - /* - Draw the faces of the block - */ - { - JMutexAutoLock lock(block->mesh_mutex); - - scene::SMesh *mesh = block->mesh; - - if(mesh == NULL) - continue; - - blocks_would_have_drawn++; - if(blocks_drawn >= m_control.wanted_max_blocks - && m_control.range_all == false - && d > m_control.wanted_min_range * BS) - continue; - - blocks_drawn++; - sector_blocks_drawn++; - - u32 c = mesh->getMeshBufferCount(); - - for(u32 i=0; igetMeshBuffer(i); - const video::SMaterial& material = buf->getMaterial(); - video::IMaterialRenderer* rnd = - driver->getMaterialRenderer(material.MaterialType); - bool transparent = (rnd && rnd->isTransparent()); - // Render transparent on transparent pass and likewise. - if(transparent == is_transparent_pass) - { - /* - This *shouldn't* hurt too much because Irrlicht - doesn't change opengl textures if the old - material is set again. - */ - driver->setMaterial(buf->getMaterial()); - driver->drawMeshBuffer(buf); - vertex_count += buf->getVertexCount(); - } - } - } - } // foreach sectorblocks - - if(sector_blocks_drawn != 0) - { - m_last_drawn_sectors[sp] = true; - } - } - - m_control.blocks_drawn = blocks_drawn; - m_control.blocks_would_have_drawn = blocks_would_have_drawn; - - /*infostream<<"renderMap(): is_transparent_pass="<getBool("free_move") == false) - { - post_effect_color = video::SColor(255, 0, 0, 0); - } - if (post_effect_color.getAlpha() != 0) - { - // Draw a full-screen rectangle - video::IVideoDriver* driver = SceneManager->getVideoDriver(); - v2u32 ss = driver->getScreenSize(); - core::rect rect(0,0, ss.X, ss.Y); - driver->draw2DRectangle(post_effect_color, rect); - } -} - -bool ClientMap::setTempMod(v3s16 p, NodeMod mod, - core::map *affected_blocks) -{ - bool changed = false; - /* - Add it to all blocks touching it - */ - v3s16 dirs[7] = { - v3s16(0,0,0), // this - v3s16(0,0,1), // back - v3s16(0,1,0), // top - v3s16(1,0,0), // right - v3s16(0,0,-1), // front - v3s16(0,-1,0), // bottom - v3s16(-1,0,0), // left - }; - for(u16 i=0; i<7; i++) - { - v3s16 p2 = p + dirs[i]; - // Block position of neighbor (or requested) node - v3s16 blockpos = getNodeBlockPos(p2); - MapBlock * blockref = getBlockNoCreateNoEx(blockpos); - if(blockref == NULL) - continue; - // Relative position of requested node - v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; - if(blockref->setTempMod(relpos, mod)) - { - changed = true; - } - } - if(changed && affected_blocks!=NULL) - { - for(u16 i=0; i<7; i++) - { - v3s16 p2 = p + dirs[i]; - // Block position of neighbor (or requested) node - v3s16 blockpos = getNodeBlockPos(p2); - MapBlock * blockref = getBlockNoCreateNoEx(blockpos); - if(blockref == NULL) - continue; - affected_blocks->insert(blockpos, blockref); - } - } - return changed; -} - -bool ClientMap::clearTempMod(v3s16 p, - core::map *affected_blocks) -{ - bool changed = false; - v3s16 dirs[7] = { - v3s16(0,0,0), // this - v3s16(0,0,1), // back - v3s16(0,1,0), // top - v3s16(1,0,0), // right - v3s16(0,0,-1), // front - v3s16(0,-1,0), // bottom - v3s16(-1,0,0), // left - }; - for(u16 i=0; i<7; i++) - { - v3s16 p2 = p + dirs[i]; - // Block position of neighbor (or requested) node - v3s16 blockpos = getNodeBlockPos(p2); - MapBlock * blockref = getBlockNoCreateNoEx(blockpos); - if(blockref == NULL) - continue; - // Relative position of requested node - v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; - if(blockref->clearTempMod(relpos)) - { - changed = true; - } - } - if(changed && affected_blocks!=NULL) - { - for(u16 i=0; i<7; i++) - { - v3s16 p2 = p + dirs[i]; - // Block position of neighbor (or requested) node - v3s16 blockpos = getNodeBlockPos(p2); - MapBlock * blockref = getBlockNoCreateNoEx(blockpos); - if(blockref == NULL) - continue; - affected_blocks->insert(blockpos, blockref); - } - } - return changed; -} - -void ClientMap::expireMeshes(bool only_daynight_diffed) -{ - TimeTaker timer("expireMeshes()"); - - core::map::Iterator si; - si = m_sectors.getIterator(); - for(; si.atEnd() == false; si++) - { - MapSector *sector = si.getNode()->getValue(); - - core::list< MapBlock * > sectorblocks; - sector->getBlocks(sectorblocks); - - core::list< MapBlock * >::Iterator i; - for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++) - { - MapBlock *block = *i; - - if(only_daynight_diffed && dayNightDiffed(block->getPos()) == false) - { - continue; - } - - { - JMutexAutoLock lock(block->mesh_mutex); - if(block->mesh != NULL) - { - /*block->mesh->drop(); - block->mesh = NULL;*/ - block->setMeshExpired(true); - } - } - } - } -} - -void ClientMap::updateMeshes(v3s16 blockpos, u32 daynight_ratio) -{ - assert(mapType() == MAPTYPE_CLIENT); - - try{ - v3s16 p = blockpos + v3s16(0,0,0); - MapBlock *b = getBlockNoCreate(p); - b->updateMesh(daynight_ratio); - //b->setMeshExpired(true); - } - catch(InvalidPositionException &e){} - // Leading edge - try{ - v3s16 p = blockpos + v3s16(-1,0,0); - MapBlock *b = getBlockNoCreate(p); - b->updateMesh(daynight_ratio); - //b->setMeshExpired(true); - } - catch(InvalidPositionException &e){} - try{ - v3s16 p = blockpos + v3s16(0,-1,0); - MapBlock *b = getBlockNoCreate(p); - b->updateMesh(daynight_ratio); - //b->setMeshExpired(true); - } - catch(InvalidPositionException &e){} - try{ - v3s16 p = blockpos + v3s16(0,0,-1); - MapBlock *b = getBlockNoCreate(p); - b->updateMesh(daynight_ratio); - //b->setMeshExpired(true); - } - catch(InvalidPositionException &e){} -} - -#if 0 -/* - Update mesh of block in which the node is, and if the node is at the - leading edge, update the appropriate leading blocks too. -*/ -void ClientMap::updateNodeMeshes(v3s16 nodepos, u32 daynight_ratio) -{ - v3s16 dirs[4] = { - v3s16(0,0,0), - v3s16(-1,0,0), - v3s16(0,-1,0), - v3s16(0,0,-1), - }; - v3s16 blockposes[4]; - for(u32 i=0; i<4; i++) - { - v3s16 np = nodepos + dirs[i]; - blockposes[i] = getNodeBlockPos(np); - // Don't update mesh of block if it has been done already - bool already_updated = false; - for(u32 j=0; jupdateMesh(daynight_ratio); - } -} -#endif - -void ClientMap::PrintInfo(std::ostream &out) -{ - out<<"ClientMap: "; -} - -#endif // !SERVER - /* MapVoxelManipulator */