add 'on_block_present' event
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include "voxels/Chunks.hpp"
|
||||
#include "voxels/GlobalChunks.hpp"
|
||||
#include "world/Level.hpp"
|
||||
#include "world/LevelEvents.hpp"
|
||||
#include "world/World.hpp"
|
||||
#include "world/generator/WorldGenerator.hpp"
|
||||
|
||||
@@ -172,13 +173,12 @@ void ChunksController::createChunk(const Player& player, int x, int z) const {
|
||||
auto chunk = level.chunks->create(x, z);
|
||||
player.chunks->putChunk(chunk);
|
||||
auto& chunkFlags = chunk->flags;
|
||||
|
||||
if (!chunkFlags.loaded) {
|
||||
generator->generate(chunk->voxels, x, z);
|
||||
chunkFlags.unsaved = true;
|
||||
}
|
||||
chunk->updateHeights();
|
||||
|
||||
level.events->trigger(LevelEventType::CHUNK_PRESENT, chunk.get());
|
||||
if (!chunkFlags.loadedLights) {
|
||||
Lighting::prebuildSkyLight(*chunk, *level.content.getIndices());
|
||||
}
|
||||
|
||||
@@ -675,6 +675,8 @@ void scripting::load_content_script(
|
||||
register_event(env, "on_block_tick", prefix + ".blocktick");
|
||||
funcsset.onblockstick =
|
||||
register_event(env, "on_blocks_tick", prefix + ".blockstick");
|
||||
funcsset.onblockpresent =
|
||||
register_event(env, "on_block_present", prefix + ".blockpresent");
|
||||
}
|
||||
|
||||
void scripting::load_content_script(
|
||||
|
||||
@@ -50,6 +50,7 @@ struct BlockFuncsSet {
|
||||
bool randupdate : 1;
|
||||
bool onblocktick : 1;
|
||||
bool onblockstick : 1;
|
||||
bool onblockpresent : 1;
|
||||
};
|
||||
|
||||
struct CoordSystem {
|
||||
|
||||
@@ -127,8 +127,6 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
+19
-13
@@ -14,12 +14,12 @@ std::vector<BlockRegisterEvent> blocks_agent::pull_register_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;
|
||||
}
|
||||
static uint8_t get_events_bits(bool reg, const Block& def) {
|
||||
uint8_t bits = 0;
|
||||
auto funcsset = def.rt.funcsset;
|
||||
bits |= BlockRegisterEvent::REGISTER_BIT * reg;
|
||||
bits |= BlockRegisterEvent::UPDATING_BIT * funcsset.onblocktick;
|
||||
bits |= BlockRegisterEvent::PRESENT_EVENT_BIT * funcsset.onblockpresent;
|
||||
return bits;
|
||||
}
|
||||
|
||||
@@ -28,10 +28,16 @@ static void on_chunk_register_event(
|
||||
const Chunk& chunk,
|
||||
bool present
|
||||
) {
|
||||
for (int i = 0; i < CHUNK_VOL; i++) {
|
||||
const auto& def =
|
||||
indices.blocks.require(chunk.voxels[i].id);
|
||||
uint16_t bits = get_events_bits(present, def);
|
||||
const auto& voxels = chunk.voxels;
|
||||
|
||||
int totalBegin = chunk.bottom * (CHUNK_W * CHUNK_D);
|
||||
int totalEnd = chunk.top * (CHUNK_W * CHUNK_D);
|
||||
|
||||
for (int i = totalBegin; i <= totalEnd; i++) {
|
||||
blockid_t id = voxels[i].id;
|
||||
const auto& def =
|
||||
indices.blocks.require(id);
|
||||
uint8_t bits = get_events_bits(present, def);
|
||||
if (bits == 0) {
|
||||
continue;
|
||||
}
|
||||
@@ -39,7 +45,7 @@ static void on_chunk_register_event(
|
||||
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}
|
||||
bits, id, {x, y, z}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -109,7 +115,7 @@ static void finalize_block(
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t bits = get_events_bits(false, def);
|
||||
uint8_t bits = get_events_bits(false, def);
|
||||
if (bits == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -141,7 +147,7 @@ static void initialize_block(
|
||||
refresh_chunk_heights(chunk, id == BLOCK_AIR, y);
|
||||
mark_neighboirs_modified(chunks, cx, cz, lx, lz);
|
||||
|
||||
uint16_t bits = get_events_bits(true, def);
|
||||
uint8_t bits = get_events_bits(true, def);
|
||||
if (bits == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -25,10 +25,10 @@ struct AABB;
|
||||
namespace blocks_agent {
|
||||
|
||||
struct BlockRegisterEvent {
|
||||
static inline constexpr uint16_t REGISTER_UPDATING_BIT = 0x1;
|
||||
static inline constexpr uint16_t UNREGISTER_UPDATING_BIT = 0x2;
|
||||
static inline constexpr uint16_t PRESENT_EVENT_BIT = 0x4;
|
||||
uint16_t bits;
|
||||
static inline constexpr uint8_t REGISTER_BIT = 0x1;
|
||||
static inline constexpr uint8_t UPDATING_BIT = 0x2;
|
||||
static inline constexpr uint8_t PRESENT_EVENT_BIT = 0x4;
|
||||
uint8_t bits;
|
||||
blockid_t id;
|
||||
glm::ivec3 coord;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user