refactor: PVS-Studio warnings fixes
This commit is contained in:
@@ -96,8 +96,9 @@ void ALStream::bindSpeaker(speakerid_t speakerid) {
|
|||||||
this->speaker = speakerid;
|
this->speaker = speakerid;
|
||||||
sp = audio::get_speaker(speakerid);
|
sp = audio::get_speaker(speakerid);
|
||||||
if (sp) {
|
if (sp) {
|
||||||
auto alspeaker = dynamic_cast<ALSpeaker*>(sp); //FIXME: Potentional null pointer
|
auto alspeaker = dynamic_cast<ALSpeaker*>(sp);
|
||||||
alspeaker->stream = this; //-V522
|
assert(alspeaker != nullptr); // backends must not be mixed
|
||||||
|
alspeaker->stream = this;
|
||||||
alspeaker->duration = source->getTotalDuration();
|
alspeaker->duration = source->getTotalDuration();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,12 +146,13 @@ void ALStream::update(double delta) {
|
|||||||
}
|
}
|
||||||
auto p_speaker = audio::get_speaker(this->speaker);
|
auto p_speaker = audio::get_speaker(this->speaker);
|
||||||
if (p_speaker == nullptr) {
|
if (p_speaker == nullptr) {
|
||||||
p_speaker = 0; //FIXME: Not used
|
this->speaker = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(p_speaker); //FIXME: Potentional null pointer
|
ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(p_speaker);
|
||||||
|
assert(alspeaker != nullptr);
|
||||||
if (alspeaker->stopped) { //-V522
|
if (alspeaker->stopped) { //-V522
|
||||||
p_speaker = 0; //FIXME: Not used
|
this->speaker = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-1
@@ -59,13 +59,25 @@ public:
|
|||||||
ContentUnitIndices(std::vector<T*> defs) : defs(std::move(defs)) {
|
ContentUnitIndices(std::vector<T*> defs) : defs(std::move(defs)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T* get(blockid_t id) const {
|
inline const T* get(blockid_t id) const {
|
||||||
if (id >= defs.size()) {
|
if (id >= defs.size()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return defs[id];
|
return defs[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[deprecated]]
|
||||||
|
inline T* getWriteable(blockid_t id) const { // TODO: remove
|
||||||
|
if (id >= defs.size()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return defs[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const T& require(blockid_t id) const {
|
||||||
|
return *defs.at(id);
|
||||||
|
}
|
||||||
|
|
||||||
inline size_t count() const {
|
inline size_t count() const {
|
||||||
return defs.size();
|
return defs.size();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,11 +132,7 @@ fs::path ContentPack::findPack(
|
|||||||
if (fs::is_directory(folder)) {
|
if (fs::is_directory(folder)) {
|
||||||
return folder;
|
return folder;
|
||||||
}
|
}
|
||||||
folder = paths->getResources() / fs::path("content") / fs::path(name);
|
return paths->getResources() / fs::path("content") / fs::path(name);
|
||||||
if (fs::is_directory(folder)) {
|
|
||||||
return folder; //-V523
|
|
||||||
}
|
|
||||||
return folder; // FIXME: V523 The 'then' statement is equivalent to the subsequent code fragment //-V523
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ContentPackRuntime::ContentPackRuntime(ContentPack info, scriptenv env)
|
ContentPackRuntime::ContentPackRuntime(ContentPack info, scriptenv env)
|
||||||
|
|||||||
+2
-2
@@ -268,8 +268,8 @@ void Engine::loadAssets() {
|
|||||||
|
|
||||||
// no need
|
// no need
|
||||||
// correct log messages order is more useful
|
// correct log messages order is more useful
|
||||||
bool threading = false;
|
bool threading = false; // look at two upper lines
|
||||||
if (threading) { // TODO: Why is always false?
|
if (threading) {
|
||||||
auto task = loader.startTask([=](){});
|
auto task = loader.startTask([=](){});
|
||||||
task->waitForEnd();
|
task->waitForEnd();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : conte
|
|||||||
auto atlas = assets->get<Atlas>("blocks");
|
auto atlas = assets->get<Atlas>("blocks");
|
||||||
|
|
||||||
for (uint i = 0; i < indices->blocks.count(); i++) {
|
for (uint i = 0; i < indices->blocks.count(); i++) {
|
||||||
Block* def = indices->blocks.get(i); //FIXME: Potential null pointer
|
auto def = indices->blocks.getWriteable(i); //FIXME: Potential null pointer
|
||||||
for (uint side = 0; side < 6; side++) {
|
for (uint side = 0; side < 6; side++) {
|
||||||
const std::string& tex = def->textureFaces[side]; //-V522
|
const std::string& tex = def->textureFaces[side]; //-V522
|
||||||
if (atlas->has(tex)) {
|
if (atlas->has(tex)) {
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ LevelFrontend::LevelFrontend(
|
|||||||
"block-previews"
|
"block-previews"
|
||||||
);
|
);
|
||||||
controller->getBlocksController()->listenBlockInteraction(
|
controller->getBlocksController()->listenBlockInteraction(
|
||||||
[=](Player* player, glm::ivec3 pos, const Block* def, BlockInteraction type) {
|
[=](Player* player, glm::ivec3 pos, const Block& def, BlockInteraction type) {
|
||||||
auto material = level->content->findBlockMaterial(def->material);
|
auto material = level->content->findBlockMaterial(def.material);
|
||||||
if (material == nullptr) {
|
if (material == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
#include "WorldRenderer.hpp"
|
#include "WorldRenderer.hpp"
|
||||||
|
|
||||||
#include "ChunksRenderer.hpp"
|
#include <GL/glew.h>
|
||||||
#include "ModelBatch.hpp"
|
#include <assert.h>
|
||||||
#include "Skybox.hpp"
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <glm/ext.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "../../assets/Assets.hpp"
|
#include "../../assets/Assets.hpp"
|
||||||
#include "../../content/Content.hpp"
|
#include "../../content/Content.hpp"
|
||||||
@@ -15,8 +19,8 @@
|
|||||||
#include "../../logic/scripting/scripting_hud.hpp"
|
#include "../../logic/scripting/scripting_hud.hpp"
|
||||||
#include "../../maths/FrustumCulling.hpp"
|
#include "../../maths/FrustumCulling.hpp"
|
||||||
#include "../../maths/voxmaths.hpp"
|
#include "../../maths/voxmaths.hpp"
|
||||||
#include "../../objects/Player.hpp"
|
|
||||||
#include "../../objects/Entities.hpp"
|
#include "../../objects/Entities.hpp"
|
||||||
|
#include "../../objects/Player.hpp"
|
||||||
#include "../../settings.hpp"
|
#include "../../settings.hpp"
|
||||||
#include "../../voxels/Block.hpp"
|
#include "../../voxels/Block.hpp"
|
||||||
#include "../../voxels/Chunk.hpp"
|
#include "../../voxels/Chunk.hpp"
|
||||||
@@ -31,42 +35,37 @@
|
|||||||
#include "../core/DrawContext.hpp"
|
#include "../core/DrawContext.hpp"
|
||||||
#include "../core/LineBatch.hpp"
|
#include "../core/LineBatch.hpp"
|
||||||
#include "../core/Mesh.hpp"
|
#include "../core/Mesh.hpp"
|
||||||
|
#include "../core/Model.hpp"
|
||||||
#include "../core/PostProcessing.hpp"
|
#include "../core/PostProcessing.hpp"
|
||||||
#include "../core/Shader.hpp"
|
#include "../core/Shader.hpp"
|
||||||
#include "../core/Texture.hpp"
|
#include "../core/Texture.hpp"
|
||||||
#include "../core/Model.hpp"
|
#include "ChunksRenderer.hpp"
|
||||||
|
#include "ModelBatch.hpp"
|
||||||
#include <assert.h>
|
#include "Skybox.hpp"
|
||||||
#include <GL/glew.h>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include <glm/ext.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
|
|
||||||
bool WorldRenderer::showChunkBorders = false;
|
bool WorldRenderer::showChunkBorders = false;
|
||||||
bool WorldRenderer::showEntitiesDebug = false;
|
bool WorldRenderer::showEntitiesDebug = false;
|
||||||
|
|
||||||
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player)
|
WorldRenderer::WorldRenderer(
|
||||||
: engine(engine),
|
Engine* engine, LevelFrontend* frontend, Player* player
|
||||||
level(frontend->getLevel()),
|
)
|
||||||
player(player),
|
: engine(engine),
|
||||||
frustumCulling(std::make_unique<Frustum>()),
|
level(frontend->getLevel()),
|
||||||
lineBatch(std::make_unique<LineBatch>()),
|
player(player),
|
||||||
modelBatch(std::make_unique<ModelBatch>(20'000, engine->getAssets(), level->chunks.get()))
|
frustumCulling(std::make_unique<Frustum>()),
|
||||||
{
|
lineBatch(std::make_unique<LineBatch>()),
|
||||||
|
modelBatch(std::make_unique<ModelBatch>(
|
||||||
|
20'000, engine->getAssets(), level->chunks.get()
|
||||||
|
)) {
|
||||||
renderer = std::make_unique<ChunksRenderer>(
|
renderer = std::make_unique<ChunksRenderer>(
|
||||||
level,
|
level, frontend->getContentGfxCache(), &engine->getSettings()
|
||||||
frontend->getContentGfxCache(),
|
|
||||||
&engine->getSettings()
|
|
||||||
);
|
);
|
||||||
batch3d = std::make_unique<Batch3D>(4096);
|
batch3d = std::make_unique<Batch3D>(4096);
|
||||||
|
|
||||||
auto& settings = engine->getSettings();
|
auto& settings = engine->getSettings();
|
||||||
level->events->listen(EVT_CHUNK_HIDDEN,
|
level->events->listen(
|
||||||
[this](lvl_event_type, Chunk* chunk) {
|
EVT_CHUNK_HIDDEN,
|
||||||
renderer->unload(chunk);
|
[this](lvl_event_type, Chunk* chunk) { renderer->unload(chunk); }
|
||||||
}
|
|
||||||
);
|
);
|
||||||
auto assets = engine->getAssets();
|
auto assets = engine->getAssets();
|
||||||
skybox = std::make_unique<Skybox>(
|
skybox = std::make_unique<Skybox>(
|
||||||
@@ -78,41 +77,35 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl
|
|||||||
WorldRenderer::~WorldRenderer() = default;
|
WorldRenderer::~WorldRenderer() = default;
|
||||||
|
|
||||||
bool WorldRenderer::drawChunk(
|
bool WorldRenderer::drawChunk(
|
||||||
size_t index,
|
size_t index, Camera* camera, Shader* shader, bool culling
|
||||||
Camera* camera,
|
) {
|
||||||
Shader* shader,
|
|
||||||
bool culling
|
|
||||||
){
|
|
||||||
auto chunk = level->chunks->chunks[index];
|
auto chunk = level->chunks->chunks[index];
|
||||||
if (!chunk->flags.lighted) {
|
if (!chunk->flags.lighted) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
float distance = glm::distance(
|
float distance = glm::distance(
|
||||||
camera->position,
|
camera->position,
|
||||||
glm::vec3((chunk->x + 0.5f) * CHUNK_W,
|
glm::vec3(
|
||||||
camera->position.y,
|
(chunk->x + 0.5f) * CHUNK_W,
|
||||||
(chunk->z + 0.5f) * CHUNK_D)
|
camera->position.y,
|
||||||
|
(chunk->z + 0.5f) * CHUNK_D
|
||||||
|
)
|
||||||
);
|
);
|
||||||
auto mesh = renderer->getOrRender(chunk, distance < CHUNK_W*1.5f);
|
auto mesh = renderer->getOrRender(chunk, distance < CHUNK_W * 1.5f);
|
||||||
if (mesh == nullptr) {
|
if (mesh == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (culling){
|
if (culling) {
|
||||||
glm::vec3 min(
|
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D);
|
||||||
chunk->x * CHUNK_W,
|
|
||||||
chunk->bottom,
|
|
||||||
chunk->z * CHUNK_D
|
|
||||||
);
|
|
||||||
glm::vec3 max(
|
glm::vec3 max(
|
||||||
chunk->x * CHUNK_W + CHUNK_W,
|
chunk->x * CHUNK_W + CHUNK_W,
|
||||||
chunk->top,
|
chunk->top,
|
||||||
chunk->z * CHUNK_D + CHUNK_D
|
chunk->z * CHUNK_D + CHUNK_D
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!frustumCulling->isBoxVisible(min, max))
|
if (!frustumCulling->isBoxVisible(min, max)) return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
glm::vec3 coord(chunk->x*CHUNK_W+0.5f, 0.5f, chunk->z*CHUNK_D+0.5f);
|
glm::vec3 coord(chunk->x * CHUNK_W + 0.5f, 0.5f, chunk->z * CHUNK_D + 0.5f);
|
||||||
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
|
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
|
||||||
shader->uniformMatrix("u_model", model);
|
shader->uniformMatrix("u_model", model);
|
||||||
mesh->draw();
|
mesh->draw();
|
||||||
@@ -125,9 +118,8 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
|
|||||||
// [warning] this whole method is not thread-safe for chunks
|
// [warning] this whole method is not thread-safe for chunks
|
||||||
|
|
||||||
std::vector<size_t> indices;
|
std::vector<size_t> indices;
|
||||||
for (size_t i = 0; i < chunks->volume; i++){
|
for (size_t i = 0; i < chunks->volume; i++) {
|
||||||
if (chunks->chunks[i] == nullptr)
|
if (chunks->chunks[i] == nullptr) continue;
|
||||||
continue;
|
|
||||||
indices.emplace_back(i);
|
indices.emplace_back(i);
|
||||||
}
|
}
|
||||||
float px = camera->position.x / (float)CHUNK_W - 0.5f;
|
float px = camera->position.x / (float)CHUNK_W - 0.5f;
|
||||||
@@ -139,20 +131,22 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
|
|||||||
auto adz = (a->z - pz);
|
auto adz = (a->z - pz);
|
||||||
auto bdx = (b->x - px);
|
auto bdx = (b->x - px);
|
||||||
auto bdz = (b->z - pz);
|
auto bdz = (b->z - pz);
|
||||||
return (adx*adx + adz*adz > bdx*bdx + bdz*bdz);
|
return (adx * adx + adz * adz > bdx * bdx + bdz * bdz);
|
||||||
});
|
});
|
||||||
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
||||||
if (culling) {
|
if (culling) {
|
||||||
frustumCulling->update(camera->getProjView());
|
frustumCulling->update(camera->getProjView());
|
||||||
}
|
}
|
||||||
chunks->visible = 0;
|
chunks->visible = 0;
|
||||||
for (size_t i = 0; i < indices.size(); i++){
|
for (size_t i = 0; i < indices.size(); i++) {
|
||||||
chunks->visible += drawChunk(indices[i], camera, shader, culling);
|
chunks->visible += drawChunk(indices[i], camera, shader, culling);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldRenderer::setupWorldShader(
|
void WorldRenderer::setupWorldShader(
|
||||||
Shader* shader, Camera* camera, const EngineSettings& settings,
|
Shader* shader,
|
||||||
|
Camera* camera,
|
||||||
|
const EngineSettings& settings,
|
||||||
float fogFactor
|
float fogFactor
|
||||||
) {
|
) {
|
||||||
shader->use();
|
shader->use();
|
||||||
@@ -172,12 +166,13 @@ void WorldRenderer::setupWorldShader(
|
|||||||
{
|
{
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
||||||
auto item = indices->items.get(stack.getItemId()); //FIXME: Potentional null pointer
|
auto& item = indices->items.require(stack.getItemId());
|
||||||
float multiplier = 0.5f;
|
float multiplier = 0.5f;
|
||||||
shader->uniform3f("u_torchlightColor",
|
shader->uniform3f(
|
||||||
item->emission[0] / 15.0f * multiplier, //-V522
|
"u_torchlightColor",
|
||||||
item->emission[1] / 15.0f * multiplier,
|
item.emission[0] / 15.0f * multiplier,
|
||||||
item->emission[2] / 15.0f * multiplier
|
item.emission[1] / 15.0f * multiplier,
|
||||||
|
item.emission[2] / 15.0f * multiplier
|
||||||
);
|
);
|
||||||
shader->uniform1f("u_torchlightDistance", 6.0f);
|
shader->uniform1f("u_torchlightDistance", 6.0f);
|
||||||
}
|
}
|
||||||
@@ -194,7 +189,7 @@ void WorldRenderer::renderLevel(
|
|||||||
auto atlas = assets->get<Atlas>("blocks");
|
auto atlas = assets->get<Atlas>("blocks");
|
||||||
|
|
||||||
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
||||||
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance.get()-2);
|
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance.get() - 2);
|
||||||
|
|
||||||
auto shader = assets->get<Shader>("main");
|
auto shader = assets->get<Shader>("main");
|
||||||
setupWorldShader(shader, camera, settings, fogFactor);
|
setupWorldShader(shader, camera, settings, fogFactor);
|
||||||
@@ -208,7 +203,12 @@ void WorldRenderer::renderLevel(
|
|||||||
setupWorldShader(entityShader, camera, settings, fogFactor);
|
setupWorldShader(entityShader, camera, settings, fogFactor);
|
||||||
|
|
||||||
level->entities->render(
|
level->entities->render(
|
||||||
assets, *modelBatch, culling ? frustumCulling.get() : nullptr, delta, pause);
|
assets,
|
||||||
|
*modelBatch,
|
||||||
|
culling ? frustumCulling.get() : nullptr,
|
||||||
|
delta,
|
||||||
|
pause
|
||||||
|
);
|
||||||
|
|
||||||
if (!pause) {
|
if (!pause) {
|
||||||
scripting::on_frontend_render();
|
scripting::on_frontend_render();
|
||||||
@@ -222,22 +222,26 @@ void WorldRenderer::renderBlockSelection() {
|
|||||||
const auto& selection = player->selection;
|
const auto& selection = player->selection;
|
||||||
auto indices = level->content->getIndices();
|
auto indices = level->content->getIndices();
|
||||||
blockid_t id = selection.vox.id;
|
blockid_t id = selection.vox.id;
|
||||||
auto block = indices->blocks.get(id); //FIXME: Potentional null pointer
|
auto& block = indices->blocks.require(id);
|
||||||
const glm::ivec3 pos = player->selection.position;
|
const glm::ivec3 pos = player->selection.position;
|
||||||
const glm::vec3 point = selection.hitPosition;
|
const glm::vec3 point = selection.hitPosition;
|
||||||
const glm::vec3 norm = selection.normal;
|
const glm::vec3 norm = selection.normal;
|
||||||
|
|
||||||
const std::vector<AABB>& hitboxes = block->rotatable //-V522
|
const std::vector<AABB>& hitboxes =
|
||||||
? block->rt.hitboxes[selection.vox.state.rotation]
|
block.rotatable ? block.rt.hitboxes[selection.vox.state.rotation]
|
||||||
: block->hitboxes;
|
: block.hitboxes;
|
||||||
|
|
||||||
lineBatch->lineWidth(2.0f);
|
lineBatch->lineWidth(2.0f);
|
||||||
for (auto& hitbox: hitboxes) {
|
for (auto& hitbox : hitboxes) {
|
||||||
const glm::vec3 center = glm::vec3(pos) + hitbox.center();
|
const glm::vec3 center = glm::vec3(pos) + hitbox.center();
|
||||||
const glm::vec3 size = hitbox.size();
|
const glm::vec3 size = hitbox.size();
|
||||||
lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f));
|
lineBatch->box(
|
||||||
|
center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f)
|
||||||
|
);
|
||||||
if (player->debug) {
|
if (player->debug) {
|
||||||
lineBatch->line(point, point+norm*0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f));
|
lineBatch->line(
|
||||||
|
point, point + norm * 0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lineBatch->flush();
|
lineBatch->flush();
|
||||||
@@ -255,14 +259,13 @@ void WorldRenderer::renderLines(
|
|||||||
auto ctx = pctx.sub(lineBatch.get());
|
auto ctx = pctx.sub(lineBatch.get());
|
||||||
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
bool culling = engine->getSettings().graphics.frustumCulling.get();
|
||||||
level->entities->renderDebug(
|
level->entities->renderDebug(
|
||||||
*lineBatch, culling ? frustumCulling.get() : nullptr, ctx);
|
*lineBatch, culling ? frustumCulling.get() : nullptr, ctx
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldRenderer::renderDebugLines(
|
void WorldRenderer::renderDebugLines(
|
||||||
const DrawContext& pctx,
|
const DrawContext& pctx, Camera* camera, Shader* linesShader
|
||||||
Camera* camera,
|
|
||||||
Shader* linesShader
|
|
||||||
) {
|
) {
|
||||||
DrawContext ctx = pctx.sub(lineBatch.get());
|
DrawContext ctx = pctx.sub(lineBatch.get());
|
||||||
const auto& viewport = ctx.getViewport();
|
const auto& viewport = ctx.getViewport();
|
||||||
@@ -273,7 +276,7 @@ void WorldRenderer::renderDebugLines(
|
|||||||
|
|
||||||
linesShader->use();
|
linesShader->use();
|
||||||
|
|
||||||
if (showChunkBorders){
|
if (showChunkBorders) {
|
||||||
linesShader->uniformMatrix("u_projview", camera->getProjView());
|
linesShader->uniformMatrix("u_projview", camera->getProjView());
|
||||||
glm::vec3 coord = player->camera->position;
|
glm::vec3 coord = player->camera->position;
|
||||||
if (coord.x < 0) coord.x--;
|
if (coord.x < 0) coord.x--;
|
||||||
@@ -282,18 +285,29 @@ void WorldRenderer::renderDebugLines(
|
|||||||
int cz = floordiv(static_cast<int>(coord.z), CHUNK_D);
|
int cz = floordiv(static_cast<int>(coord.z), CHUNK_D);
|
||||||
|
|
||||||
drawBorders(
|
drawBorders(
|
||||||
cx * CHUNK_W, 0, cz * CHUNK_D,
|
cx * CHUNK_W,
|
||||||
(cx + 1) * CHUNK_W, CHUNK_H, (cz + 1) * CHUNK_D
|
0,
|
||||||
|
cz * CHUNK_D,
|
||||||
|
(cx + 1) * CHUNK_W,
|
||||||
|
CHUNK_H,
|
||||||
|
(cz + 1) * CHUNK_D
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
float length = 40.f;
|
float length = 40.f;
|
||||||
glm::vec3 tsl(displayWidth/2, displayHeight/2, 0.f);
|
glm::vec3 tsl(displayWidth / 2, displayHeight / 2, 0.f);
|
||||||
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
|
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
|
||||||
linesShader->uniformMatrix("u_projview", glm::ortho(
|
linesShader->uniformMatrix(
|
||||||
0.f, static_cast<float>(displayWidth),
|
"u_projview",
|
||||||
0.f, static_cast<float>(displayHeight),
|
glm::ortho(
|
||||||
-length, length) * model * glm::inverse(camera->rotation)
|
0.f,
|
||||||
|
static_cast<float>(displayWidth),
|
||||||
|
0.f,
|
||||||
|
static_cast<float>(displayHeight),
|
||||||
|
-length,
|
||||||
|
length
|
||||||
|
) * model *
|
||||||
|
glm::inverse(camera->rotation)
|
||||||
);
|
);
|
||||||
|
|
||||||
ctx.setDepthTest(false);
|
ctx.setDepthTest(false);
|
||||||
@@ -317,14 +331,14 @@ void WorldRenderer::draw(
|
|||||||
bool pause,
|
bool pause,
|
||||||
float delta,
|
float delta,
|
||||||
PostProcessing* postProcessing
|
PostProcessing* postProcessing
|
||||||
){
|
) {
|
||||||
timer += delta * !pause;
|
timer += delta * !pause;
|
||||||
auto world = level->getWorld();
|
auto world = level->getWorld();
|
||||||
const Viewport& vp = pctx.getViewport();
|
const Viewport& vp = pctx.getViewport();
|
||||||
camera->aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
|
camera->aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
|
||||||
|
|
||||||
const EngineSettings& settings = engine->getSettings();
|
const EngineSettings& settings = engine->getSettings();
|
||||||
skybox->refresh(pctx, world->daytime, 1.0f+world->fog*2.0f, 4);
|
skybox->refresh(pctx, world->daytime, 1.0f + world->fog * 2.0f, 4);
|
||||||
|
|
||||||
auto assets = engine->getAssets();
|
auto assets = engine->getAssets();
|
||||||
auto linesShader = assets->get<Shader>("lines");
|
auto linesShader = assets->get<Shader>("lines");
|
||||||
@@ -346,7 +360,7 @@ void WorldRenderer::draw(
|
|||||||
ctx.setCullFace(true);
|
ctx.setCullFace(true);
|
||||||
renderLevel(ctx, camera, settings, delta, pause);
|
renderLevel(ctx, camera, settings, delta, pause);
|
||||||
// Debug lines
|
// Debug lines
|
||||||
if (hudVisible){
|
if (hudVisible) {
|
||||||
renderLines(camera, linesShader, ctx);
|
renderLines(camera, linesShader, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -364,40 +378,30 @@ void WorldRenderer::draw(
|
|||||||
postProcessing->render(pctx, screenShader);
|
postProcessing->render(pctx, screenShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldRenderer::drawBorders(int sx, int sy, int sz, int ex, int ey, int ez) {
|
void WorldRenderer::drawBorders(
|
||||||
int ww = ex-sx;
|
int sx, int sy, int sz, int ex, int ey, int ez
|
||||||
int dd = ez-sz;
|
) {
|
||||||
|
int ww = ex - sx;
|
||||||
|
int dd = ez - sz;
|
||||||
/*corner*/ {
|
/*corner*/ {
|
||||||
lineBatch->line(sx, sy, sz,
|
lineBatch->line(sx, sy, sz, sx, ey, sz, 0.8f, 0, 0.8f, 1);
|
||||||
sx, ey, sz, 0.8f, 0, 0.8f, 1);
|
lineBatch->line(sx, sy, ez, sx, ey, ez, 0.8f, 0, 0.8f, 1);
|
||||||
lineBatch->line(sx, sy, ez,
|
lineBatch->line(ex, sy, sz, ex, ey, sz, 0.8f, 0, 0.8f, 1);
|
||||||
sx, ey, ez, 0.8f, 0, 0.8f, 1);
|
lineBatch->line(ex, sy, ez, ex, ey, ez, 0.8f, 0, 0.8f, 1);
|
||||||
lineBatch->line(ex, sy, sz,
|
|
||||||
ex, ey, sz, 0.8f, 0, 0.8f, 1);
|
|
||||||
lineBatch->line(ex, sy, ez,
|
|
||||||
ex, ey, ez, 0.8f, 0, 0.8f, 1);
|
|
||||||
}
|
}
|
||||||
for (int i = 2; i < ww; i+=2) {
|
for (int i = 2; i < ww; i += 2) {
|
||||||
lineBatch->line(sx + i, sy, sz,
|
lineBatch->line(sx + i, sy, sz, sx + i, ey, sz, 0, 0, 0.8f, 1);
|
||||||
sx + i, ey, sz, 0, 0, 0.8f, 1);
|
lineBatch->line(sx + i, sy, ez, sx + i, ey, ez, 0, 0, 0.8f, 1);
|
||||||
lineBatch->line(sx + i, sy, ez,
|
|
||||||
sx + i, ey, ez, 0, 0, 0.8f, 1);
|
|
||||||
}
|
}
|
||||||
for (int i = 2; i < dd; i+=2) {
|
for (int i = 2; i < dd; i += 2) {
|
||||||
lineBatch->line(sx, sy, sz + i,
|
lineBatch->line(sx, sy, sz + i, sx, ey, sz + i, 0.8f, 0, 0, 1);
|
||||||
sx, ey, sz + i, 0.8f, 0, 0, 1);
|
lineBatch->line(ex, sy, sz + i, ex, ey, sz + i, 0.8f, 0, 0, 1);
|
||||||
lineBatch->line(ex, sy, sz + i,
|
|
||||||
ex, ey, sz + i, 0.8f, 0, 0, 1);
|
|
||||||
}
|
}
|
||||||
for (int i = sy; i < ey; i+=2){
|
for (int i = sy; i < ey; i += 2) {
|
||||||
lineBatch->line(sx, i, sz,
|
lineBatch->line(sx, i, sz, sx, i, ez, 0, 0.8f, 0, 1);
|
||||||
sx, i, ez, 0, 0.8f, 0, 1);
|
lineBatch->line(sx, i, ez, ex, i, ez, 0, 0.8f, 0, 1);
|
||||||
lineBatch->line(sx, i, ez,
|
lineBatch->line(ex, i, ez, ex, i, sz, 0, 0.8f, 0, 1);
|
||||||
ex, i, ez, 0, 0.8f, 0, 1);
|
lineBatch->line(ex, i, sz, sx, i, sz, 0, 0.8f, 0, 1);
|
||||||
lineBatch->line(ex, i, ez,
|
|
||||||
ex, i, sz, 0, 0.8f, 0, 1);
|
|
||||||
lineBatch->line(ex, i, sz,
|
|
||||||
sx, i, sz, 0, 0.8f, 0, 1);
|
|
||||||
}
|
}
|
||||||
lineBatch->flush();
|
lineBatch->flush();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -159,12 +159,12 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
|
|||||||
auto previews = assets->get<Atlas>("block-previews");
|
auto previews = assets->get<Atlas>("block-previews");
|
||||||
auto indices = content->getIndices();
|
auto indices = content->getIndices();
|
||||||
|
|
||||||
ItemDef* item = indices->items.get(stack.getItemId()); //FIXME: Potentional null pointer
|
auto& item = indices->items.require(stack.getItemId()); //FIXME: Potentional null pointer
|
||||||
switch (item->iconType) { //-V522
|
switch (item.iconType) {
|
||||||
case item_icon_type::none:
|
case item_icon_type::none:
|
||||||
break;
|
break;
|
||||||
case item_icon_type::block: {
|
case item_icon_type::block: {
|
||||||
const Block& cblock = content->blocks.require(item->icon);
|
const Block& cblock = content->blocks.require(item.icon);
|
||||||
batch->texture(previews->getTexture());
|
batch->texture(previews->getTexture());
|
||||||
|
|
||||||
UVRegion region = previews->get(cblock.name);
|
UVRegion region = previews->get(cblock.name);
|
||||||
@@ -174,13 +174,13 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case item_icon_type::sprite: {
|
case item_icon_type::sprite: {
|
||||||
size_t index = item->icon.find(':');
|
size_t index = item.icon.find(':');
|
||||||
std::string name = item->icon.substr(index+1);
|
std::string name = item.icon.substr(index+1);
|
||||||
UVRegion region(0.0f, 0.0, 1.0f, 1.0f);
|
UVRegion region(0.0f, 0.0, 1.0f, 1.0f);
|
||||||
if (index == std::string::npos) {
|
if (index == std::string::npos) {
|
||||||
batch->texture(assets->get<Texture>(name));
|
batch->texture(assets->get<Texture>(name));
|
||||||
} else {
|
} else {
|
||||||
std::string atlasname = item->icon.substr(0, index);
|
std::string atlasname = item.icon.substr(0, index);
|
||||||
auto atlas = assets->get<Atlas>(atlasname);
|
auto atlas = assets->get<Atlas>(atlasname);
|
||||||
if (atlas && atlas->has(name)) {
|
if (atlas && atlas->has(name)) {
|
||||||
region = atlas->get(name);
|
region = atlas->get(name);
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
||||||
Block* block = content->getIndices()->blocks.get(id); //FIXME: Potentional null pointer
|
const auto& block = content->getIndices()->blocks.require(id);
|
||||||
solverR->remove(x,y,z);
|
solverR->remove(x,y,z);
|
||||||
solverG->remove(x,y,z);
|
solverG->remove(x,y,z);
|
||||||
solverB->remove(x,y,z);
|
solverB->remove(x,y,z);
|
||||||
@@ -161,7 +161,7 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
|||||||
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);
|
||||||
if ((vox == nullptr || vox->id != 0) && block->skyLightPassing) //-V522
|
if ((vox == nullptr || vox->id != 0) && block.skyLightPassing)
|
||||||
break;
|
break;
|
||||||
solverS->add(x,i,z, 0xF);
|
solverS->add(x,i,z, 0xF);
|
||||||
}
|
}
|
||||||
@@ -177,7 +177,7 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
|||||||
solverB->solve();
|
solverB->solve();
|
||||||
solverS->solve();
|
solverS->solve();
|
||||||
} else {
|
} else {
|
||||||
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--){
|
||||||
solverS->remove(x,i,z);
|
solverS->remove(x,i,z);
|
||||||
@@ -191,10 +191,10 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){
|
|||||||
solverG->solve();
|
solverG->solve();
|
||||||
solverB->solve();
|
solverB->solve();
|
||||||
|
|
||||||
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();
|
||||||
solverG->solve();
|
solverG->solve();
|
||||||
solverB->solve();
|
solverB->solve();
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ void BlocksController::updateSides(int x, int y, int z) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BlocksController::breakBlock(
|
void BlocksController::breakBlock(
|
||||||
Player* player, const Block* def, int x, int y, int z
|
Player* player, const Block& def, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
onBlockInteraction(
|
onBlockInteraction(
|
||||||
player, glm::ivec3(x, y, z), def, BlockInteraction::destruction
|
player, glm::ivec3(x, y, z), def, BlockInteraction::destruction
|
||||||
@@ -46,14 +46,14 @@ void BlocksController::breakBlock(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BlocksController::placeBlock(
|
void BlocksController::placeBlock(
|
||||||
Player* player, const Block* def, blockstate state, int x, int y, int z
|
Player* player, const Block& def, blockstate state, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
onBlockInteraction(
|
onBlockInteraction(
|
||||||
player, glm::ivec3(x, y, z), def, BlockInteraction::placing
|
player, glm::ivec3(x, y, z), def, BlockInteraction::placing
|
||||||
);
|
);
|
||||||
chunks->set(x, y, z, def->rt.id, state);
|
chunks->set(x, y, z, def.rt.id, state);
|
||||||
lighting->onBlockSet(x, y, z, def->rt.id);
|
lighting->onBlockSet(x, y, z, def.rt.id);
|
||||||
if (def->rt.funcsset.onplaced) {
|
if (def.rt.funcsset.onplaced) {
|
||||||
scripting::on_block_placed(player, def, x, y, z);
|
scripting::on_block_placed(player, def, x, y, z);
|
||||||
}
|
}
|
||||||
updateSides(x, y, z);
|
updateSides(x, y, z);
|
||||||
@@ -62,15 +62,15 @@ void BlocksController::placeBlock(
|
|||||||
void BlocksController::updateBlock(int x, int y, int z) {
|
void BlocksController::updateBlock(int x, int y, int z) {
|
||||||
voxel* vox = chunks->get(x, y, z);
|
voxel* vox = chunks->get(x, y, z);
|
||||||
if (vox == nullptr) return;
|
if (vox == nullptr) return;
|
||||||
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
|
auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||||
if (def->grounded) { //-V522
|
if (def.grounded) {
|
||||||
const auto& vec = get_ground_direction(def, vox->state.rotation);
|
const auto& vec = get_ground_direction(def, vox->state.rotation);
|
||||||
if (!chunks->isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) {
|
if (!chunks->isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) {
|
||||||
breakBlock(nullptr, def, x, y, z);
|
breakBlock(nullptr, def, x, y, z);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (def->rt.funcsset.update) {
|
if (def.rt.funcsset.update) {
|
||||||
scripting::update_block(def, x, y, z);
|
scripting::update_block(def, x, y, z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,9 +93,9 @@ void BlocksController::onBlocksTick(int tickid, int parts) {
|
|||||||
int tickRate = blocksTickClock.getTickRate();
|
int tickRate = blocksTickClock.getTickRate();
|
||||||
for (size_t id = 0; id < indices->blocks.count(); id++) {
|
for (size_t id = 0; id < indices->blocks.count(); id++) {
|
||||||
if ((id + tickid) % parts != 0) continue;
|
if ((id + tickid) % parts != 0) continue;
|
||||||
auto def = indices->blocks.get(id); //FIXME: Potentional null pointer
|
auto& def = indices->blocks.require(id);
|
||||||
auto interval = def->tickInterval; //-V522
|
auto interval = def.tickInterval;
|
||||||
if (def->rt.funcsset.onblockstick && tickid / parts % interval == 0) {
|
if (def.rt.funcsset.onblockstick && tickid / parts % interval == 0) {
|
||||||
scripting::on_blocks_tick(def, tickRate / interval);
|
scripting::on_blocks_tick(def, tickRate / interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -112,8 +112,8 @@ void BlocksController::randomTick(
|
|||||||
int by = random.rand() % segheight + s * segheight;
|
int by = random.rand() % segheight + s * segheight;
|
||||||
int bz = random.rand() % CHUNK_D;
|
int bz = random.rand() % CHUNK_D;
|
||||||
const voxel& vox = chunk.voxels[(by * CHUNK_D + bz) * CHUNK_W + bx];
|
const voxel& vox = chunk.voxels[(by * CHUNK_D + bz) * CHUNK_W + bx];
|
||||||
Block* block = indices->blocks.get(vox.id); //FIXME: Potentional null pointer
|
auto& block = indices->blocks.require(vox.id);
|
||||||
if (block->rt.funcsset.randupdate) { //-V522
|
if (block.rt.funcsset.randupdate) {
|
||||||
scripting::random_update_block(
|
scripting::random_update_block(
|
||||||
block, chunk.x * CHUNK_W + bx, by, chunk.z * CHUNK_D + bz
|
block, chunk.x * CHUNK_W + bx, by, chunk.z * CHUNK_D + bz
|
||||||
);
|
);
|
||||||
@@ -188,7 +188,7 @@ void BlocksController::unbindInventory(int x, int y, int z) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void BlocksController::onBlockInteraction(
|
void BlocksController::onBlockInteraction(
|
||||||
Player* player, glm::ivec3 pos, const Block* def, BlockInteraction type
|
Player* player, glm::ivec3 pos, const Block& def, BlockInteraction type
|
||||||
) {
|
) {
|
||||||
for (const auto& callback : blockInteractionCallbacks) {
|
for (const auto& callback : blockInteractionCallbacks) {
|
||||||
callback(player, pos, def, type);
|
callback(player, pos, def, type);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ enum class BlockInteraction { step, destruction, placing };
|
|||||||
|
|
||||||
/// @brief Player argument is nullable
|
/// @brief Player argument is nullable
|
||||||
using on_block_interaction = std::function<
|
using on_block_interaction = std::function<
|
||||||
void(Player*, glm::ivec3, const Block*, BlockInteraction type)>;
|
void(Player*, glm::ivec3, const Block&, BlockInteraction type)>;
|
||||||
|
|
||||||
/// BlocksController manages block updates and data (inventories, metadata)
|
/// BlocksController manages block updates and data (inventories, metadata)
|
||||||
class BlocksController {
|
class BlocksController {
|
||||||
@@ -40,9 +40,9 @@ public:
|
|||||||
void updateSides(int x, int y, int z);
|
void updateSides(int x, int y, int z);
|
||||||
void updateBlock(int x, int y, int z);
|
void updateBlock(int x, int y, int z);
|
||||||
|
|
||||||
void breakBlock(Player* player, const Block* def, int x, int y, int z);
|
void breakBlock(Player* player, const Block& def, int x, int y, int z);
|
||||||
void placeBlock(
|
void placeBlock(
|
||||||
Player* player, const Block* def, blockstate state, int x, int y, int z
|
Player* player, const Block& def, blockstate state, int x, int y, int z
|
||||||
);
|
);
|
||||||
|
|
||||||
void update(float delta);
|
void update(float delta);
|
||||||
@@ -56,7 +56,7 @@ public:
|
|||||||
void unbindInventory(int x, int y, int z);
|
void unbindInventory(int x, int y, int z);
|
||||||
|
|
||||||
void onBlockInteraction(
|
void onBlockInteraction(
|
||||||
Player* player, glm::ivec3 pos, const Block* def, BlockInteraction type
|
Player* player, glm::ivec3 pos, const Block& def, BlockInteraction type
|
||||||
);
|
);
|
||||||
|
|
||||||
/// @brief Add block interaction callback
|
/// @brief Add block interaction callback
|
||||||
|
|||||||
@@ -208,8 +208,10 @@ void PlayerController::onFootstep(const Hitbox& hitbox) {
|
|||||||
int z = std::floor(pos.z + half.z * offsetZ);
|
int z = std::floor(pos.z + half.z * offsetZ);
|
||||||
auto vox = level->chunks->get(x, y, z);
|
auto vox = level->chunks->get(x, y, z);
|
||||||
if (vox) {
|
if (vox) {
|
||||||
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
|
auto& def = level->content->getIndices()->blocks.require(vox->id);
|
||||||
if (!def->obstacle) continue; //-V522
|
if (!def.obstacle) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
blocksController->onBlockInteraction(
|
blocksController->onBlockInteraction(
|
||||||
player.get(),
|
player.get(),
|
||||||
glm::ivec3(x, y, z),
|
glm::ivec3(x, y, z),
|
||||||
@@ -299,7 +301,7 @@ void PlayerController::updatePlayer(float delta) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int determine_rotation(
|
static int determine_rotation(
|
||||||
Block* def, const glm::ivec3& norm, glm::vec3& camDir
|
const Block* def, const glm::ivec3& norm, glm::vec3& camDir
|
||||||
) {
|
) {
|
||||||
if (def && def->rotatable) {
|
if (def && def->rotatable) {
|
||||||
const std::string& name = def->rotations.name;
|
const std::string& name = def->rotations.name;
|
||||||
@@ -396,7 +398,7 @@ voxel* PlayerController::updateSelection(float maxDistance) {
|
|||||||
selection.vox = *vox;
|
selection.vox = *vox;
|
||||||
if (selectedState.segment) {
|
if (selectedState.segment) {
|
||||||
selection.position = chunks->seekOrigin(
|
selection.position = chunks->seekOrigin(
|
||||||
iend, indices->blocks.get(selection.vox.id), selectedState
|
iend, indices->blocks.require(selection.vox.id), selectedState
|
||||||
);
|
);
|
||||||
auto origin = chunks->get(selection.position);
|
auto origin = chunks->get(selection.position);
|
||||||
if (origin && origin->id != vox->id) {
|
if (origin && origin->id != vox->id) {
|
||||||
@@ -411,15 +413,15 @@ voxel* PlayerController::updateSelection(float maxDistance) {
|
|||||||
return vox;
|
return vox;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::processRightClick(Block* def, Block* target) {
|
void PlayerController::processRightClick(const Block& def, const Block& target) {
|
||||||
const auto& selection = player->selection;
|
const auto& selection = player->selection;
|
||||||
auto chunks = level->chunks.get();
|
auto chunks = level->chunks.get();
|
||||||
auto camera = player->camera.get();
|
auto camera = player->camera.get();
|
||||||
|
|
||||||
blockstate state {};
|
blockstate state {};
|
||||||
state.rotation = determine_rotation(def, selection.normal, camera->dir);
|
state.rotation = determine_rotation(&def, selection.normal, camera->dir);
|
||||||
|
|
||||||
if (!input.shift && target->rt.funcsset.oninteract) {
|
if (!input.shift && target.rt.funcsset.oninteract) {
|
||||||
if (scripting::on_block_interact(
|
if (scripting::on_block_interact(
|
||||||
player.get(), target, selection.position
|
player.get(), target, selection.position
|
||||||
)) {
|
)) {
|
||||||
@@ -427,17 +429,17 @@ void PlayerController::processRightClick(Block* def, Block* target) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto coord = selection.actualPosition;
|
auto coord = selection.actualPosition;
|
||||||
if (!target->replaceable) {
|
if (!target.replaceable) {
|
||||||
coord += selection.normal;
|
coord += selection.normal;
|
||||||
} else if (def->rotations.name == BlockRotProfile::PIPE_NAME) {
|
} else if (def.rotations.name == BlockRotProfile::PIPE_NAME) {
|
||||||
state.rotation = BLOCK_DIR_UP;
|
state.rotation = BLOCK_DIR_UP;
|
||||||
}
|
}
|
||||||
blockid_t chosenBlock = def->rt.id;
|
blockid_t chosenBlock = def.rt.id;
|
||||||
|
|
||||||
AABB blockAABB(coord, coord + 1);
|
AABB blockAABB(coord, coord + 1);
|
||||||
bool blocked = level->entities->hasBlockingInside(blockAABB);
|
bool blocked = level->entities->hasBlockingInside(blockAABB);
|
||||||
|
|
||||||
if (def->obstacle && blocked) {
|
if (def.obstacle && blocked) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto vox = chunks->get(coord);
|
auto vox = chunks->get(coord);
|
||||||
@@ -447,7 +449,7 @@ void PlayerController::processRightClick(Block* def, Block* target) {
|
|||||||
if (!chunks->checkReplaceability(def, state, coord)) {
|
if (!chunks->checkReplaceability(def, state, coord)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (def->grounded) {
|
if (def.grounded) {
|
||||||
const auto& vec = get_ground_direction(def, state.rotation);
|
const auto& vec = get_ground_direction(def, state.rotation);
|
||||||
if (!chunks->isSolidBlock(
|
if (!chunks->isSolidBlock(
|
||||||
coord.x + vec.x, coord.y + vec.y, coord.z + vec.z
|
coord.x + vec.x, coord.y + vec.y, coord.z + vec.z
|
||||||
@@ -492,11 +494,11 @@ void PlayerController::updateInteraction() {
|
|||||||
|
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
const ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
const ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
||||||
ItemDef* item = indices->items.get(stack.getItemId()); //FIXME: Potentional null pointer
|
auto& item = indices->items.require(stack.getItemId());
|
||||||
|
|
||||||
auto vox = updateSelection(maxDistance);
|
auto vox = updateSelection(maxDistance);
|
||||||
if (vox == nullptr) {
|
if (vox == nullptr) {
|
||||||
if (rclick && item->rt.funcsset.on_use) { //-V522
|
if (rclick && item.rt.funcsset.on_use) {
|
||||||
scripting::on_item_use(player.get(), item);
|
scripting::on_item_use(player.get(), item);
|
||||||
}
|
}
|
||||||
if (selection.entity) {
|
if (selection.entity) {
|
||||||
@@ -506,35 +508,35 @@ void PlayerController::updateInteraction() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto iend = selection.position;
|
auto iend = selection.position;
|
||||||
if (lclick && !input.shift && item->rt.funcsset.on_block_break_by) {
|
if (lclick && !input.shift && item.rt.funcsset.on_block_break_by) {
|
||||||
if (scripting::on_item_break_block(
|
if (scripting::on_item_break_block(
|
||||||
player.get(), item, iend.x, iend.y, iend.z
|
player.get(), item, iend.x, iend.y, iend.z
|
||||||
)) {
|
)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto target = indices->blocks.get(vox->id); //FIXME: Potentional null pointer
|
auto& target = indices->blocks.require(vox->id);
|
||||||
if (lclick && target->breakable) { //-V522
|
if (lclick && target.breakable) {
|
||||||
blocksController->breakBlock(
|
blocksController->breakBlock(
|
||||||
player.get(), target, iend.x, iend.y, iend.z
|
player.get(), target, iend.x, iend.y, iend.z
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (rclick && !input.shift) {
|
if (rclick && !input.shift) {
|
||||||
bool preventDefault = false;
|
bool preventDefault = false;
|
||||||
if (item->rt.funcsset.on_use_on_block) {
|
if (item.rt.funcsset.on_use_on_block) {
|
||||||
preventDefault = scripting::on_item_use_on_block(
|
preventDefault = scripting::on_item_use_on_block(
|
||||||
player.get(), item, iend, selection.normal
|
player.get(), item, iend, selection.normal
|
||||||
);
|
);
|
||||||
} else if (item->rt.funcsset.on_use) {
|
} else if (item.rt.funcsset.on_use) {
|
||||||
preventDefault = scripting::on_item_use(player.get(), item);
|
preventDefault = scripting::on_item_use(player.get(), item);
|
||||||
}
|
}
|
||||||
if (preventDefault) {
|
if (preventDefault) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto def = indices->blocks.get(item->rt.placingBlock);
|
auto def = indices->blocks.get(item.rt.placingBlock);
|
||||||
if (def && rclick) {
|
if (def && rclick) {
|
||||||
processRightClick(def, target);
|
processRightClick(*def, target);
|
||||||
}
|
}
|
||||||
if (Events::jactive(BIND_PLAYER_PICK)) {
|
if (Events::jactive(BIND_PLAYER_PICK)) {
|
||||||
auto coord = selection.actualPosition;
|
auto coord = selection.actualPosition;
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ class PlayerController {
|
|||||||
float stepsTimer = 0.0f;
|
float stepsTimer = 0.0f;
|
||||||
void onFootstep(const Hitbox& hitbox);
|
void onFootstep(const Hitbox& hitbox);
|
||||||
void updateFootsteps(float delta);
|
void updateFootsteps(float delta);
|
||||||
void processRightClick(Block* def, Block* target);
|
void processRightClick(const Block& def, const Block& target);
|
||||||
|
|
||||||
voxel* updateSelection(float maxDistance);
|
voxel* updateSelection(float maxDistance);
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -11,12 +11,9 @@
|
|||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
static Block* require_block(lua::State* L) {
|
static const Block* require_block(lua::State* L) {
|
||||||
auto indices = content->getIndices();
|
auto indices = content->getIndices();
|
||||||
auto id = lua::tointeger(L, 1);
|
auto id = lua::tointeger(L, 1);
|
||||||
if (static_cast<size_t>(id) >= indices->blocks.count()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return indices->blocks.get(id);
|
return indices->blocks.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +75,7 @@ static int l_seek_origin(lua::State* L) {
|
|||||||
auto y = lua::tointeger(L, 2);
|
auto y = lua::tointeger(L, 2);
|
||||||
auto z = lua::tointeger(L, 3);
|
auto z = lua::tointeger(L, 3);
|
||||||
auto vox = level->chunks->get(x, y, z);
|
auto vox = level->chunks->get(x, y, z);
|
||||||
auto def = indices->blocks.get(vox->id);
|
auto& def = indices->blocks.require(vox->id);
|
||||||
return lua::pushivec3_stack(
|
return lua::pushivec3_stack(
|
||||||
L, level->chunks->seekOrigin({x, y, z}, def, vox->state)
|
L, level->chunks->seekOrigin({x, y, z}, def, vox->state)
|
||||||
);
|
);
|
||||||
@@ -330,7 +327,7 @@ static int l_place(lua::State* L) {
|
|||||||
}
|
}
|
||||||
auto player = level->getObject<Player>(playerid);
|
auto player = level->getObject<Player>(playerid);
|
||||||
controller->getBlocksController()->placeBlock(
|
controller->getBlocksController()->placeBlock(
|
||||||
player ? player.get() : nullptr, def, int2blockstate(state), x, y, z
|
player ? player.get() : nullptr, *def, int2blockstate(state), x, y, z
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -344,7 +341,7 @@ static int l_destruct(lua::State* L) {
|
|||||||
if (voxel == nullptr) {
|
if (voxel == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
const auto def = level->content->getIndices()->blocks.get(voxel->id);
|
auto& def = level->content->getIndices()->blocks.require(voxel->id);
|
||||||
auto player = level->getObject<Player>(playerid);
|
auto player = level->getObject<Player>(playerid);
|
||||||
controller->getBlocksController()->breakBlock(
|
controller->getBlocksController()->breakBlock(
|
||||||
player ? player.get() : nullptr, def, x, y, z
|
player ? player.get() : nullptr, def, x, y, z
|
||||||
|
|||||||
@@ -12,12 +12,9 @@
|
|||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
static EntityDef* require_entity_def(lua::State* L) {
|
static const EntityDef* require_entity_def(lua::State* L) {
|
||||||
auto indices = content->getIndices();
|
auto indices = content->getIndices();
|
||||||
auto id = lua::tointeger(L, 1);
|
auto id = lua::tointeger(L, 1);
|
||||||
if (static_cast<size_t>(id) >= indices->entities.count()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return indices->entities.get(id);
|
return indices->entities.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,9 @@
|
|||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
static ItemDef* get_item_def(lua::State* L, int idx) {
|
static const ItemDef* get_item_def(lua::State* L, int idx) {
|
||||||
auto indices = content->getIndices();
|
auto indices = content->getIndices();
|
||||||
auto id = lua::tointeger(L, idx);
|
auto id = lua::tointeger(L, idx);
|
||||||
if (static_cast<size_t>(id) >= indices->items.count()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return indices->items.get(id);
|
return indices->items.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -196,38 +196,38 @@ void scripting::on_world_quit() {
|
|||||||
scripting::controller = nullptr;
|
scripting::controller = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_blocks_tick(const Block* block, int tps) {
|
void scripting::on_blocks_tick(const Block& block, int tps) {
|
||||||
std::string name = block->name + ".blockstick";
|
std::string name = block.name + ".blockstick";
|
||||||
lua::emit_event(lua::get_main_thread(), name, [tps](auto L) {
|
lua::emit_event(lua::get_main_thread(), name, [tps](auto L) {
|
||||||
return lua::pushinteger(L, tps);
|
return lua::pushinteger(L, tps);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) {
|
lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) {
|
||||||
return lua::pushivec3_stack(L, x, y, z);
|
return lua::pushivec3_stack(L, x, y, z);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::random_update_block(const Block* block, int x, int y, int z) {
|
void scripting::random_update_block(const Block& block, int x, int y, int z) {
|
||||||
std::string name = block->name + ".randupdate";
|
std::string name = block.name + ".randupdate";
|
||||||
lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) {
|
lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) {
|
||||||
return lua::pushivec3_stack(L, x, y, z);
|
return lua::pushivec3_stack(L, x, y, z);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_block_placed(
|
void scripting::on_block_placed(
|
||||||
Player* player, const Block* block, int x, int y, int z
|
Player* player, const Block& block, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
std::string name = block->name + ".placed";
|
std::string name = block.name + ".placed";
|
||||||
lua::emit_event(lua::get_main_thread(), name, [x, y, z, player](auto L) {
|
lua::emit_event(lua::get_main_thread(), name, [x, y, z, player](auto L) {
|
||||||
lua::pushivec3_stack(L, x, y, z);
|
lua::pushivec3_stack(L, x, y, z);
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
return 4;
|
return 4;
|
||||||
});
|
});
|
||||||
auto world_event_args = [block, x, y, z, player](lua::State* L) {
|
auto world_event_args = [&](lua::State* L) {
|
||||||
lua::pushinteger(L, block->rt.id);
|
lua::pushinteger(L, block.rt.id);
|
||||||
lua::pushivec3_stack(L, x, y, z);
|
lua::pushivec3_stack(L, x, y, z);
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
return 5;
|
return 5;
|
||||||
@@ -244,10 +244,10 @@ void scripting::on_block_placed(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void scripting::on_block_broken(
|
void scripting::on_block_broken(
|
||||||
Player* player, const Block* block, int x, int y, int z
|
Player* player, const Block& block, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
if (block->rt.funcsset.onbroken) {
|
if (block.rt.funcsset.onbroken) {
|
||||||
std::string name = block->name + ".broken";
|
std::string name = block.name + ".broken";
|
||||||
lua::emit_event(
|
lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_thread(),
|
||||||
name,
|
name,
|
||||||
@@ -258,8 +258,8 @@ void scripting::on_block_broken(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
auto world_event_args = [block, x, y, z, player](lua::State* L) {
|
auto world_event_args = [&](lua::State* L) {
|
||||||
lua::pushinteger(L, block->rt.id);
|
lua::pushinteger(L, block.rt.id);
|
||||||
lua::pushivec3_stack(L, x, y, z);
|
lua::pushivec3_stack(L, x, y, z);
|
||||||
lua::pushinteger(L, player ? player->getId() : -1);
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
return 5;
|
return 5;
|
||||||
@@ -276,9 +276,9 @@ void scripting::on_block_broken(
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool scripting::on_block_interact(
|
bool scripting::on_block_interact(
|
||||||
Player* player, const Block* block, glm::ivec3 pos
|
Player* player, const Block& block, glm::ivec3 pos
|
||||||
) {
|
) {
|
||||||
std::string name = block->name + ".interact";
|
std::string name = block.name + ".interact";
|
||||||
return lua::emit_event(lua::get_main_thread(), name, [pos, player](auto L) {
|
return lua::emit_event(lua::get_main_thread(), name, [pos, player](auto L) {
|
||||||
lua::pushivec3_stack(L, pos.x, pos.y, pos.z);
|
lua::pushivec3_stack(L, pos.x, pos.y, pos.z);
|
||||||
lua::pushinteger(L, player->getId());
|
lua::pushinteger(L, player->getId());
|
||||||
@@ -286,8 +286,8 @@ bool scripting::on_block_interact(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool scripting::on_item_use(Player* player, const ItemDef* item) {
|
bool scripting::on_item_use(Player* player, const ItemDef& item) {
|
||||||
std::string name = item->name + ".use";
|
std::string name = item.name + ".use";
|
||||||
return lua::emit_event(
|
return lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_thread(),
|
||||||
name,
|
name,
|
||||||
@@ -296,9 +296,9 @@ bool scripting::on_item_use(Player* player, const ItemDef* item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool scripting::on_item_use_on_block(
|
bool scripting::on_item_use_on_block(
|
||||||
Player* player, const ItemDef* item, glm::ivec3 ipos, glm::ivec3 normal
|
Player* player, const ItemDef& item, glm::ivec3 ipos, glm::ivec3 normal
|
||||||
) {
|
) {
|
||||||
std::string name = item->name + ".useon";
|
std::string name = item.name + ".useon";
|
||||||
return lua::emit_event(
|
return lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_thread(),
|
||||||
name,
|
name,
|
||||||
@@ -312,9 +312,9 @@ bool scripting::on_item_use_on_block(
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool scripting::on_item_break_block(
|
bool scripting::on_item_break_block(
|
||||||
Player* player, const ItemDef* item, int x, int y, int z
|
Player* player, const ItemDef& item, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
std::string name = item->name + ".blockbreakby";
|
std::string name = item.name + ".blockbreakby";
|
||||||
return lua::emit_event(
|
return lua::emit_event(
|
||||||
lua::get_main_thread(),
|
lua::get_main_thread(),
|
||||||
name,
|
name,
|
||||||
|
|||||||
@@ -61,31 +61,31 @@ namespace scripting {
|
|||||||
void on_world_tick();
|
void on_world_tick();
|
||||||
void on_world_save();
|
void on_world_save();
|
||||||
void on_world_quit();
|
void on_world_quit();
|
||||||
void on_blocks_tick(const Block* block, int tps);
|
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(
|
void on_block_placed(
|
||||||
Player* player, const Block* block, int x, int y, int z
|
Player* player, const Block& block, int x, int y, int z
|
||||||
);
|
);
|
||||||
void on_block_broken(
|
void on_block_broken(
|
||||||
Player* player, const Block* block, int x, int y, int z
|
Player* player, const Block& block, int x, int y, int z
|
||||||
);
|
);
|
||||||
bool on_block_interact(Player* player, const Block* block, glm::ivec3 pos);
|
bool on_block_interact(Player* player, const Block& block, glm::ivec3 pos);
|
||||||
|
|
||||||
/// @brief Called on RMB click with the item selected
|
/// @brief Called on RMB click with the item selected
|
||||||
/// @return true if prevents default action
|
/// @return true if prevents default action
|
||||||
bool on_item_use(Player* player, const ItemDef* item);
|
bool on_item_use(Player* player, const ItemDef& item);
|
||||||
|
|
||||||
/// @brief Called on RMB click on block with the item selected
|
/// @brief Called on RMB click on block with the item selected
|
||||||
/// @return true if prevents default action
|
/// @return true if prevents default action
|
||||||
bool on_item_use_on_block(
|
bool on_item_use_on_block(
|
||||||
Player* player, const ItemDef* item, glm::ivec3 ipos, glm::ivec3 normal
|
Player* player, const ItemDef& item, glm::ivec3 ipos, glm::ivec3 normal
|
||||||
);
|
);
|
||||||
|
|
||||||
/// @brief Called on LMB click on block with the item selected
|
/// @brief Called on LMB click on block with the item selected
|
||||||
/// @return true if prevents default action
|
/// @return true if prevents default action
|
||||||
bool on_item_break_block(
|
bool on_item_break_block(
|
||||||
Player* player, const ItemDef* item, int x, int y, int z
|
Player* player, const ItemDef& item, int x, int y, int z
|
||||||
);
|
);
|
||||||
|
|
||||||
dynamic::Value get_component_value(
|
dynamic::Value get_component_value(
|
||||||
|
|||||||
@@ -214,8 +214,8 @@ public:
|
|||||||
Block(const Block&) = delete;
|
Block(const Block&) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline glm::ivec3 get_ground_direction(const Block* def, int rotation) {
|
inline glm::ivec3 get_ground_direction(const Block& def, int rotation) {
|
||||||
return -def->rotations.variants[rotation].axisY;
|
return -def.rotations.variants[rotation].axisY;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // VOXELS_BLOCK_HPP_
|
#endif // VOXELS_BLOCK_HPP_
|
||||||
|
|||||||
+56
-54
@@ -76,15 +76,15 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) {
|
|||||||
return ∅
|
return ∅
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const auto def = indices->blocks.get(v->id); //FIXME: Potentional null pointer
|
const auto& def = indices->blocks.require(v->id);
|
||||||
if (def->obstacle) { //-V522
|
if (def.obstacle) {
|
||||||
glm::ivec3 offset {};
|
glm::ivec3 offset {};
|
||||||
if (v->state.segment) {
|
if (v->state.segment) {
|
||||||
glm::ivec3 point(ix, iy, iz);
|
glm::ivec3 point(ix, iy, iz);
|
||||||
offset = seekOrigin(point, def, v->state) - point;
|
offset = seekOrigin(point, def, v->state) - point;
|
||||||
}
|
}
|
||||||
const auto& boxes = def->rotatable ? def->rt.hitboxes[v->state.rotation]
|
const auto& boxes =
|
||||||
: def->hitboxes;
|
def.rotatable ? def.rt.hitboxes[v->state.rotation] : def.hitboxes;
|
||||||
for (const auto& hitbox : boxes) {
|
for (const auto& hitbox : boxes) {
|
||||||
if (hitbox.contains(
|
if (hitbox.contains(
|
||||||
{x - ix - offset.x, y - iy - offset.y, z - iz - offset.z}
|
{x - ix - offset.x, y - iy - offset.y, z - iz - offset.z}
|
||||||
@@ -99,19 +99,19 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) {
|
|||||||
bool Chunks::isSolidBlock(int32_t x, int32_t y, int32_t z) {
|
bool Chunks::isSolidBlock(int32_t x, int32_t y, int32_t z) {
|
||||||
voxel* v = get(x, y, z);
|
voxel* v = get(x, y, z);
|
||||||
if (v == nullptr) return false;
|
if (v == nullptr) return false;
|
||||||
return indices->blocks.get(v->id)->rt.solid; //-V522
|
return indices->blocks.get(v->id)->rt.solid; //-V522
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::isReplaceableBlock(int32_t x, int32_t y, int32_t z) {
|
bool Chunks::isReplaceableBlock(int32_t x, int32_t y, int32_t z) {
|
||||||
voxel* v = get(x, y, z);
|
voxel* v = get(x, y, z);
|
||||||
if (v == nullptr) return false;
|
if (v == nullptr) return false;
|
||||||
return indices->blocks.get(v->id)->replaceable; //-V522
|
return indices->blocks.get(v->id)->replaceable; //-V522
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::isObstacleBlock(int32_t x, int32_t y, int32_t z) {
|
bool Chunks::isObstacleBlock(int32_t x, int32_t y, int32_t z) {
|
||||||
voxel* v = get(x, y, z);
|
voxel* v = get(x, y, z);
|
||||||
if (v == nullptr) return false;
|
if (v == nullptr) return false;
|
||||||
return indices->blocks.get(v->id)->obstacle; //-V522
|
return indices->blocks.get(v->id)->obstacle; //-V522
|
||||||
}
|
}
|
||||||
|
|
||||||
ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel) {
|
ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel) {
|
||||||
@@ -173,9 +173,9 @@ Chunk* Chunks::getChunk(int x, int z) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
glm::ivec3 Chunks::seekOrigin(
|
glm::ivec3 Chunks::seekOrigin(
|
||||||
glm::ivec3 pos, const Block* def, blockstate state
|
glm::ivec3 pos, const Block& def, blockstate state
|
||||||
) {
|
) {
|
||||||
const auto& rotation = def->rotations.variants[state.rotation];
|
const auto& rotation = def.rotations.variants[state.rotation];
|
||||||
auto segment = state.segment;
|
auto segment = state.segment;
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!segment) {
|
if (!segment) {
|
||||||
@@ -194,12 +194,12 @@ glm::ivec3 Chunks::seekOrigin(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::eraseSegments(
|
void Chunks::eraseSegments(
|
||||||
const Block* def, blockstate state, int x, int y, int z
|
const Block& def, blockstate state, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
const auto& rotation = def->rotations.variants[state.rotation];
|
const auto& rotation = def.rotations.variants[state.rotation];
|
||||||
for (int sy = 0; sy < def->size.y; sy++) {
|
for (int sy = 0; sy < def.size.y; sy++) {
|
||||||
for (int sz = 0; sz < def->size.z; sz++) {
|
for (int sz = 0; sz < def.size.z; sz++) {
|
||||||
for (int sx = 0; sx < def->size.x; sx++) {
|
for (int sx = 0; sx < def.size.x; sx++) {
|
||||||
if ((sx | sy | sz) == 0) {
|
if ((sx | sy | sz) == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -218,11 +218,11 @@ static constexpr uint8_t segment_to_int(int sx, int sy, int sz) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::repairSegments(
|
void Chunks::repairSegments(
|
||||||
const Block* def, blockstate state, int x, int y, int z
|
const Block& def, blockstate state, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
const auto& rotation = def->rotations.variants[state.rotation];
|
const auto& rotation = def.rotations.variants[state.rotation];
|
||||||
const auto id = def->rt.id;
|
const auto id = def.rt.id;
|
||||||
const auto size = def->size;
|
const auto size = def.size;
|
||||||
for (int sy = 0; sy < size.y; sy++) {
|
for (int sy = 0; sy < size.y; sy++) {
|
||||||
for (int sz = 0; sz < size.z; sz++) {
|
for (int sz = 0; sz < size.z; sz++) {
|
||||||
for (int sx = 0; sx < size.x; sx++) {
|
for (int sx = 0; sx < size.x; sx++) {
|
||||||
@@ -243,10 +243,10 @@ void Chunks::repairSegments(
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::checkReplaceability(
|
bool Chunks::checkReplaceability(
|
||||||
const Block* def, blockstate state, glm::ivec3 origin, blockid_t ignore
|
const Block& def, blockstate state, glm::ivec3 origin, blockid_t ignore
|
||||||
) {
|
) {
|
||||||
const auto& rotation = def->rotations.variants[state.rotation];
|
const auto& rotation = def.rotations.variants[state.rotation];
|
||||||
const auto size = def->size;
|
const auto size = def.size;
|
||||||
for (int sy = 0; sy < size.y; sy++) {
|
for (int sy = 0; sy < size.y; sy++) {
|
||||||
for (int sz = 0; sz < size.z; sz++) {
|
for (int sz = 0; sz < size.z; sz++) {
|
||||||
for (int sx = 0; sx < size.x; sx++) {
|
for (int sx = 0; sx < size.x; sx++) {
|
||||||
@@ -255,8 +255,8 @@ bool Chunks::checkReplaceability(
|
|||||||
pos += rotation.axisY * sy;
|
pos += rotation.axisY * sy;
|
||||||
pos += rotation.axisZ * sz;
|
pos += rotation.axisZ * sz;
|
||||||
if (auto vox = get(pos.x, pos.y, pos.z)) {
|
if (auto vox = get(pos.x, pos.y, pos.z)) {
|
||||||
auto target = indices->blocks.get(vox->id); //FIXME: Potentional null pointer
|
auto& target = indices->blocks.require(vox->id);
|
||||||
if (!target->replaceable && vox->id != ignore) { //-V522
|
if (!target.replaceable && vox->id != ignore) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -269,18 +269,18 @@ bool Chunks::checkReplaceability(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::setRotationExtended(
|
void Chunks::setRotationExtended(
|
||||||
Block* def, blockstate state, glm::ivec3 origin, uint8_t index
|
const Block& def, blockstate state, glm::ivec3 origin, uint8_t index
|
||||||
) {
|
) {
|
||||||
auto newstate = state;
|
auto newstate = state;
|
||||||
newstate.rotation = index;
|
newstate.rotation = index;
|
||||||
|
|
||||||
// unable to rotate block (cause: obstacles)
|
// unable to rotate block (cause: obstacles)
|
||||||
if (!checkReplaceability(def, newstate, origin, def->rt.id)) {
|
if (!checkReplaceability(def, newstate, origin, def.rt.id)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto& rotation = def->rotations.variants[index];
|
const auto& rotation = def.rotations.variants[index];
|
||||||
const auto size = def->size;
|
const auto size = def.size;
|
||||||
std::vector<glm::ivec3> segmentBlocks;
|
std::vector<glm::ivec3> segmentBlocks;
|
||||||
|
|
||||||
for (int sy = 0; sy < size.y; sy++) {
|
for (int sy = 0; sy < size.y; sy++) {
|
||||||
@@ -296,18 +296,19 @@ void Chunks::setRotationExtended(
|
|||||||
|
|
||||||
auto vox = get(pos);
|
auto vox = get(pos);
|
||||||
// checked for nullptr by checkReplaceability
|
// checked for nullptr by checkReplaceability
|
||||||
if (vox->id != def->rt.id) {
|
if (vox->id != def.rt.id) {
|
||||||
set(pos.x, pos.y, pos.z, def->rt.id, segState);
|
set(pos.x, pos.y, pos.z, def.rt.id, segState);
|
||||||
} else {
|
} else {
|
||||||
vox->state = segState;
|
vox->state = segState;
|
||||||
auto chunk = getChunkByVoxel(pos.x, pos.y, pos.z); //FIXME: Potentional null pointer
|
auto chunk = getChunkByVoxel(pos.x, pos.y, pos.z);
|
||||||
chunk->setModifiedAndUnsaved(); //-V522
|
assert(chunk != nullptr);
|
||||||
|
chunk->setModifiedAndUnsaved();
|
||||||
segmentBlocks.emplace_back(pos);
|
segmentBlocks.emplace_back(pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const auto& prevRotation = def->rotations.variants[state.rotation];
|
const auto& prevRotation = def.rotations.variants[state.rotation];
|
||||||
for (int sy = 0; sy < size.y; sy++) {
|
for (int sy = 0; sy < size.y; sy++) {
|
||||||
for (int sz = 0; sz < size.z; sz++) {
|
for (int sz = 0; sz < size.z; sz++) {
|
||||||
for (int sx = 0; sx < size.x; sx++) {
|
for (int sx = 0; sx < size.x; sx++) {
|
||||||
@@ -333,16 +334,17 @@ void Chunks::setRotation(int32_t x, int32_t y, int32_t z, uint8_t index) {
|
|||||||
if (vox == nullptr) {
|
if (vox == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto def = indices->blocks.get(vox->id); //FIXME: Potentional null pointer
|
auto& def = indices->blocks.require(vox->id);
|
||||||
if (!def->rotatable || vox->state.rotation == index) { //-V522
|
if (!def.rotatable || vox->state.rotation == index) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (def->rt.extended) {
|
if (def.rt.extended) {
|
||||||
setRotationExtended(def, vox->state, {x, y, z}, index);
|
setRotationExtended(def, vox->state, {x, y, z}, index);
|
||||||
} else {
|
} else {
|
||||||
vox->state.rotation = index;
|
vox->state.rotation = index;
|
||||||
auto chunk = getChunkByVoxel(x, y, z); //FIXME: Potentional null pointer
|
auto chunk = getChunkByVoxel(x, y, z);
|
||||||
chunk->setModifiedAndUnsaved(); //-V522
|
assert(chunk != nullptr);
|
||||||
|
chunk->setModifiedAndUnsaved();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,20 +373,20 @@ void Chunks::set(
|
|||||||
|
|
||||||
// block finalization
|
// block finalization
|
||||||
voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
|
voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
|
||||||
auto prevdef = indices->blocks.get(vox.id); //FIXME: Potentional null pointer
|
const auto& prevdef = indices->blocks.require(vox.id);
|
||||||
if (prevdef->inventorySize == 0) { //-V522
|
if (prevdef.inventorySize == 0) {
|
||||||
chunk->removeBlockInventory(lx, y, lz);
|
chunk->removeBlockInventory(lx, y, lz);
|
||||||
}
|
}
|
||||||
if (prevdef->rt.extended && !vox.state.segment) {
|
if (prevdef.rt.extended && !vox.state.segment) {
|
||||||
eraseSegments(prevdef, vox.state, gx, y, gz);
|
eraseSegments(prevdef, vox.state, gx, y, gz);
|
||||||
}
|
}
|
||||||
|
|
||||||
// block initialization
|
// block initialization
|
||||||
auto newdef = indices->blocks.get(id); //FIXME: Potentional null pointer
|
const auto& newdef = indices->blocks.require(id);
|
||||||
vox.id = id;
|
vox.id = id;
|
||||||
vox.state = state;
|
vox.state = state;
|
||||||
chunk->setModifiedAndUnsaved();
|
chunk->setModifiedAndUnsaved();
|
||||||
if (!state.segment && newdef->rt.extended) { //-V522
|
if (!state.segment && newdef.rt.extended) {
|
||||||
repairSegments(newdef, state, gx, y, gz);
|
repairSegments(newdef, state, gx, y, gz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -432,7 +434,7 @@ voxel* Chunks::rayCast(
|
|||||||
int stepz = (dz > 0.0f) ? 1 : -1;
|
int stepz = (dz > 0.0f) ? 1 : -1;
|
||||||
|
|
||||||
constexpr float infinity = std::numeric_limits<float>::infinity();
|
constexpr float infinity = std::numeric_limits<float>::infinity();
|
||||||
constexpr float epsilon = 1e-6f; // 0.000001
|
constexpr float epsilon = 1e-6f; // 0.000001
|
||||||
float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx);
|
float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx);
|
||||||
float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy);
|
float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy);
|
||||||
float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz);
|
float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz);
|
||||||
@@ -452,8 +454,8 @@ voxel* Chunks::rayCast(
|
|||||||
if (voxel == nullptr) {
|
if (voxel == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
const auto def = indices->blocks.get(voxel->id); //FIXME: Potentional null pointer
|
const auto& def = indices->blocks.require(voxel->id);
|
||||||
if (def->selectable) { //-V522
|
if (def.selectable) {
|
||||||
end.x = px + t * dx;
|
end.x = px + t * dx;
|
||||||
end.y = py + t * dy;
|
end.y = py + t * dy;
|
||||||
end.z = pz + t * dz;
|
end.z = pz + t * dz;
|
||||||
@@ -461,10 +463,10 @@ voxel* Chunks::rayCast(
|
|||||||
iend.y = iy;
|
iend.y = iy;
|
||||||
iend.z = iz;
|
iend.z = iz;
|
||||||
|
|
||||||
if (!def->rt.solid) {
|
if (!def.rt.solid) {
|
||||||
const std::vector<AABB>& hitboxes =
|
const std::vector<AABB>& hitboxes =
|
||||||
def->rotatable ? def->rt.hitboxes[voxel->state.rotation]
|
def.rotatable ? def.rt.hitboxes[voxel->state.rotation]
|
||||||
: def->hitboxes;
|
: def.hitboxes;
|
||||||
|
|
||||||
scalar_t distance = maxDist;
|
scalar_t distance = maxDist;
|
||||||
Ray ray(start, dir);
|
Ray ray(start, dir);
|
||||||
@@ -563,7 +565,7 @@ glm::vec3 Chunks::rayCastToObstacle(
|
|||||||
int stepz = (dz > 0.0f) ? 1 : -1;
|
int stepz = (dz > 0.0f) ? 1 : -1;
|
||||||
|
|
||||||
constexpr float infinity = std::numeric_limits<float>::infinity();
|
constexpr float infinity = std::numeric_limits<float>::infinity();
|
||||||
constexpr float epsilon = 1e-6f; // 0.000001
|
constexpr float epsilon = 1e-6f; // 0.000001
|
||||||
float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx);
|
float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx);
|
||||||
float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy);
|
float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy);
|
||||||
float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz);
|
float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz);
|
||||||
@@ -579,12 +581,12 @@ glm::vec3 Chunks::rayCastToObstacle(
|
|||||||
while (t <= maxDist) {
|
while (t <= maxDist) {
|
||||||
voxel* voxel = get(ix, iy, iz);
|
voxel* voxel = get(ix, iy, iz);
|
||||||
if (voxel) {
|
if (voxel) {
|
||||||
const auto def = indices->blocks.get(voxel->id); //FIXME: Potentional null pointer
|
const auto& def = indices->blocks.require(voxel->id);
|
||||||
if (def->obstacle) { //-V522
|
if (def.obstacle) {
|
||||||
if (!def->rt.solid) {
|
if (!def.rt.solid) {
|
||||||
const std::vector<AABB>& hitboxes =
|
const std::vector<AABB>& hitboxes =
|
||||||
def->rotatable ? def->rt.hitboxes[voxel->state.rotation]
|
def.rotatable ? def.rt.hitboxes[voxel->state.rotation]
|
||||||
: def->modelBoxes;
|
: def.modelBoxes;
|
||||||
|
|
||||||
scalar_t distance;
|
scalar_t distance;
|
||||||
glm::ivec3 norm;
|
glm::ivec3 norm;
|
||||||
|
|||||||
@@ -26,12 +26,12 @@ class Chunks {
|
|||||||
Level* level;
|
Level* level;
|
||||||
const ContentIndices* const indices;
|
const ContentIndices* const indices;
|
||||||
|
|
||||||
void eraseSegments(const Block* def, blockstate state, int x, int y, int z);
|
void eraseSegments(const Block& def, blockstate state, int x, int y, int z);
|
||||||
void repairSegments(
|
void repairSegments(
|
||||||
const Block* def, blockstate state, int x, int y, int z
|
const Block& def, blockstate state, int x, int y, int z
|
||||||
);
|
);
|
||||||
void setRotationExtended(
|
void setRotationExtended(
|
||||||
Block* def, blockstate state, glm::ivec3 origin, uint8_t rotation
|
const Block& def, blockstate state, glm::ivec3 origin, uint8_t rotation
|
||||||
);
|
);
|
||||||
public:
|
public:
|
||||||
std::vector<std::shared_ptr<Chunk>> chunks;
|
std::vector<std::shared_ptr<Chunk>> chunks;
|
||||||
@@ -72,7 +72,7 @@ public:
|
|||||||
/// @param def segment block definition
|
/// @param def segment block definition
|
||||||
/// @param state segment block state
|
/// @param state segment block state
|
||||||
/// @return origin block position or `pos` if block is not extended
|
/// @return origin block position or `pos` if block is not extended
|
||||||
glm::ivec3 seekOrigin(glm::ivec3 pos, const Block* def, blockstate state);
|
glm::ivec3 seekOrigin(glm::ivec3 pos, const Block& def, blockstate state);
|
||||||
|
|
||||||
/// @brief Check if required zone is replaceable
|
/// @brief Check if required zone is replaceable
|
||||||
/// @param def definition of the block that requires a replaceable zone
|
/// @param def definition of the block that requires a replaceable zone
|
||||||
@@ -80,7 +80,7 @@ public:
|
|||||||
/// @param coord position of the zone start
|
/// @param coord position of the zone start
|
||||||
/// @param ignore ignored block id (will be counted as replaceable)
|
/// @param ignore ignored block id (will be counted as replaceable)
|
||||||
bool checkReplaceability(
|
bool checkReplaceability(
|
||||||
const Block* def,
|
const Block& def,
|
||||||
blockstate state,
|
blockstate state,
|
||||||
glm::ivec3 coord,
|
glm::ivec3 coord,
|
||||||
blockid_t ignore = 0
|
blockid_t ignore = 0
|
||||||
|
|||||||
@@ -149,9 +149,9 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
|
|||||||
voxels[vidx] = cvoxels[cidx];
|
voxels[vidx] = cvoxels[cidx];
|
||||||
light_t light = clights[cidx];
|
light_t light = clights[cidx];
|
||||||
if (backlight) {
|
if (backlight) {
|
||||||
auto block =
|
const auto& block =
|
||||||
indices->blocks.get(voxels[vidx].id); //FIXME: Potentional null pointer
|
indices->blocks.require(voxels[vidx].id);
|
||||||
if (block->lightPassing) { //-V522
|
if (block.lightPassing) { //-V522
|
||||||
light = Lightmap::combine(
|
light = Lightmap::combine(
|
||||||
min(15,
|
min(15,
|
||||||
Lightmap::extract(light, 0) + 1),
|
Lightmap::extract(light, 0) + 1),
|
||||||
|
|||||||
Reference in New Issue
Block a user