This repository has been archived by the owner on May 6, 2024. It is now read-only.
forked from jakubg1/OpenSMCE
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.lua
560 lines (442 loc) · 14.2 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
-- INCLUDE ZONE
-- custom error handler
require("crash")
-- global utility methods
require("src.strmethods")
require("src.mathmethods")
local json = require("com.json")
local Vec2 = require("src.Essentials.Vector2")
local Color = require("src.Essentials.Color")
local Log = require("src.Kernel.Log")
local Debug = require("src.Kernel.Debug")
local BootScreen = require("src.Kernel.BootScreen")
local Game = require("src.Game")
local ExpressionVariables = require("src.ExpressionVariables")
local Settings = require("src.Kernel.Settings")
local DiscordRichPresence = require("src.DiscordRichPresence")
local Network = require("src.Kernel.Network")
local ThreadManager = require("src.ThreadManager")
-- CONSTANT ZONE
_VERSION = "vZB"
_VERSION_NAME = "Zuma Blitz Remake Fork"
_DISCORD_APPLICATION_ID = "797956172539887657"
_START_TIME = love.timer.getTime()
-- Set this to a string of your choice. This will be only printed in log files and is not used anywhere else.
-- You can automate this in i.e. a script by simply adding a `_BUILD_NUMBER = "<your number>"` line at the end of this main.lua file.
_BUILD_NUMBER = "2023-02-15"
-- GLOBAL ZONE
_DisplaySize = Vec2(800, 600)
_DisplayFullscreen = false
_MousePos = Vec2(0, 0)
_KeyModifiers = {lshift = false, lctrl = false, lalt = false, rshift = false, rctrl = false, ralt = false}
-- File system prefix. On Windows defaults to "", on Android defaults to "/sdcard/".
_FSPrefix = ""
---@type Game|BootScreen
_Game = nil
---@type Log
_Log = nil
---@type Debug
_Debug = nil
_Vars = ExpressionVariables()
_Network = Network()
_ThreadManager = ThreadManager()
_TotalTime = 0
_TimeScale = 1
---@type Settings
_EngineSettings = nil
---@type DiscordRichPresence
_DiscordRPC = nil
-- CALLBACK ZONE
function love.load(args)
local msg = args[1] or 'no arguments'
--print (msg)
--local s = loadFile("test.txt")
--print(s)
--print(jsonBeautify(s))
-- Initialize RNG for Boot Screen
local _ = math.randomseed(os.time())
-- Initialize some classes
_Log = Log()
_Debug = Debug()
_EngineSettings = Settings("settings.json")
_DiscordRPC = DiscordRichPresence()
-- Autoload ZBR by default, there is no need to access the boot screen unless requested
if msg ~= "boot" and msg ~= "--boot" then
_LoadGame("ZumaBlitzRemake")
else
_LoadBootScreen()
end
end
function love.update(dt)
_Debug:profUpdateStart()
_MousePos = _PosFromScreen(Vec2(love.mouse.getPosition()))
if _Game then _Game:update(dt * _TimeScale) end
_Log:update(dt)
_Debug:update(dt)
_DiscordRPC:update(dt)
_ThreadManager:update(dt)
-- rainbow effect for the shooter and console cursor blink; to be phased out soon
_TotalTime = _TotalTime + dt
_Debug:profUpdateStop()
end
function love.draw()
--dbg:profDrawStart()
-- Main
if _Game then _Game:draw() end
-- Tests
_Debug:draw()
--dbg:profDrawStop()
end
function love.mousepressed(x, y, button)
if _Game then _Game:mousepressed(x, y, button) end
end
function love.mousereleased(x, y, button)
if _Game then _Game:mousereleased(x, y, button) end
end
function love.wheelmoved(x, y)
if _Game then _Game:wheelmoved(x, y) end
end
function love.keypressed(key)
for k, v in pairs(_KeyModifiers) do if key == k then _KeyModifiers[k] = true end end
-- Backspace is treated exclusively and will trigger repeatedly when held.
love.keyboard.setKeyRepeat(key == "backspace")
if not _Debug.console.active then
if _Game then _Game:keypressed(key) end
end
_Debug:keypressed(key)
end
function love.keyreleased(key)
for k, v in pairs(_KeyModifiers) do if key == k then _KeyModifiers[k] = false end end
if not _Debug.console.active then
if _Game then _Game:keyreleased(key) end
end
_Debug:keyreleased(key)
end
function love.textinput(t)
if not _Debug.console.active then
if _Game then _Game:textinput(t) end
end
_Debug:textinput(t)
end
function love.resize(w, h)
_DisplaySize = Vec2(w, h)
end
function love.quit()
_Log:printt("main", "User-caused Exit...")
if _Game and _Game.quit then _Game:quit(true) end
_DiscordRPC:disconnect()
_Log:save(true)
end
-- FUNCTION ZONE
function _LoadGame(gameName)
_Game = Game(gameName)
_Game:init()
end
function _LoadBootScreen()
_Game = BootScreen()
_Game:init()
end
function _GetDisplayOffsetX()
return (_DisplaySize.x - _Game:getNativeResolution().x * _GetResolutionScale()) / 2
end
function _GetResolutionScale()
return _DisplaySize.y / _Game:getNativeResolution().y
end
function _PosOnScreen(pos)
return pos * _GetResolutionScale() + Vec2(_GetDisplayOffsetX(), 0)
end
function _PosFromScreen(pos)
return (pos - Vec2(_GetDisplayOffsetX(), 0)) / _GetResolutionScale()
end
---Returns precise time amount since this program has been launched.
---The output is more precise than the `_TotalTime` field.
---@return number
function _GetPreciseTime()
return love.timer.getTime() - _START_TIME
end
function _GetRainbowColor(t)
t = t * 3
local r = math.min(math.max(2 * (1 - math.abs(t % 3)), 0), 1) + math.min(math.max(2 * (1 - math.abs((t % 3) - 3)), 0), 1)
local g = math.min(math.max(2 * (1 - math.abs((t % 3) - 1)), 0), 1)
local b = math.min(math.max(2 * (1 - math.abs((t % 3) - 2)), 0), 1)
return Color(r, g, b)
end
---Checks online and returns the newest engine version tag available (i.e. `v0.47.0`). Returns `nil` on failure (for example, when you go offline).
---@return string?
function _GetNewestVersion()
local result = _Network:get("https://api.github.com/repos/jakubg1/OpenSMCE/tags")
if result.code == 200 and result.body then
result.body = json.decode(result.body)
return result.body[1].name
end
return nil
end
---Checks online and executes a function with the newest engine version tag available (i.e. `v0.47.0`) as an argument or `nil` on failure (for example, when you go offline).
---Threaded version: non-blocking call.
---@param onFinish function A function which will be called once the checking process is finished. A version argument is passed.
function _GetNewestVersionThreaded(onFinish)
_Network:getThreaded("https://api.github.com/repos/jakubg1/OpenSMCE/tags", false, function(result)
if result.code == 200 and result.body then
result.body = json.decode(result.body)
onFinish(result.body[1].name)
else
onFinish(nil)
end
end)
end
function _LoadFile(path)
local file, err = io.open(path, "r")
if not file then
_Log:printt("main", string.format("WARNING: Error during loading: \"%s\" (%s): expect errors!", path, err))
return
end
io.input(file)
local contents = io.read("*a")
io.close(file)
return contents
end
function _LoadJson(path)
local contents = _LoadFile(path)
assert(contents, string.format("Could not JSON-decode: %s, file does not exist", path))
local success, data = pcall(function() return json.decode(contents) end)
assert(success, string.format("JSON error: %s: %s", path, data))
assert(data, string.format("Could not JSON-decode: %s, error in file contents", path))
return data
end
-- This function allows to load images from external sources.
-- This is an altered code from https://love2d.org/forums/viewtopic.php?t=85350#p221460
function _LoadImageData(path)
local f = io.open(path, "rb")
if f then
local data = f:read("*all")
f:close()
if data then
data = love.filesystem.newFileData(data, "tempname")
data = love.image.newImageData(data)
return data
end
end
end
function _LoadImage(path)
local imageData = _LoadImageData(path)
assert(imageData, string.format("LOAD IMAGE FAIL: %s", path))
local image = love.graphics.newImage(imageData)
return image
end
-- This function allows to load sounds from external sources.
-- This is an altered code from the above function.
function _LoadSoundData(path)
local f = io.open(path, "rb")
if f then
local data = f:read("*all")
f:close()
if data then
-- to make everything work properly, we need to get the extension from the path, because it is used
-- source: https://love2d.org/wiki/love.filesystem.newFileData
local t = _StrSplit(path, ".")
local extension = t[#t]
data = love.filesystem.newFileData(data, "tempname." .. extension)
data = love.sound.newSoundData(data)
return data
end
end
end
function _LoadSound(path, type)
local soundData = _LoadSoundData(path)
assert(soundData, string.format("LOAD SOUND FAIL: %s", path))
local sound = love.audio.newSource(soundData, type)
return sound
end
function _LoadSounds(path, type, instanceCount)
local soundData = _LoadSoundData(path)
assert(soundData, string.format("LOAD SOUND FAIL: %s", path))
local sounds = {}
for i = 1, instanceCount do
table.insert(sounds, love.audio.newSource(soundData, type))
end
return sounds
end
-- This function allows to load fonts from external sources.
-- This is an altered code from the above function.
function _LoadFontData(path, size)
local f = io.open(path, "rb")
if f then
local data = f:read("*all")
f:close()
if data then
data = love.filesystem.newFileData(data, "tempname")
data = love.font.newRasterizer(data, size)
return data
end
end
end
function _LoadFont(path, size)
local fontData = _LoadFontData(path, size)
assert(fontData, string.format("LOAD FONT FAIL: %s", path))
local font = love.graphics.newFont(fontData)
return font
end
function _SaveFile(path, data)
local file = io.open(path, "w")
assert(file, string.format("SAVE FILE FAIL: %s", path))
io.output(file)
io.write(data)
io.close(file)
end
function _SaveJson(path, data)
_Log:printt("main", "Saving JSON data to " .. path .. "...")
_SaveFile(path, _JsonBeautify(json.encode(data)))
end
function _GetDirListing(path, filter, extFilter, recursive, pathRec)
-- Returns a list of directories and/or files in a given path.
-- filter can be "all", "dir" for directories only or "file" for files only.
filter = filter or "all"
pathRec = pathRec or ""
local result = {}
-- If it's compiled /fused/, this piece of code is needed to be able to read the external files
if love.filesystem.isFused() then
local success = love.filesystem.mount(love.filesystem.getSourceBaseDirectory(), _FSPrefix)
if not success then
local msg = string.format("Failed to read contents of folder: \"%s\". Report this error to a developer.", path)
error(msg)
end
end
-- Now we can access the directory regardless of whether it's fused or not.
local items = love.filesystem.getDirectoryItems(path .. "/" .. pathRec)
-- Each folder will get a / character on the end BUT ONLY IN "ALL" FILTER so it's easier to tell whether this is a file or a directory.
for i, item in ipairs(items) do
local p = path .. "/" .. pathRec .. item
if love.filesystem.getInfo(p).type == "directory" then
if filter == "all" then
table.insert(result, pathRec .. item .. "/")
elseif filter == "dir" then
table.insert(result, pathRec .. item)
end
if recursive then
for j, file in ipairs(_GetDirListing(path, filter, extFilter, true, pathRec .. item .. "/")) do
table.insert(result, file)
end
end
else
if filter == "all" or filter == "file" and (not extFilter or item:sub(item:len() - extFilter:len() + 1) == extFilter) then
table.insert(result, pathRec .. item)
end
end
end
-- Unmount it so we don't get into safety problems.
if pathRec == "" then
love.filesystem.unmount(love.filesystem.getSourceBaseDirectory())
end
return result
end
function _ParsePath(data)
if not data then
return nil
end
return _FSPrefix .. "games/" .. _Game.name .. "/" .. data
end
function _ParsePathDots(data)
if not data then
return nil
end
return _FSPrefix .. "games." .. _Game.name .. "." .. data
end
function _ParseNumber(data)
if not data then
return nil
end
if type(data) == "number" then
return data
end
if type(data) == "string" then
return tonumber(data)
end
if data.type == "randomSign" then
local value = _ParseNumber(data.value)
return math.random() < 0.5 and -value or value
end
if data.type == "randomInt" then
local min = _ParseNumber(data.min)
local max = _ParseNumber(data.max)
return math.random(min, max)
end
if data.type == "randomFloat" then
local min = _ParseNumber(data.min)
local max = _ParseNumber(data.max)
return min + math.random() * (max - min)
end
if data.type == "expr_graph" then
local value = _ParseNumber(data.value)
local points = {}
for i, point in ipairs(data.points) do
points[i] = _ParseVec2(point)
end
for i, point in ipairs(points) do
if value < point.x then
local prevPoint = points[i - 1]
if prevPoint and point.x - prevPoint.x > 0 then
local t = (point.x - value) / (point.x - prevPoint.x)
return prevPoint.y * t + point.y * (1 - t)
end
return point.y
end
end
return points[#points].y
end
end
function _ParseVec2(data)
if not data then
return nil
end
return Vec2(_ParseNumber(data.x), _ParseNumber(data.y))
end
function _ParseColor(data)
if not data then
return nil
end
return Color(_ParseNumber(data.r), _ParseNumber(data.g), _ParseNumber(data.b))
end
---Parses a number or an Expression which evaluates to a number, enclosed in a `"$expr{...}"` clause.
---@param data number|string A number or an Expression which evaluates to a number.
---@return number?
function _ParseExprNumber(data)
if type(data) == "number" then
return data
end
if type(data) == "string" then
return _Vars:evaluateExpression(data)
end
return Vec2(_ParseNumber(data.x), _ParseNumber(data.y))
end
---Parses a table of `{x=number, y=number}` format or an Expression which evaluates to a Vector2, enclosed in a `"$expr{...}"` clause.
---@param data table|string A table or an Expression which evaluates to a number.
---@return Vector2?
function _ParseExprVec2(data)
if type(data) == "table" then
return _ParseVec2(data)
end
if type(data) == "string" then
return _Vars:evaluateExpression(data)
end
end
function _NumStr(n)
local text = ""
local s = tostring(n)
local l = s:len()
for i = 1, l do
text = text .. s:sub(i, i)
if l - i > 0 and (l - i) % 3 == 0 then text = text .. "," end
end
return text
end
-- One-dimensional cubic Beazier curve.
-- More info: http://www.demofox.org/bezcubic1d.html
-- The given expression can be simplified, because we are defining A = 0 and D = 1.
-- The shortened expression: y = B * 3x(1-x)^2 + C * 3x^2(1-x) + x^3
-- x is t, B is p1 and C is p2.
function _BzLerp(t, p1, p2)
local b = p1 * (3 * t * math.pow(1 - t, 2))
local c = p2 * (3 * math.pow(t, 2) * (1 - t))
local d = math.pow(t, 3)
return b + c + d
end