TimeOfDayLib

Level 17
Joined
Jun 17, 2007
Messages
1,433
A library that's used to get the amount of day time that has passed since the beginning of the game. It requires more an arithmetic operation because the time can be slowed down, sped up, or stopped completely. I've also added IsDay() and is IsNight() for completeness.

I can see it being useful for stuff like evolution systems where the time might be slowed down or sped up for whatever reason. This system doesn't support SetTimeOfDay() because I would need a wrapper function to tell whether the time is being increased or decreased.
JASS:
//To import, create a "trigger" called TimeOfDayLib and copy this code into it.
//Change the PRECISION constant to match your needs. If you changed the time gameplay constants, then also
//configure TIME_DAWN and TIME_DUSK.
//You are given access to the following functions:
//constant function GetTimeElapsed takes nothing returns real
//Gets the time that has been experienced by players since the start of the game (in seconds).
//It will adjust to SuspendTimeOfDay and SetTimeOfDayScale.
//function IsDay takes nothing returns boolean
//Checks if the current time is day.
//function IsNight takes nothing returns boolean
//Checks if the current time is night.

library TimeOfDayLib initializer Init

globals
    private constant real PRECISION = .1 
    //This lower the variable is, the more precise answers from GetTimeElapsed will be. However, more code will be run.
    //It's negligible, but the user might want to change this.
    private constant real TIME_DUSK = 18.
    //The time when day turns into night. Configure if you changed the gameplay constant.
    private constant real TIME_DAWN = 6.
    //The time when night turns into day. Configure if you changed the gameplay constant.
    private real Increase  = PRECISION*3
    private real Time = 0.
    private timer Tim = CreateTimer()
endglobals

constant function GetTimeElapsed takes nothing returns real
    return Time
endfunction

function IsDay takes nothing returns boolean
    local real r = GetFloatGameState(GAME_STATE_TIME_OF_DAY)
    return r >= TIME_DAWN and r < TIME_DUSK
endfunction

function IsNight takes nothing returns boolean
    local real r = GetFloatGameState(GAME_STATE_TIME_OF_DAY)
    return r < TIME_DAWN and r >= TIME_DUSK
endfunction

private function onTimer takes nothing returns nothing
    set Time = Time + Increase
    debug call BJDebugMsg(R2S(GetTimeElapsed()))
endfunction

private function SuspendTimeOfDayEx takes boolean b returns nothing
    if b then
        call PauseTimer(Tim)
    else
        call TimerStart(Tim, PRECISION, true, function onTimer)
    endif
endfunction

private function SetTimeOfDayScaleEx takes real r returns nothing
    set Increase = PRECISION * 3 * r
endfunction

hook SuspendTimeOfDay SuspendTimeOfDayEx
hook SetTimeOfDayScale SetTimeOfDayScaleEx

private function Init takes nothing returns nothing
    call TimerStart(Tim, PRECISION, true, function onTimer)
endfunction

endlibrary
 
Last edited:
You should add in tilts for the axis of the planet over a given period of time to mimic actual times of day in the world (the rotation should be able to be modified ofc).

From here, you could easily create time zones across your map (sometimes quite interesting). I'm honestly not sure if time can be made local or not, but if it could, my suggestion would be entirely possible and pretty neato ; ).

Otherwise, things similar to this (like at wc3c and TH) have been done before. I tried submitting a very simple little snippet that just returned the current time and it was rejected because of the one at wc3c ; ).

You also need to allow time of day events (register on dusk, dawn) and just regular times or w/e.

Oh well, pretty decent job I think ; ).
 
Level 17
Joined
Jun 17, 2007
Messages
1,433
You should add in tilts for the axis of the planet over a given period of time to mimic actual times of day in the world (the rotation should be able to be modified ofc).
No, that was never the intention of this script.


Otherwise, things similar to this (like at wc3c and TH) have been done before. I tried submitting a very simple little snippet that just returned the current time and it was rejected because of the one at wc3c ; ).
This is not the same as Pyrogasm's GameClock.

You also need to allow time of day events (register on dusk, dawn) and just regular times or w/e.
JASS:
native TriggerRegisterGameStateEvent takes trigger whichTrigger, gamestate whichState, limitop opcode, real limitval returns event
 
Level 8
Joined
Oct 3, 2008
Messages
367
I must say I don't see a point to this. Elapsed time and time of day are two different subjects. Elapsed time can be pulled off with one ticking timer and I do believe there are systems, one on WC3C for sure, that do precisely that.

The time of day part of the system has also been done at least once before, including by myself and Jesus4Lyf. Unfortunately the one by myself is in management limbo right now so we can disregard it for the time being.
 
Level 17
Joined
Jun 17, 2007
Messages
1,433
I must say I don't see a point to this. Elapsed time and time of day are two different subjects. Elapsed time can be pulled off with one ticking timer and I do believe there are systems, one on WC3C for sure, that do precisely that.
There is one system on WC3C that does that, yes. However, this system and Pyrogasm's GameClock are fundamentally different. This is more catered towards instances where an absolute timer wouldn't make sense. For example, if you want spells that slow down, speed up, or stop time all together then this would be a better representation of the map's time, whereas GameClock would be real time.
 
Level 8
Joined
Oct 3, 2008
Messages
367
Then simply make a timer with the functionality to be slowed down and sped up. Time of day doesn't have to be too involved much past slowing the sun temporarily. Should be a standalone system. The booleans for day and night are rather unnecessary.
 
Top