Advertisement
bystander36

Task repeating

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