mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-10 21:41:50 +00:00
Merge pull request #775 from MihailRis/optimizations
allocations-related optimizations
This commit is contained in:
commit
a636347edc
12 changed files with 207 additions and 84 deletions
|
|
@ -441,7 +441,8 @@ static void load_script(const Content& content, T& def) {
|
||||||
def.name,
|
def.name,
|
||||||
scriptfile,
|
scriptfile,
|
||||||
def.scriptFile,
|
def.scriptFile,
|
||||||
def.rt.funcsset
|
def.rt.funcsset,
|
||||||
|
def.rt.eventNames
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@ struct ItemFuncsSet {
|
||||||
bool on_block_break_by : 1;
|
bool on_block_break_by : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ItemFuncNamesCache {
|
||||||
|
};
|
||||||
|
|
||||||
enum class ItemIconType {
|
enum class ItemIconType {
|
||||||
NONE, // invisible (core:empty) must not be rendered
|
NONE, // invisible (core:empty) must not be rendered
|
||||||
SPRITE, // textured quad: icon is `atlas_name:texture_name`
|
SPRITE, // textured quad: icon is `atlas_name:texture_name`
|
||||||
|
|
@ -75,6 +78,8 @@ struct ItemDef {
|
||||||
bool emissive = false;
|
bool emissive = false;
|
||||||
|
|
||||||
std::set<int> tags;
|
std::set<int> tags;
|
||||||
|
|
||||||
|
ItemFuncNamesCache eventNames;
|
||||||
} rt {};
|
} rt {};
|
||||||
|
|
||||||
ItemDef(const std::string& name);
|
ItemDef(const std::string& name);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,3 @@
|
||||||
#include <iostream>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include "LightSolver.hpp"
|
#include "LightSolver.hpp"
|
||||||
#include "Lightmap.hpp"
|
#include "Lightmap.hpp"
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
|
|
@ -9,6 +6,8 @@
|
||||||
#include "voxels/voxel.hpp"
|
#include "voxels/voxel.hpp"
|
||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
LightSolver::LightSolver(const ContentIndices& contentIds, Chunks& chunks, int channel)
|
LightSolver::LightSolver(const ContentIndices& contentIds, Chunks& chunks, int channel)
|
||||||
: blockDefs(contentIds.blocks.getDefs()),
|
: blockDefs(contentIds.blocks.getDefs()),
|
||||||
chunks(chunks),
|
chunks(chunks),
|
||||||
|
|
@ -55,7 +54,7 @@ void LightSolver::remove(int x, int y, int z) {
|
||||||
lightmap.set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, 0);
|
lightmap.set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LightSolver::solve(){
|
void LightSolver::solve(Chunk* prevailingChunk) {
|
||||||
const int coords[] = {
|
const int coords[] = {
|
||||||
0, 0, 1,
|
0, 0, 1,
|
||||||
0, 0,-1,
|
0, 0,-1,
|
||||||
|
|
@ -66,47 +65,54 @@ void LightSolver::solve(){
|
||||||
};
|
};
|
||||||
|
|
||||||
while (!remqueue.empty()){
|
while (!remqueue.empty()){
|
||||||
const lightentry entry = remqueue.front();
|
lightentry entry = std::move(remqueue.front());
|
||||||
remqueue.pop();
|
remqueue.pop();
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
int imul3 = i*3;
|
int imul3 = i * 3;
|
||||||
int x = entry.x+coords[imul3];
|
int x = entry.x+coords[imul3];
|
||||||
int y = entry.y+coords[imul3+1];
|
int y = entry.y+coords[imul3+1];
|
||||||
int z = entry.z+coords[imul3+2];
|
int z = entry.z+coords[imul3+2];
|
||||||
|
|
||||||
Chunk* chunk = chunks.getChunkByVoxel(x,y,z);
|
Chunk* chunk = prevailingChunk;
|
||||||
if (chunk) {
|
if (chunk == nullptr || !chunk->isBlockInside(x, z)) {
|
||||||
int lx = x - chunk->x * CHUNK_W;
|
chunk = chunks.getChunkByVoxel(x,y,z);
|
||||||
int lz = z - chunk->z * CHUNK_D;
|
if (chunk == nullptr) {
|
||||||
chunk->flags.modified = true;
|
continue;
|
||||||
|
}
|
||||||
|
} else if (y < 0 || y >= CHUNK_H) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
assert(chunk->lightmap != nullptr);
|
int lx = x - chunk->x * CHUNK_W;
|
||||||
auto& lightmap = *chunk->lightmap;
|
int lz = z - chunk->z * CHUNK_D;
|
||||||
|
chunk->flags.modified = true;
|
||||||
|
|
||||||
ubyte light = lightmap.get(lx,y,lz, channel);
|
assert(chunk->lightmap != nullptr);
|
||||||
if (light != 0 && light == entry.light-1){
|
auto& lightmap = *chunk->lightmap;
|
||||||
voxel* vox = chunks.get(x, y, z);
|
|
||||||
if (vox && vox->id != 0) {
|
ubyte light = lightmap.get(lx,y,lz, channel);
|
||||||
const Block* block = blockDefs[vox->id];
|
if (light != 0 && light == entry.light-1) {
|
||||||
if (uint8_t emission = block->emission[channel]) {
|
voxel* vox = chunks.get(x, y, z);
|
||||||
addqueue.push(lightentry {x, y, z, emission});
|
if (vox && vox->id != 0) {
|
||||||
lightmap.set(lx, y, lz, channel, emission);
|
const Block* block = blockDefs[vox->id];
|
||||||
}
|
if (uint8_t emission = block->emission[channel]) {
|
||||||
else lightmap.set(lx, y, lz, channel, 0);
|
addqueue.push(lightentry {x, y, z, emission});
|
||||||
|
lightmap.set(lx, y, lz, channel, emission);
|
||||||
}
|
}
|
||||||
else lightmap.set(lx, y, lz, channel, 0);
|
else lightmap.set(lx, y, lz, channel, 0);
|
||||||
remqueue.push(lightentry {x, y, z, light});
|
|
||||||
}
|
|
||||||
else if (light >= entry.light){
|
|
||||||
addqueue.push(lightentry {x, y, z, light});
|
|
||||||
}
|
}
|
||||||
|
else lightmap.set(lx, y, lz, channel, 0);
|
||||||
|
remqueue.push(lightentry {x, y, z, light});
|
||||||
|
}
|
||||||
|
else if (light >= entry.light) {
|
||||||
|
addqueue.push(lightentry {x, y, z, light});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!addqueue.empty()){
|
while (!addqueue.empty()){
|
||||||
const lightentry entry = addqueue.front();
|
lightentry entry = std::move(addqueue.front());
|
||||||
addqueue.pop();
|
addqueue.pop();
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
|
|
@ -115,10 +121,16 @@ void LightSolver::solve(){
|
||||||
int y = entry.y+coords[imul3+1];
|
int y = entry.y+coords[imul3+1];
|
||||||
int z = entry.z+coords[imul3+2];
|
int z = entry.z+coords[imul3+2];
|
||||||
|
|
||||||
Chunk* chunk = chunks.getChunkByVoxel(x,y,z);
|
Chunk* chunk = prevailingChunk;
|
||||||
if (chunk == nullptr) {
|
if (chunk == nullptr || !chunk->isBlockInside(x, z)) {
|
||||||
|
chunk = chunks.getChunkByVoxel(x,y,z);
|
||||||
|
if (chunk == nullptr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if (y < 0 || y >= CHUNK_H) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(chunk->lightmap != nullptr);
|
assert(chunk->lightmap != nullptr);
|
||||||
auto& lightmap = *chunk->lightmap;
|
auto& lightmap = *chunk->lightmap;
|
||||||
int lx = x - chunk->x * CHUNK_W;
|
int lx = x - chunk->x * CHUNK_W;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <queue>
|
#include "util/array_queue.hpp"
|
||||||
|
|
||||||
|
class Chunk;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
class ContentIndices;
|
class ContentIndices;
|
||||||
class Block;
|
class Block;
|
||||||
|
|
@ -14,8 +15,8 @@ struct lightentry {
|
||||||
};
|
};
|
||||||
|
|
||||||
class LightSolver {
|
class LightSolver {
|
||||||
std::queue<lightentry> addqueue;
|
util::array_queue<lightentry> addqueue;
|
||||||
std::queue<lightentry> remqueue;
|
util::array_queue<lightentry> remqueue;
|
||||||
const Block* const* blockDefs;
|
const Block* const* blockDefs;
|
||||||
Chunks& chunks;
|
Chunks& chunks;
|
||||||
int channel;
|
int channel;
|
||||||
|
|
@ -25,5 +26,5 @@ public:
|
||||||
void add(int x, int y, int z);
|
void add(int x, int y, int z);
|
||||||
void add(int x, int y, int z, int emission);
|
void add(int x, int y, int z, int emission);
|
||||||
void remove(int x, int y, int z);
|
void remove(int x, int y, int z);
|
||||||
void solve();
|
void solve(Chunk* prevailingChunk = nullptr);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -167,10 +167,10 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
solverR.solve();
|
solverR.solve(chunk);
|
||||||
solverG.solve();
|
solverG.solve(chunk);
|
||||||
solverB.solve();
|
solverB.solve(chunk);
|
||||||
solverS.solve();
|
solverS.solve(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
||||||
|
|
@ -180,9 +180,10 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
||||||
solverB->remove(x,y,z);
|
solverB->remove(x,y,z);
|
||||||
|
|
||||||
if (id == 0){
|
if (id == 0){
|
||||||
solverR->solve();
|
auto chunk = chunks.getChunkByVoxel(glm::ivec3{x, y, z});
|
||||||
solverG->solve();
|
solverR->solve(chunk);
|
||||||
solverB->solve();
|
solverG->solve(chunk);
|
||||||
|
solverB->solve(chunk);
|
||||||
if (chunks.getLight(x,y+1,z, 3) == 0xF){
|
if (chunks.getLight(x,y+1,z, 3) == 0xF){
|
||||||
for (int i = y; i >= 0; i--){
|
for (int i = y; i >= 0; i--){
|
||||||
voxel* vox = chunks.get(x,i,z);
|
voxel* vox = chunks.get(x,i,z);
|
||||||
|
|
@ -191,17 +192,30 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
||||||
solverS->add(x,i,z, 0xF);
|
solverS->add(x,i,z, 0xF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
solverR->add(x,y+1,z); solverG->add(x,y+1,z); solverB->add(x,y+1,z); solverS->add(x,y+1,z);
|
const int coords[] = {
|
||||||
solverR->add(x,y-1,z); solverG->add(x,y-1,z); solverB->add(x,y-1,z); solverS->add(x,y-1,z);
|
0, 0, 1,
|
||||||
solverR->add(x+1,y,z); solverG->add(x+1,y,z); solverB->add(x+1,y,z); solverS->add(x+1,y,z);
|
0, 0,-1,
|
||||||
solverR->add(x-1,y,z); solverG->add(x-1,y,z); solverB->add(x-1,y,z); solverS->add(x-1,y,z);
|
0, 1, 0,
|
||||||
solverR->add(x,y,z+1); solverG->add(x,y,z+1); solverB->add(x,y,z+1); solverS->add(x,y,z+1);
|
0,-1, 0,
|
||||||
solverR->add(x,y,z-1); solverG->add(x,y,z-1); solverB->add(x,y,z-1); solverS->add(x,y,z-1);
|
1, 0, 0,
|
||||||
solverR->solve();
|
-1, 0, 0
|
||||||
solverG->solve();
|
};
|
||||||
solverB->solve();
|
for (int i = 0; i < 6; i++) {
|
||||||
solverS->solve();
|
int lx = x + coords[i * 3];
|
||||||
|
int ly = y + coords[i * 3 + 1];
|
||||||
|
int lz = z + coords[i * 3 + 2];
|
||||||
|
|
||||||
|
solverR->add(lx, ly, lz);
|
||||||
|
solverG->add(lx, ly, lz);
|
||||||
|
solverB->add(lx, ly, lz);
|
||||||
|
solverS->add(lx, ly, lz);
|
||||||
|
solverR->solve(chunk);
|
||||||
|
solverG->solve(chunk);
|
||||||
|
solverB->solve(chunk);
|
||||||
|
solverS->solve(chunk);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
auto chunk = chunks.getChunkByVoxel(glm::ivec3{x, y, z});
|
||||||
if (!block.skyLightPassing){
|
if (!block.skyLightPassing){
|
||||||
solverS->remove(x,y,z);
|
solverS->remove(x,y,z);
|
||||||
for (int i = y-1; i >= 0; i--){
|
for (int i = y-1; i >= 0; i--){
|
||||||
|
|
@ -210,19 +224,19 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
solverS->solve();
|
solverS->solve(chunk);
|
||||||
}
|
}
|
||||||
solverR->solve();
|
solverR->solve(chunk);
|
||||||
solverG->solve();
|
solverG->solve(chunk);
|
||||||
solverB->solve();
|
solverB->solve(chunk);
|
||||||
|
|
||||||
if (block.emission[0] || block.emission[1] || block.emission[2]){
|
if (block.emission[0] || block.emission[1] || block.emission[2]){
|
||||||
solverR->add(x,y,z,block.emission[0]);
|
solverR->add(x,y,z,block.emission[0]);
|
||||||
solverG->add(x,y,z,block.emission[1]);
|
solverG->add(x,y,z,block.emission[1]);
|
||||||
solverB->add(x,y,z,block.emission[2]);
|
solverB->add(x,y,z,block.emission[2]);
|
||||||
solverR->solve();
|
solverR->solve(chunk);
|
||||||
solverG->solve();
|
solverG->solve(chunk);
|
||||||
solverB->solve();
|
solverB->solve(chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
#include "BlocksController.hpp"
|
#include "BlocksController.hpp"
|
||||||
|
|
||||||
#include <set>
|
|
||||||
|
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
#include "items/Inventories.hpp"
|
#include "items/Inventories.hpp"
|
||||||
#include "items/Inventory.hpp"
|
#include "items/Inventory.hpp"
|
||||||
|
|
@ -175,12 +173,8 @@ void BlocksController::randomTick(
|
||||||
void BlocksController::randomTick(int tickid, int parts, uint padding) {
|
void BlocksController::randomTick(int tickid, int parts, uint padding) {
|
||||||
auto indices = level.content.getIndices();
|
auto indices = level.content.getIndices();
|
||||||
|
|
||||||
std::set<uint64_t> chunksIterated;
|
|
||||||
|
|
||||||
for (const auto& [pid, player] : *level.players) {
|
for (const auto& [pid, player] : *level.players) {
|
||||||
const auto& chunks = *player->chunks;
|
const auto& chunks = *player->chunks;
|
||||||
int offsetX = chunks.getOffsetX();
|
|
||||||
int offsetY = chunks.getOffsetY();
|
|
||||||
int width = chunks.getWidth();
|
int width = chunks.getWidth();
|
||||||
int height = chunks.getHeight();
|
int height = chunks.getHeight();
|
||||||
int segments = 4;
|
int segments = 4;
|
||||||
|
|
@ -195,20 +189,15 @@ void BlocksController::randomTick(int tickid, int parts, uint padding) {
|
||||||
if (chunk == nullptr || !chunk->flags.lighted) {
|
if (chunk == nullptr || !chunk->flags.lighted) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
union {
|
if (chunk->lastRandomTickId == randomTickId) {
|
||||||
int32_t pos[2];
|
|
||||||
uint64_t key;
|
|
||||||
} posU;
|
|
||||||
posU.pos[0] = x + offsetX;
|
|
||||||
posU.pos[1] = z + offsetY;
|
|
||||||
if (chunksIterated.find(posU.key) != chunksIterated.end()) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
chunksIterated.insert(posU.key);
|
chunk->lastRandomTickId = randomTickId;
|
||||||
randomTick(*chunk, segments, indices);
|
randomTick(*chunk, segments, indices);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
randomTickId++;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t BlocksController::createBlockInventory(int x, int y, int z) {
|
int64_t BlocksController::createBlockInventory(int x, int y, int z) {
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ class BlocksController {
|
||||||
util::Clock worldTickClock;
|
util::Clock worldTickClock;
|
||||||
FastRandom random {};
|
FastRandom random {};
|
||||||
std::vector<on_block_interaction> blockInteractionCallbacks;
|
std::vector<on_block_interaction> blockInteractionCallbacks;
|
||||||
|
uint64_t randomTickId = 0;
|
||||||
public:
|
public:
|
||||||
BlocksController(const Level& level, Lighting* lighting);
|
BlocksController(const Level& level, Lighting* lighting);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
#include "scripting.hpp"
|
#include "scripting.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#include "scripting_commons.hpp"
|
#include "scripting_commons.hpp"
|
||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
#include "content/ContentPack.hpp"
|
#include "content/ContentPack.hpp"
|
||||||
|
|
@ -28,6 +25,9 @@
|
||||||
#include "world/World.hpp"
|
#include "world/World.hpp"
|
||||||
#include "interfaces/Process.hpp"
|
#include "interfaces/Process.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
static debug::Logger logger("scripting");
|
static debug::Logger logger("scripting");
|
||||||
|
|
@ -391,15 +391,15 @@ void scripting::on_blocks_tick(const Block& block, int tps) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::update_block(const Block& block, const glm::ivec3& pos) {
|
void scripting::update_block(const Block& block, const glm::ivec3& pos) {
|
||||||
std::string name = block.name + ".update";
|
lua::emit_event(lua::get_main_state(), block.rt.eventNames.update,
|
||||||
lua::emit_event(lua::get_main_state(), name, [pos](auto L) {
|
[pos](auto L) {
|
||||||
return lua::pushivec_stack(L, pos);
|
return lua::pushivec_stack(L, pos);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::random_update_block(const Block& block, const glm::ivec3& pos) {
|
void scripting::random_update_block(const Block& block, const glm::ivec3& pos) {
|
||||||
std::string name = block.name + ".randupdate";
|
lua::emit_event(lua::get_main_state(), block.rt.eventNames.randomUpdate,
|
||||||
lua::emit_event(lua::get_main_state(), name, [pos](auto L) {
|
[pos](auto L) {
|
||||||
return lua::pushivec_stack(L, pos);
|
return lua::pushivec_stack(L, pos);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -660,7 +660,8 @@ void scripting::load_content_script(
|
||||||
const std::string& prefix,
|
const std::string& prefix,
|
||||||
const io::path& file,
|
const io::path& file,
|
||||||
const std::string& fileName,
|
const std::string& fileName,
|
||||||
BlockFuncsSet& funcsset
|
BlockFuncsSet& funcsset,
|
||||||
|
BlockFuncNamesCache& namesCache
|
||||||
) {
|
) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
lua::pop(lua::get_main_state(), load_script(env, "block", file, fileName));
|
lua::pop(lua::get_main_state(), load_script(env, "block", file, fileName));
|
||||||
|
|
@ -686,6 +687,9 @@ void scripting::load_content_script(
|
||||||
register_event(env, "on_block_present", prefix + ".blockpresent");
|
register_event(env, "on_block_present", prefix + ".blockpresent");
|
||||||
funcsset.onblockremoved =
|
funcsset.onblockremoved =
|
||||||
register_event(env, "on_block_removed", prefix + ".blockremoved");
|
register_event(env, "on_block_removed", prefix + ".blockremoved");
|
||||||
|
|
||||||
|
namesCache.randomUpdate = prefix + ".update";
|
||||||
|
namesCache.randomUpdate = prefix + ".randupdate";
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::load_content_script(
|
void scripting::load_content_script(
|
||||||
|
|
@ -693,7 +697,8 @@ void scripting::load_content_script(
|
||||||
const std::string& prefix,
|
const std::string& prefix,
|
||||||
const io::path& file,
|
const io::path& file,
|
||||||
const std::string& fileName,
|
const std::string& fileName,
|
||||||
ItemFuncsSet& funcsset
|
ItemFuncsSet& funcsset,
|
||||||
|
ItemFuncNamesCache& namesCache
|
||||||
) {
|
) {
|
||||||
int env = *senv;
|
int env = *senv;
|
||||||
lua::pop(lua::get_main_state(), load_script(env, "item", file, fileName));
|
lua::pop(lua::get_main_state(), load_script(env, "item", file, fileName));
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,9 @@ struct ItemDef;
|
||||||
class Inventory;
|
class Inventory;
|
||||||
class UiDocument;
|
class UiDocument;
|
||||||
struct BlockFuncsSet;
|
struct BlockFuncsSet;
|
||||||
|
struct BlockFuncNamesCache;
|
||||||
struct ItemFuncsSet;
|
struct ItemFuncsSet;
|
||||||
|
struct ItemFuncNamesCache;
|
||||||
struct WorldFuncsSet;
|
struct WorldFuncsSet;
|
||||||
struct UserComponent;
|
struct UserComponent;
|
||||||
struct uidocscript;
|
struct uidocscript;
|
||||||
|
|
@ -168,7 +170,8 @@ namespace scripting {
|
||||||
const std::string& prefix,
|
const std::string& prefix,
|
||||||
const io::path& file,
|
const io::path& file,
|
||||||
const std::string& fileName,
|
const std::string& fileName,
|
||||||
BlockFuncsSet& funcsset
|
BlockFuncsSet& funcsset,
|
||||||
|
BlockFuncNamesCache& namesCache
|
||||||
);
|
);
|
||||||
|
|
||||||
/// @brief Load script associated with an Item
|
/// @brief Load script associated with an Item
|
||||||
|
|
@ -182,7 +185,8 @@ namespace scripting {
|
||||||
const std::string& prefix,
|
const std::string& prefix,
|
||||||
const io::path& file,
|
const io::path& file,
|
||||||
const std::string& fileName,
|
const std::string& fileName,
|
||||||
ItemFuncsSet& funcsset
|
ItemFuncsSet& funcsset,
|
||||||
|
ItemFuncNamesCache& namesCache
|
||||||
);
|
);
|
||||||
|
|
||||||
/// @brief Load component script
|
/// @brief Load component script
|
||||||
|
|
|
||||||
76
src/util/array_queue.hpp
Normal file
76
src/util/array_queue.hpp
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <cstring>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
template <typename T>
|
||||||
|
class array_queue {
|
||||||
|
public:
|
||||||
|
array_queue(size_t initCapacity = 8)
|
||||||
|
: _capacity(initCapacity),
|
||||||
|
_size(0),
|
||||||
|
_front(0),
|
||||||
|
_back(0),
|
||||||
|
_data(std::make_unique<T[]>(_capacity)) {
|
||||||
|
if (initCapacity == 0 || (initCapacity & (initCapacity - 1)) != 0) {
|
||||||
|
throw std::invalid_argument("initCapacity must be positive power of 2");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~array_queue() {
|
||||||
|
while (!empty()) {
|
||||||
|
pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void push(T&& value) {
|
||||||
|
if (_back == _front && _size) {
|
||||||
|
grow();
|
||||||
|
}
|
||||||
|
_data[_back] = std::forward<T>(value);
|
||||||
|
_back = (_back + 1) & (_capacity - 1);
|
||||||
|
++_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
T& front() {
|
||||||
|
return _data[_front];
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop() {
|
||||||
|
_data[_front].~T();
|
||||||
|
_front = (_front + 1) & (_capacity - 1);
|
||||||
|
--_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const {
|
||||||
|
return _size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size() const {
|
||||||
|
return _size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t _capacity;
|
||||||
|
size_t _size;
|
||||||
|
size_t _front;
|
||||||
|
size_t _back;
|
||||||
|
std::unique_ptr<T[]> _data;
|
||||||
|
|
||||||
|
void grow() {
|
||||||
|
size_t newCapacity = _capacity * 2;
|
||||||
|
auto newData = std::make_unique<T[]>(newCapacity);
|
||||||
|
|
||||||
|
std::move(_data.get() + _front, _data.get() + _capacity, newData.get());
|
||||||
|
std::move(_data.get(), _data.get() + _front, newData.get() + (_capacity - _front));
|
||||||
|
|
||||||
|
_back = _size;
|
||||||
|
_front = 0;
|
||||||
|
|
||||||
|
_data.reset(newData.release());
|
||||||
|
_capacity = newCapacity;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -54,6 +54,11 @@ struct BlockFuncsSet {
|
||||||
bool onblockremoved : 1;
|
bool onblockremoved : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct BlockFuncNamesCache {
|
||||||
|
std::string update;
|
||||||
|
std::string randomUpdate;
|
||||||
|
};
|
||||||
|
|
||||||
struct CoordSystem {
|
struct CoordSystem {
|
||||||
std::array<glm::ivec3, 3> axes;
|
std::array<glm::ivec3, 3> axes;
|
||||||
/// @brief Grid 3d position fix offset (for negative vectors)
|
/// @brief Grid 3d position fix offset (for negative vectors)
|
||||||
|
|
@ -296,6 +301,8 @@ public:
|
||||||
blockid_t surfaceReplacement = 0;
|
blockid_t surfaceReplacement = 0;
|
||||||
|
|
||||||
std::set<int> tags;
|
std::set<int> tags;
|
||||||
|
|
||||||
|
BlockFuncNamesCache eventNames;
|
||||||
} rt {};
|
} rt {};
|
||||||
|
|
||||||
Block(const std::string& name);
|
Block(const std::string& name);
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ public:
|
||||||
bool dirtyHeights : 1;
|
bool dirtyHeights : 1;
|
||||||
} flags {};
|
} flags {};
|
||||||
|
|
||||||
|
uint64_t lastRandomTickId = -1;
|
||||||
|
|
||||||
/// @brief Block inventories map where key is index of block in voxels array
|
/// @brief Block inventories map where key is index of block in voxels array
|
||||||
ChunkInventoriesMap inventories;
|
ChunkInventoriesMap inventories;
|
||||||
/// @brief Blocks metadata heap
|
/// @brief Blocks metadata heap
|
||||||
|
|
@ -81,4 +83,10 @@ public:
|
||||||
glm::vec3((x + 1) * CHUNK_W, INFINITY, (z + 1) * CHUNK_D)
|
glm::vec3((x + 1) * CHUNK_W, INFINITY, (z + 1) * CHUNK_D)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isBlockInside(int x, int z) const {
|
||||||
|
x -= this->x * CHUNK_W;
|
||||||
|
z -= this->z * CHUNK_D;
|
||||||
|
return x >= 0 && z >= 0 && x < CHUNK_W && z < CHUNK_D;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue