replace argument 'seed' with global constant 'SEED'

This commit is contained in:
MihailRis
2024-10-16 08:46:32 +03:00
parent 0ad59fadd9
commit faca68e6c3
5 changed files with 85 additions and 74 deletions
@@ -24,10 +24,13 @@ static debug::Logger logger("generator-scripting");
class LuaGeneratorScript : public GeneratorScript {
State* L;
const GeneratorDef& def;
scriptenv env;
scriptenv env = nullptr;
fs::path file;
std::string dirPath;
public:
LuaGeneratorScript(State* L, const GeneratorDef& def, scriptenv env)
: L(L), def(def), env(std::move(env)) {
LuaGeneratorScript(State* L, const GeneratorDef& def, const fs::path& file, const std::string& dirPath)
: L(L), def(def), file(file), dirPath(dirPath) {
}
virtual ~LuaGeneratorScript() {
@@ -37,10 +40,33 @@ public:
}
}
void initialize(uint64_t seed) override {
env = create_environment(L);
stackguard _(L);
pushenv(L, *env);
pushstring(L, dirPath);
setfield(L, "__DIR__");
pushstring(L, dirPath + "/script.lua");
setfield(L, "__FILE__");
pushinteger(L, seed);
setfield(L, "SEED");
pop(L);
if (fs::exists(file)) {
std::string src = files::read_string(file);
logger.info() << "script (generator) " << file.u8string();
pop(L, execute(L, *env, src, file.u8string()));
} else {
// Use default (empty) script
pop(L, execute(L, *env, "", "<empty>"));
}
}
std::shared_ptr<Heightmap> generateHeightmap(
const glm::ivec2& offset,
const glm::ivec2& size,
uint64_t seed,
uint bpd,
const std::vector<std::shared_ptr<Heightmap>>& inputs
) override {
@@ -48,7 +74,6 @@ public:
if (getfield(L, "generate_heightmap")) {
pushivec_stack(L, offset);
pushivec_stack(L, size);
pushinteger(L, seed);
pushinteger(L, bpd);
if (!inputs.empty()) {
size_t inputsNum = def.heightmapInputs.size();
@@ -58,7 +83,7 @@ public:
rawseti(L, i+1);
}
}
if (call_nothrow(L, 6 + (!inputs.empty()))) {
if (call_nothrow(L, 5 + (!inputs.empty()))) {
auto map = touserdata<LuaHeightmap>(L, -1)->getHeightmap();
pop(L, 2);
return map;
@@ -69,7 +94,7 @@ public:
}
std::vector<std::shared_ptr<Heightmap>> generateParameterMaps(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd
const glm::ivec2& offset, const glm::ivec2& size, uint bpd
) override {
std::vector<std::shared_ptr<Heightmap>> maps;
@@ -78,9 +103,8 @@ public:
if (getfield(L, "generate_biome_parameters")) {
pushivec_stack(L, offset);
pushivec_stack(L, size);
pushinteger(L, seed);
pushinteger(L, bpd);
if (call_nothrow(L, 6, biomeParameters)) {
if (call_nothrow(L, 5, biomeParameters)) {
for (int i = biomeParameters-1; i >= 0; i--) {
maps.push_back(
touserdata<LuaHeightmap>(L, -1-i)->getHeightmap());
@@ -166,7 +190,6 @@ public:
std::vector<Placement> placeStructuresWide(
const glm::ivec2& offset,
const glm::ivec2& size,
uint64_t seed,
uint chunkHeight
) override {
std::vector<Placement> placements {};
@@ -176,9 +199,8 @@ public:
if (getfield(L, "place_structures_wide")) {
pushivec_stack(L, offset);
pushivec_stack(L, size);
pushinteger(L, seed);
pushinteger(L, chunkHeight);
if (call_nothrow(L, 6, 1)) {
if (call_nothrow(L, 5, 1)) {
int len = objlen(L, -1);
for (int i = 1; i <= len; i++) {
rawgeti(L, i);
@@ -194,8 +216,10 @@ public:
}
std::vector<Placement> placeStructures(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed,
const std::shared_ptr<Heightmap>& heightmap, uint chunkHeight
const glm::ivec2& offset,
const glm::ivec2& size,
const std::shared_ptr<Heightmap>& heightmap,
uint chunkHeight
) override {
std::vector<Placement> placements {};
@@ -204,10 +228,9 @@ public:
if (getfield(L, "place_structures")) {
pushivec_stack(L, offset);
pushivec_stack(L, size);
pushinteger(L, seed);
newuserdata<LuaHeightmap>(L, heightmap);
pushinteger(L, chunkHeight);
if (call_nothrow(L, 7, 1)) {
if (call_nothrow(L, 6, 1)) {
int len = objlen(L, -1);
for (int i = 1; i <= len; i++) {
rawgeti(L, i);
@@ -224,28 +247,11 @@ public:
};
std::unique_ptr<GeneratorScript> scripting::load_generator(
const GeneratorDef& def, const fs::path& file, const std::string& dirPath
const GeneratorDef& def,
const fs::path& file,
const std::string& dirPath
) {
auto L = create_state(*engine->getPaths(), StateType::GENERATOR);
auto env = create_environment(L);
stackguard _(L);
pushenv(L, *env);
pushstring(L, dirPath);
setfield(L, "__DIR__");
pushstring(L, dirPath + "/script.lua");
setfield(L, "__FILE__");
pop(L);
if (fs::exists(file)) {
std::string src = files::read_string(file);
logger.info() << "script (generator) " << file.u8string();
pop(L, execute(L, *env, src, file.u8string()));
} else {
// Use default (empty) script
pop(L, execute(L, *env, "", "<empty>"));
}
return std::make_unique<LuaGeneratorScript>(L, def, std::move(env));
return std::make_unique<LuaGeneratorScript>(L, def, file, dirPath);
}
+9 -11
View File
@@ -123,17 +123,17 @@ class GeneratorScript {
public:
virtual ~GeneratorScript() = default;
virtual void initialize(uint64_t seed) = 0;
/// @brief Generate a heightmap with values in range 0..1
/// @param offset position of the heightmap in the world
/// @param size size of the heightmap
/// @param seed world seed
/// @param bpd blocks per dot
/// @param inputs biome parameter maps passed to generate_heightmap
/// @return generated heightmap (can't be nullptr)
virtual std::shared_ptr<Heightmap> generateHeightmap(
const glm::ivec2& offset,
const glm::ivec2& size,
uint64_t seed,
uint bpd,
const std::vector<std::shared_ptr<Heightmap>>& inputs
) = 0;
@@ -141,13 +141,11 @@ public:
/// @brief Generate a biomes parameters maps
/// @param offset position of maps in the world
/// @param size maps size
/// @param seed world seed
/// @param bpd blocks per dot
/// @return generated maps (can't be nullptr)
virtual std::vector<std::shared_ptr<Heightmap>> generateParameterMaps(
const glm::ivec2& offset,
const glm::ivec2& size,
uint64_t seed,
uint bpd
) = 0;
@@ -156,12 +154,10 @@ public:
/// wide-structs-chunks-radius
/// @param offset position of the area
/// @param size size of the area (blocks)
/// @param seed world seed
/// @param chunkHeight chunk height to use as heights multiplier
virtual std::vector<Placement> placeStructuresWide(
const glm::ivec2& offset,
const glm::ivec2& size,
uint64_t seed,
const glm::ivec2& offset,
const glm::ivec2& size,
uint chunkHeight
) = 0;
@@ -169,13 +165,15 @@ public:
/// placed to nearest chunks also (position of out area).
/// @param offset position of the area
/// @param size size of the area (blocks)
/// @param seed world seed
/// @param heightmap area heightmap
/// @param chunkHeight chunk height to use as heights multiplier
/// @return structure & line placements
virtual std::vector<Placement> placeStructures(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed,
const std::shared_ptr<Heightmap>& heightmap, uint chunkHeight) = 0;
const glm::ivec2& offset,
const glm::ivec2& size,
const std::shared_ptr<Heightmap>& heightmap,
uint chunkHeight
) = 0;
};
/// @brief Structure voxel fragments and metadata
+4 -4
View File
@@ -31,6 +31,8 @@ WorldGenerator::WorldGenerator(
seed(seed),
surroundMap(0, BASIC_PROTOTYPE_LAYERS + def.wideStructsChunksRadius * 2)
{
def.script->initialize(seed);
uint levels = BASIC_PROTOTYPE_LAYERS + def.wideStructsChunksRadius * 2;
surroundMap = SurroundMap(0, levels);
@@ -228,7 +230,7 @@ void WorldGenerator::generateStructuresWide(
return;
}
auto placements = def.script->placeStructuresWide(
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed, CHUNK_H
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, CHUNK_H
);
placeStructures(placements, prototype, chunkX, chunkZ);
@@ -245,7 +247,7 @@ void WorldGenerator::generateStructures(
const auto& heightmap = prototype.heightmap;
auto placements = def.script->placeStructures(
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed,
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D},
heightmap, CHUNK_H
);
placeStructures(placements, prototype, chunkX, chunkZ);
@@ -297,7 +299,6 @@ void WorldGenerator::generateBiomes(
auto biomeParams = def.script->generateParameterMaps(
{floordiv(chunkX * CHUNK_W, bpd), floordiv(chunkZ * CHUNK_D, bpd)},
{floordiv(CHUNK_W, bpd)+1, floordiv(CHUNK_D, bpd)+1},
seed,
bpd
);
for (auto index : def.heightmapInputs) {
@@ -339,7 +340,6 @@ void WorldGenerator::generateHeightmap(
prototype.heightmap = def.script->generateHeightmap(
{floordiv(chunkX * CHUNK_W, bpd), floordiv(chunkZ * CHUNK_D, bpd)},
{floordiv(CHUNK_W, bpd)+1, floordiv(CHUNK_D, bpd)+1},
seed,
bpd,
prototype.heightmapInputs
);