Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- fully animated 24 hour per day (day:hh:mm:ss) countdown timer!
- credit to divo for shortening the code by half & showing me how to use tables!
- written & annotated by AFRLme
- --]]
- -- * local variable which contains the condition for timer_is_on? * --
- local cond = getObject('Conditions[timer_is_on?]')
- -- * local variable which contains the action to check the current time! * --
- local act = getObject('Actions[check_time]')
- -- * let's create the function to find the first & last frames of an active animation! * --
- local function setAnimationFrame(anim, frame)
- anim:setValue(VAnimationFirstFrame, frame)
- anim:setValue(VAnimationLastFrame, frame)
- end
- -- * let's get the current active animations for displaying the timer! * --
- local anims_name = {
- -- * global variable for getting single second digit animations * --
- ssa = getObject('ActiveAnimations[ssa]'),
- -- * global variable for getting double second digit animations * --
- sda = getObject('ActiveAnimations[sda]'),
- -- * global variable for getting single minute digit animations * --
- msa = getObject('ActiveAnimations[msa]'),
- -- * global variable for getting double minute digit animations * --
- mda = getObject('ActiveAnimations[mda]'),
- -- * global variable for getting single hour digit animations * --
- hsa = getObject('ActiveAnimations[hsa]'),
- -- * global variable for getting double hour digit animations * --
- hda = getObject('ActiveAnimations[hda]'),
- -- * global variable for getting the remaining day animation * --
- dsa = getObject('ActiveAnimations[dsa]')
- }
- -- * get global value for days remaining! * --
- local day_val = getObject('Values[sin_day_val]')
- -- * get global value for double hours! * --
- local hd_val = getObject('Values[dbl_hour_val]')
- -- * get global value for single hours! * --
- local hs_val = getObject('Values[sin_hour_val]')
- -- * get global value for double minutes! * --
- local md_val = getObject('Values[dbl_min_val]')
- -- * get global value for single minutes! * --
- local ms_val = getObject('Values[sin_min_val]')
- -- * get global value for double seconds! * --
- local sd_val = getObject('Values[dbl_sec_val]')
- -- * get global value for single seconds! * --
- local ss_val = getObject('Values[sin_sec_val]')
- -- * global variable defaults for timer display value * --
- local vals = { ss_val:getInt(VValueInt), sd_val:getInt(VValueInt), ms_val:getInt(VValueInt), md_val:getInt(VValueInt), hs_val:getInt(VValueInt), hd_val:getInt(VValueInt), day_val:getInt(VValueInt) }
- -- * index table list for retrieving animations * --
- local anims_index = { anims_name.ssa, anims_name.sda, anims_name.msa, anims_name.mda, anims_name.hsa, anims_name.hda, anims_name.dsa }
- -- * let's add some countermeasures to auto-correct values that are too high or low! * --
- if vals[6] >= 2 and vals[5] >= 4 then
- vals[1] = 1 -- single second = 0
- vals[2] = 1 -- double second = 0
- vals[3] = 1 -- single minute = 0
- vals[4] = 1 -- double minute = 0
- vals[5] = 5 -- single hour = 4
- vals[6] = 3 -- double hour = 2
- if vals[7] > 9 then vals[7] = 10 end -- days remaining = 9
- if vals[7] < 0 then vals[7] = 1 end -- days remaining = 0
- if vals[7] >= 0 and vals[7] <= 9 then vals[7] = vals[7] + 1 end -- days remaining = days remaining + 1
- else
- -- * let's check if any values are greater than default maximum value! * --
- if vals[1] > 9 then vals[1] = 9 end -- single second = 9
- if vals[2] > 5 then vals[2] = 5 end -- double second = 5
- if vals[3] > 9 then vals[3] = 9 end -- single minute = 9
- if vals[4] > 5 then vals[4] = 5 end -- double minute = 5
- if vals[6] >= 2 then if vals[5] > 3 then vals[5] = 3 end end -- single hour = 3
- if vals[6] >= 0 and vals[6] < 2 then if vals[5] > 9 then vals[5] = 9 end end -- else single hour = 9
- if vals[7] > 9 then vals[7] = 9 end -- days remaining = 0
- -- * let's check if any values are less than or equal to zero! * --
- if vals[1] < 0 then vals[1] = 0 end -- single second = 0
- if vals[2] < 0 then vals[2] = 0 end -- double second = 0
- if vals[3] < 0 then vals[3] = 0 end -- single minute = 0
- if vals[4] < 0 then vals[4] = 0 end -- double minute = 0
- if vals[5] < 0 then vals[5] = 0 end -- single hour = 0
- if vals[6] < 0 then vals[6] = 0 end -- double hour = 0
- if vals[7] < 0 then vals[7] = 0 end -- days remaining = 0
- -- * let's double check our maths! * --
- if vals[1] >= 0 and vals[1] <= 9 then vals[1] = vals[1] + 1 end -- single second = single second + 1
- if vals[2] >= 0 and vals[2] <= 5 then vals[2] = vals[2] + 1 end -- double second = double second + 1
- if vals[3] >= 0 and vals[3] <= 9 then vals[3] = vals[3] + 1 end -- single minute = single minute + 1
- if vals[4] >= 0 and vals[4] <= 5 then vals[4] = vals[4] + 1 end -- double minute = double minute + 1
- if vals[5] >= 0 and vals[5] <= 9 then vals[5] = vals[5] + 1 end -- single hour = single hour + 1
- if vals[6] >= 0 and vals[6] <= 2 then vals[6] = vals[6] + 1 end -- double hour = double hour + 1
- if vals[7] >= 0 and vals[7] <= 9 then vals[7] = vals[7] + 1 end -- days remaining = days remaining + 1
- end
- -- * set each animation frame to current time values found in timer_interface value tab! * --
- setAnimationFrame(anims_name.ssa, vals[1]) -- single second
- setAnimationFrame(anims_name.sda, vals[2]) -- double second
- setAnimationFrame(anims_name.msa, vals[3]) -- single minute
- setAnimationFrame(anims_name.mda, vals[4]) -- double minute
- setAnimationFrame(anims_name.hsa, vals[5]) -- single hour
- setAnimationFrame(anims_name.hda, vals[6]) -- double hour
- setAnimationFrame(anims_name.dsa, vals[7]) -- days remaining
- -- * start variable countdown * --
- function startCountdown()
- -- * if all the vals do not equal one then start the countdown timer! * --
- if vals[1] * vals[2] * vals[3] * vals[4] * vals[5] * vals[6] * vals[7] ~= 1 then startAction(act)
- -- * print some messages to the log file * --
- print('-- * --') -- spacer
- print('timer has been set or resumed from ' .. vals[7]-1 .. ' days, ' .. vals[6]-1 .. vals[5]-1 .. ' hours, ' .. vals[4]-1 .. vals[3]-1 .. ' minutes, ' .. vals[2]-1 .. vals[1]-1 .. ' seconds!')
- print('timer has been started!')
- else
- -- * let's kill the timer! * --
- print('timer has already finished counting down!')
- cond:setValue(VConditionValue, false)
- startAction(act)
- end
- end
- -- * checks the current time values & adapts animation frames based on time values! * --
- function check_ssd_val()
- -- * set & check single second * --
- vals[1] = vals[1] - 1
- if vals[1] <= 0 then vals[1] = 10
- -- * set & check double second * --
- vals[2] = vals[2] - 1
- if vals[2] <= 0 then vals[2] = 6
- -- * set & check single minute * --
- vals[3] = vals[3] - 1
- if vals[3] <= 0 then vals[3] = 10
- -- * set & check double minute * --
- vals[4] = vals[4] - 1
- if vals[4] <= 0 then vals[4] = 6
- -- * set & check single hour * --
- vals[5] = vals[5] - 1
- if vals[5] <= 0 then vals[5] = 10
- -- * set & check double hour * --
- vals[6] = vals[6] - 1
- if vals[6] <= 0 then vals[6] = 3
- -- * set & check days remaining & reset 24 hour countdown * --
- vals[7] = vals[7] - 1
- vals[6] = 3
- vals[5] = 5
- vals[4] = 1
- vals[3] = 1
- vals[2] = 1
- vals[1] = 1
- end end end end end end
- -- * save the values to the editor * --
- day_val:setValue(VValueInt, vals[7]-1)
- hd_val:setValue(VValueInt, vals[6]-1)
- hs_val:setValue(VValueInt, vals[5]-1)
- md_val:setValue(VValueInt, vals[4]-1)
- ms_val:setValue(VValueInt, vals[3]-1)
- sd_val:setValue(VValueInt, vals[2]-1)
- ss_val:setValue(VValueInt, vals[1]-1)
- -- * adapt the frames * --
- for i=1,table.getn(vals) do
- setAnimationFrame(anims_index[i], vals[i])
- end
- -- * let's check to see if the time & days have reached zero? * --
- if vals[1] * vals[2] * vals[3] * vals[4] * vals[5] * vals[6] * vals[7] == 1 then
- cond:setValue(VConditionValue, false)
- print('timer has finished counting down!')
- end
- end
- -- * let's print the current values to the log while saving game! * --
- function endCountdown()
- print('time was saved at: ' .. vals[7]-1 .. ' days, ' .. vals[6]-1 .. vals[5]-1 .. ' hours, ' .. vals[4]-1 .. vals[3]-1 .. ' minutes & ' .. vals[2]-1 .. vals[1]-1 .. ' seconds!')
- print('timer has been saved!')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement