Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an API function for synchronously activating areas #11609

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -5266,6 +5266,12 @@ Environment access
* Load the mapblocks containing the area from `pos1` to `pos2`.
`pos2` defaults to `pos1` if not specified.
* This function does not trigger map generation.
* `minetest.activate_area(pos1, pos2)`
* Activate the mapblocks containing the area from `pos1` to `pos2`.
* The area will remain active at least until control passes from the Lua
code back to the Minetest engine.
* Mapblocks are not activated if they do not exist or are already active.
* This function does not trigger map generation.
* `minetest.emerge_area(pos1, pos2, [callback], [param])`
* Queue all blocks in the area from `pos1` to `pos2`, inclusive, to be
asynchronously fetched from memory, loaded from disk, or if inexistent,
Expand Down
28 changes: 27 additions & 1 deletion src/script/lua_api/l_env.cpp
Expand Up @@ -1181,7 +1181,6 @@ int ModApiEnvMod::l_raycast(lua_State *L)
int ModApiEnvMod::l_load_area(lua_State *L)
{
GET_ENV_PTR;
MAP_LOCK_REQUIRED;

Map *map = &(env->getMap());
v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 1));
Expand All @@ -1200,6 +1199,32 @@ int ModApiEnvMod::l_load_area(lua_State *L)
return 0;
}

// activate_area(p1, p2)
// temporarily activate existing mapblocks in area p1..p2
int ModApiEnvMod::l_activate_area(lua_State *L)
{
GET_ENV_PTR;

Map &map = env->getMap();
v3s16 bpmin = getNodeBlockPos(check_v3s16(L, 1));
v3s16 bpmax = getNodeBlockPos(check_v3s16(L, 2));
sortBoxVerticies(bpmin, bpmax);
for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
for (s16 x = bpmin.X; x <= bpmax.X; x++) {
v3s16 bp(x, y, z);
// blocks are skipped if they are already activated:
if (env->getBlockStatus(bp) < ServerEnvironment::BS_ACTIVE) {
MapBlock *block = map.emergeBlock(bp, false);
// blocks are skipped if they do not exist:
if (block)
env->activateBlock(block, 0);
}
}

return 0;
}

// emerge_area(p1, p2, [callback, context])
// emerge mapblocks in area p1..p2, calls callback with context upon completion
int ModApiEnvMod::l_emerge_area(lua_State *L)
Expand Down Expand Up @@ -1476,6 +1501,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
API_FCT(find_nodes_in_area_under_air);
API_FCT(fix_light);
API_FCT(load_area);
API_FCT(activate_area);
API_FCT(emerge_area);
API_FCT(delete_area);
API_FCT(get_perlin);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_env.h
Expand Up @@ -149,6 +149,9 @@ class ModApiEnvMod : public ModApiBase {
// load_area(p1)
static int l_load_area(lua_State *L);

// activate_area(p1, p2)
static int l_activate_area(lua_State *L);

// emerge_area(p1, p2)
static int l_emerge_area(lua_State *L);

Expand Down