Skip to content

Commit

Permalink
Merged PR 450: Merge 92_time_support to master
Browse files Browse the repository at this point in the history
Related work items: #93, #92
  • Loading branch information
Allen B. Cummings committed Feb 26, 2019
1 parent 02917a4 commit ccb8ec3
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 8 deletions.
220 changes: 212 additions & 8 deletions dictionaries/environmentDictionary.c
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
{
Expand All @@ -202,14 +374,37 @@ 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;
}

/////////////////////////////////////////////////////////////////////////////
public nomask string *timesOfDay()
{
return validTimesOfDay + ({});
return m_indices(timesOfDay);
}

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -247,3 +442,12 @@ public nomask object getRegion(string region)
{
return 0;
}

/////////////////////////////////////////////////////////////////////////////
public nomask void reset(int arg)
{
if (!arg)
{
advanceTime();
}
}
81 changes: 81 additions & 0 deletions 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());
}

0 comments on commit ccb8ec3

Please sign in to comment.