载入中...
搜索中...
未找到
Organism.cpp
浏览该文件的文档.
1#include "growth/Organism.h"
2
3#include "common/Diagnostic.h"
5
6#include <algorithm>
7#include <cmath>
8
9namespace eve::growth {
10namespace {
11
12double clampPermille(double value) {
13 if (!std::isfinite(value)) return 0.0;
14 return std::clamp(value, 0.0, 1000.0);
15}
16
17double meterOrZero(const Organism::VitalNeeds& needs, const std::string& id) {
18 auto it = needs.meters.find(id);
19 if (it == needs.meters.end()) return 0.0;
20 return it->second;
21}
22
23} // namespace
24
25Result<Organism*> Organism::createOrganism(const std::string& speciesId) {
27 if (!species) {
29 Diagnostic::error(DiagnosticCode::NotFound, "unknown species", "growth.organism.speciesId"));
30 }
31 Organism* organism = Organism::create();
32 auto& growth = *organism->growth();
33 growth.speciesId = species->id;
34 growth.kindId = species->kindId;
35 growth.kind = species->kind;
36 growth.stageIndex = 0;
37 growth.stageProgressDays = 0.0;
38 growth.mature = false;
39 growth.dead = false;
40 growth.harvested = false;
41
42 auto& needs = *organism->vitalNeeds();
43 needs.meters.clear();
44 for (const NeedMeterDef& need : species->needs) needs.meters[need.id] = 1000.0;
45
46 (void)*organism->health();
47 (void)*organism->reproduction();
48 (void)*organism->produce();
49 return Result<Organism*>::success(organism);
50}
51
52const std::vector<Organism*>& Organism::liveOrganisms() {
53 static std::vector<Organism*> resolved;
54 resolved.clear();
55 auto view = ecs::View<Organism, Organism::Growth>();
56 for (auto it = view.begin(); it != view.end(); ++it) resolved.push_back(it.entity());
57 return resolved;
58}
59
60const std::string& Organism::speciesId() { return growth()->speciesId; }
61
62const std::string& Organism::kindId() { return growth()->kindId; }
63
64OrganismKind Organism::kind() { return growth()->kind; }
65
66int Organism::stageIndex() { return growth()->stageIndex; }
67
68std::string Organism::stageId() {
69 const SpeciesDef* species = SpeciesRegistry::find(growth()->speciesId);
70 if (!species) return {};
71 const int index = growth()->stageIndex;
72 if (index < 0 || index >= static_cast<int>(species->stages.size())) return {};
73 return species->stages[static_cast<std::size_t>(index)].id;
74}
75
76double Organism::stageProgressDays() { return growth()->stageProgressDays; }
77
78bool Organism::isMature() { return growth()->mature; }
79
80bool Organism::isDead() { return growth()->dead; }
81
82bool Organism::isHarvested() { return growth()->harvested; }
83
84bool Organism::isDiseased() { return !health()->activeDiseaseId.empty(); }
85
86bool Organism::isPregnant() { return reproduction()->pregnant; }
87
88std::string Organism::diseaseId() { return health()->activeDiseaseId; }
89
90double Organism::waterPermille() { return meterOrZero(*vitalNeeds(), "water"); }
91
92double Organism::foodPermille() { return meterOrZero(*vitalNeeds(), "food"); }
93
94double Organism::fertilityPermille() { return meterOrZero(*vitalNeeds(), "fertility"); }
95
96double Organism::needPermille(const std::string& needId) { return meterOrZero(*vitalNeeds(), needId); }
97
98Result<void> Organism::refillNeed(const std::string& needId, double permille) {
99 const SpeciesDef* species = SpeciesRegistry::find(growth()->speciesId);
100 if (!species) {
102 Diagnostic::error(DiagnosticCode::NotFound, "unknown species", "growth.organism.refillNeed"));
103 }
104 if (!species->hasNeed(needId)) {
106 "species does not declare need '" + needId + "'",
107 "growth.organism.refillNeed"));
108 }
109 if (growth()->dead) {
111 Diagnostic::error(DiagnosticCode::PreconditionViolation, "organism is dead", "growth.organism.refillNeed"));
112 }
113 if (!std::isfinite(permille) || permille < 0.0) {
115 DiagnosticCode::InvalidArgument, "need amount must be finite and >= 0", "growth.organism.refillNeed"));
116 }
117 auto& meters = vitalNeeds()->meters;
118 meters[needId] = clampPermille(meterOrZero(*vitalNeeds(), needId) + permille);
119 if (needId == "water") {
120 GrowthWorld::instance().emit(GrowthEventKind::Watered, *this, std::to_string(permille));
121 } else if (needId == "food") {
122 GrowthWorld::instance().emit(GrowthEventKind::Fed, *this, std::to_string(permille));
123 } else if (needId == "fertility") {
124 GrowthWorld::instance().emit(GrowthEventKind::Fertilized, *this, std::to_string(permille));
125 } else {
126 GrowthWorld::instance().emit(GrowthEventKind::Watered, *this, needId + ":" + std::to_string(permille));
127 }
128 return Result<void>::success();
129}
130
131Result<void> Organism::waterBy(double permille) { return refillNeed("water", permille); }
132
133Result<void> Organism::feed(double permille) { return refillNeed("food", permille); }
134
135Result<void> Organism::fertilize(double permille) { return refillNeed("fertility", permille); }
136
138 if (growth()->dead) {
139 return Result<std::vector<YieldReceipt>>::failure(
140 Diagnostic::error(DiagnosticCode::PreconditionViolation, "organism is dead", "growth.organism.harvest"));
141 }
142 if (!growth()->mature) {
144 DiagnosticCode::PreconditionViolation, "organism is not mature", "growth.organism.harvest"));
145 }
146 if (growth()->harvested) {
148 DiagnosticCode::PreconditionViolation, "organism already harvested", "growth.organism.harvest"));
149 }
150 const SpeciesDef* species = SpeciesRegistry::find(growth()->speciesId);
151 if (!species) {
152 return Result<std::vector<YieldReceipt>>::failure(
153 Diagnostic::error(DiagnosticCode::NotFound, "unknown species", "growth.organism.harvest"));
154 }
155 if (species->harvestYield.empty()) {
157 DiagnosticCode::InvalidArgument, "species has no harvestYield", "growth.organism.harvest"));
158 }
159
160 std::vector<YieldReceipt> yields;
161 yields.reserve(species->harvestYield.size());
162 for (const YieldDef& def : species->harvestYield) {
163 const int count = def.minCount + (def.maxCount - def.minCount) / 2;
164 yields.push_back(YieldReceipt{def.itemId, count});
165 }
166 growth()->harvested = true;
167 GrowthWorld::instance().emit(GrowthEventKind::Harvested, *this, std::to_string(static_cast<int>(yields.size())));
168 return Result<std::vector<YieldReceipt>>::success(std::move(yields));
169}
170
171std::uint64_t Organism::stableId() const noexcept {
172 return static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(this));
173}
174
176
177} // namespace eve::growth
Stable, structured diagnostics shared by engine modules.
OceanSprayView view
Object::Ptr value
RoadNetwork resolved
int count
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static GrowthWorld & instance()
Process-wide world used by Organism helpers and the Growth module.
Growth short-root entity for any configured lifeform.
Definition Organism.h:29
Result< std::vector< YieldReceipt > > harvest()
Harvests once when mature and harvestYield is non-empty (any lifeform). @cost Moderate: copies yield ...
Definition Organism.cpp:137
double fertilityPermille()
Definition Organism.cpp:94
static const std::vector< Organism * > & liveOrganisms()
Live organisms currently visible to Growth views.
Definition Organism.cpp:52
std::string diseaseId()
Definition Organism.cpp:88
Result< void > waterBy(double permille)
Adds water when the species declares a water need. @cost Cheap: O(1) meter write + optional event.
Definition Organism.cpp:131
Result< void > feed(double permille)
Adds food when the species declares a food need. @cost Cheap: O(1) meter write + optional event.
Definition Organism.cpp:133
Result< void > fertilize(double permille)
Adds fertility when the species declares a fertility need. @cost Cheap: O(1) meter write + optional e...
Definition Organism.cpp:135
const std::string & kindId()
Definition Organism.cpp:62
void release() override
Definition Organism.h:33
static Result< Organism * > createOrganism(const std::string &speciesId)
Creates an organism bound to a registered species.
Definition Organism.cpp:25
std::string stageId()
Definition Organism.cpp:68
double needPermille(const std::string &needId)
Generic meter read; missing need returns 0.
Definition Organism.cpp:96
double stageProgressDays()
Definition Organism.cpp:76
std::uint64_t stableId() const noexcept
Stable ECS id for event correlation.
Definition Organism.cpp:171
const std::string & speciesId()
Definition Organism.cpp:60
double waterPermille()
Convenience meters when the species declares water/food/fertility needs.
Definition Organism.cpp:90
void destroy()
Script destroy entry (aliases release).
Definition Organism.cpp:175
OrganismKind kind()
Definition Organism.cpp:64
Result< void > refillNeed(const std::string &needId, double permille)
Refills a named need declared by the species. @cost Cheap: O(1) meter write + optional event.
Definition Organism.cpp:98
static const SpeciesDef * find(const std::string &id)
Looks up a species; nullptr when missing. @ownership Borrowed from the process-wide registry; caller ...
Definition Species.cpp:540
OrganismKind
Optional Earth-biology label; custom lifeforms use Custom + free-form kindId.
Definition Species.h:18
Named vital meter (water, radiation, ether, …) — not fixed to Earth biology.
Definition Species.h:35
Static species definition — fully data-driven life cycle.
Definition Species.h:81
std::vector< NeedMeterDef > needs
Definition Species.h:89
std::vector< GrowthStageDef > stages
Definition Species.h:88
std::vector< YieldDef > harvestYield
Definition Species.h:99
OrganismKind kind
Convenience label derived from kindId when plant/animal; else Custom.
Definition Species.h:87
std::string kindId
Free-form lifeform tag (plant, animal, xeno.crystal, …).
Definition Species.h:85
bool hasNeed(std::string_view needId) const
True when the species declares a need meter with this id.
Definition Species.h:112
One item emitted by harvest or periodic produce.
Definition Species.h:28
One harvested or produced item line (no inventory dependency).
Definition Organism.h:18