Skip to content

Commit

Permalink
Remove most exceptions from getNode() (and variants)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeno- committed Nov 14, 2014
1 parent 92815ad commit 5b8855e
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 434 deletions.
11 changes: 7 additions & 4 deletions src/collision.cpp
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions src/content_cso.cpp
Expand Up @@ -60,13 +60,12 @@ class SmokePuffCSO: public ClientSimpleObject
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);
}
Expand Down
36 changes: 18 additions & 18 deletions src/environment.cpp
Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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();
Expand Down
44 changes: 27 additions & 17 deletions src/game.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

node = map.getNodeNoEx(nodepos, &is_valid_position);
if (!is_valid_position)
return false;

if (prediction != "" && !nodedef->get(map.getNode(nodepos)).rightclickable) {
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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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: ";
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 =
Expand Down
101 changes: 56 additions & 45 deletions src/localplayer.cpp
Expand Up @@ -101,57 +101,70 @@ 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;

// If in liquid, the threshold of coming out is at higher y
if (in_liquid)
{
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;
}
}
catch(InvalidPositionException &e)
// If not in liquid, the threshold of going in is at lower y
else
{
in_liquid = false;
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;
}

/*
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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5b8855e

Please sign in to comment.