Remove most exceptions from getNode() (and variants)

master
Craig Robbins 2014-11-14 18:05:34 +10:00
parent 92815ad54b
commit 5b8855e83c
12 changed files with 429 additions and 455 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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) {}
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{
bool pos_ok;
// Get node at head
v3s16 p = obj->getLightPosition();
MapNode n = m_map->getNode(p);
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{
bool pos_ok;
// Get node at head
v3s16 p = object->getLightPosition();
MapNode n = m_map->getNode(p);
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();

View File

@ -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)
else {
node = map.getNodeNoEx(p, &is_valid_position);
if (is_valid_position &&!nodedef->get(node).buildable_to)
return false;
} catch (InvalidPositionException &e) {}
}
}
// 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,7 +3631,9 @@ 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);
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")) {

View File

@ -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;
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
{
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)
{
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);
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)
{
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,21 +277,19 @@ 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)
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
if(nodemgr->get(map->getNode(p+v3s16(0,1,0))).walkable == true) {
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) {
if (nodemgr->get(map->getNode(p+v3s16(0,2,0))).walkable)
continue;
}
}
catch(InvalidPositionException &e)
{
node =map->getNodeNoEx(p + v3s16(0,2,0), &is_valid_position);
if (!is_valid_position || nodemgr->get(node).walkable)
continue;
}

View File

@ -186,17 +186,28 @@ 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);
}
v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
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);
@ -204,8 +215,13 @@ MapNode Map::getNode(v3s16 p)
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 \""
<<m_gamedef->ndef()->get(block->getNodeNoCheck(relpos)).name
<<m_gamedef->ndef()->get(block->getNodeNoCheck(relpos, &temp_bool)).name
<<"\" at "<<PP(p)<<" (block "<<PP(blockpos)<<")"<<std::endl;
debug_stacks_print_to(infostream);
return;
@ -315,8 +332,6 @@ void Map::unspreadLight(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){
@ -327,15 +342,17 @@ void Map::unspreadLight(enum LightBank bank,
blockchangecount++;
}
}
catch(InvalidPositionException &e)
{
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 is_valid_position;
MapNode n2 = block->getNode(relpos, &is_valid_position);
if (!is_valid_position)
continue;
bool changed = false;
@ -394,11 +411,6 @@ void Map::unspreadLight(enum LightBank bank,
block_checked_in_modified = true;
}
}
catch(InvalidPositionException &e)
{
continue;
}
}
}
/*infostream<<"unspreadLight(): Changed block "
@ -474,8 +486,7 @@ void Map::spreadLight(enum LightBank bank,
blockchangecount++;
}
}
catch(InvalidPositionException &e)
{
catch(InvalidPositionException &e) {
continue;
}
@ -486,9 +497,10 @@ void Map::spreadLight(enum LightBank bank,
v3s16 relpos = pos - blockpos_last * MAP_BLOCKSIZE;
// Get node straight from the block
MapNode n = block->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,8 +511,6 @@ 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){
@ -511,15 +521,16 @@ void Map::spreadLight(enum LightBank bank,
blockchangecount++;
}
}
catch(InvalidPositionException &e)
{
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);
MapNode n2 = block->getNode(relpos, &is_valid_position);
if (!is_valid_position)
continue;
bool changed = false;
/*
@ -557,11 +568,6 @@ void Map::spreadLight(enum LightBank bank,
block_checked_in_modified = true;
}
}
catch(InvalidPositionException &e)
{
continue;
}
}
}
/*infostream<<"spreadLight(): Changed block "
@ -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,10 +730,17 @@ void Map::updateLighting(enum LightBank bank,
for(s16 x=0; x<MAP_BLOCKSIZE; x++)
for(s16 y=0; y<MAP_BLOCKSIZE; y++)
{
try{
v3s16 p(x,y,z);
MapNode n = block->getNode(p);
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.
*/
infostream<<"updateLighting(): InvalidPositionException"
<<std::endl;
continue;
}
u8 oldlight = n.getLight(bank, nodemgr);
n.setLight(bank, 0, nodemgr);
block->setNode(p, n);
@ -745,17 +759,8 @@ void Map::updateLighting(enum LightBank bank,
v3s16 p_map = p + posnodes;
unlight_from[p_map] = oldlight;
}
}
catch(InvalidPositionException &e)
{
/*
This would happen when dealing with a
dummy block.
*/
//assert(0);
infostream<<"updateLighting(): InvalidPositionException"
<<std::endl;
}
}
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)
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;
}
catch(InvalidPositionException &e)
{
}
/*
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)
if(is_valid_position && topnode.getLight(LIGHTBANK_DAY, ndef) != LIGHT_SUN)
node_under_sunlight = false;
}
catch(InvalidPositionException &e)
{
}
std::set<v3s16> 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)
{
}
}
}

View File

@ -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<v3s16, u8> & from_nodes,

View File

@ -97,55 +97,20 @@ 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];
}
}
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)
{
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];
}
}
/*
Propagates sunlight down through the block.
@ -183,9 +148,14 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & 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<v3s16> & light_sources,
no_sunlight = true;
}
}
catch(InvalidPositionException &e)
else
{
no_top_block = true;
@ -208,7 +178,7 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & 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,11 +278,11 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & 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));
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
@ -322,9 +292,8 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
&& sunlight_should_go_down == true)
block_below_is_valid = false;
}
}//if
}//try
catch(InvalidPositionException &e)
}
else
{
/*std::cout<<"InvalidBlockException for bottom block node"
<<std::endl;*/
@ -332,6 +301,7 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
}
}
}
}
return block_below_is_valid;
}
@ -1070,7 +1040,7 @@ std::string analyze_block(MapBlock *block)
for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
{
v3s16 p(x0,y0,z0);
MapNode n = block->getNode(p);
MapNode n = block->getNodeNoEx(p);
content_t c = n.getContent();
if(c == CONTENT_IGNORE)
some_ignore = true;

View File

@ -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)
{

View File

@ -168,18 +168,19 @@ void Particle::step(float dtime)
void Particle::updateLight()
{
u8 light = 0;
try{
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().getNode(p);
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);
}

View File

@ -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)

View File

@ -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."
<<std::endl;
m_emerge->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,13 +2480,9 @@ 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)
{
bool pos_ok;
MapNode n = m_env->getMap().getNodeNoEx(p_under, &pos_ok);
if (!pos_ok) {
infostream << "Server: Not finishing digging: Node not found."
<< " Adding block to emerge queue."
<< std::endl;