move block ticks register to separate script
This commit is contained in:
parent
9653b18143
commit
473f9f1a6a
@ -171,73 +171,6 @@ 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
|
|
||||||
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
|
|
||||||
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
|
||||||
|
|||||||
66
res/scripts/internal_events.lua
Normal file
66
res/scripts/internal_events.lua
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
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 event_bits = 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 bit.band(event_bits, TYPE_REGISTER) ~= 0 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 bit.band(event_bits, TYPE_UNREGISTER) ~= 0 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
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -730,7 +730,7 @@ static int l_pull_register_events(lua::State* L) {
|
|||||||
lua::createtable(L, events.size() * 4, 0);
|
lua::createtable(L, events.size() * 4, 0);
|
||||||
for (int i = 0; i < events.size(); i++) {
|
for (int i = 0; i < events.size(); i++) {
|
||||||
const auto& event = events[i];
|
const auto& event = events[i];
|
||||||
lua::pushinteger(L, static_cast<int>(event.type) | event.id << 16);
|
lua::pushinteger(L, static_cast<int>(event.bits) | event.id << 16);
|
||||||
lua::rawseti(L, i * 4 + 1);
|
lua::rawseti(L, i * 4 + 1);
|
||||||
|
|
||||||
for (int j = 0; j < 3; j++) {
|
for (int j = 0; j < 3; j++) {
|
||||||
|
|||||||
@ -72,6 +72,7 @@ void scripting::initialize(Engine* engine) {
|
|||||||
|
|
||||||
load_script(io::path("stdlib.lua"), true);
|
load_script(io::path("stdlib.lua"), true);
|
||||||
load_script(io::path("classes.lua"), true);
|
load_script(io::path("classes.lua"), true);
|
||||||
|
load_script(io::path("internal_events.lua"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
class LuaCoroutine : public Process {
|
class LuaCoroutine : public Process {
|
||||||
|
|||||||
@ -14,39 +14,46 @@ std::vector<BlockRegisterEvent> blocks_agent::pull_register_events() {
|
|||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint16_t get_events_bits(bool present, const Block& def) {
|
||||||
|
uint16_t bits = 0;
|
||||||
|
if (def.rt.funcsset.onblocktick) {
|
||||||
|
bits |= present ? BlockRegisterEvent::REGISTER_UPDATING_BIT
|
||||||
|
: BlockRegisterEvent::UNREGISTER_UPDATING_BIT;
|
||||||
|
}
|
||||||
|
return bits;
|
||||||
|
}
|
||||||
|
|
||||||
static void on_chunk_register_event(
|
static void on_chunk_register_event(
|
||||||
const ContentIndices& indices,
|
const ContentIndices& indices,
|
||||||
const Chunk& chunk,
|
const Chunk& chunk,
|
||||||
BlockRegisterEvent::Type type
|
bool present
|
||||||
) {
|
) {
|
||||||
for (int i = 0; i < CHUNK_VOL; i++) {
|
for (int i = 0; i < CHUNK_VOL; i++) {
|
||||||
const auto& def =
|
const auto& def =
|
||||||
indices.blocks.require(chunk.voxels[i].id);
|
indices.blocks.require(chunk.voxels[i].id);
|
||||||
if (def.rt.funcsset.onblocktick) {
|
uint16_t bits = get_events_bits(present, def);
|
||||||
int x = i % CHUNK_W + chunk.x * CHUNK_W;
|
if (bits == 0) {
|
||||||
int z = (i / CHUNK_W) % CHUNK_D + chunk.z * CHUNK_D;
|
continue;
|
||||||
int y = (i / CHUNK_W / CHUNK_D);
|
|
||||||
block_register_events.push_back(BlockRegisterEvent {
|
|
||||||
type, def.rt.id, {x, y, z}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
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 {
|
||||||
|
bits, def.rt.id, {x, y, z}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void blocks_agent::on_chunk_present(
|
void blocks_agent::on_chunk_present(
|
||||||
const ContentIndices& indices, const Chunk& chunk
|
const ContentIndices& indices, const Chunk& chunk
|
||||||
) {
|
) {
|
||||||
on_chunk_register_event(
|
on_chunk_register_event(indices, chunk, true);
|
||||||
indices, chunk, BlockRegisterEvent::Type::REGISTER_UPDATING
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void blocks_agent::on_chunk_remove(
|
void blocks_agent::on_chunk_remove(
|
||||||
const ContentIndices& indices, const Chunk& chunk
|
const ContentIndices& indices, const Chunk& chunk
|
||||||
) {
|
) {
|
||||||
on_chunk_register_event(
|
on_chunk_register_event(indices, chunk, false);
|
||||||
indices, chunk, BlockRegisterEvent::Type::UNREGISTER_UPDATING
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Storage>
|
template <class Storage>
|
||||||
@ -101,11 +108,14 @@ static void finalize_block(
|
|||||||
chunk.flags.blocksData = true;
|
chunk.flags.blocksData = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (def.rt.funcsset.onblocktick) {
|
|
||||||
block_register_events.push_back(BlockRegisterEvent {
|
uint16_t bits = get_events_bits(false, def);
|
||||||
BlockRegisterEvent::Type::UNREGISTER_UPDATING, def.rt.id, {x, y, z}
|
if (bits == 0) {
|
||||||
});
|
return;
|
||||||
}
|
}
|
||||||
|
block_register_events.push_back(BlockRegisterEvent {
|
||||||
|
bits, def.rt.id, {x, y, z}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Storage>
|
template <class Storage>
|
||||||
@ -131,9 +141,17 @@ 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);
|
||||||
|
|
||||||
|
uint16_t bits = get_events_bits(true, def);
|
||||||
|
if (bits == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
block_register_events.push_back(BlockRegisterEvent {
|
||||||
|
bits, def.rt.id, {x, y, z}
|
||||||
|
});
|
||||||
|
|
||||||
if (def.rt.funcsset.onblocktick) {
|
if (def.rt.funcsset.onblocktick) {
|
||||||
block_register_events.push_back(BlockRegisterEvent {
|
block_register_events.push_back(BlockRegisterEvent {
|
||||||
BlockRegisterEvent::Type::REGISTER_UPDATING, def.rt.id, {x, y, z}
|
bits, def.rt.id, {x, y, z}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,11 +25,10 @@ struct AABB;
|
|||||||
namespace blocks_agent {
|
namespace blocks_agent {
|
||||||
|
|
||||||
struct BlockRegisterEvent {
|
struct BlockRegisterEvent {
|
||||||
enum class Type : uint16_t {
|
static inline constexpr uint16_t REGISTER_UPDATING_BIT = 0x1;
|
||||||
REGISTER_UPDATING,
|
static inline constexpr uint16_t UNREGISTER_UPDATING_BIT = 0x2;
|
||||||
UNREGISTER_UPDATING,
|
static inline constexpr uint16_t PRESENT_EVENT_BIT = 0x4;
|
||||||
};
|
uint16_t bits;
|
||||||
Type type;
|
|
||||||
blockid_t id;
|
blockid_t id;
|
||||||
glm::ivec3 coord;
|
glm::ivec3 coord;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user