Merge branch 'main' into devtools

This commit is contained in:
MihailRis
2024-05-13 00:15:14 +03:00
25 changed files with 336 additions and 318 deletions
+47 -47
View File
@@ -24,10 +24,10 @@ const uint MIN_SURROUNDING = 9;
ChunksController::ChunksController(Level* level, uint padding)
: level(level),
chunks(level->chunks.get()),
lighting(level->lighting.get()),
padding(padding),
generator(WorldGenerators::createGenerator(level->getWorld()->getGenerator(), level->content)) {
chunks(level->chunks.get()),
lighting(level->lighting.get()),
padding(padding),
generator(WorldGenerators::createGenerator(level->getWorld()->getGenerator(), level->content)) {
}
ChunksController::~ChunksController(){
@@ -37,7 +37,7 @@ void ChunksController::update(int64_t maxDuration) {
int64_t mcstotal = 0;
for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) {
timeutil::Timer timer;
timeutil::Timer timer;
if (loadVisible()) {
int64_t mcs = timer.stop();
if (mcstotal + mcs < maxDuration * 1000) {
@@ -50,44 +50,44 @@ void ChunksController::update(int64_t maxDuration) {
}
bool ChunksController::loadVisible(){
const int w = chunks->w;
const int d = chunks->d;
const int w = chunks->w;
const int d = chunks->d;
int nearX = 0;
int nearZ = 0;
int minDistance = ((w-padding*2)/2)*((w-padding*2)/2);
for (uint z = padding; z < d-padding; z++){
for (uint x = padding; x < w-padding; x++){
int index = z * w + x;
auto chunk = chunks->chunks[index];
if (chunk != nullptr){
if (chunk->isLoaded() && !chunk->isLighted()) {
if (buildLights(chunk)) {
int nearX = 0;
int nearZ = 0;
int minDistance = ((w-padding*2)/2)*((w-padding*2)/2);
for (uint z = padding; z < d-padding; z++){
for (uint x = padding; x < w-padding; x++){
int index = z * w + x;
auto chunk = chunks->chunks[index];
if (chunk != nullptr){
if (chunk->isLoaded() && !chunk->isLighted()) {
if (buildLights(chunk)) {
return true;
}
}
continue;
}
int lx = x - w / 2;
int lz = z - d / 2;
int distance = (lx * lx + lz * lz);
if (distance < minDistance){
minDistance = distance;
nearX = x;
nearZ = z;
}
}
}
}
continue;
}
int lx = x - w / 2;
int lz = z - d / 2;
int distance = (lx * lx + lz * lz);
if (distance < minDistance){
minDistance = distance;
nearX = x;
nearZ = z;
}
}
}
auto chunk = chunks->chunks[nearZ * w + nearX];
if (chunk != nullptr) {
return false;
}
auto chunk = chunks->chunks[nearZ * w + nearX];
if (chunk != nullptr) {
return false;
}
const int ox = chunks->ox;
const int oz = chunks->oz;
createChunk(nearX+ox, nearZ+oz);
return true;
const int oz = chunks->oz;
createChunk(nearX+ox, nearZ+oz);
return true;
}
bool ChunksController::buildLights(std::shared_ptr<Chunk> chunk) {
@@ -112,22 +112,22 @@ bool ChunksController::buildLights(std::shared_ptr<Chunk> chunk) {
void ChunksController::createChunk(int x, int z) {
auto chunk = level->chunksStorage->create(x, z);
chunks->putChunk(chunk);
chunks->putChunk(chunk);
if (!chunk->isLoaded()) {
generator->generate(
if (!chunk->isLoaded()) {
generator->generate(
chunk->voxels, x, z,
level->getWorld()->getSeed()
);
chunk->setUnsaved(true);
}
chunk->updateHeights();
chunk->setUnsaved(true);
}
chunk->updateHeights();
if (!chunk->isLoadedLights()) {
Lighting::prebuildSkyLight(
if (!chunk->isLoadedLights()) {
Lighting::prebuildSkyLight(
chunk.get(), level->content->getIndices()
);
}
}
chunk->setLoaded(true);
chunk->setReady(true);
chunk->setReady(true);
}
+1 -1
View File
@@ -31,4 +31,4 @@ public:
void update(int64_t maxDuration);
};
#endif /* VOXELS_CHUNKSCONTROLLER_HPP_ */
#endif // VOXELS_CHUNKSCONTROLLER_HPP_
+4 -4
View File
@@ -110,8 +110,8 @@ static void loadWorld(Engine* engine, fs::path folder) {
auto& packs = engine->getContentPacks();
auto& settings = engine->getSettings();
Level* level = World::load(folder, settings, content, packs);
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
auto level = World::load(folder, settings, content, packs);
engine->setScreen(std::make_shared<LevelScreen>(engine, std::move(level)));
} catch (const world_load_error& error) {
guiutil::alert(
engine->getGUI(), langs::get(L"Error")+L": "+
@@ -197,13 +197,13 @@ void EngineController::createWorld(
return;
}
Level* level = World::create(
auto level = World::create(
name, generatorID, folder, seed,
engine->getSettings(),
engine->getContent(),
engine->getContentPacks()
);
engine->setScreen(std::make_shared<LevelScreen>(engine, level));
engine->setScreen(std::make_shared<LevelScreen>(engine, std::move(level)));
}
void EngineController::reopenWorld(World* world) {
+6 -6
View File
@@ -10,18 +10,18 @@
static debug::Logger logger("level-control");
LevelController::LevelController(EngineSettings& settings, Level* level)
: settings(settings), level(level),
blocks(std::make_unique<BlocksController>(level, settings.chunks.padding.get())),
chunks(std::make_unique<ChunksController>(level, settings.chunks.padding.get())),
player(std::make_unique<PlayerController>(level, settings, blocks.get())) {
LevelController::LevelController(EngineSettings& settings, std::unique_ptr<Level> level)
: settings(settings), level(std::move(level)),
blocks(std::make_unique<BlocksController>(this->level.get(), settings.chunks.padding.get())),
chunks(std::make_unique<ChunksController>(this->level.get(), settings.chunks.padding.get())),
player(std::make_unique<PlayerController>(this->level.get(), settings, blocks.get())) {
scripting::on_world_load(this);
}
void LevelController::update(float delta, bool input, bool pause) {
player->update(delta, input, pause);
glm::vec3 position = player->getPlayer()->hitbox->position;
glm::vec3 position = player->getPlayer()->hitbox->position;
level->loadMatrix(position.x, position.z,
settings.chunks.loadDistance.get() +
settings.chunks.padding.get() * 2);
+1 -1
View File
@@ -20,7 +20,7 @@ class LevelController {
std::unique_ptr<ChunksController> chunks;
std::unique_ptr<PlayerController> player;
public:
LevelController(EngineSettings& settings, Level* level);
LevelController(EngineSettings& settings, std::unique_ptr<Level> level);
/// @param delta time elapsed since the last update
/// @param input is user input allowed to be handled
+1 -2
View File
@@ -34,7 +34,6 @@ const float RUN_ZOOM = 1.1f;
const float C_ZOOM = 0.1f;
const float CROUCH_SHIFT_Y = -0.2f;
CameraControl::CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings)
: player(player),
camera(player->camera),
@@ -136,7 +135,7 @@ void CameraControl::switchCamera() {
}
}
void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) {
void CameraControl::update(const PlayerInput& input, float delta, Chunks* chunks) {
offset = glm::vec3(0.0f, 0.7f, 0.0f);
if (settings.shaking.get() && !input.cheat) {
+5 -5
View File
@@ -1,14 +1,14 @@
#ifndef PLAYER_CONTROL_HPP_
#define PLAYER_CONTROL_HPP_
#include "../settings.hpp"
#include "../objects/Player.hpp"
#include <memory>
#include <vector>
#include <functional>
#include <glm/glm.hpp>
#include "../settings.hpp"
#include "../objects/Player.hpp"
class Camera;
class Level;
class Block;
@@ -38,7 +38,7 @@ class CameraControl {
public:
CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings);
void updateMouse(PlayerInput& input);
void update(PlayerInput& input, float delta, Chunks* chunks);
void update(const PlayerInput& input, float delta, Chunks* chunks);
void refresh();
};
@@ -55,7 +55,7 @@ using on_block_interaction = std::function<void(
class PlayerController {
Level* level;
std::shared_ptr<Player> player;
PlayerInput input;
PlayerInput input {};
CameraControl camControl;
BlocksController* blocksController;
+1 -1
View File
@@ -34,7 +34,7 @@ static bool processCallback(
try {
return state->eval(*env, src, file) != 0;
} catch (lua::luaerror& err) {
std::cerr << err.what() << std::endl;
logger.error() << err.what();
return false;
}
}
@@ -8,6 +8,8 @@
#include <glm/glm.hpp>
#include <string>
#include <string>
namespace scripting {
using common_func = std::function<dynamic::Value(const std::vector<dynamic::Value>&)>;