From 5188ee7bd806167240441db0d682863808fc8e8f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 22 Jan 2024 14:28:39 +0300 Subject: [PATCH] on_blocks_tick event --- src/logic/BlocksController.cpp | 25 +++++++++++++++++++++++-- src/logic/BlocksController.h | 3 +++ src/logic/scripting/scripting.cpp | 8 ++++++++ src/logic/scripting/scripting.h | 1 + src/voxels/Block.h | 1 + 5 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 4d2b3655..5378010a 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -40,11 +40,16 @@ int Clock::getPart() const { return tickParts-tickPartsUndone-1; } +int Clock::getTickRate() const { + return tickRate; +} + BlocksController::BlocksController(Level* level, uint padding) : level(level), chunks(level->chunks), lighting(level->lighting), randTickClock(20, 3), + blocksTickClock(20, 1), padding(padding) { } @@ -84,10 +89,26 @@ void BlocksController::update(float delta) { if (randTickClock.update(delta)) { randomTick(randTickClock.getPart(), randTickClock.getParts()); } + if (blocksTickClock.update(delta)) { + onBlocksTick(blocksTickClock.getPart(), blocksTickClock.getParts()); + } +} + +void BlocksController::onBlocksTick(int tickid, int parts) { + auto content = level->content; + auto indices = content->getIndices(); + int tickRate = blocksTickClock.getTickRate(); + for (size_t id = 0; id < indices->countBlockDefs(); id++) { + if ((id + tickid) % parts != 0) + continue; + auto def = indices->getBlockDef(id); + if (def->rt.funcsset.onblockstick) { + scripting::on_blocks_tick(def, tickRate); + } + } } void BlocksController::randomTick(int tickid, int parts) { - // timeutil::ScopeLogTimer timer(5000+tickid); const int w = chunks->w; const int d = chunks->d; int segments = 4; @@ -99,7 +120,7 @@ void BlocksController::randomTick(int tickid, int parts) { int index = z * w + x; if ((index + tickid) % parts != 0) continue; - std::shared_ptr chunk = chunks->chunks[index]; + auto chunk = chunks->chunks[index]; if (chunk == nullptr || !chunk->isLighted()) continue; for (int s = 0; s < segments; s++) { diff --git a/src/logic/BlocksController.h b/src/logic/BlocksController.h index 454bd16b..b0dc0fe7 100644 --- a/src/logic/BlocksController.h +++ b/src/logic/BlocksController.h @@ -23,6 +23,7 @@ public: int getParts() const; int getPart() const; + int getTickRate() const; }; class BlocksController { @@ -30,6 +31,7 @@ class BlocksController { Chunks* chunks; Lighting* lighting; Clock randTickClock; + Clock blocksTickClock; uint padding; public: BlocksController(Level* level, uint padding); @@ -41,6 +43,7 @@ public: void update(float delta); void randomTick(int tickid, int parts); + void onBlocksTick(int tickid, int parts); }; #endif // LOGIC_BLOCKS_CONTROLLER_H_ diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index bddd54dd..6c1ab283 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -112,6 +112,13 @@ void scripting::on_world_quit() { scripting::content = nullptr; } +void scripting::on_blocks_tick(const Block* block, int tps) { + std::string name = block->name+".blockstick"; + lua_getglobal(L, name.c_str()); + lua_pushinteger(L, tps); + call_func(L, 1, name); +} + void scripting::update_block(const Block* block, int x, int y, int z) { std::string name = block->name+".update"; lua_getglobal(L, name.c_str()); @@ -188,6 +195,7 @@ void scripting::load_block_script(std::string prefix, fs::path file, block_funcs funcsset->onbroken=rename_global(L, "on_broken", (prefix+".broken").c_str()); funcsset->onplaced=rename_global(L, "on_placed", (prefix+".placed").c_str()); funcsset->oninteract=rename_global(L, "on_interact", (prefix+".oninteract").c_str()); + funcsset->onblockstick=rename_global(L, "on_blocks_tick", (prefix+".blockstick").c_str()); } void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset) { diff --git a/src/logic/scripting/scripting.h b/src/logic/scripting/scripting.h index c4cd6b05..6ce6af7f 100644 --- a/src/logic/scripting/scripting.h +++ b/src/logic/scripting/scripting.h @@ -22,6 +22,7 @@ namespace scripting { void initialize(Engine* engine); void on_world_load(Level* level, BlocksController* blocks); void on_world_quit(); + void on_blocks_tick(const Block* block, int tps); void update_block(const Block* block, int x, int y, int z); void random_update_block(const Block* block, int x, int y, int z); void on_block_placed(Player* player, const Block* block, int x, int y, int z); diff --git a/src/voxels/Block.h b/src/voxels/Block.h index 9f55f1c7..5157713d 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -27,6 +27,7 @@ struct block_funcs_set { bool onbroken: 1; bool oninteract: 1; bool randupdate: 1; + bool onblockstick: 1; }; struct CoordSystem {