Skip to content

Commit c9144ae

Browse files
authoredMay 30, 2021
Add core.compare_block_status function (#11247)
Makes it possible to check the status of the mapblock in a future-extensible way.
1 parent 89f3991 commit c9144ae

File tree

10 files changed

+89
-4
lines changed

10 files changed

+89
-4
lines changed
 

‎doc/lua_api.txt

+13
Original file line numberDiff line numberDiff line change
@@ -5863,6 +5863,19 @@ Misc.
58635863
* If `transient` is `false` or absent, frees a persistent forceload.
58645864
If `true`, frees a transient forceload.
58655865

5866+
* `minetest.compare_block_status(pos, condition)`
5867+
* Checks whether the mapblock at positition `pos` is in the wanted condition.
5868+
* `condition` may be one of the following values:
5869+
* `"unknown"`: not in memory
5870+
* `"emerging"`: in the queue for loading from disk or generating
5871+
* `"loaded"`: in memory but inactive (no ABMs are executed)
5872+
* `"active"`: in memory and active
5873+
* Other values are reserved for future functionality extensions
5874+
* Return value, the comparison status:
5875+
* `false`: Mapblock does not fulfil the wanted condition
5876+
* `true`: Mapblock meets the requirement
5877+
* `nil`: Unsupported `condition` value
5878+
58665879
* `minetest.request_insecure_environment()`: returns an environment containing
58675880
insecure functions if the calling mod has been listed as trusted in the
58685881
`secure.trusted_mods` setting or security is disabled, otherwise returns

‎src/emerge.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ bool EmergeManager::enqueueBlockEmergeEx(
358358
}
359359

360360

361+
bool EmergeManager::isBlockInQueue(v3s16 pos)
362+
{
363+
MutexAutoLock queuelock(m_queue_mutex);
364+
return m_blocks_enqueued.find(pos) != m_blocks_enqueued.end();
365+
}
366+
367+
361368
//
362369
// Mapgen-related helper functions
363370
//

‎src/emerge.h

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ class EmergeManager {
174174
EmergeCompletionCallback callback,
175175
void *callback_param);
176176

177+
bool isBlockInQueue(v3s16 pos);
178+
177179
v3s16 getContainingChunk(v3s16 blockpos);
178180

179181
Mapgen *getCurrentMapgen();

‎src/map.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,11 @@ MapBlock *ServerMap::getBlockOrEmerge(v3s16 p3d)
15491549
return block;
15501550
}
15511551

1552+
bool ServerMap::isBlockInQueue(v3s16 pos)
1553+
{
1554+
return m_emerge && m_emerge->isBlockInQueue(pos);
1555+
}
1556+
15521557
// N.B. This requires no synchronization, since data will not be modified unless
15531558
// the VoxelManipulator being updated belongs to the same thread.
15541559
void ServerMap::updateVManip(v3s16 pos)

‎src/map.h

+2
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ class ServerMap : public Map
365365
*/
366366
MapBlock *getBlockOrEmerge(v3s16 p3d);
367367

368+
bool isBlockInQueue(v3s16 pos);
369+
368370
/*
369371
Database functions
370372
*/

‎src/mapblock.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class MapBlock
140140
//// Flags
141141
////
142142

143-
inline bool isDummy()
143+
inline bool isDummy() const
144144
{
145145
return !data;
146146
}

‎src/script/lua_api/l_env.cpp

+29-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
4646
#include "client/client.h"
4747
#endif
4848

49-
struct EnumString ModApiEnvMod::es_ClearObjectsMode[] =
49+
const EnumString ModApiEnvMod::es_ClearObjectsMode[] =
5050
{
5151
{CLEAR_OBJECTS_MODE_FULL, "full"},
5252
{CLEAR_OBJECTS_MODE_QUICK, "quick"},
5353
{0, NULL},
5454
};
5555

56+
const EnumString ModApiEnvMod::es_BlockStatusType[] =
57+
{
58+
{ServerEnvironment::BS_UNKNOWN, "unknown"},
59+
{ServerEnvironment::BS_EMERGING, "emerging"},
60+
{ServerEnvironment::BS_LOADED, "loaded"},
61+
{ServerEnvironment::BS_ACTIVE, "active"},
62+
{0, NULL},
63+
};
64+
5665
///////////////////////////////////////////////////////////////////////////////
5766

