-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcl_k9_handler.lua
More file actions
427 lines (378 loc) · 13.4 KB
/
Copy pathcl_k9_handler.lua
File metadata and controls
427 lines (378 loc) · 13.4 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
--[[
DO NOT TOUCH THIS. THIS CONTROLS THE DOG IF THIS BREAKS THE DOG BREAKS!!
--]]
local current_dog = nil
local following = false
local attacking = false
local animation_played = nil
local dog_name = "DefaultNameHere"
local closest_vehicle = nil
local closest_door_name = nil
local closest_door_pos = nil
local other_ped_attacked = nil
RegisterNetEvent("K9:ToggleDog")
AddEventHandler("K9:ToggleDog", function(data)
if current_dog == nil then
-- Spawn Dog
local model = GetHashKey(data.dogmodel)
RequestModel(model)
while not HasModelLoaded(model) do
RequestModel(model)
Citizen.Wait(100)
end
local plypos = GetOffsetFromEntityInWorldCoords(GetPlayerPed(PlayerId()), 0.0, 2.0, 0.0)
local plyhead = GetEntityHeading(GetPlayerPed(PlayerId()))
local spawned_entity = CreatePed(GetPedType(model), model, plypos.x, plypos.y, plypos.z, plyhead, 1, 1)
SetBlockingOfNonTemporaryEvents(spawned_entity, true)
SetPedFleeAttributes(spawned_entity, 0, 0)
current_dog = spawned_entity
local blip = AddBlipForEntity(current_dog)
SetBlipAsFriendly(blip, true)
SetBlipSprite(blip, 442)
BeginTextCommandSetBlipName("STRING");
AddTextComponentString(tostring("K9"))
EndTextCommandSetBlipName(blip)
NetworkRegisterEntityAsNetworked(current_dog)
while not NetworkGetEntityIsNetworked(current_dog) do
NetworkRegisterEntityAsNetworked(current_dog)
Citizen.Wait(1)
end
if data.godmode == true then
SetEntityInvincible(spawned_entity, true)
SetPedCanRagdoll(spawned_entity, false)
end
Notification("Your dog " .. dog_name .. " is in service.")
else
SetEntityAsMissionEntity(current_dog, true, true)
DeleteEntity(current_dog)
Notification("Your dog " .. dog_name .. " is out of service.")
current_dog = nil
end
end)
RegisterNetEvent("K9:Vehicle")
AddEventHandler("K9:Vehicle", function(data)
if current_dog ~= nil then
if IsPedInAnyVehicle(current_dog, false) then
TaskLeaveVehicle(current_dog, GetVehiclePedIsIn(current_dog, false), 256)
else
local plyPos = GetEntityCoords(GetPlayerPed(-1), false)
local vehicle = GetClosestVehicle(plyPos['x'], plyPos['y'], plyPos['z'], 3.0, 0, 23)
if data.VehicleRestricted == true then
if CheckVehicleRestriction(vehicle, data.VehicleList) == true then
TaskEnterVehicle(current_dog, vehicle, -1, 2, 2.0, 1, 0)
following = false
attacking = false
end
else
TaskEnterVehicle(current_dog, vehicle, -1, 2, 2.0, 1, 0)
following = false
attacking = false
end
end
end
end)
RegisterNetEvent("K9:Follow")
AddEventHandler("K9:Follow", function()
if current_dog ~= nil then
if following == false then
TaskFollowToOffsetOfEntity(current_dog, GetPlayerPed(PlayerId()), 0.5, 0.0, 0.0, 5.0, -1, 0.0, 1)
following = true
Notification(tostring(dog_name .. " Hier!")) -- Come
else
ClearPedTasks(current_dog)
following = false
Notification(tostring(dog_name .. " Bleib!")) -- Stay
end
end
end)
RegisterNetEvent("K9:Attack")
AddEventHandler("K9:Attack", function()
local dog_ped = current_dog
local bool, other_ped = GetEntityPlayerIsFreeAimingAt(PlayerId())
other_ped_attacked = other_ped
if attacking == false then
if IsEntityAPed(other_ped) then
if not IsEntityDead(other_ped) then
SetCanAttackFriendly(dog_ped, true, true)
TaskPutPedDirectlyIntoMelee(dog_ped, other_ped, 0.0, -1.0, 0.0, 0)
other_ped_attacked = other_ped
attacking = true
following = false
Notification(tostring(dog_name .. " Fass!")) -- Attack
end
end
else
attacking = false
other_ped_attacked = nil
ClearPedTasks(dog_ped)
Notification(tostring(dog_name .. " Zei Brav!")) -- Stopping Dog (Good Dog)
end
end)
RegisterNetEvent("K9:Animations")
AddEventHandler("K9:Animations", function(choice)
if animation_played ~= nil then -- Clear Animation Before Doing another action
if animation_played == "sit" then -- Sit End
sit(current_dog)
elseif animation_played == "laydown" then
laydown(current_dog)
end
else -- Start animation
if choice == "sit" then -- Sit Start
sit(current_dog)
elseif choice == "laydown" then -- Laydown Start
laydown(current_dog)
end
end
attacking = false
following = false
end)
--[[ ANIMATION FUNCTIONS ]]--
function sit(entity)
local animdicstart = "creatures@rottweiler@amb@world_dog_sitting@base"
local animnamestart = "base"
local animdicend = "creatures@rottweiler@amb@world_dog_sitting@exit"
local animnameend = "exit"
if IsEntityPlayingAnim(entity, animdicstart, animnamestart, 3) then
RequestAnimDict(animdicend)
while not HasAnimDictLoaded(animdicend) do
Citizen.Wait(100)
RequestAnimDict(animdicend)
end
TaskPlayAnim(current_dog, animdicend, animnameend, 1.0, -1, -1, 2, 0, 0, 0, 0)
if HasEntityAnimFinished(current_dog, animdicend, animnameend, 3) then
ClearPedSecondaryTask(current_dog)
end
animation_played = nil
Notification(tostring(dog_name .. " Stehen!"))
else
RequestAnimDict(animdicstart)
while not HasAnimDictLoaded(animdicstart) do
Citizen.Wait(100)
RequestAnimDict(animdicstart)
end
TaskPlayAnim(current_dog, animdicstart, animnamestart, 1.0, -1, -1, 2, 0, 0, 0, 0)
animation_played = "sit"
Notification(tostring(dog_name .. " Sitz!"))
end
end
function laydown(entity)
local animdicstart = "creatures@rottweiler@amb@sleep_in_kennel@"
local animnamestart = "sleep_in_kennel"
local animdicend = "creatures@rottweiler@amb@sleep_in_kennel@"
local animnameend = "exit_kennel"
if IsEntityPlayingAnim(entity, animdicstart, animnamestart, 3) then
RequestAnimDict(animdicend)
while not HasAnimDictLoaded(animdicend) do
Citizen.Wait(100)
RequestAnimDict(animdicend)
end
TaskPlayAnim(current_dog, animdicend, animnameend, 1.0, -1, -1, 2, 0, 0, 0, 0)
if HasEntityAnimFinished(current_dog, animdicend, animnameend, 3) then
ClearPedSecondaryTask(current_dog)
end
animation_played = nil
Notification(tostring(dog_name .. " Stehen!"))
else
RequestAnimDict(animdicstart)
while not HasAnimDictLoaded(animdicstart) do
Citizen.Wait(100)
RequestAnimDict(animdicstart)
end
TaskPlayAnim(current_dog, animdicstart, animnamestart, 1.0, -1, -1, 2, 0, 0, 0, 0)
animation_played = "laydown"
Notification(tostring(dog_name .. " Platz!"))
end
end
--]]
--[[ SEARCH FUNCTIONS ]]--
RegisterNetEvent('K9:Search')
AddEventHandler('K9:Search', function(data)
if not IsPedInAnyVehicle(GetPlayerPed(PlayerId(), false)) then
if closest_door_name ~= "Can't Search" then
if data.config == "random" then
Notification("Dog is searching")
local vHeading = GetEntityHeading(closest_vehicle)
Citizen.Wait(1500)
if closest_door_name == "Front Left Door" then
TaskGoToCoordAnyMeans(current_dog, closest_door_pos['x'], closest_door_pos['y'], closest_door_pos['z'], 5.0, 0, 0, 786603, 0xbf800000)
Citizen.Wait(3000)
TaskAchieveHeading(current_dog, vHeading - 90, -1)
SetVehicleDoorOpen(closest_vehicle, 0, false, false)
elseif closest_door_name == "Front Right Door" then
TaskGoToCoordAnyMeans(current_dog, closest_door_pos['x'], closest_door_pos['y'], closest_door_pos['z'], 5.0, 0, 0, 786603, 0xbf800000)
Citizen.Wait(3000)
TaskAchieveHeading(current_dog, vHeading - 270, -1)
SetVehicleDoorOpen(closest_vehicle, 1, false, false)
elseif closest_door_name == "Back Left Door" then
TaskGoToCoordAnyMeans(current_dog, closest_door_pos['x'], closest_door_pos['y'], closest_door_pos['z'], 5.0, 0, 0, 786603, 0xbf800000)
Citizen.Wait(3000)
TaskAchieveHeading(current_dog, vHeading - 90, -1)
SetVehicleDoorOpen(closest_vehicle, 2, false, false)
elseif closest_door_name == "Back Right Door" then
TaskGoToCoordAnyMeans(current_dog, closest_door_pos['x'], closest_door_pos['y'], closest_door_pos['z'], 5.0, 0, 0, 786603, 0xbf800000)
Citizen.Wait(3000)
TaskAchieveHeading(current_dog, vHeading - 270, -1)
SetVehicleDoorOpen(closest_vehicle, 3, false, false)
end
Citizen.Wait(4000)
TriggerEvent("chatMessage", tostring("^1K9 Found: ^0" .. RandomSearch(data.items) .. " in the " .. closest_door_name))
SetVehicleDoorsShut(closest_vehicle, false)
end
end
end
end)
function RandomSearch(items)
local timesToRun = math.random(1, 4)
for i = 1, timesToRun do
local category = math.random(1, 100)
if category > 31 and category <=80 then -- Clean Vehicle
itemFound = "Nothing Found"
return itemFound
else
if category >= 1 and category <= 10 then -- Illegal Choice
local list = items['illegal']
local chooseItem = math.random(1, #items['illegal'])
itemFound = items['illegal'][chooseItem]
return itemFound
elseif category > 10 and category <=30 then -- Legal Choice
local list = items['legal']
local chooseItem = math.random(1, #items['legal'])
itemFound = items['legal'][chooseItem]
return itemFound
elseif category > 80 and category <= 100 then -- Suspicious Items
local list = items['suspicious']
local chooseItem = math.random(1, #items['suspicious'])
itemFound = items['suspicious'][chooseItem]
return itemFound
end
end
end
end
--]]
-- Handling Key Bindings and Attacking auto stop --
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
-- Toggle Menu
if IsControlPressed(1, 19) and IsControlJustPressed(1,20) then -- LEFTALT + Z
TriggerServerEvent('K9:SendMenuToggle')
end
-- Follow Key --
if IsControlJustPressed(1, 47) and not IsPlayerFreeAiming(PlayerId()) and current_dog ~= nil then
TriggerEvent("K9:Follow")
end
-- Attacking Key --
if IsPlayerFreeAiming(PlayerId()) and IsControlJustPressed(1, 47) and current_dog ~= nil then
TriggerEvent("K9:Attack")
end
-- If other ped attacking dies it stops attacking --
if attacking == true then
if IsPedDeadOrDying(other_ped_attacked, 1) then
Notification("Pust!") -- Release Bite
TriggerEvent("K9:Attack")
end
end
end
end)
--[[ MENU FUNCTIONS ]]--
RegisterNetEvent("K9:OpenMenu")
AddEventHandler("K9:OpenMenu", function(data)
if not data.isRestricted then
EnableK9Menu()
else
if CheckPedRestriction(GetPlayerPed(PlayerId()), data.PedList) == true then
EnableK9Menu()
else
Notification("You are not allowed to use the K9 menu.")
end
end
end)
function EnableK9Menu()
SendNUIMessage({
type = "enable_k9_menu"
})
SetNuiFocus(true, true)
end
function DisableK9Menu()
SetNuiFocus(false, false)
end
RegisterNUICallback("triggerk9animation", function(data)
TriggerEvent("K9:Animations", data.anim)
end)
RegisterNUICallback("triggertogglek9", function(data)
TriggerServerEvent('K9:SendSpawnSettings')
end)
RegisterNUICallback("triggervehicletoggle", function(data)
TriggerServerEvent("K9:SendVehicleSettings")
end)
RegisterNUICallback("triggerk9search", function(data)
GetClosestVehicleDoor()
TriggerServerEvent("K9:SendSearchSettings")
end)
RegisterNUICallback("recievek9name", function(data)
dog_name = data.name
end)
RegisterNUICallback("disablek9menu", function(data)
DisableK9Menu()
end)
--]]
--[[ EXTRA FUNCTIONS ]]--
-- Converted to check tables from argument
function CheckVehicleRestriction(vehicle, VehicleList)
for i = 1, #VehicleList do
if GetHashKey(VehicleList[i]) == GetEntityModel(vehicle) then
return true
end
end
return false
end
-- Converted to check tables from argument
function CheckPedRestriction(ped, PedList)
for i = 1, #PedList do
print(tostring(GetHashKey(PedList[i])))
if GetHashKey(PedList[i]) == GetEntityModel(ped) then
return true
end
end
return false
end
function GetClosestVehicleDoor()
local plyCoords = GetEntityCoords(GetPlayerPed(PlayerId(), false))
local vehicle = GetClosestVehicle(plyCoords.x, plyCoords.y, plyCoords.z, 5.0, 0, 70)
local frontleft = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "door_dside_f"))
local frontright = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "door_pside_f"))
local backleft = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "door_dside_r"))
local backright = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "door_pside_r"))
local fldistance = GetDistanceBetweenCoords(frontleft['x'], frontleft['y'], frontleft['z'], plyCoords.x, plyCoords.y, plyCoords.z, 1)
local frdistance = GetDistanceBetweenCoords(frontright['x'], frontright['y'], frontright['z'], plyCoords.x, plyCoords.y, plyCoords.z, 1)
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)
if vehicle ~= 0 then
closest_vehicle = vehicle
if (fldistance < frdistance and fldistance < bldistance and fldistance < brdistance) then
-- Front Left
closest_door_pos = frontleft
closest_door_name = "Front Left Door"
elseif (frdistance < fldistance and frdistance < bldistance and frdistance < brdistance) then
-- Front Right
closest_door_pos = frontright
closest_door_name = "Front Right Door"
elseif (bldistance < fldistance and bldistance < frdistance and bldistance < brdistance) then
-- Back Left
closest_door_pos = backleft
closest_door_name = "Back Left Door"
elseif(brdistance < fldistance and brdistance < frdistance and brdistance < bldistance) then
-- Back Right
closest_door_pos = backright
closest_door_name = "Back Right Door"
end
else
closest_door_name = "Can't Search"
end
end
function Notification(message)
SetNotificationTextEntry("STRING")
AddTextComponentString(message)
DrawNotification(0, 1)
end
--]]