Merge pull request #610 from MihailRis/on-block-tick

Add on_block_tick event
This commit is contained in:
MihailRis 2025-09-15 00:54:29 +03:00 committed by GitHub
commit 78574f2460
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 271 additions and 57 deletions

View File

@ -46,6 +46,13 @@ function on_blocks_tick(tps: int)
Called tps (20) times per second. Use 1/tps instead of `time.delta()`.
```lua
function on_block_tick(x, y, z, tps: number)
```
Called tps (20 / tick-interval) times per second for a block.
Use 1/tps instead of `time.delta()`.
```lua
function on_player_tick(playerid: int, tps: int)
```

View File

@ -46,6 +46,13 @@ function on_blocks_tick(tps: int)
Вызывается tps (20) раз в секунду. Используйте 1/tps вместо `time.delta()`.
```lua
function on_block_tick(x, y, z, tps: number)
```
Вызывается tps (20 / tick-interval) раз в секунду для конкретного блока.
Используйте 1/tps вместо `time.delta()`.
```lua
function on_player_tick(playerid: int, tps: int)
```

View File

@ -161,6 +161,75 @@ local function clean(iterable, checkFun, ...)
end
end
local updating_blocks = {}
local TYPE_REGISTER = 0
local TYPE_UNREGISTER = 1
block.__perform_ticks = function(delta)
for id, entry in pairs(updating_blocks) do
entry.timer = entry.timer + delta
local steps = math.floor(entry.timer / entry.delta * #entry / 3)
if steps == 0 then
goto continue
end
entry.timer = 0.0
local event = entry.event
local tps = entry.tps
for i=1, steps do
local x = entry[entry.pointer + 1]
local y = entry[entry.pointer + 2]
local z = entry[entry.pointer + 3]
entry.pointer = (entry.pointer + 3) % #entry
events.emit(event, x, y, z, tps)
end
::continue::
end
end
block.__process_register_events = function()
local register_events = block.__pull_register_events()
if not register_events then
return
end
for i=1, #register_events, 4 do
local header = register_events[i]
local type = bit.band(header, 0xFFFF)
local id = bit.rshift(header, 16)
local x = register_events[i + 1]
local y = register_events[i + 2]
local z = register_events[i + 3]
local list = updating_blocks[id]
if type == TYPE_REGISTER then
if not list then
list = {}
list.event = block.name(id) .. ".blocktick"
list.tps = 20 / (block.properties[id]["tick-interval"] or 1)
list.delta = 1.0 / list.tps
list.timer = 0.0
list.pointer = 0
updating_blocks[id] = list
end
table.insert(list, x)
table.insert(list, y)
table.insert(list, z)
elseif type == TYPE_UNREGISTER then
if list then
for j=1, #list, 3 do
if list[j] == x and list[j + 1] == y and list[j + 2] == z then
for k=1,3 do
table.remove(list, j)
end
j = j - 3
end
end
end
end
print(type, id, x, y, z)
end
end
network.__process_events = function()
local CLIENT_CONNECTED = 1
local CONNECTED_TO_SERVER = 2

View File

@ -6,7 +6,7 @@ local names = {
"shadeless", "ambient-occlusion", "breakable", "selectable", "grounded",
"hidden", "draw-group", "picking-item", "surface-replacement", "script-name",
"ui-layout", "inventory-size", "tick-interval", "overlay-texture",
"translucent", "fields", "particles", "icon-type", "icon", "placing-block",
"translucent", "fields", "particles", "icon-type", "icon", "placing-block",
"stack-size", "name", "script-file", "culling"
}
for name, _ in pairs(user_props) do

View File

@ -588,6 +588,8 @@ function __process_post_runnables()
end
network.__process_events()
block.__process_register_events()
block.__perform_ticks(time.delta())
end
function time.post_runnable(runnable)

View File

@ -707,6 +707,25 @@ static int l_has_tag(lua::State* L) {
return 0;
}
static int l_pull_register_events(lua::State* L) {
auto events = blocks_agent::pull_register_events();
if (events.empty())
return 0;
lua::createtable(L, events.size() * 4, 0);
for (int i = 0; i < events.size(); i++) {
const auto& event = events[i];
lua::pushinteger(L, static_cast<int>(event.type) | event.id << 16);
lua::rawseti(L, i * 4 + 1);
for (int j = 0; j < 3; j++) {
lua::pushinteger(L, event.coord[j]);
lua::rawseti(L, i * 4 + j + 2);
}
}
return 1;
}
const luaL_Reg blocklib[] = {
{"index", lua::wrap<l_index>},
{"name", lua::wrap<l_get_def>},
@ -747,5 +766,6 @@ const luaL_Reg blocklib[] = {
{"set_field", lua::wrap<l_set_field>},
{"reload_script", lua::wrap<l_reload_script>},
{"has_tag", lua::wrap<l_has_tag>},
{"__pull_register_events", lua::wrap<l_pull_register_events>},
{NULL, NULL}
};

View File

@ -455,7 +455,7 @@ namespace lua {
inline bool getfield(lua::State* L, const std::string& name, int idx = -1) {
lua_getfield(L, idx, name.c_str());
if (isnil(L, idx)) {
if (isnoneornil(L, -1)) {
pop(L);
return false;
}

View File

@ -24,6 +24,7 @@
#include "util/timeutil.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/blocks_agent.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "interfaces/Process.hpp"
@ -464,6 +465,7 @@ void scripting::on_chunk_present(const Chunk& chunk, bool loaded) {
);
}
}
blocks_agent::on_chunk_present(*content->getIndices(), chunk);
}
void scripting::on_chunk_remove(const Chunk& chunk) {
@ -478,6 +480,7 @@ void scripting::on_chunk_remove(const Chunk& chunk) {
);
}
}
blocks_agent::on_chunk_remove(*content->getIndices(), chunk);
}
void scripting::on_inventory_open(const Player* player, const Inventory& inventory) {
@ -648,6 +651,8 @@ void scripting::load_content_script(
register_event(env, "on_replaced", prefix + ".replaced");
funcsset.oninteract =
register_event(env, "on_interact", prefix + ".interact");
funcsset.onblocktick =
register_event(env, "on_block_tick", prefix + ".blocktick");
funcsset.onblockstick =
register_event(env, "on_blocks_tick", prefix + ".blockstick");
}

View File

@ -48,6 +48,7 @@ struct BlockFuncsSet {
bool onreplaced : 1;
bool oninteract : 1;
bool randupdate : 1;
bool onblocktick : 1;
bool onblockstick : 1;
};

View File

@ -171,10 +171,10 @@ void Chunks::eraseSegments(
blocks_agent::erase_segments(*this, def, state, x, y, z);
}
void Chunks::repairSegments(
void Chunks::restoreSegments(
const Block& def, blockstate state, int x, int y, int z
) {
blocks_agent::repair_segments(*this, def, state, x, y, z);
blocks_agent::restore_segments(*this, def, state, x, y, z);
}
bool Chunks::checkReplaceability(

View File

@ -28,7 +28,7 @@ class Chunks {
const ContentIndices& indices;
void eraseSegments(const Block& def, blockstate state, int x, int y, int z);
void repairSegments(
void restoreSegments(
const Block& def, blockstate state, int x, int y, int z
);
void setRotationExtended(

View File

@ -6,63 +6,54 @@
using namespace blocks_agent;
template <class Storage>
static inline bool set_block(
Storage& chunks,
int32_t x,
int32_t y,
int32_t z,
uint32_t id,
blockstate state
) {
if (y < 0 || y >= CHUNK_H) {
return false;
}
const auto& indices = chunks.getContentIndices();
int cx = floordiv<CHUNK_W>(x);
int cz = floordiv<CHUNK_D>(z);
Chunk* chunk = get_chunk(chunks, cx, cz);
if (chunk == nullptr) {
return false;
}
int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D;
size_t index = vox_index(lx, y, lz);
static std::vector<BlockRegisterEvent> block_register_events {};
// block finalization
voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
const auto& prevdef = indices.blocks.require(vox.id);
if (prevdef.inventorySize != 0) {
chunk->removeBlockInventory(lx, y, lz);
}
if (prevdef.rt.extended && !vox.state.segment) {
erase_segments(chunks, prevdef, vox.state, x, y, z);
}
if (prevdef.dataStruct) {
if (auto found = chunk->blocksMetadata.find(index)) {
chunk->blocksMetadata.free(found);
chunk->flags.unsaved = true;
chunk->flags.blocksData = true;
std::vector<BlockRegisterEvent> blocks_agent::pull_register_events() {
auto events = block_register_events;
block_register_events.clear();
return events;
}
static void on_chunk_register_event(
const ContentIndices& indices,
const Chunk& chunk,
BlockRegisterEvent::Type type
) {
for (int i = 0; i < CHUNK_VOL; i++) {
const auto& def =
indices.blocks.require(chunk.voxels[i].id);
if (def.rt.funcsset.onblocktick) {
int x = i % CHUNK_W + chunk.x * CHUNK_W;
int z = (i / CHUNK_W) % CHUNK_D + chunk.z * CHUNK_D;
int y = (i / CHUNK_W / CHUNK_D);
block_register_events.push_back(BlockRegisterEvent {
type, def.rt.id, {x, y, z}
});
}
}
}
// block initialization
const auto& newdef = indices.blocks.require(id);
vox.id = id;
vox.state = state;
chunk->setModifiedAndUnsaved();
if (!state.segment && newdef.rt.extended) {
repair_segments(chunks, newdef, state, x, y, z);
}
if (y < chunk->bottom)
chunk->bottom = y;
else if (y + 1 > chunk->top)
chunk->top = y + 1;
else if (id == 0)
chunk->flags.dirtyHeights = true;
void blocks_agent::on_chunk_present(
const ContentIndices& indices, const Chunk& chunk
) {
on_chunk_register_event(
indices, chunk, BlockRegisterEvent::Type::REGISTER_UPDATING
);
}
void blocks_agent::on_chunk_remove(
const ContentIndices& indices, const Chunk& chunk
) {
on_chunk_register_event(
indices, chunk, BlockRegisterEvent::Type::UNREGISTER_UPDATING
);
}
template <class Storage>
static void mark_neighboirs_modified(
Storage& chunks, int32_t cx, int32_t cz, int32_t lx, int32_t lz
) {
Chunk* chunk;
if (lx == 0 && (chunk = get_chunk(chunks, cx - 1, cz))) {
chunk->flags.modified = true;
}
@ -75,6 +66,103 @@ static inline bool set_block(
if (lz == CHUNK_D - 1 && (chunk = get_chunk(chunks, cx, cz + 1))) {
chunk->flags.modified = true;
}
}
static void refresh_chunk_heights(Chunk& chunk, bool isAir, int y) {
if (y < chunk.bottom)
chunk.bottom = y;
else if (y + 1 > chunk.top)
chunk.top = y + 1;
else if (isAir)
chunk.flags.dirtyHeights = true;
}
template <class Storage>
static void finalize_block(
Storage& chunks,
Chunk& chunk,
voxel& vox,
int32_t x, int32_t y, int32_t z,
int32_t lx, int32_t lz
) {
size_t index = vox_index(lx, y, lz);
const auto& indices = chunks.getContentIndices();
const auto& def = indices.blocks.require(vox.id);
if (def.inventorySize != 0) {
chunk.removeBlockInventory(lx, y, lz);
}
if (def.rt.extended && !vox.state.segment) {
erase_segments(chunks, def, vox.state, x, y, z);
}
if (def.dataStruct) {
if (auto found = chunk.blocksMetadata.find(index)) {
chunk.blocksMetadata.free(found);
chunk.flags.unsaved = true;
chunk.flags.blocksData = true;
}
}
if (def.rt.funcsset.onblocktick) {
block_register_events.push_back(BlockRegisterEvent {
BlockRegisterEvent::Type::UNREGISTER_UPDATING, def.rt.id, {x, y, z}
});
}
}
template <class Storage>
static void initialize_block(
Storage& chunks,
Chunk& chunk,
voxel& vox,
blockid_t id,
blockstate state,
int32_t x, int32_t y, int32_t z,
int32_t lx, int32_t lz,
int32_t cx, int32_t cz
) {
const auto& indices = chunks.getContentIndices();
const auto& def = indices.blocks.require(id);
vox.id = id;
vox.state = state;
chunk.setModifiedAndUnsaved();
if (!state.segment && def.rt.extended) {
restore_segments(chunks, def, state, x, y, z);
}
refresh_chunk_heights(chunk, id == BLOCK_AIR, y);
mark_neighboirs_modified(chunks, cx, cz, lx, lz);
if (def.rt.funcsset.onblocktick) {
block_register_events.push_back(BlockRegisterEvent {
BlockRegisterEvent::Type::REGISTER_UPDATING, def.rt.id, {x, y, z}
});
}
}
template <class Storage>
static inline bool set_block(
Storage& chunks,
int32_t x,
int32_t y,
int32_t z,
blockid_t id,
blockstate state
) {
if (y < 0 || y >= CHUNK_H) {
return false;
}
int cx = floordiv<CHUNK_W>(x);
int cz = floordiv<CHUNK_D>(z);
Chunk* chunk = get_chunk(chunks, cx, cz);
if (chunk == nullptr) {
return false;
}
int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D;
voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
finalize_block(chunks, *chunk, vox, x, y, z, lx, lz);
initialize_block(chunks, *chunk, vox, id, state, x, y, z, lx, lz, cx, cz);
return true;
}

View File

@ -24,6 +24,21 @@ struct AABB;
namespace blocks_agent {
struct BlockRegisterEvent {
enum class Type : uint16_t {
REGISTER_UPDATING,
UNREGISTER_UPDATING,
};
Type type;
blockid_t id;
glm::ivec3 coord;
};
std::vector<BlockRegisterEvent> pull_register_events();
void on_chunk_present(const ContentIndices& indices, const Chunk& chunk);
void on_chunk_remove(const ContentIndices& indices, const Chunk& chunk);
/// @brief Get specified chunk.
/// @tparam Storage
/// @param chunks
@ -191,7 +206,7 @@ static constexpr inline uint8_t segment_to_int(int sx, int sy, int sz) {
/// @param y origin position Y
/// @param z origin position Z
template <class Storage>
inline void repair_segments(
inline void restore_segments(
Storage& chunks, const Block& def, blockstate state, int x, int y, int z
) {
const auto& rotation = def.rotations.variants[state.rotation];