• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[JASS] Simple Day and Night toggler with counter

Level 3
Joined
May 4, 2023
Messages
33
Greetings everyone! It's been a long time since I was using Hive, and now, i suppose, it's time to provide some stuff in return

This function simply toggles daytime (12:00) or night time (00:00) when need as well as freezes it.
When toggled off, unfreezes time and procceds from the daytime which would be if there was no imitation called (that's the whole purpose of this function)
Change Divider if you have different day length.

Day imitation has higher priority than night imitation.

All needed comments are provided inside

JASS:
function ImitateDayNightEffectsTimeOperations takes nothing returns nothing
    local integer dayCounter=LoadInteger(GlobalHashtable,'DTIM',0)
    local integer nightCounter=LoadInteger(GlobalHashtable,'DTIM',1)
    local real dayTimeStart
    local real timerTimeStart
    local real finalTime
    if dayCounter==0 and nightCounter==0 then
        call SaveBoolean(GlobalHashtable,'DTIM',0,false)
        set dayTimeStart=LoadReal(GlobalHashtable,'DTIM',0)
        set timerTimeStart=LoadReal(GlobalHashtable,'DTIM',1)
        set finalTime=dayTimeStart+((TimerGetElapsed(GlobalTimer)-timerTimeStart)/ 25.0)
        // divider 25.0 is related to DayLength = 600.0 in War3mapMisc.txt
        if finalTime>= 24. then
            set finalTime=finalTime-24.
        endif
        call SetFloatGameState(GAME_STATE_TIME_OF_DAY,finalTime)
        call SuspendTimeOfDay(false)
    elseif dayCounter>0 then
        if not LoadBoolean(GlobalHashtable,'DTIM',0) then
            call SaveBoolean(GlobalHashtable,'DTIM',0,true)
            call SaveReal(GlobalHashtable,'DTIM',0,GetFloatGameState(GAME_STATE_TIME_OF_DAY))
            call SaveReal(GlobalHashtable,'DTIM',1,TimerGetElapsed(GlobalTimer))
        endif
        call SetFloatGameState(GAME_STATE_TIME_OF_DAY,12)
        call SuspendTimeOfDay(true)
    elseif nightCounter>0 then
        if not LoadBoolean(GlobalHashtable,'DTIM',0) then
            call SaveBoolean(GlobalHashtable,'DTIM',0,true)
            call SaveReal(GlobalHashtable,'DTIM',0,GetFloatGameState(GAME_STATE_TIME_OF_DAY))
            call SaveReal(GlobalHashtable,'DTIM',1,TimerGetElapsed(GlobalTimer))
        endif
        call SetFloatGameState(GAME_STATE_TIME_OF_DAY,0)
        call SuspendTimeOfDay(true)
    endif
endfunction

function ImitateDayNightEffectToggle takes integer i returns nothing
    //-1 to start night, -2 to end night, 1 to start day, 2 to end day
    if i==-1 then
        call SaveInteger(GlobalHashtable,'DTIM',1,LoadInteger(GlobalHashtable,'DTIM',1)+1)
    elseif i==-2 then
        call SaveInteger(GlobalHashtable,'DTIM',1,LoadInteger(GlobalHashtable,'DTIM',1)-1)
    elseif i==1 then
        call SaveInteger(GlobalHashtable,'DTIM',0,LoadInteger(GlobalHashtable,'DTIM',0)+1)
    elseif i==2 then
        call SaveInteger(GlobalHashtable,'DTIM',0,LoadInteger(GlobalHashtable,'DTIM',0)-1)
    else
        return
    endif
    call ImitateDayNightEffectsTimeOperations()
endfunction
 
Top