Skip to content

Commit

Permalink
[TerrainBiomeSampler] Now aware of dimensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 8, 2020
1 parent be1e49c commit 58ef40b
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 22 deletions.
4 changes: 3 additions & 1 deletion client/source/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ void ClientCommandHandler::setupCallbacks() {
packet >> clientId >> x >> y >> z >> dimension;

if (clientId == m_client.id()) {
DEBUG("PlayerChangeDimension received:", clientId, x, y, z, dimension);
m_player.setDimension(dimension);
m_player.setPosition(x, y, z);
m_world.clear();
}
});

Expand Down
4 changes: 4 additions & 0 deletions client/source/world/ClientWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ void ClientWorld::checkPlayerChunk(double playerX, double playerY, double player
}
}

void ClientWorld::clear() {
m_chunks.clear();
}

void ClientWorld::receiveChunkData(sf::Packet &packet) {
s32 cx, cy, cz;
packet >> cx >> cy >> cz;
Expand Down
2 changes: 2 additions & 0 deletions client/source/world/ClientWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ClientWorld : public World, public gk::Drawable {
void sendChunkRequests();
void checkPlayerChunk(double playerX, double playerY, double playerZ);

void clear();

void receiveChunkData(sf::Packet &packet);
void removeChunk(ChunkMap::iterator &it);

Expand Down
4 changes: 2 additions & 2 deletions common/source/core/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ const Tree &Registry::getTreeFromStringID(const std::string &stringID) {

const Biome &Registry::getBiomeFromStringID(const std::string &stringID) {
if (stringID.empty())
throw EXCEPTION("Trying to get tree from empty string ID.");
throw EXCEPTION("Trying to get biome from empty string ID.");

auto it = m_biomesID.find(stringID);
if (it == m_biomesID.end())
throw EXCEPTION("Unknown tree:", stringID);
throw EXCEPTION("Unknown biome:", stringID);

return getBiome(it->second);
}
Expand Down
2 changes: 2 additions & 0 deletions common/source/world/Dimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Dimension : public ISerializable {
void serialize(sf::Packet &packet) const override;
void deserialize(sf::Packet &packet) override;

const std::vector<std::string> &biomes() const { return m_biomes; }

private:
u16 m_id = 0;

Expand Down
6 changes: 4 additions & 2 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ mod:block {

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
local dim = (player:dimension() + 1) % 2
local pos = {x = 0, y = 0, z = 20}

server:send_player_change_dimension(client.id, 0, 0, 20, dim, client);
player:set_dimension(dim);
server:send_player_change_dimension(client.id, pos.x, pos.y, pos.z, dim, client)
player:set_dimension(dim)
player:set_position(pos.x, pos.y, pos.z)
end,
}

2 changes: 2 additions & 0 deletions server/source/lua/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ void ScriptEngine::initUsertypes() {
m_lua.new_usertype<Player>("Player",
"inventory", &Player::inventory,

"set_position", &Player::setPosition,

"dimension", &Player::dimension,
"set_dimension", &Player::setDimension
);
Expand Down
3 changes: 2 additions & 1 deletion server/source/world/ServerWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class ServerWorld : public World {
using ChunkMap = std::unordered_map<gk::Vector3i, std::unique_ptr<ServerChunk>>;

public:
ServerWorld(const Dimension &dimension) : m_dimension(dimension) {}
ServerWorld(const Dimension &dimension)
: m_dimension(dimension), m_terrainGenerator(dimension) {}

void update();

Expand Down
15 changes: 9 additions & 6 deletions server/source/world/TerrainBiomeSampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
*/
#include "Biome.hpp"
#include "Registry.hpp"
#include "ServerWorld.hpp"
#include "TerrainBiomeSampler.hpp"

TerrainBiomeSampler::TerrainBiomeSampler() {
TerrainBiomeSampler::TerrainBiomeSampler(const Dimension &dimension) : m_dimension(dimension) {
for (u8 i = 0; i < biomeParamCount; i++) {
m_paramNoises.emplace_back();

Expand All @@ -44,21 +45,23 @@ u16 TerrainBiomeSampler::getBiomeIndexAt(s32 x, s32 y) const {
// Should also finish solving for analytic blending, or find completely separate solution such as isotropically-modified genlayer
// If we continue with temp/precip/etc params, need to write a weighted lloyd smoother so biomes becone fairly represented.
// True temp/precip values can then be re-interpolated out from the Voronoi diagram using a neighborhood figure "kernel".
// TODO with multiple worldtypes added, need to only consider biomes in one worldtype.

u16 decidedBiomeIndex = 0;
double decidedBiomeDeviation = INFINITY;
u16 j = 0;
for (auto &biome : Registry::getInstance().biomes()) {

for (auto &biomeID : m_dimension.biomes()) {
const Biome &biome = Registry::getInstance().getBiomeFromStringID(biomeID);

double deviation = 0;
for (int i = 0; i < biomeParamCount; i++) {
double dp = m_paramNoises[i].GetNoise(x, y) - biome.getParams()[i];
deviation += dp * dp;
}

if (deviation < decidedBiomeDeviation) {
decidedBiomeDeviation = deviation;
decidedBiomeIndex = j;
decidedBiomeIndex = biome.id();
}
j++;
}

return decidedBiomeIndex;
Expand Down
13 changes: 9 additions & 4 deletions server/source/world/TerrainBiomeSampler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@

#include <gk/core/IntTypes.hpp>

#include "Biome.hpp"
#include "FastNoise.hpp"

class Dimension;

class TerrainBiomeSampler {
public:
TerrainBiomeSampler(); // TODO should eventually take a worldtype
TerrainBiomeSampler(const Dimension &dimension);

u16 getBiomeIndexAt(s32 x, s32 y) const;
//std::vector<WeightedIndex> getWeightedBiomeIndicesAt(double x, double y);

// std::vector<WeightedIndex> getWeightedBiomeIndicesAt(double x, double y);

private:
static const u8 biomeParamCount = 2; // TODO if kept, should be defined in the worldtype, dynamically.
// TODO: If kept, should be defined in the worldtype, dynamically
static const u8 biomeParamCount = 2;

const Dimension &m_dimension;

std::vector<FastNoise> m_paramNoises;
};
Expand Down
5 changes: 1 addition & 4 deletions server/source/world/TerrainGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
#include <glm/gtc/noise.hpp>
#include "FastNoise.hpp"

TerrainGenerator::TerrainGenerator() {
}

void TerrainGenerator::generate(ServerChunk &chunk) const {
fastNoiseGeneration(chunk);
}
Expand All @@ -50,7 +47,7 @@ void TerrainGenerator::fastNoiseGeneration(ServerChunk &chunk) const {
Chunk *topChunk = chunk.getSurroundingChunk(Chunk::Top);
for(int y = 0 ; y < CHUNK_DEPTH ; y++) {
for(int x = 0 ; x < CHUNK_WIDTH ; x++) {
u16 biomeIndex = biomeSampler.getBiomeIndexAt(x + chunk.x() * CHUNK_WIDTH, y + chunk.y() * CHUNK_DEPTH);
u16 biomeIndex = m_biomeSampler.getBiomeIndexAt(x + chunk.x() * CHUNK_WIDTH, y + chunk.y() * CHUNK_DEPTH);
const Biome &biome = Registry::getInstance().getBiome(biomeIndex);

// Land height
Expand Down
5 changes: 3 additions & 2 deletions server/source/world/TerrainGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@

#include "TerrainBiomeSampler.hpp"

class Dimension;
class ServerChunk;

class TerrainGenerator {
public:
TerrainGenerator();
TerrainGenerator(const Dimension &dimension) : m_biomeSampler(dimension) {}

void generate(ServerChunk &chunk) const;

Expand All @@ -48,7 +49,7 @@ class TerrainGenerator {
static float noise2d(double x, double y, int octaves, float persistence);
static float noise3d_abs(double x, double y, double z, int octaves, float persistence);

TerrainBiomeSampler biomeSampler;
TerrainBiomeSampler m_biomeSampler;
};

#endif // TERRAINGENERATOR_HPP_

0 comments on commit 58ef40b

Please sign in to comment.