-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
713 lines (563 loc) · 13 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
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
712
713
wWidth, wHeight = 1000, 675
rWidth, rHeight = love.graphics.getDimensions()
currGamestate = "menu"
scalerW, scalerH, centered = 0,0,0
Ftimer = 0
OS = love.system.getOS()
isMobile = OS == "Android"
inasversion = "1.5"
lg = love.graphics
la = love.audio
lm = love.mouse
lk = love.keyboard
lfs = love.filesystem
-- ANCHOR Libs
utf8 = require "utf8"
lily = require "libs.lily.lily"
timer = require "libs.hump.timer"
class = require "libs.hump.class"
lume = require "libs.lume.lume"
gamejolt = require "libs.gamejolt"
chain = require "libs.grove.chainshaders"
animation = require "libs.grove.animation"
moonshine = require "libs.moonshine"
kuey = require "libs.kuey"
local fpsgraph = require "libs.fpsgraph.FPSGraph"
local resolution = require "libs.grove.resolution"
lume.extend(_G, math)
-- ANCHOR Shaders
perspective = lg.newShader("shaders/perspective.glsl")
chromaticAberration = lg.newShader("shaders/chromatic_aberration.glsl")
perspective:send("depth", 3.5)
--Moonshine effects
blur = moonshine(moonshine.effects.gaussianblur)
blur.gaussianblur.sigma = 5
--Debug graphs
local graphs = {
fpsgraph.createGraph(0, 0, 50, 30, 0.5),
fpsgraph.createGraph(0, 35, 50, 30, 0.5)
}
-----------------
--Resource tables
-----------------
res = {}
menuMusic = la.newSource("sounds/musics/menu.ogg", "stream")
menuMusic:setLooping(true)
menuMusic:setVolume(.65)
-- ANCHOR Languages
langNumber = 1
languages = {}
local mt = {__index = function(self, k)
self[k] = {}
return self[k]
end}
for i, file in pairs(lfs.getDirectoryItems("lang")) do
lang = setmetatable({}, mt)
require("lang."..string.sub(file, 1, -5))
languages[i] = lume.clone(lang)
end
glang = languages[1]
lang = glang.menu
-- ANCHOR Extra files
require "tools"
require "devmenu"
require "dialf"
require "subtitles"
require "love_run"
trophies = require "achievements"
imgui = require "imgui"
-- ANCHOR Gamestate files
gamestates = {}
for _, file in ipairs(lfs.getDirectoryItems("states")) do
local name = file:sub(1, -5)
gamestates[name] = require("states."..name)
end
gamestates.cp = require "controlpanel"
-----------------
--ANCHOR Settings
-----------------
unlocks = {
beatgame = true,
sixnight = true,
insnight = true
}
resConf = {
replace = {},
width = wWidth,
height = wHeight,
centered = true,
aspectRatio = false,
clip = true,
clampMouse = true
}
subtitles = true
widescreen = false
showdebugmenu = false
fddur = 0 --Fade duration
fdalpha = 0 --Fade alpha
if OS == "Windows" then
rpc = require "libs.discordRPC.discordRPC"
end
-- ANCHOR Discord RPC
local presence = {
state = "In game",
details = "Night 1",
largeImageKey = "icon",
largeImageText = "INaS"
}
local function updatePresence()
if not rpc or not dbg.discord_rpc then
return
end
if currGamestate == "office" or currGamestate == "snight" then
presence.details = "In game"
if night <= 6 then
presence.state = languages[1].office.night.." "..night
end
if night == 7 then
presence.state = languages[1].menu["op1_4"]
end
if night == 8 then
presence.state = languages[1].customnight["c"..(currChallenge or 0)]
end
if night == 9 then
presence.state = languages[1].menu.secretnight
end
else
presence.details = "Idle"
presence.state = nil
end
rpc.updatePresence(presence)
end
-- TODO Substituir pelo hump.gamestate
gsStack = {""}
--Load a gamestate
function loadgamestate(stt, arg)
Ftimer = 0
fdin = false
fddur = 0
fdalpha = 0
--For better randomness
randomseed(os.time())
--Reset subtitles
subtitle.clear()
subtitle.pause = false
subtitle.opacity = 0
if currGamestate and gamestates[currGamestate].exit then
--Cancel exit
if gamestates[currGamestate]:exit() then
return
end
end
timer.clear()
collectgarbage()
currGamestate = stt
gsStack[#gsStack] = stt
if glang[currGamestate] then
lang = glang[currGamestate]
end
if gamestates[stt].enter then
gamestates[stt]:enter(arg)
end
timer.every(5, updatePresence)
block_dt = true
end
--Push a gamestate to the top of stack
function pushgamestate(stt, arg)
Ftimer = 0
fdin = false
fddur = 0
fdalpha = 0
--For better randomness
randomseed(os.time())
--Reset subtitles
subtitle.clear()
subtitle.pause = false
subtitle.opacity = 0
timer.clear()
collectgarbage()
currGamestate = stt
toLast(gsStack, stt)
if glang[currGamestate] then
lang = glang[currGamestate]
end
if gamestates[stt].enter then
gamestates[stt]:enter(arg)
end
timer.every(5, updatePresence)
block_dt = true
end
--Pop current gamestate from stack
function popgamestate(arg)
Ftimer = 0
fdin = false
fddur = 0
fdalpha = 0
--For better randomness
randomseed(os.time())
--Reset subtitles
subtitle.clear()
subtitle.pause = false
subtitle.opacity = 0
if gamestates[currGamestate].exit then
--Cancel exit
if gamestates[currGamestate]:exit() then
return
end
end
timer.clear()
collectgarbage()
assert(#gsStack > 1, "No more states to pop")
table.remove(gsStack, #gsStack)
currGamestate = gsStack[#gsStack]
if glang[currGamestate] then
lang = glang[currGamestate]
end
if gamestates[currGamestate].resume then
gamestates[currGamestate]:resume(arg)
end
timer.every(5, updatePresence)
block_dt = true
end
function fade(out, dur, stt, arg)
if fdalpha <= 0 then
fdin = out
fddur = dur
fdalpha = 1
if out then
timer.after(dur, function()
if stt then
loadgamestate(stt, arg)
else
popgamestate(arg)
end
end)
end
end
end
------------------------
-- ANCHOR LÖVE Callbacks
------------------------
function love.load(args)
-- ANCHOR Load settings
loadgame()
night = savedata.level
unlocks = savedata.unlocks
langNumber = savedata.lang
subtitles = savedata.sub
lowgraphics = savedata.lowgpcs
resConf.aspectRatio = savedata.wide
trophies:loadAchieved()
glang = languages[langNumber]
-- ANCHOR Gamejolt stuff (please don't steal)
local keys = require("apikeys")
gamejolt.init(keys.gj_id, keys.gj_key, args)
--Discord Rich Presence
if rpc then
rpc.initialize(keys.rpc_id, true)
end
if not gamejolt.isLoggedIn and savedata.username then
pcall(gamejolt.authUser, savedata.username, savedata.token)
end
trophies:achieve()
resolution.init(resConf)
-- ANCHOR Initial state
loadgamestate("systemcheck")
debugmenu.init(args)
end
function love.draw()
gamestates[currGamestate]:draw()
lg.setColor(1, 1, 1, 1)
subtitle:draw()
if fdalpha > 0 then
if fdin then
lg.setColor(0, 0, 0, 1 - fdalpha)
else
lg.setColor(0, 0, 0, fdalpha)
end
lg.rectangle("fill", 0, 0, wWidth, wHeight)
lg.setColor(1, 1, 1, 1)
end
dialog.draw()
trophies:draw()
-- ANCHOR Draw debug menu
if showdebugmenu then
if debugmenu.focus then
lg.setColor(0, 0, 0, 0.2)
lg.rectangle("fill", 0, 0, 1000, 675)
lg.setColor(1, 1, 1, 1)
end
lg.push()
lg.origin()
lg.setLineWidth(1)
debugmenu.draw()
lg.pop()
lg.setLineWidth(1)
lg.setColor(1, 1, 1, 1)
setFont()
fpsgraph.drawGraphs(graphs)
local stats = "Graphic stats:"
local sinfo = lg.getStats()
sinfo.texturememory = lume.round(sinfo.texturememory / 1024 / 1024, 0.01).." MB"
for k, v in pairs(sinfo) do
stats = stats.."\n"..k..": "..v
end
setFont(14)
lg.print(string.format("Debug:\nState: %s\nState stack: %d\nTime: %02d:%02d\nOS: %s\nEmpty tables pool: %s\n\n%s\n\n%s", currGamestate, #gsStack, Ftimer/60, Ftimer % 60, OS, #table_pool, stats, imgui._info), 0, 70)
lg.print(string.format("mouse X: %d\nmouse Y: %d", RXmouse, RYmouse), 0, 600)
end
end
block_dt = false
function love.update(delta)
if showdebugmenu then
debugmenu.update(dt)
end
if dbg.block_update then
return
end
if dbg.frameskip then
if dbg.prevent_big_delta then
dt = min(delta, 1)
else
dt = delta
end
else
dt = 1/60
end
if block_dt then
dt = 0
block_dt = false
end
Ftimer = Ftimer + dt
--Fade
if fdalpha > 0 then
fdalpha = fdalpha - (1/fddur) * dt
end
RXmouse, RYmouse = lm.getX(), lm.getY()
-- ANCHOR Updates
if gamestates[currGamestate].update then
gamestates[currGamestate]:update()
end
timer.update(dt)
subtitle:update()
trophies:update()
dialog.update()
fpsgraph.updateFPS(graphs[1], delta)
fpsgraph.updateMem(graphs[2], delta)
if rpc then
rpc.runCallbacks()
end
end
----------------------------
-- ANCHOR Some helpful tools
----------------------------
--Clear all loaded resources
resourcecleared = true
function clearResources()
for i, t in pairs(res) do
if type(t) == "table" then
recicleTable(t)
end
end
lume.clear(res)
resourcecleared = true
collectgarbage()
collectgarbage()
end
function simulplay(data, x, vol, pitch)
local sound = la.newSource(data, "static")
if x and x ~= 0 then
sound:setRelative(true)
sound:setPosition(x,0,0)
end
sound:setVolume(vol or .3)
sound:setPitch(pitch or 1)
sound:play()
end
function string.explode(str, div, plain)
assert(type(str) == "string" and type(div) == "string", "invalid arguments")
local o = emptyTable()
while true do
local pos1,pos2 = str:find(div, 1, plain)
if not pos1 then
o[#o+1] = str
break
end
o[#o+1],str = str:sub(1,pos1-1),str:sub(pos2+1)
end
return o
end
-------------------
-- ANCHOR Save data
-------------------
realsavedata = {}
savedata = setmetatable({}, {
__newindex = function(_, k, v)
realsavedata[k] = v
savegame({[k] = v})
end,
__index = function(_, k)
return realsavedata[k]
end
})
local emptysave = {
level = 0,
lang = boolto(os.setlocale() == "pt_BR", 2, 1),
sub = true,
lowgpcs = 1,
wide = false,
fullscreen = true,
startip = false,
trophies = {},
cnchallenge = {},
drawings = {},
jumpscared = {
snowball = {},
bunny = {},
konny = {},
beavy = {},
larry = {},
shattered = {},
powerdown = {},
sn = {}
},
unlocks = {
beatgame = false,
sixnight = false,
insnight = false
}
}
function log_err(message, text)
local vlbutton = {glang.view, function()
os.execute("explorer "..lfs.getSaveDirectory():gsub("/", "\\").."\\error.txt")
end}
dialog(message, {"OK"}, {glang.quit, love.event.quit}, boolto(OS == "Windows", nil, vlbutton))
local date = os.date("\r\n\r\n%x %X - ")
local file = ""
if lfs.getInfo("error.txt") then
local loaded = lfs.read("error.txt")
if loaded then
file = loaded
else
lfs.remove("error.txt")
end
end
local output = file..date..message..": "..text
lfs.write("error.txt", output)
end
--function love.errorhandler(msg)
-- log_err("error: ", msg)
--end
--Save game
function savegame(t)
t = lume.merge(loadgame(), t)
local ser = lume.serialize(t)
local data = kuey.encode(ser, "inaskey")
local ok, err = lfs.write("inas.sav", data)
if ok then
realsavedata = t
else
log_err(glang.saveErr, err)
end
end
--Load game
function loadgame()
if lfs.getInfo("inas.sav") then
local file, err = lfs.read("inas.sav")
if file then
local ok, loaded = pcall(kuey.decode, file, "inaskey")
if ok then
ok, loaded = pcall(lume.deserialize, loaded)
end
if ok then
realsavedata = loaded
else
log_err(glang.loadErr, loaded)
lfs.write("inas.sav.bak", file)
lfs.remove("inas.sav")
realsavedata = deepcopy(emptysave)
end
else
if lfs.getInfo("inas.sav") then
log_err(glang.loadErr, err)
end
realsavedata = deepcopy(emptysave)
end
else
realsavedata = deepcopy(emptysave)
end
return realsavedata
end
--Empty function
function fnull(...)
return ...
end
function love.keypressed(k, unicode)
if k == "f3" then
showdebugmenu = not showdebugmenu
debugmenu.focus = false
if not showdebugmenu then
lm.setCursor()
end
end
if showdebugmenu and debugmenu.focus then
return
end
if gamestates[currGamestate].keypressed and (#dialogList == 0 or currGamestate == "menu") then
gamestates[currGamestate]:keypressed(k)
end
if k == "escape" then
dialog(glang.exit, {glang.mmenu, function()loadgamestate("menu")end},{glang.desktop, love.event.quit}, {glang.cancel})
end
if k == "f2" then
dialog(glang.restart, {glang.yes, function()love.event.quit("restart")end}, {glang.no})
end
end
function love.mousepressed(x, y, button)
RXmouse, RYmouse = x, y
if showdebugmenu and debugmenu.focus then
return
end
if gamestates[currGamestate].mousepressed and #dialogList == 0 then
gamestates[currGamestate]:mousepressed(x, y, button)
end
if #dialogList > 0 and not dclosing then
dialog.mousepressed()
end
end
function love.mousereleased(x, y, button)
RXmouse, RYmouse = x, y
if showdebugmenu and debugmenu.focus then
return
end
if gamestates[currGamestate].mousereleased then
gamestates[currGamestate]:mousereleased(x, y, button)
end
end
function love.textinput(text)
if gamestates[currGamestate].textinput then
gamestates[currGamestate]:textinput(text)
end
end
function love.wheelmoved(x, y)
if gamestates[currGamestate].wheelmoved then
gamestates[currGamestate]:wheelmoved(x, y)
end
end
function love.mousemoved(x, y, dx, dy)
if gamestates[currGamestate].mousemoved then
gamestates[currGamestate]:mousemoved(x, y, dx, dy)
end
end
function love.lowmemory()
res.jumpscares = nil
dbg.load_jumpscares = false
collectgarbage()
collectgarbage()
end
function love.quit()
lily.quit()
if rpc then
rpc.shutdown()
end
end