/
natives_misc.cpp
2628 lines (2209 loc) · 74.5 KB
/
natives_misc.cpp
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
#include "precompiled.h"
/*
* Assign the number of the player's animation.
*
* @param index Client index
* @param playerAnim Specific animation number
*
* @noreturn
*
* native rg_set_animation(const index, PLAYER_ANIM:playerAnim);
*/
cell AMX_NATIVE_CALL rg_set_animation(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_anim };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
pPlayer->CSPlayer()->SetAnimation(CAmxArg(amx, params[arg_anim]));
return TRUE;
}
enum AccountSet { AS_SET, AS_ADD };
/*
* Adds money to player's account.
*
* @param index Client index
* @param amount The amount of money
* @param bTrackChange If bTrackChange is 1, the amount of money added will also be displayed.
*
* @noreturn
*
* native rg_add_account(const index, amount, AccountSet:typeSet = AS_ADD, const bool:bTrackChange = true);
*/
cell AMX_NATIVE_CALL rg_add_account(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_amount, arg_typeSet, arg_track_change };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
if (static_cast<AccountSet>(params[arg_typeSet]) == AS_SET) {
pPlayer->m_iAccount = 0;
}
pPlayer->CSPlayer()->AddAccount(params[arg_amount], RT_NONE, params[arg_track_change] != 0);
return TRUE;
}
/*
* Gives the player an item.
*
* @param index Client index
* @param pszName Item classname
* @param type Look at the enums with name GiveType
*
* @return Index of entity if successfull, -1 otherwise
*
* native rg_give_item(const index, const pszName[], GiveType:type = GT_APPEND);
*/
cell AMX_NATIVE_CALL rg_give_item(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_item, arg_type };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
char namebuf[256];
const char *itemName = getAmxString(amx, params[arg_item], namebuf);
GiveType type = static_cast<GiveType>(params[arg_type]);
if (type > GT_APPEND)
{
auto pInfo = g_ReGameApi->GetWeaponSlot(itemName);
if (pInfo)
{
pPlayer->ForEachItem(pInfo->slot, [pPlayer, type](CBasePlayerItem *pItem) {
RemoveOrDropItem(pPlayer, pItem, type);
return false;
});
}
}
auto pEntity = pPlayer->CSPlayer()->GiveNamedItemEx(itemName);
if (pEntity)
return indexOfPDataAmx(pEntity);
return AMX_NULLENT;
}
/*
* Gives the player an custom item, this means that don't handled API things.
*
* @example rg_give_custom_item(id, "weapon_c4"); doesn't sets the member m_bHasC4 to true, as the rg_give_item does.
*
* @param index Client index
* @param pszName Item classname
* @param type Look at the enums with name GiveType
* @param uid Sets a unique index for the entity
*
* @return Index of entity if successfull, -1 otherwise
*
* native rg_give_custom_item(const index, const pszName[], GiveType:type = GT_APPEND, const uid = 0);
*/
cell AMX_NATIVE_CALL rg_give_custom_item(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_item, arg_type, arg_uid };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
char namebuf[256];
const char *itemName = getAmxString(amx, params[arg_item], namebuf);
GiveType type = static_cast<GiveType>(params[arg_type]);
if (type > GT_APPEND)
{
auto pInfo = g_ReGameApi->GetWeaponSlot(itemName);
if (pInfo)
{
pPlayer->ForEachItem(pInfo->slot, [pPlayer, type](CBasePlayerItem *pItem) {
RemoveOrDropItem(pPlayer, pItem, type);
return false;
});
}
}
auto pEntity = GiveNamedItemInternal(amx, pPlayer, itemName, params[arg_uid]);
if (pEntity)
return indexOfPDataAmx(pEntity);
return AMX_NULLENT;
}
/*
* Give the default items to a player.
*
* @param index Client index
*
* @noreturn
*
* native rg_give_default_items(const index);
*/
cell AMX_NATIVE_CALL rg_give_default_items(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
pPlayer->CSPlayer()->GiveDefaultItems();
return TRUE;
}
/*
* Gives the player a shield
*
* @param index Client index
* @param bDeploy To deploy the shield
*
* @noreturn
*
* native rg_give_shield(const index, const bool:bDeploy = true);
*/
cell AMX_NATIVE_CALL rg_give_shield(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_deploy };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
pPlayer->CSPlayer()->GiveShield(params[arg_deploy] != 0);
return TRUE;
}
/*
* Inflicts damage in a radius from the source position.
*
* @param vecSrc The source position
* @param inflictor Inflictor is the entity that caused the damage (such as a gun)
* @param attacker Attacker is the entity that triggered the damage (such as the gun's owner)
* @param flDamage The amount of damage
* @param flRadius Damage radius
* @param iClassIgnore To specify classes that are immune to damage
* @param bitsDamageType Damage type DMG_*
*
* @noreturn
*
* native rg_dmg_radius(Float:vecSrc[3], const inflictor, const attacker, const Float:flDamage, const Float:flRadius, const iClassIgnore, const bitsDamageType);
*/
cell AMX_NATIVE_CALL rg_dmg_radius(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_vec, arg_inflictor, arg_attacker, arg_damage, arg_radius, arg_ignore_class, arg_dmg_type };
CHECK_ISENTITY(arg_inflictor);
CHECK_ISENTITY(arg_attacker);
CAmxArgs args(amx, params);
g_ReGameFuncs->RadiusDamage(args[arg_vec], args[arg_inflictor], args[arg_attacker], args[arg_damage], args[arg_radius], args[arg_ignore_class], args[arg_dmg_type]);
return TRUE;
}
/*
* Resets the global multi damage accumulator.
*
* @noreturn
*
* native rg_multidmg_clear();
*/
cell AMX_NATIVE_CALL rg_multidmg_clear(AMX *amx, cell *params)
{
g_ReGameFuncs->ClearMultiDamage();
return TRUE;
}
/*
* Inflicts contents of global multi damage registered on victim.
*
* @param inflictor Inflictor is the entity that caused the damage (such as a gun)
* @param attacker Attacker is the entity that triggered the damage (such as the gun's owner)
*
* @noreturn
*
* native rg_multidmg_apply(const inflictor, const attacker);
*/
cell AMX_NATIVE_CALL rg_multidmg_apply(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_inflictor, arg_attacker };
CHECK_ISENTITY(arg_inflictor);
CHECK_ISENTITY(arg_attacker);
CAmxArgs args(amx, params);
g_ReGameFuncs->ApplyMultiDamage(args[arg_inflictor], args[arg_attacker]);
return TRUE;
}
/*
* Adds damage to the accumulator.
*
* @param inflictor Inflictor is the entity that caused the damage (such as a gun)
* @param victim The victim that takes damage
* @param flDamage The amount of damage
* @param bitsDamageType Damage type DMG_*
*
* @noreturn
*
* native rg_multidmg_add(const inflictor, const victim, const Float:flDamage, const bitsDamageType);
*/
cell AMX_NATIVE_CALL rg_multidmg_add(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_inflictor, arg_victim, arg_damage, arg_dmg_type };
CHECK_ISENTITY(arg_inflictor);
CHECK_ISENTITY(arg_victim);
if (params[arg_victim] < 0) { // null
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: victim == null", __FUNCTION__);
return FALSE;
}
CAmxArgs args(amx, params);
g_ReGameFuncs->AddMultiDamage(args[arg_inflictor], args[arg_victim], args[arg_damage], args[arg_dmg_type]);
return TRUE;
}
/*
* Fires bullets from entity.
*
* @param inflictor Inflictor is the entity that caused the damage (such as a gun)
* @param attacker Attacker is the entity that tirggered the damage (such as the gun's owner)
* @param shots The number of shots
* @param vecSrc The source position of the barrel
* @param vecDirShooting Shooting direction
* @param vecSpread Spread
* @param flDistance Max shot distance
* @param iBulletType Bullet type, look at the enum with name Bullet in cssdk_const.inc
* @param iTracerFreq Tracer frequency
* @param iDamage Damage amount
*
* @noreturn
*
* native rg_fire_bullets(const inflictor, const attacker, const shots, Float:vecSrc[3], Float:vecDirShooting[3], Float:vecSpread[3], const Float:flDistance, const Bullet:iBulletType, const iTracerFreq, const iDamage);
*/
cell AMX_NATIVE_CALL rg_fire_bullets(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_inflictor, arg_attacker, arg_shots, arg_vecSrc, arg_dir, arg_spread, arg_dist, arg_bullet_type, arg_tracefrq, arg_dmg };
CHECK_ISENTITY(arg_inflictor);
CHECK_ISENTITY(arg_attacker);
CAmxArgs args(amx, params);
CBaseEntity *pInflictor = args[arg_inflictor];
pInflictor->m_pEntity->FireBullets
(
args[arg_shots],
args[arg_vecSrc],
args[arg_dir],
args[arg_spread],
args[arg_dist],
args[arg_bullet_type],
args[arg_tracefrq],
args[arg_dmg],
args[arg_attacker]
);
return TRUE;
}
/*
* Fires bullets from player's weapon.
*
* @param inflictor Inflictor is the entity that caused the damage (such as a gun)
* @param attacker Attacker is the entity that tirggered the damage (such as the gun's owner)
* @param vecSrc The source position of the barrel
* @param vecDirShooting Shooting direction
* @param vecSpread Spread
* @param flDistance Max shot distance
* @param iPenetration The amount of penetration
* @param iBulletType Bullet type, look at the enum with name Bullet in cssdk_const.inc
* @param iDamage Damage amount
* @param flRangeModifier Damage range modifier
* @param bPistol Pistol shot
* @param shared_rand Use player's random seed, get circular gaussian spread
*
* @return Float:[3] The spread result
*
* native Float:[3] rg_fire_bullets3(const inflictor, const attacker, Float:vecSrc[3], Float:vecDirShooting[3], const Float:vecSpread, const Float:flDistance, const iPenetration, const Bullet:iBulletType, const iDamage, const Float:flRangeModifier, const bool:bPistol, const shared_rand);
*/
cell AMX_NATIVE_CALL rg_fire_bullets3(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_inflictor, arg_attacker, arg_vecSrc, arg_dir, arg_spread, arg_dist, arg_penetration, arg_bullet_type, arg_dmg, arg_range_mod, arg_pistol, arg_rand, arg_out };
CHECK_ISENTITY(arg_inflictor);
CHECK_ISENTITY(arg_attacker);
CAmxArgs args(amx, params);
CBaseEntity *pInflictor = args[arg_inflictor];
entvars_t *pAttacker = args[arg_attacker];
args[arg_out].vector() = pInflictor->m_pEntity->FireBullets3
(
args[arg_vecSrc],
args[arg_dir],
args[arg_spread],
args[arg_dist],
args[arg_penetration],
args[arg_bullet_type],
args[arg_dmg],
args[arg_range_mod],
pAttacker, // icc fix
args[arg_pistol],
args[arg_rand]
);
return TRUE;
}
struct {
const char *msg;
const char *sentence;
size_t status;
} msg_sentence_list[] = {
{ "", "" }, // ROUND_NONE
{ "#Target_Bombed", "terwin", WINSTATUS_TERRORISTS }, // ROUND_TARGET_BOMB
{ "#VIP_Escaped", "ctwin", WINSTATUS_CTS }, // ROUND_VIP_ESCAPED
{ "#VIP_Assassinated", "terwin", WINSTATUS_TERRORISTS }, // ROUND_VIP_ASSASSINATED
{ "#Terrorists_Escaped", "terwin", WINSTATUS_TERRORISTS }, // ROUND_TERRORISTS_ESCAPED
{ "#CTs_PreventEscape", "ctwin", WINSTATUS_CTS }, // ROUND_CTS_PREVENT_ESCAPE
{ "#Escaping_Terrorists_Neutralized", "ctwin", WINSTATUS_CTS }, // ROUND_ESCAPING_TERRORISTS_NEUTRALIZED
{ "#Bomb_Defused", "ctwin", WINSTATUS_CTS }, // ROUND_BOMB_DEFUSED
{ "#CTs_Win", "ctwin", WINSTATUS_CTS }, // ROUND_CTS_WIN
{ "#Terrorists_Win", "terwin", WINSTATUS_TERRORISTS }, // ROUND_TERRORISTS_WIN
{ "#Round_Draw", "rounddraw", WINSTATUS_DRAW }, // ROUND_END_DRAW
{ "#All_Hostages_Rescued", "ctwin", WINSTATUS_CTS }, // ROUND_ALL_HOSTAGES_RESCUED
{ "#Target_Saved", "ctwin", WINSTATUS_CTS }, // ROUND_TARGET_SAVED
{ "#Hostages_Not_Rescued", "terwin", WINSTATUS_TERRORISTS }, // ROUND_HOSTAGE_NOT_RESCUED
{ "#Terrorists_Not_Escaped", "ctwin", WINSTATUS_CTS }, // ROUND_TERRORISTS_NOT_ESCAPED
{ "#VIP_Not_Escaped", "terwin", WINSTATUS_TERRORISTS }, // ROUND_VIP_NOT_ESCAPED
{ "#Game_Commencing", "", WINSTATUS_DRAW }, // ROUND_GAME_COMMENCE
{ "", "", WINSTATUS_DRAW }, // ROUND_GAME_RESTART
{ "#Cstrike_Tutor_Round_Over", "rounddraw", WINSTATUS_DRAW }, // ROUND_GAME_OVER
};
/*
* Forces the round to end.
*
* @param tmDelay Delay before the onset of a new round
* @param st Which team won
* @param event The event is the end of the round
* @param message The message on round end
* @param sentence The sound at the end of the round
* @param trigger This will trigger to all hooks on that function
* Be very careful about recursion!
*
* @noreturn
* native rg_round_end(const Float:tmDelay, const WinStatus:st, const ScenarioEventEndRound:event = ROUND_NONE, const message[] = "default", const sentence[] = "default", const bool:trigger = false);
*/
cell AMX_NATIVE_CALL rg_round_end(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_delay, arg_win, arg_event, arg_message, arg_sentence, arg_trigger };
CHECK_GAMERULES();
auto winstatus = params[arg_win];
if (winstatus <= 0) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: unknown win status %i", __FUNCTION__, winstatus);
return FALSE;
}
char sentencebuf[190], messagebuf[190];
const char *sentence = getAmxString(amx, params[arg_sentence], sentencebuf);
const char *message = getAmxString(amx, params[arg_message], messagebuf);
ScenarioEventEndRound event = static_cast<ScenarioEventEndRound>(params[arg_event]);
if (event != ROUND_NONE)
{
if (event < ROUND_NONE || event > ROUND_GAME_OVER) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid event %i, bounds(%i, %i)", __FUNCTION__, event, ROUND_NONE, ROUND_GAME_OVER);
return FALSE;
}
auto& lst = msg_sentence_list[event];
if (strcmp(sentence, "default") == 0)
sentence = lst.sentence;
if (strcmp(message, "default") == 0)
message = lst.msg;
}
if (sentence[0] != '\0')
{
Broadcast(sentence);
}
float tmDelay = CAmxArg(amx, params[arg_delay]);
if (params[arg_trigger] != 0)
{
return callForward<BOOL>(RG_RoundEnd,
[&message](int _winStatus, ScenarioEventEndRound _event, float _tmDelay)
{
CSGameRules()->EndRoundMessage(message, _event);
CSGameRules()->TerminateRound(_tmDelay, _winStatus);
return TRUE;
},
winstatus, event, tmDelay);
}
else
{
CSGameRules()->EndRoundMessage(message, event);
CSGameRules()->TerminateRound(tmDelay, winstatus);
}
return TRUE;
}
/*
* Updates current scores.
*
* @param iCtsWins The amount of wins for counter-terrorists
* @param iTsWins The amount of wins for terrorists
* @param bAdd Adds the score to the current amount
*
* @noreturn
* native rg_update_teamscores(const iCtsWins = 0, const iTsWins = 0, const bool:bAdd = true);
*/
cell AMX_NATIVE_CALL rg_update_teamscores(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_cts, arg_ts, arg_add };
CHECK_GAMERULES();
CSGameRules()->m_iNumCTWins = ((params[arg_add] != 0) ? CSGameRules()->m_iNumCTWins : 0) + params[arg_cts];
CSGameRules()->m_iNumTerroristWins = ((params[arg_add] != 0) ? CSGameRules()->m_iNumTerroristWins : 0) + params[arg_ts];
CSGameRules()->UpdateTeamScores();
return TRUE;
}
/*
* Creates an entity using Counter-Strike's custom CreateNamedEntity wrapper.
*
* @param classname Entity classname
* @param useHashTable Use this only for known game entities
*
* @note: Do not use this if you use a custom classname
*
* @return Index of the created entity or 0 otherwise
*
* native rg_create_entity(const classname[], const bool:useHashTable = false);
*/
cell AMX_NATIVE_CALL rg_create_entity(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_classname, arg_hashtable };
char classname[256];
string_t iClass = getAmxStringAlloc(amx, params[arg_classname], classname);
edict_t *pEntity;
if (params[arg_hashtable] != 0)
pEntity = g_ReGameFuncs->CREATE_NAMED_ENTITY2(iClass);
else
pEntity = CREATE_NAMED_ENTITY(iClass);
if (pEntity) {
return indexOfEdict(pEntity);
}
return 0;
}
/*
* Finds an entity in the world using Counter-Strike's custom FindEntityByString wrapper.
*
* @param start_index Entity index to start searching from. -1 to start from the first entity
* @param classname Classname to search for
* @param useHashTable Use this only for known game entities
*
* @note: Do not use this if you use a custom classname
*
* @return Entity index > 0 if found, 0 otherwise
*
* native rg_find_ent_by_class(start_index, const classname[], const bool:useHashTable = false);
*/
cell AMX_NATIVE_CALL rg_find_ent_by_class(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_start_index, arg_classname, arg_hashtable };
char classname[256];
const char* value = getAmxString(amx, params[arg_classname], classname);
if (params[arg_hashtable] != 0)
{
auto pStartEntity = getPrivate<CBaseEntity>(params[arg_start_index]);
auto pEntity = g_ReGameFuncs->UTIL_FindEntityByString(pStartEntity, "classname", value);
if (pEntity) {
return indexOfEdict(pEntity->pev);
}
return 0;
}
auto pStartEntity = edictByIndexAmx(params[arg_start_index]);
auto pEdict = FIND_ENTITY_BY_STRING(pStartEntity, "classname", value);
if (pEdict) {
return indexOfEdict(pEdict);
}
return 0;
}
/*
* Finds an entity in the world using Counter-Strike's custom FindEntityByString wrapper, matching by owner.
*
* @param start_index Entity index to start searching from. -1 to start from the first entity
* @param classname Classname to search for
*
* @return 1 if found, 0 otherwise
*
* native rg_find_ent_by_owner(&start_index, const classname[], owner);
*/
cell AMX_NATIVE_CALL rg_find_ent_by_owner(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_start_index, arg_classname, arg_onwer };
CHECK_ISENTITY(arg_onwer);
char classname[256];
cell& startIndex = *getAmxAddr(amx, params[arg_start_index]);
const char* value = getAmxString(amx, params[arg_classname], classname);
edict_t* pOwner = edictByIndexAmx(params[arg_onwer]);
edict_t* pEntity = g_pEdicts + startIndex;
for (int i = startIndex; i < gpGlobals->maxEntities; i++, pEntity++)
{
if (pEntity->v.owner != pOwner)
continue;
// yet not allocated
if (!pEntity->pvPrivateData || pEntity->free)
continue;
if (FClassnameIs(pEntity, value))
{
startIndex = i;
return TRUE;
}
}
return FALSE;
}
/*
* Finds the weapon by name in the player's inventory.
*
* @param index Client index
* @param weapon Weapon name
*
* @return Weapon's entity index, 0 otherwise
*
* native rg_find_weapon_bpack_by_name(const index, const weapon[]);
*/
cell AMX_NATIVE_CALL rg_find_weapon_bpack_by_name(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_weapon };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
char wname[256];
const char *pszWeaponName = getAmxString(amx, params[arg_weapon], wname);
auto pInfo = g_ReGameApi->GetWeaponSlot(pszWeaponName);
if (pInfo != nullptr)
{
auto pItem = pPlayer->m_rgpPlayerItems[ pInfo->slot ];
while (pItem)
{
if (FClassnameIs(pItem->pev, pszWeaponName)) {
return indexOfEdict(pItem->pev);
}
pItem = pItem->m_pNext;
}
}
return FALSE;
}
struct {
const char* pszItemName;
bool(*hasItem)(CBasePlayer* pl);
} itemInfoStruct[] = {
{ "item_thighpack", [](CBasePlayer* pl) -> bool { return pl->m_bHasDefuser; } },
{ "item_longjump", [](CBasePlayer* pl) -> bool { return pl->m_fLongJump == TRUE; } },
{ "item_assaultsuit", [](CBasePlayer* pl) -> bool { return pl->m_iKevlar == ARMOR_VESTHELM; } },
{ "item_kevlar", [](CBasePlayer* pl) -> bool { return pl->m_iKevlar == ARMOR_KEVLAR; } },
{ "weapon_shield", [](CBasePlayer* pl) -> bool { return pl->m_bOwnsShield; } },
};
/*
* Checks if the player has the item.
*
* @param index Client index
* @param item Item name
*
* @return true if he does, false otherwise
*
* native bool:rg_has_item_by_name(const index, const item[]);
*/
cell AMX_NATIVE_CALL rg_has_item_by_name(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_item };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
char iname[256];
const char *pszItemName = getAmxString(amx, params[arg_item], iname);
// item_* and weapon_shield
for (auto& inf : itemInfoStruct) {
if (FStrEq(inf.pszItemName, pszItemName)) {
return (cell)inf.hasItem(pPlayer);
}
}
// weapon_*
auto pInfo = g_ReGameApi->GetWeaponSlot(pszItemName);
if (pInfo)
{
auto pItem = pPlayer->m_rgpPlayerItems[ pInfo->slot ];
while (pItem)
{
if (FClassnameIs(pItem->pev, pszItemName)) {
return TRUE;
}
pItem = pItem->m_pNext;
}
}
return FALSE;
}
/*
* Returns specific information about the weapon.
*
* @param weapon name or id Weapon id, see WEAPON_* constants, WeaponIdType or weapon_* name
* @param WpnInfo:type Info type, see WI_* constants
*
* @return Weapon information
* @error If weapon_id or type are out of bounds, an error will be thrown
*
* native any:rg_get_weapon_info(any:...);
*/
cell AMX_NATIVE_CALL rg_get_weapon_info(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_weapon_id, arg_type, arg_3, arg_4 };
WeaponIdType weaponId = static_cast<WeaponIdType>(*getAmxAddr(amx, params[arg_weapon_id]));
WpnInfo info_type = static_cast<WpnInfo>(*getAmxAddr(amx, params[arg_type]));
if (!GetWeaponInfoRange(weaponId, false) && info_type != WI_ID)
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid weapon id %i", __FUNCTION__, weaponId);
return 0;
}
char wname[256];
const char* szWeaponName = getAmxString(amx, params[arg_weapon_id], wname);
WeaponInfoStruct* info = g_ReGameApi->GetWeaponInfo(weaponId);
switch (info_type)
{
case WI_ID:
if (szWeaponName != nullptr) {
auto infoName = g_ReGameApi->GetWeaponInfo(szWeaponName);
if (infoName != nullptr) {
return infoName->id;
}
}
return WEAPON_NONE;
case WI_COST:
return info->cost;
case WI_CLIP_COST:
return info->clipCost;
case WI_BUY_CLIP_SIZE:
return info->buyClipSize;
case WI_GUN_CLIP_SIZE:
return info->gunClipSize;
case WI_MAX_ROUNDS:
return info->maxRounds;
case WI_AMMO_TYPE:
return info->ammoType;
case WI_AMMO_NAME:
{
if (PARAMS_COUNT != arg_4) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: bad parameter count, got %i, expected %i", __FUNCTION__, PARAMS_COUNT, arg_4);
return -1;
}
// native rg_get_weapon_info(id, WI_AMMO_NAME, output[], maxlength);
cell* dest = getAmxAddr(amx, params[arg_3]);
size_t length = *getAmxAddr(amx, params[arg_4]);
if (info->ammoName1 == nullptr) {
setAmxString(dest, "", 1);
return 0;
}
setAmxString(dest, info->ammoName1, length);
return 1;
}
case WI_NAME:
{
if (PARAMS_COUNT != arg_4) {
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: bad parameter count, got %i, expected %i", __FUNCTION__, PARAMS_COUNT, arg_4);
return -1;
}
// native rg_get_weapon_info(id, WI_NAME, output[], maxlength);
cell* dest = getAmxAddr(amx, params[arg_3]);
size_t length = *getAmxAddr(amx, params[arg_4]);
if (info->entityName == nullptr) {
setAmxString(dest, "", 1);
return 0;
}
setAmxString(dest, info->entityName, length);
return 1;
}
default:
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: unknown type statement %i, params count %i", __FUNCTION__, info_type, PARAMS_COUNT);
return -1;
}
}
/*
* Sets specific weapon info values.
*
* @param weapon_id Weapon id, see WEAPON_* constants
* @param type Info type, see WI_* constants
*
* @return 1 on success, 0 otherwise
*
* native rg_set_weapon_info(const {WeaponIdType,_}:weapon_id, WpnInfo:type, any:...);
*/
cell AMX_NATIVE_CALL rg_set_weapon_info(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_weapon_id, arg_type, arg_value };
WeaponIdType weaponId = static_cast<WeaponIdType>(params[arg_weapon_id]);
if (!GetWeaponInfoRange(weaponId, true))
{
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: invalid weapon id %i", __FUNCTION__, weaponId);
return 0;
}
cell* value = getAmxAddr(amx, params[arg_value]);
WeaponInfoStruct *info = g_ReGameApi->GetWeaponInfo(weaponId);
WpnInfo info_type = static_cast<WpnInfo>(params[arg_type]);
switch (info_type)
{
case WI_COST:
info->cost = *value;
break;
case WI_CLIP_COST:
info->clipCost = *value;
break;
case WI_BUY_CLIP_SIZE:
info->buyClipSize = *value;
break;
case WI_GUN_CLIP_SIZE:
info->gunClipSize= *value;
break;
case WI_MAX_ROUNDS:
info->maxRounds = *value;
break;
case WI_AMMO_TYPE:
info->ammoType = *value;
break;
case WI_AMMO_NAME:
case WI_NAME:
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: this change will have no effect, type statement %i", __FUNCTION__, info_type);
return 0;
default:
AMXX_LogError(amx, AMX_ERR_NATIVE, "%s: unknown type statement %i, params count %i", __FUNCTION__, info_type, PARAMS_COUNT);
return 0;
}
return 1;
}
/*
* Remove all the player's stuff in a specific slot.
*
* @param index Client index
* @param slot The slot that will be emptied
*
* @return 1 on success, 0 otherwise
*
* native rg_remove_items_by_slot(const index, const InventorySlotType:slot);
*/
cell AMX_NATIVE_CALL rg_remove_items_by_slot(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_slot };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
pPlayer->ForEachItem(params[arg_slot], [pPlayer](CBasePlayerItem *pItem)
{
if (pItem->IsWeapon()) {
if (pItem == pPlayer->m_pActiveItem) {
((CBasePlayerWeapon *)pItem)->RetireWeapon();
}
pPlayer->m_rgAmmo[ pItem->PrimaryAmmoIndex() ] = 0;
}
if (pPlayer->RemovePlayerItem(pItem)) {
pPlayer->pev->weapons &= ~(1 << pItem->m_iId);
pItem->Kill();
}
return false;
});
if (!pPlayer->m_rgpPlayerItems[PRIMARY_WEAPON_SLOT]) {
pPlayer->m_bHasPrimary = false;
}
return TRUE;
}
/*
* Drop to floor all the player's stuff by specific slot.
*
* @param index Client index
* @param slot Specific slot for remove of each item.
*
* @return 1 on success, 0 otherwise
*
* native rg_drop_items_by_slot(const index, const InventorySlotType:slot);
*/
cell AMX_NATIVE_CALL rg_drop_items_by_slot(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_slot };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
pPlayer->ForEachItem(params[arg_slot], [pPlayer](CBasePlayerItem *pItem) {
pPlayer->CSPlayer()->DropPlayerItem(STRING(pItem->pev->classname));
return false;
});
return TRUE;
}
/*
* Remove all of the player's items.
*
* @param index Client index
* @param removeSuit Remove suit
*
* @return 1 on success, 0 otherwise
*
* native rg_remove_all_items(const index, const bool:removeSuit = false);
*/
cell AMX_NATIVE_CALL rg_remove_all_items(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_suit };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
pPlayer->CSPlayer()->RemoveAllItems(params[arg_suit] != 0);
return TRUE;
}
/*
* Forces the player to drop the specified item classname.
*
* @param index Client index
* @param item_name Item classname
*
* @return 1 on success, 0 otherwise
*
* native rg_drop_item(const index, const item_name[]);
*/
cell AMX_NATIVE_CALL rg_drop_item(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_item_name };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
CHECK_CONNECTED(pPlayer, arg_index);
char item[256];
pPlayer->CSPlayer()->DropPlayerItem(getAmxString(amx, params[arg_item_name], item));
return TRUE;
}
/*
* Executes a client command on the gamedll side.
*
* @param index Client index
* @param command Client command to execute
* @param arg Optional command arguments
*
* @return 1 on success, 0 otherwise
*
* native rg_internal_cmd(const index, const cmd[], const arg[] = "");
*/
cell AMX_NATIVE_CALL rg_internal_cmd(AMX *amx, cell *params)
{
enum args_e { arg_count, arg_index, arg_cmd, arg_arg };
CHECK_ISPLAYER(arg_index);
CBasePlayer *pPlayer = UTIL_PlayerByIndex(params[arg_index]);
if (unlikely(pPlayer == nullptr || pPlayer->has_disconnected)) {
return FALSE;
}
char cmd[256], arg[256];
pPlayer->CSPlayer()->ClientCommand(getAmxString(amx, params[arg_cmd], cmd), getAmxString(amx, params[arg_arg], arg));