-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
341 lines (321 loc) · 11.9 KB
/
main.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
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
meta.name = "TAS Wizard"
meta.version = "1.1.1"
meta.description = "A mod for creating tool-assisted speedruns."
meta.author = "Cosine"
-- Check for the latest added feature needed by this mod to ensure that it's not being loaded in an outdated version of the script API.
if type(pause) ~= "userdata" then
local message = "ERROR: This is an outdated version of Playlunky or Overlunky that lacks features required by TAS Wizard! You might need the nightly version!"
print(message)
register_option_callback("", options, function(ctx)
ctx:win_text(message)
end)
return
end
local common = require("common")
local common_enums = require("common_enums")
local drawing = require("gui/drawing")
local game_controller = require("game_controller")
local pause_lib = require("pause")
local persistence = require("persistence")
local Tas = require("tas")
local TasSession = require("tas_session")
tool_guis = {
file = require("gui/file"),
frames = require("gui/frames"),
ghost = require("gui/ghost"),
options = require("gui/options"),
playback_record = require("gui/playback_record"),
root = require("gui/root"),
single_frame_editor = require("gui/single_frame_editor"),
status = require("gui/status"),
tas_data = require("gui/tas_data"),
tas_root = require("gui/tas_root"),
tas_settings = require("gui/tas_settings"),
warp = require("gui/warp")
}
local CURRENT_SCRIPT_DATA_FORMAT = 1
default_options = {
tas_file_name = "tas.json",
tas_file_history = {},
tas_file_history_max_size = 10,
playback_from = "here_or_nearest_screen",
playback_target_mode = "playback",
playback_target_pause = true,
record_frame_clear_action = "remaining_tas",
record_frame_write_type = "overwrite",
playback_from_here_unpause = true,
playback_from_warp_unpause = true,
playback_screen_load_pause = false,
record_screen_load_pause = true,
pause_type_force_supported = true,
desync_pause = true,
pause_suppress_transition_tas_inputs = true,
playback_fast_update = false,
fast_update_flash_prevention = true,
transition_skip = false,
presentation_start_on_playback = false,
presentation_stop_after_playback = false,
presentation_mode_watermark_visible = false,
frames_viewer_follow_current = true,
frames_viewer_page_size = 10,
frames_viewer_step_size = 10,
active_path_visible = true,
ghost_path_visible = true,
path_frame_mark_visible = true,
path_frame_mark_label_visible = true,
path_frame_mark_label_size = 20.0,
path_frame_mark_interval = 30,
path_frame_tag_visible = true,
path_frame_tag_label_visible = true,
path_frame_tag_label_size = 20.0,
mode_watermark_visible = true,
mode_watermark_x = 0.0,
mode_watermark_y = -0.95,
mode_watermark_size = 32.0,
new_tas = {
name = "Unnamed TAS",
description = "",
start_type = "simple",
start_simple = {
seed_type = "seeded",
seeded_seed = 0x00000000,
-- Adventure seed 0,0 generates runs with unusual similarity between levels. This default adventure seed has better behavior.
adventure_seed = { 0x0000000000000001, 0x0000000000000000 },
is_custom_preset = false,
screen = SCREEN.LEVEL,
world = 1,
level = 1,
theme = THEME.DWELLING,
shortcut = false,
tutorial_race = false,
tutorial_race_referee = "margaret",
player_count = 1,
players = { "ana", "margaret", "colin", "roffy" },
},
start_snapshot = {},
screens = {},
frame_tags = {
{
name = "Start",
screen = 1,
frame = 0,
show_on_path = false
},
{
name = "End",
screen = -1,
frame = -1,
show_on_path = false
}
},
save_player_positions_default = true,
save_screen_snapshot_defaults = {
[common_enums.TASABLE_SCREEN[SCREEN.CAMP].data_id] = true,
[common_enums.TASABLE_SCREEN[SCREEN.LEVEL].data_id] = true
}
},
tool_guis = {
file = {
windowed = false,
window_settings = { x = -0.3, y = 0.5, w = 0.4, h = 0.6 }
},
frames = {
windowed = false,
window_settings = { x = -0.59, y = 0.8, w = 0.4, h = 1.2 }
},
ghost = {
windowed = false,
window_settings = { x = -0.25, y = 0.45, w = 0.4, h = 0.3 }
},
options = {
windowed = false,
window_settings = { x = -0.2, y = 0.4, w = 0.4, h = 1.0 }
},
playback_record = {
windowed = false,
window_settings = { x = 0.6, y = 0.8, w = 0.4, h = 1.0 }
},
root = {
windowed = true,
window_settings = { x = 0.6, y = -0.21, w = 0.4, h = 0.74 }
},
single_frame_editor = {
window_settings = { x = -0.2, y = 0.25, w = 0.4, h = 0.5 }
},
status = {
windowed = false,
window_settings = { x = -0.15, y = 0.35, w = 0.4, h = 0.6 }
},
tas_data = {
windowed = false,
window_settings = { x = -0.1, y = 0.3, w = 0.4, h = 1.2 }
},
tas_root = {
windowed = true,
window_settings = { x = -1.0, y = 0.8, w = 0.4, h = 1.2 }
},
tas_settings = {
windowed = false,
window_settings = { x = -0.05, y = 0.25, w = 0.4, h = 0.8 }
},
warp = {
windowed = false,
window_settings = { x = 0.0, y = 0.2, w = 0.4, h = 0.4 }
}
},
debug_prints = {
fast_update = false,
file = false,
input = false,
misc = false,
mode = false,
pause = false,
screen_load = false,
snapshot = false
}
}
---@type TasSession?
active_tas_session = nil
---@type TasSession?
ghost_tas_session = nil
presentation_active = false
function print_debug(category, format_string, ...)
if not presentation_active and options.debug_prints[category] then
print("[Debug] "..string.format(format_string, ...))
end
end
function print_info(format_string, ...)
print("[Info] "..string.format(format_string, ...))
end
function print_warn(format_string, ...)
print("[Warning] "..string.format(format_string, ...))
end
local function save_script_data(save_ctx)
local save_data = {
format = CURRENT_SCRIPT_DATA_FORMAT,
options = common.deep_copy(options)
}
save_data.options.new_tas = options.new_tas:to_raw(Tas.SERIAL_MODS.OPTIONS)
local save_json = json.encode(save_data)
local success, err = pcall(function()
save_ctx:save(save_json)
end)
if not success then
print_warn("Failed to save script data: %s", err)
end
end
local function set_to_default_option_if_nil(options, option_id)
if options[option_id] == nil then
options[option_id] = common.deep_copy(default_options[option_id])
end
end
-- Perform a soft update on the script data. A "soft update" consists of minor changes to the data that don't break compatibility with older versions of the mod and don't require a change to the format number. This is mostly limited to adding new options which would be ignored by older versions, or new combo option choices that would fall back to valid choices in older versions. Soft updates allow for some forward compatibility in older versions, and they help to avoid long chains of format updaters containing only minor changes. When a format update is required, any current soft updates will be removed from here and added to the new format updater.
local function soft_update_script_data(load_data)
set_to_default_option_if_nil(load_data.options, "playback_from_here_unpause")
set_to_default_option_if_nil(load_data.options, "playback_from_warp_unpause")
set_to_default_option_if_nil(load_data.options, "pause_type_force_supported")
end
local function load_script_data(load_ctx)
local load_json
local load_data
local success, err = pcall(function()
load_json = load_ctx:load()
end)
if not success then
print_warn("Failed to load script data: %s", err)
else
local result
success, result = persistence.json_decode(load_json, true)
if not success then
print_warn("Failed to load script data: %s", result)
elseif result then
load_data = result
end
end
if load_data then
persistence.update_format(load_data, CURRENT_SCRIPT_DATA_FORMAT, {})
soft_update_script_data(load_data)
options = load_data.options
options.new_tas = Tas:from_raw(options.new_tas, Tas.SERIAL_MODS.OPTIONS)
else
options = common.deep_copy(default_options)
options.new_tas = Tas:from_raw(options.new_tas, Tas.SERIAL_MODS.NONE)
end
end
local function on_active_tas_mode_set(old_mode, new_mode)
if presentation_active then
if options.presentation_stop_after_playback and old_mode == common_enums.MODE.PLAYBACK and new_mode ~= common_enums.MODE.PLAYBACK then
presentation_active = false
end
else
if options.presentation_start_on_playback and new_mode == common_enums.MODE.PLAYBACK then
presentation_active = true
end
end
end
-- Set the TAS for a new active TAS session.
function set_active_tas(tas)
if active_tas_session then
if active_tas_session.tas.screen_snapshot_request_id then
game_controller.clear_screen_snapshot_request(active_tas_session.tas.screen_snapshot_request_id)
active_tas_session.tas.screen_snapshot_request_id = nil
end
on_active_tas_mode_set(active_tas_session.mode, nil)
end
if tas then
active_tas_session = TasSession:new(tas)
active_tas_session.set_mode_callback = on_active_tas_mode_set
active_tas_session:find_current_screen()
on_active_tas_mode_set(nil, active_tas_session.mode)
else
active_tas_session = nil
end
for _, tool_gui in pairs(tool_guis) do
tool_gui:reset_session_vars()
end
end
-- Set the TAS for a new ghost TAS session.
function set_ghost_tas(tas)
if tas then
ghost_tas_session = TasSession:new(tas)
ghost_tas_session:find_current_screen()
else
ghost_tas_session = nil
end
end
local function on_gui_frame(ctx)
-- Draw the windows before the background elements. The background elements should not throw any errors, but if they do, then the user will still be able to interact with the windows and save their TAS or disable whatever is causing the errors.
if not presentation_active then
ctx:draw_layer(DRAW_LAYER.WINDOW)
for _, tool_gui in pairs(tool_guis) do
tool_gui:draw_window(ctx)
end
end
ctx:draw_layer(DRAW_LAYER.BACKGROUND)
drawing.update_screen_vars()
if options.fast_update_flash_prevention and game_controller.game_loop_executed_fast_update_batch then
drawing.draw_black_overlay(ctx)
elseif not presentation_active then
if ghost_tas_session and options.ghost_path_visible then
drawing.draw_tas_path(ctx, ghost_tas_session, true)
end
if active_tas_session and options.active_path_visible then
drawing.draw_tas_path(ctx, active_tas_session, false)
end
end
if active_tas_session then
drawing.draw_mode_watermark(ctx)
end
pause_lib.on_gui_frame()
end
set_callback(function(ctx)
load_script_data(ctx)
set_callback(save_script_data, ON.SAVE)
register_option_callback("", options, function(ctx) tool_guis.options:draw_panel(ctx, false) end)
set_callback(on_gui_frame, ON.GUIFRAME)
game_controller.initialize()
for _, tool_gui in pairs(tool_guis) do
tool_gui:initialize()
tool_gui:reset_session_vars()
end
end, ON.LOAD)