forked from DaveYo1/k9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.lua
More file actions
544 lines (477 loc) · 17.8 KB
/
client.lua
File metadata and controls
544 lines (477 loc) · 17.8 KB
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
--[[ Variables ]]--
-- DO NOT CHANGE --
local just_started = true
local k9_name = "Default"
local spawned_ped = nil
local following = false
local attacking = false
local attacked_player = 0
local searching = false
local playing_animation = false
local animations = {
['Normal'] = {
sit = {
dict = "creatures@rottweiler@amb@world_dog_sitting@idle_a",
anim = "idle_b"
},
laydown = {
dict = "creatures@rottweiler@amb@sleep_in_kennel@",
anim = "sleep_in_kennel"
},
searchhit = {
dict = "creatures@rottweiler@indication@",
anim = "indicate_high"
}
}
}
--]]
--[[ Tables ]]--
local language = {}
--]]
--[[ NUI Messages ]]--
-- Open Menu --
function EnableMenu()
SetNuiFocus(true, true)
SendNUIMessage({
type = "open_k9_menu"
})
end
--]]
--[[ NUI Callbacks ]]--
RegisterNUICallback("closemenu", function(data)
SetNuiFocus(false, false)
end)
RegisterNUICallback("updatename", function(data)
k9_name = data.name
end)
RegisterNUICallback("spawnk9", function(data)
TriggerEvent("K9:ToggleK9", data.model)
end)
RegisterNUICallback("vehicletoggle", function(data)
if spawned_ped ~= nil then
TriggerServerEvent("K9:RequestVehicleToggle")
end
end)
RegisterNUICallback("vehiclesearch", function(data)
if spawned_ped ~= nil then
TriggerServerEvent("K9:RequestItems")
end
end)
RegisterNUICallback("sit", function(data)
if spawned_ped ~= nil then
PlayAnimation(animations['Normal'].sit.dict, animations['Normal'].sit.anim)
end
end)
RegisterNUICallback("laydown", function(data)
if spawned_ped ~= nil then
PlayAnimation(animations['Normal'].laydown.dict, animations['Normal'].laydown.anim)
end
end)
--]]
--[[ Main Event Handlers ]]--
-- Updates Language Settings
RegisterNetEvent("K9:UpdateLanguage")
AddEventHandler("K9:UpdateLanguage", function(commands)
language = commands
Citizen.Trace(tostring(json.encode(language)))
end)
-- Opens K9 Menu
RegisterNetEvent("K9:OpenMenu")
AddEventHandler("K9:OpenMenu", function(pedRestriction, pedList)
if pedRestriction then
if CheckPedRestriction(GetLocalPed(), pedList) then
EnableMenu()
else
Notification(tostring("~r~You do not have the right PED to use the K9."))
end
else
EnableMenu()
end
end)
-- Error for Identifier Whitelist
RegisterNetEvent("K9:IdentifierRestricted")
AddEventHandler("K9:IdentifierRestricted", function()
Notification(tostring("~r~You do not match any identifiers in the whitelist."))
end)
-- Spawns and Deletes K9
RegisterNetEvent("K9:ToggleK9")
AddEventHandler("K9:ToggleK9", function(model)
if spawned_ped == nil then
local ped = GetHashKey(model)
RequestModel(ped)
while not HasModelLoaded(ped) do
Citizen.Wait(1)
RequestModel(ped)
end
local plyCoords = GetOffsetFromEntityInWorldCoords(GetLocalPed(), 0.0, 2.0, 0.0)
local dog = CreatePed(28, ped, plyCoords.x, plyCoords.y, plyCoords.z, GetEntityHeading(GetLocalPed()), 0, 1)
spawned_ped = dog
SetBlockingOfNonTemporaryEvents(spawned_ped, true)
SetPedFleeAttributes(spawned_ped, 0, 0)
SetPedRelationshipGroupHash(spawned_ped, GetHashKey("k9"))
local blip = AddBlipForEntity(spawned_ped)
SetBlipAsFriendly(blip, true)
SetBlipSprite(blip, 442)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString(tostring("K9: ".. k9_name))
EndTextCommandSetBlipName(blip)
NetworkRegisterEntityAsNetworked(spawned_ped)
while not NetworkGetEntityIsNetworked(spawned_ped) do
NetworkRegisterEntityAsNetworked(spawned_ped)
Citizen.Wait(1)
end
else
local has_control = false
RequestNetworkControl(function(cb)
has_control = cb
end)
if has_control then
SetEntityAsMissionEntity(spawned_ped, true, true)
DeleteEntity(spawned_ped)
spawned_ped = nil
if attacking then
SetPedRelationshipGroupDefaultHash(target_ped, GetHashKey("CIVMALE"))
target_ped = nil
attacking = false
end
following = false
searching = false
playing_animation = false
end
end
end)
-- Toggles K9 to Follow / Heel
RegisterNetEvent("K9:ToggleFollow")
AddEventHandler("K9:ToggleFollow", function()
if spawned_ped ~= nil then
if not following then
local has_control = false
RequestNetworkControl(function(cb)
has_control = cb
end)
if has_control then
TaskFollowToOffsetOfEntity(spawned_ped, GetLocalPed(), 0.5, 0.0, 0.0, 5.0, -1, 0.0, 1)
SetPedKeepTask(spawned_ped, true)
following = true
attacking = false
Notification(tostring(k9_name .. " " .. language.follow))
end
else
local has_control = false
RequestNetworkControl(function(cb)
has_control = cb
end)
if has_control then
SetPedKeepTask(spawned_ped, false)
ClearPedTasks(spawned_ped)
following = false
attacking = false
Notification(tostring(k9_name .. " " .. language.stop))
end
end
end
end)
-- Toggles K9 In and Out of Vehicles
RegisterNetEvent("K9:ToggleVehicle")
AddEventHandler("K9:ToggleVehicle", function(isRestricted, vehList)
if not searching then
if IsPedInAnyVehicle(spawned_ped, false) then
TaskLeaveVehicle(spawned_ped, GetVehiclePedIsIn(spawned_ped, false), 256)
Notification(tostring(k9_name .. " " .. language.exit))
else
if not IsPedInAnyVehicle(GetLocalPed(), false) then
local plyCoords = GetEntityCoords(GetLocalPed(), false)
local vehicle = GetVehicleAheadOfPlayer()
local door = GetClosestVehicleDoor(vehicle)
if door ~= false then
if isRestricted then
if CheckVehicleRestriction(vehicle, vehList) then
TaskEnterVehicle(spawned_ped, vehicle, -1, door, 2.0, 1, 0)
Notification(tostring(k9_name .. " " .. language.enter))
end
else
TaskEnterVehicle(spawned_ped, vehicle, -1, door, 2.0, 1, 0)
Notification(tostring(k9_name .. " " .. language.enter))
end
end
else
local vehicle = GetVehiclePedIsIn(GetLocalPed(), false)
local door = 1
if isRestricted then
if CheckVehicleRestriction(vehicle, vehList) then
TaskEnterVehicle(spawned_ped, vehicle, -1, door, 2.0, 1, 0)
Notification(tostring(k9_name .. " " .. language.enter))
end
else
TaskEnterVehicle(spawned_ped, vehicle, -1, door, 2.0, 1, 0)
Notification(tostring(k9_name .. " " .. language.enter))
end
end
end
end
end)
-- Triggers K9 to Attack
RegisterNetEvent("K9:ToggleAttack")
AddEventHandler("K9:ToggleAttack", function(target)
if not attacking and not searching then
if IsPedAPlayer(target) then
local has_control = false
RequestNetworkControl(function(cb)
has_control = cb
end)
if has_control then
local player = GetPlayerFromServerId(GetPlayerId(target))
SetCanAttackFriendly(spawned_ped, true, true)
TaskPutPedDirectlyIntoMelee(spawned_ped, GetPlayerPed(player), 0.0, -1.0, 0.0, 0)
attacked_player = player
end
else
local has_control = false
RequestNetworkControl(function(cb)
has_control = cb
end)
if has_control then
SetCanAttackFriendly(spawned_ped, true, true)
TaskPutPedDirectlyIntoMelee(spawned_ped, target, 0.0, -1.0, 0.0, 0)
attacked_player = 0
end
end
attacking = true
following = false
Notification(tostring(k9_name .. " " .. language.attack))
end
end)
-- Triggers K9 to Search Vehicle
RegisterNetEvent("K9:SearchVehicle")
AddEventHandler("K9:SearchVehicle", function(items, openDoors)
local vehicle = GetVehicleAheadOfPlayer()
Citizen.Trace(tostring(vehicle))
Citizen.Trace(tostring(json.encode(items)))
if vehicle ~= 0 and not searching then
searching = true
local found_table = {}
Notification(tostring(k9_name .. " has began searching..."))
if openDoors then
SetVehicleDoorOpen(vehicle, 0, 0, 0)
SetVehicleDoorOpen(vehicle, 1, 0, 0)
SetVehicleDoorOpen(vehicle, 2, 0, 0)
SetVehicleDoorOpen(vehicle, 3, 0, 0)
SetVehicleDoorOpen(vehicle, 4, 0, 0)
SetVehicleDoorOpen(vehicle, 5, 0, 0)
SetVehicleDoorOpen(vehicle, 6, 0, 0)
SetVehicleDoorOpen(vehicle, 7, 0, 0)
end
-- Back Right
local offsetOne = GetOffsetFromEntityInWorldCoords(vehicle, 2.0, -2.0, 0.0)
TaskGoToCoordAnyMeans(spawned_ped, offsetOne.x, offsetOne.y, offsetOne.z, 5.0, 0, 0, 1, 10.0)
local oneItem = ChooseItem(items)
if oneItem ~= false then
table.insert(found_table, oneItem)
end
Citizen.Wait(7000)
-- Front Right
local offsetTwo = GetOffsetFromEntityInWorldCoords(vehicle, 2.0, 2.0, 0.0)
TaskGoToCoordAnyMeans(spawned_ped, offsetTwo.x, offsetTwo.y, offsetTwo.z, 5.0, 0, 0, 1, 10.0)
local twoItem = ChooseItem(items)
if twoItem ~= false then
table.insert(found_table, twoItem)
end
Citizen.Wait(7000)
-- Front Left
local offsetThree = GetOffsetFromEntityInWorldCoords(vehicle, -2.0, 2.0, 0.0)
TaskGoToCoordAnyMeans(spawned_ped, offsetThree.x, offsetThree.y, offsetThree.z, 5.0, 0, 0, 1, 10.0)
local threeItem = ChooseItem(items)
if threeItem ~= false then
table.insert(found_table, threeItem)
end
Citizen.Wait(7000)
-- Front Right
local offsetFour = GetOffsetFromEntityInWorldCoords(vehicle, -2.0, -2.0, 0.0)
TaskGoToCoordAnyMeans(spawned_ped, offsetFour.x, offsetFour.y, offsetFour.z, 5.0, 0, 0, 1, 10.0)
local fourItem = ChooseItem(items)
if fourItem ~= false then
table.insert(found_table, fourItem)
end
Citizen.Wait(7000)
if openDoors then
SetVehicleDoorsShut(vehicle, 0)
end
local stringified_table = {}
local found_illegal_item = false
for a = 1, #found_table do
table.insert(stringified_table, found_table[a].item)
if found_table[a].illegal then
found_illegal_item = true
end
end
if found_illegal_item then
PlayAnimation(animations['Normal'].searchhit.dict, animations['Normal'].searchhit.anim)
Citizen.Wait(3000)
PlayAnimation(animations['Normal'].sit.dict, animations['Normal'].sit.anim)
end
Notification(tostring(k9_name .. " has found [ " .. tostring(table.concat(stringified_table, ", ")) .. " ]."))
searching = false
end
end)
--]]
--[[ Threads ]]
-- Controls Menu
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
-- Trigger Opens Menu
if IsControlPressed(1, 19) and IsControlJustPressed(1, 213) then
TriggerServerEvent("K9:RequestOpenMenu")
end
-- Trigger Attack
if IsControlJustPressed(1, 47) and IsPlayerFreeAiming(PlayerId()) then
local bool, target = GetEntityPlayerIsFreeAimingAt(PlayerId())
if bool then
if IsEntityAPed(target) then
TriggerEvent("K9:ToggleAttack", target)
end
end
end
-- Trigger Follow
if IsControlJustPressed(1, 47) and not IsPlayerFreeAiming(PlayerId()) then
TriggerEvent("K9:ToggleFollow")
end
end
end)
-- DO NOT TOUCH (CLEANER)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
-- Setting K9 Settings
if just_started then
Citizen.Wait(1000)
local resource = GetCurrentResourceName()
SendNUIMessage({
type = "update_resource_name",
name = resource
})
just_started = false
TriggerServerEvent("K9:SendLanguage")
end
-- Deletes K9 when you die
if spawned_ped ~= nil and IsEntityDead(GetLocalPed()) then
TriggerEvent("K9:ToggleK9")
end
end
end)
--]]
--[[ EXTRA FUNCTIONS ]]--
-- Gets Local Ped
function GetLocalPed()
return GetPlayerPed(PlayerId())
end
-- Gets Control Of Ped
function RequestNetworkControl(callback)
local netId = NetworkGetNetworkIdFromEntity(spawned_ped)
local timer = 0
NetworkRequestControlOfNetworkId(netId)
while not NetworkHasControlOfNetworkId(netId) do
Citizen.Wait(1)
NetworkRequestControlOfNetworkId(netId)
timer = timer + 1
if timer == 5000 then
Citizen.Trace("Control failed")
callback(false)
break
end
end
callback(true)
end
-- Gets Players
function GetPlayers()
local players = {}
for i = 0, 32 do
if NetworkIsPlayerActive(i) then
table.insert(players, i)
end
end
return players
end
-- Get Searching item
function ChooseItem(items)
local number = math.random(1, 100)
if number > 70 and number < 95 then -- 70 | 95
local randomItem = math.random(1, #items)
return items[randomItem]
else
return false
end
end
-- Set K9 Animation (Sit / Laydown)
function PlayAnimation(dict, anim)
RequestAnimDict(dict)
while not HasAnimDictLoaded(dict) do
Citizen.Wait(0)
end
TaskPlayAnim(spawned_ped, dict, anim, 8.0, -8.0, -1, 2, 0.0, 0, 0, 0)
end
-- Gets Player ID
function GetPlayerId(target_ped)
local players = GetPlayers()
for a = 1, #players do
local ped = GetPlayerPed(players[a])
local server_id = GetPlayerServerId(players[a])
if target_ped == ped then
return server_id
end
end
return 0
end
-- Checks Ped Restriction
function CheckPedRestriction(ped, PedList)
for i = 1, #PedList do
if GetHashKey(PedList[i]) == GetEntityModel(ped) then
return true
end
end
return false
end
-- Checks Vehicle Restriction
function CheckVehicleRestriction(vehicle, VehicleList)
for i = 1, #VehicleList do
if GetHashKey(VehicleList[i]) == GetEntityModel(vehicle) then
return true
end
end
return false
end
-- Gets Vehicle Ahead Of Player
function GetVehicleAheadOfPlayer()
local lPed = GetLocalPed()
local lPedCoords = GetEntityCoords(lPed, alive)
local lPedOffset = GetOffsetFromEntityInWorldCoords(lPed, 0.0, 3.0, 0.0)
local rayHandle = StartShapeTestCapsule(lPedCoords.x, lPedCoords.y, lPedCoords.z, lPedOffset.x, lPedOffset.y, lPedOffset.z, 1.2, 10, lPed, 7)
local returnValue, hit, endcoords, surface, vehicle = GetShapeTestResult(rayHandle)
if hit then
return vehicle
else
return false
end
end
-- Gets Closest Door To Player
function GetClosestVehicleDoor(vehicle)
local plyCoords = GetEntityCoords(GetLocalPed(), false)
local backleft = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "door_dside_r"))
local backright = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "door_pside_r"))
local bldistance = GetDistanceBetweenCoords(backleft['x'], backleft['y'], backleft['z'], plyCoords.x, plyCoords.y, plyCoords.z, 1)
local brdistance = GetDistanceBetweenCoords(backright['x'], backright['y'], backright['z'], plyCoords.x, plyCoords.y, plyCoords.z, 1)
local found_door = false
if (bldistance < brdistance) then
found_door = 1
elseif(brdistance < bldistance) then
found_door = 2
end
return found_door
end
-- Displays Notification
function Notification(message)
SetNotificationTextEntry("STRING")
AddTextComponentString(message)
DrawNotification(0, 1)
end
--]]