From 5b8855e83c0d1cc7aef21492e7fe862b7d06917e Mon Sep 17 00:00:00 2001 From: Craig Robbins Date: Fri, 14 Nov 2014 18:05:34 +1000 Subject: Remove most exceptions from getNode() (and variants) --- src/mapblock.h | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) (limited to 'src/mapblock.h') diff --git a/src/mapblock.h b/src/mapblock.h index 3879c5b0e..fa56f3318 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -251,37 +251,39 @@ public: Regular MapNode get-setters */ + bool isValidPosition(s16 x, s16 y, s16 z) + { + return data != NULL + && x >= 0 && x < MAP_BLOCKSIZE + && y >= 0 && y < MAP_BLOCKSIZE + && z >= 0 && z < MAP_BLOCKSIZE; + } + bool isValidPosition(v3s16 p) { - 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 isValidPosition(p.X, p.Y, p.Z); } - MapNode getNode(s16 x, s16 y, s16 z) + MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position) { - 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(); + *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) { -- cgit v1.2.3