Skip to content

Commit 5b8855e

Browse files
committedNov 14, 2014
Remove most exceptions from getNode() (and variants)
1 parent 92815ad commit 5b8855e

12 files changed

+408
-434
lines changed
 

‎src/collision.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,13 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
251251
for(s16 z = min_z; z <= max_z; z++)
252252
{
253253
v3s16 p(x,y,z);
254-
try{
254+
255+
bool is_position_valid;
256+
MapNode n = map->getNodeNoEx(p, &is_position_valid);
257+
258+
if (is_position_valid) {
255259
// Object collides into walkable nodes
256-
MapNode n = map->getNode(p);
260+
257261
const ContentFeatures &f = gamedef->getNodeDefManager()->get(n);
258262
if(f.walkable == false)
259263
continue;
@@ -275,8 +279,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
275279
is_object.push_back(false);
276280
}
277281
}
278-
catch(InvalidPositionException &e)
279-
{
282+
else {
280283
// Collide with unloaded nodes
281284
aabb3f box = getNodeBox(p, BS);
282285
cboxes.push_back(box);

‎src/content_cso.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ class SmokePuffCSO: public ClientSimpleObject
6060
m_spritenode->setVisible(true);
6161
m_spritenode->setSize(size);
6262
/* Update brightness */
63-
u8 light = 64;
64-
try{
65-
MapNode n = env->getMap().getNode(floatToInt(pos, BS));
66-
light = decode_light(n.getLightBlend(env->getDayNightRatio(),
67-
env->getGameDef()->ndef()));
68-
}
69-
catch(InvalidPositionException &e){}
63+
u8 light;
64+
bool pos_ok;
65+
MapNode n = env->getMap().getNodeNoEx(floatToInt(pos, BS), &pos_ok);
66+
light = pos_ok ? decode_light(n.getLightBlend(env->getDayNightRatio(),
67+
env->getGameDef()->ndef()))
68+
: 64;
7069
video::SColor color(255,light,light,light);
7170
m_spritenode->setColor(color);
7271
}

‎src/environment.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -2342,10 +2342,8 @@ void ClientEnvironment::step(float dtime)
23422342
// (day: LIGHT_SUN, night: 0)
23432343
MapNode node_at_lplayer(CONTENT_AIR, 0x0f, 0);
23442344

2345-
try {
2346-
v3s16 p = lplayer->getLightPosition();
2347-
node_at_lplayer = m_map->getNode(p);
2348-
} catch (InvalidPositionException &e) {}
2345+
v3s16 p = lplayer->getLightPosition();
2346+
node_at_lplayer = m_map->getNodeNoEx(p);
23492347

23502348
u16 light = getInteriorLight(node_at_lplayer, 0, m_gamedef->ndef());
23512349
u8 day = light & 0xff;
@@ -2371,15 +2369,16 @@ void ClientEnvironment::step(float dtime)
23712369
{
23722370
// Update lighting
23732371
u8 light = 0;
2374-
try{
2375-
// Get node at head
2376-
v3s16 p = obj->getLightPosition();
2377-
MapNode n = m_map->getNode(p);
2372+
bool pos_ok;
2373+
2374+
// Get node at head
2375+
v3s16 p = obj->getLightPosition();
2376+
MapNode n = m_map->getNodeNoEx(p, &pos_ok);
2377+
if (pos_ok)
23782378
light = n.getLightBlend(day_night_ratio, m_gamedef->ndef());
2379-
}
2380-
catch(InvalidPositionException &e){
2379+
else
23812380
light = blend_light(day_night_ratio, LIGHT_SUN, 0);
2382-
}
2381+
23832382
obj->updateLight(light);
23842383
}
23852384
}
@@ -2470,15 +2469,16 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
24702469
object->addToScene(m_smgr, m_texturesource, m_irr);
24712470
{ // Update lighting immediately
24722471
u8 light = 0;
2473-
try{
2474-
// Get node at head
2475-
v3s16 p = object->getLightPosition();
2476-
MapNode n = m_map->getNode(p);
2472+
bool pos_ok;
2473+
2474+
// Get node at head
2475+
v3s16 p = object->getLightPosition();
2476+
MapNode n = m_map->getNodeNoEx(p, &pos_ok);
2477+
if (pos_ok)
24772478
light = n.getLightBlend(getDayNightRatio(), m_gamedef->ndef());
2478-
}
2479-
catch(InvalidPositionException &e){
2479+
else
24802480
light = blend_light(getDayNightRatio(), LIGHT_SUN, 0);
2481-
}
2481+
24822482
object->updateLight(light);
24832483
}
24842484
return object->getId();

‎src/game.cpp

+27-17
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,11 @@ PointedThing getPointedThing(Client *client, v3f player_position,
360360
for (s16 z = zstart; z <= zend; z++)
361361
for (s16 x = xstart; x <= xend; x++) {
362362
MapNode n;
363+
bool is_valid_position;
363364

364-
try {
365-
n = map.getNode(v3s16(x, y, z));
366-
} catch (InvalidPositionException &e) {
365+
n = map.getNodeNoEx(v3s16(x, y, z), &is_valid_position);
366+
if (!is_valid_position)
367367
continue;
368-
}
369368

370369
if (!isPointableNode(n, client, liquids_pointable))
371370
continue;
@@ -873,22 +872,31 @@ bool nodePlacementPrediction(Client &client,
873872
std::string prediction = playeritem_def.node_placement_prediction;
874873
INodeDefManager *nodedef = client.ndef();
875874
ClientMap &map = client.getEnv().getClientMap();
875+
MapNode node;
876+
bool is_valid_position;
877+
878+
node = map.getNodeNoEx(nodepos, &is_valid_position);
879+
if (!is_valid_position)
880+
return false;
876881

877-
if (prediction != "" && !nodedef->get(map.getNode(nodepos)).rightclickable) {
882+
if (prediction != "" && !nodedef->get(node).rightclickable) {
878883
verbosestream << "Node placement prediction for "
879884
<< playeritem_def.name << " is "
880885
<< prediction << std::endl;
881886
v3s16 p = neighbourpos;
882887

883888
// Place inside node itself if buildable_to
884-
try {
885-
MapNode n_under = map.getNode(nodepos);
886-
889+
MapNode n_under = map.getNodeNoEx(nodepos, &is_valid_position);
890+
if (is_valid_position)
891+
{
887892
if (nodedef->get(n_under).buildable_to)
888893
p = nodepos;
889-
else if (!nodedef->get(map.getNode(p)).buildable_to)
890-
return false;
891-
} catch (InvalidPositionException &e) {}
894+
else {
895+
node = map.getNodeNoEx(p, &is_valid_position);
896+
if (is_valid_position &&!nodedef->get(node).buildable_to)
897+
return false;
898+
}
899+
}
892900

893901
// Find id of predicted node
894902
content_t id;
@@ -946,7 +954,7 @@ bool nodePlacementPrediction(Client &client,
946954
else
947955
pp = p + v3s16(0, -1, 0);
948956

949-
if (!nodedef->get(map.getNode(pp)).walkable)
957+
if (!nodedef->get(map.getNodeNoEx(pp)).walkable)
950958
return false;
951959
}
952960

@@ -3431,7 +3439,7 @@ void Game::handlePointingAtNode(GameRunData *runData,
34313439
if (meta) {
34323440
infotext = narrow_to_wide(meta->getString("infotext"));
34333441
} else {
3434-
MapNode n = map.getNode(nodepos);
3442+
MapNode n = map.getNodeNoEx(nodepos);
34353443

34363444
if (nodedef_manager->get(n).tiledef[0].name == "unknown_node.png") {
34373445
infotext = L"Unknown node: ";
@@ -3489,7 +3497,7 @@ void Game::handlePointingAtNode(GameRunData *runData,
34893497
}
34903498

34913499
if (playeritem_def.node_placement_prediction == "" ||
3492-
nodedef_manager->get(map.getNode(nodepos)).rightclickable)
3500+
nodedef_manager->get(map.getNodeNoEx(nodepos)).rightclickable)
34933501
client->interact(3, pointed); // Report to server
34943502
}
34953503
}
@@ -3558,7 +3566,7 @@ void Game::handleDigging(GameRunData *runData,
35583566

35593567
LocalPlayer *player = client->getEnv().getLocalPlayer();
35603568
ClientMap &map = client->getEnv().getClientMap();
3561-
MapNode n = client->getEnv().getClientMap().getNode(nodepos);
3569+
MapNode n = client->getEnv().getClientMap().getNodeNoEx(nodepos);
35623570

35633571
// NOTE: Similar piece of code exists on the server side for
35643572
// cheat detection.
@@ -3623,8 +3631,10 @@ void Game::handleDigging(GameRunData *runData,
36233631
infostream << "Digging completed" << std::endl;
36243632
client->interact(2, pointed);
36253633
client->setCrack(-1, v3s16(0, 0, 0));
3626-
MapNode wasnode = map.getNode(nodepos);
3627-
client->removeNode(nodepos);
3634+
bool is_valid_position;
3635+
MapNode wasnode = map.getNodeNoEx(nodepos, &is_valid_position);
3636+
if (is_valid_position)
3637+
client->removeNode(nodepos);
36283638

36293639
if (g_settings->getBool("enable_particles")) {
36303640
const ContentFeatures &features =

‎src/localplayer.cpp

+56-45
Original file line numberDiff line numberDiff line change
@@ -101,57 +101,70 @@ void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
101101
Collision detection
102102
*/
103103

104+
bool is_valid_position;
105+
MapNode node;
106+
v3s16 pp;
107+
104108
/*
105109
Check if player is in liquid (the oscillating value)
106110
*/
107-
try{
108-
// If in liquid, the threshold of coming out is at higher y
109-
if(in_liquid)
110-
{
111-
v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
112-
in_liquid = nodemgr->get(map->getNode(pp).getContent()).isLiquid();
113-
liquid_viscosity = nodemgr->get(map->getNode(pp).getContent()).liquid_viscosity;
114-
}
115-
// If not in liquid, the threshold of going in is at lower y
116-
else
117-
{
118-
v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
119-
in_liquid = nodemgr->get(map->getNode(pp).getContent()).isLiquid();
120-
liquid_viscosity = nodemgr->get(map->getNode(pp).getContent()).liquid_viscosity;
111+
112+
// If in liquid, the threshold of coming out is at higher y
113+
if (in_liquid)
114+
{
115+
pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
116+
node = map->getNodeNoEx(pp, &is_valid_position);
117+
if (is_valid_position) {
118+
in_liquid = nodemgr->get(node.getContent()).isLiquid();
119+
liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
120+
} else {
121+
in_liquid = false;
121122
}
122123
}
123-
catch(InvalidPositionException &e)
124+
// If not in liquid, the threshold of going in is at lower y
125+
else
124126
{
125-
in_liquid = false;
127+
pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
128+
node = map->getNodeNoEx(pp, &is_valid_position);
129+
if (is_valid_position) {
130+
in_liquid = nodemgr->get(node.getContent()).isLiquid();
131+
liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
132+
} else {
133+
in_liquid = false;
134+
}
126135
}
127136

137+
128138
/*
129139
Check if player is in liquid (the stable value)
130140
*/
131-
try{
132-
v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
133-
in_liquid_stable = nodemgr->get(map->getNode(pp).getContent()).isLiquid();
134-
}
135-
catch(InvalidPositionException &e)
136-
{
141+
pp = floatToInt(position + v3f(0,0,0), BS);
142+
node = map->getNodeNoEx(pp, &is_valid_position);
143+
if (is_valid_position) {
144+
in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
145+
} else {
137146
in_liquid_stable = false;
138147
}
139148

140149
/*
141150
Check if player is climbing
142151
*/
143152

144-
try {
145-
v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
146-
v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
147-
is_climbing = ((nodemgr->get(map->getNode(pp).getContent()).climbable ||
148-
nodemgr->get(map->getNode(pp2).getContent()).climbable) && !free_move);
149-
}
150-
catch(InvalidPositionException &e)
151-
{
153+
154+
pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
155+
v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
156+
node = map->getNodeNoEx(pp, &is_valid_position);
157+
bool is_valid_position2;
158+
MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2);
159+
160+
if (!(is_valid_position && is_valid_position2)) {
152161
is_climbing = false;
162+
} else {
163+
is_climbing = (nodemgr->get(node.getContent()).climbable
164+
|| nodemgr->get(node2.getContent()).climbable) && !free_move;
153165
}
154166

167+
155168
/*
156169
Collision uncertainty radius
157170
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,
264277
max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
265278
continue;
266279

267-
try{
268-
// The node to be sneaked on has to be walkable
269-
if(nodemgr->get(map->getNode(p)).walkable == false)
270-
continue;
271-
// And the node above it has to be nonwalkable
272-
if(nodemgr->get(map->getNode(p+v3s16(0,1,0))).walkable == true) {
273-
continue;
274-
}
275-
if (!physics_override_sneak_glitch) {
276-
if (nodemgr->get(map->getNode(p+v3s16(0,2,0))).walkable)
277-
continue;
278-
}
279-
}
280-
catch(InvalidPositionException &e)
281-
{
280+
281+
// The node to be sneaked on has to be walkable
282+
node = map->getNodeNoEx(p, &is_valid_position);
283+
if (!is_valid_position || nodemgr->get(node).walkable == false)
282284
continue;
285+
// And the node above it has to be nonwalkable
286+
node = map->getNodeNoEx(p + v3s16(0,1,0), &is_valid_position);
287+
if (!is_valid_position || nodemgr->get(node).walkable) {
288+
continue;
289+
}
290+
if (!physics_override_sneak_glitch) {
291+
node =map->getNodeNoEx(p + v3s16(0,2,0), &is_valid_position);
292+
if (!is_valid_position || nodemgr->get(node).walkable)
293+
continue;
283294
}
284295

285296
min_distance_f = distance_f;

‎src/map.cpp

+191-209
Large diffs are not rendered by default.

‎src/map.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,13 @@ class Map /*: public NodeContainer*/
197197

198198
bool isValidPosition(v3s16 p);
199199

200-
// throws InvalidPositionException if not found
201-
MapNode getNode(v3s16 p);
202-
203200
// throws InvalidPositionException if not found
204201
void setNode(v3s16 p, MapNode & n);
205202

206203
// Returns a CONTENT_IGNORE node if not found
207-
MapNode getNodeNoEx(v3s16 p);
204+
// If is_valid_position is not NULL then this will be set to true if the
205+
// position is valid, otherwise false
206+
MapNode getNodeNoEx(v3s16 p, bool *is_valid_position = NULL);
208207

209208
void unspreadLight(enum LightBank bank,
210209
std::map<v3s16, u8> & from_nodes,

‎src/mapblock.cpp

+37-67
Original file line numberDiff line numberDiff line change
@@ -97,54 +97,19 @@ bool MapBlock::isValidPositionParent(v3s16 p)
9797
}
9898
}
9999

100-
MapNode MapBlock::getNodeParent(v3s16 p)
100+
MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
101101
{
102-
if(isValidPosition(p) == false)
103-
{
104-
return m_parent->getNode(getPosRelative() + p);
105-
}
106-
else
107-
{
108-
if(data == NULL)
109-
throw InvalidPositionException();
110-
return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
111-
}
112-
}
102+
if (isValidPosition(p) == false)
103+
return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);
113104

114-
void MapBlock::setNodeParent(v3s16 p, MapNode & n)
115-
{
116-
if(isValidPosition(p) == false)
117-
{
118-
m_parent->setNode(getPosRelative() + p, n);
119-
}
120-
else
121-
{
122-
if(data == NULL)
123-
throw InvalidPositionException();
124-
data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n;
125-
}
126-
}
127-
128-
MapNode MapBlock::getNodeParentNoEx(v3s16 p)
129-
{
130-
if(isValidPosition(p) == false)
131-
{
132-
try{
133-
return m_parent->getNode(getPosRelative() + p);
134-
}
135-
catch(InvalidPositionException &e)
136-
{
137-
return MapNode(CONTENT_IGNORE);
138-
}
139-
}
140-
else
141-
{
142-
if(data == NULL)
143-
{
144-
return MapNode(CONTENT_IGNORE);
145-
}
146-
return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
105+
if (data == NULL) {
106+
if (is_valid_position)
107+
*is_valid_position = false;
108+
return MapNode(CONTENT_IGNORE);
147109
}
110+
if (is_valid_position)
111+
*is_valid_position = true;
112+
return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
148113
}
149114

150115
/*
@@ -183,9 +148,14 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
183148
#if 1
184149
bool no_sunlight = false;
185150
bool no_top_block = false;
151+
186152
// Check if node above block has sunlight
187-
try{
188-
MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z));
153+
154+
bool is_valid_position;
155+
MapNode n = getNodeParent(v3s16(x, MAP_BLOCKSIZE, z),
156+
&is_valid_position);
157+
if (is_valid_position)
158+
{
189159
if(n.getContent() == CONTENT_IGNORE)
190160
{
191161
// Trust heuristics
@@ -196,7 +166,7 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
196166
no_sunlight = true;
197167
}
198168
}
199-
catch(InvalidPositionException &e)
169+
else
200170
{
201171
no_top_block = true;
202172

@@ -208,7 +178,7 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
208178
}
209179
else
210180
{
211-
MapNode n = getNode(v3s16(x, MAP_BLOCKSIZE-1, z));
181+
MapNode n = getNodeNoEx(v3s16(x, MAP_BLOCKSIZE-1, z));
212182
if(m_gamedef->ndef()->get(n).sunlight_propagates == false)
213183
{
214184
no_sunlight = true;
@@ -308,27 +278,27 @@ bool MapBlock::propagateSunlight(std::set<v3s16> & light_sources,
308278
309279
Ignore non-transparent nodes as they always have no light
310280
*/
311-
try
312-
{
281+
313282
if(block_below_is_valid)
314283
{
315-
MapNode n = getNodeParent(v3s16(x, -1, z));
316-
if(nodemgr->get(n).light_propagates)
284+
MapNode n = getNodeParent(v3s16(x, -1, z), &is_valid_position);
285+
if (is_valid_position) {
286+
if(nodemgr->get(n).light_propagates)
287+
{
288+
if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
289+
&& sunlight_should_go_down == false)
290+
block_below_is_valid = false;
291+
else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
292+
&& sunlight_should_go_down == true)
293+
block_below_is_valid = false;
294+
}
295+
}
296+
else
317297
{
318-
if(n.getLight(LIGHTBANK_DAY, nodemgr) == LIGHT_SUN
319-
&& sunlight_should_go_down == false)
320-
block_below_is_valid = false;
321-
else if(n.getLight(LIGHTBANK_DAY, nodemgr) != LIGHT_SUN
322-
&& sunlight_should_go_down == true)
323-
block_below_is_valid = false;
298+
/*std::cout<<"InvalidBlockException for bottom block node"
299+
<<std::endl;*/
300+
// Just no block below, no need to panic.
324301
}
325-
}//if
326-
}//try
327-
catch(InvalidPositionException &e)
328-
{
329-
/*std::cout<<"InvalidBlockException for bottom block node"
330-
<<std::endl;*/
331-
// Just no block below, no need to panic.
332302
}
333303
}
334304
}
@@ -1070,7 +1040,7 @@ std::string analyze_block(MapBlock *block)
10701040
for(s16 x0=0; x0<MAP_BLOCKSIZE; x0++)
10711041
{
10721042
v3s16 p(x0,y0,z0);
1073-
MapNode n = block->getNode(p);
1043+
MapNode n = block->getNodeNoEx(p);
10741044
content_t c = n.getContent();
10751045
if(c == CONTENT_IGNORE)
10761046
some_ignore = true;

‎src/mapblock.h

+28-25
Original file line numberDiff line numberDiff line change
@@ -251,37 +251,39 @@ class MapBlock /*: public NodeContainer*/
251251
Regular MapNode get-setters
252252
*/
253253

254+
bool isValidPosition(s16 x, s16 y, s16 z)
255+
{
256+
return data != NULL
257+
&& x >= 0 && x < MAP_BLOCKSIZE
258+
&& y >= 0 && y < MAP_BLOCKSIZE
259+
&& z >= 0 && z < MAP_BLOCKSIZE;
260+
}
261+
254262
bool isValidPosition(v3s16 p)
255263
{
256-
if(data == NULL)
257-
return false;
258-
return (p.X >= 0 && p.X < MAP_BLOCKSIZE
259-
&& p.Y >= 0 && p.Y < MAP_BLOCKSIZE
260-
&& p.Z >= 0 && p.Z < MAP_BLOCKSIZE);
264+
return isValidPosition(p.X, p.Y, p.Z);
261265
}
262266

263-
MapNode getNode(s16 x, s16 y, s16 z)
267+
MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position)
264268
{
265-
if(data == NULL)
266-
throw InvalidPositionException();
267-
if(x < 0 || x >= MAP_BLOCKSIZE) throw InvalidPositionException();
268-
if(y < 0 || y >= MAP_BLOCKSIZE) throw InvalidPositionException();
269-
if(z < 0 || z >= MAP_BLOCKSIZE) throw InvalidPositionException();
269+
*valid_position = isValidPosition(x, y, z);
270+
271+
if (!*valid_position)
272+
return MapNode(CONTENT_IGNORE);
273+
270274
return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
271275
}
272276

273-
MapNode getNode(v3s16 p)
277+
MapNode getNode(v3s16 p, bool *valid_position)
274278
{
275-
return getNode(p.X, p.Y, p.Z);
279+
return getNode(p.X, p.Y, p.Z, valid_position);
276280
}
277281

278282
MapNode getNodeNoEx(v3s16 p)
279283
{
280-
try{
281-
return getNode(p.X, p.Y, p.Z);
282-
}catch(InvalidPositionException &e){
283-
return MapNode(CONTENT_IGNORE);
284-
}
284+
bool is_valid;
285+
MapNode node = getNode(p.X, p.Y, p.Z, &is_valid);
286+
return is_valid ? node : MapNode(CONTENT_IGNORE);
285287
}
286288

287289
void setNode(s16 x, s16 y, s16 z, MapNode & n)
@@ -304,16 +306,18 @@ class MapBlock /*: public NodeContainer*/
304306
Non-checking variants of the above
305307
*/
306308

307-
MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
309+
MapNode getNodeNoCheck(s16 x, s16 y, s16 z, bool *valid_position)
308310
{
309-
if(data == NULL)
310-
throw InvalidPositionException();
311+
*valid_position = data != NULL;
312+
if(!valid_position)
313+
return MapNode(CONTENT_IGNORE);
314+
311315
return data[z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + y*MAP_BLOCKSIZE + x];
312316
}
313317

314-
MapNode getNodeNoCheck(v3s16 p)
318+
MapNode getNodeNoCheck(v3s16 p, bool *valid_position)
315319
{
316-
return getNodeNoCheck(p.X, p.Y, p.Z);
320+
return getNodeNoCheck(p.X, p.Y, p.Z, valid_position);
317321
}
318322

319323
void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
@@ -334,9 +338,8 @@ class MapBlock /*: public NodeContainer*/
334338
is not valid on this MapBlock.
335339
*/
336340
bool isValidPositionParent(v3s16 p);
337-
MapNode getNodeParent(v3s16 p);
341+
MapNode getNodeParent(v3s16 p, bool *is_valid_position = NULL);
338342
void setNodeParent(v3s16 p, MapNode & n);
339-
MapNode getNodeParentNoEx(v3s16 p);
340343

341344
void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
342345
{

‎src/particles.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,19 @@ void Particle::step(float dtime)
168168
void Particle::updateLight()
169169
{
170170
u8 light = 0;
171-
try{
172-
v3s16 p = v3s16(
173-
floor(m_pos.X+0.5),
174-
floor(m_pos.Y+0.5),
175-
floor(m_pos.Z+0.5)
176-
);
177-
MapNode n = m_env->getClientMap().getNode(p);
171+
bool pos_ok;
172+
173+
v3s16 p = v3s16(
174+
floor(m_pos.X+0.5),
175+
floor(m_pos.Y+0.5),
176+
floor(m_pos.Z+0.5)
177+
);
178+
MapNode n = m_env->getClientMap().getNodeNoEx(p, &pos_ok);
179+
if (pos_ok)
178180
light = n.getLightBlend(m_env->getDayNightRatio(), m_gamedef->ndef());
179-
}
180-
catch(InvalidPositionException &e){
181+
else
181182
light = blend_light(m_env->getDayNightRatio(), LIGHT_SUN, 0);
182-
}
183+
183184
m_light = decode_light(light);
184185
}
185186

‎src/script/lua_api/l_env.cpp

+11-12
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,15 @@ int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
159159
// pos
160160
v3s16 pos = read_v3s16(L, 1);
161161
// Do it
162-
try{
163-
MapNode n = env->getMap().getNode(pos);
162+
bool pos_ok;
163+
MapNode n = env->getMap().getNodeNoEx(pos, &pos_ok);
164+
if (pos_ok) {
164165
// Return node
165166
pushnode(L, n, env->getGameDef()->ndef());
166-
return 1;
167-
} catch(InvalidPositionException &e)
168-
{
167+
} else {
169168
lua_pushnil(L);
170-
return 1;
171169
}
170+
return 1;
172171
}
173172

174173
// get_node_light(pos, timeofday)
@@ -185,16 +184,16 @@ int ModApiEnvMod::l_get_node_light(lua_State *L)
185184
time_of_day = 24000.0 * lua_tonumber(L, 2);
186185
time_of_day %= 24000;
187186
u32 dnr = time_to_daynight_ratio(time_of_day, true);
188-
try{
189-
MapNode n = env->getMap().getNode(pos);
187+
188+
bool is_position_ok;
189+
MapNode n = env->getMap().getNodeNoEx(pos, &is_position_ok);
190+
if (is_position_ok) {
190191
INodeDefManager *ndef = env->getGameDef()->ndef();
191192
lua_pushinteger(L, n.getLightBlend(dnr, ndef));
192-
return 1;
193-
} catch(InvalidPositionException &e)
194-
{
193+
} else {
195194
lua_pushnil(L);
196-
return 1;
197195
}
196+
return 1;
198197
}
199198

200199
// place_node(pos, node)

‎src/server.cpp

+13-16
Original file line numberDiff line numberDiff line change
@@ -2424,17 +2424,18 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
24242424
somebody is cheating, by checking the timing.
24252425
*/
24262426
MapNode n(CONTENT_IGNORE);
2427-
try
2428-
{
2429-
n = m_env->getMap().getNode(p_under);
2430-
}
2431-
catch(InvalidPositionException &e)
2432-
{
2427+
bool pos_ok;
2428+
n = m_env->getMap().getNodeNoEx(p_under, &pos_ok);
2429+
if (pos_ok)
2430+
n = m_env->getMap().getNodeNoEx(p_under, &pos_ok);
2431+
2432+
if (!pos_ok) {
24332433
infostream<<"Server: Not punching: Node not found."
24342434
<<" Adding block to emerge queue."
24352435
<<std::endl;
24362436
m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above), false);
24372437
}
2438+
24382439
if(n.getContent() != CONTENT_IGNORE)
24392440
m_script->node_on_punch(p_under, n, playersao, pointed);
24402441
// Cheat prevention
@@ -2479,16 +2480,12 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
24792480
// Only digging of nodes
24802481
if(pointed.type == POINTEDTHING_NODE)
24812482
{
2482-
MapNode n(CONTENT_IGNORE);
2483-
try
2484-
{
2485-
n = m_env->getMap().getNode(p_under);
2486-
}
2487-
catch(InvalidPositionException &e)
2488-
{
2489-
infostream<<"Server: Not finishing digging: Node not found."
2490-
<<" Adding block to emerge queue."
2491-
<<std::endl;
2483+
bool pos_ok;
2484+
MapNode n = m_env->getMap().getNodeNoEx(p_under, &pos_ok);
2485+
if (!pos_ok) {
2486+
infostream << "Server: Not finishing digging: Node not found."
2487+
<< " Adding block to emerge queue."
2488+
<< std::endl;
24922489
m_emerge->enqueueBlockEmerge(peer_id, getNodeBlockPos(p_above), false);
24932490
}
24942491

0 commit comments

Comments
 (0)
Please sign in to comment.