forked from x2048/cinematic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
711 lines (622 loc) · 19.7 KB
/
init.lua
File metadata and controls
711 lines (622 loc) · 19.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
-- Copyright (c) 2021 Dmitry Kostenko. Licensed under AGPL v3
-- Parsing utilities
local function starts_with(str, prefix)
return str:sub(1, #prefix) == prefix
end
local function skip_prefix(str, prefix)
return str:sub(#prefix + 1)
end
local function string_split(str, char)
result = {}
for part in str:gmatch("[^"..char.."]+") do
table.insert(result, part)
end
return result
end
local function is_in(item, set)
for _,valid in ipairs(set) do
if item == valid then return true end
end
return false
end
-- Position helpers
local position = {}
function position.save(player, slot)
local state = { pos = player:get_pos(), look = { h = player:get_look_horizontal(), v = player:get_look_vertical() }}
player:get_meta():set_string("cc_pos_"..slot, minetest.serialize(state))
end
function position.get(player, slot)
local state = player:get_meta():get_string("cc_pos_"..slot)
if state == nil then
return nil, "Saved position not found"
end
state = minetest.deserialize(state)
if state == nil then
return nil, "Saved position could not be restored"
end
return state
end
function position.restore(player, slot)
local state,message = position.get(player, slot)
if state == nil then
minetest.chat_send_player(player:get_player_name(), message)
return
end
player:set_pos(state.pos)
player:set_look_horizontal(state.look.h)
player:set_look_vertical(state.look.v)
end
function position.clear(player, slot)
player:get_meta():set_string("cc_pos_"..slot, "")
end
function position.list(player)
local result = {}
for key,_ in pairs(player:get_meta():to_table().fields) do
if starts_with(key, "cc_pos_") then
table.insert(result, skip_prefix(key, "cc_pos_"))
end
end
return result
end
-- Waypoint helpers
local wp_storage = minetest.get_mod_storage()
local waypoint = {}
function waypoint.get_list(player)
local raw = player:get_meta():get_string("cc_wp")
if raw == nil or raw == "" then
return {}
end
local list = minetest.deserialize(raw)
if list == nil then
return {}
end
return list
end
function waypoint.save_list(player, list)
player:get_meta():set_string("cc_wp", minetest.serialize(list))
end
function waypoint.add_current(player, stop)
local list = waypoint.get_list(player)
table.insert(list, {
pos = player:get_pos(),
look = { h = player:get_look_horizontal(), v = player:get_look_vertical() },
stop = stop,
})
waypoint.save_list(player, list)
return #list
end
function waypoint.clear(player)
player:get_meta():set_string("cc_wp", "")
end
function waypoint.playlist_list()
local raw = wp_storage:get_string("wp_playlist_index")
if raw == nil or raw == "" then
return {}
end
local list = minetest.deserialize(raw)
if list == nil then
return {}
end
return list
end
function waypoint.playlist_get(name)
local raw = wp_storage:get_string("wp_playlist:"..name)
if raw == nil or raw == "" then
return nil
end
local list = minetest.deserialize(raw)
return list
end
function waypoint.playlist_save(name, list)
wp_storage:set_string("wp_playlist:"..name, minetest.serialize(list))
local index = waypoint.playlist_list()
for _, entry in ipairs(index) do
if entry == name then
return
end
end
table.insert(index, name)
wp_storage:set_string("wp_playlist_index", minetest.serialize(index))
end
function waypoint.playlist_remove(name)
wp_storage:set_string("wp_playlist:"..name, "")
local index = waypoint.playlist_list()
local next_index = {}
for _, entry in ipairs(index) do
if entry ~= name then
table.insert(next_index, entry)
end
end
wp_storage:set_string("wp_playlist_index", minetest.serialize(next_index))
end
minetest.register_privilege("waypoint_run", {
description = "Start waypoint playlists",
give_to_singleplayer = false,
})
minetest.register_privilege("waypoint_edit", {
description = "Edit waypoints and playlists",
give_to_singleplayer = false,
})
-- Core API
local cinematic
cinematic = {
motions = {},
register_motion = function(name, definition)
definition.name = name
cinematic.motions[name] = definition
table.insert(cinematic.motions, definition)
end,
commands = {},
register_command = function(name, definition)
definition.name = name
cinematic.commands[name] = definition
table.insert(cinematic.commands, definition)
end,
players = {},
start = function(player, motion, params)
local player_name = player:get_player_name()
-- Stop previous motion and clean up
if cinematic.players[player_name] ~= nil then
player:set_fov(unpack(cinematic.players[player_name].fov))
cinematic.players[player_name] = nil
end
local state = cinematic.motions[motion].initialize(player, params)
-- motion can return nil from initialize to abort the process
if state ~= nil then
position.save(player, "auto")
cinematic.players[player_name] = { player = player, motion = motion, state = state, fov = {player:get_fov()} }
if params.fov == "wide" then
params.fov = 1.4
elseif params.fov == "narrow" then
params.fov = 0.5
elseif params.fov ~= nil then
params.fov = tonumber(params.fov)
end
if params.fov ~= nil then
player:set_fov(params.fov, true)
end
end
end,
stop = function(player)
cinematic.start(player, "stop", {})
end,
}
-- Update loop
minetest.register_globalstep(function(dtime)
for _, entry in pairs(cinematic.players) do
cinematic.motions[entry.motion].tick(entry.player, entry.state, dtime)
end
end)
-- Motions
cinematic.register_motion("360", {
initialize = function(player, params)
local player_pos = player:get_pos()
local center = vector.add(player_pos, vector.multiply(vector.normalize(player:get_look_dir()), params.radius or 50))
return {
center = center,
distance = vector.distance(vector.new(center.x, 0, center.z), vector.new(player_pos.x, 0, player_pos.z)),
angle = minetest.dir_to_yaw(vector.subtract(player_pos, center)) + math.pi / 2,
height = player_pos.y - center.y,
speed = params:get_speed({"l", "left"}, "right"),
}
end,
tick = function(player, state)
state.angle = state.angle + state.speed * math.pi / 3600
if state.angle < 0 then state.angle = state.angle + 2 * math.pi end
if state.angle > 2 * math.pi then state.angle = state.angle - 2 * math.pi end
player_pos = vector.add(state.center, vector.new(state.distance * math.cos(state.angle), state.height, state.distance * math.sin(state.angle)))
player:set_pos(player_pos)
player:set_look_horizontal(state.angle + math.pi / 2)
end
})
cinematic.register_motion("dolly", {
initialize = function(player, params)
return {
speed = params:get_speed({"b", "back", "backwards", "out"}, "forward"),
direction = vector.normalize(vector.new(player:get_look_dir().x, 0, player:get_look_dir().z)),
}
end,
tick = function(player, state)
local player_pos = player:get_pos()
player_pos = vector.add(player_pos, vector.multiply(state.direction, state.speed * 0.05))
player:set_pos(player_pos)
end
})
cinematic.register_motion("truck", {
initialize = function(player, params)
return {
speed = params:get_speed({"l", "left"}, "right"),
direction = vector.normalize(vector.cross(vector.new(0,1,0), player:get_look_dir())),
}
end,
tick = function(player, state)
local player_pos = player:get_pos()
player_pos = vector.add(player_pos, vector.multiply(state.direction, state.speed * 0.05))
player:set_pos(player_pos)
end
})
cinematic.register_motion("pedestal", {
initialize = function(player, params)
return {
speed = params:get_speed({"d", "down"}, "up"),
direction = vector.new(0,1,0)
}
end,
tick = function(player, state)
local player_pos = player:get_pos()
player_pos = vector.add(player_pos, vector.multiply(state.direction, state.speed * 0.05))
player:set_pos(player_pos)
end
})
cinematic.register_motion("pan", {
initialize = function(player, params)
return {
speed = params:get_speed({"l", "left"}, "right"),
angle = player:get_look_horizontal()
}
end,
tick = function(player, state)
state.angle = state.angle - state.speed * math.pi / 3600
if state.angle < 0 then state.angle = state.angle + 2 * math.pi end
if state.angle > 2 * math.pi then state.angle = state.angle - 2 * math.pi end
player:set_look_horizontal(state.angle)
end
})
cinematic.register_motion("tilt", {
initialize = function(player, params)
return {
speed = params:get_speed({"d", "down"}, "up"),
angle = player:get_look_vertical()
}
end,
tick = function(player, state)
state.angle = state.angle - state.speed * math.pi / 3600
if state.angle < 0 then state.angle = state.angle + 2 * math.pi end
if state.angle > 2 * math.pi then state.angle = state.angle - 2 * math.pi end
player:set_look_vertical(state.angle)
end
})
cinematic.register_motion("zoom", {
initialize = function(player, params)
return {
speed = params:get_speed({"out"}, "in"),
}
end,
tick = function(player, state)
-- Capture initial FOV at the tick
-- This is not possible in initialize because the FOV modifier has not been applied yet
if state.fov == nil then
local fov = {player:get_fov()}
minetest.chat_send_all(dump(fov,""))
if fov[1] == 0 then
fov[1] = 1
fov[2] = true
end
fov[3] = 0
state.fov = fov
end
state.fov[1] = state.fov[1] - 0.001 * state.speed
player:set_fov(unpack(state.fov))
end
})
cinematic.register_motion("stop", {initialize = function() end})
cinematic.register_motion("revert", {initialize = function(player) position.restore(player, "auto") end})
cinematic.register_motion("waypoints", {
initialize = function(player, params)
local list = params.list or waypoint.get_list(player)
if #list == 0 then
minetest.chat_send_player(player:get_player_name(), "No waypoints found. Use /wp add first.")
return nil
end
local speed = params.speed or 4
if speed <= 0 then speed = 4 end
local start_pos = player:get_pos()
local start_look = { h = player:get_look_horizontal(), v = player:get_look_vertical() }
local target = list[1]
local distance = vector.distance(start_pos, target.pos)
local duration = distance / speed
if duration < 0.1 then duration = 0.1 end
return {
list = list,
index = 1,
speed = speed,
t = 0,
duration = duration,
from = { pos = start_pos, look = start_look },
to = target,
}
end,
tick = function(player, state, dtime)
if dtime == nil then dtime = 0.05 end
state.t = state.t + dtime
local progress = state.t / state.duration
if progress > 1 then progress = 1 end
local eased = progress
if state.to.stop ~= false then
-- Smoothstep easing for cinematic movement when stopping
eased = progress * progress * (3 - 2 * progress)
end
local from_pos = state.from.pos
local to_pos = state.to.pos
local new_pos = vector.new(
from_pos.x + (to_pos.x - from_pos.x) * eased,
from_pos.y + (to_pos.y - from_pos.y) * eased,
from_pos.z + (to_pos.z - from_pos.z) * eased
)
player:set_pos(new_pos)
local from_h = state.from.look.h
local to_h = state.to.look.h
local delta_h = (to_h - from_h + math.pi) % (2 * math.pi) - math.pi
player:set_look_horizontal(from_h + delta_h * eased)
local from_v = state.from.look.v
local to_v = state.to.look.v
player:set_look_vertical(from_v + (to_v - from_v) * eased)
if progress >= 1 then
state.index = state.index + 1
if state.index > #state.list then
cinematic.stop(player)
return
end
state.t = 0
state.from = state.to
state.to = state.list[state.index]
local distance = vector.distance(state.from.pos, state.to.pos)
state.duration = distance / state.speed
if state.duration < 0.1 then state.duration = 0.1 end
end
end
})
cinematic.register_command("pos", {
run = function(player, args)
local slot = args[2] or "default"
if args[1] == "save" then
position.save(player, slot)
return true
elseif args[1] == "restore" then
position.restore(player, slot)
return true
elseif args[1] == "clear" then
position.clear(player, slot)
elseif args[1] == "list" then
for _,slot in ipairs(position.list(player)) do
minetest.chat_send_player(player:get_player_name(), slot)
end
else
return false, "Unknown subcommand"..args[1]
end
end
})
cinematic.register_command("wp", {
run = function(player, args)
local sub = args[1]
if sub == "add" then
if not minetest.check_player_privs(player:get_player_name(), { waypoint_edit = true }) then
return false, "Missing privilege: waypoint_edit"
end
local mode = args[2]
local stop = true
if mode == "flow" or mode == "fluent" or mode == "continuous" or mode == "go" then
stop = false
elseif mode ~= nil and mode ~= "stop" then
return false, "Unknown waypoint mode "..mode
end
local count = waypoint.add_current(player, stop)
minetest.chat_send_player(player:get_player_name(), "Waypoint "..count.." added.")
return true
elseif sub == "clear" then
if not minetest.check_player_privs(player:get_player_name(), { waypoint_edit = true }) then
return false, "Missing privilege: waypoint_edit"
end
waypoint.clear(player)
minetest.chat_send_player(player:get_player_name(), "Waypoints cleared.")
return true
else
return false, "Unknown subcommand "..(sub or "")
end
end
})
cinematic.register_command("playlist", {
run = function(player, args)
local sub = args[1]
if sub == "save" then
if not minetest.check_player_privs(player:get_player_name(), { waypoint_edit = true }) then
return false, "Missing privilege: waypoint_edit"
end
local name = args[2]
if name == nil or name == "" then
return false, "Missing playlist name"
end
local list = waypoint.get_list(player)
if #list == 0 then
return false, "No waypoints to save"
end
waypoint.playlist_save(name, list)
minetest.chat_send_player(player:get_player_name(), "Playlist "..name.." saved.")
return true
elseif sub == "start" then
if not minetest.check_player_privs(player:get_player_name(), { waypoint_run = true }) then
return false, "Missing privilege: waypoint_run"
end
local name = nil
local params = {}
for i = 2,#args do
local parsed = false
for _,setting in ipairs({ "speed", "v" }) do
if not parsed and starts_with(args[i], setting.."=") then
params[setting] = skip_prefix(args[i], setting.."=")
parsed = true
end
end
if not parsed and name == nil then
name = args[i]
parsed = true
end
if not parsed then
return false, "Invalid parameter "..args[i]
end
end
if name == nil or name == "" then
return false, "Missing playlist name"
end
local list = waypoint.playlist_get(name)
if list == nil or #list == 0 then
return false, "Playlist not found: "..name
end
local first = list[1]
player:set_pos(first.pos)
player:set_look_horizontal(first.look.h)
player:set_look_vertical(first.look.v)
params.speed = params.speed or params.v
params.speed = (params.speed and tonumber(params.speed))
params.list = list
cinematic.start(player, "waypoints", params)
return true
elseif sub == "remove" then
if not minetest.check_player_privs(player:get_player_name(), { waypoint_edit = true }) then
return false, "Missing privilege: waypoint_edit"
end
local name = args[2]
if name == nil or name == "" then
return false, "Missing playlist name"
end
waypoint.playlist_remove(name)
minetest.chat_send_player(player:get_player_name(), "Playlist "..name.." removed.")
return true
elseif sub == "list" then
local list = waypoint.playlist_list()
if #list == 0 then
minetest.chat_send_player(player:get_player_name(), "No playlists saved.")
return true
end
for _, entry in ipairs(list) do
minetest.chat_send_player(player:get_player_name(), entry)
end
return true
else
return false, "Unknown subcommand "..(sub or "")
end
end
})
cinematic.register_command("cancel", {
run = function(player)
cinematic.stop(player)
return true
end
})
cinematic.register_command("start", {
run = function(player, args)
local params = {}
local list_name = nil
for i = 1,#args do
local parsed = false
for _,setting in ipairs({ "speed", "v" }) do
if not parsed and starts_with(args[i], setting.."=") then
params[setting] = skip_prefix(args[i], setting.."=")
parsed = true
end
end
if not parsed and list_name == nil then
list_name = args[i]
parsed = true
end
if not parsed then
return false, "Invalid parameter "..args[i]
end
end
params.speed = params.speed or params.v
params.speed = (params.speed and tonumber(params.speed))
local list = nil
if list_name ~= nil then
if not minetest.check_player_privs(player:get_player_name(), { waypoint_run = true }) then
return false, "Missing privilege: waypoint_run"
end
list = waypoint.playlist_get(list_name)
if list == nil or #list == 0 then
return false, "Playlist not found: "..list_name
end
else
list = waypoint.get_list(player)
end
if #list == 0 then
return false, "No waypoints found. Use /wp add first."
end
local first = list[1]
player:set_pos(first.pos)
player:set_look_horizontal(first.look.h)
player:set_look_vertical(first.look.v)
params.list = list
cinematic.start(player, "waypoints", params)
return true,""
end
})
-- Chat command handler
minetest.register_chatcommand("cc", {
params = "((360|tilt|pan|truck|dolly|pedestal) [direction=(right|left|in|out|up|down)] [speed=<speed>] [radius=<radius>] | pos ((save|restore|clear [<name>])|list)) | (stop|revert)",
description = "Simulate cinematic camera motion",
privs = { fly = true },
func = function(name, cmdline)
local player = minetest.get_player_by_name(name)
local params = {}
local parts = string_split(cmdline, " ")
local command = parts[1]
table.remove(parts, 1)
-- Handle commands
if command == "wp" or command == "start" or command == "cancel" or command == "playlist" then
return false, "Waypoint commands moved to /wp"
end
if cinematic.commands[command] ~= nil then
return cinematic.commands[command].run(player, parts)
end
if cinematic.motions[command] == nil then
return false, "Invalid command or motion, see /help cc"
end
-- Parse command line
for i = 1,#parts do
local parsed = false
for _,setting in ipairs({ "direction", "dir", "speed", "v", "radius", "r", "fov" }) do
if not parsed and starts_with(parts[i], setting.."=") then
params[setting] = skip_prefix(parts[i], setting.."=")
parsed = true
end
end
if not parsed then
return false, "Invalid parameter "..parts[i]
end
end
-- Fix parameters
params.direction = params.direction or params.dir
params.speed = params.speed or params.v
params.radius = params.radius or params.r
params.speed = (params.speed and tonumber(params.speed))
params.radius = (params.radius and tonumber(params.radius))
params.get_speed = function(self, negative_dirs, default_dir)
return (self.speed or 1) * (is_in(self.direction or default_dir, negative_dirs) and -1 or 1)
end
cinematic.start(player, command, params)
return true,""
end
})
minetest.register_chatcommand("wp", {
params = "(add [stop|flow] | clear | start [name] [speed=<speed>] | cancel | playlist (save <name> | start <name> [speed=<speed>] | remove <name> | list))",
description = "Waypoint camera path control",
privs = { fly = true, teleport = true, noclip = true },
func = function(name, cmdline)
local player = minetest.get_player_by_name(name)
local parts = string_split(cmdline, " ")
local command = parts[1]
table.remove(parts, 1)
if command == nil or command == "" then
return false, "Missing subcommand, see /help wp"
end
if command == "start" or command == "cancel" then
return cinematic.commands[command].run(player, parts)
end
if command == "add" or command == "clear" then
return cinematic.commands["wp"].run(player, { command, unpack(parts) })
end
if command == "playlist" then
return cinematic.commands["playlist"].run(player, parts)
end
return false, "Unknown subcommand "..command
end
})