forked from Fundynamic/RealBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
1098 lines (935 loc) · 32.5 KB
/
util.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
/***
*
* Copyright (c) 1999, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
/**
* RealBot : Artificial Intelligence
* Version : Work In Progress
* Author : Stefan Hendriks
* Url : http://realbot.bots-united.com
**
* DISCLAIMER
*
* History, Information & Credits:
* RealBot is based partially upon the HPB-Bot Template #3 by Botman
* Thanks to Ditlew (NNBot), Pierre Marie Baty (RACCBOT), Tub (RB AI PR1/2/3)
* Greg Slocum & Shivan (RB V1.0), Botman (HPB-Bot) and Aspirin (JOEBOT). And
* everybody else who helped me with this project.
* Storage of Visibility Table using BITS by Cheesemonster.
*
* Some portions of code are from other bots, special thanks (and credits) go
* to (in no specific order):
*
* Pierre Marie Baty
* Count-Floyd
*
* !! BOTS-UNITED FOREVER !!
*
* This project is open-source, it is protected under the GPL license;
* By using this source-code you agree that you will ALWAYS release the
* source-code with your project.
*
**/
#include <string.h>
#include <extdll.h>
#include <dllapi.h>
#include <meta_api.h>
#include <entity_state.h>
#include "bot.h"
#include "bot_weapons.h"
#include "bot_func.h"
#include "game.h"
#include "ChatEngine.h"
#include <ctype.h>
#ifndef _WIN32
#define _snprintf snprintf
#endif
extern int mod_id;
extern cBot bots[32];
extern cGame Game;
extern cChatEngine ChatEngine;
int gmsgTextMsg = 0;
int gmsgSayText = 0;
int gmsgShowMenu = 0;
Vector UTIL_VecToAngles(const Vector &vec) {
float rgflVecOut[3];
VEC_TO_ANGLES(vec, rgflVecOut);
return Vector(rgflVecOut);
}
// Overloaded to add IGNORE_GLASS
void
UTIL_TraceLine(const Vector &vecStart, const Vector &vecEnd,
IGNORE_MONSTERS igmon, IGNORE_GLASS ignoreGlass,
edict_t *pentIgnore, TraceResult *ptr) {
TRACE_LINE(vecStart, vecEnd,
(igmon ==
ignore_monsters ? TRUE : FALSE) | (ignoreGlass ? 0x100 : 0),
pentIgnore, ptr);
}
void
UTIL_TraceLine(const Vector &vecStart, const Vector &vecEnd,
IGNORE_MONSTERS igmon, edict_t *pentIgnore,
TraceResult *ptr) {
TRACE_LINE(vecStart, vecEnd, (igmon == ignore_monsters ? TRUE : FALSE),
pentIgnore, ptr);
}
void
UTIL_TraceHull(const Vector &vecStart, const Vector &vecEnd,
IGNORE_MONSTERS igmon, int hullNumber, edict_t *pentIgnore,
TraceResult *ptr) {
TRACE_HULL(vecStart, vecEnd, (igmon == ignore_monsters ? TRUE : FALSE), hullNumber, pentIgnore, ptr);
}
void UTIL_MakeVectors(const Vector &vecAngles) {
MAKE_VECTORS(vecAngles);
}
edict_t *UTIL_FindEntityInSphere(edict_t *pentStart,
const Vector &vecCenter, float flRadius) {
edict_t *pentEntity;
pentEntity = FIND_ENTITY_IN_SPHERE(pentStart, vecCenter, flRadius);
if (!FNullEnt(pentEntity))
return pentEntity;
return NULL;
}
edict_t *UTIL_FindEntityByString(edict_t *pentStart,
const char *szKeyword,
const char *szValue) {
edict_t *pentEntity;
pentEntity = FIND_ENTITY_BY_STRING(pentStart, szKeyword, szValue);
if (!FNullEnt(pentEntity))
return pentEntity;
return NULL;
}
edict_t *UTIL_FindEntityByClassname(edict_t *pentStart,
const char *szName) {
return UTIL_FindEntityByString(pentStart, "classname", szName);
}
edict_t *UTIL_FindEntityByTargetname(edict_t *pentStart,
const char *szName) {
return UTIL_FindEntityByString(pentStart, "targetname", szName);
}
int UTIL_PointContents(const Vector &vec) {
return POINT_CONTENTS(vec);
}
void
UTIL_SetSize(entvars_t *pev, const Vector &vecMin, const Vector &vecMax) {
SET_SIZE(ENT(pev), vecMin, vecMax);
}
void UTIL_SetOrigin(entvars_t *pev, const Vector &vecOrigin) {
SET_ORIGIN(ENT(pev), vecOrigin);
}
void ClientPrint(edict_t *pEntity, int msg_dest, const char *msg_name) {
if (gmsgTextMsg == 0)
gmsgTextMsg = REG_USER_MSG("TextMsg", -1);
MESSAGE_BEGIN(MSG_ONE, gmsgTextMsg, NULL, pEntity);
WRITE_BYTE(msg_dest);
WRITE_STRING(msg_name);
MESSAGE_END();
}
void
UTIL_ClientPrintAll(int msg_dest, const char *msg_name, const char *param1,
const char *param2, const char *param3,
const char *param4) {
if (gmsgTextMsg == 0)
gmsgTextMsg = REG_USER_MSG("TextMsg", -1);
MESSAGE_BEGIN(MSG_ALL, gmsgTextMsg);
WRITE_BYTE(msg_dest);
WRITE_STRING(msg_name);
if (param1)
WRITE_STRING(param1);
if (param2)
WRITE_STRING(param2);
if (param3)
WRITE_STRING(param3);
if (param4)
WRITE_STRING(param4);
MESSAGE_END();
}
void UTIL_SayText(const char *pText, edict_t *pEdict) {
if (gmsgSayText == 0)
gmsgSayText = REG_USER_MSG("SayText", -1);
MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, pEdict);
WRITE_BYTE(ENTINDEX(pEdict));
//if (mod_id == FRONTLINE_DLL)
// WRITE_SHORT(0);
WRITE_STRING(pText);
MESSAGE_END();
}
void UTIL_HostSay(edict_t *pEntity, int teamonly, char *message) {
int j;
char text[128];
char *pc;
int sender_team, player_team;
edict_t *client;
// make sure the text has content
for (pc = message; pc != NULL && *pc != 0; pc++) {
if (isprint(*pc) && !isspace(*pc)) {
pc = NULL; // we've found an alphanumeric character, so text is valid
break;
}
}
if (pc != NULL)
return; // no character found, so say nothing
// turn on color set 2 (color on, no sound)
if (teamonly)
sprintf(text, "%c(TEAM) %s: ", 2, STRING(pEntity->v.netname));
else
sprintf(text, "%c%s: ", 2, STRING(pEntity->v.netname));
j = sizeof(text) - 2 - strlen(text); // -2 for /n and null terminator
if ((int) strlen(message) > j)
message[j] = 0;
strcat(text, message);
strcat(text, "\n");
// loop through all players
// Start with the first player.
// This may return the world in single player if the client types something between levels or during spawn
// so check it, or it will infinite loop
if (gmsgSayText == 0)
gmsgSayText = REG_USER_MSG("SayText", -1);
sender_team = UTIL_GetTeam(pEntity);
client = NULL;
while (((client = UTIL_FindEntityByClassname(client, "player")) != NULL)
&& (!FNullEnt(client))) {
if (client == pEntity) // skip sender of message
continue;
player_team = UTIL_GetTeam(client);
if (teamonly && (sender_team != player_team))
continue;
MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, client);
WRITE_BYTE(ENTINDEX(pEntity));
//if (mod_id == FRONTLINE_DLL)
// WRITE_SHORT(0);
WRITE_STRING(text);
MESSAGE_END();
}
// print to the sending client
MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, pEntity);
WRITE_BYTE(ENTINDEX(pEntity));
//if (mod_id == FRONTLINE_DLL)
// WRITE_SHORT(0);
WRITE_STRING(text);
MESSAGE_END();
// echo to server console
g_engfuncs.pfnServerPrint(text);
}
#ifdef DEBUG
edict_t *DBG_EntOfVars(const entvars_t * pev) {
if (pev->pContainingEntity != NULL)
return pev->pContainingEntity;
ALERT(at_console,
"entvars_t pContainingEntity is NULL, calling into engine");
edict_t *pent = (*g_engfuncs.pfnFindEntityByVars) ((entvars_t *) pev);
if (pent == NULL)
ALERT(at_console,
"DAMN! Even the engine couldn't FindEntityByVars!");
((entvars_t *) pev)->pContainingEntity = pent;
return pent;
}
#endif //DEBUG
// Is the edict a vip or not?
bool UTIL_IsVip(edict_t *pEntity) {
if (mod_id != CSTRIKE_DLL)
return false;
else {
char *infobuffer;
char model_name[32];
infobuffer = (*g_engfuncs.pfnGetInfoKeyBuffer)(pEntity);
strcpy(model_name, (g_engfuncs.pfnInfoKeyValue(infobuffer, "model")));
if (strcmp(model_name, "vip") == 0) // VIP
return true;
}
return false;
}
// return team number 0 through 3 based what MOD uses for team numbers
int UTIL_GetTeam(edict_t *pEntity) {
if (mod_id == CSTRIKE_DLL) {
char *infobuffer;
char model_name[32];
infobuffer = (*g_engfuncs.pfnGetInfoKeyBuffer)(pEntity);
strcpy(model_name,
(g_engfuncs.pfnInfoKeyValue(infobuffer, "model")));
if ((strcmp(model_name, "terror") == 0) || // Phoenix Connektion
(strcmp(model_name, "arab") == 0) || // old L337 Krew
(strcmp(model_name, "leet") == 0) || // L337 Krew
(strcmp(model_name, "artic") == 0) || // Artic Avenger
(strcmp(model_name, "arctic") == 0) || // Artic Avenger - fix for arctic? - seemed a typo?
(strcmp(model_name, "guerilla") == 0)) // Gorilla Warfare
{
return 0; // team Terrorists
} else if ((strcmp(model_name, "urban") == 0) || // Seal Team 6
(strcmp(model_name, "gsg9") == 0) || // German GSG-9
(strcmp(model_name, "sas") == 0) || // UK SAS
(strcmp(model_name, "gign") == 0) || // French GIGN
(strcmp(model_name, "vip") == 0) || // VIP
(strcmp(model_name, "spetsnatz") == 0)) // CZ Spetsnatz
{
return 1; // team Counter-Terrorists
}
return -1; // unknown team
}
return -2; // unknown mod
}
// return class number 0 through N
int UTIL_GetClass(edict_t *pEntity) {
char *infobuffer;
char model_name[32];
infobuffer = (*g_engfuncs.pfnGetInfoKeyBuffer)(pEntity);
strcpy(model_name, (g_engfuncs.pfnInfoKeyValue(infobuffer, "model")));
return 0;
}
int UTIL_GetBotIndex(edict_t *pEdict) {
int index;
for (index = 0; index < 32; index++) {
if (bots[index].pEdict == pEdict) {
return index;
}
}
return -1; // return -1 if edict is not a bot
}
/**
* Returns bot pointer for possible pEdict (player)
* @param pEdict
* @return
*/
cBot *UTIL_GetBotPointer(edict_t *pEdict) {
int index;
for (index = 0; index < 32; index++) {
if (bots[index].pEdict == pEdict) {
return (&bots[index]);
}
}
return NULL; // return NULL if edict is not a bot
}
bool IsAlive(edict_t *pEdict) {
// FIX: Make sure the edict is valid and such, else return false:
return ((pEdict != NULL) && // VALID
(pEdict->v.deadflag == DEAD_NO) && // NOT DEAD
(pEdict->v.health > 0) && // ENOUGHT HEALTH
!(pEdict->v.flags & FL_NOTARGET) && // ?
(pEdict->v.takedamage != 0)); // CAN TAKE DAMAGE
}
bool FInViewCone(Vector *pOrigin, edict_t *pEdict) {
Vector2D vec2LOS;
float flDot;
UTIL_MakeVectors(pEdict->v.angles);
vec2LOS = (*pOrigin - pEdict->v.origin).Make2D();
vec2LOS = vec2LOS.Normalize();
flDot = DotProduct(vec2LOS, gpGlobals->v_forward.Make2D());
if (flDot > 0.50) // 60 degree field of view
{
return TRUE;
} else {
return FALSE;
}
}
// FVisible()
bool FVisible(const Vector &vecOrigin, edict_t *pEdict) {
TraceResult tr;
Vector vecLookerOrigin;
// look through caller's eyes
vecLookerOrigin = pEdict->v.origin + pEdict->v.view_ofs;
int bInWater = (UTIL_PointContents(vecOrigin) == CONTENTS_WATER);
int bLookerInWater =
(UTIL_PointContents(vecLookerOrigin) == CONTENTS_WATER);
// don't look through water
if (bInWater != bLookerInWater)
return FALSE;
UTIL_TraceLine(vecLookerOrigin, vecOrigin, ignore_monsters,
ignore_glass, pEdict, &tr);
if (tr.flFraction != 1.0) {
return FALSE; // Line of sight is not established
} else {
return TRUE; // line of sight is valid.
}
}
Vector GetGunPosition(edict_t *pEdict) {
return (pEdict->v.origin + pEdict->v.view_ofs);
}
void UTIL_SelectItem(edict_t *pEdict, char *item_name) {
/*BotDebug( item_name); */
FakeClientCommand(pEdict, item_name, NULL, NULL);
}
Vector VecBModelOrigin(edict_t *pEdict) {
return pEdict->v.absmin + (pEdict->v.size * 0.5);
}
void
UTIL_ShowMenu(edict_t *pEdict, int slots, int displaytime, bool needmore,
char *pText) {
if (gmsgShowMenu == 0)
gmsgShowMenu = REG_USER_MSG("ShowMenu", -1);
MESSAGE_BEGIN(MSG_ONE, gmsgShowMenu, NULL, pEdict);
WRITE_SHORT(slots);
WRITE_CHAR(displaytime);
WRITE_BYTE(needmore);
WRITE_STRING(pText);
MESSAGE_END();
}
void UTIL_BuildFileName(char *filename, char *arg1, char *arg2) {
if (mod_id == VALVE_DLL)
strcpy(filename, "valve/");
else if (mod_id == CSTRIKE_DLL)
strcpy(filename, "cstrike/");
else {
filename[0] = 0;
return;
}
if ((arg1) && (*arg1) && (arg2) && (*arg2)) {
strcat(filename, arg1);
strcat(filename, "/");
strcat(filename, arg2);
} else if ((arg1) && (*arg1)) {
strcat(filename, arg1);
}
}
// added by Tub
// copies subdir/file to filename
void UTIL_BuildFileNameRB(char *subdir, char *filename) {
#if DO_DEBUG != 0
char *filenamedebug = filename;
#endif
#ifdef _WIN32
#define S '\\'
#else
#define S '/'
#endif
strcpy(filename, "realbot/");
strcat(filename, subdir);
while (*filename) {
if ((*filename == '/') && (*filename == '\\'))
*filename = S;
filename++;
}
#if DO_DEBUG != 0
DebugOut(filenamedebug);
#endif
#undef S
}
//=========================================================
// UTIL_LogPrintf - Prints a logged message to console.
// Preceded by LOG: ( timestamp ) < message >
//=========================================================
void UTIL_LogPrintf(char *fmt, ...) {
va_list argptr;
static char string[1024];
va_start(argptr, fmt);
vsprintf(string, fmt, argptr);
va_end(argptr);
// Print to server console
ALERT(at_logged, "%s", string);
}
void UTIL_BotPressKey(cBot *pBot, int type) {
if (type == IN_JUMP || type == IN_DUCK) {
if (pBot->isFreezeTime()) {
pBot->rprint_trace("UTIL_BotPressKey", "IN_JUMP or IN_DUCK press requested, but in freezetime - IGNORING");
return; // do nothing when in freezetime
}
}
if (type == IN_JUMP) {
if (pBot->f_may_jump_time > gpGlobals->time || // do nothing when we may not jump
pBot->f_camp_time > gpGlobals->time || // when camping do not jump
FUNC_IsOnLadder(pBot->pEdict) // don't jump from ladder
) {
pBot->rprint_trace("UTIL_BotPressKey",
"IN_JUMP requested, but either camping, still jumping or on ladder - IGNORING");
return;
}
}
// KEY: Reload
if (type == IN_RELOAD) // when reloading, there is NO zooming (when holding a zoomable weapon or a sniper gun)
{
pBot->rprint_trace("UTIL_BotPressKey", "IN_RELOAD");
if (FUNC_BotHoldsZoomWeapon(pBot) || UTIL_GiveWeaponType(pBot->current_weapon.iId) == SNIPER)
//pBot->zoomed = ZOOM_NONE; // not zoomed anymore
// FIX: Do not let bots do anything with this weapon for 0.7 second. So the engine can
// update the information.
pBot->f_update_weapon_time = gpGlobals->time + 0.7;
}
// KEY: End
switch (type) {
case IN_DUCK:
pBot->rprint_trace("UTIL_BotPressKey", "IN_DUCK");
break;
case IN_JUMP:
pBot->rprint_trace("UTIL_BotPressKey", "IN_JUMP");
break;
case IN_USE:
pBot->rprint_trace("UTIL_BotPressKey", "IN_USE");
break;
case IN_RELOAD:
pBot->rprint_trace("UTIL_BotPressKey", "IN_RELOAD");
break;
case IN_ATTACK:
pBot->rprint_trace("UTIL_BotPressKey", "IN_ATTACK");
break;
case IN_RUN:
pBot->rprint_trace("UTIL_BotPressKey", "IN_RUN");
break;
case IN_MOVELEFT:
pBot->rprint_trace("UTIL_BotPressKey", "IN_MOVELEFT");
break;
case IN_MOVERIGHT:
pBot->rprint_trace("UTIL_BotPressKey", "IN_MOVERIGHT");
break;
default:
char msg[255];
sprintf(msg, "unknown key to print [%d]", type);
pBot->rprint_trace("UTIL_BotPressKey", msg);
break;
}
pBot->pEdict->v.button |= type;
}
int UTIL_GiveWeaponType(int weapon_id) {
int kind = NONE;
// 30/07/04 by Josh
// Use switch instead of multiple if
switch (weapon_id) {
case CS_WEAPON_KNIFE:
kind = KNIFE;
break; // Check1. Is it a knife?
case CS_WEAPON_FLASHBANG:
case CS_WEAPON_HEGRENADE:
case CS_WEAPON_SMOKEGRENADE:
kind = GRENADE;
break; // Check 2, is it a 'tool'?
case CS_WEAPON_P228:
case CS_WEAPON_ELITE:
case CS_WEAPON_USP:
case CS_WEAPON_GLOCK18:
case CS_WEAPON_DEAGLE:
case CS_WEAPON_FIVESEVEN:
kind = SECONDARY;
break; // Check 3, is it a secondary gun?
case CS_WEAPON_SCOUT:
case CS_WEAPON_SG550:
case CS_WEAPON_AWP:
case CS_WEAPON_G3SG1:
kind = SNIPER;
break; // Check 4, is it a sniper gun?
//31.08.04 Frashman: added to switch construct
case CS_WEAPON_SHIELD:
kind = SHIELD; // 08/07/04 - Also take shield into account
}
// When the kind of weapon is still not found, its a primary (in CS)
if (kind == NONE)
kind = PRIMARY;
if (weapon_id < 1)
kind = NONE;
return kind;
}
// Return weapon ID (depended on mod)
int UTIL_GiveWeaponId(const char *name) {
if (mod_id == CSTRIKE_DLL) {
if (strcmp(name, "weapon_knife") == 0)
return CS_WEAPON_KNIFE;
if (strcmp(name, "weapon_c4") == 0)
return CS_WEAPON_C4;
if (strcmp(name, "weapon_mp5navy") == 0)
return CS_WEAPON_MP5NAVY;
if (strcmp(name, "weapon_ak47") == 0)
return CS_WEAPON_AK47;
if (strcmp(name, "weapon_m3") == 0)
return CS_WEAPON_M3;
if (strcmp(name, "weapon_aug") == 0)
return CS_WEAPON_AUG;
if (strcmp(name, "weapon_sg552") == 0)
return CS_WEAPON_SG552;
if (strcmp(name, "weapon_m249") == 0)
return CS_WEAPON_M249;
if (strcmp(name, "weapon_xm1014") == 0)
return CS_WEAPON_XM1014;
if (strcmp(name, "weapon_p90") == 0)
return CS_WEAPON_P90;
if (strcmp(name, "weapon_tmp") == 0)
return CS_WEAPON_TMP;
if (strcmp(name, "weapon_m4a1") == 0)
return CS_WEAPON_M4A1;
if (strcmp(name, "weapon_awp") == 0)
return CS_WEAPON_AWP;
if (strcmp(name, "weapon_fiveseven") == 0)
return CS_WEAPON_FIVESEVEN;
if (strcmp(name, "weapon_ump45") == 0)
return CS_WEAPON_UMP45;
if (strcmp(name, "weapon_sg550") == 0)
return CS_WEAPON_SG550;
if (strcmp(name, "weapon_scout") == 0)
return CS_WEAPON_SCOUT;
if (strcmp(name, "weapon_mac10") == 0)
return CS_WEAPON_MAC10;
if (strcmp(name, "weapon_g3sg1") == 0)
return CS_WEAPON_G3SG1;
if (strcmp(name, "weapon_elite") == 0)
return CS_WEAPON_ELITE;
if (strcmp(name, "weapon_p228") == 0)
return CS_WEAPON_P228;
if (strcmp(name, "weapon_deagle") == 0)
return CS_WEAPON_DEAGLE;
if (strcmp(name, "weapon_usp") == 0)
return CS_WEAPON_USP;
if (strcmp(name, "weapon_glock18") == 0)
return CS_WEAPON_GLOCK18;
// Counter-Strike 1.6
if (strcmp(name, "weapon_famas") == 0)
return CS_WEAPON_FAMAS;
if (strcmp(name, "weapon_galil") == 0)
return CS_WEAPON_GALIL;
// TODO: Detect shield carrying.
// 06/07/04
// Unconfirmed for handling shield
// 31.08.04 Frashman: moved shield string before unknown weapon, not after it
if (strcmp(name, "weapon_shield") == 0)
return CS_WEAPON_SHIELD;
char buffer[80];
_snprintf(buffer, 79, "UTIL_GiveWeaponId: Unknown weapon name %s\n", name);
rblog(buffer);
}
return -1;
}
// Return weapon ID (depended on mod)
char *UTIL_GiveWeaponName(int id) {
if (mod_id == CSTRIKE_DLL) {
switch (id) {
case CS_WEAPON_C4:
return "weapon_c4";
break;
case CS_WEAPON_MP5NAVY:
return "weapon_mp5navy";
break;
case CS_WEAPON_AK47:
return "weapon_ak47";
break;
case CS_WEAPON_M3:
return "weapon_m3";
break;
case CS_WEAPON_AUG:
return "weapon_aug";
break;
case CS_WEAPON_SG552:
return "weapon_sg552";
break;
case CS_WEAPON_M249:
return "weapon_m249";
break;
case CS_WEAPON_XM1014:
return "weapon_xm1014";
break;
case CS_WEAPON_P90:
return "weapon_p90";
break;
case CS_WEAPON_TMP:
return "weapon_tmp";
break;
case CS_WEAPON_M4A1:
return "weapon_m4a1";
break;
case CS_WEAPON_AWP:
return "weapon_awp";
break;
case CS_WEAPON_FIVESEVEN:
return "weapon_fiveseven";
break;
case CS_WEAPON_UMP45:
return "weapon_ump45";
break;
case CS_WEAPON_SG550:
return "weapon_ag550";
break;
case CS_WEAPON_SCOUT:
return "weapon_scout";
break;
case CS_WEAPON_MAC10:
return "weapon_mac10";
break;
case CS_WEAPON_G3SG1:
return "weapon_g3sg1";
break;
case CS_WEAPON_ELITE:
return "weapon_elite";
break;
case CS_WEAPON_P228:
return "weapon_p228";
break;
case CS_WEAPON_DEAGLE:
return "weapon_deagle";
break;
case CS_WEAPON_USP:
return "weapon_usp";
break;
case CS_WEAPON_GLOCK18:
return "weapon_glock18";
break;
// Counter-Strike 1.6
case CS_WEAPON_FAMAS:
return "weapon_famas";
break;
case CS_WEAPON_GALIL:
return "weapon_galil";
break;
// Unconfirmed shield
case CS_WEAPON_SHIELD:
return "weapon_shield";
break;
}
}
return "weapon_knife"; // return knife, always good ;)
}
// Thanks Botman for this code (from forum).
void UTIL_BotSprayLogo(edict_t *pEntity, char *logo_name) {
int index;
TraceResult pTrace;
Vector v_src, v_dest;
UTIL_MakeVectors(pEntity->v.v_angle);
v_src = pEntity->v.origin + pEntity->v.view_ofs;
v_dest = v_src + gpGlobals->v_forward * 80;
UTIL_TraceLine(v_src, v_dest, ignore_monsters,
pEntity->v.pContainingEntity, &pTrace);
index = DECAL_INDEX(logo_name);
if (index < 0)
return;
if ((pTrace.pHit) && (pTrace.flFraction < 1.0)) {
if (pTrace.pHit->v.solid != SOLID_BSP)
return;
MESSAGE_BEGIN(MSG_BROADCAST, SVC_TEMPENTITY);
if (index > 255) {
WRITE_BYTE(TE_WORLDDECALHIGH);
index -= 256;
} else
WRITE_BYTE(TE_WORLDDECAL);
WRITE_COORD(pTrace.vecEndPos.x);
WRITE_COORD(pTrace.vecEndPos.y);
WRITE_COORD(pTrace.vecEndPos.z);
WRITE_BYTE(index);
MESSAGE_END();
EMIT_SOUND_DYN2(pEntity, CHAN_VOICE, "player/sprayer.wav", 1.0,
ATTN_NORM, 0, 100);
}
}
// Give a radio message botty boy!
void UTIL_BotRadioMessage(cBot *pBot, int radio, char *arg1, char *arg2) {
// To be sure the console will only change when we MAY change.
// The values will only be changed when console_nr is 0
if (pBot->console_nr == 0) {
switch (radio) {
case 1:
strcpy(pBot->arg1, "radio1");
break;
case 2:
strcpy(pBot->arg1, "radio2");
break;
case 3:
strcpy(pBot->arg1, "radio3");
break;
}
strcpy(pBot->arg2, arg1);
strcpy(pBot->arg3, arg2);
pBot->console_nr = 1; // Begin message
// 02/07/04 - pointed out, eliminate any possible 'devide by zero'
int iExtra = (100 / (pBot->ipCreateRadio + 1));
if (iExtra > 30)
iExtra = 30;
pBot->fDoRadio = gpGlobals->time + iExtra;
}
}
//////////////////////////////////
// UTIL_getGrenadeType function // - Stefan
//////////////////////////////////
int UTIL_GetGrenadeType(edict_t *pEntity) {
const int length = 32;
char model_name[length];
memset(model_name, 0, sizeof(model_name));
strncpy(model_name, STRING(pEntity->v.model), length - 1);
model_name[length - 1] = 0; // Make sure it is NULL terminated
if (strcmp(model_name, "models/w_hegrenade.mdl") == 0) return 1; // He grenade
if (strcmp(model_name, "models/w_flashbang.mdl") == 0) return 2; // FlashBang
if (strcmp(model_name, "models/w_smokegrenade.mdl") == 0) return 3; // SmokeGrenade
if (strcmp(model_name, "models/w_c4.mdl") == 0) return 4; // C4 Explosive
char msg[512];
memset(msg, 0, sizeof(msg));
// when non empty string, let us know we missed something
if (!strlen(model_name) == 0) {
sprintf(msg, "UTIL_GetGrenadeType unknown grenade model : %s\n", model_name);
}
rblog(msg);
return 0;
}
// 2 functions from podbot source
unsigned short FixedUnsigned16(float value, float scale) {
int output;
output = (int) value * scale;
if (output < 0)
output = 0;
if (output > 0xFFFF)
output = 0xFFFF;
return (unsigned short) output;
}
short FixedSigned16(float value, float scale) {
int output;
output = (int) value * scale;
if (output > 32767)
output = 32767;
if (output < -32768)
output = -32768;
return (short) output;
}
// Using POD/SDK source to print nice messages on the client machine
void HUD_DrawString(int r, int g, int b, char *msg, edict_t *edict) {
// FROM PODBOT SOURCE
// Hacked together Version of HUD_DrawString
MESSAGE_BEGIN(MSG_ONE, SVC_TEMPENTITY, NULL, edict);
WRITE_BYTE(TE_TEXTMESSAGE);
WRITE_BYTE(1);
WRITE_SHORT(FixedSigned16(-1, 1 << 13));
WRITE_SHORT(FixedSigned16(0, 1 << 13));
WRITE_BYTE(2);
WRITE_BYTE(r); //r
WRITE_BYTE(g); //g
WRITE_BYTE(b); //b
WRITE_BYTE(0);
WRITE_BYTE(255);
WRITE_BYTE(255);
WRITE_BYTE(255);
WRITE_BYTE(200);
WRITE_SHORT(FixedUnsigned16(0.0078125, 1 << 8));
WRITE_SHORT(FixedUnsigned16(2, 1 << 8));
WRITE_SHORT(FixedUnsigned16(6, 1 << 8));
WRITE_SHORT(FixedUnsigned16(0.1, 1 << 8));
WRITE_STRING((const char *) &msg[0]);
MESSAGE_END();
}
void UTIL_FixAngles(Vector *Angles) {
if (Angles->x > 180.0)
Angles->x -= 360.0;
if (Angles->x < -180.0)
Angles->x += 360.0;
if (Angles->y > 180.0)
Angles->y -= 360.0;
if (Angles->y < -180.0)
Angles->y += 360.0;
Angles->z = 0.0;
}
void UTIL_SayTextBot(const char *pText, cBot *pBot) {
if (gmsgSayText == 0)
gmsgSayText = REG_USER_MSG("SayText", -1);
char szTemp[160];
char szName[BOT_NAME_LEN + 1];
int i = 0;
// clear out
memset(szTemp, 0, sizeof(szTemp));
memset(szName, 0, sizeof(szName));
// init
szTemp[0] = 2;
int entind = ENTINDEX(pBot->pEdict);
if (IsAlive(pBot->pEdict)) {
strcpy(szName, pBot->name);
for (i = 1; i <= gpGlobals->maxClients; i++) {
edict_t *pPlayer = INDEXENT(i);
// valid
if (pPlayer)
if (IsAlive(pPlayer)) // alive
{
MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, pPlayer);
WRITE_BYTE(entind);
sprintf(&szTemp[1], "%s : %s", szName, pText);
WRITE_STRING(&szTemp[0]);
MESSAGE_END();
}
}
} else {