Advertisement
AFRLme

animated 24 hour per day countdown timer (day:hh:mm:ss) [VS]

Jan 11th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.04 KB | None | 0 0
  1. --[[
  2. fully animated 24 hour per day (day:hh:mm:ss) countdown timer!
  3. credit to divo for shortening the code by half & showing me how to use tables!
  4. written & annotated by AFRLme  
  5. --]]
  6.  
  7. -- * local variable which contains the condition for timer_is_on? * --
  8. local cond = getObject('Conditions[timer_is_on?]')
  9.  
  10. -- * local variable which contains the action to check the current time! * --
  11. local act = getObject('Actions[check_time]')
  12.  
  13. -- * let's create the function to find the first & last frames of an active animation! * --
  14. local function setAnimationFrame(anim, frame)
  15.  anim:setValue(VAnimationFirstFrame, frame)
  16.  anim:setValue(VAnimationLastFrame, frame)
  17. end
  18.  
  19. -- * let's get the current active animations for displaying the timer! * --
  20. local anims_name = {
  21.  -- * global variable for getting single second digit animations * --
  22.  ssa = getObject('ActiveAnimations[ssa]'),
  23.  -- * global variable for getting double second digit animations * --
  24.  sda = getObject('ActiveAnimations[sda]'),
  25.  -- * global variable for getting single minute digit animations * --
  26.  msa = getObject('ActiveAnimations[msa]'),
  27.  -- * global variable for getting double minute digit animations * --
  28.  mda = getObject('ActiveAnimations[mda]'),
  29.  -- * global variable for getting single hour digit animations * --
  30.  hsa = getObject('ActiveAnimations[hsa]'),
  31.  -- * global variable for getting double hour digit animations * --
  32.  hda = getObject('ActiveAnimations[hda]'),
  33.  -- * global variable for getting the remaining day animation * --
  34.  dsa = getObject('ActiveAnimations[dsa]')
  35. }
  36.  
  37. -- * get global value for days remaining! * --
  38. local day_val = getObject('Values[sin_day_val]')
  39. -- * get global value for double hours! * --
  40. local hd_val = getObject('Values[dbl_hour_val]')
  41. -- * get global value for single hours! * --
  42. local hs_val = getObject('Values[sin_hour_val]')
  43. -- * get global value for double minutes! * --
  44. local md_val = getObject('Values[dbl_min_val]')
  45. -- * get global value for single minutes! * --
  46. local ms_val = getObject('Values[sin_min_val]')
  47. -- * get global value for double seconds! * --
  48. local sd_val = getObject('Values[dbl_sec_val]')
  49. -- * get global value for single seconds! * --
  50. local ss_val = getObject('Values[sin_sec_val]')
  51.  
  52. -- * global variable defaults for timer display value * --
  53. 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) }
  54.  
  55. -- * index table list for retrieving animations * --
  56. local anims_index = { anims_name.ssa, anims_name.sda, anims_name.msa, anims_name.mda, anims_name.hsa, anims_name.hda, anims_name.dsa }
  57.  
  58. -- * let's add some countermeasures to auto-correct values that are too high or low! * --
  59. if vals[6] >= 2 and vals[5] >= 4 then
  60.  vals[1] = 1 -- single second = 0
  61.  vals[2] = 1 -- double second = 0
  62.  vals[3] = 1 -- single minute = 0
  63.  vals[4] = 1 -- double minute = 0
  64.  vals[5] = 5 -- single hour = 4
  65.  vals[6] = 3 -- double hour = 2
  66.  if vals[7] > 9 then vals[7] = 10 end -- days remaining = 9
  67.  if vals[7] < 0 then vals[7] = 1 end -- days remaining = 0
  68.  if vals[7] >= 0 and vals[7] <= 9 then vals[7] = vals[7] + 1 end -- days remaining = days remaining + 1
  69. else
  70.  -- * let's check if any values are greater than default maximum value! * --
  71.  if vals[1] > 9 then vals[1] = 9 end -- single second = 9
  72.  if vals[2] > 5 then vals[2] = 5 end -- double second = 5
  73.  if vals[3] > 9 then vals[3] = 9 end -- single minute = 9
  74.  if vals[4] > 5 then vals[4] = 5 end -- double minute = 5
  75.  if vals[6] >= 2 then if vals[5] > 3 then vals[5] = 3 end end -- single hour = 3
  76.  if vals[6] >= 0 and vals[6] < 2 then if vals[5] > 9 then vals[5] = 9 end end -- else single hour = 9
  77.  if vals[7] > 9 then vals[7] = 9 end -- days remaining = 0
  78.  -- * let's check if any values are less than or equal to zero! * --
  79.  if vals[1] < 0 then vals[1] = 0 end -- single second = 0
  80.  if vals[2] < 0 then vals[2] = 0 end -- double second = 0
  81.  if vals[3] < 0 then vals[3] = 0 end -- single minute = 0
  82.  if vals[4] < 0 then vals[4] = 0 end -- double minute = 0
  83.  if vals[5] < 0 then vals[5] = 0 end -- single hour = 0
  84.  if vals[6] < 0 then vals[6] = 0 end -- double hour = 0
  85.  if vals[7] < 0 then vals[7] = 0 end -- days remaining = 0
  86.  -- * let's double check our maths! * --
  87.  if vals[1] >= 0 and vals[1] <= 9 then vals[1] = vals[1] + 1 end -- single second = single second + 1
  88.  if vals[2] >= 0 and vals[2] <= 5 then vals[2] = vals[2] + 1 end -- double second = double second + 1
  89.  if vals[3] >= 0 and vals[3] <= 9 then vals[3] = vals[3] + 1 end -- single minute = single minute + 1
  90.  if vals[4] >= 0 and vals[4] <= 5 then vals[4] = vals[4] + 1 end -- double minute = double minute + 1
  91.  if vals[5] >= 0 and vals[5] <= 9 then vals[5] = vals[5] + 1 end -- single hour = single hour + 1
  92.  if vals[6] >= 0 and vals[6] <= 2 then vals[6] = vals[6] + 1 end -- double hour = double hour + 1
  93.  if vals[7] >= 0 and vals[7] <= 9 then vals[7] = vals[7] + 1 end -- days remaining = days remaining + 1
  94. end
  95.  
  96. -- * set each animation frame to current time values found in timer_interface value tab! * --
  97. setAnimationFrame(anims_name.ssa, vals[1]) -- single second
  98. setAnimationFrame(anims_name.sda, vals[2]) -- double second
  99. setAnimationFrame(anims_name.msa, vals[3]) -- single minute
  100. setAnimationFrame(anims_name.mda, vals[4]) -- double minute
  101. setAnimationFrame(anims_name.hsa, vals[5]) -- single hour
  102. setAnimationFrame(anims_name.hda, vals[6]) -- double hour
  103. setAnimationFrame(anims_name.dsa, vals[7]) -- days remaining
  104.  
  105. -- * start variable countdown * --
  106. function startCountdown()
  107.  -- * if all the vals do not equal one then start the countdown timer! * --
  108.  if vals[1] * vals[2] * vals[3] * vals[4] * vals[5] * vals[6] * vals[7] ~= 1 then startAction(act)
  109.   -- * print some messages to the log file * --
  110.   print('-- * --') -- spacer
  111.   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!')
  112.   print('timer has been started!')
  113.  else
  114.   -- * let's kill the timer! * --
  115.   print('timer has already finished counting down!')
  116.   cond:setValue(VConditionValue, false)
  117.   startAction(act)
  118.  end
  119. end
  120.  
  121. -- * checks the current time values & adapts animation frames based on time values! * --
  122. function check_ssd_val()
  123.  -- * set & check single second * --
  124.  vals[1] = vals[1] - 1
  125.  if vals[1] <= 0 then vals[1] = 10
  126.  -- * set & check double second * --
  127.  vals[2] = vals[2] - 1
  128.  if vals[2] <= 0 then vals[2] = 6
  129.  -- * set & check single minute * --
  130.  vals[3] = vals[3] - 1
  131.  if vals[3] <= 0 then vals[3] = 10
  132.  -- * set & check double minute * --
  133.  vals[4] = vals[4] - 1
  134.  if vals[4] <= 0 then vals[4] = 6
  135.  -- * set & check single hour * --
  136.  vals[5] = vals[5] - 1
  137.  if vals[5] <= 0 then vals[5] = 10
  138.  -- * set & check double hour * --
  139.  vals[6] = vals[6] - 1
  140.  if vals[6] <= 0 then vals[6] = 3
  141.  -- * set & check days remaining & reset 24 hour countdown * --
  142.  vals[7] = vals[7] - 1
  143.  vals[6] = 3
  144.  vals[5] = 5
  145.  vals[4] = 1
  146.  vals[3] = 1
  147.  vals[2] = 1
  148.  vals[1] = 1
  149.  end end end end end end
  150.  
  151.  -- * save the values to the editor * --
  152.  day_val:setValue(VValueInt, vals[7]-1)
  153.  hd_val:setValue(VValueInt, vals[6]-1)
  154.  hs_val:setValue(VValueInt, vals[5]-1)
  155.  md_val:setValue(VValueInt, vals[4]-1)
  156.  ms_val:setValue(VValueInt, vals[3]-1)
  157.  sd_val:setValue(VValueInt, vals[2]-1)
  158.  ss_val:setValue(VValueInt, vals[1]-1)
  159.  
  160.  -- * adapt the frames * --
  161.  for i=1,table.getn(vals) do
  162.   setAnimationFrame(anims_index[i], vals[i])
  163.  end
  164.  
  165.  -- * let's check to see if the time & days have reached zero? * --
  166.  if vals[1] * vals[2] * vals[3] * vals[4] * vals[5] * vals[6] * vals[7] == 1 then
  167.    cond:setValue(VConditionValue, false)
  168.    print('timer has finished counting down!')
  169.  end
  170. end
  171.  
  172. -- * let's print the current values to the log while saving game! * --
  173. function endCountdown()
  174.  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!')
  175.  print('timer has been saved!')
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement