forked from miellaby/renabomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaitFor.lua
225 lines (192 loc) · 6.1 KB
/
waitFor.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
require('json')
-- Module to wait for seconds and signals
waitFor = {
-- few signals
HELLO = {},
BEGIN = {},
PROGRESS = {},
POKE = {},
END = {},
BYEBYE = {},
TIMEOUT = {},
-- coroutines which waits for some delay
waitingOnTime = {},
-- coroutines which wait for some signal
waitingOnSignal = {},
-- coroutines which waits for some condition
waitingOnCondition = {},
-- signals waited by coroutines
waitedSignals = {},
-- time
now = 0,
-- usage waitFor.seconds(seconds)
seconds = function(seconds)
-- print("wait seconds", seconds)
local co = coroutine.running()
assert(co ~= nil, "The main thread cannot wait!")
-- store coroutine into the waitingOnTime table
waitFor.waitingOnTime[co] = waitFor.now + seconds
return coroutine.yield(co)
end,
-- usage waitFor.signal(signal, seconds)
signal = function(signal, during, condition)
-- print("wait signal", signal.label or signal, during, condition and "condition" or "")
local co = coroutine.running()
assert(co ~= nil, "The main thread cannot wait!")
assert(signal ~= nil, "signal is nil!")
-- store coroutine into the waitedSignals table
waitFor.waitedSignals[co] = signal
-- store coroutine into the waitingOnSignal table
if waitFor.waitingOnSignal[signal] == nil then
waitFor.waitingOnSignal[signal] = {}
end
waitFor.waitingOnSignal[signal][co] = co
-- yield or redirect to waitFor.seconds()
if (condition ~= nil) then
return waitFor.condition(condition, during)
elseif (during ~= nil) then
return waitFor.seconds(during)
else
return coroutine.yield()
end
end,
condition = function(c, during)
-- print("waitFor condition", c, during)
-- not yielding if condition is already verified
local check0 = c()
if check0 then
local co = coroutine.running()
-- if coroutine was waiting for a timer or signal, also forget it
local waitedSignal = waitFor.waitedSignals[co]
if waitedSignal ~= nil then
waitFor.waitingOnSignal[waitedSignal][co] = nil
end
waitFor.waitingOnCondition[co] = nil
waitFor.waitedSignals[co] = nil
waitFor.waitingOnTime[co] = nil
return check0
end
local co = coroutine.running()
assert(co ~= nil, "The main thread cannot wait!")
-- store coroutine into the waitingOnCondition table
waitFor.waitingOnCondition[co] = c
-- yield or redirect to waitFor.seconds()
if (during == nil) then
return coroutine.yield()
else
return waitFor.seconds(during)
end
end,
-- usage waitFor.tick(delta) to be called from main loop
tick = function(deltaTime)
assert(deltaTime)
-- print("tick start", deltaTime)
-- update time
waitFor.now = waitFor.now + (deltaTime or 1)
-- list coroutines to be woken up because time out
local threadsToWake = {}
for co, wakeupTime in pairs(waitFor.waitingOnTime) do
if wakeupTime < waitFor.now then
table.insert(threadsToWake, co)
end
end
-- unrecord waiters
for _, co in ipairs(threadsToWake) do
-- if coroutine was waiting something else, forget it
local waitedSignal = waitFor.waitedSignals[co]
if waitedSignal ~= nil then
waitFor.waitingOnSignal[waitedSignal][co] = nil
end
waitFor.waitedSignals[co] = nil
waitFor.waitingOnCondition[co] = nil
waitFor.waitingOnTime[co] = nil
end
-- wake them
for _, co in ipairs(threadsToWake) do
-- print("timer resume", names[co])
local ok, msg = coroutine.resume(co, waitFor.TIMEOUT)
if not ok then error(msg) end
end
-- list coroutines to be woken up because condition is checked
threadsToWake = {}
for co, condition in pairs(waitFor.waitingOnCondition) do
-- print("condition", names[co])
if condition() then
table.insert(threadsToWake, co)
end
end
-- unrecord waiters
for _, co in ipairs(threadsToWake) do
-- if coroutine was waiting for a timer or signal, also forget it
local waitedSignal = waitFor.waitedSignals[co]
if waitedSignal ~= nil then
waitFor.waitingOnSignal[waitedSignal][co] = nil
end
waitFor.waitedSignals[co] = nil
waitFor.waitingOnTime[co] = nil
waitFor.waitingOnCondition[co] = nil
end
-- wake them
for _, co in ipairs(threadsToWake) do
-- print("cdt resume", names[co])
local ok, msg = coroutine.resume(co, waitFor.POKE)
if not ok then error(msg) end
end
-- print("tick end", deltaTime)
end,
-- usage waitFor.trigger(someSignal)
trigger = function(signal, ...)
assert(signal ~= nil)
-- print('trigger start')
-- get signal waiters table
local cos = waitFor.waitingOnSignal[signal]
if cos == nil then return end
-- reset table
waitFor.waitingOnSignal[signal] = nil
-- unrecord waiters
for _, co in pairs(cos) do
waitFor.waitingOnTime[co] = nil
waitFor.waitingOnCondition[co] = nil
waitFor.waitedSignals[co] = nil
end
-- resume waiters
for _, co in pairs(cos) do
-- print('trigger resume', names[co])
local args = {co, signal, ...}
local ok, msg = coroutine.resume(unpack(args))
if not ok then error(msg) end
end
-- print('trigger end')
end,
triggerEvent = function(event)
return waitFor.trigger(event.type or Event.COMPLETE, {x = event.x, y = event.y })
end,
theEnd = function()
local stillWaiting = true;
while stillWaiting do
local wot = waitFor.waitingOnTime
local woc = waitFor.waitingOnCondition
local wos = waitFor.waitedSignals
waitFor.waitingOnTime = {};
waitFor.waitingOnCondition = {};
waitFor.waitedSignals = {};
waitFor.waitingOnSignal = {};
stillWaiting = false;
for co, wakeupTime in pairs(wot) do
-- print('was waiting on time: ', names[co], co)
coroutine.resume(co)
stillWaiting = true;
end
for co, condition in pairs(woc) do
-- print('was waiting on condition: ', names[co], co)
coroutine.resume(co)
stillWaiting = true;
end
for co, signal in pairs(wos) do
-- print('was waiting on signal: ', names[co], co)
coroutine.resume(co)
stillWaiting = true;
end
end
end
}