-
Notifications
You must be signed in to change notification settings - Fork 21
/
util.cpp
1072 lines (821 loc) · 26.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
//
// JK_Botti - be more human!
//
// util.cpp
//
#ifndef _WIN32
#include <string.h>
#endif
#include <extdll.h>
#include <dllapi.h>
#include <h_export.h>
#include <meta_api.h>
#include "usercmd.h"
#include "bot.h"
#include "bot_func.h"
#include "player.h"
#define BREAKABLE_LIST_MAX 1024
extern bot_t bots[32];
extern qboolean is_team_play;
static breakable_list_t *g_breakable_list = NULL;
static breakable_list_t breakable_list_memarray[BREAKABLE_LIST_MAX];
static unsigned int rnd_idnum[2] = {1, 1};
#ifdef __GNUC__
inline void fsincos(double x, double &s, double &c)
{
__asm__ ("fsincos;" : "=t" (c), "=u" (s) : "0" (x) : "st(7)");
}
#else
inline void fsincos(double x, double &s, double &c)
{
s = sin(x);
c = cos(x);
}
#endif
void null_terminate_buffer(char *buf, const size_t maxlen)
{
for(size_t i = 0; i < maxlen; i++)
if(buf[i] == 0)
return;
buf[maxlen-1] = 0;
}
double UTIL_GetSecs(void)
{
#ifdef _WIN32
LARGE_INTEGER count, freq;
count.QuadPart = 0;
freq.QuadPart = 0;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&count);
return (double)count.QuadPart / (double)freq.QuadPart;
#else
struct timeval tv;
gettimeofday (&tv, NULL);
return (double) tv.tv_sec + ((double) tv.tv_usec) / 1000000.0;
#endif
}
/*static float UTIL_WrapAngle360(float angle)
{
// this function returns an angle normalized to the range [0 <= angle < 360]
const unsigned int bits = 0x80000000;
return ((360.0 / bits) * ((int64_t)(angle * (bits / 360.0)) & (bits-1)));
}*/
float UTIL_WrapAngle(float angle)
{
// this function returns an angle normalized to the range [-180 < angle <= 180]
angle += 180.0;
const unsigned int bits = 0x80000000;
angle = -180.0 + ((360.0 / bits) * ((int64_t)(angle * (bits / 360.0)) & (bits-1)));
if(angle == -180.0f)
angle = 180.0;
return(angle);
}
Vector UTIL_WrapAngles(const Vector & angles)
{
// check for wraparound of angles
return Vector( UTIL_WrapAngle(angles.x), UTIL_WrapAngle(angles.y), UTIL_WrapAngle(angles.z) );
}
Vector UTIL_AnglesToForward(const Vector &angles)
{
// from pm_shared/pm_math.h
double sp, cp, pitch, sy, cy, yaw;
pitch = angles.x * (M_PI*2 / 360);
fsincos(pitch, sp, cp);
yaw = angles.y * (M_PI*2 / 360);
fsincos(yaw, sy, cy);
return(Vector(cp*cy, cp*sy, -sp));
}
Vector UTIL_AnglesToRight(const Vector &angles)
{
// from pm_shared/pm_math.h
double sr, cr, roll, sp, cp, pitch, sy, cy, yaw;
pitch = angles.x * (M_PI*2 / 360);
fsincos(pitch, sp, cp);
yaw = angles.y * (M_PI*2 / 360);
fsincos(yaw, sy, cy);
roll = angles.z * (M_PI*2 / 360);
fsincos(roll, sr, cr);
return(Vector(-1*sr*sp*cy+-1*cr*-sy, -1*sr*sp*sy+-1*cr*cy, -1*sr*cp));
}
void UTIL_MakeVectorsPrivate( const Vector &angles, Vector &v_forward, Vector &v_right, Vector &v_up )
{
// from pm_shared/pm_math.h
double sr, cr, roll, sp, cp, pitch, sy, cy, yaw;
pitch = angles.x * (M_PI*2 / 360);
fsincos(pitch, sp, cp);
yaw = angles.y * (M_PI*2 / 360);
fsincos(yaw, sy, cy);
roll = angles.z * (M_PI*2 / 360);
fsincos(roll, sr, cr);
v_forward = Vector(cp*cy, cp*sy, -sp);
v_right = Vector(-1*sr*sp*cy+-1*cr*-sy, -1*sr*sp*sy+-1*cr*cy, -1*sr*cp);
v_up = Vector(cr*sp*cy+-sr*-sy, cr*sp*sy+-sr*cy, cr*cp);
}
Vector UTIL_VecToAngles(const Vector &forward)
{
// from pm_shared/pm_math.h
float tmp, yaw, pitch;
if (unlikely(forward.y == 0) && unlikely(forward.x == 0))
{
yaw = 0;
pitch = (forward.z > 0) ? 90 : -90;
}
else
{
// atan2 returns values in range [-pi < x < +pi]
yaw = (atan2(forward.y, forward.x) * 180 / M_PI);
tmp = sqrt(forward.x * forward.x + forward.y * forward.y);
pitch = (atan2(forward.z, tmp) * 180 / M_PI);
}
return(Vector(pitch, yaw, 0));
}
int UTIL_GetBotIndex(const 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
}
bot_t *UTIL_GetBotPointer(const edict_t *pEdict)
{
int index = UTIL_GetBotIndex(pEdict);
if(index == -1)
return NULL; // return NULL if edict is not a bot
return(&bots[index]);
}
Vector UTIL_AdjustOriginWithExtent(bot_t &pBot, const Vector & v_target_origin, edict_t *pTarget)
{
// mins/maxs are absolute values for SOLID_BSP
if (pTarget->v.solid == SOLID_BSP)
return v_target_origin;
// get smallest extent of bots mins/maxs
float smallest_extent = -pTarget->v.mins[0];
if(-pTarget->v.mins[1] < smallest_extent)
smallest_extent = -pTarget->v.mins[1];
if(-pTarget->v.mins[2] < smallest_extent)
smallest_extent = -pTarget->v.mins[2];
if(pTarget->v.maxs[0] < smallest_extent)
smallest_extent = pTarget->v.maxs[0];
if(pTarget->v.maxs[1] < smallest_extent)
smallest_extent = pTarget->v.maxs[1];
if(pTarget->v.maxs[2] < smallest_extent)
smallest_extent = pTarget->v.maxs[2];
if(smallest_extent <= 0.0f)
return(v_target_origin);
// extent origin towards bot
Vector v_extent_dir = (GetGunPosition(pBot.pEdict) - v_target_origin).Normalize();
return(v_target_origin + v_extent_dir * smallest_extent);
}
/* generates a random 32bit integer */
static unsigned int fast_generate_random(void)
{
rnd_idnum[0] ^= rnd_idnum[1] << 5;
rnd_idnum[0] *= 1664525L;
rnd_idnum[0] += 1013904223L;
rnd_idnum[1] *= 1664525L;
rnd_idnum[1] += 1013904223L;
rnd_idnum[1] ^= rnd_idnum[0] << 3;
return rnd_idnum[0];
}
void fast_random_seed(unsigned int seed)
{
rnd_idnum[0] = seed;
rnd_idnum[1] = ~(seed + 6);
rnd_idnum[1] = fast_generate_random();
}
/* supports range INT_MIN, INT_MAX */
int RANDOM_LONG2(int lLow, int lHigh)
{
const double c_divider = ((unsigned long long)1) << 32; // div by (1<<32)
double rnd;
if(unlikely(lLow >= lHigh))
return(lLow);
rnd = fast_generate_random();
rnd *= (double)lHigh - (double)lLow + 1.0;
rnd /= c_divider; // div by (1<<32)
return (int)(rnd + (double)lLow);
}
float RANDOM_FLOAT2(float flLow, float flHigh)
{
const double c_divider = (((unsigned long long)1) << 32) - 1; // div by (1<<32)-1
double rnd;
if(unlikely(flLow >= flHigh))
return(flLow);
rnd = fast_generate_random();
rnd *= (double)flHigh - (double)flLow;
rnd /= c_divider; // div by (1<<32)-1
return (float)(rnd + (double)flLow);
}
// classic way of getting connected client count without using data collected from 'ClientPutInServer'/'ClientDisconnect'.
int UTIL_GetClientCount(void)
{
int count = 0;
for(int i = 1; i <= gpGlobals->maxClients; i++)
{
edict_t * pClient = INDEXENT(i);
if(!pClient || pClient->free || FNullEnt(pClient) || GETPLAYERUSERID(pClient) <= 0 || STRING(pClient->v.netname)[0] == 0)
continue;
count++;
}
return(count);
}
//
int UTIL_GetBotCount(void)
{
int count = 0;
for(int i = 0; i < 32; i++)
if(bots[i].is_used)
count++;
return(count);
}
//
int UTIL_PickRandomBot(void)
{
int bot_index_list[32];
int num_bots = 0;
for(int i = 0; i < 32; i++)
if(bots[i].is_used) // is this slot used?
bot_index_list[num_bots++] = i;
if(num_bots > 0)
{
if(num_bots == 1)
return(bot_index_list[0]);
int pick = RANDOM_LONG2(0, num_bots-1);
JKASSERT(pick < 0 || pick > num_bots-1);
return(bot_index_list[pick]);
}
return(-1);
}
//
void UTIL_DrawBeam(edict_t *pEnemy, const Vector &start, const Vector &end, int width,
int noise, int red, int green, int blue, int brightness, int speed)
{
if(pEnemy && (ENTINDEX(pEnemy)-1 < 0 || ENTINDEX(pEnemy)-1 >= gpGlobals->maxClients))
return;
if(pEnemy == NULL)
MESSAGE_BEGIN(MSG_ALL, SVC_TEMPENTITY);
else
MESSAGE_BEGIN(MSG_ONE, SVC_TEMPENTITY, NULL, pEnemy);
WRITE_BYTE( TE_BEAMPOINTS);
WRITE_COORD(start.x);
WRITE_COORD(start.y);
WRITE_COORD(start.z);
WRITE_COORD(end.x);
WRITE_COORD(end.y);
WRITE_COORD(end.z);
WRITE_SHORT( m_spriteTexture );
WRITE_BYTE( 1 ); // framestart
WRITE_BYTE( 10 ); // framerate
WRITE_BYTE( 10 ); // life in 0.1's
WRITE_BYTE( width ); // width
WRITE_BYTE( noise ); // noise
WRITE_BYTE( red ); // r, g, b
WRITE_BYTE( green ); // r, g, b
WRITE_BYTE( blue ); // r, g, b
WRITE_BYTE( brightness ); // brightness
WRITE_BYTE( speed ); // speed
MESSAGE_END();
}
//
static breakable_list_t * UTIL_AddFuncBreakable(edict_t *pEdict)
{
int i;
// get end of list
breakable_list_t *prev = NULL;
breakable_list_t *next = g_breakable_list;
while(next)
{
prev = next;
next = next->next;
}
// get unused slot
for(i = 0; i < BREAKABLE_LIST_MAX; i++)
if(!breakable_list_memarray[i].inuse)
break;
if(i >= BREAKABLE_LIST_MAX)
return(NULL);
next = &breakable_list_memarray[i];
memset(next, 0, sizeof(breakable_list_t));
next->inuse = TRUE;
// fill in data
next->next = NULL;
next->material_breakable = FALSE;
next->pEdict = pEdict;
//link end of list next
if(prev)
prev->next = next;
else
g_breakable_list = next;
return(next);
}
typedef enum { matGlass = 0, matWood, matMetal, matFlesh, matCinderBlock, matCeilingTile, matComputer, matUnbreakableGlass, matRocks, matNone, matLastMaterial } Materials;
// called on DispatchKeyValue
void UTIL_UpdateFuncBreakable(edict_t *pEdict, const char * setting, const char * value)
{
// find breakable
breakable_list_t *plist = g_breakable_list;
while(plist)
{
if(plist->pEdict == pEdict)
break;
plist = plist->next;
}
// not found?
if(!plist)
{
// add new
plist = UTIL_AddFuncBreakable(pEdict);
JKASSERT(plist == NULL);
if(!plist)
return;
}
// check if interesting setting
if(FStrEq(setting, "material"))
{
// update data value
plist->material_breakable = (atoi(value) != matUnbreakableGlass);
}
}
// called on ServerDeactivate
void UTIL_FreeFuncBreakables(void)
{
memset(breakable_list_memarray, 0, sizeof(breakable_list_memarray));
g_breakable_list = NULL;
}
//
static breakable_list_t * UTIL_FindBreakable_Internal(breakable_list_t * pbreakable)
{
if(unlikely(!pbreakable))
return(g_breakable_list);
else
return(pbreakable->next);
}
//
breakable_list_t * UTIL_FindBreakable(breakable_list_t * pbreakable)
{
do {
pbreakable = UTIL_FindBreakable_Internal(pbreakable);
if(unlikely(!pbreakable))
return(NULL);
// skip unbreakable glass
if(!pbreakable->material_breakable)
continue;
// skip deleted entities
if(FNullEnt(pbreakable->pEdict) || pbreakable->pEdict->v.health <= 0)
continue;
// skip reused/wrong name entities
} while(!FIsClassname(pbreakable->pEdict, "func_breakable") && !FIsClassname(pbreakable->pEdict, "func_pushable"));
return(pbreakable);
}
//
void SaveAliveStatus(edict_t * pPlayer)
{
int idx;
idx = ENTINDEX(pPlayer) - 1;
if(idx < 0 || idx >= gpGlobals->maxClients)
return;
if(!IsAlive(pPlayer))
players[idx].last_time_dead = gpGlobals->time;
}
//
float UTIL_GetTimeSinceRespawn(edict_t * pPlayer)
{
int idx;
idx = ENTINDEX(pPlayer) - 1;
if(idx < 0 || idx >= gpGlobals->maxClients)
return(-1.0);
if(!IsAlive(pPlayer))
{
//we are dead, so time since respawn is...
return(-1.0);
}
else
{
return(gpGlobals->time - players[idx].last_time_dead);
}
}
//
static qboolean IsPlayerFacingWall(edict_t * pPlayer)
{
TraceResult tr;
Vector v_forward, EyePosition;
EyePosition = pPlayer->v.origin + pPlayer->v.view_ofs;
v_forward = UTIL_AnglesToForward(pPlayer->v.v_angle);
UTIL_TraceLine(EyePosition, EyePosition + gpGlobals->v_forward * 48, ignore_monsters, ignore_glass, pPlayer, &tr);
if (tr.flFraction > 0.999999f)
return(FALSE);
if (DotProduct(gpGlobals->v_forward, tr.vecPlaneNormal) > -0.5f) //60deg
return(FALSE);
return(TRUE);
}
//
void CheckPlayerChatProtection(edict_t * pPlayer)
{
int idx;
idx = ENTINDEX(pPlayer) - 1;
if(idx < 0 || idx >= gpGlobals->maxClients)
return;
// skip bots
if (FBitSet(pPlayer->v.flags, FL_FAKECLIENT) || FBitSet(pPlayer->v.flags, FL_THIRDPARTYBOT))
{
players[idx].last_time_not_facing_wall = gpGlobals->time;
return;
}
// use of any buttons will reset protection
if((pPlayer->v.button & ~(IN_SCORE | IN_DUCK)) != 0)
{
players[idx].last_time_not_facing_wall = gpGlobals->time;
return;
}
// is not facing wall?
if(!IsPlayerFacingWall(pPlayer))
{
players[idx].last_time_not_facing_wall = gpGlobals->time;
return;
}
// This cannot be checked, because if someone accidentally shoots chatter, chatter will move abit -> resets protection
/*
// is moving
if(pPlayer->v.velocity.Length() > 1.0)
{
players[idx].last_time_not_facing_wall = gpGlobals->time;
return;
}*/
}
//
qboolean IsPlayerChatProtected(edict_t * pPlayer)
{
int idx;
idx = ENTINDEX(pPlayer) - 1;
if(idx < 0 || idx >= gpGlobals->maxClients)
return(FALSE);
if(players[idx].last_time_not_facing_wall + 2.0 < gpGlobals->time)
{
return TRUE;
}
return FALSE;
}
void ClientPrint( edict_t *pEntity, int msg_dest, const char *msg_name)
{
if (GET_USER_MSG_ID (PLID, "TextMsg", NULL) <= 0)
REG_USER_MSG ("TextMsg", -1);
MESSAGE_BEGIN( MSG_ONE, GET_USER_MSG_ID (PLID, "TextMsg", NULL), NULL, pEntity );
WRITE_BYTE( msg_dest );
WRITE_STRING( msg_name );
MESSAGE_END();
}
#if 0
static void UTIL_SayText( const char *pText, edict_t *pEdict )
{
if (GET_USER_MSG_ID (PLID, "SayText", NULL) <= 0)
REG_USER_MSG ("SayText", -1);
MESSAGE_BEGIN( MSG_ONE, GET_USER_MSG_ID (PLID, "SayText", NULL), NULL, pEdict );
WRITE_BYTE( ENTINDEX(pEdict) );
WRITE_STRING( pText );
MESSAGE_END();
}
#endif
void UTIL_HostSay( edict_t *pEntity, int teamonly, char *message )
{
int j;
char text[128];
char *pc;
edict_t *client;
char sender_teamstr[MAX_TEAMNAME_LENGTH];
char player_teamstr[MAX_TEAMNAME_LENGTH];
// 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 )
safevoid_snprintf( text, sizeof(text), "%c(TEAM) %s: ", 2, STRING( pEntity->v.netname ) );
else
safevoid_snprintf( text, sizeof(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 (GET_USER_MSG_ID (PLID, "SayText", NULL) <= 0)
REG_USER_MSG ("SayText", -1);
UTIL_GetTeam(pEntity, sender_teamstr, sizeof(sender_teamstr));
client = NULL;
while((client = UTIL_FindEntityByClassname( client, "player" )) != NULL && !FNullEnt(client))
{
if ( client == pEntity ) // skip sender of message
continue;
UTIL_GetTeam(client, player_teamstr, sizeof(player_teamstr));
if ( teamonly && is_team_play && stricmp(sender_teamstr, player_teamstr) != 0 )
continue;
MESSAGE_BEGIN( MSG_ONE, GET_USER_MSG_ID (PLID, "SayText", NULL), NULL, client );
WRITE_BYTE( ENTINDEX(pEntity) );
WRITE_STRING( text );
MESSAGE_END();
}
// print to the sending client
MESSAGE_BEGIN( MSG_ONE, GET_USER_MSG_ID (PLID, "SayText", NULL), NULL, pEntity );
WRITE_BYTE( ENTINDEX(pEntity) );
WRITE_STRING( text );
MESSAGE_END();
// echo to server console
SERVER_PRINT( text );
// write to log file
// team match?
if ( is_team_play )
{
UTIL_LogPrintf( "\"%s<%i><%s><%s>\" %s \"%s\"\n",
STRING( pEntity->v.netname ),
GETPLAYERUSERID( pEntity ),
(*g_engfuncs.pfnGetPlayerAuthId)( pEntity ),
sender_teamstr,
teamonly ? "say_team" : "say",
message );
}
else
{
UTIL_LogPrintf( "\"%s<%i><%s><%i>\" %s \"%s\"\n",
STRING( pEntity->v.netname ),
GETPLAYERUSERID( pEntity ),
(*g_engfuncs.pfnGetPlayerAuthId)( pEntity ),
GETPLAYERUSERID( pEntity ),
teamonly ? "say_team" : "say",
message );
}
}
#ifdef DEBUG
edict_t *DBG_EntOfVars( const entvars_t *pev )
{
if (pev->pContainingEntity != NULL)
return pev->pContainingEntity;
UTIL_ConsolePrintf("%s", "entvars_t pContainingEntity is NULL, calling into engine");
edict_t* pent = (*g_engfuncs.pfnFindEntityByVars)((entvars_t*)pev);
if (pent == NULL)
UTIL_ConsolePrintf("%s", "DAMN! Even the engine couldn't FindEntityByVars!");
((entvars_t *)pev)->pContainingEntity = pent;
return pent;
}
#endif //DEBUG
// return team string 0 through 3 based what MOD uses for team numbers
char * UTIL_GetTeam(edict_t *pEntity, char *teamstr, size_t slen)
{
safe_strcopy(teamstr, slen, INFOKEY_VALUE(GET_INFOKEYBUFFER(pEntity), "model"));
return(teamstr);
}
qboolean FVisible( const Vector &vecOrigin, edict_t *pEdict, edict_t ** pHit )
{
TraceResult tr;
Vector vecLookerOrigin;
if(pHit)
*pHit = NULL;
// look through caller's eyes
vecLookerOrigin = GetGunPosition(pEdict);
int bInWater = (POINT_CONTENTS (vecOrigin) == CONTENTS_WATER);
int bLookerInWater = (POINT_CONTENTS (vecLookerOrigin) == CONTENTS_WATER);
// don't look through water
if (bInWater != bLookerInWater)
return FALSE;
UTIL_TraceLine(vecLookerOrigin, vecOrigin, ignore_monsters, ignore_glass, pEdict, &tr);
if(pHit)
*pHit = tr.pHit;
return(tr.flFraction > 0.999999f);
}
static qboolean FVisibleEnemyOffset( const Vector &vecOrigin, const Vector &vecOffset, edict_t *pEdict, edict_t *pEnemy )
{
edict_t * pHit = NULL;
if(FVisible(vecOrigin + vecOffset, pEdict, &pHit) || (pEnemy != NULL && pHit == pEnemy))
return(TRUE);
if(FNullEnt(pHit))
return(FALSE);
if(!(pHit->v.flags & FL_MONSTER) && !FIsClassname(pHit, "player"))
return(FALSE);
if(!IsAlive (pHit))
return(FALSE);
return(TRUE);
}
qboolean FVisibleEnemy( const Vector &vecOrigin, edict_t *pEdict, edict_t *pEnemy )
{
// only check center if cannot use extra information
if(!pEnemy)
return(FVisibleEnemyOffset( vecOrigin, Vector(0, 0, 0), pEdict, pEnemy ));
if (pEnemy->v.solid != SOLID_BSP) {
// first check for if head is visible
Vector head_offset = Vector(0, 0, pEnemy->v.maxs.z - 6);
if(FVisibleEnemyOffset( vecOrigin, head_offset, pEdict, pEnemy ))
return(TRUE);
// then check if feet are visible
Vector feet_offset = Vector(0, 0, pEnemy->v.mins.z - 6);
if(FVisibleEnemyOffset( vecOrigin, feet_offset, pEdict, pEnemy ))
return(TRUE);
}
// check center
if(FVisibleEnemyOffset( vecOrigin, Vector(0, 0, 0), pEdict, pEnemy ))
return(TRUE);
#if 0
/*
* For long time this part was unintentionally disabled. Everything worked
* just fine. And enabling this appears to increase CPU usage quite abit.
* So keep this disabled after all.
*/
if (pEnemy->v.solid != SOLID_BSP) {
// construct sideways vector
Vector v_right = UTIL_AnglesToRight(UTIL_VecToAngles(vecOrigin - GetGunPosition(pEdict)));
// check if right side of player is visible
Vector right_offset = v_right * (pEnemy->v.maxs.x - 4);
if(FVisibleEnemyOffset( vecOrigin, right_offset, pEdict, pEnemy ))
return(TRUE);
// check if left side of player is visible
Vector left_offset = v_right * (pEnemy->v.mins.x - 4);
if(FVisibleEnemyOffset( vecOrigin, left_offset, pEdict, pEnemy ))
return(TRUE);
}
#endif
return(FALSE);
}
qboolean FInShootCone(const Vector & Origin, edict_t *pEdict, float distance, float diameter, float min_angle)
{
/*
<----- distance ---->
___....->T^ ^
...----'''' | <- radius |
O ------------------->Qv |
^ '''----....___ | <- diameter
| ''''-> v
|
Bot(pEdict)
T: Target (Origin)
if angle Q-O-T is less than min_angle, always return true.
*/
if(distance < 0.01)
return TRUE;
// angle between forward-view-vector and vector to player (as cos(angle))
float flDot = DotProduct( (Origin - (pEdict->v.origin + pEdict->v.view_ofs)).Normalize(), UTIL_AnglesToForward(pEdict->v.v_angle) );
if(flDot > cos(deg2rad(min_angle))) // smaller angle, bigger cosine
return TRUE;
Vector2D triangle;
triangle.x = distance;
triangle.y = diameter / 2.0;
// full angle of shootcode at this distance (as cos(angle))
if(flDot > (distance / triangle.Length())) // smaller angle, bigger cosine
return TRUE;
return FALSE;
}
void UTIL_SelectWeapon(edict_t *pEdict, int weapon_index)
{
usercmd_t user;
user.lerp_msec = 0;
user.msec = 0;
user.viewangles = pEdict->v.v_angle;
user.forwardmove = 0;
user.sidemove = 0;
user.upmove = 0;
user.lightlevel = 127;
user.buttons = 0;
user.impulse = 0;
user.weaponselect = weapon_index;
user.impact_index = 0;
user.impact_position = Vector(0, 0, 0);
MDLL_CmdStart(pEdict, &user, 0);
MDLL_CmdEnd(pEdict);
}
void UTIL_BuildFileName_N(char *filename, int size, char *arg1, char *arg2)
{
const char * mod_dir = (submod_id == SUBMOD_OP4) ? "gearbox" : "valve";
if ((arg1 != NULL) && (arg2 != NULL))
{
if (*arg1 && *arg2)
{
safevoid_snprintf(filename, size, "%s/%s/%s", mod_dir, arg1, arg2);
return;
}
}
if (arg1 != NULL)
{
if (*arg1)
{
safevoid_snprintf(filename, size, "%s/%s", mod_dir, arg1);
return;
}
}
safevoid_snprintf(filename, size, "%s/", mod_dir);
return;
}
//=========================================================
// UTIL_LogPrintf - Prints a logged message to console.
// Preceded by LOG: ( timestamp ) < message >
//=========================================================
void UTIL_LogPrintf( char *fmt, ... )
{
va_list argptr;
char string[1024];
va_start( argptr, fmt );
safevoid_vsnprintf( string, sizeof(string), fmt, argptr );
va_end( argptr );
// Print to server console
ALERT( at_logged, "%s", string );
}
#if 0
static void UTIL_ServerPrintf( char *fmt, ... )
{
va_list argptr;
char string[512];
va_start( argptr, fmt );
safevoid_vsnprintf( string, sizeof(string), fmt, argptr );
va_end( argptr );
// Print to server console
SERVER_PRINT( string );
}
#endif
void UTIL_ConsolePrintf( char *fmt, ... )
{
va_list argptr;
char string[512];
size_t len;
strcpy(string, "[jk_botti] ");
len = strlen(string);
va_start( argptr, fmt );
safevoid_vsnprintf( string+len, sizeof(string)-len, fmt, argptr );
va_end( argptr );
// end msg with newline if not already
len = strlen(string);
if(string[len-1] != '\n')
{
if(len < sizeof(string)-2)// -1 null, -1 for newline
strcat(string, "\n");
else
string[len-1] = '\n';
}
// Print to server console
SERVER_PRINT( string );
}
void UTIL_AssertConsolePrintf(const char *file, const char *str, int line)
{
UTIL_ConsolePrintf("[ASSERT] '%s' : '%s' : 'line %d'", file, str, line);
__asm__ ("int $3");
}