aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSmallJoker <SmallJoker@users.noreply.github.com>2017-06-19 16:30:26 +0200
committerGitHub <noreply@github.com>2017-06-19 16:30:26 +0200
commit6eb03c135f17f05d48dd13fe8957bfe0f9654b00 (patch)
tree52ba1f16498adcbe836a9f0cb1d7237ad28114e2
parent071736ba3f99c9827af074bed3b3ff84c05e0d51 (diff)
downloadminetest-6eb03c135f17f05d48dd13fe8957bfe0f9654b00.tar.gz
minetest-6eb03c135f17f05d48dd13fe8957bfe0f9654b00.tar.bz2
minetest-6eb03c135f17f05d48dd13fe8957bfe0f9654b00.zip
find_nodes_in_area: Extend maximal count to U32_MAX (#5277)
Extend documentation, limit area volume Remove u16 count limitation * Prevent integer overflow, replace minp/maxp with pos1/pos2
-rw-r--r--doc/lua_api.txt9
-rw-r--r--src/script/lua_api/l_env.cpp62
2 files changed, 49 insertions, 22 deletions
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 28067eef1..a3413221b 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -2465,12 +2465,13 @@ and `minetest.auth_reload` call the authetification handler.
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
* `search_center` is an optional boolean (default: `false`)
If true `pos` is also checked for the nodes
-* `minetest.find_nodes_in_area(minp, maxp, nodenames)`: returns a list of positions
- * returns as second value a table with the count of the individual nodes found
+* `minetest.find_nodes_in_area(pos1, pos2, nodenames)`: returns a list of positions
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
-* `minetest.find_nodes_in_area_under_air(minp, maxp, nodenames)`: returns a list of positions
- * returned positions are nodes with a node air above
+ * First return value: Table with all node positions
+ * Second return value: Table with the count of each node with the node name as index
+* `minetest.find_nodes_in_area_under_air(pos1, pos2, nodenames)`: returns a list of positions
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
+ * Return value: Table with all node positions with a node air above
* `minetest.get_perlin(noiseparams)`
* `minetest.get_perlin(seeddiff, octaves, persistence, scale)`
* Return world-specific perlin noise (`int(worldseed)+seeddiff`)
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index b8b6bc5ba..e7284b035 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -651,38 +651,51 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
INodeDefManager *ndef = getServer(L)->ndef();
v3s16 minp = read_v3s16(L, 1);
v3s16 maxp = read_v3s16(L, 2);
+ sortBoxVerticies(minp, maxp);
+
+ v3s16 cube = maxp - minp + 1;
+
+ /* Limit for too large areas, assume default values
+ * and give tolerances of 1 node on each side
+ * (chunksize * MAP_BLOCKSIZE + 2)^3 = 551368
+ */
+ if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 551368) {
+ luaL_error(L, "find_nodes_in_area(): area volume"
+ " exceeds allowed value of 551368");
+ return 0;
+ }
+
std::set<content_t> filter;
- if(lua_istable(L, 3)) {
- int table = 3;
+ if (lua_istable(L, 3)) {
lua_pushnil(L);
- while(lua_next(L, table) != 0) {
+ while (lua_next(L, 3) != 0) {
// key at index -2 and value at index -1
luaL_checktype(L, -1, LUA_TSTRING);
ndef->getIds(lua_tostring(L, -1), filter);
// removes value, keeps key for next iteration
lua_pop(L, 1);
}
- } else if(lua_isstring(L, 3)) {
+ } else if (lua_isstring(L, 3)) {
ndef->getIds(lua_tostring(L, 3), filter);
}
- std::map<content_t, u16> individual_count;
+ std::unordered_map<content_t, u32> individual_count;
lua_newtable(L);
u64 i = 0;
for (s16 x = minp.X; x <= maxp.X; x++)
- for (s16 y = minp.Y; y <= maxp.Y; y++)
- for (s16 z = minp.Z; z <= maxp.Z; z++) {
- v3s16 p(x, y, z);
- content_t c = env->getMap().getNodeNoEx(p).getContent();
- if (filter.count(c) != 0) {
- push_v3s16(L, p);
- lua_rawseti(L, -2, ++i);
- individual_count[c]++;
- }
+ for (s16 y = minp.Y; y <= maxp.Y; y++)
+ for (s16 z = minp.Z; z <= maxp.Z; z++) {
+ v3s16 p(x, y, z);
+ content_t c = env->getMap().getNodeNoEx(p).getContent();
+ if (filter.count(c) != 0) {
+ push_v3s16(L, p);
+ lua_rawseti(L, -2, ++i);
+ individual_count[c]++;
+ }
}
lua_newtable(L);
- for (std::set<content_t>::iterator it = filter.begin();
+ for (std::set<content_t>::const_iterator it = filter.begin();
it != filter.end(); ++it) {
lua_pushnumber(L, individual_count[*it]);
lua_setfield(L, -2, ndef->get(*it).name.c_str());
@@ -706,12 +719,25 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
INodeDefManager *ndef = getServer(L)->ndef();
v3s16 minp = read_v3s16(L, 1);
v3s16 maxp = read_v3s16(L, 2);
+ sortBoxVerticies(minp, maxp);
+
+ v3s16 cube = maxp - minp + 1;
+
+ /* Limit for too large areas, assume default values
+ * and give tolerances of 1 node on each side
+ * (chunksize * MAP_BLOCKSIZE + 2)^3 = 551368
+ */
+ if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 551368) {
+ luaL_error(L, "find_nodes_in_area_under_air(): area volume"
+ " exceeds allowed value of 551368");
+ return 0;
+ }
+
std::set<content_t> filter;
if (lua_istable(L, 3)) {
- int table = 3;
lua_pushnil(L);
- while(lua_next(L, table) != 0) {
+ while (lua_next(L, 3) != 0) {
// key at index -2 and value at index -1
luaL_checktype(L, -1, LUA_TSTRING);
ndef->getIds(lua_tostring(L, -1), filter);
@@ -732,7 +758,7 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
for (; y <= maxp.Y; y++) {
v3s16 psurf(x, y + 1, z);
content_t csurf = env->getMap().getNodeNoEx(psurf).getContent();
- if(c != CONTENT_AIR && csurf == CONTENT_AIR &&
+ if (c != CONTENT_AIR && csurf == CONTENT_AIR &&
filter.count(c) != 0) {
push_v3s16(L, v3s16(x, y, z));
lua_rawseti(L, -2, ++i);