· 6 years ago · Mar 13, 2019, 09:20 PM
1DiscoDelay = 300000 -- Wait 5:00 minutes before doing Songs & Dances
2DiscoTime = 10000 -- Wait 10 seconds for Songs & Dances are done
3
4BuffTime = 25000 -- Wait 25 seconds for buffs are done
5
6FirstDelay = 285000 -- Wait 4:45 minutes before doing 2nd Songs & Dances
7LastDelay = 240000 -- Wait 4:00 minutes before doing a full rebuff
8
9COVDelay = 601000 -- Wait 10:01 minutes before doing CoV
10
11DiscoTriggerIsActive = false
12COVTriggerIsActive = false
13
14log = OutputLogMessage
15
16function _OnEvent(event, arg, family)
17 if event == "G_PRESSED" and arg == 20 and not DiscoTriggerIsActive then
18 AutoClick_Task.Start()
19 DiscoTriggerIsActive = true
20 elseif event == "G_PRESSED" and arg == 20 and DiscoTriggerIsActive then
21 DiscoTriggerIsActive = false
22 AutoClick_Task.Stop()
23 AbortMacro()
24
25 --elseif event == "G_PRESSED" and arg == 21 and not COVTriggerIsActive then
26 -- Macro_Task.Start()
27 -- COVTriggerIsActive = true
28 --elseif event == "G_PRESSED" and arg == 21 and COVTriggerIsActive then
29 -- COVTriggerIsActive = false
30 -- Macro_Task.Stop()
31
32 --elseif event == "G_PRESSED" and arg == 14 then
33 -- AutoToggle = not AutoToggle
34 -- log("AutoToggle = %s\n", tostring(AutoToggle))
35 end
36end
37
38function _OnActivated(event, arg, family)
39 AutoClick_Task = NewTask(AutoClick)
40 Macro_Task = NewTask(Macro)
41end
42
43
44
45function AutoClick()
46 while true do
47 -- Auto Attack
48 PlayMacro("AUTO")
49
50 -- Wait 5 min
51 TaskSleep(DiscoDelay)
52
53 -- Abort all running macros
54 AbortMacro()
55
56 -- Songs & Dances
57 PlayMacro("Songs & Dances")
58
59 -- Wait 10 s for Songs & Dances to finish
60 TaskSleep(DiscoTime)
61 end
62end
63
64function Macro()
65 while true do
66 -- Buff CoV
67 PlayMacro("COV")
68
69 -- Wait 10 min
70 TaskSleep(COVDelay)
71 end
72end
73
74
75------------------------------------------------------------------------------------------------------
76-- Task Class
77------------------------------------------------------------------------------------------------------
78-- NewTask(func, ...) returns a new object, and creates the coroutine with given func and variables
79-- .ChangeVars(...) Change vars to new vars for next .Execute()
80-- .SetRepeat(boolean) Sets repeat status to true or false. If repeat, when the task ends, it restarts.
81-- .GetRepeat() returns if Repeat is set to true or false.
82-- .Create() creates the task, but is not running yet.
83-- .Start() creates if not already created and at the start, then sets the task to Run.
84-- .Stop() stops and finishes the task. It cannot be resumed.
85-- .IsCreated() checks if the Task's coroutine exists (self._Co ~= nil)
86-- .IsRunning() checks if the Task's is created, if it is running, and not dead (finished running)
87-- .IsAtStart() checks if the Task is created but has not yet run.
88-- .IsAtEnd() checks if the Task has completed, and still exists.
89-- .GetStatus() returns the status of the coroutine, or nil if it doesn't exist
90-- .Pause() stops running the task, but does not destroy it.
91-- .Resume() resumes the task where it left off.
92-- .Execute() run the Task with the given variables. This should be run every event to keep the task running
93-- .Destroy() stops and removes the Task. It remove from the TaskHandler and nilled.
94-- .Remove() calls .Destroy()
95-- TaskSleep(delay) This will yield within a Task function with the given delay. Execution will pick up where it left off after the delay.
96-- PressKey(key, tag) Presses key, if no tag given, it will be saved under the coroutine running, or "generic" if not in a coroutine
97-- ReleaseKey(key) Releases key, and clears it from tracking table.
98-- ReleaseAllKeys(tag) If tag, releases all keys pressed within a coroutine handler, or specific handler given. If no tag is given, releases all keys.
99------------------------------------------------------------------------------------------------------
100
101-------------------------------------------------
102-- The following is for polling. Do not alter.
103-------------------------------------------------
104_StartUpParameters = {
105 PollDevice = "lhc",
106 PollDelay = 50,
107 AutoTaskSleep = false,
108}
109function _PreEvent() end
110function _PostEvent()
111 _TaskHandler.Execute()
112end
113function OnEvent(event, arg, family)
114 if event == "PROFILE_ACTIVATED" then
115 _TaskHandler = InitTaskHandler()
116 Poll = InitPolling(_StartUpParameters.PollDelay, _StartUpParameters.PollDevice, _PreEvent, _PostEvent)
117 end
118 Poll.Execute(event, arg, family)
119end
120
121----------------------------
122-- Polling Class
123----------------------------
124function InitPolling(PollDelay, PollDevice, PreOnEventFunc, PostOnEventFunc)
125 local self = {
126 PollDelay = PollDelay,
127 PollDevice = PollDevice,
128 PreOnEventFunc = PreOnEventFunc,
129 PostOnEventFunc = PostOnEventFunc,
130 Sleep = Sleep_hook,
131 }
132 local function CreateEvent() SetMKeyState(1, self.PollDevice) end
133 local function OnEvent(event, arg, family)
134 if self.PreOnEventFunc then self.PreOnEventFunc() end
135 _OnEvent(event, arg, family)
136 if self.PostOnEventFunc then self.PostOnEventFunc() end
137 end
138 function self.Execute(event, arg, family)
139 if event == "PROFILE_ACTIVATED" then
140 if _OnActivated then _OnActivated(event, arg, family) end
141 OnEvent(event, arg, family)
142 CreateEvent() -- initiates the first polling event
143 elseif event == "M_RELEASED" and family == self.PollDevice then
144 OnEvent("POLLING", 0, self.PollDevice)
145 CreateEvent()
146 self.Sleep(self.PollDelay)
147 elseif event == "M_PRESSED" and family == self.PollDevice then
148 OnEvent("POLLING", 0, self.PollDevice)
149 self.Sleep(self.PollDelay)
150 elseif event == "PROFILE_DEACTIVATED" then
151 if _OnDeactivated then _OnDeactivated(event, arg, family) end
152 else
153 OnEvent(event, arg, family)
154 end
155 end
156 function self.SetPreOnEventFunc(func) self.PreOnEventFunc = func end
157 function self.SetPostOnEventFunc(func) self.PosOnEventFunc = func end
158 return self
159end
160
161------------------------
162-- Task Class
163------------------------
164function TaskSleep(delay) return coroutine.yield(delay) end
165function NewTask(func, ...)
166 local self = {
167 _Func = func,
168 _Running = false,
169 _Co = nil,
170 _ResumeRunningTime = -1,
171 _AtStart = false,
172 _Repeat = false,
173 _Vars = nil,
174 _TH = _TaskHandler or nil,
175 }
176 function self.ChangeVars(...) self._Vars = { ... } end
177 function self.SetRepeat(r) self._Repeat = r end
178 function self.GetRepeat() return self._Repeat end
179 function self.Create()
180 self._ResumeRunningTime = -1
181 self._Running = false
182 self._Co = coroutine.create(self._Func)
183 self._AtStart = true
184 end
185 function self.Start()
186 if not self.IsAtStart() or not self.IsCreated() then
187 self.Create()
188 end
189 self._Running = true
190 end
191 function self.Stop() self._Running = false; self._Co = nil end
192 function self.GetStatus()
193 if self._Co then return coroutine.status(self._Co)
194 else return nil end
195 end
196 function self.IsAtStart() return self._AtStart end
197 function self.IsAtEnd() return self.IsDead() end
198 function self.IsCreated()
199 if self._Co then return true
200 else return false end
201 end
202 function self.IsDead()
203 if self._Co and self.GetStatus() == "dead" then return true
204 else return false end
205 end
206 function self.IsRunning()
207 if self.IsCreated() and self._Running and not self.IsDead() then return true
208 else return false end
209 end
210 function self.IsReady()
211 if self._Running and self.IsCreated() and not self.IsDead()
212 and self._ResumeRunningTime <= GetRunningTime() then
213 return true
214 else return false end
215 end
216 function self.Pause() self._Running = false end
217 function self.Resume() self._Running = true end
218 function self.Execute()
219 if self.GetRepeat() and self.IsDead() and self._Running then self.Start() end
220 if self.IsReady() then
221 local status, delay = coroutine.resume(self._Co, unpack(self._Vars))
222 self._AtStart = false
223 if delay then self._ResumeRunningTime = delay + GetRunningTime()
224 else self._ResumeRunningTime = -1 end
225 return status
226 end
227 end
228 function self.Destroy()
229 if self._TH then self._TH.RemoveTask(self) end
230 self = nil
231 return nil
232 end
233 function self.Remove() self.Destroy() end
234 self.ChangeVars(...)
235 self.Create()
236 if self._TH then self._TH.AddTask(self) end
237 return self
238end
239
240--------------------------
241-- TaskHandler
242--------------------------
243function InitTaskHandler()
244 local self = { _TaskList = {}, }
245 function self.AddTask(Task) self._TaskList[Task] = true end
246 function self.RemoveTask(Task) self._TaskList[Task] = nil end
247 function self.Execute()
248 for k,v in pairs(self._TaskList) do k.Execute() end
249 end
250 return self
251end
252coroutine.running_hook = coroutine.running
253function coroutine.running()
254 local v = coroutine.running_hook()
255 return v
256end
257Sleep_hook = Sleep
258function Sleep(d)
259 if _StartUpParameters.AutoTaskSleep and coroutine.running() then return TaskSleep(d)
260 else return Sleep_hook(d) end
261end
262_KeysPressed = {} -- Keys pressed from within a coroutine. CO-KeysPressed[ct][key]
263
264PressKey_hook = PressKey
265ReleaseKey_hook = ReleaseKey
266
267function PressKey(k, tag)
268 local t = tag or coroutine.running() or "generic"
269
270 if not _KeysPressed[t] then
271 _KeysPressed[t] = {}
272 end
273 _KeysPressed[t][k] = true -- set the key to true, meaning it is pressed
274
275 PressKey_hook(k)
276end
277
278function ReleaseKey(k) -- releases the key, and nil's the key in all locations of _KeysPressed
279 ReleaseKey_hook(k)
280
281 for key, value in pairs(_KeysPressed) do
282 _KeysPressed[key][k] = nil
283 end
284end
285
286function ReleaseAllKeys(tag)
287
288 if _KeysPressed[tag] then
289 for key, value in pairs(_KeysPressed[tag]) do
290 ReleaseKey(key)
291 end
292 elseif not tag then
293 for key, value in pairs(_KeysPressed) do
294 for k, v in pairs(_KeysPressed[key]) do
295 ReleaseKey(k)
296 end
297 end
298 end
299end