载入中...
搜索中...
未找到
TerrainBiomePreset.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <type_traits>
5
6namespace eve::procgen {
7namespace {
8std::string namespacedRuleId(const std::string& entryId, const std::string& ruleId) {
9 return std::to_string(entryId.size()) + ":" + entryId + ruleId;
10}
11} // namespace
12
13Result<int> TerrainBiomePreset::addSpawner(const std::string& entryId, const TerrainSpawnPlan& plan,
14 bool activeInBiome, bool activeInStamper,
15 bool autoAssignResources) {
16 if (entryId.empty() || plan.rules().empty())
18 DiagnosticCode::InvalidArgument, "terrain.biomePreset: nonempty entry ID and spawn plan required"));
19 if (std::any_of(entries_.begin(), entries_.end(), [&](const auto& entry) { return entry.entryId == entryId; }))
21 Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.biomePreset: unique entry ID required"));
22 auto candidate = entries_;
23 candidate.push_back({entryId, plan, activeInBiome, activeInStamper, autoAssignResources});
24 entries_.swap(candidate);
25 return Result<int>::success(static_cast<int>(entries_.size()));
26}
27
28Result<void> TerrainBiomePreset::setActiveInBiome(const std::string& entryId, bool active) {
29 auto candidate = entries_;
30 auto found = std::find_if(candidate.begin(), candidate.end(), [&](const auto& entry) {
31 return entry.entryId == entryId;
32 });
33 if (found == candidate.end())
35 Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.biomePreset: entry ID not found"));
36 found->activeInBiome = active;
37 entries_.swap(candidate);
38 return Result<void>::success();
39}
40
42 auto candidate = entries_;
43 auto found = std::find_if(candidate.begin(), candidate.end(), [&](const auto& entry) {
44 return entry.entryId == entryId;
45 });
46 if (found == candidate.end())
48 Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.biomePreset: entry ID not found"));
49 found->activeInStamper = active;
50 entries_.swap(candidate);
51 return Result<void>::success();
52}
53
54Result<TerrainSpawnPlan> TerrainBiomePreset::compile(bool biomeScope) const {
55 TerrainSpawnPlan result;
56 for (const auto& entry : entries_) {
57 if (!(biomeScope ? entry.activeInBiome : entry.activeInStamper)) continue;
58 for (const auto& variant : entry.plan.rules()) {
59 auto added = std::visit([&](const auto& rule) -> Result<int> {
60 using Rule = std::decay_t<decltype(rule)>;
61 const auto id = namespacedRuleId(entry.entryId, rule.ruleId);
62 auto insertion = [&]() -> Result<int> {
63 if constexpr (std::is_same_v<Rule, TerrainSplatSpawnRule>)
64 return result.addSplat(id, rule.paint, rule.targetLayer, rule.operation);
65 else if constexpr (std::is_same_v<Rule, TerrainDetailSpawnRule>)
66 return result.addDetail(id, rule.fitness, rule.settings, rule.operation, rule.mode, rule.seed);
67 else if constexpr (std::is_same_v<Rule, TerrainTreeSpawnRule>)
68 return result.addTrees(id, rule.fitness, rule.settings, rule.operation, rule.mode);
69 else if constexpr (std::is_same_v<Rule, TerrainObjectSpawnRule>)
70 return result.addObjects(id, rule.fitness, rule.settings, rule.operation, rule.mode);
71 else if constexpr (std::is_same_v<Rule, TerrainProbeSpawnRule>)
72 return result.addProbes(id, rule.fitness, rule.settings, rule.operation, rule.mode);
73 else
74 return result.addModifierStamp(id, rule.stamp, rule.operation, rule.localMask,
75 rule.globalMask);
76 }();
77 if (insertion.ok() && !rule.enabled) {
78 auto disabled = result.setEnabled(id, false);
79 if (!disabled.ok()) return Result<int>::failure(disabled.status());
80 }
81 return insertion;
82 }, variant);
83 if (!added.ok()) return Result<TerrainSpawnPlan>::failure(added.status());
84 }
85 }
86 if (result.rules().empty())
88 Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.biomePreset: no active spawners"));
89 return Result<TerrainSpawnPlan>::success(std::move(result));
90}
91
94int TerrainBiomePreset::getSpawnerCount() const noexcept { return static_cast<int>(entries_.size()); }
95
97 auto found = std::find_if(entries_.begin(), entries_.end(), [&](const auto& entry) {
98 return entry.entryId == entryId;
99 });
100 if (found == entries_.end())
102 Diagnostic::error(DiagnosticCode::InvalidArgument, "terrain.biomePreset: entry ID not found"));
103 return Result<bool>::success(found->autoAssignResources);
104}
105
106} // namespace eve::procgen
bool & active
std::string variant
bool found
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
Result< void > setActiveInBiome(const std::string &entryId, bool active)
Change the biome-controller activation flag of one stable entry.
Result< TerrainSpawnPlan > compileForStamper() const
Compile active stamper-linked entries into one owned transactional plan.
Result< int > addSpawner(const std::string &entryId, const TerrainSpawnPlan &plan, bool activeInBiome, bool activeInStamper, bool autoAssignResources)
Append an owned nonempty spawner entry with a stable unique entry ID.
Result< void > setActiveInStamper(const std::string &entryId, bool active)
Change the stamper-linked activation flag of one stable entry.
int getSpawnerCount() const noexcept
Return the number of retained spawner entries.
Result< TerrainSpawnPlan > compileForBiome() const
Compile active biome-controller entries into one owned transactional plan.
Result< bool > getAutoAssignResources(const std::string &entryId) const
Return whether an entry requests prototype/resource assignment.
Own an ordered mixed-domain Pcg-style spawn recipe. Rule IDs are stable and unique....
const std::vector< TerrainSpawnRule > & rules() const noexcept
Return immutable ordered rules for transactional execution.
Result< int > addProbes(const std::string &ruleId, const Heightmap &fitness, const TerrainProbePlacementSettings &settings, const TerrainStampSettings &operation, TerrainProbeOperationMode mode)
Append an owned reflection or light probe rule after validating a nonempty unique rule ID.
Result< int > addSplat(const std::string &ruleId, const Heightmap &paint, int targetLayer, const TerrainStampSettings &operation)
Append an owned terrain texture rule after validating a nonempty unique rule ID.
Result< int > addTrees(const std::string &ruleId, const Heightmap &fitness, const TerrainTreePlacementSettings &settings, const TerrainStampSettings &operation, TerrainTreeOperationMode mode)
Append an owned tree rule after validating a nonempty unique rule ID.
Result< void > setEnabled(const std::string &ruleId, bool enabled)
Enable or disable a stable rule without changing its order.
Result< int > addDetail(const std::string &ruleId, const Heightmap &fitness, const TerrainDetailSettings &settings, const TerrainStampSettings &operation, TerrainDetailMode mode, std::int32_t seed)
Append an owned detail rule after validating a nonempty unique rule ID.
Result< int > addObjects(const std::string &ruleId, const Heightmap &fitness, const TerrainObjectPlacementSettings &settings, const TerrainStampSettings &operation, TerrainObjectOperationMode mode)
Append an owned object rule after validating a nonempty unique rule ID.
Result< int > addModifierStamp(const std::string &ruleId, const Heightmap &stamp, const TerrainStampSettings &operation, const Heightmap &localMask, const Heightmap &globalMask)
Append an owned TerrainModifierStamp with independent local and global mask snapshots.