-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.lua
More file actions
1867 lines (1612 loc) · 58.1 KB
/
Copy pathfunctions.lua
File metadata and controls
1867 lines (1612 loc) · 58.1 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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----------------------------------------
-- Local Player Functions
----------------------------------------
--- 传送本地玩家
--- @param coords v3
--- @param heading? float
function teleport(coords, heading)
local ent = entities.get_user_vehicle_as_handle(false)
if ent == INVALID_GUID then
ent = players.user_ped()
end
TP_ENTITY(ent, coords, heading)
end
--- 传送本地玩家
--- @param x float
--- @param y float
--- @param z float
--- @param heading? float
function teleport2(x, y, z, heading)
teleport(v3.new(x, y, z), heading)
end
--- 设置/获取本地玩家朝向
--- @param heading? float
--- @return float
function user_heading(heading)
local ent = entities.get_user_vehicle_as_handle(false)
if ent == INVALID_GUID then
ent = players.user_ped()
end
if heading ~= nil then
ENTITY.SET_ENTITY_HEADING(ent, heading)
return heading
end
return ENTITY.GET_ENTITY_HEADING(ent)
end
--- 获取本地玩家室内 ID,不在室内则返回 -1
--- @return integer
function user_interior()
if INTERIOR.IS_INTERIOR_SCENE() then
return INTERIOR.GET_INTERIOR_FROM_ENTITY(players.user_ped())
end
return -1
end
--- 传送实体到本地玩家 `offset`: 左/右,前/后,上/下
--- @param entity Entity
--- @param offsetX? float
--- @param offsetY? float
--- @param offsetZ? float
function tp_entity_to_me(entity, offsetX, offsetY, offsetZ)
offsetX = offsetX or 0.0
offsetY = offsetY or 0.0
offsetZ = offsetZ or 0.0
local coords = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(players.user_ped(), offsetX, offsetY, offsetZ)
TP_ENTITY(entity, coords)
end
--- 传送本地玩家到实体 `offset`: 左/右,前/后,上/下
--- @param entity Entity
--- @param offsetX? float
--- @param offsetY? float
--- @param offsetZ? float
function tp_to_entity(entity, offsetX, offsetY, offsetZ)
offsetX = offsetX or 0.0
offsetY = offsetY or 0.0
offsetZ = offsetZ or 0.0
local coords = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(entity, offsetX, offsetY, offsetZ)
teleport(coords)
end
--- 本地玩家传送进载具
--- @param vehicle Vehicle
--- @param door? string *"delete"* : 删除车门
--- @param driver? string *"tp"* : 传送司机到外面, *"delete"* : 删除司机
--- @param seat? int default: -1
function tp_into_vehicle(vehicle, door, driver, seat)
seat = seat or -1
unlock_vehicle_doors(vehicle)
clear_vehicle_wanted(vehicle)
-- unfreeze
ENTITY.FREEZE_ENTITY_POSITION(vehicle, false)
VEHICLE.SET_VEHICLE_UNDRIVEABLE(vehicle, false)
if door == "delete" then
VEHICLE.SET_VEHICLE_DOOR_BROKEN(vehicle, 0, true) -- left front door
end
if driver then
local ped = VEHICLE.GET_PED_IN_VEHICLE_SEAT(vehicle, seat)
if ped ~= 0 then
if driver == "tp" then
set_entity_move(ped, 0.0, 5.0, 3.0)
elseif driver == "delete" then
entities.delete(ped)
end
end
end
VEHICLE.SET_VEHICLE_ENGINE_ON(vehicle, true, true, false)
VEHICLE.SET_HELI_BLADES_FULL_SPEED(vehicle)
PED.SET_PED_INTO_VEHICLE(players.user_ped(), vehicle, seat)
end
--- 传送载具到本地玩家,本地玩家传送进载具
--- @param vehicle Vehicle
--- @param door? string *"delete"* : 删除车门
--- @param driver? string *"tp"* : 传送司机到外面, *"delete"* : 删除司机
--- @param seat? int default: -1
function tp_vehicle_to_me(vehicle, door, driver, seat)
ENTITY_HEADING(vehicle, user_heading())
tp_entity_to_me(vehicle)
tp_into_vehicle(vehicle, door, driver, seat)
end
--- 传送拾取物到本地玩家
--- @param pickup Pickup
--- @param attachToSelf? boolean
function tp_pickup_to_me(pickup, attachToSelf)
if ENTITY.IS_ENTITY_ATTACHED(pickup) then
ENTITY.DETACH_ENTITY(pickup, true, true)
ENTITY.SET_ENTITY_VISIBLE(pickup, true, false)
end
OBJECT.SET_PICKUP_OBJECT_COLLECTABLE_IN_VEHICLE(pickup)
if attachToSelf then
OBJECT.ATTACH_PORTABLE_PICKUP_TO_PED(pickup, players.user_ped())
else
tp_entity_to_me(pickup)
end
end
--- 本地玩家与实体绘制连线
--- @param entity Entity
--- @param colour Colour?
function draw_line_to_entity(entity, colour)
local player_pos = ENTITY.GET_ENTITY_COORDS(players.user_ped())
local ent_pos = ENTITY.GET_ENTITY_COORDS(entity)
DRAW_LINE(player_pos, ent_pos, colour)
end
----------------------------------------
-- Entity Functions
----------------------------------------
--- 获取实体类型 (Integer)
--- @param entity Entity
--- @return ENTITY_TYPE
function get_entity_type(entity)
if not ENTITY.DOES_ENTITY_EXIST(entity) or not ENTITY.IS_AN_ENTITY(entity) then
return ENTITY_NONE
end
if ENTITY.IS_ENTITY_A_PED(entity) then
return ENTITY_PED
end
if ENTITY.IS_ENTITY_A_VEHICLE(entity) then
return ENTITY_VEHICLE
end
if ENTITY.IS_ENTITY_AN_OBJECT(entity) then
if OBJECT.IS_OBJECT_A_PICKUP(entity) or OBJECT.IS_OBJECT_A_PORTABLE_PICKUP(entity) then
return ENTITY_PICKUP
end
return ENTITY_OBJECT
end
return ENTITY_NONE
end
--- 获取实体类型 (String)
---
--- `text_type`: 1 小写, 2 首字母大写, 3 大写 (默认: 2)
--- @param entity Entity
--- @param textType integer?
--- @return string
function get_entity_type_text(entity, textType)
local entity_type_list = {
[ENTITY_NONE] = "No Entity",
[ENTITY_PED] = "Ped",
[ENTITY_VEHICLE] = "Vehicle",
[ENTITY_OBJECT] = "Object",
[ENTITY_PICKUP] = "Pickup"
}
local entity_type = get_entity_type(entity)
entity_type = entity_type_list[entity_type]
if textType == 1 then
return string.lower(entity_type)
end
if textType == 3 then
return string.upper(entity_type)
end
return entity_type
end
--- 设置实体左/右、前/后、上/下移动
--- @param entity Entity
--- @param offsetX float
--- @param offsetY float
--- @param offsetZ float
function set_entity_move(entity, offsetX, offsetY, offsetZ)
local coords = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(entity, offsetX, offsetY, offsetZ)
TP_ENTITY(entity, coords)
end
--- 设置实体与目标实体之间的朝向角度差
--- @param setEntity Entity
--- @param toEntity Entity
--- @param angle? float
function set_entity_heading_to_entity(setEntity, toEntity, angle)
angle = angle or 0.0
local heading = ENTITY.GET_ENTITY_HEADING(toEntity)
ENTITY.SET_ENTITY_HEADING(setEntity, heading + angle)
end
--- 传送实体到目标实体
---
--- `offset`: 左/右,前/后,上/下
--- @param tpEntity Entity
--- @param toEntity Entity
--- @param offsetX? float
--- @param offsetY? float
--- @param offsetZ? float
function tp_entity_to_entity(tpEntity, toEntity, offsetX, offsetY, offsetZ)
offsetX = offsetX or 0.0
offsetY = offsetY or 0.0
offsetZ = offsetZ or 0.0
local coords = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(toEntity, offsetX, offsetY, offsetZ)
TP_ENTITY(tpEntity, coords)
end
--- @param entity Entity
--- @param toggle boolean
function set_entity_godmode(entity, toggle)
if not ENTITY.DOES_ENTITY_EXIST(entity) then
return false
end
ENTITY.SET_ENTITY_INVINCIBLE(entity, toggle)
ENTITY.SET_ENTITY_PROOFS(entity, toggle, toggle, toggle, toggle, toggle, toggle, toggle, toggle)
ENTITY.SET_ENTITY_CAN_BE_DAMAGED(entity, not toggle)
end
--- 获取拥有实体控制权的玩家名称
--- @param entity Entity
--- @return string
function get_entity_owner_name(entity)
return players.get_name(entities.get_owner(entity))
end
----------------------------------------
-- Get Entities Functions
----------------------------------------
--- 获取对应类型的所有实体
--- @param entityType ENTITY_TYPE
--- @return table<int, Entity>
function get_all_entities(entityType)
if entityType == ENTITY_PED then
return entities.get_all_peds_as_handles()
end
if entityType == ENTITY_VEHICLE then
return entities.get_all_vehicles_as_handles()
end
if entityType == ENTITY_OBJECT then
return entities.get_all_objects_as_handles()
end
if entityType == ENTITY_PICKUP then
return entities.get_all_pickups_as_handles()
end
return {}
end
--- 获取对应类型的所有实体指针
--- @param entityType ENTITY_TYPE
--- @return table<int, pointer>
function get_all_entities_as_pointers(entityType)
if entityType == ENTITY_PED then
return entities.get_all_peds_as_pointers()
end
if entityType == ENTITY_VEHICLE then
return entities.get_all_vehicles_as_pointers()
end
if entityType == ENTITY_OBJECT then
return entities.get_all_objects_as_pointers()
end
if entityType == ENTITY_PICKUP then
return entities.get_all_pickups_as_pointers()
end
return {}
end
--- 通过 model hash 获取实体
--- @param entityType ENTITY_TYPE
--- @param isMission boolean
--- @param ... Hash
--- @return table<int, Entity>
function get_entities_by_hash(entityType, isMission, ...)
local entity_list = {}
local hash_list = { ... }
for key, entity_ptr in pairs(get_all_entities_as_pointers(entityType)) do
local entity_hash = entities.get_model_hash(entity_ptr)
for _, hash in pairs(hash_list) do
if entity_hash == hash then
local entity = entities.pointer_to_handle(entity_ptr)
if isMission then
if ENTITY.IS_ENTITY_A_MISSION_ENTITY(entity) then
table.insert(entity_list, entity)
end
else
table.insert(entity_list, entity)
end
end
end
end
return entity_list
end
--- 通过 model hash 获取指定脚本的任务实体
--- @param entityType ENTITY_TYPE
--- @param scriptName string
--- @param ... Hash
--- @return table<int, Entity>
function get_mission_entities_by_hash(entityType, scriptName, ...)
if not IS_SCRIPT_RUNNING(scriptName) then
return {}
end
local entity_list = {}
local hash_list = { ... }
for key, entity_ptr in pairs(get_all_entities_as_pointers(entityType)) do
local entity_hash = entities.get_model_hash(entity_ptr)
for _, hash in pairs(hash_list) do
if entity_hash == hash then
local entity = entities.pointer_to_handle(entity_ptr)
if GET_ENTITY_SCRIPT(entity) == scriptName then
table.insert(entity_list, entity)
end
end
end
end
return entity_list
end
--- 获取指定脚本的任务拾取物
--- @param scriptName string
--- @return table<int, Entity>
function get_mission_pickups(scriptName)
if not IS_SCRIPT_RUNNING(scriptName) then
return {}
end
local entity_list = {}
for _, entity in pairs(entities.get_all_pickups_as_handles()) do
if GET_ENTITY_SCRIPT(entity) == scriptName then
table.insert(entity_list, entity)
end
end
return entity_list
end
--- 获取距离坐标最近的实体
--- @param entityType ENTITY_TYPE
--- @param coords v3
--- @param isMission boolean? default: false
--- @param radius float? default: 100.0
--- @return Entity
function get_closest_entity(entityType, coords, isMission, radius)
radius = radius or 100.0
local closest_disance = 9999.0
local closest_ent = 0
for _, ent in pairs(get_all_entities(entityType)) do
if isMission and not ENTITY.IS_ENTITY_A_MISSION_ENTITY(ent) then
goto continue
end
local ent_pos = ENTITY.GET_ENTITY_COORDS(ent)
local distance = v3.distance(coords, ent_pos)
if distance <= radius then
if distance < closest_disance then
closest_disance = distance
closest_ent = ent
end
end
::continue::
end
return closest_ent
end
--- 获取坐标附近的实体
--- @param entityType ENTITY_TYPE
--- @param coords v3
--- @param radius float
--- @return table<int, Entity>
function get_nearby_entities(entityType, coords, radius)
local entity_list = {}
for _, ent in pairs(get_all_entities(entityType)) do
local ent_pos = ENTITY.GET_ENTITY_COORDS(ent)
if radius <= 0 then
table.insert(entity_list, ent)
elseif v3.distance(coords, ent_pos) <= radius then
table.insert(entity_list, ent)
end
end
return entity_list
end
----------------------------------------
-- Check Entity Functions
----------------------------------------
--- 判断是否为玩家
--- @param ped Ped
--- @return boolean
function is_player_ped(ped)
return entities.is_player_ped(ped)
end
--- 判断是否为玩家载具
--- @param vehicle Vehicle
--- @return boolean
function is_player_vehicle(vehicle)
if vehicle == entities.get_user_vehicle_as_handle() or vehicle == entities.get_user_personal_vehicle_as_handle() then
return true
end
local driver = VEHICLE.GET_PED_IN_VEHICLE_SEAT(vehicle, -1, false)
if driver ~= 0 then
if is_player_ped(driver) then
return true
end
end
return false
end
--- 判断是否为实体
--- @param entity Entity
--- @return boolean
function IS_AN_ENTITY(entity)
if ENTITY.DOES_ENTITY_EXIST(entity) then
if ENTITY.IS_ENTITY_A_PED(entity) or ENTITY.IS_ENTITY_A_VEHICLE(entity) or ENTITY.IS_ENTITY_AN_OBJECT(entity) then
return true
end
end
return false
end
--- 判断是否为拾取物
--- @param entity Entity
--- @return boolean
function IS_ENTITY_A_PICKUP(entity)
if ENTITY.IS_ENTITY_AN_OBJECT(entity) then
if OBJECT.IS_OBJECT_A_PICKUP(entity) or OBJECT.IS_OBJECT_A_PORTABLE_PICKUP(entity) then
return true
end
end
return false
end
--- 判断是否为敌对实体
--- @param entity Entity
--- @return boolean
function is_hostile_entity(entity)
if ENTITY.IS_ENTITY_A_PED(entity) then
if is_hostile_ped(entity) then
return true
end
end
if ENTITY.IS_ENTITY_A_VEHICLE(entity) then
local driver = VEHICLE.GET_PED_IN_VEHICLE_SEAT(entity, -1)
if is_hostile_ped(driver) then
return true
end
end
local blip = HUD.GET_BLIP_FROM_ENTITY(entity)
if HUD.DOES_BLIP_EXIST(blip) then
local blip_colour = HUD.GET_BLIP_COLOUR(blip)
if blip_colour == 1 or blip_colour == 59 then -- red
return true
end
end
return false
end
----------------------------------------
-- Create Entity Functions
----------------------------------------
--- @param ped_type int
--- @param hash Hash
--- @param coords v3
--- @param heading float
--- @param is_networked boolean? [default = true]
--- @param is_mission boolean? [default = true]
--- @return Ped
function create_ped(ped_type, hash, coords, heading, is_networked, is_mission)
if is_networked == nil then is_networked = true end
if is_mission == nil then is_mission = true end
request_model(hash)
local ped = PED.CREATE_PED(ped_type, hash, coords.x, coords.y, coords.z, heading, is_networked, is_mission)
ENTITY.SET_ENTITY_LOAD_COLLISION_FLAG(ped, true, 1)
ENTITY.SET_ENTITY_SHOULD_FREEZE_WAITING_ON_COLLISION(ped, true)
if is_mission then
ENTITY.SET_ENTITY_AS_MISSION_ENTITY(ped, true, false)
end
if is_networked then
NETWORK.NETWORK_REGISTER_ENTITY_AS_NETWORKED(ped)
local net_id = NETWORK.NETWORK_GET_NETWORK_ID_FROM_ENTITY(ped)
NETWORK.SET_NETWORK_ID_EXISTS_ON_ALL_MACHINES(net_id, true)
NETWORK.SET_NETWORK_ID_CAN_MIGRATE(net_id, true)
for _, pid in pairs(players.list()) do
NETWORK.SET_NETWORK_ID_ALWAYS_EXISTS_FOR_PLAYER(net_id, pid, true)
end
end
STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(hash)
return ped
end
--- @param hash Hash
--- @param coords v3
--- @param heading float
--- @param is_networked boolean? [default = true]
--- @param is_mission boolean? [default = true]
--- @return Vehicle
function create_vehicle(hash, coords, heading, is_networked, is_mission)
if is_networked == nil then is_networked = true end
if is_mission == nil then is_mission = true end
request_model(hash)
local veh = VEHICLE.CREATE_VEHICLE(hash, coords.x, coords.y, coords.z, heading, is_networked, is_mission, false)
ENTITY.SET_ENTITY_LOAD_COLLISION_FLAG(veh, true, 1)
ENTITY.SET_ENTITY_SHOULD_FREEZE_WAITING_ON_COLLISION(veh, true)
if is_mission then
ENTITY.SET_ENTITY_AS_MISSION_ENTITY(veh, true, false)
end
if is_networked then
NETWORK.NETWORK_REGISTER_ENTITY_AS_NETWORKED(veh)
local net_id = NETWORK.NETWORK_GET_NETWORK_ID_FROM_ENTITY(veh)
NETWORK.SET_NETWORK_ID_EXISTS_ON_ALL_MACHINES(net_id, true)
NETWORK.SET_NETWORK_ID_CAN_MIGRATE(net_id, true)
for _, pid in pairs(players.list()) do
NETWORK.SET_NETWORK_ID_ALWAYS_EXISTS_FOR_PLAYER(net_id, pid, true)
end
end
STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(hash)
return veh
end
--- @param hash Hash
--- @param coords v3
--- @param is_networked boolean? [default = true]
--- @param is_mission boolean? [default = true]
--- @return Object
function create_object(hash, coords, is_networked, is_mission)
if is_networked == nil then is_networked = true end
if is_mission == nil then is_mission = true end
request_model(hash)
local obj = OBJECT.CREATE_OBJECT_NO_OFFSET(hash, coords.x, coords.y, coords.z, is_networked, is_mission, false)
ENTITY.SET_ENTITY_LOAD_COLLISION_FLAG(obj, true, 1)
ENTITY.SET_ENTITY_SHOULD_FREEZE_WAITING_ON_COLLISION(obj, true)
if is_mission then
ENTITY.SET_ENTITY_AS_MISSION_ENTITY(obj, true, false)
end
if is_networked then
NETWORK.NETWORK_REGISTER_ENTITY_AS_NETWORKED(obj)
local net_id = NETWORK.NETWORK_GET_NETWORK_ID_FROM_ENTITY(obj)
NETWORK.SET_NETWORK_ID_EXISTS_ON_ALL_MACHINES(net_id, true)
NETWORK.SET_NETWORK_ID_CAN_MIGRATE(net_id, true)
for _, pid in pairs(players.list()) do
NETWORK.SET_NETWORK_ID_ALWAYS_EXISTS_FOR_PLAYER(net_id, pid, true)
end
end
STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(hash)
return obj
end
----------------------------------------
-- Clone Entity Functions
----------------------------------------
--- 复制 Ped
--- @param target_ped Ped
--- @param coords v3
--- @param heading float
--- @param is_networked boolean
--- @return Ped
function clone_target_ped(target_ped, coords, heading, is_networked)
if ENTITY.DOES_ENTITY_EXIST(target_ped) and ENTITY.IS_ENTITY_A_PED(target_ped) then
local ped_type = PED.GET_PED_TYPE(target_ped)
local hash = ENTITY.GET_ENTITY_MODEL(target_ped)
local clone_ped = create_ped(ped_type, hash, coords, heading, is_networked)
PED.CLONE_PED_TO_TARGET(target_ped, clone_ped)
return clone_ped
end
return 0
end
--- 复制目标 Vehicle 的数据 应用到 克隆 Vehicle
--- @param target_vehicle Vehicle
--- @param clone_vehicle Vehicle
function clone_target_vehicle_data(target_vehicle, clone_vehicle)
VEHICLE.SET_VEHICLE_MOD_KIT(clone_vehicle, 0)
for i = 17, 22 do
VEHICLE.TOGGLE_VEHICLE_MOD(clone_vehicle, i, VEHICLE.IS_TOGGLE_MOD_ON(target_vehicle, i))
end
for i = 0, 49 do
local modValue = VEHICLE.GET_VEHICLE_MOD(target_vehicle, i)
VEHICLE.SET_VEHICLE_MOD(clone_vehicle, i, modValue)
end
for i = 1, 14 do
VEHICLE.SET_VEHICLE_EXTRA(clone_vehicle, i, not VEHICLE.IS_VEHICLE_EXTRA_TURNED_ON(target_vehicle, i))
end
local colorR, colorG, colorB = memory.alloc(1), memory.alloc(1), memory.alloc(1)
if VEHICLE.GET_IS_VEHICLE_PRIMARY_COLOUR_CUSTOM(target_vehicle) then
VEHICLE.GET_VEHICLE_CUSTOM_PRIMARY_COLOUR(target_vehicle, colorR, colorG, colorB)
VEHICLE.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG),
memory.read_ubyte(colorB))
else
VEHICLE.GET_VEHICLE_MOD_COLOR_1(target_vehicle, colorR, colorG, colorB)
VEHICLE.SET_VEHICLE_MOD_COLOR_1(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG),
memory.read_ubyte(colorB))
end
if VEHICLE.GET_IS_VEHICLE_SECONDARY_COLOUR_CUSTOM(target_vehicle) then
VEHICLE.GET_VEHICLE_CUSTOM_SECONDARY_COLOUR(target_vehicle, colorR, colorG, colorB)
VEHICLE.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG),
memory.read_ubyte(colorB))
else
VEHICLE.GET_VEHICLE_MOD_COLOR_2(target_vehicle, colorR, colorG)
VEHICLE.SET_VEHICLE_MOD_COLOR_2(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG))
end
VEHICLE.GET_VEHICLE_COLOURS(target_vehicle, colorR, colorG)
VEHICLE.SET_VEHICLE_COLOURS(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG))
VEHICLE.GET_VEHICLE_EXTRA_COLOURS(target_vehicle, colorR, colorG)
VEHICLE.SET_VEHICLE_EXTRA_COLOURS(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG))
VEHICLE.GET_VEHICLE_EXTRA_COLOUR_5(target_vehicle, colorR)
VEHICLE.GET_VEHICLE_EXTRA_COLOUR_6(target_vehicle, colorG)
VEHICLE.SET_VEHICLE_EXTRA_COLOUR_5(clone_vehicle, memory.read_ubyte(colorR))
VEHICLE.SET_VEHICLE_EXTRA_COLOUR_6(clone_vehicle, memory.read_ubyte(colorG))
VEHICLE.GET_VEHICLE_TYRE_SMOKE_COLOR(target_vehicle, colorR, colorG, colorB)
VEHICLE.SET_VEHICLE_TYRE_SMOKE_COLOR(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG),
memory.read_ubyte(colorB))
VEHICLE.GET_VEHICLE_NEON_COLOUR(target_vehicle, colorR, colorG, colorB)
VEHICLE.SET_VEHICLE_NEON_COLOUR(clone_vehicle, memory.read_ubyte(colorR), memory.read_ubyte(colorG),
memory.read_ubyte(colorB))
for i = 0, 3 do
VEHICLE.SET_VEHICLE_NEON_ENABLED(clone_vehicle, i, VEHICLE.GET_VEHICLE_NEON_ENABLED(target_vehicle, i))
end
local windowTint = VEHICLE.GET_VEHICLE_WINDOW_TINT(target_vehicle)
VEHICLE.SET_VEHICLE_WINDOW_TINT(clone_vehicle, windowTint)
local lightsColor = VEHICLE.GET_VEHICLE_XENON_LIGHT_COLOR_INDEX(target_vehicle)
VEHICLE.SET_VEHICLE_XENON_LIGHT_COLOR_INDEX(clone_vehicle, lightsColor)
VEHICLE.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX(clone_vehicle,
VEHICLE.GET_VEHICLE_NUMBER_PLATE_TEXT_INDEX(target_vehicle))
VEHICLE.SET_VEHICLE_NUMBER_PLATE_TEXT(clone_vehicle, VEHICLE.GET_VEHICLE_NUMBER_PLATE_TEXT(target_vehicle))
VEHICLE.SET_VEHICLE_TYRES_CAN_BURST(clone_vehicle, VEHICLE.GET_VEHICLE_TYRES_CAN_BURST(target_vehicle))
VEHICLE.SET_VEHICLE_DIRT_LEVEL(clone_vehicle, VEHICLE.GET_VEHICLE_DIRT_LEVEL(target_vehicle))
local roofState = VEHICLE.GET_CONVERTIBLE_ROOF_STATE(target_vehicle)
if roofState == 1 or roofState == 2 then
VEHICLE.LOWER_CONVERTIBLE_ROOF(clone_vehicle, true)
end
end
--- 复制 Vehicle
--- @param target_vehicle Vehicle
--- @param coords v3
--- @param heading float
--- @param is_networked boolean
--- @return Vehicle
function clone_target_vehicle(target_vehicle, coords, heading, is_networked)
if ENTITY.DOES_ENTITY_EXIST(target_vehicle) and ENTITY.IS_ENTITY_A_VEHICLE(target_vehicle) then
local hash = ENTITY.GET_ENTITY_MODEL(target_vehicle)
local clone_vehicle = create_vehicle(hash, coords, heading, is_networked)
clone_target_vehicle_data(target_vehicle, clone_vehicle)
return clone_vehicle
end
return 0
end
--- 复制 Object
--- @param target_object Object
--- @param coords v3
--- @param is_networked boolean
--- @return Object
function clone_target_object(target_object, coords, is_networked)
if ENTITY.DOES_ENTITY_EXIST(target_object) and ENTITY.IS_ENTITY_AN_OBJECT(target_object) then
local hash = ENTITY.GET_ENTITY_MODEL(target_object)
local clone_object = create_object(hash, coords, is_networked)
local rotation = ENTITY.GET_ENTITY_ROTATION(target_object, 2)
ENTITY.SET_ENTITY_ROTATION(clone_object, rotation.x, rotation.y, rotation.z, 2, true)
return clone_object
end
return 0
end
----------------------------------------
-- Vehicle Functions
----------------------------------------
--- 升级载具(排除喇叭、悬挂、车轮、涂装)
--- @param vehicle Vehicle
function upgrade_vehicle(vehicle)
if not ENTITY.IS_ENTITY_A_VEHICLE(vehicle) then
return
end
VEHICLE.SET_VEHICLE_MOD_KIT(vehicle, 0)
local excluded_mod_types = { 14, 15, 23, 24, 48 }
for i = 0, 50 do
if not table.contains(excluded_mod_types, i) then
local mod_num = VEHICLE.GET_NUM_VEHICLE_MODS(vehicle, i)
if mod_num > 0 then
VEHICLE.SET_VEHICLE_MOD(vehicle, i, mod_num - 1, false)
end
end
end
for i = 17, 22 do
VEHICLE.TOGGLE_VEHICLE_MOD(vehicle, i, true)
end
VEHICLE.SET_VEHICLE_TYRES_CAN_BURST(vehicle, false)
VEHICLE.SET_VEHICLE_WHEELS_CAN_BREAK(vehicle, false)
VEHICLE.SET_VEHICLE_HAS_UNBREAKABLE_LIGHTS(vehicle, true)
VEHICLE.SET_VEHICLE_CAN_ENGINE_MISSFIRE(vehicle, false)
VEHICLE.SET_VEHICLE_CAN_LEAK_OIL(vehicle, false)
VEHICLE.SET_VEHICLE_CAN_LEAK_PETROL(vehicle, false)
VEHICLE.SET_DISABLE_VEHICLE_ENGINE_FIRES(vehicle, true)
VEHICLE.SET_DISABLE_VEHICLE_PETROL_TANK_FIRES(vehicle, true)
VEHICLE.SET_DISABLE_VEHICLE_PETROL_TANK_DAMAGE(vehicle, true)
for i = 0, 3 do
VEHICLE.SET_DOOR_ALLOWED_TO_BE_BROKEN_OFF(vehicle, i, false)
end
VEHICLE.SET_HELI_TAIL_BOOM_CAN_BREAK_OFF(vehicle, false)
end
--- 修复载具
--- @param vehicle Vehicle
function fix_vehicle(vehicle)
if not ENTITY.IS_ENTITY_A_VEHICLE(vehicle) then
return
end
VEHICLE.SET_VEHICLE_FIXED(vehicle)
VEHICLE.SET_VEHICLE_DEFORMATION_FIXED(vehicle)
VEHICLE.SET_VEHICLE_DIRT_LEVEL(vehicle, 0.0)
VEHICLE.SET_VEHICLE_ENGINE_HEALTH(vehicle, 5000.0)
SET_ENTITY_HEALTH(vehicle, ENTITY.GET_ENTITY_MAX_HEALTH(vehicle))
end
--- 强化载具
--- @param vehicle Vehicle
function strong_vehicle(vehicle)
if not ENTITY.IS_ENTITY_A_VEHICLE(vehicle) then
return
end
VEHICLE.SET_VEHICLE_CAN_BREAK(vehicle, false)
VEHICLE.SET_VEHICLE_TYRES_CAN_BURST(vehicle, false)
VEHICLE.SET_VEHICLE_WHEELS_CAN_BREAK(vehicle, false)
for i = 0, 5 do
if VEHICLE.GET_IS_DOOR_VALID(vehicle, i) then
VEHICLE.SET_DOOR_ALLOWED_TO_BE_BROKEN_OFF(vehicle, i, false)
end
end
VEHICLE.SET_VEHICLE_HAS_UNBREAKABLE_LIGHTS(vehicle, true)
VEHICLE.SET_VEHICLE_CAN_ENGINE_MISSFIRE(vehicle, false)
VEHICLE.SET_VEHICLE_CAN_LEAK_OIL(vehicle, false)
VEHICLE.SET_VEHICLE_CAN_LEAK_PETROL(vehicle, false)
VEHICLE.SET_DISABLE_VEHICLE_ENGINE_FIRES(vehicle, true)
VEHICLE.SET_DISABLE_VEHICLE_PETROL_TANK_FIRES(vehicle, true)
VEHICLE.SET_DISABLE_VEHICLE_PETROL_TANK_DAMAGE(vehicle, true)
VEHICLE.SET_VEHICLE_STRONG(vehicle, true)
VEHICLE.SET_VEHICLE_HAS_STRONG_AXLES(vehicle, true)
--Damage
VEHICLE.VEHICLE_SET_RAMP_AND_RAMMING_CARS_TAKE_DAMAGE(vehicle, false)
VEHICLE.SET_INCREASE_WHEEL_CRUSH_DAMAGE(vehicle, false)
VEHICLE.SET_DISABLE_DAMAGE_WITH_PICKED_UP_ENTITY(vehicle, 1)
VEHICLE.SET_VEHICLE_USES_MP_PLAYER_DAMAGE_MULTIPLIER(vehicle, 1)
VEHICLE.SET_FORCE_VEHICLE_ENGINE_DAMAGE_BY_BULLET(vehicle, false)
--Explode
VEHICLE.SET_VEHICLE_NO_EXPLOSION_DAMAGE_FROM_DRIVER(vehicle, true)
VEHICLE.SET_DISABLE_EXPLODE_FROM_BODY_DAMAGE_ON_COLLISION(vehicle, 1)
VEHICLE.SET_VEHICLE_EXPLODES_ON_HIGH_EXPLOSION_DAMAGE(vehicle, false)
VEHICLE.SET_VEHICLE_EXPLODES_ON_EXPLOSION_DAMAGE_AT_ZERO_BODY_HEALTH(vehicle, false)
VEHICLE.SET_ALLOW_VEHICLE_EXPLODES_ON_CONTACT(vehicle, false)
--Heli
VEHICLE.SET_HELI_TAIL_BOOM_CAN_BREAK_OFF(vehicle, false)
VEHICLE.SET_DISABLE_HELI_EXPLODE_FROM_BODY_DAMAGE(vehicle, 1)
--MP Only
VEHICLE.SET_PLANE_RESIST_TO_EXPLOSION(vehicle, true)
VEHICLE.SET_HELI_RESIST_TO_EXPLOSION(vehicle, true)
--Remove Check
VEHICLE.REMOVE_VEHICLE_UPSIDEDOWN_CHECK(vehicle)
VEHICLE.REMOVE_VEHICLE_STUCK_CHECK(vehicle)
end
--- 解锁载具车门
--- @param vehicle Vehicle
function unlock_vehicle_doors(vehicle)
if not ENTITY.IS_ENTITY_A_VEHICLE(vehicle) then
return
end
VEHICLE.SET_VEHICLE_DOORS_LOCKED(vehicle, 1)
VEHICLE.SET_VEHICLE_DOORS_LOCKED_FOR_ALL_PLAYERS(vehicle, false)
VEHICLE.SET_VEHICLE_DOORS_LOCKED_FOR_NON_SCRIPT_PLAYERS(vehicle, false)
VEHICLE.SET_VEHICLE_DOORS_LOCKED_FOR_ALL_TEAMS(vehicle, false)
VEHICLE.SET_DONT_ALLOW_PLAYER_TO_ENTER_VEHICLE_IF_LOCKED_FOR_PLAYER(vehicle, false)
VEHICLE.SET_VEHICLE_IS_CONSIDERED_BY_PLAYER(vehicle, true)
VEHICLE.SET_VEHICLE_EXCLUSIVE_DRIVER(vehicle, 0, 0)
end
--- 清除载具通缉状态
--- @param vehicle Vehicle
function clear_vehicle_wanted(vehicle)
if not ENTITY.IS_ENTITY_A_VEHICLE(vehicle) then
return
end
VEHICLE.SET_VEHICLE_HAS_BEEN_OWNED_BY_PLAYER(vehicle, true)
VEHICLE.SET_VEHICLE_IS_STOLEN(vehicle, false)
VEHICLE.SET_VEHICLE_IS_WANTED(vehicle, false)
VEHICLE.SET_POLICE_FOCUS_WILL_TRACK_VEHICLE(vehicle, false)
VEHICLE.SET_VEHICLE_INFLUENCES_WANTED_LEVEL(vehicle, false)
VEHICLE.SET_DISABLE_WANTED_CONES_RESPONSE(vehicle, true)
end
--- 获取最近的载具生成点
--- - - -
--- `nodeType`: 0 = main roads, 1 = any dry path, 3 = water
--- @param coords v3
--- @param nodeType integer
--- @return boolean, v3, float
function get_closest_vehicle_node(coords, nodeType)
local outCoords = v3.new()
local outHeading = memory.alloc(4)
if PATHFIND.GET_CLOSEST_VEHICLE_NODE_WITH_HEADING(coords.x, coords.y, coords.z,
memory.addrof(outCoords), outHeading, nodeType, 3.0, 0) then
return true, outCoords, memory.read_float(outHeading)
else
return false
end
end
--- 获取随机的载具生成点
--- @param coords v3
--- @param radius float
--- @return boolean, v3, integer
function get_random_vehicle_node(coords, radius)
local vecReturn = v3.new()
local NodeAddress = memory.alloc_int()
if PATHFIND.GET_RANDOM_VEHICLE_NODE(coords.x, coords.y, coords.z, radius,
0, false, false, memory.addrof(vecReturn), NodeAddress) then
return true, vecReturn, memory.read_int(NodeAddress)
else
return false
end
end
--- 获取载具内所有 Ped
---
--- 返回 ped num 和 ped table
--- @param vehicle Vehicle
--- @return integer, table<int, ped>
function get_vehicle_peds(vehicle)
local num = 0
local peds = {}
if ENTITY.IS_ENTITY_A_VEHICLE(vehicle) then
local seats = VEHICLE.GET_VEHICLE_MAX_NUMBER_OF_PASSENGERS(vehicle)
for i = -1, seats - 1 do
if not VEHICLE.IS_VEHICLE_SEAT_FREE(vehicle, i, false) then
local ped = VEHICLE.GET_PED_IN_VEHICLE_SEAT(vehicle, i)
num = num + 1
table.insert(peds, ped)
end
end
end
return num, peds
end
--- 通过载具 Hash 获取载具名称
--- @param modelHash Hash
--- @return string
function get_vehicle_display_name_by_hash(modelHash)
if not STREAMING.IS_MODEL_VALID(modelHash) then
return "NULL"
end
return util.get_label_text(VEHICLE.GET_DISPLAY_NAME_FROM_VEHICLE_MODEL(modelHash))
end
--- 获取载具名称
--- @param vehicle Vehicle
--- @return string
function get_vehicle_display_name(vehicle)
if not ENTITY.IS_ENTITY_A_VEHICLE(vehicle) then
return "NULL"
end
local hash = ENTITY.GET_ENTITY_MODEL(vehicle)
return util.get_label_text(VEHICLE.GET_DISPLAY_NAME_FROM_VEHICLE_MODEL(hash))
end
----------------------------------------
-- Ped Functions
----------------------------------------
--- 增强 NPC 作战能力
--- @param ped Ped
--- @param isGodmode boolean? [default = false]
--- @param canRagdoll boolean? [default = true]
function increase_ped_combat_ability(ped, isGodmode, canRagdoll)
if not ENTITY.IS_ENTITY_A_PED(ped) then
return
end
if isGodmode == nil then isGodmode = false end
if canRagdoll == nil then canRagdoll = true end
-- GODMODE
ENTITY.SET_ENTITY_INVINCIBLE(ped, isGodmode)
ENTITY.SET_ENTITY_PROOFS(ped, isGodmode, isGodmode, isGodmode, isGodmode, isGodmode, isGodmode, isGodmode,
isGodmode)
-- RAGDOLL
PED.SET_PED_CAN_RAGDOLL(ped, canRagdoll)
-- PERCEPTIVE
PED.SET_PED_HIGHLY_PERCEPTIVE(ped, true)
PED.SET_PED_VISUAL_FIELD_PERIPHERAL_RANGE(ped, 500.0)
PED.SET_PED_SEEING_RANGE(ped, 500.0)
PED.SET_PED_HEARING_RANGE(ped, 500.0)