summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/Stardate.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-04-01 05:16:14 +0200
committerAki <please@ignore.pl>2024-04-01 05:21:16 +0200
commit3be3bfaa17773550a696ed2b756136debfe79ae2 (patch)
treedb55a420e43ddaa4a72140d2795892757c1672d3 /StarsEx/Stardate.cpp
parent0a3451f251f360267e2927d8320787d59148eadd (diff)
downloadstarshatter-3be3bfaa17773550a696ed2b756136debfe79ae2.zip
starshatter-3be3bfaa17773550a696ed2b756136debfe79ae2.tar.gz
starshatter-3be3bfaa17773550a696ed2b756136debfe79ae2.tar.bz2
Fixed date and time consistency across Campaigns and Missions
This fixes campaign mission generation mostly, but a full playthrough will be needed. Missions now serialize and accept stardate setting a bit better. Thanks to this, date is propagated over multiplayer, too. This seems to break points system? Code-wise, this does not workaround the problems from before namely over-reliance on side-effects. Stardate class is at least one small step into good direction. Now, it'd be nice to attach clocks to simulation and campaign and whatever else that needs them.
Diffstat (limited to 'StarsEx/Stardate.cpp')
-rw-r--r--StarsEx/Stardate.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/StarsEx/Stardate.cpp b/StarsEx/Stardate.cpp
new file mode 100644
index 0000000..f57205a
--- /dev/null
+++ b/StarsEx/Stardate.cpp
@@ -0,0 +1,137 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#include "Stardate.h"
+
+#include <cstdint>
+
+#include <Text.h>
+
+#include "Clock.h"
+
+
+namespace starshatter
+{
+namespace engine
+{
+
+
+static constexpr long double EPOCH {0.5e9l};
+static constexpr long double MINUTE {60};
+static constexpr long double HOUR {60 * MINUTE};
+static constexpr long double DAY {24 * HOUR};
+static long double operation_start {};
+static long double mission_start {};
+
+
+Stardate::Stardate() :
+ value {0}
+{
+}
+
+
+Stardate::Stardate(long double value_) :
+ value {value_}
+{
+}
+
+
+Stardate::operator long double() const
+{
+ return value;
+}
+
+
+Text
+Stardate::Format(bool short_format) const
+{
+ auto time = value;
+ std::int_fast32_t day = 1, hours = 0, minutes = 0, seconds = 0;
+ while (time >= DAY) {
+ time -= DAY;
+ ++day;
+ }
+ while (time >= HOUR) {
+ time -= HOUR;
+ ++hours;
+ }
+ while (time >= MINUTE) {
+ time -= MINUTE;
+ ++minutes;
+ }
+ seconds = time;
+ return Text::format(
+ short_format ? "%02d/%02d:%02d:%02d" : "Day %d, %02d:%02d:%02d",
+ day, hours, minutes, seconds);
+}
+
+
+Stardate
+CurrentTime()
+{
+ return Epoch() + OperationStart() + OperationTime();
+}
+
+
+Stardate
+OperationTime()
+{
+ return MissionStart() + MissionTime();
+}
+
+
+Stardate
+MissionTime()
+{
+ if (const auto* clock = Clock::GetInstance())
+ return clock->GameTime<long double>() / 1000.0l;
+ throw "game clock not initialized";
+}
+
+
+long double
+Epoch()
+{
+ return EPOCH;
+}
+
+
+long double
+OperationStart()
+{
+ return operation_start;
+}
+
+
+long double
+MissionStart()
+{
+ return mission_start;
+}
+
+
+void
+SetOperationStart(long double value, bool relative)
+{
+ if (relative)
+ operation_start += value;
+ else
+ operation_start = value;
+}
+
+
+void
+SetMissionStart(long double value, bool relative)
+{
+ if (relative)
+ mission_start += value;
+ else
+ mission_start = value;
+}
+
+
+} // namespace engine
+} // namespace starshatter