-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.lua
595 lines (495 loc) · 18.5 KB
/
client.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
local isProne = false
local isCrouched = false
local isCrawling = false
local inAction = false
local proneType = 'onfront'
local lastKeyPress = 0
local walkstyle = nil
local forceEndProne = false
-- Utils --
---Checks if the player should be able to crawl or not
---@param playerPed number
---@return boolean
local function CanPlayerCrouchCrawl(playerPed)
if not IsPedOnFoot(playerPed) or IsPedJumping(playerPed) or IsPedFalling(playerPed) or IsPedInjured(playerPed) or IsPedInMeleeCombat(playerPed) or IsPedRagdoll(playerPed) then
return false
end
return true
end
---Load animation dictionary
---@param dict string
local function LoadAnimDict(dict)
RequestAnimDict(dict)
while not HasAnimDictLoaded(dict) do
Wait(0)
end
end
---Loads clipset/walkstyle
---@param clipset string
local function LoadClipSet(clipset)
RequestClipSet(clipset)
while not HasClipSetLoaded(clipset) do
Wait(0)
end
end
---Sets clipset/walkstyle
---@param clipset string
local function SetPlayerClipset(clipset)
LoadClipSet(clipset)
SetPedMovementClipset(PlayerPedId(), clipset, 0.5)
RemoveClipSet(clipset)
end
---Returns if the ped is aiming a weapon
---@param ped number
---@return boolean
local function IsPedAiming(ped)
return GetPedConfigFlag(ped, 78, true) == 1 and true or false
end
---Plays an animation on the ped. (Loads an unloads needed anim dict)
---@param ped number
---@param animDict string
---@param animName string
---@param blendInSpeed number|nil
---@param blendOutSpeed number|nil
---@param duration number|nil
---@param startTime number|nil
local function PlayAnimOnce(ped, animDict, animName, blendInSpeed, blendOutSpeed, duration, startTime)
LoadAnimDict(animDict)
TaskPlayAnim(ped, animDict, animName, blendInSpeed or 2.0, blendOutSpeed or 2.0, duration or -1, 0, startTime or 0.0, false, false, false)
RemoveAnimDict(animDict)
end
---Smoothly changes the ped's heading
---@param ped number
---@param amount number
---@param time number ms
local function ChangeHeadingSmooth(ped, amount, time)
local times = math.abs(amount)
local test = amount / times
local wait = time / times
for _i = 1, times do
Wait(wait)
SetEntityHeading(ped, GetEntityHeading(ped) + test)
end
end
-- Crouching --
---Resets the crouch effect (clipsets etc.)
local function ResetCrouch()
local playerPed = PlayerPedId()
ResetPedStrafeClipset(playerPed)
ResetPedWeaponMovementClipset(playerPed)
SetPedMaxMoveBlendRatio(playerPed, 1.0)
SetPedCanPlayAmbientAnims(playerPed, true)
-- Applies the previous walk style (or resets to default if non had been set)
if walkstyle ~= nil then
SetPlayerClipset(walkstyle)
else
ResetPedMovementClipset(playerPed, 0.5)
end
RemoveAnimSet('move_ped_crouched')
end
---Starts the crouch loop
local function CrouchLoop()
while isCrouched do
local playerPed = PlayerPedId()
-- Checks if the player is falling, in vehicle, dead etc.
if not CanPlayerCrouchCrawl(playerPed) then
isCrouched = false
break
end
-- Limit the speed that the player can walk when aiming
if IsPedAiming(playerPed) then
SetPedMaxMoveBlendRatio(playerPed, 0.15)
end
-- This blocks the ped from standing up and playing idle anims (this needs to be looped)
SetPedCanPlayAmbientAnims(playerPed, false)
-- Disables "INPUT_DUCK" and blocks action mode
DisableControlAction(0, 36, true)
if IsPedUsingActionMode(playerPed) == 1 then
SetPedUsingActionMode(playerPed, false, -1, 'DEFAULT_ACTION')
end
-- Disable first person
DisableFirstPersonCamThisFrame()
Wait(0)
end
TriggerEvent('crouch_crawl:onCrouch', false)
-- Reset walk style and ped variables
ResetCrouch()
end
---Starts crouching
local function StartCrouch()
isCrouched = true
LoadClipSet('move_ped_crouched')
local playerPed = PlayerPedId()
-- Force leave stealth mode
if GetPedStealthMovement(playerPed) == 1 then
SetPedStealthMovement(playerPed, false, 'DEFAULT_ACTION')
Wait(100)
end
-- Force leave first person view
if GetFollowPedCamViewMode() == 4 then
SetFollowPedCamViewMode(0) -- THIRD_PERSON_NEAR
end
walkstyle = GetPedWalkstyle(playerPed) or walkstyle
SetPedMovementClipset(playerPed, 'move_ped_crouched', 0.6)
SetPedStrafeClipset(playerPed, 'move_ped_crouched_strafing')
-- For other scripts to use
TriggerEvent('crouch_crawl:onCrouch', true)
CreateThread(CrouchLoop)
end
---@param playerPed number
---@return boolean success
local function AttemptCrouch(playerPed)
if CanPlayerCrouchCrawl(playerPed) and IsPedHuman(playerPed) then
StartCrouch()
return true
else
return false
end
end
---Disables a control until it's key has been released
---@param padIndex integer
---@param control integer
local function DisableControlUntilReleased(padIndex, control)
CreateThread(function()
while IsDisabledControlPressed(padIndex, control) do
DisableControlAction(padIndex, control, true)
Wait(0)
end
end)
end
---Called when the crouch key is pressed
local function CrouchKeyPressed()
-- If we already are doing something, then don't continue
if inAction then
return
end
-- Don't start/stop crouching if we are in the pause menu or the NUI is in focus
if IsPauseMenuActive() or IsNuiFocused() then
return
end
-- If crouched then stop crouching
if isCrouched then
isCrouched = false
local crouchKey = GetControlInstructionalButton(0, `+crouch` | 0x80000000, false)
local lookBehindKey = GetControlInstructionalButton(0, 26, false) -- INPUT_LOOK_BEHIND
-- Disable look behind if the crouch and look behind keys are the same
if crouchKey == lookBehindKey then
DisableControlUntilReleased(0, 26) -- INPUT_LOOK_BEHIND
end
return
end
-- Get the player ped
local playerPed = PlayerPedId()
-- Check if we can actually crouch and check if the player ped is humanoid
if not CanPlayerCrouchCrawl(playerPed) or not IsPedHuman(playerPed) then
return
end
-- Get +crouch, INPUT_LOOK_BEHIND and INPUT_DUCK controls
local crouchKey = GetControlInstructionalButton(0, `+crouch` | 0x80000000, false)
local lookBehindKey = GetControlInstructionalButton(0, 26, false) -- INPUT_LOOK_BEHIND
local duckKey = GetControlInstructionalButton(0, 36, false) -- INPUT_DUCK
-- Disable look behind if the crouch and look behind keys are the same
if crouchKey == lookBehindKey then
DisableControlUntilReleased(0, 26) -- INPUT_LOOK_BEHIND
end
-- If the crouch and duck key are the same
if crouchKey == duckKey then
if Config.CrouchOverrideStealthMode then
DisableControlAction(0, 36, true) -- Disable INPUT_DUCK this frame
elseif not isProne then
local timer = GetGameTimer()
-- If we are in stealth mode and we have already pressed the button in the last second
if GetPedStealthMovement(playerPed) == 1 and timer - lastKeyPress < 1000 then
DisableControlAction(0, 36, true) -- Disable INPUT_DUCK this frame
lastKeyPress = 0
else
lastKeyPress = timer
return
end
end
end
-- Start to crouch
StartCrouch()
-- If we are prone play an animation from prone to crouch
if isProne then
inAction = true
isProne = false
PlayAnimOnce(playerPed, 'get_up@directional@transition@prone_to_knees@crawl', 'front', nil, nil, 780)
Wait(780)
inAction = false
end
end
-- Crawling --
---@param playerPed number
---@return boolean
local function ShouldPlayerDiveToCrawl(playerPed)
if IsPedRunning(playerPed) or IsPedSprinting(playerPed) then
return true
end
return false
end
---Stops the player from being prone
---@param force boolean If forced then no exit anim is played
local function stopPlayerProne(force)
isProne = false
forceEndProne = force
end
---@param playerPed number
---@param heading number|nil
---@param blendInSpeed number|nil
local function PlayIdleCrawlAnim(playerPed, heading, blendInSpeed)
local playerCoords = GetEntityCoords(playerPed)
TaskPlayAnimAdvanced(playerPed, 'move_crawl', proneType..'_fwd', playerCoords.x, playerCoords.y, playerCoords.z, 0.0, 0.0, heading or GetEntityHeading(playerPed), blendInSpeed or 2.0, 2.0, -1, 2, 1.0, false, false)
end
---@param forceEnd boolean
local function PlayExitCrawlAnims(forceEnd)
if not forceEnd then
inAction = true
local playerPed = PlayerPedId()
if proneType == 'onfront' then
PlayAnimOnce(playerPed, 'get_up@directional@transition@prone_to_knees@crawl', 'front', nil, nil, 780)
-- Only stand fully up if we are not crouching
if not isCrouched then
Wait(780)
PlayAnimOnce(playerPed, 'get_up@directional@movement@from_knees@standard', 'getup_l_0', nil, nil, 1300)
end
else
PlayAnimOnce(playerPed, 'get_up@directional@transition@prone_to_seated@crawl', 'back', 16.0, nil, 950)
-- Only stand fully up if we are not crouching
if not isCrouched then
Wait(950)
PlayAnimOnce(playerPed, 'get_up@directional@movement@from_seated@standard', 'get_up_l_0', nil, nil, 1300)
end
end
end
end
---Crawls one "step" forward/backward
---@param playerPed number
---@param type string
---@param direction string
local function Crawl(playerPed, type, direction)
isCrawling = true
TaskPlayAnim(playerPed, 'move_crawl', type..'_'..direction, 8.0, -8.0, -1, 2, 0.0, false, false, false)
local time = {
['onfront'] = {
['fwd'] = 820,
['bwd'] = 990
},
['onback'] = {
['fwd'] = 1200,
['bwd'] = 1200
}
}
SetTimeout(time[type][direction], function()
isCrawling = false
end)
end
---Flips the player when crawling
---@param playerPed number
local function CrawlFlip(playerPed)
inAction = true
local heading = GetEntityHeading(playerPed)
if proneType == 'onfront' then
proneType = 'onback'
PlayAnimOnce(playerPed, 'get_up@directional_sweep@combat@pistol@front', 'front_to_prone', 2.0)
ChangeHeadingSmooth(playerPed, -18.0, 3600)
else
proneType = 'onfront'
PlayAnimOnce(playerPed, 'move_crawlprone2crawlfront', 'back', 2.0, nil, -1)
ChangeHeadingSmooth(playerPed, 12.0, 1700)
end
PlayIdleCrawlAnim(playerPed, heading + 180.0)
Wait(400)
inAction = false
end
---The crawl loop
local function CrawlLoop()
Wait(400)
while isProne do
local playerPed = PlayerPedId()
-- Checks if the player is falling, in vehicle, dead etc.
if not CanPlayerCrouchCrawl(playerPed) or IsEntityInWater(playerPed) then
ClearPedTasks(playerPed)
stopPlayerProne(true)
break
end
-- Handles forwad/backward movement
local forward, backwards = IsControlPressed(0, 32), IsControlPressed(0, 33) -- INPUT_MOVE_UP_ONLY, INPUT_MOVE_DOWN_ONLY
if not isCrawling then
if forward then -- Forward
Crawl(playerPed, proneType, 'fwd')
elseif backwards then -- Back
Crawl(playerPed, proneType, 'bwd')
end
end
-- Moving left/right
if IsControlPressed(0, 34) then -- INPUT_MOVE_LEFT_ONLY
if isCrawling then
local headingDiff = forward and 1.0 or -1.0
SetEntityHeading(playerPed, GetEntityHeading(playerPed) + headingDiff)
else
inAction = true
if proneType == 'onfront' then
local playerCoords = GetEntityCoords(playerPed)
TaskPlayAnimAdvanced(playerPed, 'move_crawlprone2crawlfront', 'left', playerCoords.x, playerCoords.y, playerCoords.z, 0.0, 0.0, GetEntityHeading(playerPed), 2.0, 2.0, -1, 2, 0.1, false, false)
ChangeHeadingSmooth(playerPed, -10.0, 300)
Wait(700)
else
PlayAnimOnce(playerPed, 'get_up@directional_sweep@combat@pistol@left', 'left_to_prone')
ChangeHeadingSmooth(playerPed, 25.0, 400)
PlayIdleCrawlAnim(playerPed)
Wait(600)
end
inAction = false
end
elseif IsControlPressed(0, 35) then -- INPUT_MOVE_RIGHT_ONLY
if isCrawling then
local headingDiff = backwards and 1.0 or -1.0
SetEntityHeading(playerPed, GetEntityHeading(playerPed) + headingDiff)
else
inAction = true
if proneType == 'onfront' then
local playerCoords = GetEntityCoords(playerPed)
TaskPlayAnimAdvanced(playerPed, 'move_crawlprone2crawlfront', 'right', playerCoords.x, playerCoords.y, playerCoords.z, 0.0, 0.0, GetEntityHeading(playerPed), 2.0, 2.0, -1, 2, 0.1, false, false)
ChangeHeadingSmooth(playerPed, 10.0, 300)
Wait(700)
else
PlayAnimOnce(playerPed, 'get_up@directional_sweep@combat@pistol@right', 'right_to_prone')
ChangeHeadingSmooth(playerPed, -25.0, 400)
PlayIdleCrawlAnim(playerPed)
Wait(600)
end
inAction = false
end
end
-- Flipping around
if not isCrawling then
if IsControlPressed(0, 22) then -- INPUT_JUMP
CrawlFlip(playerPed)
end
end
Wait(0)
end
TriggerEvent('crouch_crawl:onCrawl', false)
-- If the crawling wasn't forcefully ended, then play the get up animations
PlayExitCrawlAnims(forceEndProne)
-- Reset variabels
isCrawling = false
inAction = false
forceEndProne = false
proneType = 'onfront'
SetPedConfigFlag(PlayerPedId(), 48, false) -- CPED_CONFIG_FLAG_BlockWeaponSwitching
-- Unload animation dictionaries
RemoveAnimDict('move_crawl')
RemoveAnimDict('move_crawlprone2crawlfront')
end
---Gets called when the crawl key is pressed
local function CrawlKeyPressed()
-- If we already are doing something, then don't continue
if inAction then
return
end
-- Don't start/stop to crawl if we are in the pause menu or the NUI is in focus
if IsPauseMenuActive() or IsNuiFocused() then
return
end
-- If already prone, then stop
if isProne then
isProne = false
return
end
-- If we are crouching we should stop that first
local wasCrouched = false
if isCrouched then
isCrouched = false
wasCrouched = true
end
local playerPed = PlayerPedId()
if not CanPlayerCrouchCrawl(playerPed) or IsEntityInWater(playerPed) or not IsPedHuman(playerPed) then
return
end
inAction = true
-- If we are pointing then stop pointing
if Pointing then
Pointing = false
end
isProne = true
SetPedConfigFlag(playerPed, 48, true) -- CPED_CONFIG_FLAG_BlockWeaponSwitching
-- Force leave stealth mode
if GetPedStealthMovement(playerPed) == 1 then
SetPedStealthMovement(playerPed, false, 'DEFAULT_ACTION')
Wait(100)
end
-- Load animations that the crawling is going to use
LoadAnimDict('move_crawl')
LoadAnimDict('move_crawlprone2crawlfront')
if ShouldPlayerDiveToCrawl(playerPed) then
PlayAnimOnce(playerPed, 'explosions', 'react_blown_forwards', nil, 3.0)
Wait(1100)
elseif wasCrouched then
PlayAnimOnce(playerPed, 'amb@world_human_sunbathe@male@front@enter', 'enter', nil, nil, -1, 0.3)
Wait(1500)
else
PlayAnimOnce(playerPed, 'amb@world_human_sunbathe@male@front@enter', 'enter')
Wait(3000)
end
-- Set the player into the idle position (but only if we can still crawl)
if CanPlayerCrouchCrawl(playerPed) and not IsEntityInWater(playerPed) then
PlayIdleCrawlAnim(playerPed, nil, 3.0)
end
TriggerEvent('crouch_crawl:onCrawl', true)
inAction = false
CreateThread(CrawlLoop)
end
-- Commands & KeyMapping --
if Config.CrouchEnabled then
if Config.CrouchKeybindEnabled then
RegisterKeyMapping('+crouch', Config.Localization['crouch_keymapping'], 'keyboard', Config.CrouchKeybind)
RegisterCommand('+crouch', function() CrouchKeyPressed() end, false)
RegisterCommand('-crouch', function() end, false) -- This needs to be here to prevent warnings in chat
end
RegisterCommand('crouch', function()
if isCrouched then
isCrouched = false
return
end
AttemptCrouch(PlayerPedId())
end, false)
TriggerEvent('chat:addSuggestion', '/crouch', Config.Localization['crouch_chat_suggestion'])
end
if Config.CrawlEnabled then
if Config.CrawlKeybindEnabled then
RegisterKeyMapping('+crawl', Config.Localization['crawl_keymapping'], 'keyboard', Config.CrawlKeybind)
RegisterCommand('+crawl', function() CrawlKeyPressed() end, false)
RegisterCommand('-crawl', function() end, false) -- This needs to be here to prevent warnings in chat
end
RegisterCommand('crawl', function() CrawlKeyPressed() end, false)
TriggerEvent('chat:addSuggestion', '/crawl', Config.Localization['crawl_chat_suggestion'])
end
-- Exports --
---Returns if the player is crouched
---@return boolean
local function IsPlayerCrouched()
return isCrouched
end
exports('IsPlayerCrouched', IsPlayerCrouched)
---Returns if the player is prone (both when laying still and when moving)
---@return boolean
local function IsPlayerProne()
return isProne
end
exports('IsPlayerProne', IsPlayerProne)
---Returns if the player is crawling (only when moving forward/backward)
---@return boolean
local function IsPlayerCrawling()
return isCrawling
end
exports('IsPlayerCrawling', IsPlayerCrawling)
---Returns either "onfront" or "onback", this can be used to check if the player is on his back or on his stomach. NOTE: This will still return a string even if the player is not prone. Use IsPlayerProne() to check if the player is prone.
---@return string
local function GetPlayerProneType()
return proneType
end
exports('GetPlayerProneType', GetPlayerProneType)
-- Usefull to call if hte player gets handcuffed etc.
exports('StopPlayerProne', stopPlayerProne)