-
Notifications
You must be signed in to change notification settings - Fork 4
/
FCNPC-AI.inc
2008 lines (1853 loc) · 80.5 KB
/
FCNPC-AI.inc
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
#if defined _FCNPC_AI_included
#endinput
#endif
#define _FCNPC_AI_included
#include <a_samp>
#include <FCNPC>
#include <util>
// ========================================FAKE NATIVES========================================
/*
// NPCs
native bool:FAI_IsValidNPC(npcid);
native bool:FAI_IsValidNPCForPlayer(npcid, playerid);
native FCNPC_SetHealth(npcid, Float:health, Float:max = -1.0);
native Float:FCNPC_GetHealth(npcid, &Float:max = 100.0);
native FCNPC_SetTarget(npcid, playerid);
native FCNPC_GetTarget(npcid);
native FCNPC_SetGoToInfo(npcid, type = FCNPC_MOVE_TYPE_AUTO, Float:speed = FCNPC_MOVE_SPEED_AUTO, mode = FCNPC_MOVE_MODE_AUTO, pathfinding = FCNPC_MOVE_PATHFINDING_AUTO, Float:radius = 0.0, bool:set_angle = true, Float:min_distance = 0.0, Float:dist_check = 1.5, stop_delay = 250, playerid = -1);
native FCNPC_GetGoToInfo(npcid, playerid, &type, &Float:speed, &mode, &pathfinding, &Float:radius, &bool:set_angle, &Float:min_distance, &Float:dist_check, &stop_delay);
native FCNPC_SetAimAtInfo(npcid, Float:range = 20.0, bool:shoot = true, shoot_delay = -1, bool:set_angle = true, Float:offset_x = 0.0, Float:offset_y = 0.0, Float:offset_z = 0.0, Float:offset_from_x = 0.0, Float:offset_from_y = 0.0, Float:offset_from_z = 0.0, between_check_mode = FCNPC_ENTITY_MODE_AUTO, between_check_flags = FCNPC_ENTITY_CHECK_ALL, playerid = -1);
native FCNPC_GetAimAtInfo(npcid, playerid, &Float:range, &bool:shoot, &shoot_delay, &bool:set_angle, &Float:offset_x, &Float:offset_y, &Float:offset_z, &Float:offset_from_x, &Float:offset_from_y, &Float:offset_from_z, &between_check_mode, &between_check_flags);
native FCNPC_SetMeleeAttackInfo(npcid, Float:range = 0.5, delay = -1, bool:fighting_style = false, playerid = -1);
native FCNPC_GetMeleeAttackInfo(npcid, playerid, &Float:range, &delay, &bool:fighting_style);
native FCNPC_SetOutOfAmmoMeleeWeapon(npcid, weaponid, playerid = -1);
native FCNPC_GetOutOfAmmoMeleeWeapon(npcid, playerid);
native FCNPC_SetBehaviour(npcid, FCNPC_E_BEHAVIOUR:behaviour, playerid = -1);
native FCNPC_E_BEHAVIOUR:FCNPC_GetBehaviour(npcid, playerid);
native FCNPC_SetAggroViewingAngle(npcid, Float:angle, Float:vangle = 180.0, playerid = -1);
native Float:FCNPC_GetAggroViewingAngle(npcid, playerid, &Float:vangle = 180.0);
native FCNPC_UseAggroLineOfSight(npcid, bool:use = true, playerid = -1);
native bool:FCNPC_IsAggroLineOfSightUsed(npcid, playerid);
native FCNPC_SetAggroSightRange(npcid, Float:range, playerid = -1);
native Float:FCNPC_GetAggroSightRange(npcid, playerid);
native FCNPC_SetAggroHearingPercentage(npcid, Float:percentage, playerid = -1);
native Float:FCNPC_GetAggroHearingPercentage(npcid, playerid);
native FCNPC_SetLeashRange(npcid, Float:range, playerid = -1);
native Float:FCNPC_GetLeashRange(npcid, playerid);
native FCNPC_SetThreat(npcid, Float:threat, playerid = -1);
native Float:FCNPC_GetThreat(npcid, playerid);
native FCNPC_SetNormalHearingSoundRng(npcid, FCNPC_E_SOUND_RANGE:type, Float:normalHearingSoundRange, playerid = -1);
native Float:FCNPC_GetNormalHearingSoundRng(npcid, FCNPC_E_SOUND_RANGE:type, playerid);
native FCNPC_TriggerSound(Float:x, Float:y, Float:z, Float:normalHearingSoundRange, npcid = -1, playerid = INVALID_PLAYER_ID);
// Debug
native FAI_ToggleRangeDebugging(npcid, playerid, FAI_E_DEBUG_RANGE:ranges);
native FAI_E_DEBUG_RANGE:FAI_GetRangeDebugging(npcid);
native FAI_ToggleSoundRangeDebugging(npcid, playerid, FAI_E_DEBUG_SOUND_RANGE:ranges);
native FAI_E_DEBUG_SOUND_RANGE:FAI_GetSoundRangeDebugging(npcid);
native FAI_ToggleAngleDebugging(npcid, playerid, FAI_E_DEBUG_ANGLE:angles);
native FAI_E_DEBUG_ANGLE:FAI_GetAngleDebugging(npcid);
*/
// ========================================CALLBACKS========================================
// NPCs
forward FCNPC_OnEncounterStart(npcid, firsttargetid, FCNPC_E_START_REASON:reason);
forward FCNPC_OnEncounterStop(npcid, lasttargetid, FCNPC_E_STOP_REASON:reason);
forward FCNPC_OnPlayerGetAggro(playerid, npcid);
forward FCNPC_OnPlayerLoseAggro(playerid, npcid);
forward FCNCP_OnPlayerThreatChange(playerid, npcid, Float:newthreat, Float:oldthreat, FCNPC_E_THREAT_REASON:reason);
forward FCNPC_OnHearSound(npcid, issuerid, Float:x, Float:y, Float:z, Float:normalHearingSoundRange, FCNPC_E_SOUND_REASON:reason);
// ========================================CONSTANTS========================================
// General
#define FCNPC_AI_VERSION "2.0.0"
// NPCs
enum FCNPC_E_BEHAVIOUR {
FCNPC_BEHAVIOUR_FRIENDLY,
FCNPC_BEHAVIOUR_NEUTRAL,
FCNPC_BEHAVIOUR_UNFRIENDLY
}
enum FCNPC_E_START_REASON {
FCNPC_START_REASON_DAMAGE,
FCNPC_START_REASON_SIGHT,
FCNPC_START_REASON_SOUND,
FCNPC_START_REASON_MAN_TAR_SET
}
enum FCNPC_E_STOP_REASON {
FCNPC_STOP_REASON_NPC_DEATH,
FCNPC_STOP_REASON_NPC_RESPAWN,
FCNPC_STOP_REASON_NPC_DESTROY,
FCNPC_STOP_REASON_NO_VAL_TAR,
FCNPC_STOP_REASON_MAN_TAR_SET
}
enum FCNPC_E_THREAT_REASON {
FCNPC_THREAT_REASON_RESET,
FCNPC_THREAT_REASON_DAMAGE,
FCNPC_THREAT_REASON_SOUND,
FCNPC_THREAT_REASON_MAN_SET
}
enum FCNPC_E_SOUND_REASON {
FCNPC_SOUND_REASON_MOVEMENT,
FCNPC_SOUND_REASON_WEAPON_SHOT,
FCNPC_SOUND_REASON_BUL_IMPACT,
FCNPC_SOUND_REASON_MAN_TRIGGER
}
enum FCNPC_E_SOUND_RANGE {
FCNPC_SOUND_RANGE_DUCK,
FCNPC_SOUND_RANGE_WALK,
FCNPC_SOUND_RANGE_RUN,
FCNPC_SOUND_RANGE_SPRINT,
FCNPC_SOUND_RANGE_VEHICLE,
FCNPC_SOUND_RANGE_WEAPON,
FCNPC_SOUND_RANGE_BUL_IMPACT
}
// Debug
enum FAI_E_DEBUG_RANGE (<<=1) {
FAI_DEBUG_RANGE_ALL = -1,
FAI_DEBUG_RANGE_NONE = 0,
FAI_DEBUG_AIM_AT_RANGE = 1,
FAI_DEBUG_MELEE_ATTACK_RANGE,
FAI_DEBUG_AGGRO_SIGHT_RANGE,
FAI_DEBUG_LEASH_RANGE
}
enum FAI_E_DEBUG_SOUND_RANGE (<<=1) {
FAI_DEBUG_SOUND_RANGE_ALL = -1,
FAI_DEBUG_SOUND_RANGE_NONE = 0,
FAI_DEBUG_DUCK_SOUND_RANGE = 1,
FAI_DEBUG_WALK_SOUND_RANGE,
FAI_DEBUG_RUN_SOUND_RANGE,
FAI_DEBUG_SPRINT_SOUND_RANGE,
FAI_DEBUG_VEHICLE_SOUND_RANGE,
FAI_DEBUG_WEAPON_SOUND_RANGE,
FAI_DEBUG_BUL_IMPCT_SOUND_RANGE
}
enum FAI_E_DEBUG_ANGLE (<<=1) {
FAI_DEBUG_ANGLE_ALL = -1,
FAI_DEBUG_ANGLE_NONE = 0,
FAI_DEBUG_FACING_ANGLE = 1,
FAI_DEBUG_VIEWING_ANGLE
}
// ========================================VARIABLES========================================
// Players
static FAI_PauseTickCount[MAX_PLAYERS] = {0, ...};
static bool:FAI_PlayerPaused[MAX_PLAYERS] = {false, ...};
static FAI_PauseTimer = INVALID_TIMER_ID;
// NPCs
enum FAI_E_NPC {
// Can't be set by the user
bool:FAI_NPC_VALID,
// Can be set by the user
Float:FAI_NPC_HEALTH, // Can be changed automatically by the script
Float:FAI_NPC_MAX_HEALTH,
FAI_NPC_TARGET, // Can be changed automatically by the script
FAI_NPC_GO_TO_TYPE[MAX_PLAYERS],
Float:FAI_NPC_GO_TO_SPEED[MAX_PLAYERS],
FAI_NPC_GO_TO_MODE[MAX_PLAYERS],
FAI_NPC_GO_TO_PATHFINDING[MAX_PLAYERS],
Float:FAI_NPC_GO_TO_RADIUS[MAX_PLAYERS],
bool:FAI_NPC_GO_TO_SET_ANGLE[MAX_PLAYERS],
Float:FAI_NPC_GO_TO_MIN_DISTANCE[MAX_PLAYERS],
Float:FAI_NPC_GO_TO_DIST_CHECK[MAX_PLAYERS],
FAI_NPC_GO_TO_STOP_DELAY[MAX_PLAYERS],
Float:FAI_NPC_AIM_AT_RANGE[MAX_PLAYERS],
bool:FAI_NPC_AIM_AT_SHOOT[MAX_PLAYERS],
FAI_NPC_AIM_AT_SHOOT_DELAY[MAX_PLAYERS],
bool:FAI_NPC_AIM_AT_SET_ANGLE[MAX_PLAYERS],
Float:FAI_NPC_AIM_AT_OFFSET_X[MAX_PLAYERS],
Float:FAI_NPC_AIM_AT_OFFSET_Y[MAX_PLAYERS],
Float:FAI_NPC_AIM_AT_OFFSET_Z[MAX_PLAYERS],
Float:FAI_NPC_AIM_AT_OFFSET_FROM_X[MAX_PLAYERS],
Float:FAI_NPC_AIM_AT_OFFSET_FROM_Y[MAX_PLAYERS],
Float:FAI_NPC_AIM_AT_OFFSET_FROM_Z[MAX_PLAYERS],
FAI_NPC_AIM_AT_BTWN_CHECK_MODE[MAX_PLAYERS],
FAI_NPC_AIM_AT_BTWN_CHECK_FLAGS[MAX_PLAYERS],
Float:FAI_NPC_MELEE_ATTACK_RANGE[MAX_PLAYERS],
FAI_NPC_MELEE_ATTACK_DELAY[MAX_PLAYERS],
bool:FAI_NPC_MELEE_ATTACK_FIGHT_STL[MAX_PLAYERS],
FAI_NPC_OUT_OF_AMMO_MELEE_WEAP[MAX_PLAYERS],
FCNPC_E_BEHAVIOUR:FAI_NPC_BEHAVIOUR[MAX_PLAYERS],
Float:FAI_NPC_AGGRO_HOR_VIEWING_ANGLE[MAX_PLAYERS],
Float:FAI_NPC_AGGRO_VER_VIEWING_ANGLE[MAX_PLAYERS],
bool:FAI_NPC_AGGRO_LINE_OF_SIGHT[MAX_PLAYERS],
Float:FAI_NPC_AGGRO_SIGHT_RANGE[MAX_PLAYERS],
Float:FAI_NPC_AGGRO_HEARING_PERCENT[MAX_PLAYERS],
Float:FAI_NPC_LEASH_RANGE[MAX_PLAYERS],
Float:FAI_NPC_THREAT[MAX_PLAYERS], // Can be changed automatically by the script
Float:FAI_NPC_INVAL_HEARING_PERCENT,
Float:FAI_NPC_SOUND_RANGE[MAX_PLAYERS * _:FCNPC_E_SOUND_RANGE] // Calculate the offset manually, since the compiler doesn't allow for another array dimension
}
static FAI_NPCs[MAX_PLAYERS][FAI_E_NPC];
// Debug
static FAI_E_DEBUG_RANGE:FAI_DebugRanges[MAX_PLAYERS] = {FAI_DEBUG_RANGE_NONE, ...};
static FAI_DebugRangesPlayerID[MAX_PLAYERS] = INVALID_PLAYER_ID;
static FAI_E_DEBUG_SOUND_RANGE:FAI_DebugSoundRanges[MAX_PLAYERS] = {FAI_DEBUG_SOUND_RANGE_NONE, ...};
static FAI_DebugSoundRangesPlayerID[MAX_PLAYERS] = INVALID_PLAYER_ID;
static FAI_E_DEBUG_ANGLE:FAI_DebugAngles[MAX_PLAYERS] = {FAI_DEBUG_ANGLE_NONE, ...};
static FAI_DebugAnglesPlayerID[MAX_PLAYERS] = INVALID_PLAYER_ID;
// ========================================INTERNAL FUNCTIONS========================================
// Silence warning 208
forward bool:FAI_IsPlayerInLeashRange(playerid, npcid);
forward bool:FAI_IsValidNPC(npcid);
forward bool:FAI_IsValidNPCForPlayer(npcid, playerid);
// General
static FAI_ScriptInit() {
// Players
for(new playerid = 0; playerid < MAX_PLAYERS; playerid++) {
FAI_PauseTickCount[playerid] = GetTickCount();
FAI_PlayerPaused[playerid] = false;
}
FAI_PauseTimer = SetTimer("FAI_CheckPlayersPausedState", 1000, true);
// NPCs
for(new npcid = 0; npcid < MAX_PLAYERS; npcid++) {
FAI_ResetNPC(npcid);
}
}
static FAI_ScriptExit() {
// Players
for(new playerid = 0; playerid < MAX_PLAYERS; playerid++) {
FAI_PauseTickCount[playerid] = 0;
FAI_PlayerPaused[playerid] = false;
}
KillTimer(FAI_PauseTimer);
FAI_PauseTimer = INVALID_TIMER_ID;
// NPCs
// Automatically destroy NPCs on script exit
for(new npcid = 0; npcid < MAX_PLAYERS; npcid++) {
if(FAI_IsValidNPC(npcid)) {
FCNPC_Destroy(npcid);
}
}
}
// Players
forward FAI_CheckPlayersPausedState();
public FAI_CheckPlayersPausedState() {
for(new playerid = 0, highestPlayerid = GetPlayerPoolSize(); playerid <= highestPlayerid; playerid++) {
new playerState = GetPlayerState(playerid);
// OnPlayerUpdate isn't called when the player is initializing, dead or in class selection
if(IsPlayerConnected(playerid) && !IsPlayerNPC(playerid) && playerState != PLAYER_STATE_WASTED && playerState != PLAYER_STATE_NONE) {
new bool:playerInGame = true;
if(GetTickCount() - FAI_PauseTickCount[playerid] > 2000) {
playerInGame = false;
}
if(!playerInGame && !FAI_PlayerPaused[playerid]) {
FAI_PlayerPaused[playerid] = true;
} else if(playerInGame && FAI_PlayerPaused[playerid]) {
FAI_PlayerPaused[playerid] = false;
}
}
}
}
// NPCs
static FAI_ResetNPC(npcid) {
// Don't use FAI_IsValidNPC
FAI_NPCs[npcid][FAI_NPC_VALID] = false;
FAI_NPCs[npcid][FAI_NPC_HEALTH] = 100.0;
FAI_NPCs[npcid][FAI_NPC_MAX_HEALTH] = 100.0;
FAI_NPCs[npcid][FAI_NPC_TARGET] = INVALID_PLAYER_ID;
for(new playerid = 0; playerid < MAX_PLAYERS; playerid++) {
FAI_NPCs[npcid][FAI_NPC_GO_TO_TYPE][playerid] = FCNPC_MOVE_TYPE_AUTO;
FAI_NPCs[npcid][FAI_NPC_GO_TO_SPEED][playerid] = FCNPC_MOVE_SPEED_AUTO;
FAI_NPCs[npcid][FAI_NPC_GO_TO_MODE][playerid] = FCNPC_MOVE_MODE_AUTO;
FAI_NPCs[npcid][FAI_NPC_GO_TO_PATHFINDING][playerid] = FCNPC_MOVE_PATHFINDING_AUTO;
FAI_NPCs[npcid][FAI_NPC_GO_TO_RADIUS][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_GO_TO_SET_ANGLE][playerid] = true;
FAI_NPCs[npcid][FAI_NPC_GO_TO_MIN_DISTANCE][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_GO_TO_DIST_CHECK][playerid] = 1.5;
FAI_NPCs[npcid][FAI_NPC_GO_TO_STOP_DELAY][playerid] = 250;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_RANGE][playerid] = 20.0;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_SHOOT][playerid] = true;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_SHOOT_DELAY][playerid] = -1;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_SET_ANGLE][playerid] = true;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_X][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_Y][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_Z][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_FROM_X][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_FROM_Y][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_FROM_Z][playerid] = 0.0;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_BTWN_CHECK_MODE][playerid] = FCNPC_ENTITY_MODE_AUTO;
FAI_NPCs[npcid][FAI_NPC_AIM_AT_BTWN_CHECK_FLAGS][playerid] = FCNPC_ENTITY_CHECK_ALL;
FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_RANGE][playerid] = 0.5;
FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_DELAY][playerid] = -1;
FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_FIGHT_STL][playerid] = false;
FAI_NPCs[npcid][FAI_NPC_OUT_OF_AMMO_MELEE_WEAP][playerid] = WEAPON_FISTS;
FAI_NPCs[npcid][FAI_NPC_BEHAVIOUR][playerid] = FCNPC_BEHAVIOUR_FRIENDLY;
FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][playerid] = 360.0;
FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][playerid] = 180.0;
FAI_NPCs[npcid][FAI_NPC_AGGRO_LINE_OF_SIGHT][playerid] = true;
FAI_NPCs[npcid][FAI_NPC_AGGRO_SIGHT_RANGE][playerid] = 20.0;
FAI_NPCs[npcid][FAI_NPC_AGGRO_HEARING_PERCENT][playerid] = 1.0;
FAI_NPCs[npcid][FAI_NPC_LEASH_RANGE][playerid] = 200.0; // The default SA-MP stream_distance
FAI_ChangeThreat(npcid, playerid, 0.0, FCNPC_THREAT_REASON_RESET);
FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][playerid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_DUCK] = 1.0;
FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][playerid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WALK] = 2.0;
FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][playerid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_RUN] = 5.0;
FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][playerid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_SPRINT] = 8.0;
FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][playerid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_VEHICLE] = 20.0;
FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][playerid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WEAPON] = 40.0;
FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][playerid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_BUL_IMPACT] = 5.0;
}
FAI_NPCs[npcid][FAI_NPC_INVAL_HEARING_PERCENT] = 1.0;
// Debug
FAI_DebugRanges[npcid] = FAI_DEBUG_RANGE_NONE;
FAI_DebugRangesPlayerID[npcid] = INVALID_PLAYER_ID;
FAI_DebugSoundRanges[npcid] = FAI_DEBUG_SOUND_RANGE_NONE;
FAI_DebugSoundRangesPlayerID[npcid] = INVALID_PLAYER_ID;
FAI_DebugAngles[npcid] = FAI_DEBUG_ANGLE_NONE;
FAI_DebugAnglesPlayerID[npcid] = INVALID_PLAYER_ID;
}
static bool:FAI_HasNPCMeleeWeapon(npcid) {
new weaponid = FCNPC_GetWeapon(npcid);
return IsMeleeWeapon(weaponid);
}
static FAI_UpdateNPCAttackMove(npcid) {
if(!FCNPC_IsDead(npcid) && FAI_IsValidNPCForPlayer(npcid, FAI_NPCs[npcid][FAI_NPC_TARGET])) {
new Float:x, Float:y, Float:z;
FCNPC_GetPosition(npcid, x, y, z);
new Float:attackRange = FAI_NPCs[npcid][FAI_NPC_AIM_AT_RANGE][FAI_NPCs[npcid][FAI_NPC_TARGET]];
if(FAI_HasNPCMeleeWeapon(npcid) || (FCNPC_GetAmmo(npcid) == 0 && !FCNPC_IsInfiniteAmmoUsed(npcid))) {
attackRange = FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_RANGE][FAI_NPCs[npcid][FAI_NPC_TARGET]];
}
new bool:canMove = true;
new bool:canAttack = true;
// Target in attack range, attack if allowed, otherwise move if allowed, otherwise stop attacking
if(attackRange < 0.0 || GetPlayerDistanceFromPoint(FAI_NPCs[npcid][FAI_NPC_TARGET], x, y, z) <= attackRange) {
if(canAttack) {
FAI_ChangeNPCAttack(npcid);
} else {
if(canMove) {
FAI_ChangeNPCMove(npcid);
} else {
FAI_ChangeNPCStopAttackMove(npcid);
}
}
}
// Target not in attack range, move to target if allowed, otherwise attack if allowed, otherwise stop attacking
else {
if(canMove) {
FAI_ChangeNPCMove(npcid);
} else {
if(canAttack) {
FAI_ChangeNPCAttack(npcid);
} else {
FAI_ChangeNPCStopAttackMove(npcid);
}
}
}
}
}
static FAI_ChangeNPCAttack(npcid) {
if(FCNPC_IsMoving(npcid)) {
FCNPC_Stop(npcid);
}
if(!FAI_HasNPCMeleeWeapon(npcid) && (FCNPC_GetAmmo(npcid) != 0 || FCNPC_IsInfiniteAmmoUsed(npcid))) {
// In case the npc switched weapons
if(FCNPC_IsAttacking(npcid)) {
FCNPC_StopAttack(npcid);
}
// So we don't aim again when we were already aiming at that same player. If aiming at another player, this will execute
if(!FCNPC_IsAimingAtPlayer(npcid, FAI_NPCs[npcid][FAI_NPC_TARGET]) || (FCNPC_IsShooting(npcid) && !FAI_NPCs[npcid][FAI_NPC_AIM_AT_SHOOT]) || (!FCNPC_IsShooting(npcid) && FAI_NPCs[npcid][FAI_NPC_AIM_AT_SHOOT])) {
FCNPC_AimAtPlayer(npcid, FAI_NPCs[npcid][FAI_NPC_TARGET], FAI_NPCs[npcid][FAI_NPC_AIM_AT_SHOOT][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_SHOOT_DELAY][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_SET_ANGLE][FAI_NPCs[npcid][FAI_NPC_TARGET]],
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_X][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_Y][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_Z][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_FROM_X][FAI_NPCs[npcid][FAI_NPC_TARGET]],
FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_FROM_Y][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_OFFSET_FROM_Z][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_BTWN_CHECK_MODE][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_AIM_AT_BTWN_CHECK_FLAGS][FAI_NPCs[npcid][FAI_NPC_TARGET]]);
}
} else {
// In case the npc switched weapons
if(FCNPC_IsAiming(npcid)) {
FCNPC_StopAim(npcid);
}
// So we don't attack again when we were already attacking
if(!FCNPC_IsAttacking(npcid)) {
FCNPC_MeleeAttack(npcid, FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_DELAY][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_FIGHT_STL][FAI_NPCs[npcid][FAI_NPC_TARGET]]);
}
}
}
static FAI_ChangeNPCMove(npcid) {
if(FCNPC_IsAiming(npcid)) {
FCNPC_StopAim(npcid);
}
if(FCNPC_IsAttacking(npcid)) {
FCNPC_StopAttack(npcid);
}
// In case the npc is out of ammo
if(!FAI_HasNPCMeleeWeapon(npcid) && FCNPC_GetAmmo(npcid) == 0 && !FCNPC_IsInfiniteAmmoUsed(npcid)) {
FCNPC_SetWeapon(npcid, FAI_NPCs[npcid][FAI_NPC_OUT_OF_AMMO_MELEE_WEAP][FAI_NPCs[npcid][FAI_NPC_TARGET]]);
}
// So we don't move again when we were already moving to that same player. If moving at another player, this will execute
if(!FCNPC_IsMovingAtPlayer(npcid, FAI_NPCs[npcid][FAI_NPC_TARGET])) {
FCNPC_GoToPlayer(npcid, FAI_NPCs[npcid][FAI_NPC_TARGET], FAI_NPCs[npcid][FAI_NPC_GO_TO_TYPE][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_GO_TO_SPEED][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_GO_TO_MODE][FAI_NPCs[npcid][FAI_NPC_TARGET]],
FAI_NPCs[npcid][FAI_NPC_GO_TO_PATHFINDING][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_GO_TO_RADIUS][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_GO_TO_SET_ANGLE][FAI_NPCs[npcid][FAI_NPC_TARGET]],
FAI_NPCs[npcid][FAI_NPC_GO_TO_MIN_DISTANCE][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_GO_TO_DIST_CHECK][FAI_NPCs[npcid][FAI_NPC_TARGET]], FAI_NPCs[npcid][FAI_NPC_GO_TO_STOP_DELAY][FAI_NPCs[npcid][FAI_NPC_TARGET]]);
}
}
static FAI_ChangeNPCStopAttackMove(npcid) {
if(FCNPC_IsMoving(npcid)) {
FCNPC_Stop(npcid);
}
if(FCNPC_IsAiming(npcid)) {
FCNPC_StopAim(npcid);
}
if(FCNPC_IsAttacking(npcid)) {
FCNPC_StopAttack(npcid);
}
}
static FAI_UpdateNPCTarget(npcid) {
if(!FCNPC_IsDead(npcid)) {
new newtargetid = INVALID_PLAYER_ID;
// When the encounter has been started, search for an overaggroing player
if(FAI_NPCs[npcid][FAI_NPC_TARGET] != INVALID_PLAYER_ID) {
new highestplayerid = FAI_GetHighestThreatPlayer(npcid);
// For another player to become the target during the encounter, he must exceed 130% of the current valid target's threat (also accounted for divide by 0), or the current target must be invalid, or the behaviour for the current target is friendly
if(highestplayerid != INVALID_PLAYER_ID && (FAI_NPCs[npcid][FAI_NPC_THREAT][FAI_NPCs[npcid][FAI_NPC_TARGET]] == 0.0 || FAI_NPCs[npcid][FAI_NPC_THREAT][highestplayerid] / FAI_NPCs[npcid][FAI_NPC_THREAT][FAI_NPCs[npcid][FAI_NPC_TARGET]] > 1.3
|| !FAI_IsValidNPCForPlayer(npcid, FAI_NPCs[npcid][FAI_NPC_TARGET]) || FAI_NPCs[npcid][FAI_NPC_BEHAVIOUR][FAI_NPCs[npcid][FAI_NPC_TARGET]] == FCNPC_BEHAVIOUR_FRIENDLY || !FAI_IsPlayerInLeashRange(FAI_NPCs[npcid][FAI_NPC_TARGET], npcid))) {
// Set target to player with highest threat
newtargetid = highestplayerid;
FAI_ChangeNPCTarget(npcid, newtargetid, FCNPC_START_REASON_SIGHT);
}
}
// When the encounter hasn't been started, or there is no overaggroing player
if(newtargetid == INVALID_PLAYER_ID) {
// When there is no target, or if the current target is invalid, or if the behaviour for the current target is friendly
if(!FAI_IsValidNPCForPlayer(npcid, FAI_NPCs[npcid][FAI_NPC_TARGET]) || FAI_NPCs[npcid][FAI_NPC_BEHAVIOUR][FAI_NPCs[npcid][FAI_NPC_TARGET]] == FCNPC_BEHAVIOUR_FRIENDLY || !FAI_IsPlayerInLeashRange(FAI_NPCs[npcid][FAI_NPC_TARGET], npcid)) {
// When the encounter hasn't been started
if(FAI_NPCs[npcid][FAI_NPC_TARGET] == INVALID_PLAYER_ID) {
// Set target to closest player in viewing angle and line of sight
newtargetid = FAI_ClosestPlayerInAggroSghtRng(npcid, true, true);
}
// When the encounter has been started
else {
// Set target to closest player
newtargetid = FAI_ClosestPlayerInAggroSghtRng(npcid);
}
// Don't do anything when the target doesn't change
if(FAI_NPCs[npcid][FAI_NPC_TARGET] != newtargetid) {
if(newtargetid == INVALID_PLAYER_ID) {
FAI_ChangeNPCTarget(npcid, newtargetid, FCNPC_STOP_REASON_NO_VAL_TAR);
} else {
FAI_ChangeNPCTarget(npcid, newtargetid, FCNPC_START_REASON_SIGHT);
}
}
}
}
}
}
static FAI_ChangeNPCTarget(npcid, newtargetid, {FCNPC_E_START_REASON,FCNPC_E_STOP_REASON}:reason) {
if(newtargetid == INVALID_PLAYER_ID || (!FCNPC_IsDead(npcid) && FAI_IsValidNPCForPlayer(npcid, newtargetid) && FAI_IsPlayerInLeashRange(newtargetid, npcid))) {
new oldtargetid = FAI_NPCs[npcid][FAI_NPC_TARGET];
if(oldtargetid != newtargetid) {
if(oldtargetid != INVALID_PLAYER_ID) {
CallLocalFunction("FCNPC_OnPlayerLoseAggro", "dd", oldtargetid, npcid);
}
FAI_NPCs[npcid][FAI_NPC_TARGET] = newtargetid;
if(newtargetid != INVALID_PLAYER_ID) {
CallLocalFunction("FCNPC_OnPlayerGetAggro", "dd", newtargetid, npcid);
FAI_UpdateNPCAttackMove(npcid);
if(oldtargetid == INVALID_PLAYER_ID) {
CallLocalFunction("FCNPC_OnEncounterStart", "ddd", npcid, newtargetid, _:reason);
}
} else {
if(oldtargetid != INVALID_PLAYER_ID) {
for(new playerid = 0; playerid < MAX_PLAYERS; playerid++) {
FAI_ChangeThreat(npcid, playerid, 0.0, FCNPC_THREAT_REASON_RESET);
}
FAI_ChangeNPCStopAttackMove(npcid);
CallLocalFunction("FCNPC_OnEncounterStop", "ddd", npcid, oldtargetid, _:reason);
}
}
return 1;
}
}
return 0;
}
static FAI_ChangeThreat(npcid, playerid, Float:threat, FCNPC_E_THREAT_REASON:reason) {
// The actual threat change happens in the callback, to allow the user to override the decision to persist it
new Float:oldThreat = FAI_NPCs[npcid][FAI_NPC_THREAT][playerid];
if(threat != oldThreat) {
CallLocalFunction("FCNCP_OnPlayerThreatChange", "ddffd", playerid, npcid, threat, oldThreat, _:reason);
}
}
static bool:FAI_IsPlayerInAggroViewingAngle(playerid, npcid) {
// Get NPC position and special action
new Float:xn, Float:yn, Float:zn, npcAction;
FCNPC_GetPosition(npcid, xn, yn, zn);
npcAction = FCNPC_GetSpecialAction(npcid);
// Get player position and special action
new Float:xp, Float:yp, Float:zp, playerAction;
if(!IsPlayerNPC(playerid)) {
GetPlayerPos(playerid, xp, yp, zp);
playerAction = GetPlayerSpecialAction(playerid);
} else {
FCNPC_GetPosition(playerid, xp, yp, zp);
playerAction = FCNPC_GetSpecialAction(playerid);
}
// Adjust height position of the NPC so that the viewing angles will be calculated from the face instead of the groin
if(npcAction == SPECIAL_ACTION_DUCK) {
zn -= 0.1;
} else {
zn += 0.7;
}
// Define which vertical parts of the player to check
new Float:zp_feet = zp - 1.0;
new Float:zp_head = zp + 0.8;
if(playerAction == SPECIAL_ACTION_DUCK) {
zp_head = zp;
}
// ===== HORIZONTAL VIEWING ANGLE =====
// Get the NPC horizontal facing angle adjusted for the weird GTA angle system
new Float:npcHorizontalFacingAngle = FCNPC_GetAngle(npcid) + 90.0;
// Calculate the angle between the NPC and the player in the horizontal plane
new Float:horizontalAngleBetweenPoints = atan2(yp - yn, xp - xn);
// Calculate the smallest difference between these 2 angles as a value between -180.0 and 180.0
new Float:horizontalAngleDifference = horizontalAngleBetweenPoints - npcHorizontalFacingAngle;
if(horizontalAngleDifference > 180.0) {
horizontalAngleDifference -= 360.0;
}
if(horizontalAngleDifference < -180.0) {
horizontalAngleDifference += 360.0;
}
// Get the absolute value of this angle
horizontalAngleDifference = floatabs(horizontalAngleDifference);
// Check if the player is within the horizontal aggro viewing angle
if(horizontalAngleDifference > FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][playerid]/2) {
return false;
}
// ===== VERTICAL VIEWING ANGLE =====
// Calculate the Euclidean distance in the horizontal plane
new Float:horizontalDistance = VectorSize(xp - xn, yp - yn, 0.0);
// Check if any vertical part of the player is within the vertical aggro viewing range, using decrements of one third the height of the player and starting from the most likely match which is the head
for(new Float:zp_part = zp_head, Float:zp_diff = (zp_head - zp_feet)/3; zp_part >= zp_feet; zp_part -= zp_diff) {
// Calculate the angle between the NPC and the player in the vertical plane as a value between -90.0 and 90.0
// The game only has the ability to rotate the NPC along the vertical axis, so the NPC vertical facing angle is always 0.0, so we don't need to subtract it from the calculated angle
new Float:verticalAngleBetweenPoints = atan2(zp_part - zn, horizontalDistance);
// Get the absolute value of this angle
verticalAngleBetweenPoints = floatabs(verticalAngleBetweenPoints);
// Check if the player is within the vertical aggro viewing angle
if(verticalAngleBetweenPoints <= FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][playerid]/2) {
return true;
}
}
return false;
}
static bool:FAI_IsPlayerInAggroLineOfSight(playerid, npcid) {
// Get NPC position and special action
new Float:xn, Float:yn, Float:zn, npcAction;
FCNPC_GetPosition(npcid, xn, yn, zn);
npcAction = FCNPC_GetSpecialAction(npcid);
// Get player position and special action
new Float:xp, Float:yp, Float:zp, playerAction;
if(!IsPlayerNPC(playerid)) {
GetPlayerPos(playerid, xp, yp, zp);
playerAction = GetPlayerSpecialAction(playerid);
} else {
FCNPC_GetPosition(playerid, xp, yp, zp);
playerAction = FCNPC_GetSpecialAction(playerid);
}
// Adjust height position of the NPC so that the ray will be cast from the shoulder instead of the groin
new Float:offset_from_z = 0.7;
if(npcAction == SPECIAL_ACTION_DUCK) {
offset_from_z -= 0.1;
}
// Define which vertical parts of the player to check
new Float:zp_feet = zp - 1.0;
new Float:zp_head = zp + 0.8;
if(playerAction == SPECIAL_ACTION_DUCK) {
zp_head = zp;
}
// Check if a ray cast between the NPC and any vertical part of the player does not collide with anything, using decrements of one third the height of the player and starting from the most likely match which is the head
for(new Float:zp_part = zp_head, Float:zp_diff = (zp_head - zp_feet)/3; zp_part >= zp_feet; zp_part -= zp_diff) {
new Float:range = GetPlayerDistanceFromPoint(npcid, xp, yp, zp_part);
new entity_id;
FCNPC_GetClosestEntityInBetween(npcid, xp, yp, zp_part, range, FCNPC_ENTITY_MODE_COLANDREAS, FCNPC_ENTITY_CHECK_ALL, 0.0, 0.0, offset_from_z, entity_id);
if(entity_id == 0xFFFF) {
return true;
}
}
return false;
}
static bool:FAI_IsPlayerInAggroSightRange(playerid, npcid) {
new Float:x, Float:y, Float:z;
if(!IsPlayerNPC(playerid)) {
GetPlayerPos(playerid, x, y, z);
} else {
FCNPC_GetPosition(playerid, x, y, z);
}
new Float:npcRange = GetPlayerDistanceFromPoint(npcid, x, y, z);
if(FAI_NPCs[npcid][FAI_NPC_AGGRO_SIGHT_RANGE][playerid] < 0.0 || npcRange <= FAI_NPCs[npcid][FAI_NPC_AGGRO_SIGHT_RANGE][playerid]) {
return true;
}
return false;
}
static bool:FAI_IsSoundHeard(playerid, npcid, Float:x, Float:y, Float:z, Float:normalHearingSoundRange) {
new Float:hearingPercent = FAI_NPCs[npcid][FAI_NPC_INVAL_HEARING_PERCENT];
if(playerid != INVALID_PLAYER_ID) {
hearingPercent = FAI_NPCs[npcid][FAI_NPC_AGGRO_HEARING_PERCENT][playerid];
}
// When the NPC is deaf, or the sound is silent
if(hearingPercent == 0 || normalHearingSoundRange == 0) {
return false;
}
// When the NPC has unlimited hearing, or the sound is unlimitedly loud
if(hearingPercent < 0 || normalHearingSoundRange < 0) {
return true;
}
// Non-extreme cases
new Float:npcNormalHearingSoundRange = normalHearingSoundRange * hearingPercent;
new Float:npcRange = GetPlayerDistanceFromPoint(npcid, x, y, z);
if(npcNormalHearingSoundRange >= npcRange) {
return true;
}
return false;
}
static bool:FAI_IsPlayerInLeashRange(playerid, npcid) {
new Float:x, Float:y, Float:z;
if(!IsPlayerNPC(playerid)) {
GetPlayerPos(playerid, x, y, z);
} else {
FCNPC_GetPosition(playerid, x, y, z);
}
new Float:npcRange = GetPlayerDistanceFromPoint(npcid, x, y, z);
if(FAI_NPCs[npcid][FAI_NPC_LEASH_RANGE][playerid] < 0.0 || npcRange <= FAI_NPCs[npcid][FAI_NPC_LEASH_RANGE][playerid]) {
return true;
}
return false;
}
static FAI_ClosestPlayerInAggroSghtRng(npcid, bool:checkViewingAngle = false, bool:checkLineOfSight = false) {
new closestPlayer = INVALID_PLAYER_ID;
new Float:closestPlayerRange = 0.0;
for(new playerid = 0, highestPlayerid = GetPlayerPoolSize(); playerid <= highestPlayerid; playerid++) {
// Only set target if the encounter has already started OR if the behaviour is unfriendly for that player
// Don't check for death, because won't be called when dead
if(FAI_IsValidNPCForPlayer(npcid, playerid) && ((FAI_NPCs[npcid][FAI_NPC_TARGET] != INVALID_PLAYER_ID && FAI_NPCs[npcid][FAI_NPC_BEHAVIOUR][playerid] != FCNPC_BEHAVIOUR_FRIENDLY)
|| (FAI_NPCs[npcid][FAI_NPC_TARGET] == INVALID_PLAYER_ID && FAI_NPCs[npcid][FAI_NPC_BEHAVIOUR][playerid] == FCNPC_BEHAVIOUR_UNFRIENDLY))) {
new Float:x, Float:y, Float:z;
if(!IsPlayerNPC(playerid)) {
GetPlayerPos(playerid, x, y, z);
} else {
FCNPC_GetPosition(playerid, x, y, z);
}
new Float:npcRange = GetPlayerDistanceFromPoint(npcid, x, y, z);
if(FAI_IsPlayerInAggroSightRange(playerid, npcid) && (!checkViewingAngle || FAI_IsPlayerInAggroViewingAngle(playerid, npcid)) && (!checkLineOfSight || !FAI_NPCs[npcid][FAI_NPC_AGGRO_LINE_OF_SIGHT][playerid] || FAI_IsPlayerInAggroLineOfSight(playerid, npcid)) && (closestPlayer == INVALID_PLAYER_ID || npcRange < closestPlayerRange)) {
closestPlayerRange = npcRange;
closestPlayer = playerid;
}
}
}
return closestPlayer;
}
static FAI_GetHighestThreatPlayer(npcid) {
new highestPlayer = INVALID_PLAYER_ID;
new Float:highestPlayerThreat = 0.0;
for(new playerid = 0, highestPlayerid = GetPlayerPoolSize(); playerid <= highestPlayerid; playerid++) {
// Only set target if the player has any threat AND the behaviour is not friendly for that player
// Don't check for death, because won't be called when dead
if(FAI_IsValidNPCForPlayer(npcid, playerid) && FAI_IsPlayerInLeashRange(playerid, npcid) && FAI_NPCs[npcid][FAI_NPC_THREAT][playerid] > 0.0 && FAI_NPCs[npcid][FAI_NPC_BEHAVIOUR][playerid] != FCNPC_BEHAVIOUR_FRIENDLY) {
// When there are multiple players with the same threat, the first ID will be used
if(highestPlayer == INVALID_PLAYER_ID || FAI_NPCs[npcid][FAI_NPC_THREAT][playerid] > highestPlayerThreat) {
highestPlayerThreat = FAI_NPCs[npcid][FAI_NPC_THREAT][playerid];
highestPlayer = playerid;
}
}
}
return highestPlayer;
}
static FAI_OnWeaponShot(npcid, shooterid, hittype, hitid, Float:fX, Float:fY, Float:fZ) {
new Float:x, Float:y, Float:z;
if(!IsPlayerNPC(shooterid)) {
GetPlayerPos(shooterid, x, y, z);
} else {
FCNPC_GetPosition(shooterid, x, y, z);
}
// If both the firing and the impact are heard, the firing will take precedence
if(FAI_IsSoundHeard(shooterid, npcid, x, y, z, FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][shooterid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WEAPON])) {
CallLocalFunction("FCNPC_OnHearSound", "ddffffd", npcid, shooterid, x, y, z, FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][shooterid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WEAPON], _:FCNPC_SOUND_REASON_WEAPON_SHOT);
} else {
switch(hittype) {
case BULLET_HIT_TYPE_NONE: {
// Don't act when nothing is hit, includes actors
if(fX == 0.0 && fY == 0.0 && fZ == 0.0) {
return;
}
x = 0.0; y = 0.0; z = 0.0;
}
case BULLET_HIT_TYPE_PLAYER: {
// NPC hits are already handled by the damage callback
if(hitid == npcid) {
return;
}
if(!IsPlayerNPC(hitid)) {
GetPlayerPos(hitid, x, y, z);
} else {
FCNPC_GetPosition(hitid, x, y, z);
}
}
case BULLET_HIT_TYPE_VEHICLE: {
GetVehiclePos(hitid, x, y, z);
}
case BULLET_HIT_TYPE_OBJECT: {
GetObjectPos(hitid, x, y, z);
}
case BULLET_HIT_TYPE_PLAYER_OBJECT: {
// We don't know which owner this object belongs to
// GetPlayerObjectPos(ownerid, hitid, x, y, z);
return;
}
}
if(FAI_IsSoundHeard(INVALID_PLAYER_ID, npcid, x + fX, y + fY, z + fZ, FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][shooterid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_BUL_IMPACT])) {
CallLocalFunction("FCNPC_OnHearSound", "ddffffd", npcid, INVALID_PLAYER_ID, x + fX, y + fY, z + fZ, FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][shooterid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_BUL_IMPACT], _:FCNPC_SOUND_REASON_BUL_IMPACT);
}
}
}
static FAI_OnMovement(npcid, moverid) {
new Float:x, Float:y, Float:z, Float:normalHearingSoundRange;
new keys, updown, leftright;
if(!IsPlayerNPC(moverid)) {
GetPlayerKeys(moverid, keys, updown, leftright);
} else {
FCNPC_GetKeys(moverid, updown, leftright, keys);
}
// On foot
if(!IsPlayerInAnyVehicle(moverid)) {
// Don't act when there is no movement
if(updown == 0 && leftright == 0) {
return;
}
new moverAction, moverWeapon;
if(!IsPlayerNPC(moverid)) {
GetPlayerPos(moverid, x, y, z);
moverAction = GetPlayerSpecialAction(moverid);
moverWeapon = GetPlayerWeapon(moverid);
} else {
FCNPC_GetPosition(moverid, x, y, z);
moverAction = FCNPC_GetSpecialAction(moverid);
moverWeapon = FCNPC_GetWeapon(moverid);
}
if(moverAction == SPECIAL_ACTION_DUCK) {
normalHearingSoundRange = FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][moverid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_DUCK];
} else {
// When both the walk and sprint button are pressed, sprinting takes precedence when that weapon allows sprinting
// Sprinting
if(IsWeaponSprintAllowed(moverWeapon) && (keys & KEY_SPRINT) == KEY_SPRINT) {
normalHearingSoundRange = FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][moverid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_SPRINT];
}
// Walking
else if((keys & KEY_WALK) == KEY_WALK) {
normalHearingSoundRange = FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][moverid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WALK];
}
// Running
else {
normalHearingSoundRange = FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][moverid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_RUN];
}
}
}
// In vehicle
else {
// Don't act when there is no movement
if(updown == 0 && leftright == 0 && (keys & KEY_UP) != KEY_UP && (keys & KEY_DOWN) != KEY_DOWN) {
return;
}
if(!IsPlayerNPC(moverid)) {
GetVehiclePos(GetPlayerVehicleID(moverid), x, y, z);
} else {
GetVehiclePos(FCNPC_GetVehicleID(moverid), x, y, z);
}
normalHearingSoundRange = FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][moverid * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_VEHICLE];
}
if(FAI_IsSoundHeard(moverid, npcid, x, y, z, normalHearingSoundRange)) {
CallLocalFunction("FCNPC_OnHearSound", "ddffffd", npcid, moverid, x, y, z, normalHearingSoundRange, _:FCNPC_SOUND_REASON_MOVEMENT);
}
}
// Debug
static FAI_ToggleDebugRangeObjects(npcid) {
new Float:iScale = 1.0, Float:radius = 50.0; // Radius of modelid is 50.0
if(FAI_DebugRanges[npcid] & FAI_DEBUG_AIM_AT_RANGE && FAI_NPCs[npcid][FAI_NPC_AIM_AT_RANGE][FAI_DebugRangesPlayerID[npcid]] > 0.0) { // Yellow
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_AIM_AT_RANGE][FAI_DebugRangesPlayerID[npcid]];
SetPlayerAttachedObject(npcid, 0, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFFFFFF00);
} else {
RemovePlayerAttachedObject(npcid, 0);
}
if(FAI_DebugRanges[npcid] & FAI_DEBUG_MELEE_ATTACK_RANGE && FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_RANGE][FAI_DebugRangesPlayerID[npcid]] > 0.0) { // Purple
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_MELEE_ATTACK_RANGE][FAI_DebugRangesPlayerID[npcid]];
SetPlayerAttachedObject(npcid, 1, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFF800080);
} else {
RemovePlayerAttachedObject(npcid, 1);
}
if(FAI_DebugRanges[npcid] & FAI_DEBUG_AGGRO_SIGHT_RANGE && FAI_NPCs[npcid][FAI_NPC_AGGRO_SIGHT_RANGE][FAI_DebugRangesPlayerID[npcid]] > 0.0) { // Red
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_AGGRO_SIGHT_RANGE][FAI_DebugRangesPlayerID[npcid]];
SetPlayerAttachedObject(npcid, 2, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFFFF0000);
} else {
RemovePlayerAttachedObject(npcid, 2);
}
if(FAI_DebugRanges[npcid] & FAI_DEBUG_LEASH_RANGE && FAI_NPCs[npcid][FAI_NPC_LEASH_RANGE][FAI_DebugRangesPlayerID[npcid]] > 0.0) { // Green
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_LEASH_RANGE][FAI_DebugRangesPlayerID[npcid]];
SetPlayerAttachedObject(npcid, 3, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFF00FF00);
} else {
RemovePlayerAttachedObject(npcid, 3);
}
}
static FAI_ToggleDebugSoundRngObjects(npcid) {
new Float:iScale = 1.0, Float:radius = 50.0; // Radius of modelid is 50.0
if(FAI_DebugSoundRanges[npcid] & FAI_DEBUG_DUCK_SOUND_RANGE && FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_DUCK] > 0.0) { // Yellow
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_DUCK];
SetPlayerAttachedObject(npcid, 0, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFFFFFF00);
} else {
RemovePlayerAttachedObject(npcid, 0);
}
if(FAI_DebugSoundRanges[npcid] & FAI_DEBUG_WALK_SOUND_RANGE && FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WALK] > 0.0) { // Purple
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WALK];
SetPlayerAttachedObject(npcid, 1, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFF800080);
} else {
RemovePlayerAttachedObject(npcid, 1);
}
if(FAI_DebugSoundRanges[npcid] & FAI_DEBUG_RUN_SOUND_RANGE && FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_RUN] > 0.0) { // Red
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_RUN];
SetPlayerAttachedObject(npcid, 2, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFFFF0000);
} else {
RemovePlayerAttachedObject(npcid, 2);
}
if(FAI_DebugSoundRanges[npcid] & FAI_DEBUG_SPRINT_SOUND_RANGE && FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_SPRINT] > 0.0) { // Green
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_SPRINT];
SetPlayerAttachedObject(npcid, 3, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFF00FF00);
} else {
RemovePlayerAttachedObject(npcid, 3);
}
if(FAI_DebugSoundRanges[npcid] & FAI_DEBUG_VEHICLE_SOUND_RANGE && FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_VEHICLE] > 0.0) { // Blue
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_VEHICLE];
SetPlayerAttachedObject(npcid, 4, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFF0000FF);
} else {
RemovePlayerAttachedObject(npcid, 4);
}
if(FAI_DebugSoundRanges[npcid] & FAI_DEBUG_WEAPON_SOUND_RANGE && FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WEAPON] > 0.0) { // White
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_WEAPON];
SetPlayerAttachedObject(npcid, 5, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFFFFFFFF);
} else {
RemovePlayerAttachedObject(npcid, 5);
}
if(FAI_DebugSoundRanges[npcid] & FAI_DEBUG_BUL_IMPCT_SOUND_RANGE && FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_BUL_IMPACT] > 0.0) { // Cyan
new Float:scale = iScale / radius * FAI_NPCs[npcid][FAI_NPC_SOUND_RANGE][FAI_DebugSoundRangesPlayerID[npcid] * _:FCNPC_E_SOUND_RANGE + _:FCNPC_SOUND_RANGE_BUL_IMPACT];
SetPlayerAttachedObject(npcid, 6, 18843, 1, .fRotY = 90.0, .fScaleX = scale, .fScaleY = scale, .fScaleZ = scale, .materialcolor1 = 0xFF00FFFF);
} else {
RemovePlayerAttachedObject(npcid, 6);
}
}
static FAI_ToggleDebugAngleObjects(npcid) {
if(FAI_DebugAngles[npcid] & FAI_DEBUG_FACING_ANGLE) { // Red
SetPlayerAttachedObject(npcid, 0, 18643, 1, .fOffsetX = 0.5, .fRotZ = 90.0);
} else {
RemovePlayerAttachedObject(npcid, 0);
}
if(FAI_DebugAngles[npcid] & FAI_DEBUG_VIEWING_ANGLE) { // Green & blue
SetPlayerAttachedObject(npcid, 1, 19083, 1, .fOffsetX = 0.5, .fRotX = FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2, .fRotZ = 90.0 - FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2);
SetPlayerAttachedObject(npcid, 2, 19083, 1, .fOffsetX = 0.5, .fRotX = -FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2, .fRotZ = 90.0 - FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2);
SetPlayerAttachedObject(npcid, 3, 19083, 1, .fOffsetX = 0.5, .fRotX = FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2, .fRotZ = 90.0 + FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2);
SetPlayerAttachedObject(npcid, 4, 19083, 1, .fOffsetX = 0.5, .fRotX = -FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2, .fRotZ = 90.0 + FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2);
SetPlayerAttachedObject(npcid, 5, 19080, 1, .fOffsetX = 0.5, .fRotX = FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2, .fRotZ = 90.0);
SetPlayerAttachedObject(npcid, 6, 19080, 1, .fOffsetX = 0.5, .fRotX = -FAI_NPCs[npcid][FAI_NPC_AGGRO_HOR_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2, .fRotZ = 90.0);
SetPlayerAttachedObject(npcid, 7, 19080, 1, .fOffsetX = 0.5, .fRotZ = 90.0 - FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2);
SetPlayerAttachedObject(npcid, 8, 19080, 1, .fOffsetX = 0.5, .fRotZ = 90.0 + FAI_NPCs[npcid][FAI_NPC_AGGRO_VER_VIEWING_ANGLE][FAI_DebugAnglesPlayerID[npcid]]/2);
} else {
RemovePlayerAttachedObject(npcid, 1);
RemovePlayerAttachedObject(npcid, 2);
RemovePlayerAttachedObject(npcid, 3);
RemovePlayerAttachedObject(npcid, 4);
RemovePlayerAttachedObject(npcid, 5);
RemovePlayerAttachedObject(npcid, 6);
RemovePlayerAttachedObject(npcid, 7);
RemovePlayerAttachedObject(npcid, 8);
}
}
// ========================================HOOKED CALLBACKS========================================
#if defined FILTERSCRIPT
public OnFilterScriptInit()
{
FAI_ScriptInit();
new ret = 1;
#if defined FAI_OnFilterScriptInit
ret = FAI_OnFilterScriptInit();
#endif
return ret;
}
#if defined _ALS_OnFilterScriptInit
#undef OnFilterScriptInit
#else
#define _ALS_OnFilterScriptInit
#endif
#define OnFilterScriptInit FAI_OnFilterScriptInit
#if defined FAI_OnFilterScriptInit
forward FAI_OnFilterScriptInit();
#endif
public OnFilterScriptExit()
{
new ret = 1;
#if defined FAI_OnFilterScriptExit
ret = FAI_OnFilterScriptExit();
#endif
FAI_ScriptExit(); // Call after
return ret;
}
#if defined _ALS_OnFilterScriptExit
#undef OnFilterScriptExit
#else
#define _ALS_OnFilterScriptExit
#endif
#define OnFilterScriptExit FAI_OnFilterScriptExit
#if defined FAI_OnFilterScriptExit
forward FAI_OnFilterScriptExit();
#endif
#else
public OnGameModeInit()
{
FAI_ScriptInit();
new ret = 1;
#if defined FAI_OnGameModeInit
ret = FAI_OnGameModeInit();
#endif
return ret;
}
#if defined _ALS_OnGameModeInit
#undef OnGameModeInit
#else
#define _ALS_OnGameModeInit
#endif
#define OnGameModeInit FAI_OnGameModeInit
#if defined FAI_OnGameModeInit
forward FAI_OnGameModeInit();
#endif
public OnGameModeExit()
{
new ret = 1;
#if defined FAI_OnGameModeExit
ret = FAI_OnGameModeExit();
#endif
FAI_ScriptExit(); // Call after
return ret;
}
#if defined _ALS_OnGameModeExit
#undef OnGameModeExit
#else
#define _ALS_OnGameModeExit
#endif
#define OnGameModeExit FAI_OnGameModeExit
#if defined FAI_OnGameModeExit
forward FAI_OnGameModeExit();
#endif
#endif