summaryrefslogtreecommitdiff
path: root/src/environment.cpp
diff options
context:
space:
mode:
authorAuke Kok <sofar@foo-projects.org>2016-03-06 12:02:21 -0800
committerest31 <MTest31@outlook.com>2016-03-09 01:45:23 +0100
commit4e59fcf5c1e40dee764317a1190dceadc3a77829 (patch)
treebe216e4eb402e8529ff7a001839077f22c74d8ac /src/environment.cpp
parent3ce6642a26d43fd368c24cdfc5460557258fb44f (diff)
downloadminetest-4e59fcf5c1e40dee764317a1190dceadc3a77829.tar.gz
minetest-4e59fcf5c1e40dee764317a1190dceadc3a77829.tar.bz2
minetest-4e59fcf5c1e40dee764317a1190dceadc3a77829.zip
Add consistent monotonic day counter - get_day_count()
I've written several experimental bits of code that revolve around the need for a consistent calendar, but implementing one is extremely hard in mods due to time changes and mods overriding core.get_timeofday(), which will conflict. The second part of the problem is that doing this from a mod requires constant maintenance of a settings file. An implementation in core is trivial, however, and solves all of these problems at virtually no cost: No extra branches in server steps, and a single branch when minetest.set_time_of_day(), which is entirely reasonable. We store the day_count value in env_meta.txt. The use case is obvious: This change allows mods to create an actual virtual calendar, or properly account for seasonal changes, etc.. We add a "/days" chatcommand that displays the current day count. No permissions are needed. It can only retrieve the day count, not modify it.
Diffstat (limited to 'src/environment.cpp')
-rw-r--r--src/environment.cpp20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/environment.cpp b/src/environment.cpp
index 6da376a15..0d00ed170 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -208,6 +208,8 @@ void Environment::setDayNightRatioOverride(bool enable, u32 value)
void Environment::setTimeOfDay(u32 time)
{
MutexAutoLock lock(this->m_time_lock);
+ if (m_time_of_day > time)
+ m_day_count++;
m_time_of_day = time;
m_time_of_day_f = (float)time / 24000.0;
}
@@ -238,8 +240,10 @@ void Environment::stepTimeOfDay(float dtime)
bool sync_f = false;
if (units > 0) {
// Sync at overflow
- if (m_time_of_day + units >= 24000)
+ if (m_time_of_day + units >= 24000) {
sync_f = true;
+ m_day_count++;
+ }
m_time_of_day = (m_time_of_day + units) % 24000;
if (sync_f)
m_time_of_day_f = (float)m_time_of_day / 24000.0;
@@ -256,6 +260,13 @@ void Environment::stepTimeOfDay(float dtime)
}
}
+u32 Environment::getDayCount()
+{
+ // Atomic<u32> counter
+ return m_day_count;
+}
+
+
/*
ABMWithState
*/
@@ -727,6 +738,7 @@ void ServerEnvironment::saveMeta()
args.setU64("lbm_introduction_times_version", 1);
args.set("lbm_introduction_times",
m_lbm_mgr.createIntroductionTimesString());
+ args.setU64("day_count", m_day_count);
args.writeLines(ss);
ss<<"EnvArgsEnd\n";
@@ -792,6 +804,12 @@ void ServerEnvironment::loadMeta()
}
m_lbm_mgr.loadIntroductionTimes(lbm_introduction_times, m_gamedef, m_game_time);
+ try {
+ m_day_count = args.getU64("day_count");
+ } catch (SettingNotFoundException &e) {
+ // If missing, start the day counter
+ m_day_count = 0;
+ }
}
void ServerEnvironment::loadDefaultMeta()