Advertisement
bystander36

Loop script

Jan 24th, 2018
2,871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.31 KB | None | 0 0
  1. function _OnActivated()
  2.     Task_Loop = NewTask(LoopFunction)
  3. end
  4.  
  5. function _OnEvent(event, arg, family)
  6.     if event == "MOUSE_BUTTON_PRESSED" and arg == 7 then
  7.         ToggleLoop = not ToggleLoop
  8.         if ToggleLoop then
  9.             PressKey("f")
  10.             Task_Loop.SetRepeat(true)
  11.             Task_Loop.Start()
  12.         else
  13.             Task_Loop.SetRepeat(false)
  14.         end
  15.     end
  16. end
  17.  
  18. function LoopFunction()
  19.     PressMouseButton(3)
  20.     TaskSleep(500)
  21.     MoveMouseRelative(600, 0)
  22.     TaskSleep(500)
  23.     ReleaseMouseButton(3)
  24.     TaskSleep(6000)
  25.     PressKey("w")
  26.     TaskSleep(350)
  27.     ReleaseKey("w")
  28.     if not Task_Loop.GetRepeat() then
  29.         TaskSleep(300)
  30.         ReleaseKey("f")
  31.     end
  32. end
  33.  
  34.  
  35. ------------------------------------------------------------------------------------------------------
  36. -- Task Class
  37. ------------------------------------------------------------------------------------------------------
  38. -- NewTask(func, ...)   returns a new object, and creates the coroutine with given func and variables
  39. --  .ChangeVars(...)    Change vars to new vars for next .Execute()
  40. --  .SetRepeat(boolean) Sets repeat status to true or false.  If repeat, when the task ends, it restarts.
  41. --  .GetRepeat()    returns if Repeat is set to true or false.
  42. --  .Create()       creates the task, but is not running yet.
  43. --  .Start()        creates if not already created and at the start, then sets the task to Run.
  44. --  .Stop()         stops and finishes the task.  It cannot be resumed.
  45. --  .IsCreated()    checks if the Task's coroutine exists (self._Co ~= nil)
  46. --  .IsRunning()    checks if the Task's is created, if it is running, and not dead (finished running)
  47. --  .IsAtStart()    checks if the Task is created but has not yet run.
  48. --  .IsAtEnd()      checks if the Task has completed, and still exists.
  49. --  .GetStatus()    returns the status of the coroutine, or nil if it doesn't exist
  50. --  .Pause()        stops running the task, but does not destroy it.
  51. --  .Resume()       resumes the task where it left off.
  52. --  .Execute()      run the Task with the given variables.  This should be run every event to keep the task running
  53. --  .Destroy()      stops and removes the Task.  It remove from the TaskHandler and nilled.
  54. --  .Remove()       calls .Destroy()
  55. -- TaskSleep(delay) This will yield within a Task function with the given delay.  Execution will pick up where it left off after the delay.
  56. ------------------------------------------------------------------------------------------------------
  57.  
  58. -------------------------------------------------
  59. --  The following is for polling.  Do not alter.
  60. -------------------------------------------------
  61. _StartUpParameters = {
  62.     PollDevice = "mouse",
  63.     PollDelay = 10,
  64.     AutoTaskSleep = false,
  65. }
  66. function _PreEvent() end
  67. function _PostEvent()
  68.     _TaskHandler.Execute()
  69. end
  70. function OnEvent(event, arg, family)
  71.     if event == "PROFILE_ACTIVATED" then
  72.         _TaskHandler = InitTaskHandler()
  73.         Poll = InitPolling(_StartUpParameters.PollDelay, _StartUpParameters.PollDevice, _PreEvent, _PostEvent)
  74.     end
  75.     Poll.Execute(event, arg, family)
  76. end
  77.  
  78. ----------------------------
  79. -- Polling Class
  80. ----------------------------
  81. function InitPolling(PollDelay, PollDevice, PreOnEventFunc, PostOnEventFunc)
  82.     local self = {
  83.         PollDelay = PollDelay,
  84.         PollDevice = PollDevice,
  85.         PreOnEventFunc = PreOnEventFunc,
  86.         PostOnEventFunc = PostOnEventFunc,
  87.         Sleep = Sleep_hook,
  88.     }
  89.     local function CreateEvent() SetMKeyState(1, self.PollDevice) end
  90.     local function OnEvent(event, arg, family)
  91.         if self.PreOnEventFunc then self.PreOnEventFunc() end
  92.         _OnEvent(event, arg, family)
  93.         if self.PostOnEventFunc then self.PostOnEventFunc() end
  94.     end
  95.     function self.Execute(event, arg, family)
  96.         if event == "PROFILE_ACTIVATED" then
  97.             if _OnActivated then _OnActivated(event, arg, family) end
  98.             OnEvent(event, arg, family)
  99.             CreateEvent()                                   -- initiates the first polling event
  100.         elseif event == "M_RELEASED" and family == self.PollDevice then
  101.             OnEvent("POLLING", 0, self.PollDevice)
  102.             CreateEvent()
  103.             self.Sleep(self.PollDelay)
  104.         elseif event == "M_PRESSED" and family == self.PollDevice then
  105.             OnEvent("POLLING", 0, self.PollDevice)
  106.             self.Sleep(self.PollDelay)
  107.         elseif event == "PROFILE_DEACTIVATED" then
  108.             if _OnDeactivated then  _OnDeactivated(event, arg, family) end
  109.         else
  110.             OnEvent(event, arg, family)
  111.         end
  112.     end
  113.     function self.SetPreOnEventFunc(func) self.PreOnEventFunc = func end
  114.     function self.SetPostOnEventFunc(func) self.PosOnEventFunc = func end
  115.     return self
  116. end
  117.  
  118. ------------------------
  119. -- Task Class
  120. ------------------------
  121. function TaskSleep(delay) return coroutine.yield(delay) end
  122. function NewTask(func, ...)
  123.     local self = {
  124.         _Func = func,
  125.         _Running = false,
  126.         _Co = nil,
  127.         _ResumeRunningTime = -1,
  128.         _AtStart = false,
  129.         _Repeat = false,
  130.         _Vars = nil,
  131.         _TH = _TaskHandler or nil,
  132.     }
  133.     function self.ChangeVars(...)   self._Vars = { ... } end
  134.     function self.SetRepeat(r)  self._Repeat = r end
  135.     function self.GetRepeat()   return self._Repeat end
  136.     function self.Create()
  137.         self._ResumeRunningTime = -1
  138.         self._Running = false
  139.         self._Co = coroutine.create(self._Func)
  140.         self._AtStart = true
  141.     end
  142.     function self.Start()
  143.         if not self.IsAtStart() or not self.IsCreated() then
  144.             self.Create()
  145.         end
  146.         self._Running = true
  147.     end
  148.     function self.Stop() self._Running = false; self._Co = nil end
  149.     function self.GetStatus()
  150.         if self._Co then return coroutine.status(self._Co)
  151.         else return nil end
  152.     end
  153.     function self.IsAtStart() return self._AtStart end
  154.     function self.IsAtEnd() return self.IsDead() end
  155.     function self.IsCreated()
  156.         if self._Co then return true
  157.         else return false   end
  158.     end
  159.     function self.IsDead()
  160.         if self._Co and self.GetStatus() == "dead" then return true
  161.         else return false   end
  162.     end
  163.     function self.IsRunning()
  164.         if self.IsCreated() and self._Running and not self.IsDead() then return true
  165.         else return false end
  166.     end
  167.     function self.IsReady()
  168.         if self._Running and self.IsCreated() and not self.IsDead()
  169.             and self._ResumeRunningTime <= GetRunningTime() then
  170.             return true
  171.         else return false end
  172.     end
  173.     function self.Pause() self._Running = false end
  174.     function self.Resume() self._Running = true end
  175.     function self.Execute()
  176.         if self.GetRepeat() and self.IsDead() and self._Running then self.Start() end
  177.         if self.IsReady() then
  178.             local status, delay = coroutine.resume(self._Co, unpack(self._Vars))
  179.             self._AtStart = false
  180.             if delay then self._ResumeRunningTime = delay + GetRunningTime()
  181.             else self._ResumeRunningTime = -1 end
  182.             return status
  183.         end
  184.     end
  185.     function self.Destroy()
  186.         if self._TH then self._TH.RemoveTask(self) end
  187.         self = nil
  188.         return nil
  189.     end
  190.     function self.Remove() self.Destroy() end
  191.     self.ChangeVars(...)
  192.     self.Create()
  193.     if self._TH then self._TH.AddTask(self) end
  194.     return self
  195. end
  196.  
  197. --------------------------
  198. --  TaskHandler
  199. --------------------------
  200. function InitTaskHandler()
  201.     local self = {  _TaskList = {}, }
  202.     function self.AddTask(Task) self._TaskList[Task] = true end
  203.     function self.RemoveTask(Task) self._TaskList[Task] = nil end
  204.     function self.Execute()
  205.         for k,v in pairs(self._TaskList) do k.Execute() end
  206.     end
  207.     return self
  208. end
  209. coroutine.running_hook = coroutine.running
  210. function coroutine.running()
  211.     local v = coroutine.running_hook()
  212.     return v
  213. end
  214. Sleep_hook = Sleep
  215. function Sleep(d)
  216.     if _StartUpParameters.AutoTaskSleep and coroutine.running() then return TaskSleep(d)
  217.     else return Sleep_hook(d) end
  218. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement