Guest User

Untitled

a guest
May 7th, 2015
237
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local CurTime = CurTime
  2. local UnPredictedCurTime = UnPredictedCurTime
  3. local unpack = unpack
  4. local pairs = pairs
  5. local table = table
  6. local pcall = pcall
  7. local ErrorNoHalt = ErrorNoHalt
  8. local hook = hook
  9. local tostring = tostring
  10.  
  11. --[[---------------------------------------------------------
  12. Name: timer
  13. Desc: A module implementing timed execution of functions.
  14. -----------------------------------------------------------]]
  15. module("timer")
  16.  
  17. -- Some definitions
  18. local PAUSED = -1
  19. local STOPPED = 0
  20. local RUNNING = 1
  21.  
  22. -- Declare our locals
  23. local Timer = {}
  24. local TimerSimple = {}
  25.  
  26. local function CreateTimer( name )
  27. if ( Timer[name] == nil ) then
  28. Timer[name] = {}
  29. Timer[name].Status = STOPPED
  30. return true
  31. end
  32. return false
  33. end
  34.  
  35. --[[---------------------------------------------------------
  36. Name: IsTimer( name )
  37. Desc: Returns boolean whether or not name is a timer.
  38. -----------------------------------------------------------]]
  39. function IsTimer( name )
  40. if ( Timer[name] == nil ) then return false
  41. else return true end
  42. end
  43.  
  44. --[[---------------------------------------------------------
  45. Name: Create( name, delay, reps, func, ... )
  46. Desc: Setup and start a timer by name.
  47. -----------------------------------------------------------]]
  48. function Create( name, delay, reps, func, ... )
  49. if ( IsTimer( name ) ) then
  50. Destroy( name )
  51. end
  52. Adjust( name, delay, reps, func, ... )
  53. Start( name )
  54. end
  55.  
  56. --[[---------------------------------------------------------
  57. Name: Start( name )
  58. Desc: (Re)start the timer by name.
  59. -----------------------------------------------------------]]
  60. function Start( name )
  61. if ( !IsTimer( name ) ) then return false; end
  62. Timer[name].n = 0
  63. Timer[name].Status = RUNNING
  64. Timer[name].Last = CurTime()
  65. return true;
  66. end
  67.  
  68. --[[---------------------------------------------------------
  69. Name: Adjust( name, delay, reps, func, ... )
  70. Desc: Adjust a running, stopped or paused timer by name.
  71. -----------------------------------------------------------]]
  72. function Adjust( name, delay, reps, func, ... )
  73. CreateTimer( name )
  74. Timer[name].Delay = delay
  75. Timer[name].Repetitions = reps
  76. if ( func != nil ) then Timer[name].Func = func end
  77. Timer[name].Args = {...}
  78. return true;
  79. end
  80.  
  81. --[[---------------------------------------------------------
  82. Name: Pause( name )
  83. Desc: Pause a running timer by name.
  84. -----------------------------------------------------------]]
  85. function Pause( name )
  86. if ( !IsTimer( name ) ) then return false; end
  87. if ( Timer[name].Status == RUNNING ) then
  88. Timer[name].Diff = CurTime() - Timer[name].Last
  89. Timer[name].Status = PAUSED
  90. return true
  91. end
  92. return false
  93. end
  94.  
  95. --[[---------------------------------------------------------
  96. Name: UnPause( name )
  97. Desc: Unpause a paused timer by name.
  98. -----------------------------------------------------------]]
  99. function UnPause( name )
  100. if ( !IsTimer( name ) ) then return false; end
  101. if ( Timer[name].Status == PAUSED ) then
  102. Timer[name].Diff = nil
  103. Timer[name].Status = RUNNING
  104. return true
  105. end
  106. return false
  107. end
  108.  
  109. --[[---------------------------------------------------------
  110. Name: Toggle( name )
  111. Desc: Toggle a timer's pause state by name.
  112. -----------------------------------------------------------]]
  113. function Toggle( name )
  114. if ( IsTimer( name ) ) then
  115. if ( Timer[name].Status == PAUSED ) then
  116. return UnPause( name )
  117. elseif ( Timer[name].Status == RUNNING ) then
  118. return Pause( name )
  119. end
  120. end
  121. return false
  122. end
  123.  
  124. --[[---------------------------------------------------------
  125. Name: Stop( name )
  126. Desc: Stop a running or paused timer by name.
  127. -----------------------------------------------------------]]
  128. function Stop( name )
  129. if ( !IsTimer( name ) ) then return false; end
  130. if ( Timer[name].Status != STOPPED ) then
  131. Timer[name].Status = STOPPED
  132. return true
  133. end
  134. return false
  135. end
  136.  
  137. --[[---------------------------------------------------------
  138. Name: Check()
  139. Desc: Check all timers and complete any tasks needed.
  140. This should be run every frame.
  141. -----------------------------------------------------------]]
  142. function Check()
  143.  
  144. for key, value in pairs( Timer ) do
  145.  
  146. if ( value.Status == PAUSED ) then
  147.  
  148. value.Last = CurTime() - value.Diff
  149.  
  150. elseif ( value.Status == RUNNING && ( value.Last + value.Delay ) <= CurTime() ) then
  151.  
  152. value.Last = CurTime()
  153. value.n = value.n + 1
  154.  
  155. local b, e = pcall( value.Func, unpack( value.Args ) )
  156. if ( !b ) then
  157. ErrorNoHalt( "Timer '"..key.."' Error: "..tostring(e).."\n" )
  158. end
  159.  
  160. if ( value.n >= value.Repetitions && value.Repetitions != 0) then
  161. Stop( key )
  162. end
  163.  
  164. end
  165.  
  166. end
  167.  
  168. -- Run Simple timers
  169. for key, value in pairs( TimerSimple ) do
  170.  
  171. if ( value.Finish <= CurTime() ) then
  172.  
  173. local b, e = pcall( value.Func, unpack( value.Args ) )
  174. if ( !b ) then
  175. ErrorNoHalt( "Simple Timer Error: "..tostring(e).."\n" )
  176. end
  177.  
  178. table.remove( TimerSimple, key )
  179.  
  180. end
  181. end
  182.  
  183. end
  184.  
  185. --[[---------------------------------------------------------
  186. Name: Destroy( name )
  187. Desc: Destroy the timer by name and remove all evidence.
  188. -----------------------------------------------------------]]
  189. function Destroy( name )
  190. Timer[ name ] = nil
  191. end
  192.  
  193. Remove = Destroy
  194.  
  195. --[[---------------------------------------------------------
  196. Name: Simple( delay, func, ... )
  197. Desc: Make a simple "create and forget" timer
  198. -----------------------------------------------------------]]
  199. function Simple( delay, func, ... )
  200.  
  201. local new_timer = {}
  202.  
  203. new_timer.Finish = UnPredictedCurTime() + delay
  204.  
  205. if ( func != nil ) then new_timer.Func = func end
  206. new_timer.Args = {...}
  207.  
  208. table.insert( TimerSimple, new_timer )
  209.  
  210. return true;
  211.  
  212. end
  213.  
  214. hook.Add( "Think", "CheckTimers", Check )
RAW Paste Data