-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.lua
458 lines (364 loc) · 12.7 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
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
io.stdout:setvbuf("no")
__print = print
require "loxel"
API = require "funkin.backend.api"
if love.system.getOS() == "Windows" then
WindowDialogue = require "lib.windows.dialogue"
WindowUtil = require "lib.windows.util"
local _ogSetMode = love.window.setMode
function love.window.setMode(...)
_ogSetMode(...)
WindowUtil.setDarkMode(true)
end
end
Timer = require "lib.timer"
Https = require "lib.https"
paths = require "funkin.paths"
util = require "funkin.util"
ClientPrefs = require "funkin.backend.clientprefs"
Conductor = require "funkin.backend.conductor"
Highscore = require "funkin.backend.highscore"
Mods = require "funkin.backend.mods"
Script = require "funkin.backend.scripting.script"
ScriptsHandler = require "funkin.backend.scripting.scriptshandler"
Throttle = require "funkin.backend.throttle"
if love.system.getDevice() == "Desktop" then
Discord = require "funkin.backend.discord"
end
Receptor = require "funkin.gameplay.receptor"
Note = require "funkin.gameplay.note"
Notefield = require "funkin.gameplay.notefield"
Countdown = require "funkin.gameplay.ui.countdown"
HealthIcon = require "funkin.gameplay.ui.healthicon"
HealthBar = require "funkin.gameplay.ui.healthbar"
ProgressArc = require "funkin.gameplay.ui.progressarc"
NoteModifier = require "funkin.gameplay.notemods.notemodifier"
BackgroundDancer = require "funkin.gameplay.backgrounddancer"
BackgroundGirls = require "funkin.gameplay.backgroundgirls"
Character = require "funkin.gameplay.character"
Stage = require "funkin.gameplay.stage"
TankmenBG = require "funkin.gameplay.tankmenbg"
Judgements = require "funkin.gameplay.ui.judgements"
Alphabet = require "funkin.ui.alphabet"
MenuCharacter = require "funkin.ui.menucharacter"
MenuItem = require "funkin.ui.menuitem"
ModCard = require "funkin.ui.modcard"
Options = require "funkin.ui.options"
StatsCounter = require "funkin.ui.statscounter"
SoundTray = require "funkin.ui.soundtray"
CalibrationState = require 'funkin.states.calibration'
CreditsState = require "funkin.states.credits"
TitleState = require "funkin.states.title"
MainMenuState = require "funkin.states.mainmenu"
ModsState = require "funkin.states.mods"
StoryMenuState = require "funkin.states.storymenu"
FreeplayState = require "funkin.states.freeplay"
PlayState = require "funkin.states.play"
GameOverSubstate = require "funkin.substates.gameover"
StickersSubstate = require "funkin.substates.stickers"
EditorMenu = require "funkin.ui.editor.editormenu"
CutsceneState = require "funkin.states.editors.cutscene"
CharacterEditor = require "funkin.states.editors.character"
ChartingState = require "funkin.states.editors.charting"
ChartingNote = require "funkin.ui.editor.charting.chartingnote"
RGBShader = require "funkin.shaders.rgb"
local TransitionFade = loxreq "transition.transitionfade"
local SplashScreen = require "funkin.states.splash"
function love.load()
game.save.init('funkin')
pcall(table.merge, ClientPrefs.data, game.save.data.prefs)
pcall(table.merge, ClientPrefs.controls, game.save.data.controls)
if game.save.data.prefs then
love.FPScap = ClientPrefs.data.fps
love.parallelUpdate = ClientPrefs.data.parallelUpdate
love.asyncInput = ClientPrefs.data.asyncInput
love.autoPause = ClientPrefs.data.autoPause
else
ClientPrefs.data.fps = love.FPScap
ClientPrefs.data.parallelUpdate = love.parallelUpdate
ClientPrefs.data.resolution = -1
end
Object.defaultAntialiasing = ClientPrefs.data.antialiasing
pcall(table.merge, ClientPrefs.controls, game.save.data.controls)
local config = {controls = table.clone(ClientPrefs.controls)}
if controls == nil then
controls = (require "lib.baton").new(config)
else
controls:reset(config)
end
local res, isMobile = math.abs(ClientPrefs.data.resolution), love.system.getDevice() == "Mobile"
Camera.defaultResolution = res
love.window.setTitle(Project.title)
love.window.setIcon(love.image.newImageData(Project.icon))
love.window.setMode(Project.width * res, Project.height * res, {
fullscreen = isMobile or ClientPrefs.data.fullscreen,
resizable = not isMobile,
vsync = 0,
usedpiscale = false
})
if Project.bgColor then
love.graphics.setBackgroundColor(Project.bgColor)
end
Mods.loadMods()
Highscore.load()
local color = Color.BLACK
State.defaultTransIn = TransitionFade(0.6, color, "vertical")
State.defaultTransOut = TransitionFade(0.7, color, "vertical")
game.onPreStateEnter = function(state)
if paths and getmetatable(state) ~= getmetatable(game.getState()) then
paths.clearCache()
end
end
SoundTray.init(love.graphics.getDimensions())
game:add(SoundTray)
SoundTray.new()
game.init(Project, SplashScreen)
if ClientPrefs.data.resolution == -1 then
Camera.defaultResolution = love.graphics.getFixedScale()
ClientPrefs.data.resolution = Camera.defaultResolution
for _, camera in ipairs(game.cameras.list) do
if camera then camera:resize(camera.width, camera.height, value) end
end
end
game.statsCounter = StatsCounter(6, 6, love.graphics.newFont('assets/fonts/consolas.ttf', 14),
love.graphics.newFont('assets/fonts/consolas.ttf', 18))
game.statsCounter.showFps = ClientPrefs.data.showFps
game.statsCounter.showRender = ClientPrefs.data.showRender
game.statsCounter.showMemory = ClientPrefs.data.showMemory
game.statsCounter.showDraws = ClientPrefs.data.showDraws
game:add(game.statsCounter)
if Discord then
Discord.init()
end
function print(...)
local v = {...}
for i = 1, #v do v[i] = tostring(v[i]) end
if ClientPrefs.data.toastPrints then
Toast.new(table.concat(v, ", "))
end
__print(...)
end
end
function love.resize(w, h) game.resize(w, h) end
function love.keypressed(key, ...)
if key == "f5" then
game.resetState(true)
elseif Project.DEBUG_MODE and love.keyboard.isDown("lctrl", "rctrl") then
if key == "f4" then error("force crash") end
if key == "`" then return "restart" end
end
controls:onKeyPress(key, ...)
game.keypressed(key, ...)
end
function love.keyreleased(...)
controls:onKeyRelease(...)
game.keyreleased(...)
end
function love.wheelmoved(...) game.wheelmoved(...) end
function love.mousemoved(...) game.mousemoved(...) end
function love.mousepressed(...) game.mousepressed(...) end
function love.mousereleased(...) game.mousereleased(...) end
function love.touchmoved(...) game.touchmoved(...) end
function love.touchpressed(...) game.touchpressed(...) end
function love.touchreleased(...) game.touchreleased(...) end
function love.update(dt)
controls:update()
Throttle:update(dt)
Timer.update(dt)
game.update(dt)
if Discord then Discord.update() end
if controls:pressed("fullscreen") then love.window.setFullscreen(not love.window.getFullscreen()) end
end
function love.draw()
game.draw()
end
function love.focus(f) game.focus(f) end
function love.fullscreen(f, t)
ClientPrefs.data.fullscreen = f
game.fullscreen(f)
end
function love.quit()
ClientPrefs.saveData()
game.quit()
if Discord then
Discord.shutdown()
end
end
local function error_printer(msg, layer)
print((debug.traceback("Error: " .. tostring(msg), 1 + (layer or 1)):gsub("\n[^\n]+$", "")))
end
function love.errorhandler(msg)
pcall(love.errorhandler_quit)
msg = tostring(msg)
error_printer(msg, 2)
if not love.window or not love.graphics or not love.event then return end
if not love.window.isOpen() then
local success, status = pcall(love.window.setMode, 800, 600)
if not success or not status then return end
end
local trace = debug.traceback()
if utf8 == nil then utf8 = require("utf8") end
local sanitizedmsg, err = {}, {}
for char in msg:gmatch(utf8.charpattern) do
table.insert(sanitizedmsg, char)
end
sanitizedmsg = table.concat(sanitizedmsg)
table.insert(err, sanitizedmsg)
if #sanitizedmsg ~= #msg then
table.insert(err, "Invalid UTF-8 string in error message.")
end
table.insert(err, "\n")
for l in trace:gmatch("(.-)\n") do
l = l:gsub("stack traceback:", "Traceback\n")
table.insert(err, l)
end
local p = table.concat(err, "\n"):gsub("\t", ""):gsub("%[string \"(.-)\"%]", "%1")
local fullErrorText = p
love.graphics.reset()
love.graphics.setCanvas()
love.graphics.setColor(1, 1, 1)
love.graphics.origin()
if love.mouse then
love.mouse.setVisible(true)
love.mouse.setGrabbed(false)
love.mouse.setRelativeMode(false)
if love.mouse.isCursorSupported() then love.mouse.setCursor() end
end
if love.joystick then
for i, v in ipairs(love.joystick.getJoysticks()) do
v:setVibration()
end
end
if love.window then
love.window.setFullscreen(false)
love.window.setDisplaySleepEnabled(true)
end
if love.audio then love.audio.stop() end
if love.handlers then love.handlers = nil end
collectgarbage()
collectgarbage()
local interactTxt = ""
if love.system then
interactTxt = interactTxt .. "\n\nPress Ctrl+C or tap to copy this error"
end
interactTxt = interactTxt .. "\nPress ESC to quit"
interactTxt = interactTxt .. "\nPress Ctrl+R to restart"
local menuDesat, funkinLogo, fnfFont18, fnfFont20
local bgMusic, missSfx
function firstPass()
menuDesat = paths.getImage("menus/menuDesat")
funkinLogo = paths.getImage("menus/splashscreen/FNFLOVE_logo")
fnfFont18 = paths.getFont("phantommuff.ttf", 18) or love.graphics.setNewFont(18)
fnfFont20 = paths.getFont("phantommuff.ttf", 35) or love.graphics.setNewFont(35)
bgMusic = paths.getMusic("pause/railways", "static")
missSfx = love.audio.newSource(paths.getSound("gameplay/missnote" .. love.math.random(1, 3)), "static")
bgMusic:setLooping(true)
bgMusic:setVolume(0.7)
bgMusic:play()
missSfx:setVolume(0.4)
missSfx:play()
end
local dontDraw = false
local __error__, __align__, focused = "[ ERROR ]", "left"
local scale1, scale2, gameW, gameH, hgameW, hgameH, retval
local menuDesatW, menuDesatH, funkinLogoW, funkinLogoH
local function draw(force)
if not force and dontDraw then return end
love.graphics.clear(0, 0, 0)
hgameW, hgameH = gameW / 2, gameH / 2
menuDesatW, menuDesatH = menuDesat:getWidth(), menuDesat:getHeight()
funkinLogoW, funkinLogoH = funkinLogo:getWidth(), funkinLogo:getHeight()
scale1 = math.max(gameW / menuDesatW, gameH / menuDesatH)
scale2 = math.max(math.min(gameW, 1600) / funkinLogoW, math.min(gameH, 900) / funkinLogoH) * 0.525
love.graphics.setColor(0.2, 0.2, 0.2)
love.graphics.draw(menuDesat, hgameW, hgameH, 0, scale1, scale1, menuDesatW / 2, menuDesatH / 2)
love.graphics.draw(funkinLogo, (hgameW * 2) - (scale2 * funkinLogoW / 1.8) - 64, hgameH, 0, scale2, scale2, funkinLogoW / 2, funkinLogoH / 2)
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(fnfFont20)
love.graphics.printf(__error__, 40, 40, love.graphics.getWidth() - 80, __align__)
love.graphics.setFont(fnfFont18)
love.graphics.printf(p, 40, 110, love.graphics.getWidth() - 80, __align__)
love.graphics.setFont(fnfFont18)
love.graphics.printf(interactTxt, 40, love.graphics.getHeight() - 130, love.graphics.getWidth() - 80, __align__)
love.graphics.present()
end
local function copyToClipboard()
love.system.setClipboardText(fullErrorText)
p = p .. "\nCopied to clipboard!"
draw()
end
eventhandlers = {
quit = function()
return 1
end,
keypressed = function(key)
if key == "escape" then return 1 end
if not love.keyboard.isDown("lctrl", "rctrl") then return end
if love.system and key == "c" then
copyToClipboard()
elseif key == "r" then
return "restart"
end
end,
touchpressed = function()
local name = love.window.getTitle()
if #name == 0 or name == "Untitled" then name = "Game" end
local buttons = {"OK", "Cancel", "Restart"}
if love.system then buttons[4] = "Copy to clipboard" end
local pressed = love.window.showMessageBox("Quit " .. name .. "?", "", buttons)
if pressed == 1 then
return 1
elseif pressed == 3 then
return "restart"
elseif pressed == 4 then
copyToClipboard()
end
end,
focus = function(f)
bgMusic:setVolume(f and 0.7 or 0.3)
end,
resize = function(w, h)
gameW, gameH = w, h
draw()
end,
displayrotated = function(force)
gameW, gameH = love.graphics.getDimensions()
draw(force)
end
}
local __step__, name, a, b = "step"
--[[ if love.system.getDevice() == "Mobile" then
dontDraw = true
local first, done = true, false
return function()
if first then
first = false
return
end
love.event.pump()
for name, a in love.event.poll() do
if eventhandlers[name] ~= nil then
retval = eventhandlers[name](a)
if retval then return retval end
end
end
if not done then
firstPass()
end
eventhandlers.displayrotated(true)
done = true
collectgarbage(__step__)
sleep(0.1)
end
end]]
firstPass()
eventhandlers.displayrotated(true)
return function()
name, a, b = love.event.wait()
if eventhandlers[name] ~= nil then
collectgarbage(__step__)
retval = (eventhandlers[name])(a, b)
if retval then return retval end
end
end
end