on_blocks_tick event

This commit is contained in:
MihailRis
2024-01-22 14:28:39 +03:00
parent 0c2b58c8ac
commit 5188ee7bd8
5 changed files with 36 additions and 2 deletions
+23 -2
View File
@@ -40,11 +40,16 @@ int Clock::getPart() const {
return tickParts-tickPartsUndone-1; return tickParts-tickPartsUndone-1;
} }
int Clock::getTickRate() const {
return tickRate;
}
BlocksController::BlocksController(Level* level, uint padding) BlocksController::BlocksController(Level* level, uint padding)
: level(level), : level(level),
chunks(level->chunks), chunks(level->chunks),
lighting(level->lighting), lighting(level->lighting),
randTickClock(20, 3), randTickClock(20, 3),
blocksTickClock(20, 1),
padding(padding) { padding(padding) {
} }
@@ -84,10 +89,26 @@ void BlocksController::update(float delta) {
if (randTickClock.update(delta)) { if (randTickClock.update(delta)) {
randomTick(randTickClock.getPart(), randTickClock.getParts()); 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) { void BlocksController::randomTick(int tickid, int parts) {
// timeutil::ScopeLogTimer timer(5000+tickid);
const int w = chunks->w; const int w = chunks->w;
const int d = chunks->d; const int d = chunks->d;
int segments = 4; int segments = 4;
@@ -99,7 +120,7 @@ void BlocksController::randomTick(int tickid, int parts) {
int index = z * w + x; int index = z * w + x;
if ((index + tickid) % parts != 0) if ((index + tickid) % parts != 0)
continue; continue;
std::shared_ptr<Chunk> chunk = chunks->chunks[index]; auto chunk = chunks->chunks[index];
if (chunk == nullptr || !chunk->isLighted()) if (chunk == nullptr || !chunk->isLighted())
continue; continue;
for (int s = 0; s < segments; s++) { for (int s = 0; s < segments; s++) {
+3
View File
@@ -23,6 +23,7 @@ public:
int getParts() const; int getParts() const;
int getPart() const; int getPart() const;
int getTickRate() const;
}; };
class BlocksController { class BlocksController {
@@ -30,6 +31,7 @@ class BlocksController {
Chunks* chunks; Chunks* chunks;
Lighting* lighting; Lighting* lighting;
Clock randTickClock; Clock randTickClock;
Clock blocksTickClock;
uint padding; uint padding;
public: public:
BlocksController(Level* level, uint padding); BlocksController(Level* level, uint padding);
@@ -41,6 +43,7 @@ public:
void update(float delta); void update(float delta);
void randomTick(int tickid, int parts); void randomTick(int tickid, int parts);
void onBlocksTick(int tickid, int parts);
}; };
#endif // LOGIC_BLOCKS_CONTROLLER_H_ #endif // LOGIC_BLOCKS_CONTROLLER_H_
+8
View File
@@ -112,6 +112,13 @@ void scripting::on_world_quit() {
scripting::content = nullptr; 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) { void scripting::update_block(const Block* block, int x, int y, int z) {
std::string name = block->name+".update"; std::string name = block->name+".update";
lua_getglobal(L, name.c_str()); 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->onbroken=rename_global(L, "on_broken", (prefix+".broken").c_str());
funcsset->onplaced=rename_global(L, "on_placed", (prefix+".placed").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->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) { void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset) {
+1
View File
@@ -22,6 +22,7 @@ namespace scripting {
void initialize(Engine* engine); void initialize(Engine* engine);
void on_world_load(Level* level, BlocksController* blocks); void on_world_load(Level* level, BlocksController* blocks);
void on_world_quit(); 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 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 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); void on_block_placed(Player* player, const Block* block, int x, int y, int z);
+1
View File
@@ -27,6 +27,7 @@ struct block_funcs_set {
bool onbroken: 1; bool onbroken: 1;
bool oninteract: 1; bool oninteract: 1;
bool randupdate: 1; bool randupdate: 1;
bool onblockstick: 1;
}; };
struct CoordSystem { struct CoordSystem {