disable the flight stuff for now, we'll fix it after thanksgiving
[QuestHelper.git] / timeslice.lua
blob71e7e0de645495597c3d24249fb6ff1fd9cccf66
1 QuestHelper_File["timeslice.lua"] = "Development Version"
3 -- Any non-local item here is part of an available public interface.
5 local coroutine_running = false
6 local coroutine_stop_time = 0
7 local coroutine_list = {}
8 local coroutine_route_pass = 1
10 local coroutine_verbose = false
12 local coroutine_time_used = {}
13 local coroutine_power_up = GetTime()
15 local coroutine_time_exceeded = 0
17 function QH_Timeslice_DumpPerf()
18 local sortable = {}
19 for k, v in pairs(coroutine_time_used) do
20 table.insert(sortable, {name = k, amount = v})
21 end
22 table.sort(sortable, function(a, b) return a.name < b.name end)
23 for _, v in pairs(sortable) do
24 QuestHelper:TextOut(string.format("%s: %f", QuestHelper:HighlightText(v.name), v.amount))
25 end
26 QuestHelper:TextOut(string.format("%s: %f", QuestHelper:HighlightText("poweron"), GetTime() - coroutine_power_up))
27 end
29 function QH_Timeslice_Yield()
30 if coroutine_running then
31 -- Check if we've run our alotted time
32 if GetTime() > coroutine_stop_time then
33 -- As a safety, reset stop time to 0. If somehow we fail to set it next time,
34 -- we'll be sure to yield promptly.
35 coroutine_stop_time = 0
36 coroutine.yield()
37 end
38 end
39 end
41 function QH_Timeslice_Bonus(quantity)
42 if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %d bonus", quantity)) end
43 coroutine_route_pass = coroutine_route_pass + quantity
44 end
46 local prioritize = {
47 criteria = 10,
48 lzw = -5,
49 routing = -10,
52 function QH_Timeslice_Add(workfunc, name)
53 local priority = prioritize[name] or 0
54 if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %s added (%s, %d)", name, tostring(workfunc), priority)) end
55 local ncoro = coroutine.create(workfunc)
56 QuestHelper: Assert(ncoro)
57 table.insert(coroutine_list, {priority = priority, name = name, coro = ncoro, active = true})
58 end
60 function QH_Timeslice_Toggle(name, flag)
61 --if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %s toggled to %s", name, tostring(not not flag))) end
62 for _, v in pairs(coroutine_list) do
63 if v.name == name then v.active = flag end
64 end
65 end
67 function QH_Timeslice_Work()
68 -- There's probably a better way to do this, but. Eh. Lua.
69 coro = nil
70 key = nil
71 for k, v in pairs(coroutine_list) do
72 if v.active and (not coro or v.priority > coro.priority) then coro = v; key = k end
73 end
75 if coro then
76 --if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %s running", coro.name)) end
78 QuestHelper: Assert(coroutine.status(coro.coro) ~= "dead")
79 local coroutine_intended_stop_time = GetTime() + 4e-3 * (QuestHelper_Pref.hide and 0.01 or (QuestHelper_Pref.perf_scale * math.min(coroutine_route_pass, 5)))
80 coroutine_stop_time = coroutine_intended_stop_time - coroutine_time_exceeded
81 coroutine_route_pass = coroutine_route_pass - 5
82 if coroutine_route_pass <= 0 then coroutine_route_pass = 1 end
84 local start = GetTime()
85 local state, err = true, nil -- default values for "we're fine"
86 if start < coroutine_stop_time then -- We don't want to just return on failure because we want to credit the exceeded time properly.
87 coroutine_running = true
88 state, err = coroutine.resume(coro.coro)
89 coroutine_running = false
90 end
91 local total = GetTime() - start
93 local coroutine_this_cycle_exceeded = GetTime() - coroutine_intended_stop_time -- may be either positive or negative
94 coroutine_time_exceeded = coroutine_time_exceeded + coroutine_this_cycle_exceeded
96 coroutine_time_used[coro.name] = (coroutine_time_used[coro.name] or 0) + total
98 if not state then
99 if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %s errored", coro.name)) end
100 QuestHelper_ErrorCatcher_ExplicitError(err, "", string.format("(Coroutine error in %s)\n", coro.name))
103 QuestHelper: Assert(coro.coro)
104 if coroutine.status(coro.coro) == "dead" then
105 if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: %s complete", coro.name)) end
106 coroutine_list[key] = nil
108 else
109 if coroutine_verbose then QuestHelper:TextOut(string.format("timeslice: no available tasks")) end
113 function QH_Timeslice_Increment(quantity, name)
114 local an = "(nc) " .. name
115 coroutine_time_used[an] = (coroutine_time_used[an] or 0) + quantity