add on_chunk_present and on_chunk_remove events

This commit is contained in:
MihailRis 2025-01-12 05:41:42 +03:00
parent cfa6b5e578
commit 22c7cb8398
9 changed files with 65 additions and 9 deletions

View File

@ -109,6 +109,8 @@ struct WorldFuncsSet {
bool onblockbroken;
bool onblockinteract;
bool onplayertick;
bool onchunkpresent;
bool onchunkremove;
};
class ContentPackRuntime {

View File

@ -89,7 +89,7 @@ WorldRenderer::WorldRenderer(
) {
auto& settings = engine.getSettings();
level.events->listen(
EVT_CHUNK_HIDDEN,
LevelEventType::CHUNK_HIDDEN,
[this](LevelEventType, Chunk* chunk) { chunks->unload(chunk); }
);
auto assets = engine.getAssets();

View File

@ -14,6 +14,7 @@
#include "scripting/scripting.hpp"
#include "lighting/Lighting.hpp"
#include "settings.hpp"
#include "world/LevelEvents.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
@ -26,6 +27,14 @@ LevelController::LevelController(
level(std::move(levelPtr)),
chunks(std::make_unique<ChunksController>(*level)),
playerTickClock(20, 3) {
level->events->listen(LevelEventType::CHUNK_PRESENT, [](auto, Chunk* chunk) {
scripting::on_chunk_present(*chunk, chunk->flags.loaded);
});
level->events->listen(LevelEventType::CHUNK_UNLOAD, [](auto, Chunk* chunk) {
scripting::on_chunk_remove(*chunk);
});
if (clientPlayer) {
chunks->lighting = std::make_unique<Lighting>(
level->content, *clientPlayer->chunks

View File

@ -24,6 +24,7 @@
#include "util/stringutil.hpp"
#include "util/timeutil.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "world/Level.hpp"
#include "interfaces/Process.hpp"
@ -411,6 +412,35 @@ bool scripting::on_block_interact(
);
}
void scripting::on_chunk_present(const Chunk& chunk, bool loaded) {
auto args = [&chunk, loaded](lua::State* L) {
lua::pushvec_stack<2>(L, {chunk.x, chunk.z});
lua::pushboolean(L, loaded);
return 3;
};
for (auto& [packid, pack] : content->getPacks()) {
if (pack->worldfuncsset.onchunkpresent) {
lua::emit_event(
lua::get_main_state(), packid + ":.chunkpresent", args
);
}
}
}
void scripting::on_chunk_remove(const Chunk& chunk) {
auto args = [&chunk](lua::State* L) {
lua::pushvec_stack<2>(L, {chunk.x, chunk.z});
return 2;
};
for (auto& [packid, pack] : content->getPacks()) {
if (pack->worldfuncsset.onchunkremove) {
lua::emit_event(
lua::get_main_state(), packid + ":.chunkremove", args
);
}
}
}
void scripting::on_player_tick(Player* player, int tps) {
auto args = [=](lua::State* L) {
lua::pushinteger(L, player ? player->getId() : -1);
@ -837,6 +867,10 @@ void scripting::load_world_script(
register_event(env, "on_block_interact", prefix + ":.blockinteract");
funcsset.onplayertick =
register_event(env, "on_player_tick", prefix + ":.playertick");
funcsset.onchunkpresent =
register_event(env, "on_chunk_present", prefix + ":.chunkpresent");
funcsset.onchunkremove =
register_event(env, "on_chunk_remove", prefix + ":.chunkremove");
}
void scripting::load_layout_script(

View File

@ -17,6 +17,7 @@ struct ContentPack;
class ContentIndices;
class Level;
class Block;
class Chunk;
class Player;
struct ItemDef;
class Inventory;
@ -84,6 +85,10 @@ namespace scripting {
Player* player, const Block& block, const glm::ivec3& pos
);
bool on_block_interact(Player* player, const Block& block, const glm::ivec3& pos);
void on_chunk_present(const Chunk& chunk, bool loaded);
void on_chunk_remove(const Chunk& chunk);
void on_player_tick(Player* player, int tps);
/// @brief Called on RMB click with the item selected

View File

@ -34,7 +34,7 @@ Chunks::Chunks(
areaMap(w, d) {
areaMap.setCenter(ox-w/2, oz-d/2);
areaMap.setOutCallback([this](int, int, const auto& chunk) {
this->events->trigger(EVT_CHUNK_HIDDEN, chunk.get());
this->events->trigger(LevelEventType::CHUNK_HIDDEN, chunk.get());
});
}
@ -323,7 +323,7 @@ void Chunks::resize(uint32_t newW, uint32_t newD) {
bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
if (areaMap.set(chunk->x, chunk->z, chunk)) {
if (events) {
events->trigger(LevelEventType::EVT_CHUNK_SHOWN, chunk.get());
events->trigger(LevelEventType::CHUNK_SHOWN, chunk.get());
}
return true;
}

View File

@ -12,6 +12,7 @@
#include "objects/Entities.hpp"
#include "voxels/blocks_agent.hpp"
#include "typedefs.hpp"
#include "world/LevelEvents.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "Block.hpp"
@ -125,6 +126,8 @@ std::shared_ptr<Chunk> GlobalChunks::create(int x, int z) {
chunk->flags.loadedLights = true;
}
chunk->blocksMetadata = regions.getBlocksData(chunk->x, chunk->z);
level.events->trigger(LevelEventType::CHUNK_PRESENT, chunk.get());
return chunk;
}

View File

@ -52,13 +52,14 @@ Level::Level(
entities->setNextID(worldInfo.nextEntityId);
}
events->listen(LevelEventType::EVT_CHUNK_SHOWN, [this](LevelEventType, Chunk* chunk) {
events->listen(LevelEventType::CHUNK_SHOWN, [this](LevelEventType, Chunk* chunk) {
chunks->incref(chunk);
});
events->listen(LevelEventType::EVT_CHUNK_HIDDEN, [this](LevelEventType, Chunk* chunk) {
events->listen(LevelEventType::CHUNK_HIDDEN, [this](LevelEventType, Chunk* chunk) {
chunks->decref(chunk);
});
chunks->setOnUnload([this](const Chunk& chunk) {
chunks->setOnUnload([this](Chunk& chunk) {
events->trigger(LevelEventType::CHUNK_UNLOAD, &chunk);
AABB aabb = chunk.getAABB();
entities->despawn(entities->getAllInside(aabb));
});

View File

@ -6,9 +6,11 @@
class Chunk;
enum LevelEventType {
EVT_CHUNK_SHOWN,
EVT_CHUNK_HIDDEN,
enum class LevelEventType {
CHUNK_SHOWN,
CHUNK_HIDDEN,
CHUNK_PRESENT,
CHUNK_UNLOAD,
};
using ChunkEventFunc = std::function<void(LevelEventType, Chunk*)>;