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,11 +651,24 @@ 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);
@ -666,7 +679,7 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
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;
@ -682,7 +695,7 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
} }
} }
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);