refactor: change pointer parameters to references for Level and Content in various classes

This commit is contained in:
MihailRis
2024-12-25 18:53:53 +03:00
parent ef2c0b2370
commit c1b311f3c4
41 changed files with 258 additions and 251 deletions
+6 -6
View File
@@ -39,7 +39,7 @@ void BlocksController::updateSides(int x, int y, int z) {
void BlocksController::updateSides(int x, int y, int z, int w, int h, int d) {
voxel* vox = blocks_agent::get(chunks, x, y, z);
const auto& def = level.content->getIndices()->blocks.require(vox->id);
const auto& def = level.content.getIndices()->blocks.require(vox->id);
const auto& rot = def.rotations.variants[vox->state.rotation];
const auto& xaxis = rot.axisX;
const auto& yaxis = rot.axisY;
@@ -85,7 +85,7 @@ void BlocksController::placeBlock(
if (voxel == nullptr) {
return;
}
const auto& prevDef = level.content->getIndices()->blocks.require(voxel->id);
const auto& prevDef = level.content.getIndices()->blocks.require(voxel->id);
scripting::on_block_replaced(player, prevDef, {x, y, z});
onBlockInteraction(
@@ -106,7 +106,7 @@ void BlocksController::placeBlock(
void BlocksController::updateBlock(int x, int y, int z) {
voxel* vox = blocks_agent::get(chunks, x, y, z);
if (vox == nullptr) return;
const auto& def = level.content->getIndices()->blocks.require(vox->id);
const auto& def = level.content.getIndices()->blocks.require(vox->id);
if (def.grounded) {
const auto& vec = get_ground_direction(def, vox->state.rotation);
if (!blocks_agent::is_solid_at(chunks, x + vec.x, y + vec.y, z + vec.z)) {
@@ -132,7 +132,7 @@ void BlocksController::update(float delta, uint padding) {
}
void BlocksController::onBlocksTick(int tickid, int parts) {
const auto& indices = level.content->getIndices()->blocks;
const auto& indices = level.content.getIndices()->blocks;
int tickRate = blocksTickClock.getTickRate();
for (size_t id = 0; id < indices.count(); id++) {
if ((id + tickid) % parts != 0) continue;
@@ -169,7 +169,7 @@ void BlocksController::randomTick(
}
void BlocksController::randomTick(int tickid, int parts, uint padding) {
auto indices = level.content->getIndices();
auto indices = level.content.getIndices();
std::set<uint64_t> chunksIterated;
@@ -218,7 +218,7 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) {
int lz = z - chunk->z * CHUNK_D;
auto inv = chunk->getBlockInventory(lx, y, lz);
if (inv == nullptr) {
const auto& indices = level.content->getIndices()->blocks;
const auto& indices = level.content.getIndices()->blocks;
auto& def = indices.require(chunk->voxels[vox_index(lx, y, lz)].id);
int invsize = def.inventorySize;
if (invsize == 0) {
+2 -2
View File
@@ -24,7 +24,7 @@ const uint MIN_SURROUNDING = 9;
ChunksController::ChunksController(Level& level)
: level(level),
generator(std::make_unique<WorldGenerator>(
level.content->generators.require(level.getWorld()->getGenerator()),
level.content.generators.require(level.getWorld()->getGenerator()),
level.content,
level.getWorld()->getSeed()
)) {}
@@ -141,7 +141,7 @@ void ChunksController::createChunk(const Player& player, int x, int z) const {
chunk->updateHeights();
if (!chunkFlags.loadedLights) {
Lighting::prebuildSkyLight(chunk.get(), level.content->getIndices());
Lighting::prebuildSkyLight(*chunk, *level.content.getIndices());
}
chunkFlags.loaded = true;
chunkFlags.ready = true;
+3 -3
View File
@@ -152,7 +152,7 @@ static void load_world(
auto& packs = engine.getContentPacks();
auto& settings = engine.getSettings();
auto level = World::load(worldFiles, settings, content, packs);
auto level = World::load(worldFiles, settings, *content, packs);
engine.onWorldOpen(std::move(level));
} catch (const world_load_error& error) {
guiutil::alert(
@@ -164,7 +164,7 @@ static void load_world(
}
void EngineController::openWorld(const std::string& name, bool confirmConvert) {
auto& paths = engine.getPaths();
const auto& paths = engine.getPaths();
auto folder = paths.getWorldsFolder() / fs::u8path(name);
auto worldFile = folder / fs::u8path("world.json");
if (!fs::exists(worldFile)) {
@@ -245,7 +245,7 @@ void EngineController::createWorld(
folder,
seed,
engine.getSettings(),
engine.getContent(),
*engine.getContent(),
engine.getContentPacks()
);
if (!engine.isHeadless()) {
+1 -1
View File
@@ -28,7 +28,7 @@ LevelController::LevelController(
playerTickClock(20, 3) {
if (clientPlayer) {
chunks->lighting = std::make_unique<Lighting>(
level->content, clientPlayer->chunks.get()
level->content, *clientPlayer->chunks
);
}
blocks = std::make_unique<BlocksController>(
+17 -15
View File
@@ -54,30 +54,32 @@ void CameraControl::refresh() {
}
void CameraControl::updateMouse(PlayerInput& input) {
glm::vec3& cam = player.cam;
glm::vec3& rotation = player.rotation;
float sensitivity =
(input.zoom ? settings.sensitivity.get() / 4.f
: settings.sensitivity.get());
auto d = glm::degrees(Events::delta / (float)Window::height * sensitivity);
cam.x -= d.x;
cam.y -= d.y;
rotation.x -= d.x;
rotation.y -= d.y;
if (cam.y < -89.9f) {
cam.y = -89.9f;
} else if (cam.y > 89.9f) {
cam.y = 89.9f;
if (rotation.y < -89.9f) {
rotation.y = -89.9f;
} else if (rotation.y > 89.9f) {
rotation.y = 89.9f;
}
if (cam.x > 180.f) {
cam.x -= 360.f;
} else if (cam.x < -180.f) {
cam.x += 360.f;
if (rotation.x > 180.f) {
rotation.x -= 360.f;
} else if (rotation.x < -180.f) {
rotation.x += 360.f;
}
camera->rotation = glm::mat4(1.0f);
camera->rotate(
glm::radians(cam.y), glm::radians(cam.x), glm::radians(cam.z)
glm::radians(rotation.y),
glm::radians(rotation.x),
glm::radians(rotation.z)
);
}
@@ -219,7 +221,7 @@ void PlayerController::onFootstep(const Hitbox& hitbox) {
int z = std::floor(pos.z + half.z * offsetZ);
auto vox = player.chunks->get(x, y, z);
if (vox) {
auto& def = level.content->getIndices()->blocks.require(vox->id);
auto& def = level.content.getIndices()->blocks.require(vox->id);
if (!def.obstacle) {
continue;
}
@@ -340,7 +342,7 @@ static int determine_rotation(
}
voxel* PlayerController::updateSelection(float maxDistance) {
auto indices = level.content->getIndices();
auto indices = level.content.getIndices();
auto& chunks = *player.chunks;
auto camera = player.fpCamera.get();
auto& selection = player.selection;
@@ -476,7 +478,7 @@ void PlayerController::updateEntityInteraction(
}
void PlayerController::updateInteraction(float delta) {
auto indices = level.content->getIndices();
auto indices = level.content.getIndices();
auto chunks = player.chunks.get();
const auto& selection = player.selection;
+5 -5
View File
@@ -133,7 +133,7 @@ static int l_get_x(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec_stack(L, glm::ivec3(1, 0, 0));
}
const auto& def = level->content->getIndices()->blocks.require(vox->id);
const auto& def = level->content.getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec_stack(L, glm::ivec3(1, 0, 0));
} else {
@@ -150,7 +150,7 @@ static int l_get_y(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec_stack(L, glm::ivec3(0, 1, 0));
}
const auto& def = level->content->getIndices()->blocks.require(vox->id);
const auto& def = level->content.getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec_stack(L, glm::ivec3(0, 1, 0));
} else {
@@ -167,7 +167,7 @@ static int l_get_z(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec_stack(L, glm::ivec3(0, 0, 1));
}
const auto& def = level->content->getIndices()->blocks.require(vox->id);
const auto& def = level->content.getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec_stack(L, glm::ivec3(0, 0, 1));
} else {
@@ -367,7 +367,7 @@ static int l_place(lua::State* L) {
if (!blocks_agent::get(*level->chunks, x, y, z)) {
return 0;
}
const auto def = level->content->getIndices()->blocks.get(id);
const auto def = level->content.getIndices()->blocks.get(id);
if (def == nullptr) {
throw std::runtime_error(
"there is no block with index " + std::to_string(id)
@@ -389,7 +389,7 @@ static int l_destruct(lua::State* L) {
if (vox == nullptr) {
return 0;
}
auto& def = level->content->getIndices()->blocks.require(vox->id);
auto& def = level->content.getIndices()->blocks.require(vox->id);
auto player = level->players->get(playerid);
controller->getBlocksController()->breakBlock(player, def, x, y, z);
return 0;
+6 -6
View File
@@ -60,7 +60,7 @@ static int l_set_vel(lua::State* L) {
static int l_get_rot(lua::State* L) {
if (auto player = get_player(L, 1)) {
return lua::pushvec_stack(L, player->cam);
return lua::pushvec_stack(L, player->rotation);
}
return 0;
}
@@ -70,17 +70,17 @@ static int l_set_rot(lua::State* L) {
if (!player) {
return 0;
}
glm::vec3& cam = player->cam;
glm::vec3& rotation = player->rotation;
auto x = lua::tonumber(L, 2);
auto y = lua::tonumber(L, 3);
auto z = cam.z;
auto z = rotation.z;
if (lua::isnumber(L, 4)) {
z = lua::tonumber(L, 4);
}
cam.x = x;
cam.y = y;
cam.z = z;
rotation.x = x;
rotation.y = y;
rotation.z = z;
return 0;
}