-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcutscene_controller.lua
More file actions
617 lines (539 loc) · 19.9 KB
/
cutscene_controller.lua
File metadata and controls
617 lines (539 loc) · 19.9 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
local Event = require 'utils.event'
local Token = require 'utils.token'
local Task = require 'utils.task'
local Global = require 'utils.global'
local Command = require 'utils.command'
local Debug = require 'utils.debug'
local Gui = require 'utils.gui'
local Settings = require 'utils.redmew_settings'
local Styles = require 'resources.styles'
local set_timeout_in_ticks = Task.set_timeout_in_ticks
local debug_print = Debug.print
local skip_btn_name = Gui.uid_name()
local backward_btn_name = Gui.uid_name()
local forward_btn_name = Gui.uid_name()
local auto_play_cutscene_checkbox_name = Gui.uid_name()
local flow_name = Gui.uid_name()
local Public = {}
local handler
local auto_play_cutscene_setting_name
local cutscene_functions = {}
local running_cutscenes = {}
local replay = {
identifier = nil,
final_transition_time = nil
}
Global.register(
{
cutscene_functions = cutscene_functions,
running_cutscenes = running_cutscenes,
replay = replay
},
function(tbl)
cutscene_functions = tbl.cutscene_functions
running_cutscenes = tbl.running_cutscenes
replay = tbl.replay
end
)
function Public.set_auto_play_cutscene_setting_name(name)
if _LIFECYCLE == _STAGE.runtime then
error('Cannot set during runtime', 2)
end
if type(auto_play_cutscene_setting_name) == nil then
error('Cannot set more than once', 2)
end
if type(name) ~= 'string' then
error('name must be of type string', 2)
end
auto_play_cutscene_setting_name = name
end
local function valid(entity)
return entity and entity.valid
end
local function waypoint_still_active(tick, player_index)
local running_cutscene = running_cutscenes[player_index]
tick = tick or -1
if tick == -1 then
debug_print('Tick was nil', 5)
end
if not running_cutscene or tick < running_cutscene.start_tick then
return false
end
return true
end
local toggle_gui_delayed =
Token.register(
function(params)
local player = params.player
if (not valid(player)) then
return
end
if not waypoint_still_active(params.tick, player.index) then
debug_print('Cutscene is no longer active. Skipping toggle_gui')
return
end
local event = {player = player}
local clear = params.clear
if clear == 'left' then
player.gui.left.clear()
elseif clear == 'top' then
player.gui.top.clear()
elseif clear == 'center' then
player.gui.center.clear()
end
params.gui.toggle(event)
end
)
function Public.toggle_gui(tick, player, gui, initial_delay, clear)
--[[if type(gui) == 'table' then
debug_print('Provided GUI is invalid.')
return
end]]
if (not valid(player)) then
return
end
set_timeout_in_ticks(initial_delay, toggle_gui_delayed, {tick = tick, player = player, gui = gui, clear = clear})
end
local play_sound_delayed =
Token.register(
function(params)
local player = params.player
if (not valid(player)) then
return
end
if not waypoint_still_active(params.tick, player.index) then
debug_print('Cutscene is no longer active. Skipping play_sound')
return
end
player.play_sound {path = params.path}
end
)
function Public.play_sound(tick, player, path, times, delay, initial_delay)
if (not valid(player)) then
return
end
if not helpers.is_valid_sound_path(path) then
debug_print('Provided SoundPath is invalid. Try opening /radio and browse for a valid path')
return
end
if not waypoint_still_active(tick, player.index) then
debug_print('Cutscene is no longer active. Skipping play_sound')
return
end
times = times or 1
if times == 1 and not delay and initial_delay then
delay = initial_delay
end
if times > 1 or delay then
delay = delay or 20
initial_delay = initial_delay or 0
for i = 1, times, 1 do
set_timeout_in_ticks(initial_delay + delay * i, play_sound_delayed, {tick = tick, player = player, path = path})
end
else
player.play_sound {path = path}
end
end
local remove_renderings =
Token.register(
function(renderings)
for _, v in pairs(renderings) do
if v.valid then
debug_print('Deleted rendering with id: ' .. v.id)
v.destroy()
end
end
end
)
---Asserts if a given variable is of the expected type using type().
---
---@param expected_type string
---@param given any
---@param variable_reference_message string displayed when the expectation is not met
local function assert_type(expected_type, given, variable_reference_message, allow_nil)
local given_type = type(given)
if given_type ~= expected_type and (allow_nil and given_type ~= 'nil') then
error('Argument ' .. variable_reference_message .. " must be of type '" .. expected_type .. "', given '" .. given_type .. "'")
end
end
function Public.register_cutscene_function(identifier, waypoints, func, terminate_func)
assert_type('string', identifier, 'identifier of function cutscene_controller.register_cutscene_function')
assert_type('table', waypoints, 'waypoints of function cutscene_controller.register_cutscene_function')
assert_type('number', func, 'func of function cutscene_controller.register_cutscene_function')
assert_type('number', terminate_func, 'func of function cutscene_controller.register_cutscene_function', true)
cutscene_functions[identifier] = {func = func, waypoints = waypoints, update = false, terminate_func = terminate_func}
end
function Public.register_running_cutscene(player_index, identifier, final_transition_time)
if _NO_CUTSCENE then
return
end
assert_type('number', player_index, 'player_index of function cutscene_controller.register_running_cutscene')
assert_type('string', identifier, 'identifier of function cutscene_controller.register_running_cutscene')
assert_type('number', final_transition_time, 'identifier of function cutscene_controller.register_running_cutscene', true)
local player = game.get_player(player_index)
if not valid(player) then
return
end
if player.controller_type ~= 1 then
player.print('Cannot start cutscene without a character')
return
end
local cutscene_function = cutscene_functions[identifier]
if not cutscene_function then
return
end
local waypoints = cutscene_function.waypoints
if not waypoints then
return
end
if running_cutscenes[player_index] then
player.print({'cutscene_controller.cannot_start_new'})
return
end
running_cutscenes[player_index] = {
func = cutscene_function.func,
waypoints = waypoints,
update = cutscene_function.update,
final_transition_time = final_transition_time,
character = player.character,
terminate_func = cutscene_function.terminate_func,
rendering = {},
current_index = 0,
start_tick = 0
}
local running_cutscene = running_cutscenes[player_index]
if player.controller_type == defines.controllers.cutscene then
debug_print('' .. player.name .. ' was already in another cutscene not controlled by this module. It has been stopped')
player.exit_cutscene()
end
player.set_controller {type = defines.controllers.ghost}
final_transition_time = final_transition_time >= 0 and final_transition_time or 60
running_cutscene.final_transition_time = final_transition_time
running_cutscene.identifier = identifier
player.set_controller {
type = defines.controllers.cutscene,
waypoints = waypoints,
final_transition_time = final_transition_time
}
local flow = Gui.add_top_element(player, { type = 'flow', name = flow_name })
running_cutscene.btn = flow
local btn = flow.add {type = 'sprite-button', caption = 'Skip cutscene', style = Styles.default_top_element.name, tags = { [Gui.event_tag] = skip_btn_name } }
btn.style.minimal_height = 36
btn.style.maximal_height = 36
btn.style.minimal_width = 150
btn.style.font = 'default-large-bold'
btn.style.font_color = {r = 255, g = 215, b = 0}
local back_btn = flow.add {type = 'sprite-button', caption = 'Go back', style = Styles.default_top_element.name, tags = { [Gui.event_tag] = backward_btn_name } }
back_btn.style.minimal_height = 36
back_btn.style.maximal_height = 36
back_btn.style.minimal_width = 100
back_btn.style.font = 'default-large-bold'
back_btn.style.font_color = {r = 255, g = 215, b = 0}
local forward_btn = flow.add {type = 'sprite-button', caption = 'Go forward', style = Styles.default_top_element.name, tags = { [Gui.event_tag] = forward_btn_name } }
forward_btn.style.minimal_height = 36
forward_btn.style.maximal_height = 36
forward_btn.style.minimal_width = 100
forward_btn.style.font = 'default-large-bold'
forward_btn.style.font_color = {r = 255, g = 215, b = 0}
if auto_play_cutscene_setting_name then
local auto_play_cutscene_checkbox = flow.add
{
type = 'checkbox',
caption = 'Auto play cutscene',
state = Settings.get(player_index, auto_play_cutscene_setting_name),
tags = { [Gui.event_tag] = auto_play_cutscene_checkbox_name },
}
auto_play_cutscene_checkbox.style.top_margin = 8
auto_play_cutscene_checkbox.style.left_margin = 3
running_cutscene.auto_play_cutscene_checkbox = auto_play_cutscene_checkbox
end
handler({player_index = player_index, waypoint_index = 0, tick = game.tick})
end
local function restart_cutscene(player_index, waypoints, start_index)
local current_running = running_cutscenes[player_index]
local final_transition_time = current_running.final_transition_time
current_running.update = false
local character = current_running.character
if not character then
log('Player index: ' .. player_index .. ' managed to lose their character in a cutscene')
end
local end_waypoint = {
-- end waypoint
position = character.position,
transition_time = final_transition_time,
time_to_wait = 1,
zoom = 1,
terminate = true
}
table.insert(waypoints, end_waypoint)
running_cutscenes[player_index] = {
func = current_running.func,
waypoints = waypoints,
update = false,
final_transition_time = final_transition_time,
character = character,
terminate_func = current_running.terminate_func,
rendering = current_running.rendering,
btn = current_running.btn,
current_index = current_running.current_index,
start_tick = current_running.start_tick
}
debug_print('Updating cutscene for player_index ' .. player_index)
debug_print(running_cutscenes[player_index])
local player = game.get_player(player_index)
if not valid(player) then
return
end
if player.controller_type == defines.controllers.cutscene then
player.exit_cutscene()
player.set_controller {type = defines.controllers.ghost}
end
player.set_controller {
type = defines.controllers.cutscene,
waypoints = waypoints,
final_transition_time = final_transition_time
}
if start_index then
player.jump_to_cutscene_waypoint(start_index + 1)
else
start_index = 0
end
handler({player_index = player_index, waypoint_index = start_index, tick = game.tick})
end
function Public.inject_waypoint(player_index, waypoint, waypoint_index, override)
local running_cutscene = running_cutscenes[player_index]
if not running_cutscene then
return
end
local waypoints = running_cutscene.waypoints
if not waypoints then
return
end
local copy_waypoints = {}
for i = 1, #waypoints do
table.insert(copy_waypoints, waypoints[i])
end
if override then
copy_waypoints[waypoint_index] = waypoint
else
table.insert(copy_waypoints, waypoint_index, waypoint)
end
running_cutscene.update = copy_waypoints
end
local callback_function =
Token.register(
function(params)
local player_index = params.player_index
local func_params = params.params
if waypoint_still_active(func_params.tick, player_index) then
Token.get(params.func)(player_index, params.waypoint_index, func_params)
else
debug_print('Skipping callback function. Cutscene got terminated!')
end
end
)
local reconnect_character =
Token.register(
function(params)
local player_index = params.player_index
local player = game.get_player(player_index)
local running_cutscene = params.running_cutscene
local character = running_cutscene.character
local func = running_cutscene.terminate_func
if valid(player) then
player.exit_cutscene()
if valid(character) then
player.set_controller {type = defines.controllers.character, character = character}
else
params.skip_btn_flag = true
player.ticks_to_respawn = 3600
player.print('[color=red][font=compi]Oops - You Died! Please go to a safe location and restart the cutscene[/font][/color]')
end
if func then
Token.get(func)(player_index, params.skip_btn_flag)
end
Token.get(remove_renderings)(running_cutscene.rendering)
running_cutscene.btn.destroy()
running_cutscenes[player_index] = nil
end
end
)
function Public.terminate_cutscene(player_index, ticks, skip_btn_flag)
local running_cutscene = running_cutscenes[player_index]
if not running_cutscene then
return
end
ticks = ticks and ticks or 1
debug_print('Terminating cutscene in ' .. ticks .. ' Ticks')
if skip_btn_flag == nil then
skip_btn_flag = false
end
debug_print('Is terminate_func ignored = ' .. tostring(skip_btn_flag))
set_timeout_in_ticks(
ticks,
reconnect_character,
{
player_index = player_index,
running_cutscene = running_cutscene,
skip_btn_flag = skip_btn_flag
}
)
end
function Public.register_rendering(player_index, tick, render)
if type(render) ~= 'table' then
render = {render}
end
local running_cutscene = running_cutscenes[player_index]
for _, obj in pairs(render) do
if obj.valid then
if not waypoint_still_active(tick, player_index) then
debug_print('The rendering with id ' .. obj.id .. ' was not added. Destroying it instead')
obj.destroy()
else
table.insert(running_cutscene.rendering, obj)
end
end
end
end
function Public.register_replay(identifier, final_transition_time)
replay.identifier = identifier
replay.final_transition_time = final_transition_time
debug_print('Identifier ' .. identifier .. ' registered as replay cutscene')
end
handler = function(event)
local player_index = event.player_index
local waypoint_index = event.waypoint_index
local tick = event.tick
debug_print('Waypoint_index ' .. waypoint_index .. ' has finished at tick: ' .. tick)
local running_cutscene = running_cutscenes[player_index]
if not running_cutscene then
return
end
running_cutscene.current_index = waypoint_index
running_cutscene.start_tick = tick
local update = running_cutscene.update
if update then
restart_cutscene(player_index, update, waypoint_index - 1)
return
end
local ticks = running_cutscene.waypoints[waypoint_index + 1]
if ticks then
ticks = ticks.transition_time
else
ticks = running_cutscene.final_transition_time
end
local func = running_cutscene.func
if not func then
return
end
local current_waypoint = running_cutscene.waypoints[waypoint_index + 1]
if not current_waypoint or current_waypoint.terminate then
Public.terminate_cutscene(player_index, ticks)
return
end
local params = {
position = current_waypoint.position,
time_to_wait = current_waypoint.time_to_wait,
transition_time = current_waypoint.transition_time,
zoom = current_waypoint.zoom,
name = current_waypoint.name,
tick = tick
}
debug_print('Waypoint_index ' .. waypoint_index .. ' (waypoint #' .. (waypoint_index + 1) .. ') callback in ' .. ticks .. ' ticks')
set_timeout_in_ticks(ticks, callback_function, {func = running_cutscene.func, player_index = player_index, waypoint_index = waypoint_index - 1, params = params})
end
function Public.goTo(player_index, waypoint_index)
local running_cutscene = running_cutscenes[player_index]
if waypoint_index < 1 or waypoint_index > #running_cutscene.waypoints - 1 then
return false
end
Token.get(remove_renderings)(running_cutscene.rendering)
game.get_player(player_index).jump_to_cutscene_waypoint(waypoint_index)
handler({player_index = player_index, waypoint_index = waypoint_index, tick = game.tick})
running_cutscene.current_index = waypoint_index
return true
end
local function restore(event)
Public.terminate_cutscene(event.player_index, 1, true)
end
Event.add(defines.events.on_cutscene_waypoint_reached, handler)
Event.add(defines.events.on_pre_player_left_game, restore)
Event.add(defines.events.on_player_joined_game, restore)
local replay_cutscene =
Token.register(
function(params)
Public.register_running_cutscene(params.event.player_index, replay.identifier, replay.final_transition_time)
end
)
local function replay_handler(_, player)
if not replay.identifier then
player.print({'cutscene_controller.cannot_replay'})
return
end
Token.get(replay_cutscene)({event = {player_index = player.index}})
end
Command.add(
'replay',
{
description = {'cutscene_controller.replay'},
capture_excess_arguments = false,
allowed_by_server = false
},
replay_handler
)
local function skip_cutscene(_, player)
if not player or not player.valid then
return
end
if player.controller_type == defines.controllers.cutscene then
Public.terminate_cutscene(player.index, 1, true)
end
end
Command.add(
'skip',
{
description = {'cutscene_controller.skip'},
capture_excess_arguments = false,
allowed_by_server = false
},
skip_cutscene
)
Gui.on_click(
skip_btn_name,
function(event)
skip_cutscene(nil, game.get_player(event.player_index))
end
)
Gui.on_click(
backward_btn_name,
function(event)
local player_index = event.player_index
if Public.goTo(player_index, running_cutscenes[player_index].current_index - 1) == false then
game.get_player(player_index).print("Cutscene: You're already at the beginning")
end
end
)
Gui.on_click(
forward_btn_name,
function(event)
local player_index = event.player_index
if Public.goTo(player_index, running_cutscenes[player_index].current_index + 1) == false then
game.get_player(player_index).print("Cutscene: You're already at the end")
end
end
)
Gui.on_checked_state_changed(auto_play_cutscene_checkbox_name, function(event)
local checkbox = event.element
Settings.set(event.player_index, auto_play_cutscene_setting_name, checkbox.state)
end)
Event.add(Settings.events.on_setting_set, function(event)
if auto_play_cutscene_setting_name == nil or event.setting_name ~= auto_play_cutscene_setting_name then
return
end
local running_cutscene = running_cutscenes[event.player_index]
if not running_cutscene then
return
end
running_cutscene.auto_play_cutscene_checkbox.state = event.new_value
end)
return Public