-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.lua
More file actions
442 lines (369 loc) · 11.7 KB
/
node.lua
File metadata and controls
442 lines (369 loc) · 11.7 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
local scale = 1.0 -- downscale. 1.0 is fullHD, 2 is half of fullHD
gl.setup(1920 / scale, 1080 / scale)
WIDTH = WIDTH * scale
HEIGHT = HEIGHT * scale
node.set_flag "slow_gc"
util.init_hosted()
local function ERROR(...)
print("ERROR: ", ...)
end
util.loaders.pkm = resource.load_image
-- available resources. global, so they can be used from modules
res = util.resource_loader({
"font.ttf";
"bottle.png";
"house1.png";
"house2.png";
"tower.png";
}, {})
local json = require "json"
local utils = require "utils"
local raw = sys.get_ext "raw_video"
local white = resource.create_colored_texture(1,1,1,1)
local black = resource.create_colored_texture(0,0,0,1)
local function highlight_a(a)
return 0.94, 0.57, 0.14, a
end
pp(CONFIG)
local white_transparent = resource.create_shader[[
uniform sampler2D Texture;
varying vec2 TexCoord;
uniform vec4 Color;
void main() {
vec4 col = texture2D(Texture, TexCoord).rgba;
if (col.r + col.g + col.b > 2.9) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0);
} else {
gl_FragColor = col;
}
}
]]
-----------------------------------------------------------------------------------------------
Time = (function()
local base_t = os.time() - sys.now()
local midnight
local function unixtime()
-- return sys.now() + base_t + 86400*3
return sys.now() + base_t
end
local function walltime()
if not midnight then
return 0, 0
else
local time = (midnight + sys.now()) % 86400
return math.floor(time/3600), math.floor(time % 3600 / 60)
end
end
util.data_mapper{
["clock/unix"] = function(time)
print("new time: ", time)
base_t = tonumber(time) - sys.now()
end;
["clock/midnight"] = function(since_midnight)
print("new midnight: ", since_midnight)
midnight = tonumber(since_midnight) - sys.now()
end;
}
return {
unixtime = unixtime;
walltime = walltime;
}
end)()
Fadeout = (function()
local current_alpha = 1
local fade_til = 0
local function alpha()
return current_alpha
end
local function fade(t)
fade_til = sys.now() + t
end
local function tick()
local target_alpha = sys.now() > fade_til and 1 or 0
if current_alpha < target_alpha then
current_alpha = current_alpha + 0.01
elseif current_alpha > target_alpha then
current_alpha = current_alpha - 0.01
end
end
return {
alpha = alpha;
tick = tick;
fade = fade;
}
end)()
Scroller = (function()
local workshops = {}
util.file_watch("workshops.json", function(content)
print("reloading workshops")
workshops = json.decode(content)
end)
local infos = {}
util.file_watch("scroll.txt", function(content)
infos = {}
for line in string.gmatch(content.."\n", "([^\n]*)\n") do
if #line > 0 then
infos[#infos+1] = line
end
end
end)
local function feeder()
local out = {}
for idx = 1, #infos do
out[#out+1] = infos[idx]
end
local now = Time.unixtime()
for idx = 1, #workshops do
local workshop = workshops[idx]
if workshop.start_unix > now and workshop.start_unix < now + 3 * 3600 then
out[#out+1] = workshop.text
end
end
return out
end
local text = util.running_text{
font = res.font;
size = 40;
speed = 120;
color = {1,1,1,.8};
generator = util.generator(feeder)
}
local visibility = 0
local target = 0
local restore = sys.now() + 1
local function hide(duration)
target = 0
restore = sys.now() + duration
end
local function draw()
if visibility > 0.01 then
-- black:draw(0, HEIGHT-45, WIDTH, HEIGHT, visibility/3)
text:draw(HEIGHT - visibility * 42)
end
end
local current_speed = 0
local function tick()
if sys.now() > restore then
target = 1
end
local current_speed = 0.05
visibility = visibility * (1-current_speed) + target * (current_speed)
draw()
end
return {
tick = tick;
hide = hide;
}
end)()
Sidebar = (function()
local sidebar_width = 339
local visibility = 0
local target = 0
local restore = sys.now() + 1
local function hide(duration)
target = 0
restore = sys.now() + duration
end
util.data_mapper{
["sidebar/hide"] = function(t)
hide(tonumber(t))
end;
}
local function draw()
-- local size = 100
-- local hour, min = Time.walltime()
-- local time = string.format("%d:%02d", hour, min)
-- local w = res.font:width(time, size)
-- local sidebar_x = WIDTH - sidebar_width + (sidebar_width-w)/2
-- local tower_x = utils.easeInOut(visibility, WIDTH+100, WIDTH-150)
-- local tower_y = utils.easeInOut(visibility, 900, 660)
-- res.tower:draw(tower_x, tower_y-200, tower_x + 100, tower_y + 145, visibility*2)
-- local house_x = utils.easeInOut(visibility, WIDTH+100, WIDTH-320)
-- local house_y = utils.easeInOut(visibility, 900, 650)
-- res.house2:draw(house_x, house_y, house_x + 280, house_y + 180, visibility*2)
-- local clock_x = utils.easeInOut(visibility, WIDTH-260, WIDTH-320)
-- local clock_y = utils.easeInOut(visibility, HEIGHT-105, 850)
-- res.house1:draw(clock_x, clock_y-100, clock_x + 300, clock_y + 155)
-- res.font:write(clock_x + 150 - w/2, clock_y+5, time, 100, highlight_a(1))
-- local clock = resource.render_child("analogclock")
-- clock:draw(WIDTH - 150, HEIGHT - 150, WIDTH, HEIGHT)
end
local current_speed = 0
local function tick()
if sys.now() > restore then
target = 1
end
local current_speed = 0.05
visibility = visibility * (1-current_speed) + target * (current_speed)
draw()
end
return {
tick = tick;
hide = hide;
}
end)()
-----------------------------------------------------------------------------------------------
local function ModuleLoader()
local modules = {}
local function module_name_from_filename(filename)
return filename:match "module_(.*)%.lua"
end
local function module_unload(module_name)
if modules[module_name] and modules[module_name].unload then
modules[module_name].unload()
end
modules[module_name] = nil
node.gc()
end
local function module_update(module_name, module)
module_unload(module_name)
modules[module_name] = module
node.gc()
end
node.event("content_update", function(filename)
local module_name = module_name_from_filename(filename)
if module_name then
module_update(module_name, assert(loadstring(resource.load_file(filename), "=" .. filename))())
end
end)
node.event("content_delete", function(filename)
local module_name = module_name_from_filename(filename)
if module_name then
module_unload(module_name)
end
end)
return modules
end
local function Scheduler(runner, modules)
local playlist = {}
local playlist_offset = 0
util.file_watch("playlist.json", function(raw)
playlist = json.decode(raw)
playlist_offset = 0
end)
local next_visual = sys.now() + 1
local next_wake = sys.now()
local function enqueue(item)
local ok, duration, options = pcall(modules[item.module].prepare, item.options or {})
if not ok then
print("failed to prepare " .. item.module .. ": " .. duration)
return
end
local visual = {
starts = next_visual - 1;
duration = duration;
module = item.module;
options = options;
}
next_visual = next_visual + duration - 1
next_wake = next_visual - 3
print("about to schedule visual ", item.module)
pp(visual)
runner.add(visual)
end
util.data_mapper{
["scheduler/enqueue"] = function(raw)
enqueue(json.decode(raw))
end
}
local function tick()
if sys.now() < next_wake then
return
end
local item, can_schedule
repeat
item, playlist_offset = utils.cycled(playlist, playlist_offset)
can_schedule = true
if item.chance then
can_schedule = math.random() < item.chance
end
if item.hours then
local hours = {}
for h in string.gmatch(item.hours, "%S+") do
hours[tonumber(h)] = true
end
local hour, min = Time.walltime()
if not hours[hour] then
can_schedule = false
end
end
if not modules[item.module] then
print("module " .. item.module .. " not available")
can_schedule = false
elseif not modules[item.module].can_schedule(item.options) then
print("module " .. item.module .. " cannot be scheduled")
can_schedule = false
end
until can_schedule
enqueue(item)
end
return {
tick = tick;
}
end
local function reset_view()
local fov = math.atan2(HEIGHT, WIDTH*2) * 360 / math.pi
return gl.perspective(fov, WIDTH/2, HEIGHT/2, -WIDTH,
WIDTH/2, HEIGHT/2, 0)
end
local function Runner(modules)
local visuals = {}
local function add(visual)
local co = coroutine.create(modules[visual.module].run)
local success, is_finished = coroutine.resume(co, visual.duration, visual.options, {
wait_next_frame = function ()
return coroutine.yield(false)
end;
wait_t = function(t)
while true do
local now = coroutine.yield(false)
if now >= t then return now end
end
end;
upto_t = function(t)
return function()
local now = coroutine.yield(false)
if now < t then return now end
end
end;
})
if not success then
ERROR(debug.traceback(co, string.format("cannot start visual: %s", is_finished)))
elseif not is_finished then
table.insert(visuals, 1, {
co = co;
starts = visual.starts;
})
end
end
local function tick()
local now = sys.now()
for idx = #visuals,1,-1 do -- iterate backwards so we can remove finished visuals
local visual = visuals[idx]
reset_view()
local success, is_finished = coroutine.resume(visual.co, now - visual.starts)
if not success then
ERROR(debug.traceback(visual.co, string.format("cannot resume visual: %s", is_finished)))
table.remove(visuals, idx)
elseif is_finished then
table.remove(visuals, idx)
end
end
end
return {
tick = tick;
add = add;
}
end
-----------------------------------------------------------------------------------------------
local modules = ModuleLoader()
local runner = Runner(modules)
local scheduler = Scheduler(runner, modules)
function node.render()
Fadeout.tick()
runner.tick()
reset_view()
scheduler.tick()
Scroller.tick()
Sidebar.tick()
end