- // If bottom is valid, we're done.
- if(bottom_valid)
- break;
- }
- else if(bank == LIGHTBANK_NIGHT)
- {
- // For night lighting, sunlight is not propagated
- break;
- }
- else
- {
- // Invalid lighting bank
- assert(0);
- }
-
- /*infostream<<"Bottom for sunlight-propagated block ("
- <<pos.X<<","<<pos.Y<<","<<pos.Z<<") not valid"
- <<std::endl;*/
-
- // Bottom sunlight is not valid; get the block and loop to it
-
- pos.Y--;
- try{
- block = getBlockNoCreate(pos);
- }
- catch(InvalidPositionException &e)
- {
- assert(0);
- }
-
- }
- }
-
- }
-
- /*
- Enable this to disable proper lighting for speeding up map
- generation for testing or whatever
- */
-#if 0
- //if(g_settings->get(""))
- {
- core::map<v3s16, MapBlock*>::Iterator i;
- i = blocks_to_update.getIterator();
- for(; i.atEnd() == false; i++)
- {
- MapBlock *block = i.getNode()->getValue();
- v3s16 p = block->getPos();
- block->setLightingExpired(false);
- }
- return;
- }
-#endif
-
-#if 1
- {
- //TimeTaker timer("unspreadLight");
- unspreadLight(bank, unlight_from, light_sources, modified_blocks);
- }
-
- /*if(debug)
- {
- u32 diff = modified_blocks.size() - count_was;
- count_was = modified_blocks.size();
- infostream<<"unspreadLight modified "<<diff<<std::endl;
- }*/
-
- {
- //TimeTaker timer("spreadLight");
- spreadLight(bank, light_sources, modified_blocks);
- }
-
- /*if(debug)
- {
- u32 diff = modified_blocks.size() - count_was;
- count_was = modified_blocks.size();
- infostream<<"spreadLight modified "<<diff<<std::endl;
- }*/
-#endif
-
-#if 0
- {
- //MapVoxelManipulator vmanip(this);
-
- // Make a manual voxel manipulator and load all the blocks
- // that touch the requested blocks
- ManualMapVoxelManipulator vmanip(this);
-
- {
- //TimeTaker timer("initialEmerge");
-
- core::map<v3s16, MapBlock*>::Iterator i;
- i = blocks_to_update.getIterator();
- for(; i.atEnd() == false; i++)
- {
- MapBlock *block = i.getNode()->getValue();
- v3s16 p = block->getPos();
-
- // Add all surrounding blocks
- vmanip.initialEmerge(p - v3s16(1,1,1), p + v3s16(1,1,1));
-
- /*
- Add all surrounding blocks that have up-to-date lighting
- NOTE: This doesn't quite do the job (not everything
- appropriate is lighted)
- */
- /*for(s16 z=-1; z<=1; z++)
- for(s16 y=-1; y<=1; y++)
- for(s16 x=-1; x<=1; x++)
- {
- 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(p2, p2);
- }*/
-
- // Lighting of block will be updated completely
- block->setLightingExpired(false);
- }
- }
-
- {
- //TimeTaker timer("unSpreadLight");
- vmanip.unspreadLight(bank, unlight_from, light_sources, nodemgr);
- }
- {
- //TimeTaker timer("spreadLight");
- vmanip.spreadLight(bank, light_sources, nodemgr);
- }
- {
- //TimeTaker timer("blitBack");
- vmanip.blitBack(modified_blocks);
- }
- /*infostream<<"emerge_time="<<emerge_time<<std::endl;
- emerge_time = 0;*/
- }
-#endif
-
- //m_dout<<"Done ("<<getTimestamp()<<")"<<std::endl;
-}
-
-void Map::updateLighting(std::map<v3s16, MapBlock*> & a_blocks,
- std::map<v3s16, MapBlock*> & modified_blocks)
-{
- updateLighting(LIGHTBANK_DAY, a_blocks, modified_blocks);
- updateLighting(LIGHTBANK_NIGHT, a_blocks, modified_blocks);
-
- /*
- Update information about whether day and night light differ
- */
- for(std::map<v3s16, MapBlock*>::iterator
- i = modified_blocks.begin();
- i != modified_blocks.end(); ++i)
- {
- MapBlock *block = i->second;
- block->expireDayNightDiff();
- }
-}
-
-/*
-*/
-void Map::addNodeAndUpdate(v3s16 p, MapNode n,
- std::map<v3s16, MapBlock*> &modified_blocks,
- bool remove_metadata)
-{
- INodeDefManager *ndef = m_gamedef->ndef();
-
- /*PrintInfo(m_dout);
- m_dout<<DTIME<<"Map::addNodeAndUpdate(): p=("
- <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
-
- /*
- From this node to nodes underneath:
- If lighting is sunlight (1.0), unlight neighbours and
- set lighting to 0.
- Else discontinue.
- */
-
- v3s16 toppos = p + v3s16(0,1,0);
- //v3s16 bottompos = p + v3s16(0,-1,0);
-
- bool node_under_sunlight = true;
- std::set<v3s16> light_sources;
-
- /*
- Collect old node for rollback
- */
- RollbackNode rollback_oldnode(this, p, m_gamedef);
-
- /*
- If there is a node at top and it doesn't have sunlight,
- there has not been any sunlight going down.
-
- Otherwise there probably is.
- */
- try{
- MapNode topnode = getNode(toppos);
-
- if(topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN)
- node_under_sunlight = false;
- }
- catch(InvalidPositionException &e)
- {
- }
-
- /*
- Remove all light that has come out of this node
- */
-
- enum LightBank banks[] =
- {
- LIGHTBANK_DAY,
- LIGHTBANK_NIGHT
- };
- for(s32 i=0; i<2; i++)
- {
- enum LightBank bank = banks[i];
-
- u8 lightwas = getNode(p).getLight(bank, ndef);
-
- // Add the block of the added node to modified_blocks
- v3s16 blockpos = getNodeBlockPos(p);
- MapBlock * block = getBlockNoCreate(blockpos);
- assert(block != NULL);
- modified_blocks[blockpos] = block;
-
- assert(isValidPosition(p));
-
- // Unlight neighbours of node.
- // This means setting light of all consequent dimmer nodes
- // to 0.
- // This also collects the nodes at the border which will spread
- // light again into this.
- unLightNeighbors(bank, p, lightwas, light_sources, modified_blocks);
-
- n.setLight(bank, 0, ndef);
- }
-
- /*
- If node lets sunlight through and is under sunlight, it has
- sunlight too.
- */
- if(node_under_sunlight && ndef->get(n).sunlight_propagates)
- {
- n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
- }
-
- /*
- Remove node metadata
- */
- if (remove_metadata) {
- removeNodeMetadata(p);
- }
-
- /*
- Set the node on the map
- */
-
- setNode(p, n);
-
- /*
- If node is under sunlight and doesn't let sunlight through,
- take all sunlighted nodes under it and clear light from them
- and from where the light has been spread.
- TODO: This could be optimized by mass-unlighting instead
- of looping
- */
- if(node_under_sunlight && !ndef->get(n).sunlight_propagates)
- {
- s16 y = p.Y - 1;
- for(;; y--){
- //m_dout<<DTIME<<"y="<<y<<std::endl;
- v3s16 n2pos(p.X, y, p.Z);
-
- MapNode n2;
- try{
- n2 = getNode(n2pos);
- }
- catch(InvalidPositionException &e)
- {
- break;
- }
-
- if(n2.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN)
- {
- unLightNeighbors(LIGHTBANK_DAY,
- n2pos, n2.getLight(LIGHTBANK_DAY, ndef),
- light_sources, modified_blocks);
- n2.setLight(LIGHTBANK_DAY, 0, ndef);
- setNode(n2pos, n2);
- }
- else
- break;
- }
- }
-
- for(s32 i=0; i<2; i++)
- {
- enum LightBank bank = banks[i];
-
- /*
- Spread light from all nodes that might be capable of doing so
- */
- spreadLight(bank, light_sources, modified_blocks);
- }
-
- /*
- Update information about whether day and night light differ
- */
- for(std::map<v3s16, MapBlock*>::iterator
- i = modified_blocks.begin();
- i != modified_blocks.end(); ++i)
- {
- i->second->expireDayNightDiff();
- }
-
- /*
- Report for rollback
- */
- if(m_gamedef->rollback())
- {
- RollbackNode rollback_newnode(this, p, m_gamedef);
- RollbackAction action;
- action.setSetNode(p, rollback_oldnode, rollback_newnode);
- m_gamedef->rollback()->reportAction(action);
- }
-
- /*
- Add neighboring liquid nodes and the node itself if it is
- liquid (=water node was added) to transform queue.
- */
- v3s16 dirs[7] = {
- v3s16(0,0,0), // self
- 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++)
- {
- try
- {
-
- v3s16 p2 = p + dirs[i];
-
- MapNode n2 = getNode(p2);
- if(ndef->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR)
- {
- m_transforming_liquid.push_back(p2);
- }
-
- }catch(InvalidPositionException &e)
- {
- }
- }
-}
-
-/*
-*/
-void Map::removeNodeAndUpdate(v3s16 p,
- std::map<v3s16, MapBlock*> &modified_blocks)
-{
- INodeDefManager *ndef = m_gamedef->ndef();
-
- /*PrintInfo(m_dout);
- m_dout<<DTIME<<"Map::removeNodeAndUpdate(): p=("
- <<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;*/
-
- bool node_under_sunlight = true;
-
- v3s16 toppos = p + v3s16(0,1,0);
-
- // Node will be replaced with this
- content_t replace_material = CONTENT_AIR;
-
- /*
- Collect old node for rollback
- */
- RollbackNode rollback_oldnode(this, p, m_gamedef);
-
- /*
- If there is a node at top and it doesn't have sunlight,
- there will be no sunlight going down.
- */
- try{
- MapNode topnode = getNode(toppos);
-
- if(topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN)
- node_under_sunlight = false;
- }
- catch(InvalidPositionException &e)
- {
- }
-
- std::set<v3s16> light_sources;
-
- enum LightBank banks[] =
- {
- LIGHTBANK_DAY,
- LIGHTBANK_NIGHT
- };
- for(s32 i=0; i<2; i++)
- {
- enum LightBank bank = banks[i];
-
- /*
- Unlight neighbors (in case the node is a light source)
- */
- unLightNeighbors(bank, p,
- getNode(p).getLight(bank, ndef),
- light_sources, modified_blocks);
- }
-
- /*
- Remove node metadata
- */
-
- removeNodeMetadata(p);
-
- /*
- Remove the node.
- This also clears the lighting.
- */
-
- MapNode n;
- n.setContent(replace_material);
- setNode(p, n);
-
- for(s32 i=0; i<2; i++)
- {
- enum LightBank bank = banks[i];
-
- /*
- Recalculate lighting
- */
- spreadLight(bank, light_sources, modified_blocks);
- }
-
- // Add the block of the removed node to modified_blocks
- v3s16 blockpos = getNodeBlockPos(p);
- MapBlock * block = getBlockNoCreate(blockpos);
- assert(block != NULL);
- modified_blocks[blockpos] = block;
-
- /*
- If the removed node was under sunlight, propagate the
- sunlight down from it and then light all neighbors
- of the propagated blocks.
- */
- if(node_under_sunlight)
- {
- s16 ybottom = propagateSunlight(p, modified_blocks);
- /*m_dout<<DTIME<<"Node was under sunlight. "
- "Propagating sunlight";
- m_dout<<DTIME<<" -> ybottom="<<ybottom<<std::endl;*/
- s16 y = p.Y;
- for(; y >= ybottom; y--)
- {
- v3s16 p2(p.X, y, p.Z);
- /*m_dout<<DTIME<<"lighting neighbors of node ("
- <<p2.X<<","<<p2.Y<<","<<p2.Z<<")"
- <<std::endl;*/
- lightNeighbors(LIGHTBANK_DAY, p2, modified_blocks);
- }
- }
- else
- {
- // Set the lighting of this node to 0
- // TODO: Is this needed? Lighting is cleared up there already.
- try{
- MapNode n = getNode(p);
- n.setLight(LIGHTBANK_DAY, 0, ndef);
- setNode(p, n);
- }
- catch(InvalidPositionException &e)
- {
- assert(0);
- }
- }
-
- for(s32 i=0; i<2; i++)
- {
- enum LightBank bank = banks[i];
-
- // Get the brightest neighbour node and propagate light from it
- v3s16 n2p = getBrightestNeighbour(bank, p);
- try{
- //MapNode n2 = getNode(n2p);
- lightNeighbors(bank, n2p, modified_blocks);
- }
- catch(InvalidPositionException &e)
- {
- }