From ccb8ec345b3e47e8268f96b9b094e4fc9fb53aa2 Mon Sep 17 00:00:00 2001 From: "Allen B. Cummings" Date: Tue, 26 Feb 2019 11:00:46 -0500 Subject: [PATCH] Merged PR 450: Merge 92_time_support to master Related work items: #93, #92 --- dictionaries/environmentDictionary.c | 220 +++++++++++++++++++++++++- tests/environment/timeAndSeasonTest.c | 81 ++++++++++ 2 files changed, 293 insertions(+), 8 deletions(-) create mode 100644 tests/environment/timeAndSeasonTest.c diff --git a/dictionaries/environmentDictionary.c b/dictionaries/environmentDictionary.c index 7690bcca9..abd4346b4 100644 --- a/dictionaries/environmentDictionary.c +++ b/dictionaries/environmentDictionary.c @@ -8,10 +8,79 @@ private string BaseEnvironment = "lib/environment/environment.c"; private string BaseElement = "lib/environment/environmentalElement.c"; private string currentSeason = "summer"; private string currentTimeOfDay = "noon"; +private int currentYear = 1; private mapping elementList = ([]); private string *validSeasons = ({ "winter", "spring", "summer", "autumn" }); -private string *validTimesOfDay = ({ "midnight", "night", "dawn", "morning", "noon", "afternoon", "evening", "dusk" }); + +private int currentTime = 660; +private int currentDayOfYear = 92; + +private mapping timesOfDay = ([ + "midnight":([ + "winter": 60, + "spring": 60, + "summer": 60, + "autumn": 60, + "next": "late night" + ]), + "late night":([ + "winter": 390, + "spring": 330, + "summer": 270, + "autumn": 330, + "next": "dawn" + ]), + "dawn":([ + "winter": 450, + "spring": 390, + "summer": 330, + "autumn": 390, + "next": "morning" + ]), + "morning":([ + "winter": 660, + "spring": 660, + "summer": 660, + "autumn": 660, + "next": "noon" + ]), + "noon":([ + "winter": 720, + "spring": 720, + "summer": 720, + "autumn": 720, + "next": "afternoon" + ]), + "afternoon":([ + "winter": 930, + "spring": 990, + "summer": 1050, + "autumn": 990, + "next": "evening" + ]), + "evening":([ + "winter": 1050, + "spring": 1110, + "summer": 1170, + "autumn": 1110, + "next": "dusk" + ]), + "dusk":([ + "winter": 1110, + "spring": 1170, + "summer": 1230, + "autumn": 1170, + "next": "night" + ]), + "night":([ + "winter": 1440, + "spring": 1440, + "summer": 1440, + "autumn": 1440, + "next": "midnight" + ]) +]); private string *entryMessages = ({ "you enter", "you have come across", "you emerge in", "you come upon", "entering the area, you see", @@ -173,27 +242,130 @@ public nomask int isValidSeason(string season) public nomask int isValidTimeOfDay(string time) { return time && stringp(time) && - (member(validTimesOfDay, time) > -1); + (member(m_indices(timesOfDay), time) > -1); } ///////////////////////////////////////////////////////////////////////////// public nomask int sunlightIsVisible() { - return member(({ "midnight", "night" }), currentTimeOfDay) < 0; + return member(({ "midnight", "night", "late night" }), currentTimeOfDay) < 0; } ///////////////////////////////////////////////////////////////////////////// -public nomask string timeOfDay(string newTime) +public nomask varargs string timeOfDay(string newTime) { - string ret = 0; - if (newTime && stringp(newTime) && - (member(validTimesOfDay, newTime) > -1)) + if (stringp(newTime) && member(timesOfDay, newTime)) { currentTimeOfDay = newTime; + currentTime = timesOfDay[newTime][currentSeason] - 60; } + return currentTimeOfDay; } +///////////////////////////////////////////////////////////////////////////// +public nomask string currentDate() +{ + return sprintf("%02d:%02d on day %d of year %d", + currentTime / 60, currentTime % 60, currentDayOfYear, currentYear); +} + +///////////////////////////////////////////////////////////////////////////// +public nomask int currentTime() +{ + return currentTime; +} + +///////////////////////////////////////////////////////////////////////////// +public nomask int currentDay() +{ + return currentDayOfYear; +} + +///////////////////////////////////////////////////////////////////////////// +public nomask int currentYear() +{ + return currentYear; +} + +///////////////////////////////////////////////////////////////////////////// +private nomask void calculateSeason() +{ + switch (currentDayOfYear) + { + case 0..91: + { + currentSeason = "spring"; + break; + } + case 92..182: + { + currentSeason = "summer"; + break; + } + case 183..273: + { + currentSeason = "autumn"; + break; + } + case 274..364: + { + currentSeason = "winter"; + break; + } + default: + { + currentYear++; + currentDayOfYear = 0; + currentSeason = "spring"; + break; + } + } +} + +///////////////////////////////////////////////////////////////////////////// +public nomask void setDay(int newDay) +{ + currentDayOfYear = newDay; + calculateSeason(); +} + +///////////////////////////////////////////////////////////////////////////// +public nomask void setYear(int value) +{ + currentYear = value; +} + +///////////////////////////////////////////////////////////////////////////// +public nomask varargs void advanceTime(int amount) +{ + if (!amount) + { + currentTime++; + } + else + { + currentTime += amount; + } + + if (currentTime >= 1440) + { + currentDayOfYear++; + currentTime = 0; + currentTimeOfDay = "midnight"; + calculateSeason(); + } + else if(currentTime >= timesOfDay[currentTimeOfDay][currentSeason]) + { + currentTimeOfDay = timesOfDay[currentTimeOfDay]["next"]; + } + + if (!amount) + { + call_out("advanceTime", 5); + } +} + ///////////////////////////////////////////////////////////////////////////// public nomask string season(string newSeason) { @@ -202,6 +374,29 @@ public nomask string season(string newSeason) (member(validSeasons, newSeason) > -1)) { currentSeason = newSeason; + switch (currentSeason) + { + case "spring": + { + currentDayOfYear = 0; + break; + } + case "summer": + { + currentDayOfYear = 92; + break; + } + case "autumn": + { + currentDayOfYear = 183; + break; + } + case "winter": + { + currentDayOfYear = 274; + break; + } + } } return currentSeason; } @@ -209,7 +404,7 @@ public nomask string season(string newSeason) ///////////////////////////////////////////////////////////////////////////// public nomask string *timesOfDay() { - return validTimesOfDay + ({}); + return m_indices(timesOfDay); } ///////////////////////////////////////////////////////////////////////////// @@ -247,3 +442,12 @@ public nomask object getRegion(string region) { return 0; } + +///////////////////////////////////////////////////////////////////////////// +public nomask void reset(int arg) +{ + if (!arg) + { + advanceTime(); + } +} diff --git a/tests/environment/timeAndSeasonTest.c b/tests/environment/timeAndSeasonTest.c new file mode 100644 index 000000000..4ffb94747 --- /dev/null +++ b/tests/environment/timeAndSeasonTest.c @@ -0,0 +1,81 @@ +//***************************************************************************** +// Copyright (c) 2019 - Allen Cummings, RealmsMUD, All rights reserved. See +// the accompanying LICENSE file for details. +//***************************************************************************** +inherit "/lib/tests/framework/testFixture.c"; + +object Dictionary; + +///////////////////////////////////////////////////////////////////////////// +void Setup() +{ + Dictionary = load_object("/lib/dictionaries/environmentDictionary.c"); +} + +///////////////////////////////////////////////////////////////////////////// +void CleanUp() +{ + destruct(Dictionary); +} + +///////////////////////////////////////////////////////////////////////////// +void CurrentTimeDayANdYearCorrectlyDisplayed() +{ + ExpectEq(661, Dictionary->currentTime()); + ExpectEq(92, Dictionary->currentDay()); + ExpectEq(1, Dictionary->currentYear()); + + Dictionary->advanceTime(345); + ExpectEq(1006, Dictionary->currentTime()); + ExpectEq(92, Dictionary->currentDay()); + ExpectEq(1, Dictionary->currentYear()); +} + +///////////////////////////////////////////////////////////////////////////// +void SettingTimeOfDayAltersCurrentTime() +{ + ExpectEq(661, Dictionary->currentTime()); + ExpectEq("noon", Dictionary->timeOfDay()); + + Dictionary->timeOfDay("afternoon"); + ExpectEq(990, Dictionary->currentTime()); + ExpectEq("afternoon", Dictionary->timeOfDay()); +} + +///////////////////////////////////////////////////////////////////////////// +void AdvancingTimePastEndOfDayIncrementsDayAndRollsTime() +{ + Dictionary->timeOfDay("night"); + ExpectEq(92, Dictionary->currentDay()); + + Dictionary->advanceTime(60); + ExpectEq(0, Dictionary->currentTime()); + ExpectEq("midnight", Dictionary->timeOfDay()); + ExpectEq(93, Dictionary->currentDay()); +} + +///////////////////////////////////////////////////////////////////////////// +void AdvancingTimePastSeasonIncrementsSeason() +{ + Dictionary->setDay(91); + ExpectEq(91, Dictionary->currentDay()); + ExpectEq("spring", Dictionary->season()); + + Dictionary->advanceTime(1440); + ExpectEq(92, Dictionary->currentDay()); + ExpectEq("summer", Dictionary->season()); +} + +///////////////////////////////////////////////////////////////////////////// +void AdvancingTimePastYearIncrementsYearAndResetsSeason() +{ + Dictionary->setDay(364); + ExpectEq(364, Dictionary->currentDay()); + ExpectEq("winter", Dictionary->season()); + ExpectEq(1, Dictionary->currentYear()); + + Dictionary->advanceTime(1440); + ExpectEq(0, Dictionary->currentDay()); + ExpectEq("spring", Dictionary->season()); + ExpectEq(2, Dictionary->currentYear()); +}