-
Notifications
You must be signed in to change notification settings - Fork 3
/
circuit.lua
575 lines (478 loc) · 20.1 KB
/
circuit.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
--#region Static Variables
HasCircuitFailed = false
local hasCircuitCompleted = false
local gameBounds = {
vec2(0.159, 0.153), -- Top Left
vec2(0.159, 0.848), -- Bottom Left
vec2(0.841, 0.848), -- Bottom Right
vec2(0.841, 0.153) -- Top Right
}
local textureDictionaries = {'MPCircuitHack', 'MPCircuitHack2', 'MPCircuitHack3'}
local gameEndTime
local gameStartTime
local delayedStartTime
local initCursorSpeed
local _cursorSpeed
local illegalAreas
local genericPorts
local _cursor
local _scaleform
local _currentDifficulty
local _currentLevelNumber
local _isEndScreenActive
local hackingKitVersionNumber
local isHackingKitDisconnected
local isDisconnectedScreenActive
local lastDisconnectCheckTime
local reconnectingTime
local backgroundSoundId
local trailSoundId
local startingHealth
local debugPortHeading = 0
--#endregion Static Variables
--#region Changeable Variables
local defaultDelayStartTimeMs <const> = 1000
local minDelayEndGameTimeMs <const> = 5000
local maxDelayEndGameTimeMs <const> = 5000
local defaultMinReconnectTimeMs <const> = 3000
local defaultMaxReconnectTimeMs <const> = 30000
local maxDisconnectChance <const> = 0.9
local minDisconnectCheckRateMs <const> = 500
local minCursorSpeed <const> = 0.00085
local maxCursorSpeed <const> = 0.01
local debuggingMapPosition = false
---Checks if you have the hacking kit item, the item name must have a number in it, ranging from 1 to 3
---@return boolean
local function doesPlayerHaveHackingKit()
return true
end
---Returns the version number of the hacking kit item, number ranging from 1 to 3
---@return integer
local function getHackingKitVersionNumber()
--local versionNumber = string.match(itemName, '%d+')
return math.random(1, 3)
end
--#endregion Changeable Variables
---Gets the cursor maximum points.
---@param cursorCoords vector2
---@param cursorHeadSize number
---@return vector2[]
local function getCursorMaxPoints(cursorCoords, cursorHeadSize)
local headPoint1 = vec2(cursorCoords.x - cursorHeadSize / 2, cursorCoords.y)
local headPoint2 = vec2(cursorCoords.x - cursorHeadSize / 2, cursorCoords.y)
local headPoint3 = vec2(cursorCoords.x, cursorCoords.y - cursorHeadSize / 2)
local headPoint4 = vec2(cursorCoords.x, cursorCoords.y + cursorHeadSize / 2)
return {headPoint1, headPoint2, headPoint3, headPoint4, cursorCoords}
end
---Determines whether the cursor is out of the specified poly bounds
---@param polyBounds vector2[][]
---@param mapBounds vector2[]
---@return boolean
local function isCursorOutOfBounds(polyBounds, mapBounds)
local headPts = getCursorMaxPoints(_cursor.position, _cursor.cursorHeadSize + -0.375 * _cursor.cursorHeadSize)
for i = 1, #headPts do
for i2 = 1, #polyBounds do
if IsInPoly(polyBounds[i2], headPts[i]) then
return true
end
end
if not IsInPoly(mapBounds, headPts[i]) then
return true
end
end
return false
end
local function disposeScaleform()
if not _scaleform then return end
SetScaleformMovieAsNoLongerNeeded(_scaleform)
_scaleform = nil
Wait(50)
end
local function disposeTextureDictionaries()
for i = 1, #textureDictionaries do
SetStreamedTextureDictAsNoLongerNeeded(textureDictionaries[i])
end
end
local function disposeSounds()
StopSound(trailSoundId)
StopSound(backgroundSoundId)
if backgroundSoundId > 0 then
ReleaseSoundId(backgroundSoundId)
end
if trailSoundId > 0 then
ReleaseSoundId(trailSoundId)
end
end
local function dispose()
disposeSounds()
disposeTextureDictionaries()
disposeScaleform()
end
local function endGame()
dispose()
end
---Shows the display scaleform.
---@param title string
---@param msg string
---@param r integer
---@param g integer
---@param b integer
---@param stagePassed boolean
local function showDisplayScaleform(title, msg, r, g, b, stagePassed)
if not _scaleform then return end
BeginScaleformMovieMethod(_scaleform, 'SET_DISPLAY')
ScaleformMovieMethodAddParamInt(0)
ScaleformMovieMethodAddParamTextureNameString(title)
ScaleformMovieMethodAddParamTextureNameString(msg)
ScaleformMovieMethodAddParamInt(r)
ScaleformMovieMethodAddParamInt(g)
ScaleformMovieMethodAddParamInt(b)
ScaleformMovieMethodAddParamBool(stagePassed)
EndScaleformMovieMethod()
end
local function setScaleform()
disposeScaleform()
_scaleform = RequestScaleformMovie('HACKING_MESSAGE')
local loadAttempt = 0
while not HasScaleformMovieLoaded(_scaleform) do
Wait(5)
loadAttempt += 1
if loadAttempt > 50 then
break
end
end
end
local function initializeResources()
for i = 1, #textureDictionaries do
RequestStreamedTextureDict(textureDictionaries[i], false)
end
RequestScriptAudioBank('DLC_MPHEIST/HEIST_HACK_SNAKE', false)
local timeout = GetGameTimer() + 5000
while GetGameTimer() - timeout < 0 do
local allLoaded = true
for i = 1, #textureDictionaries do
if not HasStreamedTextureDictLoaded(textureDictionaries[i]) then
allLoaded = false
end
end
if allLoaded then
break
end
Wait(100)
end
for i = 1, #textureDictionaries do
if not HasStreamedTextureDictLoaded(textureDictionaries[i]) then
error(('Failed to load texture dictionary %s'):format(textureDictionaries[i]))
break
end
end
setScaleform()
backgroundSoundId = GetSoundId()
trailSoundId = GetSoundId()
end
---@param delayMs integer
local function playStartSound(delayMs)
CreateThread(function()
Wait(delayMs)
PlaySoundFrontend(-1, 'Start', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
PlaySoundFrontend(trailSoundId, 'Trail_Custom', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
end)
end
local function drawCursorAndPortSprites()
_cursor:DrawCursor()
_cursor:DrawTailHistory()
genericPorts:DrawPorts()
end
---@param currentMap integer
local function drawMapSprite(currentMap)
local levelTextureDict = currentMap > 3 and 'MPCircuitHack3' or 'MPCircuitHack2'
DrawSprite(levelTextureDict, ('CBLevel%s'):format(_currentLevelNumber), 0.5, 0.5, 1, 1, 0, 255, 255, 255, 255)
end
---@param currentDifficulty Difficulty
---@return integer
local function getDisconnectCheckRateMsFromDifficulty(currentDifficulty)
if currentDifficulty == Difficulty.Beginner then
return 10000
elseif currentDifficulty == Difficulty.Easy then
return 2000
elseif currentDifficulty == Difficulty.Medium then
return 1000
elseif currentDifficulty == Difficulty.Hard then
return 500
end
return 10000
end
---@param currentDifficulty Difficulty
---@return integer
local function getDisconnectChanceFromDifficulty(currentDifficulty)
if currentDifficulty == Difficulty.Beginner then
return 0
elseif currentDifficulty == Difficulty.Easy then
return 0.15
elseif currentDifficulty == Difficulty.Medium then
return 0.30
elseif currentDifficulty == Difficulty.Hard then
return 0.45
end
return 0
end
---@param currentDifficulty Difficulty
---@return number
local function getCursorSpeedFromDifficulty(currentDifficulty)
if currentDifficulty == Difficulty.Beginner then
return 0.00085
elseif currentDifficulty == Difficulty.Easy then
return 0.002
elseif currentDifficulty == Difficulty.Medium then
return 0.004
elseif currentDifficulty == Difficulty.Hard then
return 0.006
end
return 0.00085
end
---@param currentDifficulty Difficulty
---@return number
local function getMaxSpeedIncreaseOnReconnect(currentDifficulty)
if currentDifficulty == Difficulty.Beginner then
return 0.002
elseif currentDifficulty == Difficulty.Easy then
return 0.004
elseif currentDifficulty == Difficulty.Medium then
return 0.006
elseif currentDifficulty == Difficulty.Hard then
return 0.01
end
return 0.002
end
---@param currentDifficulty Difficulty
---@return number
local function getCursorSpeedOnReconnect(currentDifficulty)
if hackingKitVersionNumber == 3 then return _cursorSpeed end
local maxSpeed = getMaxSpeedIncreaseOnReconnect(currentDifficulty) * 100000
local speedDelta = math.random(0, math.round(maxSpeed * 0.25, -1)) / 100000
if math.random() > 0.75 then
speedDelta *= -1
end
return math.clamp(_cursorSpeed + speedDelta, initCursorSpeed, maxSpeed)
end
local function finishReconnection()
PlaySoundFrontend(-1, 'Start', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
setScaleform()
isHackingKitDisconnected = false
lastDisconnectCheckTime = GetGameTimer()
_cursorSpeed = getCursorSpeedOnReconnect(_currentDifficulty)
end
---@return number
local function applyHackingKitBonusToCursorSpeed()
return hackingKitVersionNumber == 3 and math.random(0, 2000) / 100000 or 0
end
---@return number
local function applyHackingKitBonusToReconnectTime()
return hackingKitVersionNumber == 2 and -2000 or 0
end
---@return number
local function applyHackingKitBonusToDisconnectChance()
return hackingKitVersionNumber == 2 and -0.15 or hackingKitVersionNumber == 3 and -0.2 or 0
end
---@return number
local function applyHackingKitBonusToDisconnectCheckRate()
return hackingKitVersionNumber == 3 and 3000 or 0
end
---@param minReconnectTimeMs integer
---@param maxReconnectTimeMs integer
local function startReconnection(minReconnectTimeMs, maxReconnectTimeMs)
PlaySoundFrontend(-1, 'Power_Down', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
showDisplayScaleform('CONNECTION LOST', 'Reconnecting...', 188, 49, 43, false)
local reconnectTime = math.random(minReconnectTimeMs, maxReconnectTimeMs) + applyHackingKitBonusToReconnectTime()
reconnectingTime = GetGameTimer() + math.clamp(reconnectTime, 0, reconnectTime)
end
---@param disconnectChance number
---@param disconnectCheckRateMs number
local function checkIfHackingDisconnected(disconnectChance, disconnectCheckRateMs)
if GetGameTimer() - (lastDisconnectCheckTime + disconnectCheckRateMs + applyHackingKitBonusToDisconnectCheckRate()) < 0 then return end
disconnectChance += applyHackingKitBonusToDisconnectChance()
isHackingKitDisconnected = math.random() < disconnectChance
lastDisconnectCheckTime = GetGameTimer()
end
local function showFailureScreenAndPlaySound()
PlaySoundFrontend(-1, 'Crash', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
StopSound(trailSoundId)
showDisplayScaleform('CIRCUIT FAILED', 'Security Tunnel Detected', 188, 49, 43, false)
end
local function showSuccessScreenAndPlaySound()
PlaySoundFrontend(-1, 'Goal', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
StopSound(trailSoundId)
showDisplayScaleform('CIRCUIT COMPLETE', 'Decryption Execution x86 Tunneling', 45, 203, 134, true)
end
---@param cursorSpeed number
local function initializeCursorSpeed(cursorSpeed)
cursorSpeed = math.clamp(cursorSpeed - applyHackingKitBonusToCursorSpeed(), 0.001, cursorSpeed)
initCursorSpeed = cursorSpeed
_cursorSpeed = cursorSpeed
end
---@param levelNumber integer
---@param difficultyLevel Difficulty
---@param cursorSpeed number
---@param delayStartMs integer
local function initializeLevelVariables(levelNumber, difficultyLevel, cursorSpeed, delayStartMs)
HasCircuitFailed = false
hasCircuitCompleted = false
_isEndScreenActive = false
isHackingKitDisconnected = false
hackingKitVersionNumber = getHackingKitVersionNumber()
_currentLevelNumber = levelNumber
illegalAreas = GetBoxBounds(levelNumber)
genericPorts = newGeneric(levelNumber)
_cursor = newCursor(genericPorts)
initializeCursorSpeed(cursorSpeed)
_currentDifficulty = difficultyLevel
gameStartTime = GetGameTimer()
delayedStartTime = gameStartTime + delayStartMs
lastDisconnectCheckTime = gameStartTime + delayStartMs
startingHealth = GetEntityHealth(PlayerPedId())
end
---@return boolean
local function isPlayerTakingDamage()
local health = GetEntityHealth(PlayerPedId())
if health < startingHealth then
return true
end
if health > startingHealth then
startingHealth = health
end
return false
end
---@param levelNumber integer
---@param difficultyLevel Difficulty
---@param cursorSpeed number
---@param delayStartMs integer
---@param minFailureDelayTimeMs integer
---@param maxFailureDelayTimeMs integer
---@param disconnectChance number
---@param disconnectCheckRateMs integer
---@param minReconnectTimeMs integer
---@param maxReconnectTimeMs integer
---@return GameStatus
local function runMinigameTask(levelNumber, difficultyLevel, cursorSpeed, delayStartMs, minFailureDelayTimeMs, maxFailureDelayTimeMs, disconnectChance, disconnectCheckRateMs, minReconnectTimeMs, maxReconnectTimeMs)
if not NetworkIsSessionStarted() then
Wait(1000)
endGame()
return GameStatus.FailedToStart
end
if not doesPlayerHaveHackingKit() then return GameStatus.MissingHackKit end
initializeResources()
PlaySoundFrontend(backgroundSoundId, 'Background', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
playStartSound(delayStartMs)
initializeLevelVariables(levelNumber, difficultyLevel, cursorSpeed, delayStartMs)
local debugCursorSpeed = 0.0015
while true do
DisableAllControlActions(0)
if isPlayerTakingDamage() then return GameStatus.TakingDamage end
if IsDisabledControlPressed(0, 44) and not HasCircuitFailed then -- INPUT_COVER
endGame()
return GameStatus.PlayerQuit
end
drawMapSprite(_currentLevelNumber)
if debuggingMapPosition then
local newDirection = GetDebugCursorDirectionInput()
debugCursorSpeed += GetDebugCursorSpeedInput()
if newDirection >= 0 then
_cursor:DebugCursorPosition(newDirection, debugCursorSpeed)
end
debugPortHeading = GetPortDebugHeading(debugPortHeading)
DebugPortSprite(_cursor.position, debugPortHeading)
if IsDisabledControlJustPressed(0, 38) then -- INPUT_PICKUP
print(('vec2(%s, %s),'):format(_cursor.position.x, _cursor.position.y))
end
goto skipRest
end
drawCursorAndPortSprites()
DrawScaleformMovieFullscreen(_scaleform, 255, 255, 255, 255, 0)
if not _isEndScreenActive and genericPorts:IsCursorInGameWinningPosition(_cursor.position) then
hasCircuitCompleted = true
gameEndTime = GetGameTimer() + minDelayEndGameTimeMs
showSuccessScreenAndPlaySound()
_isEndScreenActive = true
elseif not _isEndScreenActive and isCursorOutOfBounds(illegalAreas, gameBounds) or genericPorts:IsCollisionWithPort(_cursor.position) or _cursor:CheckTailCollision() then
HasCircuitFailed = true
if _cursor.isAlive then
_cursor.isAlive = false
_cursor:StartCursorDeathAnimation()
end
if not _isEndScreenActive and not _cursor.isVisible then
showFailureScreenAndPlaySound()
gameEndTime = GetGameTimer() + math.random(minFailureDelayTimeMs, maxFailureDelayTimeMs)
_isEndScreenActive = true
end
elseif not _isEndScreenActive and isHackingKitDisconnected then
if not isDisconnectedScreenActive then
startReconnection(minReconnectTimeMs, maxReconnectTimeMs)
isDisconnectedScreenActive = true
end
if isDisconnectedScreenActive and GetGameTimer() - reconnectingTime >= 0 then
finishReconnection()
isDisconnectedScreenActive = false
end
end
if not isHackingKitDisconnected and disconnectChance > 0 then
checkIfHackingDisconnected(disconnectChance, disconnectCheckRateMs)
end
if GetGameTimer() - delayedStartTime >= 0 and not _isEndScreenActive and _cursor.isAlive and not isHackingKitDisconnected then
_cursor:GetCursorInputFromPlayer()
_cursor:MoveCursor(_cursorSpeed)
end
if _isEndScreenActive and (HasCircuitFailed or hasCircuitCompleted) then
if GetGameTimer() - gameEndTime >= 0 then
StopSound(backgroundSoundId)
if hasCircuitCompleted then
PlaySoundFrontend(-1, 'Success', 'DLC_HEIST_HACKING_SNAKE_SOUNDS', true)
end
endGame()
return hasCircuitCompleted and GameStatus.Success or GameStatus.Failure
end
end
:: skipRest ::
Wait(0)
end
end
---@param levelNumber integer 1 - 6
---@param difficultyLevel Difficulty 0 - 3, take a look at globals.lua
---@param cursorSpeed number 0.0085 - 0.01, the limits of this can be redefined at the top of circuit.lua
---@param delayStartMs integer How long to delay the start in milliseconds
---@param minFailureDelayTimeMs integer How long to delay the failure screen at minimal in milliseconds
---@param maxFailureDelayTimeMs integer How long to delay the failure screen at the max in milliseconds
---@param disconnectChance number A decimal number between 0 and 1, the chance that it disconnects
---@param disconnectCheckRateMs integer The amount of time in milliseconds to check if it should disconnect using the disconnectChance
---@param minReconnectTimeMs integer The time in milliseconds it takes at minimal to reconnect after disconnecting by chance
---@param maxReconnectTimeMs integer The time in milliseconds it takes at the max to reconnect after disconnecting by chance
---@return GameStatus
local function runMiniGame(levelNumber, difficultyLevel, cursorSpeed, delayStartMs, minFailureDelayTimeMs, maxFailureDelayTimeMs, disconnectChance, disconnectCheckRateMs, minReconnectTimeMs, maxReconnectTimeMs)
levelNumber = math.clamp(levelNumber, 1, 6)
difficultyLevel = math.clamp(difficultyLevel, 0, 3)
cursorSpeed = math.clamp(cursorSpeed, minCursorSpeed, maxCursorSpeed)
delayStartMs = math.clamp(delayStartMs, 1000, 60000)
minFailureDelayTimeMs = math.clamp(minFailureDelayTimeMs, minDelayEndGameTimeMs, maxFailureDelayTimeMs)
maxFailureDelayTimeMs = math.clamp(maxFailureDelayTimeMs, minDelayEndGameTimeMs, maxFailureDelayTimeMs > minFailureDelayTimeMs and maxFailureDelayTimeMs or minFailureDelayTimeMs + 1)
disconnectChance = math.clamp(disconnectChance, 0, maxDisconnectChance)
disconnectCheckRateMs = math.clamp(disconnectCheckRateMs, minDisconnectCheckRateMs, disconnectCheckRateMs)
minReconnectTimeMs = math.clamp(minReconnectTimeMs, defaultMinReconnectTimeMs, maxReconnectTimeMs)
maxReconnectTimeMs = math.clamp(maxReconnectTimeMs, minReconnectTimeMs + 1, defaultMaxReconnectTimeMs)
return runMinigameTask(levelNumber, difficultyLevel, cursorSpeed, delayStartMs, minFailureDelayTimeMs, maxFailureDelayTimeMs, disconnectChance, disconnectCheckRateMs, minReconnectTimeMs, maxReconnectTimeMs)
end
exports('run', runMiniGame)
---@param levelNumber integer 1 - 6
---@param difficultyLevel Difficulty 0 - 3, take a look at globals.lua
---@return GameStatus
local function runDefaultMiniGameFromDifficulty(levelNumber, difficultyLevel)
levelNumber = math.clamp(levelNumber, 1, 6)
difficultyLevel = math.clamp(difficultyLevel, 0, 3)
return runMiniGame(levelNumber, difficultyLevel, getCursorSpeedFromDifficulty(difficultyLevel), defaultDelayStartTimeMs, minDelayEndGameTimeMs, maxDelayEndGameTimeMs, getDisconnectChanceFromDifficulty(difficultyLevel), getDisconnectCheckRateMsFromDifficulty(difficultyLevel), defaultMinReconnectTimeMs, defaultMaxReconnectTimeMs)
end
exports('runDefaultWithDifficulty', runDefaultMiniGameFromDifficulty)
---@return GameStatus
local function runDefaultMiniGame()
local levelNumber = math.random(1, 6)
local difficultyLevel = math.random(0, 3)
return runMiniGame(levelNumber, difficultyLevel, getCursorSpeedFromDifficulty(difficultyLevel), defaultDelayStartTimeMs, minDelayEndGameTimeMs, maxDelayEndGameTimeMs, getDisconnectChanceFromDifficulty(difficultyLevel), getDisconnectCheckRateMsFromDifficulty(difficultyLevel), defaultMinReconnectTimeMs, defaultMaxReconnectTimeMs)
end
exports('runDefaultRandom', runDefaultMiniGame)