-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cpp
4672 lines (3677 loc) · 120 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.
*
****/
///////////////////////////////////////////////////////////////////////////////////////////////
//
// This file contains both GNU as VALVE licenced material.
//
// This source file contains VALVE LCC licence material.
// Read and Agree to the above stated header
// before distibuting or editing this source code.
//
// Please read and agree to the mb_gnu_license.txt file
// (the file is located in the marine_bot source folder)
// before editing or distributing this source code.
// This source code is free for use under the rules of the GNU General Public License.
// For more information goto:: http://www.gnu.org/licenses/
//
// credits to - valve, botman.
//
// Marine Bot - code by Frank McNeil, Kota@, Mav, Shrike.
//
// (http://marinebot.xf.cz)
//
//
// util.cpp
//
////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(WIN32)
#pragma warning(disable: 4005 91 4996)
#endif
#include "defines.h"
#include "extdll.h"
#include "util.h"
#include "engine.h"
#include "bot.h"
#include "bot_func.h"
#include "bot_manager.h"
#include "bot_weapons.h"
#include "waypoint.h"
int gmsgTextMsg = 0;
int gmsgSayText = 0;
int gmsgShowMenu = 0;
// util functions prototypes
edict_t *UTIL_FindEntityByString( edict_t *pentStart, const char *szKeyword, const char *szValue );
void UTIL_BuildFileName(char *filename, char *arg1, char *arg2, bool separator);
inline void SelectMainWeapon(bot_t *pBot);
inline void SelectBackupWeapon(bot_t *pBot);
inline void SelectGrenade(bot_t *pBot);
bool IgnoreFireMode(int weapon);
bool BurstFireMode(int weapon);
bool FullAutoFireMode(int weapon);
bool IsStringValid(char *str);
void ProcessTheName(char *name);
void ShortenIt(char *name);
void DumpVector(FILE *f, Vector vec);
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_MakeVectors( const Vector &vecAngles )
{
MAKE_VECTORS( vecAngles );
}
edict_t *UTIL_FindEntityInSphere( edict_t *pentStart, const Vector &vecCenter, float flRadius )
{
edict_t* pentEntity = FIND_ENTITY_IN_SPHERE(pentStart, vecCenter, flRadius);
if (!FNullEnt(pentEntity))
return pentEntity;
return nullptr;
}
// search for entity passed by its classname
edict_t *UTIL_FindEntityInSphere(edict_t *pCallerEdict, const char *ent_name)
{
edict_t *pEntity = nullptr;
while ((pEntity = UTIL_FindEntityInSphere(pEntity, pCallerEdict->v.origin, PLAYER_SEARCH_RADIUS)) != nullptr)
{
if (strcmp(STRING(pEntity->v.classname), ent_name) == 0)
return pEntity;
}
return nullptr;
}
edict_t *UTIL_FindEntityByString( edict_t *pentStart, const char *szKeyword, const char *szValue )
{
edict_t* pentEntity = FIND_ENTITY_BY_STRING(pentStart, szKeyword, szValue);
if (!FNullEnt(pentEntity))
return pentEntity;
return nullptr;
}
edict_t *UTIL_FindEntityByClassname( edict_t *pentStart, const char *szName )
{
return UTIL_FindEntityByString( pentStart, "classname", szName );
}
/* NEVER USED
edict_t *UTIL_FindEntityByTargetname( edict_t *pentStart, const char *szName )
{
return UTIL_FindEntityByString( pentStart, "targetname", szName );
}
*/
// Defined in util.h
int UTIL_PointContents( const Vector &vec )
{
return POINT_CONTENTS(vec);
}
// Defined in util.h - NEVER USED
void UTIL_SetSize( entvars_t *pev, const Vector &vecMin, const Vector &vecMax )
{
SET_SIZE( ENT(pev), vecMin, vecMax );
}
// Defined in util.h - NEVER USED
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 );
pfnMessageBegin( MSG_ONE, gmsgTextMsg, nullptr, pEntity );
pfnWriteByte( msg_dest );
pfnWriteString( msg_name );
pfnMessageEnd();
}
/* NEVER USED
void UTIL_SayText( const char *pText, edict_t *pEdict )
{
if (gmsgSayText == 0)
gmsgSayText = REG_USER_MSG( "SayText", -1 );
pfnMessageBegin( MSG_ONE, gmsgSayText, NULL, pEdict );
pfnWriteByte( ENTINDEX(pEdict) );
pfnWriteString( pText );
pfnMessageEnd();
}
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;
pfnMessageBegin( MSG_ONE, gmsgSayText, NULL, client );
pfnWriteByte( ENTINDEX(pEntity) );
pfnWriteString( text );
pfnMessageEnd();
}
// print to the sending client
pfnMessageBegin( MSG_ONE, gmsgSayText, NULL, pEntity );
pfnWriteByte( ENTINDEX(pEntity) );
pfnWriteString( text );
pfnMessageEnd();
// 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
/*
* return the team based either on team variable or on the model "name" the Edict uses
*/
int UTIL_GetTeam(edict_t *pEntity)
{
if (pEntity == nullptr)
{
#ifdef _DEBUG
char msg[256];
sprintf(msg, "<FATAL ERROR>(util.cpp|GetTeam()) pEntity is NULL\n");
UTIL_DebugDev(msg, -100, -100);
HudNotify(msg);
#endif
return TEAM_NONE;
}
if (mod_id == FIREARMS_DLL)
{
// try the team variable first
// this works in most of situations so it should be on top to save some CPU time
if (pEntity->v.team == TEAM_RED)
return TEAM_RED;
if (pEntity->v.team == TEAM_BLUE)
return TEAM_BLUE;
// works for getting team info from thrown grenades in all supported versions
if (pEntity->v.owner != nullptr)
return UTIL_GetTeam(pEntity->v.owner);
/* PREV CODE - works well in FA 29 and so on, but not in 25 etc.
if ((pEntity->v.owner != NULL) && (pEntity->v.owner->v.team == TEAM_RED))
return TEAM_RED;
else if ((pEntity->v.owner != NULL) && (pEntity->v.owner->v.team == TEAM_BLUE))
return TEAM_BLUE;
*/
// try netname is the owner pointer failed
// happens when the owner just left the game or something
if (strcmp(STRING(pEntity->v.netname), "Red Force") == 0)
return TEAM_RED;
else if (strcmp(STRING(pEntity->v.netname), "Blue Force") == 0)
return TEAM_BLUE;
/*/
#ifdef _DEBUG
if (UTIL_IsOldVersion() == FALSE)
HudNotify("***Can't get team from edict.team using edict.model\n");
#endif
/**/
// if previous checks failed then try to get the team info from the model
// (works in older FA versions)
// Red team - sand(brown) camouflage
if (strstr(STRING(pEntity->v.model), "red1") != nullptr)
return TEAM_RED;
// Blue team - jungle(green) camouflage
else if (strstr(STRING(pEntity->v.model), "blue1") != nullptr)
return TEAM_BLUE;
}
#ifdef _DEBUG
// don't print an error message in these situations
// (ie. they are quite common and don't cause problems)
//
// 1)test if the client didn't get in game yet (currently picking a team, class etc.)
// FA doesn't seem to set spectator flag or this combination when someone spectates
// so this cannot handle players who do spectate
// 2)the game world has no team so we must break it here
// happens when the bot get hurt after falling from something higher
bool okay = TRUE;
if (UTIL_IsOldVersion())
{
// comment it out if needed
//okay = FALSE;
}
if (!(pEntity->v.flags & (FL_CLIENT & FL_NOTARGET & FL_SPECTATOR)) && okay)
{
char msg[256];
if ((pEntity->v.netname != NULL) && (pEntity->v.classname != NULL))
sprintf(msg, "<BUG>Can't get team even from edict.model for edict <%s> (class <%s>)\n",
STRING(pEntity->v.netname), STRING(pEntity->v.classname));
else if (pEntity->v.classname != NULL)
sprintf(msg, "<BUG>Can't get team even from edict.model for edict <no netname> (class <%s>)\n",
STRING(pEntity->v.classname));
else if (pEntity->v.netname != NULL)
sprintf(msg, "<BUG>Can't get team even from edict.model for edict <%s> (class <no classname>)\n",
STRING(pEntity->v.netname));
else
sprintf(msg, "<BUG>Can't get team even from edict.model for edict <no netname> (class <no classname>)\n");
// don't dump grenades placed in the map as item (ie you can pick them up)
if ((pEntity->v.solid == SOLID_TRIGGER) && (pEntity->v.movetype == MOVETYPE_TOSS))
return TEAM_NONE;
HudNotify(msg, true);
}
#endif
return TEAM_NONE; // return this flag if team is unknown
}
int UTIL_GetBotIndex(edict_t *pEdict)
{
for (int index = 0; index < MAX_CLIENTS; index++)
{
if (bots[index].pEdict == pEdict)
{
return index;
}
}
return -1; // return -1 if edict is not a bot
}
/* NEVER USED
bot_t *UTIL_GetBotPointer(edict_t *pEdict)
{
int index;
for (index=0; index < MAX_CLIENTS; index++)
{
if (bots[index].pEdict == pEdict)
{
break;
}
}
if (index < MAX_CLIENTS)
return (&bots[index]);
return NULL; // return NULL if edict is not a bot
}
*/
bool IsAlive(edict_t *pEdict)
{
return ((pEdict->v.deadflag == DEAD_NO) && !(pEdict->v.effects & EF_NODRAW) &&
(pEdict->v.health > 0) && !(pEdict->v.flags & FL_NOTARGET) &&
(pEdict->v.solid == SOLID_SLIDEBOX));
}
bool FInViewCone(Vector *pOrigin, edict_t *pEdict)
{
UTIL_MakeVectors ( pEdict->v.angles );
Vector2D vec2LOS = (*pOrigin - pEdict->v.origin).Make2D();
vec2LOS = vec2LOS.Normalize();
const float flDot = DotProduct(vec2LOS, gpGlobals->v_forward.Make2D());
if ( flDot > 0.50 ) // 60 degree field of view
{
return TRUE;
}
else
{
return FALSE;
}
}
// for more accurate checks
bool FInNarrowViewCone(Vector *pOrigin, edict_t *pEdict, float cone)
{
UTIL_MakeVectors ( pEdict->v.angles );
Vector2D vec2LOS = (*pOrigin - pEdict->v.origin).Make2D();
vec2LOS = vec2LOS.Normalize();
const float flDot = DotProduct(vec2LOS, gpGlobals->v_forward.Make2D());
//@@@@@@@@@@@@@@@@@@@@@@@
/*/
#ifdef _DEBUG
char m[256];
sprintf(m, "(util.cpp) NarrowViewCone() - dot result %.2f\n", flDot);
HudNotify(m);
#endif
/**/
if ( flDot >= cone ) // by default it is something like 35 and less degree field of view
{
return TRUE;
}
else
{
return FALSE;
}
}
// ehm "overloaded" to ignore water restriction by Frank McNeil
bool FVisible(const Vector &vecOrigin, edict_t *pEdict, bool ignore_water)
{
TraceResult tr;
// look through caller's eyes
const Vector vecLookerOrigin = pEdict->v.origin + pEdict->v.view_ofs;
const int bInWater = (UTIL_PointContents (vecOrigin) == CONTENTS_WATER);
const int bLookerInWater = (UTIL_PointContents (vecLookerOrigin) == CONTENTS_WATER);
// don't look through water
if ((bInWater != bLookerInWater) && (ignore_water == FALSE))
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.
}
}
bool FVisible( const Vector &vecOrigin, edict_t *pEdict )
{
TraceResult tr;
// look through caller's eyes
const Vector vecLookerOrigin = pEdict->v.origin + pEdict->v.view_ofs;
const int bInWater = (UTIL_PointContents (vecOrigin) == CONTENTS_WATER);
const 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);
//@@@@@@@@@@@@@
/*/
#ifdef _DEBUG
char str[126];
sprintf(str, "(Original VisibleTest)Result=%.2f | entity=%s\n", tr.flFraction, STRING(tr.pHit->v.classname));
HudNotify(str);
#endif
/**/
if (tr.flFraction != 1.0)
{
return FALSE; // Line of sight is not established
}
else
{
return TRUE; // line of sight is valid.
}
}
/*
* tests direct visibility between the bot and the target
* includes tests for teammate, fence etc.
* returns TRUE if line of sight can be established
*/
int FPlayerVisible(const Vector &vecOrigin, const Vector &vecLookerOrigin, edict_t *pEdict)
{
TraceResult tr;
const int bInWater = (UTIL_PointContents(vecOrigin) == CONTENTS_WATER);
const int bLookerInWater = (UTIL_PointContents(vecLookerOrigin) == CONTENTS_WATER);
// don't look through water
if (bInWater != bLookerInWater)
return VIS_NO;
// trace to see if there is a direct visibility at the enemy
// we can't ignore monters, because if we did we would not see teammates and we would do team kills all the time
UTIL_TraceLine(vecLookerOrigin, vecOrigin, dont_ignore_monsters, dont_ignore_glass, pEdict, &tr);
//@@@@@@@@@@@@@
#ifdef _DEBUG
char strs[128];
sprintf(strs, "(PlayerVisible - FIRST test)Result=%.2f | entity=%s(name=%s)\n",
tr.flFraction, STRING(tr.pHit->v.classname), STRING(tr.pHit->v.netname));
//HudNotify(strs);
#endif
// do we have clear view?
// when we don't ignore monsters then this won't happen offen, because the player entity has a size
// and if the bot and his enemy are face to face then enemy origin is "hidden behind" the player entity
if (tr.flFraction == 1.0)
return VIS_YES;
// we don't have another entity in the way (i.e. it's a solid world that blocks the direct visibility)
// therefore we don't need any other tests
if (strcmp(STRING(tr.pHit->v.classname), "worldspawn") == 0)
return VIS_NO;
// first backup the entity that blocks the direct visibility for later use
edict_t *obstacle = tr.pHit;
// now we have to ignore the previous obstacle in order to see if the trace line can finally reach the target
// i.e. virtually remove the obstacle and check direct visibility
UTIL_TraceLine(vecLookerOrigin, vecOrigin, ignore_monsters, dont_ignore_glass, obstacle, &tr);
//@@@@@@@@@@@@@
#ifdef _DEBUG
char str[256];
sprintf(str, "(PlayerVisible - 2nd test)Result=%.2f | entity=%s(name=%s)\n",
tr.flFraction, STRING(tr.pHit->v.classname), STRING(tr.pHit->v.netname));
//HudNotify(str);
#endif
// if we can hit worldspawn now i.e. flFraction == 1.0 && pHit->v.classname is worldspawn
// then we can establish a line of sight through the entity we ignored in the 2nd traceline
// so now just decide if we can really see through the ignored entity and if we can open fire (i.e. don't do a TK)
// the traceline reached the enemy without hitting anything else -> bot may eventually open fire
if ((strcmp(STRING(tr.pHit->v.classname), "worldspawn") == 0) && (tr.flFraction == 1.0))
{
// there was a player entity so we need to check if it isn't our teammate
// (i.e. teammate standing between the bot and the enemy)
if (strcmp(STRING(obstacle->v.classname), "player") == 0)
{
const int pEdict_team = UTIL_GetTeam(pEdict);
const int obstacle_team = UTIL_GetTeam(obstacle);
// we would hit a teammate so don't shoot and risk a team kill
if (pEdict_team == obstacle_team)
{
//@@@@@
#ifdef DEBUG
//HudNotify("*** BREAKING MY TEAMMATE IS IN THE WAY!!!\n");
#endif // DEBUG
return VIS_NO;
}
// it must be enemy so the bot can open fire
return VIS_YES;
}
// there was a breakable entity, all we need to do is check if the
// entity is done in a way players can see through
// (ie. some transparency and not rendered as normal or solid)
if ((strcmp(STRING(obstacle->v.classname), "func_breakable") == 0) &&
((obstacle->v.rendermode != kRenderNormal) ||
(obstacle->v.rendermode != kRenderTransAlpha)) && (obstacle->v.renderamt <= 150)) // NEEDS more tests
{
//@@@@@
#ifdef DEBUG
//HudNotify("*** I see through func breakable!\n");
#endif // DEBUG
return VIS_YES;
}
// there was an entity that is meant to be part of the world, but it's done so that you can see through it
// you can't pass through it of course ... a fence for example
if (strcmp(STRING(obstacle->v.classname), "func_wall") == 0)
{
//@@@@@
#ifdef DEBUG
char str[256];
sprintf(str, "(%s)*** I see func wall!\n", STRING(pEdict->v.netname));
//HudNotify(str);
#endif // DEBUG
// this is ugly solution, but the central alley causes real problems
// as long as there isn't better way how to deal with it we have to use texture names
// to figure out if it's a see through object or not
if (strcmp(STRING(gpGlobals->mapname), "obj_armory") == 0)
{
const char* texture = g_engfuncs.pfnTraceTexture(obstacle, vecLookerOrigin, vecOrigin);
if ((texture != nullptr) && ((strcmp(texture, "{nm_leaves3") == 0) || (strcmp(texture, "{artrellis") == 0)))
{
#ifdef DEBUG
//HudNotify("*** I see enemy! (func wall -> fence/bush on obj_armory)\n");
#endif // DEBUG
return VIS_FENCE;
}
return VIS_NO;
}
// a transparent alpha texture case (ie. a fence, barbed wire etc.)
if ((obstacle->v.rendermode == kRenderTransAlpha) && (obstacle->v.renderamt == 255) &&
(obstacle->v.flags & FL_WORLDBRUSH))
{
//@@@@@
#ifdef DEBUG
//HudNotify("*** I see enemy! (func wall -> fence/barbed wire)\n");
#endif // DEBUG
return VIS_FENCE;
}
// a transparent texture case (ie. a window, glass wall etc.)
else if ((obstacle->v.rendermode == kRenderTransTexture) && (obstacle->v.renderamt <= 150) &&
(obstacle->v.flags & FL_WORLDBRUSH))
{
//@@@@@
#ifdef DEBUG
//HudNotify("*** I see enemy! (func wall -> window/glass)\n");
#endif // DEBUG
return VIS_YES;
}
}
}
// line of sight is not established
// (the enemy must be behind something like a truck or tank and like models)
// basically something that we cannot and should not see through
return VIS_NO;
}
// same as above, but we use caller's eyes by default
int FPlayerVisible( const Vector &vecOrigin, edict_t *pEdict )
{
TraceResult tr;
// look through caller's eyes
const Vector vecLookerOrigin = pEdict->v.origin + pEdict->v.view_ofs;
return FPlayerVisible(vecOrigin, vecLookerOrigin, pEdict);
}
Vector GetGunPosition(edict_t *pEdict)
{
return (pEdict->v.origin + pEdict->v.view_ofs);
}
Vector VecBModelOrigin(edict_t *pEdict)
{
return pEdict->v.absmin + (pEdict->v.size * 0.5);
}
/*
* works very similar to FInViewCone(), but it returns exact angle value instead of TRUE or FALSE
*/
int InFieldOfView(edict_t *pEdict, const Vector &dest)
{
// find angles from source to destination...
Vector entity_angles = UTIL_VecToAngles( dest );
// make yaw angle 0 to 360 degrees if negative...
if (entity_angles.y < 0)
entity_angles.y += 360;
// get current view angle...
float view_angle = pEdict->v.v_angle.y;
// make view angle 0 to 360 degrees if negative...
if (view_angle < 0)
view_angle += 360;
// return the absolute value of angle to destination entity
// zero degrees means straight ahead, 45 degrees to the left or
// 45 degrees to the right is the limit of the normal view angle
// rsm - START angle bug fix
int angle = abs(static_cast<int>(view_angle) - static_cast<int>(entity_angles.y));
if (angle > 180)
angle = 360 - angle;
return angle;
// rsm - END
}
/*
* checks the mod version to see if it's one of new versions ie. versions that include
* a lot of differences that are imcompatible with previous version
*/
bool UTIL_IsNewerVersion()
{
if ((g_mod_version == FA_28) || (g_mod_version == FA_29) || (g_mod_version == FA_30))
return TRUE;
return FALSE;
}
/*
* checks the mod version to see if it's one of pre 2.6 versions ie. versions that don't
* support edict->v.team and gl attachments are fired right when you press secondary fire etc.
*/
bool UTIL_IsOldVersion()
{
if ((g_mod_version == FA_25) || (g_mod_version == FA_24))
return TRUE;
return FALSE;
}
/*
* adds the bit to the bit map
*/
void UTIL_SetBit(int the_bit, int &bit_map)
{
bit_map = bit_map | the_bit;
}
/*
* checks if the bit is in the map and if so then it removes it
*/
void UTIL_ClearBit(int the_bit, int &bit_map)
{
if (bit_map & the_bit)
bit_map = bit_map & ~the_bit;
}
/*
* returns if the bit is in the bit map or if it isn't
*/
bool UTIL_IsBitSet(int the_bit, int bit_map)
{
if (bit_map & the_bit)
return TRUE;
return FALSE;
}
void UTIL_ShowMenu( edict_t *pEdict, int slots, int displaytime, bool needmore, char *pText )
{
// don't run anything when in Firearms28, this menu was completely removed from game since FA28
if (UTIL_IsNewerVersion())
{
const Vector color1 = Vector(250, 50, 0);
const Vector color2 = Vector(255, 0, 20);
CustHudMessage(pEdict, "Warning: In-game HUD menu was removed in FA2.8 and above\nThis command isn't available", color1, color2, 2, 5);
ClientPrint(pEdict, HUD_PRINTNOTIFY, "In-game HUD menu was removed in FA2.8 and above. This command isn't available!\n");
// reset also menu variable
extern int g_menu_state;
g_menu_state = 0; // 0 == MENU_NONE
return;
}
if (gmsgShowMenu == 0)
gmsgShowMenu = REG_USER_MSG( "ShowMenu", -1 );
pfnMessageBegin( MSG_ONE, gmsgShowMenu, nullptr, pEdict );
pfnWriteShort( slots );
pfnWriteChar( displaytime );
pfnWriteByte( needmore );
pfnWriteString( pText );
pfnMessageEnd();
}
/*
* builds virtual path inside Half-Life
* separator == TRUE means that we need an additional directory seperator at the end of the path
* (eg. Half-Life\firearms\)
* separator == FALSE means no dir. separator will be added in the end of the path
* (eg. Half-Life\firearms)
*/
void UTIL_BuildFileName(char *filename, char *arg1, char *arg2, bool separator)
{
if ((arg1) && (*arg1))
strcpy(filename, arg1);
if ((arg2) && (*arg2))
{
#ifndef __linux__
strcat(filename, "\\");
#else
strcat(filename, "/");
#endif
strcat(filename, arg2);
}
if (separator)
{
#ifndef __linux__
strcat(filename, "\\");
#else
strcat(filename, "/");
#endif
}
#ifdef _DEBUG
extern int debug_engine;
if (debug_engine)
{
FILE *fp;
fp=fopen("!mb_engine_debug.txt","a");
fprintf(fp, "\nVirtual path build by BuildFileName(): \"%s\"\n", filename);
fclose(fp);
}
#endif
}
/*
* builds virtual path inside "marine_bot" folder
*/
void UTIL_MarineBotFileName(char *filename, char *arg1, char *arg2)
{
// build initial point first (ie. point to MB's folder)
// we are using char variable that holds mod directory name to allow using MB
// also in non-standard mod installations (eg. "Half-Life/FA" instead of
// default "Half-Life/firearms")
if (mod_id == FIREARMS_DLL)
UTIL_BuildFileName(filename, mod_dir_name, "marine_bot", TRUE);
else
{
filename[0] = 0;
return;
}
// then add additional parameters such as subdirectory name etc.
if ((arg1) && (*arg1) && (arg2) && (*arg2))
{
strcat(filename, arg1);
#ifndef __linux__
strcat(filename, "\\");
#else
strcat(filename, "/");
#endif
strcat(filename, arg2);
}
else if ((arg1) && (*arg1))
{
strcat(filename, arg1);
}
}
//=========================================================
// 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 );
}
/*
* works same as UTIL_LogPrintf only adds [MARINE_BOT] header before the logged message
*/
void UTIL_MBLogPrint(char *fmt, ...)
{
va_list argptr;
static char string[1024];
va_start ( argptr, fmt );
vsprintf ( string, fmt, argptr );
va_end ( argptr );
// Print to server console with MB header
ALERT( at_logged, "[MARINE_BOT] %s", string );
}
/*
// NOT USED & WILL PROBABLY NEVER BE USED
// trace tree lines forward - standing position eyes, duck position, prone position
int ForwardTrace(const Vector &vecOrigin, edict_t *pEdict)
{
TraceResult tr;
Vector vecLookerOrigin;
Vector fake_origin;