add 'on_block_tick' event
This commit is contained in:
parent
bb35fc665f
commit
b863d47d7f
@ -161,6 +161,73 @@ local function clean(iterable, checkFun, ...)
|
|||||||
end
|
end
|
||||||
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
|
||||||
|
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)
|
||||||
|
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.delta = 1.0 / (20.0 / (block.properties[id]["tick-interval"] or 1))
|
||||||
|
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()
|
network.__process_events = function()
|
||||||
local CLIENT_CONNECTED = 1
|
local CLIENT_CONNECTED = 1
|
||||||
local CONNECTED_TO_SERVER = 2
|
local CONNECTED_TO_SERVER = 2
|
||||||
|
|||||||
@ -588,6 +588,8 @@ function __process_post_runnables()
|
|||||||
end
|
end
|
||||||
|
|
||||||
network.__process_events()
|
network.__process_events()
|
||||||
|
block.__process_register_events()
|
||||||
|
block.__perform_ticks(time.delta())
|
||||||
end
|
end
|
||||||
|
|
||||||
function time.post_runnable(runnable)
|
function time.post_runnable(runnable)
|
||||||
|
|||||||
@ -707,6 +707,25 @@ static int l_has_tag(lua::State* L) {
|
|||||||
return 0;
|
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[] = {
|
const luaL_Reg blocklib[] = {
|
||||||
{"index", lua::wrap<l_index>},
|
{"index", lua::wrap<l_index>},
|
||||||
{"name", lua::wrap<l_get_def>},
|
{"name", lua::wrap<l_get_def>},
|
||||||
@ -747,5 +766,6 @@ const luaL_Reg blocklib[] = {
|
|||||||
{"set_field", lua::wrap<l_set_field>},
|
{"set_field", lua::wrap<l_set_field>},
|
||||||
{"reload_script", lua::wrap<l_reload_script>},
|
{"reload_script", lua::wrap<l_reload_script>},
|
||||||
{"has_tag", lua::wrap<l_has_tag>},
|
{"has_tag", lua::wrap<l_has_tag>},
|
||||||
|
{"__pull_register_events", lua::wrap<l_pull_register_events>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -24,6 +24,7 @@
|
|||||||
#include "util/timeutil.hpp"
|
#include "util/timeutil.hpp"
|
||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
#include "voxels/Chunk.hpp"
|
#include "voxels/Chunk.hpp"
|
||||||
|
#include "voxels/blocks_agent.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "world/World.hpp"
|
#include "world/World.hpp"
|
||||||
#include "interfaces/Process.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) {
|
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) {
|
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");
|
register_event(env, "on_replaced", prefix + ".replaced");
|
||||||
funcsset.oninteract =
|
funcsset.oninteract =
|
||||||
register_event(env, "on_interact", prefix + ".interact");
|
register_event(env, "on_interact", prefix + ".interact");
|
||||||
|
funcsset.onblocktick =
|
||||||
|
register_event(env, "on_block_tick", prefix + ".blocktick");
|
||||||
funcsset.onblockstick =
|
funcsset.onblockstick =
|
||||||
register_event(env, "on_blocks_tick", prefix + ".blockstick");
|
register_event(env, "on_blocks_tick", prefix + ".blockstick");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,6 +48,7 @@ struct BlockFuncsSet {
|
|||||||
bool onreplaced : 1;
|
bool onreplaced : 1;
|
||||||
bool oninteract : 1;
|
bool oninteract : 1;
|
||||||
bool randupdate : 1;
|
bool randupdate : 1;
|
||||||
|
bool onblocktick : 1;
|
||||||
bool onblockstick : 1;
|
bool onblockstick : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,49 @@
|
|||||||
|
|
||||||
using namespace blocks_agent;
|
using namespace blocks_agent;
|
||||||
|
|
||||||
|
static std::vector<BlockRegisterEvent> block_register_events {};
|
||||||
|
|
||||||
|
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}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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>
|
template <class Storage>
|
||||||
static void mark_neighboirs_modified(
|
static void mark_neighboirs_modified(
|
||||||
Storage& chunks, int32_t cx, int32_t cz, int32_t lx, int32_t lz
|
Storage& chunks, int32_t cx, int32_t cz, int32_t lx, int32_t lz
|
||||||
@ -58,6 +101,11 @@ static void finalize_block(
|
|||||||
chunk.flags.blocksData = 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>
|
template <class Storage>
|
||||||
@ -82,6 +130,12 @@ static void initialize_block(
|
|||||||
|
|
||||||
refresh_chunk_heights(chunk, id == BLOCK_AIR, y);
|
refresh_chunk_heights(chunk, id == BLOCK_AIR, y);
|
||||||
mark_neighboirs_modified(chunks, cx, cz, lx, lz);
|
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>
|
template <class Storage>
|
||||||
|
|||||||
@ -24,6 +24,21 @@ struct AABB;
|
|||||||
|
|
||||||
namespace blocks_agent {
|
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.
|
/// @brief Get specified chunk.
|
||||||
/// @tparam Storage
|
/// @tparam Storage
|
||||||
/// @param chunks
|
/// @param chunks
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user