From c3831afd194e7364332b177cf88b77e744cd488e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 19 Aug 2024 17:42:33 +0300 Subject: [PATCH] add biome 'parameters' field --- res/generators/default.lua | 4 +++ src/logic/scripting/lua/lua_util.hpp | 13 ++++++++-- .../scripting/scripting_world_generation.cpp | 26 ++++++++++++++++--- src/world/generator/GeneratorDef.hpp | 10 +++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/res/generators/default.lua b/res/generators/default.lua index 010489a0..af59f259 100644 --- a/res/generators/default.lua +++ b/res/generators/default.lua @@ -5,6 +5,10 @@ sea_level = 64 biome_parameters = 2 biome = { + parameters = { + {value=0.5, weight=1.0}, + {value=0.5, weight=1.0}, + }, sea_layers = { {block="base:water", height=-1}, }, diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index a02fdd03..11615ba1 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -566,7 +566,7 @@ namespace lua { ) { requirefield(L, name, idx); auto value = require_string(L, -1); - lua::pop(L); + pop(L); return value; } @@ -575,7 +575,16 @@ namespace lua { ) { requirefield(L, name, idx); auto value = tointeger(L, -1); - lua::pop(L); + pop(L); + return value; + } + + inline Number require_number_field( + lua::State* L, const std::string& name, int idx=-1 + ) { + requirefield(L, name, idx); + auto value = tonumber(L, -1); + pop(L); return value; } diff --git a/src/logic/scripting/scripting_world_generation.cpp b/src/logic/scripting/scripting_world_generation.cpp index 9a2535bb..612346d6 100644 --- a/src/logic/scripting/scripting_world_generation.cpp +++ b/src/logic/scripting/scripting_world_generation.cpp @@ -112,9 +112,25 @@ static inline BlocksLayers load_layers( } static inline Biome load_biome( - lua::State* L, const std::string& name, int idx + lua::State* L, const std::string& name, uint parametersCount, int idx ) { lua::pushvalue(L, idx); + + std::vector parameters(parametersCount); + lua::requirefield(L, "parameters"); + if (lua::objlen(L, -1) < parametersCount) { + throw std::runtime_error( + std::to_string(parametersCount)+" parameters expected"); + } + for (uint i = 1; i <= parametersCount; i++) { + lua::rawgeti(L, i); + float value = lua::require_number_field(L, "value"); + float weight = lua::require_number_field(L, "weight"); + parameters.push_back(BiomeParameter {value, weight}); + lua::pop(L); + } + lua::pop(L); + BlocksLayers groundLayers; BlocksLayers seaLayers; try { @@ -124,7 +140,11 @@ static inline Biome load_biome( throw std::runtime_error("biome "+name+": "+err.what()); } lua::pop(L); - return Biome {name, std::move(groundLayers), std::move(seaLayers)}; + return Biome { + name, + std::move(parameters), + std::move(groundLayers), + std::move(seaLayers)}; } std::unique_ptr scripting::load_generator( @@ -141,7 +161,7 @@ std::unique_ptr scripting::load_generator( uint biomeParameters = lua::get_integer_field(L, "biome_parameters", 0, 0, 16); uint seaLevel = lua::get_integer_field(L, "sea_level", 0, 0, CHUNK_H); lua::requirefield(L, "biome"); - Biome biome = load_biome(L, "default", -1); + Biome biome = load_biome(L, "default", biomeParameters, -1); lua::pop(L); lua::pop(L); diff --git a/src/world/generator/GeneratorDef.hpp b/src/world/generator/GeneratorDef.hpp index 79c537ed..2828ad42 100644 --- a/src/world/generator/GeneratorDef.hpp +++ b/src/world/generator/GeneratorDef.hpp @@ -33,8 +33,17 @@ struct BlocksLayers { uint lastLayersHeight; }; +struct BiomeParameter { + /// @brief Central parameter value for the biome + float origin; + /// @brief Parameter score multiplier + /// (the higher the value, the greater the chance of biome choosing) + float weight; +}; + struct Biome { std::string name; + std::vector parameters; BlocksLayers groundLayers; BlocksLayers seaLayers; }; @@ -67,6 +76,7 @@ public: /// @brief Generator information struct GeneratorDef { + /// @brief Generator full name - packid:name std::string name; std::unique_ptr script;