From 5b8855e83c0d1cc7aef21492e7fe862b7d06917e Mon Sep 17 00:00:00 2001 From: Craig Robbins Date: Fri, 14 Nov 2014 18:05:34 +1000 Subject: [PATCH] Remove most exceptions from getNode() (and variants) --- src/collision.cpp | 11 +- src/content_cso.cpp | 13 +- src/environment.cpp | 36 +-- src/game.cpp | 44 ++-- src/localplayer.cpp | 105 +++++---- src/map.cpp | 434 +++++++++++++++++------------------ src/map.h | 7 +- src/mapblock.cpp | 106 +++------ src/mapblock.h | 55 ++--- src/particles.cpp | 21 +- src/script/lua_api/l_env.cpp | 23 +- src/server.cpp | 29 ++- 12 files changed, 429 insertions(+), 455 deletions(-) diff --git a/src/collision.cpp b/src/collision.cpp index edbee40b..9e0c8553 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -251,9 +251,13 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, for(s16 z = min_z; z <= max_z; z++) { v3s16 p(x,y,z); - try{ + + bool is_position_valid; + MapNode n = map->getNodeNoEx(p, &is_position_valid); + + if (is_position_valid) { // Object collides into walkable nodes - MapNode n = map->getNode(p); + const ContentFeatures &f = gamedef->getNodeDefManager()->get(n); if(f.walkable == false) continue; @@ -275,8 +279,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, is_object.push_back(false); } } - catch(InvalidPositionException &e) - { + else { // Collide with unloaded nodes aabb3f box = getNodeBox(p, BS); cboxes.push_back(box); diff --git a/src/content_cso.cpp b/src/content_cso.cpp index 4f652a8a..4779b20d 100644 --- a/src/content_cso.cpp +++ b/src/content_cso.cpp @@ -60,13 +60,12 @@ public: m_spritenode->setVisible(true); m_spritenode->setSize(size); /* Update brightness */ - u8 light = 64; - try{ - MapNode n = env->getMap().getNode(floatToInt(pos, BS)); - light = decode_light(n.getLightBlend(env->getDayNightRatio(), - env->getGameDef()->ndef())); - } - catch(InvalidPositionException &e){} + u8 light; + bool pos_ok; + MapNode n = env->getMap().getNodeNoEx(floatToInt(pos, BS), &pos_ok); + light = pos_ok ? decode_light(n.getLightBlend(env->getDayNightRatio(), + env->getGameDef()->ndef())) + : 64; video::SColor color(255,light,light,light); m_spritenode->setColor(color); } diff --git a/src/environment.cpp b/src/environment.cpp index 6e5305b1..3d525dad 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -2342,10 +2342,8 @@ void ClientEnvironment::step(float dtime) // (day: LIGHT_SUN, night: 0) MapNode node_at_lplayer(CONTENT_AIR, 0x0f, 0); - try { - v3s16 p = lplayer->getLightPosition(); - node_at_lplayer = m_map->getNode(p); - } catch (InvalidPositionException &e) {} + v3s16 p = lplayer->getLightPosition(); + node_at_lplayer = m_map->getNodeNoEx(p); u16 light = getInteriorLight(node_at_lplayer, 0, m_gamedef->ndef()); u8 day = light & 0xff; @@ -2371,15 +2369,16 @@ void ClientEnvironment::step(float dtime) { // Update lighting u8 light = 0; - try{ - // Get node at head - v3s16 p = obj->getLightPosition(); - MapNode n = m_map->getNode(p); + bool pos_ok; + + // Get node at head + v3s16 p = obj->getLightPosition(); + MapNode n = m_map->getNodeNoEx(p, &pos_ok); + if (pos_ok) light = n.getLightBlend(day_night_ratio, m_gamedef->ndef()); - } - catch(InvalidPositionException &e){ + else light = blend_light(day_night_ratio, LIGHT_SUN, 0); - } + obj->updateLight(light); } } @@ -2470,15 +2469,16 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object) object->addToScene(m_smgr, m_texturesource, m_irr); { // Update lighting immediately u8 light = 0; - try{ - // Get node at head - v3s16 p = object->getLightPosition(); - MapNode n = m_map->getNode(p); + bool pos_ok; + + // Get node at head + v3s16 p = object->getLightPosition(); + MapNode n = m_map->getNodeNoEx(p, &pos_ok); + if (pos_ok) light = n.getLightBlend(getDayNightRatio(), m_gamedef->ndef()); - } - catch(InvalidPositionException &e){ + else light = blend_light(getDayNightRatio(), LIGHT_SUN, 0); - } + object->updateLight(light); } return object->getId(); diff --git a/src/game.cpp b/src/game.cpp index 928c5b38..2c969b93 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -360,12 +360,11 @@ PointedThing getPointedThing(Client *client, v3f player_position, for (s16 z = zstart; z <= zend; z++) for (s16 x = xstart; x <= xend; x++) { MapNode n; + bool is_valid_position; - try { - n = map.getNode(v3s16(x, y, z)); - } catch (InvalidPositionException &e) { + n = map.getNodeNoEx(v3s16(x, y, z), &is_valid_position); + if (!is_valid_position) continue; - } if (!isPointableNode(n, client, liquids_pointable)) continue; @@ -873,22 +872,31 @@ bool nodePlacementPrediction(Client &client, std::string prediction = playeritem_def.node_placement_prediction; INodeDefManager *nodedef = client.ndef(); ClientMap &map = client.getEnv().getClientMap(); + MapNode node; + bool is_valid_position; - if (prediction != "" && !nodedef->get(map.getNode(nodepos)).rightclickable) { + node = map.getNodeNoEx(nodepos, &is_valid_position); + if (!is_valid_position) + return false; + + if (prediction != "" && !nodedef->get(node).rightclickable) { verbosestream << "Node placement prediction for " << playeritem_def.name << " is " << prediction << std::endl; v3s16 p = neighbourpos; // Place inside node itself if buildable_to - try { - MapNode n_under = map.getNode(nodepos); - + MapNode n_under = map.getNodeNoEx(nodepos, &is_valid_position); + if (is_valid_position) + { if (nodedef->get(n_under).buildable_to) p = nodepos; - else if (!nodedef->get(map.getNode(p)).buildable_to) - return false; - } catch (InvalidPositionException &e) {} + else { + node = map.getNodeNoEx(p, &is_valid_position); + if (is_valid_position &&!nodedef->get(node).buildable_to) + return false; + } + } // Find id of predicted node content_t id; @@ -946,7 +954,7 @@ bool nodePlacementPrediction(Client &client, else pp = p + v3s16(0, -1, 0); - if (!nodedef->get(map.getNode(pp)).walkable) + if (!nodedef->get(map.getNodeNoEx(pp)).walkable) return false; } @@ -3431,7 +3439,7 @@ void Game::handlePointingAtNode(GameRunData *runData, if (meta) { infotext = narrow_to_wide(meta->getString("infotext")); } else { - MapNode n = map.getNode(nodepos); + MapNode n = map.getNodeNoEx(nodepos); if (nodedef_manager->get(n).tiledef[0].name == "unknown_node.png") { infotext = L"Unknown node: "; @@ -3489,7 +3497,7 @@ void Game::handlePointingAtNode(GameRunData *runData, } if (playeritem_def.node_placement_prediction == "" || - nodedef_manager->get(map.getNode(nodepos)).rightclickable) + nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable) client->interact(3, pointed); // Report to server } } @@ -3558,7 +3566,7 @@ void Game::handleDigging(GameRunData *runData, LocalPlayer *player = client->getEnv().getLocalPlayer(); ClientMap &map = client->getEnv().getClientMap(); - MapNode n = client->getEnv().getClientMap().getNode(nodepos); + MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos); // NOTE: Similar piece of code exists on the server side for // cheat detection. @@ -3623,8 +3631,10 @@ void Game::handleDigging(GameRunData *runData, infostream << "Digging completed" << std::endl; client->interact(2, pointed); client->setCrack(-1, v3s16(0, 0, 0)); - MapNode wasnode = map.getNode(nodepos); - client->removeNode(nodepos); + bool is_valid_position; + MapNode wasnode = map.getNodeNoEx(nodepos, &is_valid_position); + if (is_valid_position) + client->removeNode(nodepos); if (g_settings->getBool("enable_particles")) { const ContentFeatures &features = diff --git a/src/localplayer.cpp b/src/localplayer.cpp index bdcf45be..b36bef4d 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -101,39 +101,48 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d, Collision detection */ + bool is_valid_position; + MapNode node; + v3s16 pp; + /* Check if player is in liquid (the oscillating value) */ - try{ - // If in liquid, the threshold of coming out is at higher y - if(in_liquid) - { - v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS); - in_liquid = nodemgr->get(map->getNode(pp).getContent()).isLiquid(); - liquid_viscosity = nodemgr->get(map->getNode(pp).getContent()).liquid_viscosity; - } - // If not in liquid, the threshold of going in is at lower y - else - { - v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS); - in_liquid = nodemgr->get(map->getNode(pp).getContent()).isLiquid(); - liquid_viscosity = nodemgr->get(map->getNode(pp).getContent()).liquid_viscosity; - } - } - catch(InvalidPositionException &e) + + // If in liquid, the threshold of coming out is at higher y + if (in_liquid) { - in_liquid = false; + pp = floatToInt(position + v3f(0,BS*0.1,0), BS); + node = map->getNodeNoEx(pp, &is_valid_position); + if (is_valid_position) { + in_liquid = nodemgr->get(node.getContent()).isLiquid(); + liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity; + } else { + in_liquid = false; + } } + // If not in liquid, the threshold of going in is at lower y + else + { + pp = floatToInt(position + v3f(0,BS*0.5,0), BS); + node = map->getNodeNoEx(pp, &is_valid_position); + if (is_valid_position) { + in_liquid = nodemgr->get(node.getContent()).isLiquid(); + liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity; + } else { + in_liquid = false; + } + } + /* Check if player is in liquid (the stable value) */ - try{ - v3s16 pp = floatToInt(position + v3f(0,0,0), BS); - in_liquid_stable = nodemgr->get(map->getNode(pp).getContent()).isLiquid(); - } - catch(InvalidPositionException &e) - { + pp = floatToInt(position + v3f(0,0,0), BS); + node = map->getNodeNoEx(pp, &is_valid_position); + if (is_valid_position) { + in_liquid_stable = nodemgr->get(node.getContent()).isLiquid(); + } else { in_liquid_stable = false; } @@ -141,17 +150,21 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d, Check if player is climbing */ - try { - v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS); - v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS); - is_climbing = ((nodemgr->get(map->getNode(pp).getContent()).climbable || - nodemgr->get(map->getNode(pp2).getContent()).climbable) && !free_move); - } - catch(InvalidPositionException &e) - { + + pp = floatToInt(position + v3f(0,0.5*BS,0), BS); + v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS); + node = map->getNodeNoEx(pp, &is_valid_position); + bool is_valid_position2; + MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2); + + if (!(is_valid_position && is_valid_position2)) { is_climbing = false; + } else { + is_climbing = (nodemgr->get(node.getContent()).climbable + || nodemgr->get(node2.getContent()).climbable) && !free_move; } + /* Collision uncertainty radius Make it a bit larger than the maximum distance of movement @@ -264,22 +277,20 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d, max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS) continue; - try{ - // The node to be sneaked on has to be walkable - if(nodemgr->get(map->getNode(p)).walkable == false) - continue; - // And the node above it has to be nonwalkable - if(nodemgr->get(map->getNode(p+v3s16(0,1,0))).walkable == true) { - continue; - } - if (!physics_override_sneak_glitch) { - if (nodemgr->get(map->getNode(p+v3s16(0,2,0))).walkable) - continue; - } - } - catch(InvalidPositionException &e) - { + + // The node to be sneaked on has to be walkable + node = map->getNodeNoEx(p, &is_valid_position); + if (!is_valid_position || nodemgr->get(node).walkable == false) continue; + // And the node above it has to be nonwalkable + node = map->getNodeNoEx(p + v3s16(0,1,0), &is_valid_position); + if (!is_valid_position || nodemgr->get(node).walkable) { + continue; + } + if (!physics_override_sneak_glitch) { + node =map->getNodeNoEx(p + v3s16(0,2,0), &is_valid_position); + if (!is_valid_position || nodemgr->get(node).walkable) + continue; } min_distance_f = distance_f; diff --git a/src/map.cpp b/src/map.cpp index 7903f305..4b0d2e2d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -186,26 +186,42 @@ bool Map::isValidPosition(v3s16 p) } // Returns a CONTENT_IGNORE node if not found -MapNode Map::getNodeNoEx(v3s16 p) +MapNode Map::getNodeNoEx(v3s16 p, bool *is_valid_position) { v3s16 blockpos = getNodeBlockPos(p); MapBlock *block = getBlockNoCreateNoEx(blockpos); - if(block == NULL) + if (block == NULL) { + if (is_valid_position != NULL) + *is_valid_position = false; return MapNode(CONTENT_IGNORE); + } + v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; - return block->getNodeNoCheck(relpos); + bool is_valid_p; + MapNode node = block->getNodeNoCheck(relpos, &is_valid_p); + if (is_valid_position != NULL) + *is_valid_position = is_valid_p; + return node; } +#if 0 +// Deprecated // throws InvalidPositionException if not found +// TODO: Now this is deprecated, getNodeNoEx should be renamed MapNode Map::getNode(v3s16 p) { v3s16 blockpos = getNodeBlockPos(p); MapBlock *block = getBlockNoCreateNoEx(blockpos); - if(block == NULL) + if (block == NULL) throw InvalidPositionException(); v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; - return block->getNodeNoCheck(relpos); + bool is_valid_position; + MapNode node = block->getNodeNoCheck(relpos, &is_valid_position); + if (!is_valid_position) + throw InvalidPositionException(); + return node; } +#endif // throws InvalidPositionException if not found void Map::setNode(v3s16 p, MapNode & n) @@ -215,9 +231,10 @@ void Map::setNode(v3s16 p, MapNode & n) v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; // Never allow placing CONTENT_IGNORE, it fucks up stuff if(n.getContent() == CONTENT_IGNORE){ + bool temp_bool; errorstream<<"Map::setNode(): Not allowing to place CONTENT_IGNORE" <<" while trying to replace \"" - <ndef()->get(block->getNodeNoCheck(relpos)).name + <ndef()->get(block->getNodeNoCheck(relpos, &temp_bool)).name <<"\" at "<getNode(relpos); - - bool changed = false; - - //TODO: Optimize output by optimizing light_sources? - - /* - If the neighbor is dimmer than what was specified - as oldlight (the light of the previous node) - */ - if(n2.getLight(bank, nodemgr) < oldlight) - { - /* - And the neighbor is transparent and it has some light - */ - 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, nodemgr); - n2.setLight(bank, 0, nodemgr); - block->setNode(relpos, n2); - - unlighted_nodes[n2pos] = current_light; - changed = true; - - /* - Remove from light_sources if it is there - NOTE: This doesn't happen nearly at all - */ - /*if(light_sources.find(n2pos)) - { - infostream<<"Removed from light_sources"<getNode(relpos, &is_valid_position); + if (!is_valid_position) + continue; + + bool changed = false; + + //TODO: Optimize output by optimizing light_sources? + + /* + If the neighbor is dimmer than what was specified + as oldlight (the light of the previous node) + */ + if(n2.getLight(bank, nodemgr) < oldlight) + { + /* + And the neighbor is transparent and it has some light + */ + 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, nodemgr); + n2.setLight(bank, 0, nodemgr); + block->setNode(relpos, n2); + + unlighted_nodes[n2pos] = current_light; + changed = true; + + /* + Remove from light_sources if it is there + NOTE: This doesn't happen nearly at all + */ + /*if(light_sources.find(n2pos)) + { + infostream<<"Removed from light_sources"<getNode(relpos); + bool is_valid_position; + MapNode n = block->getNode(relpos, &is_valid_position); - u8 oldlight = n.getLight(bank, nodemgr); + u8 oldlight = is_valid_position ? n.getLight(bank, nodemgr) : 0; u8 newlight = diminish_light(oldlight); // Loop through 6 neighbors @@ -499,67 +511,61 @@ void Map::spreadLight(enum LightBank bank, // Get the block where the node is located v3s16 blockpos = getNodeBlockPos(n2pos); - try + // Only fetch a new block if the block position has changed + try { + if(block == NULL || blockpos != blockpos_last){ + block = getBlockNoCreate(blockpos); + blockpos_last = blockpos; + + block_checked_in_modified = false; + blockchangecount++; + } + } + catch(InvalidPositionException &e) { + continue; + } + + // Calculate relative position in block + v3s16 relpos = n2pos - blockpos * MAP_BLOCKSIZE; + // Get node straight from the block + MapNode n2 = block->getNode(relpos, &is_valid_position); + if (!is_valid_position) + continue; + + bool changed = false; + /* + 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, nodemgr) > undiminish_light(oldlight)) { - // Only fetch a new block if the block position has changed - try{ - if(block == NULL || blockpos != blockpos_last){ - block = getBlockNoCreate(blockpos); - blockpos_last = blockpos; - - block_checked_in_modified = false; - blockchangecount++; - } - } - catch(InvalidPositionException &e) - { - continue; - } - - // Calculate relative position in block - v3s16 relpos = n2pos - blockpos * MAP_BLOCKSIZE; - // Get node straight from the block - MapNode n2 = block->getNode(relpos); - - bool changed = false; - /* - 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, nodemgr) > undiminish_light(oldlight)) + lighted_nodes.insert(n2pos); + changed = true; + } + /* + If the neighbor is dimmer than how much light this node + would spread on it, add to list + */ + if(n2.getLight(bank, nodemgr) < newlight) + { + if(nodemgr->get(n2).light_propagates) { + n2.setLight(bank, newlight, nodemgr); + block->setNode(relpos, n2); lighted_nodes.insert(n2pos); changed = true; } - /* - If the neighbor is dimmer than how much light this node - would spread on it, add to list - */ - if(n2.getLight(bank, nodemgr) < newlight) - { - if(nodemgr->get(n2).light_propagates) - { - n2.setLight(bank, newlight, nodemgr); - block->setNode(relpos, n2); - lighted_nodes.insert(n2pos); - changed = true; - } - } - - // Add to modified_blocks - if(changed == true && block_checked_in_modified == false) - { - // If the block is not found in modified_blocks, add. - if(modified_blocks.find(blockpos) == modified_blocks.end()) - { - modified_blocks[blockpos] = block; - } - block_checked_in_modified = true; - } } - catch(InvalidPositionException &e) + + // Add to modified_blocks + if(changed == true && block_checked_in_modified == false) { - continue; + // If the block is not found in modified_blocks, add. + if(modified_blocks.find(blockpos) == modified_blocks.end()) + { + modified_blocks[blockpos] = block; + } + block_checked_in_modified = true; } } } @@ -607,13 +613,11 @@ v3s16 Map::getBrightestNeighbour(enum LightBank bank, v3s16 p) // Get the position of the neighbor node v3s16 n2pos = p + dirs[i]; MapNode n2; - try{ - n2 = getNode(n2pos); - } - catch(InvalidPositionException &e) - { + bool is_valid_position; + n2 = getNodeNoEx(n2pos, &is_valid_position); + if (!is_valid_position) continue; - } + if(n2.getLight(bank, nodemgr) > brightest_light || found_something == false){ brightest_light = n2.getLight(bank, nodemgr); brightest_pos = n2pos; @@ -656,7 +660,10 @@ s16 Map::propagateSunlight(v3s16 start, } v3s16 relpos = pos - blockpos*MAP_BLOCKSIZE; - MapNode n = block->getNode(relpos); + bool is_valid_position; + MapNode n = block->getNode(relpos, &is_valid_position); + if (!is_valid_position) + break; if(nodemgr->get(n).sunlight_propagates) { @@ -723,39 +730,37 @@ void Map::updateLighting(enum LightBank bank, for(s16 x=0; xgetNode(p); - u8 oldlight = n.getLight(bank, nodemgr); - n.setLight(bank, 0, nodemgr); - block->setNode(p, n); - - // If node sources light, add to list - u8 source = nodemgr->get(n).light_source; - if(source != 0) - light_sources.insert(p + posnodes); - - // Collect borders for unlighting - if((x==0 || x == MAP_BLOCKSIZE-1 - || y==0 || y == MAP_BLOCKSIZE-1 - || z==0 || z == MAP_BLOCKSIZE-1) - && oldlight != 0) - { - v3s16 p_map = p + posnodes; - unlight_from[p_map] = oldlight; - } - } - catch(InvalidPositionException &e) - { - /* - This would happen when dealing with a - dummy block. + v3s16 p(x,y,z); + bool is_valid_position; + MapNode n = block->getNode(p, &is_valid_position); + if (!is_valid_position) { + /* This would happen when dealing with a + dummy block. */ - //assert(0); infostream<<"updateLighting(): InvalidPositionException" <setNode(p, n); + + // If node sources light, add to list + u8 source = nodemgr->get(n).light_source; + if(source != 0) + light_sources.insert(p + posnodes); + + // Collect borders for unlighting + if((x==0 || x == MAP_BLOCKSIZE-1 + || y==0 || y == MAP_BLOCKSIZE-1 + || z==0 || z == MAP_BLOCKSIZE-1) + && oldlight != 0) + { + v3s16 p_map = p + posnodes; + unlight_from[p_map] = oldlight; + } + + } if(bank == LIGHTBANK_DAY) @@ -965,15 +970,12 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, Otherwise there probably is. */ - try{ - MapNode topnode = getNode(toppos); - if(topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN) - node_under_sunlight = false; - } - catch(InvalidPositionException &e) - { - } + bool is_valid_position; + MapNode topnode = getNodeNoEx(toppos, &is_valid_position); + + if(is_valid_position && topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN) + node_under_sunlight = false; /* Remove all light that has come out of this node @@ -988,7 +990,7 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, { enum LightBank bank = banks[i]; - u8 lightwas = getNode(p).getLight(bank, ndef); + u8 lightwas = getNodeNoEx(p).getLight(bank, ndef); // Add the block of the added node to modified_blocks v3s16 blockpos = getNodeBlockPos(p); @@ -1045,13 +1047,10 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, v3s16 n2pos(p.X, y, p.Z); MapNode n2; - try{ - n2 = getNode(n2pos); - } - catch(InvalidPositionException &e) - { + + n2 = getNodeNoEx(n2pos, &is_valid_position); + if (!is_valid_position) break; - } if(n2.getLight(LIGHTBANK_DAY, ndef) == LIGHT_SUN) { @@ -1112,20 +1111,14 @@ void Map::addNodeAndUpdate(v3s16 p, MapNode n, }; 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) + MapNode n2 = getNodeNoEx(p2, &is_valid_position); + if(is_valid_position + && (ndef->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR)) { m_transforming_liquid.push_back(p2); } - - }catch(InvalidPositionException &e) - { - } } } @@ -1156,15 +1149,11 @@ void Map::removeNodeAndUpdate(v3s16 p, 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); + bool is_valid_position; + MapNode topnode = getNodeNoEx(toppos, &is_valid_position); - if(topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN) - node_under_sunlight = false; - } - catch(InvalidPositionException &e) - { - } + if(is_valid_position && topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN) + node_under_sunlight = false; std::set light_sources; @@ -1181,7 +1170,7 @@ void Map::removeNodeAndUpdate(v3s16 p, Unlight neighbors (in case the node is a light source) */ unLightNeighbors(bank, p, - getNode(p).getLight(bank, ndef), + getNodeNoEx(p).getLight(bank, ndef), light_sources, modified_blocks); } @@ -1241,13 +1230,11 @@ void Map::removeNodeAndUpdate(v3s16 p, { // Set the lighting of this node to 0 // TODO: Is this needed? Lighting is cleared up there already. - try{ - MapNode n = getNode(p); + MapNode n = getNodeNoEx(p, &is_valid_position); + if (is_valid_position) { n.setLight(LIGHTBANK_DAY, 0, ndef); setNode(p, n); - } - catch(InvalidPositionException &e) - { + } else { assert(0); } } @@ -1303,20 +1290,15 @@ void Map::removeNodeAndUpdate(v3s16 p, }; 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) + bool is_position_valid; + MapNode n2 = getNodeNoEx(p2, &is_position_valid); + if (is_position_valid + && (ndef->get(n2).isLiquid() || n2.getContent() == CONTENT_AIR)) { m_transforming_liquid.push_back(p2); } - - }catch(InvalidPositionException &e) - { - } } } diff --git a/src/map.h b/src/map.h index 1847c7ac..4df9a556 100644 --- a/src/map.h +++ b/src/map.h @@ -197,14 +197,13 @@ public: bool isValidPosition(v3s16 p); - // throws InvalidPositionException if not found - MapNode getNode(v3s16 p); - // throws InvalidPositionException if not found void setNode(v3s16 p, MapNode & n); // Returns a CONTENT_IGNORE node if not found - MapNode getNodeNoEx(v3s16 p); + // If is_valid_position is not NULL then this will be set to true if the + // position is valid, otherwise false + MapNode getNodeNoEx(v3s16 p, bool *is_valid_position = NULL); void unspreadLight(enum LightBank bank, std::map & from_nodes, diff --git a/src/mapblock.cpp b/src/mapblock.cpp index 8e8961e1..255b661b 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -97,54 +97,19 @@ bool MapBlock::isValidPositionParent(v3s16 p) } } -MapNode MapBlock::getNodeParent(v3s16 p) +MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position) { - if(isValidPosition(p) == false) - { - return m_parent->getNode(getPosRelative() + p); - } - else - { - if(data == NULL) - throw InvalidPositionException(); - return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X]; - } -} + if (isValidPosition(p) == false) + return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position); -void MapBlock::setNodeParent(v3s16 p, MapNode & n) -{ - if(isValidPosition(p) == false) - { - m_parent->setNode(getPosRelative() + p, n); - } - else - { - if(data == NULL) - throw InvalidPositionException(); - data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n; - } -} - -MapNode MapBlock::getNodeParentNoEx(v3s16 p) -{ - if(isValidPosition(p) == false) - { - try{ - return m_parent->getNode(getPosRelative() + p); - } - catch(InvalidPositionException &e) - { - return MapNode(CONTENT_IGNORE); - } - } - else - { - if(data == NULL) - { - return MapNode(CONTENT_IGNORE); - } - return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X]; + if (data == NULL) { + if (is_valid_position) + *is_valid_position = false; + return MapNode(CONTENT_IGNORE); } + if (is_valid_position) + *is_valid_position = true; + return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X]; } /* @@ -183,9 +148,14 @@ bool MapBlock::propagateSunlight(std::set & light_sources, #if 1 bool no_sunlight = false; bool no_top_block = false; + // Check if node above block has sunlight - try{ - MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z)); + + bool is_valid_position; + MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z), + &is_valid_position); + if (is_valid_position) + { if(n.getContent() == CONTENT_IGNORE) { // Trust heuristics @@ -196,7 +166,7 @@ bool MapBlock::propagateSunlight(std::set & light_sources, no_sunlight = true; } } - catch(InvalidPositionException &e) + else { no_top_block = true; @@ -208,7 +178,7 @@ bool MapBlock::propagateSunlight(std::set & light_sources, } else { - MapNode n = getNode(v3s16(x, MAP_BLOCKSIZE-1, z)); + MapNode n = getNodeNoEx(v3s16(x, MAP_BLOCKSIZE-1, z)); if(m_gamedef->ndef()->get(n).sunlight_propagates == false) { no_sunlight = true; @@ -308,27 +278,27 @@ bool MapBlock::propagateSunlight(std::set & light_sources, Ignore non-transparent nodes as they always have no light */ - try - { + if(block_below_is_valid) { - MapNode n = getNodeParent(v3s16(x, -1, z)); - if(nodemgr->get(n).light_propagates) - { - if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN - && sunlight_should_go_down == false) - block_below_is_valid = false; - else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN - && sunlight_should_go_down == true) - block_below_is_valid = false; + MapNode n = getNodeParent(v3s16(x, -1, z), &is_valid_position); + if (is_valid_position) { + if(nodemgr->get(n).light_propagates) + { + if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN + && sunlight_should_go_down == false) + block_below_is_valid = false; + else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN + && sunlight_should_go_down == true) + block_below_is_valid = false; + } + } + else + { + /*std::cout<<"InvalidBlockException for bottom block node" + <getNode(p); + MapNode n = block->getNodeNoEx(p); content_t c = n.getContent(); if(c == CONTENT_IGNORE) some_ignore = true; diff --git a/src/mapblock.h b/src/mapblock.h index 3879c5b0..fa56f331 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -251,37 +251,39 @@ public: Regular MapNode get-setters */ - bool isValidPosition(v3s16 p) + bool isValidPosition(s16 x, s16 y, s16 z) { - if(data == NULL) - return false; - return (p.X >= 0 && p.X < MAP_BLOCKSIZE - && p.Y >= 0 && p.Y < MAP_BLOCKSIZE - && p.Z >= 0 && p.Z < MAP_BLOCKSIZE); + return data != NULL + && x >= 0 && x < MAP_BLOCKSIZE + && y >= 0 && y < MAP_BLOCKSIZE + && z >= 0 && z < MAP_BLOCKSIZE; } - MapNode getNode(s16 x, s16 y, s16 z) + bool isValidPosition(v3s16 p) { - if(data == NULL) - throw InvalidPositionException(); - if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException(); - if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException(); - if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException(); + return isValidPosition(p.X, p.Y, p.Z); + } + + MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position) + { + *valid_position = isValidPosition(x, y, z); + + if (!*valid_position) + return MapNode(CONTENT_IGNORE); + return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x]; } - MapNode getNode(v3s16 p) + MapNode getNode(v3s16 p, bool *valid_position) { - return getNode(p.X, p.Y, p.Z); + return getNode(p.X, p.Y, p.Z, valid_position); } MapNode getNodeNoEx(v3s16 p) { - try{ - return getNode(p.X, p.Y, p.Z); - }catch(InvalidPositionException &e){ - return MapNode(CONTENT_IGNORE); - } + bool is_valid; + MapNode node = getNode(p.X, p.Y, p.Z, &is_valid); + return is_valid ? node : MapNode(CONTENT_IGNORE); } void setNode(s16 x, s16 y, s16 z, MapNode & n) @@ -304,16 +306,18 @@ public: Non-checking variants of the above */ - MapNode getNodeNoCheck(s16 x, s16 y, s16 z) + MapNode getNodeNoCheck(s16 x, s16 y, s16 z, bool *valid_position) { - if(data == NULL) - throw InvalidPositionException(); + *valid_position = data != NULL; + if(!valid_position) + return MapNode(CONTENT_IGNORE); + return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x]; } - MapNode getNodeNoCheck(v3s16 p) + MapNode getNodeNoCheck(v3s16 p, bool *valid_position) { - return getNodeNoCheck(p.X, p.Y, p.Z); + return getNodeNoCheck(p.X, p.Y, p.Z, valid_position); } void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n) @@ -334,9 +338,8 @@ public: is not valid on this MapBlock. */ bool isValidPositionParent(v3s16 p); - MapNode getNodeParent(v3s16 p); + MapNode getNodeParent(v3s16 p, bool *is_valid_position = NULL); void setNodeParent(v3s16 p, MapNode & n); - MapNode getNodeParentNoEx(v3s16 p); void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node) { diff --git a/src/particles.cpp b/src/particles.cpp index 5a3056c3..b1662e10 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -168,18 +168,19 @@ void Particle::step(float dtime) void Particle::updateLight() { u8 light = 0; - try{ - v3s16 p = v3s16( - floor(m_pos.X+0.5), - floor(m_pos.Y+0.5), - floor(m_pos.Z+0.5) - ); - MapNode n = m_env->getClientMap().getNode(p); + bool pos_ok; + + v3s16 p = v3s16( + floor(m_pos.X+0.5), + floor(m_pos.Y+0.5), + floor(m_pos.Z+0.5) + ); + MapNode n = m_env->getClientMap().getNodeNoEx(p, &pos_ok); + if (pos_ok) light = n.getLightBlend(m_env->getDayNightRatio(), m_gamedef->ndef()); - } - catch(InvalidPositionException &e){ + else light = blend_light(m_env->getDayNightRatio(), LIGHT_SUN, 0); - } + m_light = decode_light(light); } diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 7c7a68b7..130b15c0 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -159,16 +159,15 @@ int ModApiEnvMod::l_get_node_or_nil(lua_State *L) // pos v3s16 pos = read_v3s16(L, 1); // Do it - try{ - MapNode n = env->getMap().getNode(pos); + bool pos_ok; + MapNode n = env->getMap().getNodeNoEx(pos, &pos_ok); + if (pos_ok) { // Return node pushnode(L, n, env->getGameDef()->ndef()); - return 1; - } catch(InvalidPositionException &e) - { + } else { lua_pushnil(L); - return 1; } + return 1; } // get_node_light(pos, timeofday) @@ -185,16 +184,16 @@ int ModApiEnvMod::l_get_node_light(lua_State *L) time_of_day = 24000.0 * lua_tonumber(L, 2); time_of_day %= 24000; u32 dnr = time_to_daynight_ratio(time_of_day, true); - try{ - MapNode n = env->getMap().getNode(pos); + + bool is_position_ok; + MapNode n = env->getMap().getNodeNoEx(pos, &is_position_ok); + if (is_position_ok) { INodeDefManager *ndef = env->getGameDef()->ndef(); lua_pushinteger(L, n.getLightBlend(dnr, ndef)); - return 1; - } catch(InvalidPositionException &e) - { + } else { lua_pushnil(L); - return 1; } + return 1; } // place_node(pos, node) diff --git a/src/server.cpp b/src/server.cpp index 2694f1bb..d1f3997e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2424,17 +2424,18 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) somebody is cheating, by checking the timing. */ MapNode n(CONTENT_IGNORE); - try - { - n = m_env->getMap().getNode(p_under); - } - catch(InvalidPositionException &e) - { + bool pos_ok; + n = m_env->getMap().getNodeNoEx(p_under, &pos_ok); + if (pos_ok) + n = m_env->getMap().getNodeNoEx(p_under, &pos_ok); + + if (!pos_ok) { infostream<<"Server: Not punching: Node not found." <<" Adding block to emerge queue." <enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above), false); } + if(n.getContent() != CONTENT_IGNORE) m_script->node_on_punch(p_under, n, playersao, pointed); // Cheat prevention @@ -2479,16 +2480,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id) // Only digging of nodes if(pointed.type == POINTEDTHING_NODE) { - MapNode n(CONTENT_IGNORE); - try - { - n = m_env->getMap().getNode(p_under); - } - catch(InvalidPositionException &e) - { - infostream<<"Server: Not finishing digging: Node not found." - <<" Adding block to emerge queue." - <getMap().getNodeNoEx(p_under, &pos_ok); + if (!pos_ok) { + infostream << "Server: Not finishing digging: Node not found." + << " Adding block to emerge queue." + << std::endl; m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above), false); }