Skip to content

Commit

Permalink
Change find_surface_nodes_in_area to find_nodes_in_area_under_air
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeno- committed Feb 27, 2015
1 parent 0f556d0 commit fdb9140
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/lua_api.txt
Expand Up @@ -1869,7 +1869,7 @@ and `minetest.auth_reload` call the authetification handler.
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
* `minetest.find_nodes_in_area(minp, maxp, nodenames)`: returns a list of positions
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
* `minetest.find_surface_nodes_in_area(minp, maxp, nodenames)`: returns a list of positions
* `minetest.find_nodes_in_area_under_air(minp, maxp, nodenames)`: returns a list of positions
* returned positions are nodes with a node air above
* `nodenames`: e.g. `{"ignore", "group:tree"}` or `"default:dirt"`
* `minetest.get_perlin(noiseparams)`
Expand Down
26 changes: 17 additions & 9 deletions src/script/lua_api/l_env.cpp
Expand Up @@ -570,17 +570,25 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
return 1;
}

// find_surface_nodes_in_area(minp, maxp, nodenames) -> list of positions
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
int ModApiEnvMod::l_find_surface_nodes_in_area(lua_State *L)
// find_nodes_in_area_under_air(minp, maxp, nodenames) -> list of positions
// nodenames: e.g. {"ignore", "group:tree"} or "default:dirt"
int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
{
/* Note: A similar but generalized (and therefore slower) version of this
* function could be created -- e.g. find_nodes_in_area_under -- which
* would accept a node name (or ID?) or list of names that the "above node"
* should be.
* TODO
*/

GET_ENV_PTR;

INodeDefManager *ndef = getServer(L)->ndef();
v3s16 minp = read_v3s16(L, 1);
v3s16 maxp = read_v3s16(L, 2);
std::set<content_t> filter;
if(lua_istable(L, 3)) {

if (lua_istable(L, 3)) {
int table = 3;
lua_pushnil(L);
while(lua_next(L, table) != 0) {
Expand All @@ -590,18 +598,18 @@ int ModApiEnvMod::l_find_surface_nodes_in_area(lua_State *L)
// 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);
}

lua_newtable(L);
u64 i = 0;
for(s16 x = minp.X; x <= maxp.X; x++)
for(s16 z = minp.Z; z <= maxp.Z; z++) {
for (s16 x = minp.X; x <= maxp.X; x++)
for (s16 z = minp.Z; z <= maxp.Z; z++) {
s16 y = minp.Y;
v3s16 p(x, y, z);
content_t c = env->getMap().getNodeNoEx(p).getContent();
for(; y <= maxp.Y; y++) {
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 &&
Expand Down Expand Up @@ -912,7 +920,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
API_FCT(get_gametime);
API_FCT(find_node_near);
API_FCT(find_nodes_in_area);
API_FCT(find_surface_nodes_in_area);
API_FCT(find_nodes_in_area_under_air);
API_FCT(delete_area);
API_FCT(get_perlin);
API_FCT(get_perlin_map);
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_env.h
Expand Up @@ -121,7 +121,7 @@ class ModApiEnvMod : public ModApiBase {

// find_surface_nodes_in_area(minp, maxp, nodenames) -> list of positions
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
static int l_find_surface_nodes_in_area(lua_State *L);
static int l_find_nodes_in_area_under_air(lua_State *L);

// delete_area(p1, p2) -> true/false
static int l_delete_area(lua_State *L);
Expand Down

2 comments on commit fdb9140

@Zeno-
Copy link
Contributor Author

@Zeno- Zeno- commented on fdb9140 Feb 27, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that my Note/TODO comment in l_env.cpp could have been clearer.

The function can be generalised, however if that is done then find_nodes_in_area_under_air must remain because a specialised function will always be faster than a generalised one. I.e. if a find_nodes_in_area_under is created in the future then find_nodes_in_area_under_air should still remain as part of the API.

@nerzhul
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Please sign in to comment.