5867

@@ -1389,6 +1398,24 @@ int ModApiEnvMod::l_forceload_block(lua_State *L)
13891398
return 0;
13901399
}
13911400

1401+
// compare_block_status(nodepos)
1402+
int ModApiEnvMod::l_compare_block_status(lua_State *L)
1403+
{
1404+
GET_ENV_PTR;
1405+
1406+
v3s16 nodepos = check_v3s16(L, 1);
1407+
std::string condition_s = luaL_checkstring(L, 2);
1408+
auto status = env->getBlockStatus(getNodeBlockPos(nodepos));
1409+
1410+
int condition_i = -1;
1411+
if (!string_to_enum(es_BlockStatusType, condition_i, condition_s))
1412+
return 0; // Unsupported
1413+
1414+
lua_pushboolean(L, status >= condition_i);
1415+
return 1;
1416+
}
1417+
1418+
13921419
// forceload_free_block(blockpos)
13931420
// blockpos = {x=num, y=num, z=num}
13941421
int ModApiEnvMod::l_forceload_free_block(lua_State *L)
@@ -1462,6 +1489,7 @@ void ModApiEnvMod::Initialize(lua_State *L, int top)
14621489
API_FCT(transforming_liquid_add);
14631490
API_FCT(forceload_block);
14641491
API_FCT(forceload_free_block);
1492+
API_FCT(compare_block_status);
14651493
API_FCT(get_translated_string);
14661494
}
14671495

‎src/script/lua_api/l_env.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ class ModApiEnvMod : public ModApiBase {
195195
// stops forceloading a position
196196
static int l_forceload_free_block(lua_State *L);
197197

198+
// compare_block_status(nodepos)
199+
static int l_compare_block_status(lua_State *L);
200+
198201
// Get a string translated server side
199202
static int l_get_translated_string(lua_State * L);
200203

@@ -207,7 +210,8 @@ class ModApiEnvMod : public ModApiBase {
207210
static void Initialize(lua_State *L, int top);
208211
static void InitializeClient(lua_State *L, int top);
209212

210-
static struct EnumString es_ClearObjectsMode[];
213+
static const EnumString es_ClearObjectsMode[];
214+
static const EnumString es_BlockStatusType[];
211215
};
212216

213217
class LuaABM : public ActiveBlockModifier {

‎src/serverenvironment.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,21 @@ void ServerEnvironment::step(float dtime)
15421542
m_server->sendDetachedInventories(PEER_ID_INEXISTENT, true);
15431543
}
15441544

1545+
ServerEnvironment::BlockStatus ServerEnvironment::getBlockStatus(v3s16 blockpos)
1546+
{
1547+
if (m_active_blocks.contains(blockpos))
1548+
return BS_ACTIVE;
1549+
1550+
const MapBlock *block = m_map->getBlockNoCreateNoEx(blockpos);
1551+
if (block && !block->isDummy())
1552+
return BS_LOADED;
1553+
1554+
if (m_map->isBlockInQueue(blockpos))
1555+
return BS_EMERGING;
1556+
1557+
return BS_UNKNOWN;
1558+
}
1559+
15451560
u32 ServerEnvironment::addParticleSpawner(float exptime)
15461561
{
15471562
// Timers with lifetime 0 do not expire

‎src/serverenvironment.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,16 @@ class ServerEnvironment : public Environment
342342
void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
343343
float getMaxLagEstimate() { return m_max_lag_estimate; }
344344

345-
std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
345+
std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; }
346+
347+
// Sorted by how ready a mapblock is
348+
enum BlockStatus {
349+
BS_UNKNOWN,
350+
BS_EMERGING,
351+
BS_LOADED,
352+
BS_ACTIVE // always highest value
353+
};
354+
BlockStatus getBlockStatus(v3s16 blockpos);
346355

347356
// Sets the static object status all the active objects in the specified block
348357
// This is only really needed for deleting blocks from the map

0 commit comments

Comments
 (0)
Please sign in to comment.