-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdll.cpp
2960 lines (2450 loc) · 89.2 KB
/
dll.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
///////////////////////////////////////////////////////////////////////////////////////////////
//
// -- GNU -- open source
// 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)
//
//
// dll.cpp
//
////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(WIN32)
#pragma warning(disable: 4005 91 4477)
#endif
#include "defines.h"
// for new config system and map learning by Andrey Kotrekhov
#include "Config.h"
using std::string;
#include "extdll.h"
#include "enginecallback.h"
#include "util.h"
#include "cbase.h"
#include "entity_state.h"
#include "bot.h"
#include "bot_func.h"
#include "bot_manager.h"
#include "client_commands.h"
#include "waypoint.h"
Section *conf_weapons = nullptr;
extern "C"
{
#include <cstdio>
};
const int SERVER_CMD_LEN = 80;
extern GETENTITYAPI other_GetEntityAPI;
extern GETNEWDLLFUNCTIONS other_GetNewDLLFunctions;
extern enginefuncs_t g_engfuncs;
extern int debug_engine;
extern globalvars_t *gpGlobals;
extern char *g_argv;
extern char wpt_author[32];
extern char wpt_modified[32];
extern botname_t bot_names[MAX_BOT_NAMES]; // array of all names read from external file
bot_weapon_select_t bot_weapon_select[MAX_WEAPONS]; // array of all weapons the bot can use
bot_fire_delay_t bot_fire_delay[MAX_WEAPONS]; // their delays between two shots
static FILE *fp;
DLL_FUNCTIONS other_gFunctionTable;
DLL_GLOBAL const Vector g_vecZero = Vector(0,0,0);
externals_t externals;
internals_t internals;
botmanager_t botmanager;
botdebugger_t botdebugger;
int client_t::humans_num;
int client_t::bots_num;
client_t clients[MAX_CLIENTS];
int m_spriteTexture = 0;
int m_spriteTexturePath1 = 0;
int m_spriteTexturePath2 = 0;
int m_spriteTexturePath3 = 0;
bool is_dedicated_server = FALSE; // TRUE if the server is a dedicated server
Section* conf = nullptr; // for new config system by Andrey Kotrekhov
//edict_t *pent_info_firearms_detect = NULL; // NOT USED
edict_t *listenserver_edict = nullptr;
edict_t *pRecipient = nullptr; // the one who write the ClintCommand
edict_t *g_debug_bot = nullptr; // pointer on a single bot we want to debug (not all actions are logged, add the code to those that are needed at the moment)
bool g_debug_bot_on = FALSE; // do we debug a single bot?
#ifdef NOFAMAS
char mb_version_info[32] = "0.94b_noFamas-[APG]";
#else
char mb_version_info[32] = "0.94b-[APG]"; // holds MarineBot version string
#endif
// Marine Bot doesn't use these yet and most probably will never spam the game with these
char bot_whine[MAX_BOT_WHINE][81];
int whine_count;
int recent_bot_whine[5];
// following variables are used only in this file
bool Dedicated_Server_Init = FALSE; // ensures only one run of DS init
bool g_GameRules = FALSE;
int isFakeClientCommand = 0;
int fake_arg_count;
bool read_whole_cfg = TRUE; // allows to read the whole configuration file after map change
bool using_default_cfg = TRUE; // is FALSE when we changed to some map specific .cfg
bool need_to_open_cfg = TRUE;
float bot_cfg_pause_time = 0.0f;
float respawn_time = 0.0f;
bool spawn_time_reset = FALSE;
int num_bots = 0;
int prev_num_bots = 0;
bool override_max_bots = FALSE;
float f_update_wpt_time; // allows us to update waypoints in real-time based on latest game events or changes made by the waypointer
const float wpt_autosave_delay = 90.0f; // constant delay between two attempts to automatic waypoints save
float wpt_autosave_time = 0.0f; // holds time of the next attempt to automatic waypoints save
char presentation_msg[] = "This server runs Marine Bot in version";
float check_send_info = 0.0f; // send message checks
float presentation_time = 0.0f; // holds the time of last presentation
bool welcome_sent = FALSE; // Marine Bot welcome messages (fancy HUD messages on Listen server and simple one sentence text to Dedicated server console)
bool welcome2_sent = FALSE;
bool welcome3_sent = FALSE; // special that shows waypoints authors
char welcome_msg[] = "reporting for duty!\nWrite \"help\" or \"?\" into console to show console help";
char welcome2_msg[] = "Visit our web page at:\nhttp://www.marinebot.xf.cz";
float welcome_time = 0.0f;
bool error_occured = FALSE; // TRUE when fatal error was found during initialization (missing .cfg file)
bool warning_event = FALSE; // TRUE when there was some non-fatal error found like missing waypoints
bool override_reset = FALSE; // to prevent resetting both flags (eg. there is error in DLLInit and we need to print it)
char hud_error_msg[1024]; // the error/warning message that will be printed using Listen Server HUD message system
float hud_error_msg_time = 0.0f; // holds the time the error message is printed
// few function prototypes used in this file
void GameDLLInit();
void UpdateClientData(const struct edict_s *ent, int sendweapons, struct clientdata_s *cd);
void ProcessBotCfgFile(Section *conf); // for new config system by Andrey Kotrekhov
void MBServerCommand(); // Dedicated server console commands
void GameDLLInit()
{
int i;
(*g_engfuncs.pfnAddServerCommand) ("m_bot", MBServerCommand);
// is dedicated server
if (IS_DEDICATED_SERVER())
is_dedicated_server = TRUE;
for (i = 0; i < MAX_BOT_NAMES; i++)
{
bot_names[i].name;
bot_names[i].is_used = FALSE;
}
// whines aren't used at all ... probably useless and will be removed
whine_count = 0;
for (i=0; i < 5; i++)
recent_bot_whine[i] = -1;
BotNameInit();
// we need to initialize this mod weapon IDs
if (InitFAWeapons() == FALSE)
{
// TODO: Print some error message, use a hud message for this
}
// initialize the weapon arrays
memset(bot_weapon_select, 0, sizeof(bot_weapon_select));
memset(bot_fire_delay, 0, sizeof(bot_fire_delay));
//kota@ we should read weapon configuration from the file.
char msg[1024];
char filename[1024];
char FA_version_string[16];
// we need to use string version here
if (g_mod_version == FA_30)
strcpy(FA_version_string,"3_0");
else if (g_mod_version == FA_29)
strcpy(FA_version_string,"2_9");
else if (g_mod_version == FA_28)
strcpy(FA_version_string,"2_8");
else if (g_mod_version == FA_27)
strcpy(FA_version_string,"2_7");
else if (g_mod_version == FA_26)
strcpy(FA_version_string,"2_6");
else if (g_mod_version == FA_25)
strcpy(FA_version_string,"2_5");
else if (g_mod_version == FA_24)
strcpy(FA_version_string,"2_4");
UTIL_MarineBotFileName(filename, "weapons", FA_version_string);
sprintf(msg, "Executing %s\n", filename);
ALERT( at_console, msg );
conf_weapons = parceConfig(filename);
if (conf_weapons == nullptr)
{
sprintf(msg, "There is a syntax error in %s, or the file cant be found\n", filename);
PrintOutput(nullptr, msg, MType::msg_error);
sprintf(msg, "Bots will not shoot\n");
PrintOutput(nullptr, msg, MType::msg_warning);
// show also this error message through the hud once client join game
if (warning_event == FALSE)
{
// prepare error message
strcpy(hud_error_msg, "WARNING - MarineBot detected an error: weapon definitions!\nThere is a syntax error or the file can't be found\nBots will not shoot");
// to know that something bad happened
warning_event = TRUE;
// we need to prevent resetting of this error in DispatchSpawn()
override_reset = TRUE;
}
}
BotWeaponArraysInit(conf_weapons);
// initialize the bots array
bots = new bot_t[MAX_CLIENTS];
(*other_gFunctionTable.pfnGameInit)();
}
int DispatchSpawn( edict_t *pent )
{
if (gpGlobals->deathmatch)
{
char *pClassname = const_cast<char*>(STRING(pent->v.classname));
#ifdef _DEBUG
if (debug_engine)
{
fp=fopen(debug_fname,"a");
fprintf(fp, "DispatchSpawn: %p %s\n", pent, pClassname);
if (pent->v.model != 0)
fprintf(fp, " model=%s\n",STRING(pent->v.model));
fclose(fp);
}
#endif
// this method is the first method that's being called on map change
// so do level initialization stuff here
if (strcmp(pClassname, "worldspawn") == 0)
{
/*/
#ifdef _DEBUG
fp=fopen("!mb_engine_debug.txt","a");
fprintf(fp, "\n======================\n\n<dll.cpp> Dispatchspawn() - worldspawn on %s\n",
STRING(gpGlobals->mapname));
fclose(fp);
#endif
/**/
// set these internal variables back to defaults when the map changed to a new one
// or the user restarted actual map
// normally this should have been at the beginning of Start Frame method
// but Start Frame gets called after Dispatch Spawn
// which means that in Start Frame we would reset variables (like
// enemy distance limit for example)
// that we've already set here in Dispatch Spawn so it has to be here
internals.ResetOnMapChange();
// clear signatures first
strcpy(wpt_author, "unknown");
strcpy(wpt_modified, "unknown");
WaypointInit();
// reset error messaging system only if allowed to do so
if (override_reset == FALSE)
{
// prepare new error message header
strcpy(hud_error_msg, "WARNING - MarineBot detected an error");
// reset detected problems
error_occured = FALSE;
warning_event = FALSE;
}
int result = WaypointLoad(nullptr, nullptr);
// if the waypoint file doesn't exist switch to the other directory and check again
if (result == -10)
{
if (internals.IsCustomWaypoints())
internals.ResetIsCustomWaypoints();
else
internals.SetIsCustomWaypoints(true);
PrintOutput(nullptr, "There is invalid or missing waypoint file for this map. ",
MType::msg_error);
PrintOutput(nullptr, "Checking the other waypoint directory\n", MType::msg_info);
result = WaypointLoad(nullptr, nullptr);
//TODO: Switch back to default wpts directory if there are no wpts in custom
}
// if old waypoints are detected try convert them automatically
if (result == -1)
WaypointLoadUnsupported(nullptr);
// was there any other problem
else if ((result == 0) || (result == -10))
{
// print error message directly into DS console
if (is_dedicated_server)
{
PrintOutput(nullptr, "There is invalid or missing waypoint file for this map\n", MType::msg_error);
PrintOutput(nullptr, "Bots may play incorrectly\n", MType::msg_warning);
}
else
{
// show the error message once client join
if (warning_event == FALSE)
{
strcat(hud_error_msg, ": waypoints!\nThere is invalid or missing waypoint file for this map\nBots may play incorrectly");
warning_event = TRUE;
}
}
}
else
PrintOutput(nullptr, "Loading waypoints...\n", MType::msg_info);
// load waypoint paths
result = WaypointPathLoad(nullptr, nullptr);
// if old waypoint paths are detected try convert them automatically
if (result == -1)
WaypointPathLoadUnsupported(nullptr);
else if (result == 0)
{
if (is_dedicated_server)
{
PrintOutput(nullptr, "There is invalid or missing path waypoint file for this map\n", MType::msg_error);
PrintOutput(nullptr, "Bots may play incorrectly\n", MType::msg_warning);
}
else
{
if (warning_event == FALSE)
{
strcat(hud_error_msg, ": paths!\nThere is invalid or missing path waypoint file for this map\nBots may play incorrectly");
warning_event = TRUE;
}
}
}
// pent_info_firearms_detect = NULL; // @@@@ TEMP: only temp don't know if we use it!!!
PRECACHE_SOUND("weapons/xbow_hit1.wav"); // waypoint add
PRECACHE_SOUND("weapons/mine_activate.wav"); // waypoint delete
PRECACHE_SOUND("common/wpn_hudoff.wav"); // path add/delete start
PRECACHE_SOUND("common/wpn_moveselect.wav"); // path add/delete cancel
PRECACHE_SOUND("plats/elevbell1.wav"); // snd_done
PRECACHE_SOUND("buttons/button10.wav"); // snd_failed
m_spriteTexture = PRECACHE_MODEL("sprites/lgtning.spr"); // the waypoint beam
m_spriteTexturePath1 = PRECACHE_MODEL("sprites/zbeam6.spr");// the one-way path beam
m_spriteTexturePath2 = m_spriteTexture; // other path types do use same beam as waypoint
m_spriteTexturePath3 = PRECACHE_MODEL("sprites/rope.spr");// for paths with additional flags (avoid & ignore enemy and such like)
f_update_wpt_time = 0.0;
g_GameRules = TRUE;
// see if this map is one of maps where the bots can snipe through skybox
char mapname[64];
strcpy(mapname, STRING(gpGlobals->mapname));
if (strcmp(mapname, "ps_island") == 0)
{
internals.SetIsEnemyDistanceLimit(true);
internals.SetEnemyDistanceLimit(3000);
}
bot_cfg_pause_time = 0.0f;
respawn_time = 0.0f;
spawn_time_reset = FALSE;
prev_num_bots = num_bots;
num_bots = 0;
override_max_bots = FALSE;
botmanager.SetBotCheckTime(gpGlobals->time + 30.0f);
}
}
return (*other_gFunctionTable.pfnSpawn)(pent);
}
void DispatchThink( edict_t *pent )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"DispatchThink:\n"); fclose(fp); }
(*other_gFunctionTable.pfnThink)(pent);
}
void DispatchUse( edict_t *pentUsed, edict_t *pentOther )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"DispatchUse:\n"); fclose(fp); }
(*other_gFunctionTable.pfnUse)(pentUsed, pentOther);
}
void DispatchTouch( edict_t *pentTouched, edict_t *pentOther )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"DispatchTouch:\n"); fclose(fp); }
(*other_gFunctionTable.pfnTouch)(pentTouched, pentOther);
}
void DispatchBlocked( edict_t *pentBlocked, edict_t *pentOther )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"DispatchBlocked:\n"); fclose(fp); }
(*other_gFunctionTable.pfnBlocked)(pentBlocked, pentOther);
}
void DispatchKeyValue( edict_t *pentKeyvalue, KeyValueData *pkvd )
{
#ifdef _DEBUG
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp, "DispatchKeyValue: %p %s=%s\n", pentKeyvalue, pkvd->szKeyName, pkvd->szValue); fclose(fp); }
#endif
//static edict_t *temp_pent;
//static int flag_index;
/*
if (pentKeyvalue == pent_info_firearms_detect)
{
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp, "DispatchKeyValue: %x %s=%s\n",pentKeyvalue,pkvd->szKeyName,pkvd->szValue); fclose(fp); }
}
else if (pent_info_firearms_detect == NULL)
pent_info_firearms_detect = pentKeyvalue;
*/
(*other_gFunctionTable.pfnKeyValue)(pentKeyvalue, pkvd);
}
void DispatchSave( edict_t *pent, SAVERESTOREDATA *pSaveData )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"DispatchSave:\n"); fclose(fp); }
(*other_gFunctionTable.pfnSave)(pent, pSaveData);
}
int DispatchRestore( edict_t *pent, SAVERESTOREDATA *pSaveData, int globalEntity )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"DispatchRestore:\n"); fclose(fp); }
return (*other_gFunctionTable.pfnRestore)(pent, pSaveData, globalEntity);
}
void DispatchObjectCollsionBox( edict_t *pent )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"DispatchObjectCollsionBox:\n"); fclose(fp); }
(*other_gFunctionTable.pfnSetAbsBox)(pent);
}
void SaveWriteFields( SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"SaveWriteFields:\n"); fclose(fp); }
(*other_gFunctionTable.pfnSaveWriteFields)(pSaveData, pname, pBaseData, pFields, fieldCount);
}
void SaveReadFields( SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"SaveReadFields:\n"); fclose(fp); }
(*other_gFunctionTable.pfnSaveReadFields)(pSaveData, pname, pBaseData, pFields, fieldCount);
}
void SaveGlobalState( SAVERESTOREDATA *pSaveData )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"SaveGlobalState:\n"); fclose(fp); }
(*other_gFunctionTable.pfnSaveGlobalState)(pSaveData);
}
void RestoreGlobalState( SAVERESTOREDATA *pSaveData )
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"RestoreGlobalState:\n"); fclose(fp); }
(*other_gFunctionTable.pfnRestoreGlobalState)(pSaveData);
}
void ResetGlobalState()
{
//if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"ResetGlobalState:\n"); fclose(fp); }
(*other_gFunctionTable.pfnResetGlobalState)();
}
BOOL ClientConnect( edict_t *pEntity, const char *pszName, const char *pszAddress, char szRejectReason[ 128 ] )
{
if (gpGlobals->deathmatch)
{
#ifdef _DEBUG
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp, "ClientConnect: pent=%p name=%s address=%s\n", pEntity, pszName, pszAddress); fclose(fp); }
#endif
// check if this client is the listen server client
if (strcmp(pszAddress, "loopback") == 0)
{
// save the edict of the listen server client...
listenserver_edict = pEntity;
}
// check if this is NOT a bot joining the server...
if (strcmp(pszAddress, "127.0.0.1") != 0)
{
// don't try to add bots for 60 seconds, give client time to get added
botmanager.SetBotCheckTime(gpGlobals->time + 60.0f);
// if there are currently more than the minimum number of bots running AND
// there's also more than max_bots clients on the server
// then kick one of the bots off the server
// do this only on dedicated server
if ((is_dedicated_server) && (clients[0].BotCount() > 0) &&
(clients[0].BotCount() > externals.GetMinBots()) &&
(externals.GetMinBots() != -1) &&
(clients[0].ClientCount() > externals.GetMaxBots()) &&
(externals.GetMaxBots() != -1))
{
for (int i = 0; i < MAX_CLIENTS; i++)
{
// is this slot used?
if (bots[i].is_used)
{
char cmd[80];
sprintf(cmd, "kick \"%s\"\n", bots[i].name);
SERVER_COMMAND(cmd); // kick the bot using (kick "name")
break;
}
}
}
}
}
return (*other_gFunctionTable.pfnClientConnect)(pEntity, pszName, pszAddress, szRejectReason);
}
void ClientDisconnect( edict_t *pEntity )
{
if (gpGlobals->deathmatch)
{
int i;
#ifdef _DEBUG
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp, "ClientDisconnect: %p\n", pEntity); fclose(fp); }
#endif
i = 0;
while ((i < MAX_CLIENTS) && (clients[i].pEntity != pEntity))
i++;
if (i < MAX_CLIENTS)
{
if (clients[i].pEntity->v.flags & FL_FAKECLIENT)
clients[i].substr_bot();
else
clients[i].substr_human();
clients[i].pEntity = nullptr;
clients[i].SetHuman(FALSE);
clients[i].SetBleeding(FALSE);
}
/*
#ifdef _DEBUG
///@@@@@@@@@@@@@@@@@@@@22
char msg[128];
sprintf(msg, "***dll.cpp|ClientDiconnect() - total number of clients: %d\n", clients[0].ClientCount());
PrintOutput(NULL, msg, msg_null);
#endif
/**/
for (i = 0; i < MAX_CLIENTS; i++)
{
if (bots[i].pEdict == pEntity)
{
// someone kicked this bot off of the server...
bots[i].is_used = FALSE; // this slot is now free to use
bots[i].kick_time = gpGlobals->time; // save the kicked time
// try to find the name this bot used and sign it free
for (int j = 0; j < MAX_BOT_NAMES; j++)
{
if (strstr(bots[i].name, bot_names[j].name) != nullptr)
{
// this clients name is free again
bot_names[j].is_used = FALSE;
}
}
break;
}
}
// check if any other bot is aiming at this one, if so clear it
for (i = 0; i < MAX_CLIENTS; i++)
{
if (bots[i].is_used == FALSE)
continue;
if (bots[i].pEdict == pEntity)
continue;
// so NULL its enemy
if (bots[i].pBotEnemy == pEntity)
{
bots[i].BotForgetEnemy();
}
}
}
// remove the fakeclient bit before kicking the bot
if (pEntity->v.flags & FL_FAKECLIENT)
{
pEntity->v.flags &= ~FL_FAKECLIENT;
(*other_gFunctionTable.pfnClientDisconnect)(pEntity);
}
else
(*other_gFunctionTable.pfnClientDisconnect)(pEntity);
}
void ClientKill( edict_t *pEntity )
{
#ifdef _DEBUG
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp, "ClientKill: %p\n", pEntity); fclose(fp); }
#endif
(*other_gFunctionTable.pfnClientKill)(pEntity);
}
void ClientPutInServer( edict_t *pEntity )
{
#ifdef _DEBUG
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp, "ClientPutInServer: %p\n", pEntity); fclose(fp); }
#endif
int i = 0;
while ((i < MAX_CLIENTS) && (clients[i].pEntity != nullptr))
i++;
if (i < MAX_CLIENTS)
{
clients[i].pEntity = pEntity; // store this clients edict in the clients array
if (!(pEntity->v.flags & FL_FAKECLIENT))
{
clients[i].SetHuman(TRUE);
clients[i].add_human();
}
else
{
clients[i].SetHuman(FALSE);
clients[i].add_bot();
}
}
/*
#ifdef _DEBUG
//@@@@@@@@@@@@@@@@@@@@22
char msg[128];
sprintf(msg, "***dll.cpp|ClientPutInServer() - adding client %s | total number of clients: %d\n",
STRING(pEntity->v.netname), clients[0].ClientCount());
PrintOutput(NULL, msg, msg_null);
//UTIL_DebugInFile(msg);
#endif
/**/
(*other_gFunctionTable.pfnClientPutInServer)(pEntity);
}
void ClientCommand( edict_t *pEntity )
{
const char *pcmd = Cmd_Argv(0);
const char *arg1 = Cmd_Argv(1);
const char *arg2 = Cmd_Argv(2);
const char *arg3 = Cmd_Argv(3);
const char *arg4 = Cmd_Argv(4);
const char *arg5 = Cmd_Argv(5);
// save the ClCommand author if it is not a bot
if (!(pEntity->v.flags & FL_FAKECLIENT))
pRecipient = pEntity;
/*/
//@@@@@@@@@@@@@@@
if (pEntity != listenserver_edict)
ALERT(at_console, "ClientCommand:%s (arg1:%s)(arg2:%s)(arg3:%s)(arg4:%s)(arg5:%s)\n",
pcmd,arg1,arg2,arg3,arg4,arg5);
/**/
if (debug_engine)
{
char edict_name[32];
strcpy(edict_name, STRING(pEntity->v.netname));
fp=fopen(debug_fname,"a"); fprintf(fp,"%s's ClientCommand: %s ",edict_name,pcmd);
if ((arg1 != nullptr) && (*arg1 != 0))
fprintf(fp," %s", arg1);
if ((arg2 != nullptr) && (*arg2 != 0))
fprintf(fp," %s", arg2);
if ((arg3 != nullptr) && (*arg3 != 0))
fprintf(fp," %s", arg3);
if ((arg4 != nullptr) && (*arg4 != 0))
fprintf(fp," %s", arg4);
if ((arg5 != nullptr) && (*arg5 != 0))
fprintf(fp," %s", arg5);
fprintf(fp, " (gametime=%.3f)\n", gpGlobals->time);
fclose(fp);
}
// only allow custom commands if deathmatch mode and NOT dedicated server and
// client sending command is the listen server client...
if ((gpGlobals->deathmatch) && (!is_dedicated_server) && (pEntity == listenserver_edict))
{
if (CustomClientCommands(pEntity, pcmd, arg1, arg2, arg3, arg4, arg5))
return;
}
(*other_gFunctionTable.pfnClientCommand)(pEntity);
}
void ClientUserInfoChanged( edict_t *pEntity, char *infobuffer )
{
#ifdef _DEBUG
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp, "ClientUserInfoChanged: pEntity=%p infobuffer=%s\n", pEntity, infobuffer); fclose(fp); }
#endif
/*/
#ifdef _DEBUG
//@@@@@@@@@@@@@@@
ALERT(at_console, "ClientUserInfoChanged: pEntity=%x infobuffer=%s\n", pEntity, infobuffer);
#endif
/**/
(*other_gFunctionTable.pfnClientUserInfoChanged)(pEntity, infobuffer);
}
void ServerActivate( edict_t *pEdictList, int edictCount, int clientMax )
{
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"ServerActivate: edictCount%d clientMax%d\n", edictCount, clientMax); fclose(fp); }
(*other_gFunctionTable.pfnServerActivate)(pEdictList, edictCount, clientMax);
if (debug_engine) { fp=fopen(debug_fname,"a"); fprintf(fp,"ServerActivate(engine-returned): edictCount%d clientMax%d\n", edictCount, clientMax); fclose(fp); }
}
void ServerDeactivate()
{
(*other_gFunctionTable.pfnServerDeactivate)();
}
void PlayerPreThink( edict_t *pEntity )
{
(*other_gFunctionTable.pfnPlayerPreThink)(pEntity);
}
void PlayerPostThink( edict_t *pEntity )
{
(*other_gFunctionTable.pfnPlayerPostThink)(pEntity);
}
void StartFrame()
{
if (gpGlobals->deathmatch)
{
edict_t *pPlayer;
static int i, index, player_index, bot_index;
static float previous_time = -1.0f;
static float client_update_time = 0.0f;
clientdata_s cd;
char msg[256];
int count;
bool round_ended = FALSE;
// if a new map has started then (MUST BE FIRST IN StartFrame)...
// this statement will be called on 2nd map load or after the user used commands like
// 'restart' to load your current map again or 'map <mapname>' to change map
// the 1st map (after 'create a game' in the main menu) doesn't call this statement
// constructor defaults are used there
if ((gpGlobals->time + 0.1) < previous_time)
{
char filename[256];
char mapname[64];
bot_t::harakiri_moment = 0.0;
// if automatic teams balancing is enabled reset its time at start of new map
// changed by kota@
if (botmanager.IsTeamsBalanceNeeded() && is_dedicated_server)
botmanager.SetTimeOfTeamsBalanceCheck(gpGlobals->time + externals.GetBalanceTime());
// turn off teams balance override (ie do teams balance checks again if it is enabled)
botmanager.ResetOverrideTeamsBalance();
// if info message autosending is enabled reset its time at start of new map
if ((check_send_info != -1.0) && (is_dedicated_server))
check_send_info = gpGlobals->time + externals.GetInfoTime();
// turn off all waypoints show & auto adding commands
// to prevent engine overloading
wptser.ResetOnMapChange();
pRecipient = nullptr;
// reset welcome messages with new map
welcome_time = 0.0f;
welcome_sent = FALSE;
welcome2_sent = FALSE;
welcome3_sent = FALSE;
// reset error warnings time
// (the message itself is cleared right after it has been displayed)
hud_error_msg_time = 0.0f;
// show the presentation after some time from map change
presentation_time = gpGlobals->time + 90.0f;
// check if mapname_marine.cfg file exists ie are there any
// specific settings & classes for this map
strcpy(mapname, STRING(gpGlobals->mapname));
strcat(mapname, "_marine.cfg");
UTIL_MarineBotFileName(filename, "mapcfgs", mapname);
FILE *temp_fp = fopen(filename, "r");
// check if the map specific .cfg exists
if (temp_fp != nullptr)
{
// we don't need the file so we should close it
fclose(temp_fp);
// forces opening configuration file
need_to_open_cfg = TRUE;
// mark the bots "fully kicked" ie. no auto respawn at the start on new map
for (index = 0; index < MAX_CLIENTS; index++)
{
bots[index].is_used = FALSE;
bots[index].respawn_state = 0;
bots[index].kick_time = 0.0;
}
}
// otherwise we are using default .cfg file ("marine.cfg")
else
{
// there was map specific .cfg for the previous map, but there's none
// for this map so we have to read the default .cfg again
if (using_default_cfg == FALSE)
{
need_to_open_cfg = TRUE;
for (index = 0; index < MAX_CLIENTS; index++)
{
bots[index].is_used = FALSE;
bots[index].respawn_state = 0;
bots[index].kick_time = 0.0;
}
}
// otherwise we are still using the same default .cfg
// so we have to just respawn existing bots
else
{
count = 0;
// mark the bots as needing to be respawned...
for (index = 0; index < MAX_CLIENTS; index++)
{
if (count >= prev_num_bots)
{
bots[index].is_used = FALSE;
bots[index].respawn_state = 0;
bots[index].kick_time = 0.0f;
}
if (bots[index].is_used) // is this slot used?
{
bots[index].respawn_state = RESPAWN_NEED_TO_RESPAWN;
count++;
}
// check for any bots that were very recently kicked...
if ((bots[index].kick_time + 5.0f) > previous_time)
{
bots[index].respawn_state = RESPAWN_NEED_TO_RESPAWN;
count++;
}
else
bots[index].kick_time = 0.0f; // reset to prevent false spawns later
}
}
}
// set the respawn time
if (is_dedicated_server)
respawn_time = gpGlobals->time + 5.0f;
else
respawn_time = gpGlobals->time + 20.0f;
// start updating client data again
client_update_time = gpGlobals->time + 10.0f;
botmanager.SetBotCheckTime(gpGlobals->time + 30.0f);
}
// listen server
if (!is_dedicated_server)
{
if ((listenserver_edict != nullptr) && IsAlive(listenserver_edict))
{
// we found some fatal error so set error message time
if ((error_occured) && (hud_error_msg_time < 1.0f))
hud_error_msg_time = gpGlobals->time + 2.0f;
// or we found only some non-fatal error
// so set warning time and also delay welcome
else if ((warning_event) && (hud_error_msg_time < 1.0f))
{
hud_error_msg_time = gpGlobals->time + 2.0f;
welcome_time = hud_error_msg_time + 16.0f;
}
// otherwise all went fine so set standard welcome message time
else if ((error_occured == FALSE) && (warning_event == FALSE) &&
(welcome_sent == FALSE) && (welcome_time < 1.0f))
welcome_time = gpGlobals->time + 2.0f;
}
// is it time to print the error message
if (((error_occured) || (warning_event)) &&
(hud_error_msg_time > 0.0f) && (hud_error_msg_time < gpGlobals->time))
{
// send the error message to clients
Vector color1 = Vector(250, 50, 0);
Vector color2 = Vector(255, 0, 20);
CustHudMessageToAll(hud_error_msg, color1, color2, 2, 10);
// reset the error message back to default
strcpy(hud_error_msg, "WARNING - MarineBot detected an error");
// clear it, the message must have been already printed
override_reset = FALSE;
// clear this so we only do it once
if (warning_event)
warning_event = FALSE;
if (error_occured)
{
error_occured = FALSE;
// prevents to show standard welcome messages
welcome_sent = TRUE;
welcome2_sent = TRUE;
welcome3_sent = TRUE;
}
}
else if ((welcome_sent == FALSE) &&
(welcome_time > 0.0) && (welcome_time < gpGlobals->time))
{
char hi_msg[256];
sprintf(hi_msg, "MarineBot %s %s", mb_version_info, welcome_msg);
// let's send a welcome message to client