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
This commit is contained in:
SmallJoker 2017-06-19 16:30:26 +02:00 committed by GitHub
parent 071736ba3f
commit 6eb03c135f
2 changed files with 49 additions and 22 deletions

@ -2465,12 +2465,13 @@ and `minetest.auth_reload` call the authetification handler.
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"` * `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
* `search_center` is an optional boolean (default: `false`) * `search_center` is an optional boolean (default: `false`)
If true `pos` is also checked for the nodes If true `pos` is also checked for the nodes
* `minetest.find_nodes_in_area(minp, maxp, nodenames)`: returns a list of positions * `minetest.find_nodes_in_area(pos1, pos2, nodenames)`: returns a list of positions
* returns as second value a table with the count of the individual nodes found
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"` * `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
* `minetest.find_nodes_in_area_under_air(minp, maxp, nodenames)`: returns a list of positions * First return value: Table with all node positions
* returned positions are nodes with a node air above * 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"` * `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(noiseparams)`
* `minetest.get_perlin(seeddiff, octaves, persistence, scale)` * `minetest.get_perlin(seeddiff, octaves, persistence, scale)`
* Return world-specific perlin noise (`int(worldseed)+seeddiff`) * Return world-specific perlin noise (`int(worldseed)+seeddiff`)

@ -651,38 +651,51 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
INodeDefManager *ndef = getServer(L)->ndef(); INodeDefManager *ndef = getServer(L)->ndef();
v3s16 minp = read_v3s16(L, 1); v3s16 minp = read_v3s16(L, 1);
v3s16 maxp = read_v3s16(L, 2); 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; std::set<content_t> filter;
if(lua_istable(L, 3)) { if (lua_istable(L, 3)) {
int table = 3;
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, table) != 0) { while (lua_next(L, 3) != 0) {
// key at index -2 and value at index -1 // key at index -2 and value at index -1
luaL_checktype(L, -1, LUA_TSTRING); luaL_checktype(L, -1, LUA_TSTRING);
ndef->getIds(lua_tostring(L, -1), filter); ndef->getIds(lua_tostring(L, -1), filter);
// removes value, keeps key for next iteration // removes value, keeps key for next iteration
lua_pop(L, 1); lua_pop(L, 1);
} }
} else if(lua_isstring(L, 3)) { } else if (lua_isstring(L, 3)) {
ndef->getIds(lua_tostring(L, 3), filter); 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); lua_newtable(L);
u64 i = 0; u64 i = 0;
for (s16 x = minp.X; x <= maxp.X; x++) for (s16 x = minp.X; x <= maxp.X; x++)
for (s16 y = minp.Y; y <= maxp.Y; y++) for (s16 y = minp.Y; y <= maxp.Y; y++)
for (s16 z = minp.Z; z <= maxp.Z; z++) { for (s16 z = minp.Z; z <= maxp.Z; z++) {
v3s16 p(x, y, z); v3s16 p(x, y, z);
content_t c = env->getMap().getNodeNoEx(p).getContent(); content_t c = env->getMap().getNodeNoEx(p).getContent();
if (filter.count(c) != 0) { if (filter.count(c) != 0) {
push_v3s16(L, p); push_v3s16(L, p);
lua_rawseti(L, -2, ++i); lua_rawseti(L, -2, ++i);
individual_count[c]++; individual_count[c]++;
} }
} }
lua_newtable(L); 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) { it != filter.end(); ++it) {
lua_pushnumber(L, individual_count[*it]); lua_pushnumber(L, individual_count[*it]);
lua_setfield(L, -2, ndef->get(*it).name.c_str()); 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(); INodeDefManager *ndef = getServer(L)->ndef();
v3s16 minp = read_v3s16(L, 1); v3s16 minp = read_v3s16(L, 1);
v3s16 maxp = read_v3s16(L, 2); 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; std::set<content_t> filter;
if (lua_istable(L, 3)) { if (lua_istable(L, 3)) {
int table = 3;
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, table) != 0) { while (lua_next(L, 3) != 0) {
// key at index -2 and value at index -1 // key at index -2 and value at index -1
luaL_checktype(L, -1, LUA_TSTRING); luaL_checktype(L, -1, LUA_TSTRING);
ndef->getIds(lua_tostring(L, -1), filter); 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++) { for (; y <= maxp.Y; y++) {
v3s16 psurf(x, y + 1, z); v3s16 psurf(x, y + 1, z);
content_t csurf = env->getMap().getNodeNoEx(psurf).getContent(); 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) { filter.count(c) != 0) {
push_v3s16(L, v3s16(x, y, z)); push_v3s16(L, v3s16(x, y, z));
lua_rawseti(L, -2, ++i); lua_rawseti(L, -2, ++i);