载入中...
搜索中...
未找到
HexMapTerrain.cpp
浏览该文件的文档.
1
10
11#include "common/Diagnostic.h"
13#include "hexmap/HexMap.h"
15#include "hexmap/HexMetrics.h"
17#include "hexmap/HexSphereMap.h"
21#include "procgen/Procgen.h"
23
24#include <simplesquirrel/simplesquirrel.hpp>
25
26#include <string>
27
28namespace eve::procgen {
29namespace {
30
31[[nodiscard]] hexmap::HexMapGeneratorSettings planarSettings(const Params& params) {
32 hexmap::HexMapGeneratorSettings settings;
33 settings.seed = params.getSeed();
34 settings.highRiseProbability = params.getFloat("highRiseProbability", settings.highRiseProbability);
35 settings.sinkProbability = params.getFloat("sinkProbability", settings.sinkProbability);
36 settings.jitterProbability = params.getFloat("jitterProbability", settings.jitterProbability);
37 settings.chunkSizeMin = params.getInt("chunkSizeMin", settings.chunkSizeMin);
38 settings.chunkSizeMax = params.getInt("chunkSizeMax", settings.chunkSizeMax);
39 settings.landPercentage = params.getInt("landPercentage", settings.landPercentage);
40 settings.waterLevel = params.getInt("waterLevel", settings.waterLevel);
41 settings.elevationMinimum = params.getInt("elevationMinimum", settings.elevationMinimum);
42 settings.elevationMaximum = params.getInt("elevationMaximum", settings.elevationMaximum);
43 settings.mapBorderX = params.getInt("mapBorderX", settings.mapBorderX);
44 settings.mapBorderZ = params.getInt("mapBorderZ", settings.mapBorderZ);
45 settings.regionBorder = params.getInt("regionBorder", settings.regionBorder);
46 settings.regionCount = params.getInt("regionCount", settings.regionCount);
47 settings.erosionPercentage = params.getInt("erosionPercentage", settings.erosionPercentage);
48 settings.startingMoisture = params.getFloat("startingMoisture", settings.startingMoisture);
49 settings.evaporationFactor = params.getFloat("evaporationFactor", settings.evaporationFactor);
50 settings.precipitationFactor = params.getFloat("precipitationFactor", settings.precipitationFactor);
51 settings.runoffFactor = params.getFloat("runoffFactor", settings.runoffFactor);
52 settings.seepageFactor = params.getFloat("seepageFactor", settings.seepageFactor);
53 settings.windStrength = params.getFloat("windStrength", settings.windStrength);
54 settings.riverPercentage = params.getInt("riverPercentage", settings.riverPercentage);
55 settings.extraLakeProbability = params.getFloat("extraLakeProbability", settings.extraLakeProbability);
56 settings.lowTemperature = params.getFloat("lowTemperature", settings.lowTemperature);
57 settings.highTemperature = params.getFloat("highTemperature", settings.highTemperature);
58 settings.temperatureJitter = params.getFloat("temperatureJitter", settings.temperatureJitter);
59 const int wind = params.getInt("windDirection", static_cast<int>(settings.windDirection));
60 if (wind >= 0 && wind < hexmap::kHexDirectionCount)
61 settings.windDirection = static_cast<hexmap::HexDirection>(wind);
62 return settings;
63}
64
65[[nodiscard]] hexmap::HexSphereGeneratorSettings sphereSettings(const Params& params) {
66 hexmap::HexSphereGeneratorSettings settings;
67 settings.seed = params.getSeed();
68 settings.landPercentage = params.getInt("landPercentage", settings.landPercentage);
69 settings.waterLevel = params.getInt("waterLevel", settings.waterLevel);
70 settings.octaves = params.getInt("octaves", settings.octaves);
71 settings.frequency = params.getFloat("frequency", settings.frequency);
72 return settings;
73}
74
75bool refuseBakeGrid(const char* name, const char* entry, std::string& error) {
76 error = std::string(name) + " produces a hexmap bake; call procgen." + entry;
77 return false;
78}
79
80[[nodiscard]] ssq::Table projectBake(HSQUIRRELVM vm, ScriptProcgenParams* params,
81 Result<Value> (*generate)(const Params&), const char* entry) {
82 if (params == nullptr)
85 std::string(entry) + " params proxy must not be null",
86 "params", {}, "procgen.squirrel"))
87 .status());
88 auto view = Procgen::resolve(params->reference);
89 if (!view.isBound())
92 std::string(entry) + " parameters handle is stale", "params",
93 {}, "procgen.squirrel"))
94 .status());
95 return script::projectResult(vm, generate(*view), [](Value value) { return value; });
96}
97
98} // namespace
99
102 terrain.id = "hex.terrain";
103 terrain.displayName = "Hex Terrain";
104 terrain.category = "Hex Map";
105 terrain.params.push_back(ParamDescriptor::integer("seed", "Seed", 1, 0, 2147483647));
106 terrain.params.push_back(ParamDescriptor::integer("width", "Width", 20, hexmap::HexMetrics::kChunkSizeX,
108 terrain.params.push_back(ParamDescriptor::integer("height", "Height", 15, hexmap::HexMetrics::kChunkSizeZ,
110 terrain.params.push_back(ParamDescriptor::integer("landPercentage", "Land Percentage", 50, 1, 99));
111 terrain.params.push_back(ParamDescriptor::integer("waterLevel", "Water Level", 3, 0, 8));
112 terrain.params.push_back(ParamDescriptor::integer("riverPercentage", "River Percentage", 10, 0, 50));
113 terrain.params.push_back(ParamDescriptor::integer("regionCount", "Region Count", 1, 1, 4));
114 terrain.params.push_back(ParamDescriptor::integer("mapBorderX", "Map Border X", 5, 0, 64));
115 terrain.params.push_back(ParamDescriptor::integer("mapBorderZ", "Map Border Z", 5, 0, 64));
116 terrain.params.push_back(ParamDescriptor::integer("regionBorder", "Region Border", 5, 0, 64));
117 terrain.params.push_back(ParamDescriptor::integer("erosionPercentage", "Erosion Percentage", 50, 0, 100));
118 terrain.params.push_back(
119 ParamDescriptor::floating("highRiseProbability", "High Rise Probability", 0.25f, 0.f, 1.f, 0.01f));
120 terrain.params.push_back(ParamDescriptor::floating("sinkProbability", "Sink Probability", 0.2f, 0.f, 1.f, 0.01f));
121 terrain.params.push_back(
122 ParamDescriptor::floating("jitterProbability", "Jitter Probability", 0.25f, 0.f, 1.f, 0.01f));
123 terrain.params.push_back(ParamDescriptor::integer("windDirection", "Wind Direction", 5, 0, 5));
124 terrain.params.push_back(ParamDescriptor::floating("windStrength", "Wind Strength", 4.f, 0.f, 16.f, 0.1f));
125 registry.registerAlgorithm(std::move(terrain), [](const Params&, Grid2D&, std::string& error) {
126 return refuseBakeGrid("hex.terrain", "generateHexTerrain", error);
127 });
128
129 RecipeDescriptor sphere;
130 sphere.id = "hex.sphere";
131 sphere.displayName = "Hex Sphere";
132 sphere.category = "Hex Map";
133 sphere.params.push_back(ParamDescriptor::integer("seed", "Seed", 1, 0, 2147483647));
134 sphere.params.push_back(
135 ParamDescriptor::integer("subdivision", "Subdivision", 4, 0, hexmap::kMaxHexSphereSubdivision));
136 sphere.params.push_back(ParamDescriptor::floating("radius", "Radius", 100.f, 1.f, 10000.f, 1.f));
137 sphere.params.push_back(ParamDescriptor::integer("landPercentage", "Land Percentage", 45, 1, 99));
138 sphere.params.push_back(ParamDescriptor::integer("waterLevel", "Water Level", 0, -4, 8));
139 sphere.params.push_back(ParamDescriptor::integer("octaves", "Octaves", 5, 1, 16));
140 sphere.params.push_back(ParamDescriptor::floating("frequency", "Frequency", 1.55f, 0.1f, 16.f, 0.05f));
141 registry.registerAlgorithm(std::move(sphere), [](const Params&, Grid2D&, std::string& error) {
142 return refuseBakeGrid("hex.sphere", "generateHexSphere", error);
143 });
144}
145
147 hexmap::HexMap map;
148 auto reset = map.reset(params.getWidth(), params.getHeight(), params.getSeed());
149 if (!reset) return Result<Value>::failure(reset.status());
150 auto generated = hexmap::generateHexMap(map, planarSettings(params));
151 if (!generated) return Result<Value>::failure(generated.status());
153}
154
156 const std::int32_t subdivision = params.getInt("subdivision", 4);
157 const float radius = params.getFloat("radius", 100.f);
159 auto reset = map.reset(subdivision, radius, params.getSeed());
160 if (!reset) return Result<Value>::failure(reset.status());
161 auto generated = hexmap::generateSphereMap(map, sphereSettings(params));
162 if (!generated) return Result<Value>::failure(generated.status());
164}
165
166void exposeHexTerrain(ssq::Class& cls) {
167 cls.addFunc("generateHexTerrain", [vm = cls.getHandle()](Procgen*, ScriptProcgenParams* params) -> ssq::Table {
168 return projectBake(vm, params, generateHexTerrain, "generateHexTerrain");
169 });
170 cls.addFunc("generateHexSphere", [vm = cls.getHandle()](Procgen*, ScriptProcgenParams* params) -> ssq::Table {
171 return projectBake(vm, params, generateHexSphere, "generateHexSphere");
172 });
173}
174
175} // namespace eve::procgen
double value
std::string terrain
struct SQVM * HSQUIRRELVM
Stable, structured diagnostics shared by engine modules.
HSQUIRRELVM vm
Definition ECS.cpp:20
HSQOBJECT cls
Definition ECS.cpp:21
wgpu::PopErrorScopeStatus status
Deterministic procedural map generation for the hex grid.
Editable hex cell grid: topology, queries, picking and authoring.
Pointy-top hex metrics, directions and vertex helpers.
Procedural terrain generator for a spherical hex map.
Editable spherical hex map over an icosahedral hex topology.
Icosahedral hex topology: twelve pentagons and hexagons on a sphere.
Terrain bake hexmap applies; procgen produces the payload.
Optional hexmap terrain bake bindings for Procgen.
std::string name
std::string error
Definition Package.cpp:60
float radius
glm::mat4 view
The single Squirrel projection for common Result, Status and Value.
TerrainThermalSettings settings
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
An editable, chunked, pointy-top hex map.
Definition HexMap.h:72
Result< void > reset(std::int32_t cellCountX, std::int32_t cellCountZ, std::uint32_t seed)
Replaces the grid with a new cellCountX * cellCountZ map.
Definition HexMap.cpp:37
static constexpr int kChunkSizeZ
Chunk size in the Z dimension.
Definition HexMetrics.h:158
static constexpr int kChunkSizeX
Chunk size in the X dimension.
Definition HexMetrics.h:156
An editable hex map wrapped onto a sphere.
Result< void > reset(std::int32_t subdivision, float radius, std::uint32_t seed)
Replaces the map with a new sphere of 10 * 4^subdivision + 2 cells.
Registry for executable generators and their reflection metadata.
void registerAlgorithm(const std::string &id, GeneratorFn fn)
Register an algorithm without metadata for backward compatibility.
Intermediate 2D generation result. cells store semantic ids (see Semantic.h), not tile GIDs — convert...
Definition Grid2D.h:36
Owning, typed generation parameters.
Definition Params.h:27
Procedural generation module. Phase A: runtime maps (semantic Grid2D → TileLayer)....
Definition Procgen.h:146
static eve::script::Borrowed< Params > resolve(ProcgenParamsHandleRef reference) noexcept
Resolves parameters as a non-owning observation.
Definition Procgen.cpp:403
std::vector< ParamSpec > params
HexTerrainBake snapshotHexSphereTerrain(const HexSphereMap &map)
Snapshot every cell of a spherical map into a bake.
Result< void > generateSphereMap(HexSphereMap &map, const HexSphereGeneratorSettings &settings)
Regenerates every cell of map procedurally.
constexpr std::int32_t kMaxHexGridDimension
Largest grid edge, in cells, any entry point may request.
Definition HexMap.h:47
HexTerrainBake snapshotHexTerrain(const HexMap &map)
Snapshot every cell of a planar map into a bake.
constexpr std::int32_t kHexDirectionCount
Number of hex edges / facing directions.
Definition HexMetrics.h:62
constexpr std::int32_t kMaxHexSphereSubdivision
Largest supported subdivision level; 10 * 4^7 + 2 = 163842 cells.
HexDirection
Hex facing directions, counter-clockwise from north-east.
Definition HexMetrics.h:59
Value hexTerrainBakeToValue(const HexTerrainBake &bake)
Project a bake into the script Result payload.
Result< void > generateHexMap(HexMap &map, const HexMapGeneratorSettings &settings)
Replaces the grid's contents with a procedurally generated map.
Result< Grid2D > generate(std::string_view operation, const GenerateSettings &settings)
Generate.
void registerHexTerrainAlgorithms(GeneratorRegistry &registry)
Register hex.terrain / hex.sphere schemas when hexmap is present.
Result< Value > generateHexTerrain(const Params &params)
Bake a planar hex map into the script Value hexmap applies.
Result< Value > generateHexSphere(const Params &params)
Bake a spherical hex map into the script Value hexmap applies.
void exposeHexTerrain(ssq::Class &cls)
Expose generateHexTerrain / generateHexSphere on the Procgen class.
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
static ParamDescriptor floating(std::string key, std::string label, float defaultValue, float minimum, float maximum, float step)
Construct a bounded floating-point descriptor.
static ParamDescriptor integer(std::string key, std::string label, int defaultValue, int minimum, int maximum, int step=1)
Construct a bounded integer descriptor.
Complete metadata and parameter schema shared by every procgen recipe family.
Definition ParamSchema.h:52
std::vector< ParamDescriptor > params
Definition ParamSchema.h:56
Internal Squirrel proxy for a module-owned Params handle.