载入中...
搜索中...
未找到
GrowthCalendar.cpp
浏览该文件的文档.
2
3#include "common/Assert.h"
4#include "common/Diagnostic.h"
5#include "common/Value.h"
6
7namespace eve::growth {
8namespace {
9
10std::vector<SeasonSlotDef> defaultEarthSeasons(int days) {
11 return {
12 SeasonSlotDef{"spring", days},
13 SeasonSlotDef{"summer", days},
14 SeasonSlotDef{"autumn", days},
15 SeasonSlotDef{"winter", days},
16 };
17}
18
19} // namespace
20
21GrowthCalendar::GrowthCalendar() : seasons_(defaultEarthSeasons(28)) {}
22
23Result<void> GrowthCalendar::setSeasonLayout(std::vector<SeasonSlotDef> seasons) noexcept {
24 if (seasons.empty()) {
26 "season layout must be non-empty", "growth.calendar.layout"));
27 }
28 for (const SeasonSlotDef& slot : seasons) {
29 if (slot.id.empty() || slot.days <= 0) {
31 "each season needs a non-empty id and days > 0",
32 "growth.calendar.layout"));
33 }
34 }
35 seasons_ = std::move(seasons);
36 return Result<void>::success();
37}
38
40 auto parsed = Value::fromJson(json);
41 if (!parsed) return Result<void>::failure(parsed.status());
42 Value root = std::move(parsed).takeValue();
43 const auto* array = root.getIf<Value::Array>();
44 if (!array || array->empty()) {
46 "season layout JSON must be a non-empty array",
47 "growth.calendar.layout"));
48 }
49 std::vector<SeasonSlotDef> seasons;
50 seasons.reserve(array->size());
51 for (const Value& entry : *array) {
52 const auto* object = entry.getIf<Value::Object>();
53 if (!object) {
55 DiagnosticCode::InvalidArgument, "season entry must be an object", "growth.calendar.layout"));
56 }
58 auto idIt = object->find("id");
59 if (idIt == object->end() || !idIt->second.getIf<std::string>()) {
61 "season.id must be a string", "growth.calendar.layout"));
62 }
63 slot.id = *idIt->second.getIf<std::string>();
64 slot.days = 28;
65 auto daysIt = object->find("days");
66 if (daysIt != object->end()) {
67 if (const auto* integer = daysIt->second.getIf<std::int64_t>()) {
68 slot.days = static_cast<int>(*integer);
69 } else if (const auto* number = daysIt->second.getIf<double>()) {
70 slot.days = static_cast<int>(*number);
71 } else {
73 DiagnosticCode::InvalidArgument, "season.days must be an integer", "growth.calendar.layout"));
74 }
75 }
76 seasons.push_back(std::move(slot));
77 }
78 return setSeasonLayout(std::move(seasons));
79}
80
82 if (days <= 0) {
84 DiagnosticCode::InvalidArgument, "daysPerSeason must be positive", "growth.calendar.daysPerSeason"));
85 }
86 if (seasons_.empty()) seasons_ = defaultEarthSeasons(days);
87 for (SeasonSlotDef& slot : seasons_) slot.days = days;
88 return Result<void>::success();
89}
90
91int GrowthCalendar::daysPerSeason() const noexcept {
92 if (seasons_.empty()) return 28;
93 return seasons_.front().days;
94}
95
96int GrowthCalendar::seasonCount() const noexcept { return static_cast<int>(seasons_.size()); }
97
98std::int64_t GrowthCalendar::yearLength() const noexcept {
99 std::int64_t total = 0;
100 for (const SeasonSlotDef& slot : seasons_) total += slot.days;
101 EV_ASSERT(total > 0, "season layout must stay positive");
102 return total;
103}
104
105std::int64_t GrowthCalendar::year() const noexcept { return dayIndex_ / yearLength(); }
106
107int GrowthCalendar::seasonIndex() const noexcept {
108 if (seasons_.empty()) return 0;
109 std::int64_t dayInYear = dayIndex_ % yearLength();
110 for (std::size_t i = 0; i < seasons_.size(); ++i) {
111 if (dayInYear < seasons_[i].days) return static_cast<int>(i);
112 dayInYear -= seasons_[i].days;
113 }
114 return static_cast<int>(seasons_.size() - 1);
115}
116
117std::string GrowthCalendar::seasonId() const {
118 if (seasons_.empty()) return "spring";
119 return seasons_[static_cast<std::size_t>(seasonIndex())].id;
120}
121
123 return static_cast<Season>(seasonIndex() % 4);
124}
125
126int GrowthCalendar::dayOfSeason() const noexcept {
127 if (seasons_.empty()) return 0;
128 std::int64_t dayInYear = dayIndex_ % yearLength();
129 for (const SeasonSlotDef& slot : seasons_) {
130 if (dayInYear < slot.days) return static_cast<int>(dayInYear);
131 dayInYear -= slot.days;
132 }
133 return 0;
134}
135
136void GrowthCalendar::advanceWholeDays(std::int64_t days) noexcept {
137 if (days <= 0) return;
138 dayIndex_ += days;
139}
140
141void GrowthCalendar::reset() noexcept { dayIndex_ = 0; }
142
143const char* GrowthCalendar::seasonName(Season season) noexcept {
144 switch (season) {
145 case Season::Spring: return "spring";
146 case Season::Summer: return "summer";
147 case Season::Autumn: return "autumn";
148 case Season::Winter: return "winter";
149 }
150 return "spring";
151}
152
153} // namespace eve::growth
int root
Definition AnimSmr.cpp:119
EVEngine assertion entry point, backed by zeroerr.
#define EV_ASSERT(cond,...)
Assert an internal engine invariant (state that must always hold).
Definition Assert.h:37
Stable, structured diagnostics shared by engine modules.
std::string slot
double number
Json object
Owning, renderer-independent dynamic values.
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:209
int daysPerSeason() const noexcept
Days of the first season slot (legacy helper).
Result< void > setSeasonLayoutFromJson(std::string_view json)
Parses [{"id":"bloom","days":40},…] and applies setSeasonLayout. @cost Cheap JSON parse of a small ar...
std::string seasonId() const
Current season id string (never empty when layout is valid).
Result< void > setSeasonLayout(std::vector< SeasonSlotDef > seasons) noexcept
Replaces the season layout (must be non-empty; each slot days > 0). @cost Cheap: copies a small seaso...
void reset() noexcept
Resets to day 0 (tests).
std::int64_t year() const noexcept
0-based year from dayIndex / yearLength.
Season season() const noexcept
Legacy Season enum when layout is the default 4 Earth seasons; else maps by index%4.
int seasonIndex() const noexcept
Index into seasonLayout() for the current day.
Result< void > setDaysPerSeason(int days) noexcept
Sets every slot's day count (keeps current ids). Positive only.
int seasonCount() const noexcept
static const char * seasonName(Season season) noexcept
Stable English season name for the default Earth layout.
int dayOfSeason() const noexcept
Day within the current season [0, slot.days).
void advanceWholeDays(std::int64_t days) noexcept
Advances the calendar by whole simulation days (truncated toward zero).
Season
Legacy four-season index kept for Earth-farm defaults and older tests.
One named season slot in a configurable calendar layout.