-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.lua
More file actions
540 lines (493 loc) · 9.87 KB
/
Copy pathlib.lua
File metadata and controls
540 lines (493 loc) · 9.87 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
-- stdlib for dirty lua binding
local function clone_inner(val, seen)
seen = seen or {}
local ty = type(val)
if ty == "userdata" then
local name = getmetatable(val)["__name"]
if name == "vec2" or name == "vec3" or name == "color" then
return val:clone()
else
return val
end
elseif ty == "table" then
if seen[val] then
return seen[val]
end
local copy = {}
seen[val] = copy
for k, v in next, val, nil do
copy[clone_inner(k, seen)] = clone_inner(v, seen)
end
setmetatable(copy, clone_inner(getmetatable(val), seen))
end
return val
end
function clone(val)
return clone_inner(val, {})
end
function table.hashset(t)
local t2 = {}
for _, v in pairs(t) do
t2[v] = true
end
return t2
end
function table.keys(t)
local keys = {}
for k in pairs(t) do
keys[#keys + 1] = k
end
return keys
end
function table.values(t)
local values = {}
for _, v in pairs(t) do
values[#values + 1] = v
end
return values
end
function table.map(t, func)
local res = {}
for k, v in pairs(t) do
res[k] = func(v, k)
end
return res
end
function table.filter(t, pred)
local res = {}
if table.isarray(t) then
for i, v in ipairs(t) do
if pred(v, i) then
res[#res + 1] = v
end
end
else
for k, v in pairs(t) do
if pred(v, k) then
res[k] = v
end
end
end
return res
end
function table.find(t, val)
local iter = table.isarray(t) and ipairs or pairs
for k, v in iter(t) do
if v == val then
return k
end
end
return nil
end
function table.clone(t)
return clone(t)
end
function table.random(t)
return t[math.random(#t)]
end
function table.weighedchoice(t)
local sum = 0
for _, v in pairs(t) do
assert(v >= 0, "weight value less than zero")
sum = sum + v
end
assert(sum ~= 0, "all weights are zero")
local rnd = d.rand(sum)
for k, v in pairs(t) do
if rnd < v then
return k
end
rnd = rnd - v
end
end
function table.shuffle(t)
local rtn = {}
for i = 1, #t do
local r = math.random(i)
if r ~= i then
rtn[i] = rtn[r]
end
rtn[r] = t[i]
end
return rtn
end
function table.push(t, v)
t[#t + 1] = v
return v
end
function table.delete(t, val)
local i = table.find(t, val)
if i then
table.remove(t, i)
end
end
function table.slice(t, a, b)
b = b or #t
a = a or 1
local t2 = {}
for i = a, b do
t2[#t2 + 1] = t[i]
end
return t2
end
function table.isarray(t)
return type(t) == "table" and t[1] ~= nil
end
function math.round(n)
return math.floor(n + 0.5)
end
function math.clamp(x, min, max)
return x < min and min or (x > max and max or x)
end
function math.chance(c)
return math.random() <= c
end
function math.wave(a, b, t)
return a + (math.sin(t) + 1) / 2 * (b - a)
end
function math.sign(x)
return x < 0 and -1 or 1
end
function math.lerp(a, b, t)
return a + (b - a) * t
end
function math.smooth(a, b, t)
local m = t * t * (3 - 2 * t)
return a + (b - a) * m
end
function math.map(v, l1, h1, l2, h2)
return l2 + (v - l1) * (h2 - l2) / (h1 - l1)
end
function string.split(s, sep)
local l = {}
if not sep or sep == "" then
for i = 1, #s do
l[i] = s:sub(i, i)
end
else
for part in s:gmatch("([^" .. sep .. "]+)") do
l[#l + 1] = part
end
end
return l
end
function string.trim(s)
return s:match("^%s*(.-)%s*$")
end
function string.startswith(s, start)
return s:sub(1, #start) == start
end
function string.endswith(s, ending)
return ending == "" or s:sub(-#ending) == ending
end
d.tween = {}
function d.tween.update(t, dt)
if t.done or t.paused then
return
end
t.elapsed = t.elapsed + dt
local tt = t.elapsed / t.duration
if tt >= 1.0 then
t.done = true
t.elapsed = t.duration
t.val = t.to
t.action(t)
return
end
local t2 = t.func(tt)
local ty = type(t.val)
if ty == "number" then
t.val = math.lerp(t.from, t.to, t2)
elseif ty == "userdata" then
local name = getmetatable(t.val)["__name"]
if name == "vec2" then
t.val.x = math.lerp(t.from.x, t.to.x, t2)
t.val.y = math.lerp(t.from.y, t.to.y, t2)
elseif name == "vec3" then
t.val.x = math.lerp(t.from.x, t.to.x, t2)
t.val.y = math.lerp(t.from.y, t.to.y, t2)
t.val.z = math.lerp(t.from.z, t.to.z, t2)
elseif name == "color" then
t.val.r = math.lerp(t.from.r, t.to.r, t2)
t.val.g = math.lerp(t.from.g, t.to.g, t2)
t.val.b = math.lerp(t.from.b, t.to.b, t2)
t.val.a = math.lerp(t.from.a, t.to.a, t2)
else
error("unsupported tween type: " .. name)
end
else
error("unsupported tween type: " .. ty)
end
t.action(t)
end
function d.tween.new(from, to, duration, func, action, name)
return {
from = from,
to = to,
val = clone(from),
duration = duration,
func = func,
action = action,
name = name,
elapsed = 0,
done = false,
paused = false,
update = d.tween.update,
}
end
function d.tween.manager()
local m = {
paused = false,
}
local tweens = {}
function m:add(...)
local t = d.tween.new(...)
if t.name then
tweens = table.filter(tweens, function(t2)
return t.name ~= t2.name
end)
end
tweens[#tweens + 1] = t
return t
end
-- TODO: name
function m:add_async(...)
local t = m:add(...)
while not t.done do
coroutine.yield()
end
end
function m:update(dt)
if m.paused then return end
for i = 1, #tweens do
local t = tweens[i]
t:update(dt)
end
tweens = table.filter(tweens, function(t)
return t.done == false
end)
end
function m:clear()
tweens = {}
end
return m
end
d.timer = {}
function d.timer.new(time, action, loop)
local t = {
elapsed = 0,
time = time,
paused = false,
done = false,
}
function t:reset(time2)
self.elapsed = 0
self.done = false
self.time = time2 or self.time
end
function t:update(dt)
if self.paused or self.done then return end
self.elapsed = self.elapsed + dt
if self.elapsed >= self.time then
if loop then
self.elapsed = 0
else
self.done = true
end
if action then
action(self)
end
end
end
return t
end
function d.timer.manager()
local timers = {}
local m = {
paused = false,
}
function m:add(...)
local t = d.timer.new(...)
timers[#timers + 1] = t
return t
end
function m:wait(secs)
local t = self:add(secs)
while not t.done do
coroutine.yield()
end
end
function m:update(dt)
for i = 1, #timers do
local t = timers[i]
t:update(dt)
end
timers = table.filter(timers, function(t)
return t.done == false
end)
end
return m
end
d.data = {}
local function serialize_inner(value, fmt, depth, seen)
seen = seen or {}
depth = depth or 0
local nl = fmt and "\n" or ""
local sp = fmt and " " or ""
local indent = string.rep(sp, 2)
local ty = type(value)
local supported = table.hashset({ "table", "number", "string", "boolean", "nil" })
if ty == "table" then
if seen[value] then
error("serialization cannot contain circular reference")
end
seen[value] = true
local result = "{" .. nl
local keys = table.keys(value)
table.sort(keys, function(a, b)
return tostring(a) < tostring(b)
end)
for _, k in ipairs(keys) do
local v = value[k]
if supported[type(v)] then
result = result
.. string.rep(indent, depth + 1)
.. "["
.. serialize_inner(k)
.. "]" .. sp .. "=" .. sp
.. serialize_inner(v, fmt, depth + 1, seen)
.. ","
.. nl
end
end
result = result .. string.rep(indent, depth) .. "}"
return result
elseif ty == "string" then
return string.format("%q", value)
elseif ty == "boolean" or t == "nil" then
return tostring(value)
elseif ty == "number" then
if value ~= value then return "0/0" -- nan
elseif value == 1 / 0 then return "1/0" -- inf
elseif value == -1 / 0 then return "-1/0" end -- -inf
return tostring(value)
else
error("unsupported serialization type: " .. ty)
end
end
function d.data.serialize(value, fmt)
return serialize_inner(value, fmt, 0, {})
end
function d.data.deserialize(str)
return load("return " .. str)()
end
function d.data.load(name, def)
local dir = d.fs.data_dir()
-- TODO
end
function d.data.save(name, data)
local dir = d.fs.data_dir()
local buf = d.data.serialize(dats)
-- TODO
end
d.task = {}
function d.task.isrunning()
local co, ismain = coroutine.running()
return co and not ismain
end
function d.task.manager()
local tasks = {}
local m = {}
function m:update()
for i = 1, #tasks do
local task = tasks[i]
task.run()
end
tasks = table.filter(tasks, function(t)
return coroutine.status(t.co) ~= "dead"
end)
end
function m:run(task)
local co = coroutine.create(task)
local task = {
done = function()
return coroutine.status(co) == "dead"
end,
co = co,
run = function()
if coroutine.status(co) == "dead" then
return
end
local success, err = coroutine.resume(co)
if not success then
error(err)
end
end,
}
tasks[#tasks + 1] = task
return task
end
return m
end
function d.task.loader()
local loader = {}
local actions = {}
local loaded = 0
function loader:add(action)
actions[#actions + 1] = action
end
function loader:load()
loaded = 0
for _, action in ipairs(actions) do
action()
loaded = loaded + 1
if d.task.isrunning() then
coroutine.yield()
end
end
end
function loader:progress()
return loaded / #actions
end
function loader:finished()
return loaded >= #actions
end
function loader:loaded()
return loaded
end
function loader:count()
return #actions
end
return loader
end
function d.task.assetloader(desc)
local loader = d.task.loader()
local assets = {
img = {},
model = {},
sound = {},
loader = loader,
}
for name, path in pairs(desc.img or {}) do
loader:add(function()
assets.img[name] = d.gfx.img_load(path)
end)
end
for name, path in pairs(desc.model or {}) do
loader:add(function()
assets.model[name] = d.gfx.model_load(path)
end)
end
for name, path in pairs(desc.sound or {}) do
loader:add(function()
assets.sound[name] = d.audio.sound_load(path)
end)
end
return assets
end
function wait(secs)
local start = d.app.time()
while d.app.time() - start < secs do
coroutine.yield()
end
end