add 'player-spawn-radius' generator parameter

This commit is contained in:
MihailRis 2026-03-19 19:03:22 +03:00
parent 4271f537a3
commit 2600dd7ed9
5 changed files with 27 additions and 9 deletions

View file

@ -16,6 +16,8 @@
#include "voxels/Chunks.hpp"
#include "window/Camera.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "world/generator/GeneratorDef.hpp"
#include "data/dv_util.hpp"
#include "debug/Logger.hpp"
@ -50,6 +52,7 @@ Player::Player(
fpCamera->setFov(glm::radians(90.0f));
spCamera->setFov(glm::radians(90.0f));
tpCamera->setFov(glm::radians(90.0f));
random.setSeed((id << 8) ^ 34076213);
}
Player::~Player() = default;
@ -106,7 +109,7 @@ void Player::postUpdate() {
flight = false;
}
for (int i = 0; i < SPAWN_ATTEMPTS_PER_UPDATE && std::isnan(spawnpoint.x); i++) {
attemptToFindSpawnpoint();
attemptToChooseSpawnpoint();
}
}
@ -120,12 +123,17 @@ void Player::teleport(glm::vec3 position) {
}
}
void Player::attemptToFindSpawnpoint() {
glm::vec3 newpos(
position.x + (rand() % 200 - 100),
rand() % 80 + 100,
position.z + (rand() % 200 - 100)
);
void Player::attemptToChooseSpawnpoint() {
// looks bad to be here tbh
const auto& generatorDef =
level.content.generators.require(level.getWorld()->getGenerator());
glm::vec3 newpos {0.0f, random.randFloat() * 100 + 100, 0.0f};
double angle = random.randDouble() * glm::two_pi<double>();
double radius = glm::sqrt(random.randDouble());
newpos.x += glm::cos(angle) * generatorDef.playerSpawnRadius * radius;
newpos.z += glm::sin(angle) * generatorDef.playerSpawnRadius * radius;
while (newpos.y > 0 &&
!chunks->isObstacleBlock(newpos.x, newpos.y - 2, newpos.z)) {
newpos.y--;

View file

@ -7,6 +7,7 @@
#include "settings.hpp"
#include "voxels/voxel.hpp"
#include "util/Interpolation.hpp"
#include "maths/util.hpp"
class Chunks;
class Camera;
@ -63,6 +64,9 @@ class Player : public Serializable {
entityid_t selectedEid = 0;
glm::vec3 rotation {};
util::PseudoRandom random;
void attemptToChooseSpawnpoint();
public:
util::VecInterpolation<3, float, true> rotationInterpolation {true};
@ -88,8 +92,6 @@ public:
void updateSelectedEntity();
void postUpdate();
void attemptToFindSpawnpoint();
void setChosenSlot(int index);
int getChosenSlot() const;