X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fmap.cpp;h=8bdc2ad4c97f341e8bffd3faa737cfccdcad97de;hb=cd563473fa298db3b910009f26ba263bacd84be9;hp=febc40ddd98321affbdb33a9d2d9387c7366e1f6;hpb=4e1055543c01af8f17f1ab2e742922575170251e;p=oweals%2Fminetest.git diff --git a/src/map.cpp b/src/map.cpp index febc40ddd..8bdc2ad4c 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -21,20 +21,23 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapsector.h" #include "mapblock.h" #include "main.h" +#ifndef SERVER #include "client.h" +#endif #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<<")" @@ -59,8 +62,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(); @@ -204,6 +208,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); } @@ -230,6 +243,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 @@ -326,19 +341,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); @@ -412,6 +428,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 @@ -470,7 +488,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 @@ -508,7 +526,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); @@ -518,11 +536,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); @@ -571,6 +589,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 @@ -596,8 +616,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; } @@ -620,6 +640,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--) { @@ -638,23 +660,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; } @@ -666,6 +680,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 @@ -844,15 +860,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 @@ -861,11 +877,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"); @@ -901,6 +917,8 @@ void Map::updateLighting(core::map & a_blocks, void Map::addNodeAndUpdate(v3s16 p, MapNode n, core::map &modified_blocks, std::string &player_name) { + 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); } /* @@ -1018,13 +1003,17 @@ 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 \"" + <setOwner(player_name); + setNodeMetadata(p, meta); + } } /* @@ -1034,7 +1023,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, TODO: This could be optimized by mass-unlighting instead of looping */ - if(node_under_sunlight && !content_features(n).sunlight_propagates) + if(node_under_sunlight && !nodemgr->get(n).sunlight_propagates) { s16 y = p.Y - 1; for(;; y--){ @@ -1050,12 +1039,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 @@ -1105,7 +1094,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); } @@ -1121,6 +1110,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); } @@ -1413,9 +1404,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; @@ -1445,6 +1440,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++; } @@ -1460,6 +1456,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, else { all_blocks_deleted = false; + block_count_all++; } } @@ -1480,7 +1477,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()"); @@ -1610,11 +1615,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); @@ -1654,7 +1659,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; @@ -1674,18 +1679,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; @@ -1706,7 +1713,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; @@ -1735,7 +1742,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 @@ -1761,7 +1768,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))) @@ -1771,8 +1778,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 { @@ -1786,14 +1793,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 @@ -1833,7 +1840,7 @@ NodeMetadata* Map::getNodeMetadata(v3s16 p) <m_node_metadata.get(p_rel); + NodeMetadata *meta = block->m_node_metadata->get(p_rel); return meta; } @@ -1853,7 +1860,7 @@ void Map::setNodeMetadata(v3s16 p, NodeMetadata *meta) <m_node_metadata.set(p_rel, meta); + block->m_node_metadata->set(p_rel, meta); } void Map::removeNodeMetadata(v3s16 p) @@ -1867,7 +1874,7 @@ void Map::removeNodeMetadata(v3s16 p) <m_node_metadata.remove(p_rel); + block->m_node_metadata->remove(p_rel); } void Map::nodeMetadataStep(float dtime, @@ -1892,7 +1899,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; } @@ -1903,8 +1910,8 @@ 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), @@ -2011,7 +2018,7 @@ ServerMap::ServerMap(std::string savedir): emergeSector(v2s16(0,0)); // Initially write whole map - save(false); + save(MOD_STATE_CLEAN); } ServerMap::~ServerMap() @@ -2023,7 +2030,7 @@ ServerMap::~ServerMap() if(m_map_saving_enabled) { // Save only changed parts - save(true); + save(MOD_STATE_WRITE_AT_UNLOAD); infostream<<"Server: saved map to "<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 @@ -2161,7 +2169,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 @@ -2300,7 +2318,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"); } /* @@ -2312,7 +2331,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); } } @@ -2823,7 +2839,7 @@ std::string ServerMap::getBlockFilename(v3s16 p) return cc; } -void ServerMap::save(bool only_changed) +void ServerMap::save(ModifiedState save_level) { DSTACK(__FUNCTION_NAME); if(m_map_saving_enabled == false) @@ -2832,27 +2848,32 @@ void ServerMap::save(bool only_changed) return; } - if(only_changed == false) + if(save_level == MOD_STATE_CLEAN) infostream<<"ServerMap: Saving whole map, this can take time." <::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++; @@ -2861,16 +2882,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++; @@ -2880,15 +2907,15 @@ void ServerMap::save(bool only_changed) <getPos().Z<<")" <resetModified(); @@ -3519,12 +3549,13 @@ void ServerMap::PrintInfo(std::ostream &out) ClientMap::ClientMap( Client *client, + IGameDef *gamedef, MapDrawControl &control, scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id ): - Map(dout_client), + Map(dout_client, gamedef), scene::ISceneNode(parent, mgr, id), m_client(client), m_control(control), @@ -3562,7 +3593,7 @@ MapSector * ClientMap::emergeSector(v2s16 p2d) } // Create a sector - ClientMapSector *sector = new ClientMapSector(this, p2d); + ClientMapSector *sector = new ClientMapSector(this, p2d, m_gamedef); { //JMutexAutoLock lock(m_sector_mutex); // Bulk comment-out @@ -3611,13 +3642,50 @@ void ClientMap::OnRegisterSceneNode() ISceneNode::OnRegisterSceneNode(); } +static bool isOccluded(Map *map, v3s16 p0, v3s16 p1, float step, float stepfac, + float start_off, float end_off, u32 needed_count, INodeDefManager *nodemgr) +{ + float d0 = (float)BS * p0.getDistanceFrom(p1); + v3s16 u0 = p1 - p0; + v3f uf = v3f(u0.X, u0.Y, u0.Z) * BS; + uf.normalize(); + v3f p0f = v3f(p0.X, p0.Y, p0.Z) * BS; + u32 count = 0; + for(float s=start_off; sgetNodeNoEx(p); + bool is_transparent = false; + const ContentFeatures &f = nodemgr->get(n); + if(f.solidness == 0) + is_transparent = (f.visual_solidness != 2); + else + is_transparent = (f.solidness != 2); + if(!is_transparent){ + count++; + if(count >= needed_count) + return true; + } + step *= stepfac; + } + return false; +} + void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) { + INodeDefManager *nodemgr = m_gamedef->ndef(); + //m_dout<::Iterator si; - si = m_sectors.getIterator(); - for(; si.atEnd() == false; si++) + /* + Collect a set of blocks for drawing + */ + + core::map drawset; + { - { - timecheck_counter++; - if(timecheck_counter > 50) - { - timecheck_counter = 0; - int time2 = time(0); - if(time2 > time1 + 4) - { - infostream<<"ClientMap::renderMap(): " - "Rendering takes ages, returning." - <::Iterator + si = m_sectors.getIterator(); + si.atEnd() == false; si++) + { MapSector *sector = si.getNode()->getValue(); v2s16 sp = sector->getPos(); @@ -3724,11 +3783,11 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) sector->getBlocks(sectorblocks); /* - Draw blocks + Loop through blocks in sector */ - - u32 sector_blocks_drawn = 0; + u32 sector_blocks_drawn = 0; + core::list< MapBlock * >::Iterator i; for(i=sectorblocks.begin(); i!=sectorblocks.end(); i++) { @@ -3742,7 +3801,7 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) 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, @@ -3751,16 +3810,13 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) 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;*/ - - blocks_in_range++; + blocks_in_range++; + #if 1 /* Update expired mesh (used for day/night change) @@ -3810,10 +3866,51 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) mesh_expired = false; } - #endif + + /* + Occlusion culling + */ + + v3s16 cpn = block->getPos() * MAP_BLOCKSIZE; + cpn += v3s16(MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2, MAP_BLOCKSIZE/2); + float step = BS*1; + float stepfac = 1.1; + float startoff = BS*1; + float endoff = -BS*MAP_BLOCKSIZE*1.42*1.42; + v3s16 spn = cam_pos_nodes + v3s16(0,0,0); + s16 bs2 = MAP_BLOCKSIZE/2 + 1; + u32 needed_count = 1; + if( + isOccluded(this, spn, cpn + v3s16(0,0,0), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(bs2,bs2,bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(bs2,bs2,-bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(bs2,-bs2,bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(bs2,-bs2,-bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(-bs2,bs2,bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(-bs2,bs2,-bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) && + isOccluded(this, spn, cpn + v3s16(-bs2,-bs2,-bs2), + step, stepfac, startoff, endoff, needed_count, nodemgr) + ) + { + blocks_occlusion_culled++; + continue; + } + + // This block is in range. Reset usage timer. + block->resetUsageTimer(); + /* - Draw the faces of the block + Ignore if mesh doesn't exist */ { JMutexAutoLock lock(block->mesh_mutex); @@ -3824,72 +3921,112 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) blocks_in_range_without_mesh++; 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; + } + + // Limit block count in case of a sudden increase + 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; + + // Add to set + drawset[block->getPos()] = block; + + sector_blocks_drawn++; + blocks_drawn++; + + } // foreach sectorblocks + + if(sector_blocks_drawn != 0) + m_last_drawn_sectors[sp] = true; + } + } // ScopeProfiler + + /* + Draw the selected MapBlocks + */ - blocks_drawn++; - sector_blocks_drawn++; + { + ScopeProfiler sp(g_profiler, prefix+"drawing blocks", SPT_AVG); - u32 c = mesh->getMeshBufferCount(); - bool stuff_actually_drawn = false; - for(u32 i=0; i::Iterator + i = drawset.getIterator(); + i.atEnd() == false; i++) + { + { + timecheck_counter++; + if(timecheck_counter > 50) + { + timecheck_counter = 0; + int time2 = time(0); + if(time2 > time1 + 4) { - scene::IMeshBuffer *buf = mesh->getMeshBuffer(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) - { - if(buf->getVertexCount() == 0) - errorstream<<"Block ["<setMaterial(buf->getMaterial()); - driver->drawMeshBuffer(buf); - vertex_count += buf->getVertexCount(); - meshbuffer_count++; - stuff_actually_drawn = true; - } + infostream<<"ClientMap::renderMap(): " + "Rendering takes ages, returning." + <getValue(); - if(sector_blocks_drawn != 0) + /* + Draw the faces of the block + */ { - m_last_drawn_sectors[sp] = true; + JMutexAutoLock lock(block->mesh_mutex); + + scene::SMesh *mesh = block->mesh; + assert(mesh); + + u32 c = mesh->getMeshBufferCount(); + bool stuff_actually_drawn = false; + 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) + { + if(buf->getVertexCount() == 0) + errorstream<<"Block ["<setMaterial(buf->getMaterial()); + driver->drawMeshBuffer(buf); + vertex_count += buf->getVertexCount(); + meshbuffer_count++; + stuff_actually_drawn = true; + } + } + if(stuff_actually_drawn) + blocks_had_pass_meshbuf++; + else + blocks_without_stuff++; } } + } // ScopeProfiler - std::string prefix = "CM: "; - // Log only on solid pass because values are the same if(pass == scene::ESNRP_SOLID){ - g_profiler->avg(prefix+"blocks in range", blocks_in_range); + g_profiler->avg("CM: blocks in range", blocks_in_range); + g_profiler->avg("CM: blocks occlusion culled", blocks_occlusion_culled); if(blocks_in_range != 0) - g_profiler->avg(prefix+"blocks in range without mesh (frac)", + g_profiler->avg("CM: blocks in range without mesh (frac)", (float)blocks_in_range_without_mesh/blocks_in_range); - g_profiler->avg(prefix+"blocks drawn", blocks_drawn); + g_profiler->avg("CM: blocks drawn", blocks_drawn); } - if(pass == scene::ESNRP_SOLID) - prefix = "CM: solid: "; - else - prefix = "CM: transparent: "; - g_profiler->avg(prefix+"vertices drawn", vertex_count); if(blocks_had_pass_meshbuf != 0) g_profiler->avg(prefix+"meshbuffers per block", @@ -3907,6 +4044,8 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass) void ClientMap::renderPostFx() { + INodeDefManager *nodemgr = m_gamedef->ndef(); + // Sadly ISceneManager has no "post effects" render pass, in that case we // could just register for that and handle it in renderMap(). @@ -3918,7 +4057,7 @@ void ClientMap::renderPostFx() // - If the player is in a solid node, make everything black. // - If the player is in liquid, draw a semi-transparent overlay. - ContentFeatures& features = content_features(n); + const ContentFeatures& features = nodemgr->get(n); video::SColor post_effect_color = features.post_effect_color; if(features.solidness == 2 && g_settings->getBool("free_move") == false) {