Skip to content

Commit 4adf3e2

Browse files
authored
Merge pull request #819 from MihailRis/new-generator-parameters
add new generator parameters
2 parents 4271f53 + 4d56eb8 commit 4adf3e2

7 files changed

Lines changed: 39 additions & 9 deletions

File tree

doc/en/world-generator.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ The main properties described in the configuration file:
5454
- **heights-bpd** - number of blocks per point of the height map. Default: 4.
5555
- **wide-structs-chunks-radius** - maximum radius for placing 'wide' structures, measured in chunks.
5656
- **heightmap-inputs** - an array of parameter map numbers that will be passed by the inputs table to the height map generation function.
57+
- **player-spawn-radius** - radius of the player spawn zone.
58+
- **player-min-spawn-height**, **player-max-spawn-height** - specifying the **preferred** vertical spawn zone.
59+
Specifying a minimum height allows you to minimize the chance of spawning in underground voids, just as specifying a maximum allows you to minimize the chance of spawning on suspiciously dense cloud.
5760

5861
## Global variables
5962

doc/ru/world-generator.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
- **heights-bpd** - количество блоков на точку карты высот. По-умолчанию: 4.
5555
- **wide-structs-chunks-radius** - масимальный радиус размещения 'широких' структур, измеряемый в чанках.
5656
- **heightmap-inputs** - массив номеров карт параметров, которые будут переданы таблицей inputs в функцию генерации карты высот.
57+
- **player-spawn-radius** - радиус зоны спавна игроков.
58+
- **player-min-spawn-height**, **player-max-spawn-height** - указание **предпочтительной** вертикальной зоны спавна.
59+
Указание минимальной высоты позволяет минимизировать шанс спавна в подземных пустотах, как и указание максимальной - шанс спавна на подозрительно плотном облаке.
5760

5861
## Глобальные переменные
5962

src/constants.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ inline constexpr float CHUNKS_MAP_MAX_LOAD_FACTOR = 0.1f;
4646
/// @brief chunk volume (count of voxels per Chunk)
4747
inline constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D);
4848

49+
/// @brief default player spawn radius (see GeneratorDef::playerSpawnRadius)
50+
inline constexpr float DEFAULT_PLAYER_SPAWN_RADIUS = 100.0f;
51+
4952
/// @brief block id used to mark non-existing voxel (voxel of missing chunk)
5053
inline constexpr blockid_t BLOCK_VOID = std::numeric_limits<blockid_t>::max();
5154
/// @brief item id used to mark non-existing item (error)

src/content/loading/GeneratorLoader.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../ContentPack.hpp"
77

88
#include "io/io.hpp"
9+
#include "data/dv_util.hpp"
910
#include "engine/EnginePaths.hpp"
1011
#include "logic/scripting/scripting.hpp"
1112
#include "util/stringutil.hpp"
@@ -217,6 +218,9 @@ void ContentLoader::loadGenerator(
217218

218219
map.at("sea-level").get(def.seaLevel);
219220
map.at("wide-structs-chunks-radius").get(def.wideStructsChunksRadius);
221+
map.at("player-spawn-radius").get(def.playerSpawnRadius);
222+
map.at("player-min-spawn-height").get(def.playerMinSpawnHeight);
223+
map.at("player-max-spawn-height").get(def.playerMaxSpawnHeight);
220224
if (map.has("heightmap-inputs")) {
221225
for (const auto& element : map["heightmap-inputs"]) {
222226
int index = element.asInteger();

src/objects/Player.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "voxels/Chunks.hpp"
1717
#include "window/Camera.hpp"
1818
#include "world/Level.hpp"
19+
#include "world/World.hpp"
20+
#include "world/generator/GeneratorDef.hpp"
1921
#include "data/dv_util.hpp"
2022
#include "debug/Logger.hpp"
2123

@@ -50,6 +52,7 @@ Player::Player(
5052
fpCamera->setFov(glm::radians(90.0f));
5153
spCamera->setFov(glm::radians(90.0f));
5254
tpCamera->setFov(glm::radians(90.0f));
55+
random.setSeed((id << 8) ^ 34076213);
5356
}
5457

5558
Player::~Player() = default;
@@ -106,7 +109,7 @@ void Player::postUpdate() {
106109
flight = false;
107110
}
108111
for (int i = 0; i < SPAWN_ATTEMPTS_PER_UPDATE && std::isnan(spawnpoint.x); i++) {
109-
attemptToFindSpawnpoint();
112+
attemptToChooseSpawnpoint();
110113
}
111114
}
112115

@@ -120,12 +123,19 @@ void Player::teleport(glm::vec3 position) {
120123
}
121124
}
122125

123-
void Player::attemptToFindSpawnpoint() {
124-
glm::vec3 newpos(
125-
position.x + (rand() % 200 - 100),
126-
rand() % 80 + 100,
127-
position.z + (rand() % 200 - 100)
128-
);
126+
void Player::attemptToChooseSpawnpoint() {
127+
// looks bad to be here tbh
128+
const auto& generatorDef =
129+
level.content.generators.require(level.getWorld()->getGenerator());
130+
131+
int minHeight = generatorDef.playerMinSpawnHeight;
132+
int maxHeight = generatorDef.playerMaxSpawnHeight;
133+
glm::vec3 newpos {0.0f, random.randFloat() * (maxHeight - minHeight + 1) + minHeight, 0.0f};
134+
double angle = random.randDouble() * glm::two_pi<double>();
135+
double radius = glm::sqrt(random.randDouble());
136+
newpos.x += glm::cos(angle) * generatorDef.playerSpawnRadius * radius;
137+
newpos.z += glm::sin(angle) * generatorDef.playerSpawnRadius * radius;
138+
129139
while (newpos.y > 0 &&
130140
!chunks->isObstacleBlock(newpos.x, newpos.y - 2, newpos.z)) {
131141
newpos.y--;

src/objects/Player.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "settings.hpp"
88
#include "voxels/voxel.hpp"
99
#include "util/Interpolation.hpp"
10+
#include "maths/util.hpp"
1011

1112
class Chunks;
1213
class Camera;
@@ -63,6 +64,9 @@ class Player : public Serializable {
6364
entityid_t selectedEid = 0;
6465

6566
glm::vec3 rotation {};
67+
util::PseudoRandom random;
68+
69+
void attemptToChooseSpawnpoint();
6670
public:
6771
util::VecInterpolation<3, float, true> rotationInterpolation {true};
6872

@@ -88,8 +92,6 @@ class Player : public Serializable {
8892
void updateSelectedEntity();
8993
void postUpdate();
9094

91-
void attemptToFindSpawnpoint();
92-
9395
void setChosenSlot(int index);
9496

9597
int getChosenSlot() const;

src/world/generator/GeneratorDef.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <unordered_map>
88

99
#include "typedefs.hpp"
10+
#include "constants.hpp"
1011
#include "maths/Heightmap.hpp"
1112
#include "StructurePlacement.hpp"
1213

@@ -212,6 +213,10 @@ struct GeneratorDef {
212213
/// @brief Heightmap blocks per dot
213214
uint heightsBPD = 4;
214215

216+
float playerSpawnRadius = DEFAULT_PLAYER_SPAWN_RADIUS;
217+
int playerMinSpawnHeight = CHUNK_H / 4;
218+
int playerMaxSpawnHeight = CHUNK_H;
219+
215220
/// @brief Biome parameter maps interpolation method
216221
InterpolationType biomesInterpolation = InterpolationType::LINEAR;
217222

0 commit comments

Comments
 (0)