-
Notifications
You must be signed in to change notification settings - Fork 19
/
dll.cpp
2174 lines (1862 loc) · 79 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
/**
* 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_func.h"
#include "IniParser.h"
#include "game.h" // GAME CLASS
#include "NodeMachine.h" // NodeMachine
#include "ChatEngine.h"
// this makes sure function `min` is available (instead of fmin).
#include <algorithm>
using namespace std;
DLL_FUNCTIONS gFunctionTable;
DLL_FUNCTIONS gFunctionTable_post;
enginefuncs_t g_engfuncs;
globalvars_t *gpGlobals;
char g_argv[1024];
extern cBot bots[32];
extern bool radio_message;
extern char *rb_version_nr;
extern char *message;
// DLL specific variables
DLL_GLOBAL const Vector g_vecZero = Vector(0, 0, 0);
// CLASS DEFINITIONS
cGame Game;
cNodeMachine NodeMachine;
cChatEngine ChatEngine;
FILE *fpRblog = NULL;
float f_load_time = 0.0;
float f_minplayers_think = 0.0; // timer used to add realbots if internet play enabled
int mod_id = CSTRIKE_DLL; // should be changed to 0 when we are going to do multi-mod stuff
int m_spriteTexture = 0;
bool isFakeClientCommand = FALSE;
int fake_arg_count;
float bot_check_time = 30.0;
int min_bots = -1;
int max_bots = -1;
int min_players = -1; // minimum amount of players that should be in the server all the time
int num_bots = 0;
int prev_num_bots = 0;
bool g_GameRules = FALSE;
edict_t *clients[32];
edict_t *pHostEdict = NULL;
float welcome_time = 0.0;
bool welcome_sent = false;
FILE *bot_cfg_fp = NULL;
bool need_to_open_cfg = TRUE;
float bot_cfg_pause_time = 0.0;
float respawn_time = 0.0;
bool spawn_time_reset = FALSE;
// Interval between joining bots.
int internet_max_interval = 30;
int internet_min_interval = 10;
// End...
// Counter-Strike 1.6 or 1.5
int counterstrike = 0; // Default 1.5
void UpdateClientData(const struct edict_s *ent, int sendweapons,
struct clientdata_s *cd);
void ProcessBotCfgFile(void);
// External added variables
bool end_round = false;
bool autoskill = false;
bool draw_nodes = false;
int draw_nodepath = -1;
bool draw_connodes = false;
int kick_amount_bots = 0;
int kick_bots_team = 0;
// internet mode vars
bool internet_addbot = false; // Add a bot?
float add_timer = -1; // Timer for adding bots
bool internet_play = false;
void RealBot_ServerCommand(void);
// START of Metamod stuff
enginefuncs_t meta_engfuncs;
gamedll_funcs_t *gpGamedllFuncs;
mutil_funcs_t *gpMetaUtilFuncs;
meta_globals_t *gpMetaGlobals;
META_FUNCTIONS gMetaFunctionTable = {
NULL, // pfnGetEntityAPI()
NULL, // pfnGetEntityAPI_Post()
GetEntityAPI2, // pfnGetEntityAPI2()
GetEntityAPI2_Post, // pfnGetEntityAPI2_Post()
NULL, // pfnGetNewDLLFunctions()
NULL, // pfnGetNewDLLFunctions_Post()
GetEngineFunctions, // pfnGetEngineFunctions()
NULL, // pfnGetEngineFunctions_Post()
};
plugin_info_t Plugin_info = {
META_INTERFACE_VERSION, // interface version
"RealBot", // plugin name
rb_version_nr, // plugin version
__DATE__, // date of creation
"Stefan Hendriks", // plugin author
"http://realbot.bots-united.com/", // plugin URL
"REALBOT", // plugin logtag
PT_CHANGELEVEL, // when loadable <-- FIX
PT_ANYTIME, // when unloadable
};
C_DLLEXPORT int Meta_Query(const char *ifvers, plugin_info_t **pPlugInfo,
mutil_funcs_t *pMetaUtilFuncs) {
// this function is the first function ever called by metamod in the plugin DLL. Its purpose
// is for metamod to retrieve basic information about the plugin, such as its meta-interface
// version, for ensuring compatibility with the current version of the running metamod.
// keep track of the pointers to metamod function tables metamod gives us
gpMetaUtilFuncs = pMetaUtilFuncs;
*pPlugInfo = &Plugin_info;
// check for interface version compatibility
if (strcmp(ifvers, Plugin_info.ifvers) != 0) {
int mmajor = 0, mminor = 0, pmajor = 0, pminor = 0;
LOG_CONSOLE(PLID,
"%s: meta-interface version mismatch (metamod: %s, %s: %s)",
Plugin_info.name, ifvers, Plugin_info.name,
Plugin_info.ifvers);
LOG_MESSAGE(PLID,
"%s: meta-interface version mismatch (metamod: %s, %s: %s)",
Plugin_info.name, ifvers, Plugin_info.name,
Plugin_info.ifvers);
// if plugin has later interface version, it's incompatible (update metamod)
sscanf(ifvers, "%d:%d", &mmajor, &mminor);
sscanf(META_INTERFACE_VERSION, "%d:%d", &pmajor, &pminor);
if ((pmajor > mmajor) || ((pmajor == mmajor) && (pminor > mminor))) {
LOG_CONSOLE(PLID,
"metamod version is too old for this plugin; update metamod");
LOG_ERROR(PLID,
"metamod version is too old for this plugin; update metamod");
return (FALSE);
}
// if plugin has older major interface version, it's incompatible (update plugin)
else if (pmajor < mmajor) {
LOG_CONSOLE(PLID,
"metamod version is incompatible with this plugin; please find a newer version of this plugin");
LOG_ERROR(PLID,
"metamod version is incompatible with this plugin; please find a newer version of this plugin");
return (FALSE);
}
}
return (TRUE); // tell metamod this plugin looks safe
}
C_DLLEXPORT int
Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable,
meta_globals_t *pMGlobals, gamedll_funcs_t *pGamedllFuncs) {
// this function is called when metamod attempts to load the plugin. Since it's the place
// where we can tell if the plugin will be allowed to run or not, we wait until here to make
// our initialization stuff, like registering CVARs and dedicated server commands.
// are we allowed to load this plugin now ?
if (now > Plugin_info.loadable) {
LOG_CONSOLE(PLID,
"%s: plugin NOT attaching (can't load plugin right now)",
Plugin_info.name);
LOG_ERROR(PLID,
"%s: plugin NOT attaching (can't load plugin right now)",
Plugin_info.name);
return (FALSE); // returning FALSE prevents metamod from attaching this plugin
}
// keep track of the pointers to engine function tables metamod gives us
gpMetaGlobals = pMGlobals;
memcpy(pFunctionTable, &gMetaFunctionTable, sizeof(META_FUNCTIONS));
gpGamedllFuncs = pGamedllFuncs;
// print a message to notify about plugin attaching
LOG_CONSOLE(PLID, "%s: plugin attaching", Plugin_info.name);
LOG_MESSAGE(PLID, "%s: plugin attaching", Plugin_info.name);
// ask the engine to register the server commands this plugin uses
REG_SVR_COMMAND("realbot", RealBot_ServerCommand);
// Notify user that 'realbot' command is regged
LOG_CONSOLE(PLID, "realbot - command prefix is now reserved.");
LOG_MESSAGE(PLID, "realbot - command prefix is now reserved.");
return (TRUE); // returning TRUE enables metamod to attach this plugin
}
C_DLLEXPORT int Meta_Detach(PLUG_LOADTIME now, PL_UNLOAD_REASON reason) {
// this function is called when metamod unloads the plugin. A basic check is made in order
// to prevent unloading the plugin if its processing should not be interrupted.
// is metamod allowed to unload the plugin ?
if ((now > Plugin_info.unloadable) && (reason != PNL_CMD_FORCED)) {
LOG_CONSOLE(PLID,
"%s: plugin NOT detaching (can't unload plugin right now)",
Plugin_info.name);
LOG_ERROR(PLID,
"%s: plugin NOT detaching (can't unload plugin right now)",
Plugin_info.name);
return (FALSE); // returning FALSE prevents metamod from unloading this plugin
}
NodeMachine.FreeVisibilityTable();
free(message);
return (TRUE); // returning TRUE enables metamod to unload this plugin
}
// END of Metamod stuff
#ifdef _WIN32
// Required DLL entry point
int WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
return TRUE;
}
#endif /* */
C_DLLEXPORT void WINAPI
GiveFnptrsToDll(enginefuncs_t *pengfuncsFromEngine,
globalvars_t *pGlobals) {
// get the engine functions from the engine...
memcpy(&g_engfuncs, pengfuncsFromEngine, sizeof(enginefuncs_t));
gpGlobals = pGlobals;
mod_id = CSTRIKE_DLL; // so far RealBot works only for CS, eh Stefan ? :)
// @PMB -> Yes so far it does ;) working on this MOD-GAME independent structure... grmbl
}
void GameDLLInit(void) {
// clear log.txt
FILE *fplog;
fplog = fopen("reallog.txt", "wt");
if (fplog) {
fprintf(fplog, "Realbot Logbook\n");
fprintf(fplog, "Version %s\n\n", rb_version_nr);
fclose(fplog);
}
// and now open it for the entire bot-dll-lifetime
fpRblog = fopen("reallog.txt", "at");
rblog("Initializing clients..");
for (int i = 0; i < 32; i++)
clients[i] = NULL;
rblog("OK\n");
// initialize the bots array of structures...
rblog("Initializing memory for bots array..");
memset(bots, 0, sizeof(bots));
rblog("OK\n");
rblog("Verifying realbot is installed correctly..");
FILE *fp;
bool bInstalledCorrectly = false;
fp = fopen("realbot/dll/realbot_mm.dll", "rb");
if (fp != NULL) {
bInstalledCorrectly = true;
fclose(fp);
}
fp = fopen("realbot/dll/realbot_mm_i386.so", "rb");
if (fp != NULL) {
bInstalledCorrectly = true;
fclose(fp);
}
if (bInstalledCorrectly)
rblog("OK\n");
else
rblog("NOT FOUND!\n");
// When installed correctly let the user know
if (bInstalledCorrectly)
REALBOT_PRINT(NULL, "GAMEDLLINIT",
"Notice: RealBot is installed in the correct directory.");
else
REALBOT_PRINT(NULL, "GAMEDLLINIT",
"WARNING: RealBot is NOT installed in the correct directory.");
Game.Init();
Game.LoadNames();
Game.LoadBuyTable();
// NodeMachine
NodeMachine.init();
NodeMachine.init_players();
// Chat engine
ChatEngine.initAndload();
ChatEngine.fThinkTimer = gpGlobals->time;
// Set 'installed correctly' flag.
Game.bInstalledCorrectly = bInstalledCorrectly;
// CHECK FOR STEAM;
// As lazy as i am i use pierre's code which is in our modified metamod dll.
// Omg, why use so many comments? Sory pierre for ripping some pieces out.
// Pierre-Marie Baty -- STEAM/NOSTEAM auto-adaptative fix -- START
// to check whether Steam is installed or not, I test Steam-specific files.
// if you know a more orthodox way of doing this, please tell me.
// test file, if found = STEAM Linux/Win32 dedicated server
fp = fopen("valve/steam.inf", "rb");
if (fp != NULL) {
fclose(fp); // test was successful, close it
counterstrike = 1; // its cs 1.6
}
// test file, if found = STEAM Win32 listenserver
fp = fopen("FileSystem_Steam.dll", "rb");
if (fp != NULL) {
fclose(fp); // test was successful, close it
counterstrike = 1; // its cs 1.6
}
if (counterstrike == 0)
SERVER_PRINT("Notice: Assuming you run Counter-Strike 1.5\n");
else
SERVER_PRINT("Notice: Assuming you run Counter-Strike 1.6\n");
RETURN_META(MRES_IGNORED);
}
// INITIALIZATION
int Spawn(edict_t *pent) {
if (gpGlobals->deathmatch) {
char *pClassname = (char *) STRING(pent->v.classname);
if (strcmp(pClassname, "worldspawn") == 0) {
// do level initialization stuff here...
draw_nodes = false;
draw_nodepath = -1;
draw_connodes = false;
// FIX: Internet mode timing
internet_addbot = false; // Add a bot?
add_timer = gpGlobals->time; // Timer for adding bots
m_spriteTexture = PRECACHE_MODEL("sprites/lgtning.spr");
// sound
PRECACHE_SOUND("misc/imgood12.wav");
g_GameRules = TRUE;
//bot_cfg_pause_time = 0.0;
respawn_time = 0.0;
spawn_time_reset = FALSE;
prev_num_bots = num_bots;
num_bots = 0;
bot_check_time = gpGlobals->time + 30.0;
rblog("SPAWN : f_load_time is set\n");
f_load_time = gpGlobals->time + 6;
// Node machine loads
NodeMachine.init();
NodeMachine.load();
NodeMachine.experience_load();
ChatEngine.fThinkTimer = gpGlobals->time;
} else if (strcmp(pClassname, "trigger_multiple") == 0) {
// make it a func_button?
//sprintf(STRING(pent->v.classname), "func_button");
}
}
RETURN_META_VALUE(MRES_IGNORED, 0);
}
BOOL
ClientConnect(edict_t *pEntity, const char *pszName,
const char *pszAddress, char szRejectReason[128]) {
if (gpGlobals->deathmatch) {
int i;
int count = 0;
// check if this client is the listen server client
if (strcmp(pszAddress, "loopback") == 0) {
// save the edict of the listen server client...
pHostEdict = pEntity;
}
// 20/06/04 - It does not matter who joins, but we should never
// EVER try to look for players for some time now.
f_minplayers_think = gpGlobals->time + 90; // give 90 seconds instead of 60
// 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
bot_check_time = gpGlobals->time + 60.0;
for (i = 0; i < 32; i++) {
if (bots[i].bIsUsed) // count the number of bots in use
count++;
}
// if there are currently more than the minimum number of bots running
// then kick one of the bots off the server...
if ((count > min_bots) && (min_bots != -1)) {
for (i = 0; i < 32; i++) {
if (bots[i].bIsUsed) // is this slot used?
{
char cmd[80];
sprintf(cmd, "kick \"%s\"\n", bots[i].name);
SERVER_COMMAND(cmd); // kick the bot using (kick "name")
break;
}
}
}
} else {}
}
RETURN_META_VALUE(MRES_IGNORED, 0);
}
void ClientDisconnect(edict_t *pEntity) {
if (gpGlobals->deathmatch) {
int i;
i = 0;
while ((i < 32) && (clients[i] != pEntity))
i++;
if (i < 32)
clients[i] = NULL;
// when a bot...
for (i = 0; i < 32; i++) {
if (bots[i].pEdict == pEntity) {
// someone kicked this bot off of the server...
bots[i].bIsUsed = false; // this slot is now free to use
bots[i].fKickTime = gpGlobals->time; // save the kicked time
bots[i].pEdict = NULL; // make NULL
break;
}
}
}
RETURN_META(MRES_IGNORED);
}
void ClientPutInServer(edict_t *pEntity) {
int i = 0;
while ((i < 32) && (clients[i] != NULL))
i++;
if (i < 32)
clients[i] = pEntity; // store this clients edict in the clients array
RETURN_META(MRES_IGNORED);
}
// CLIENT / CONSOLE / COMMANDS
void ClientCommand(edict_t *pEntity) {
/*
EMPTY - BANNED - UNUSED - MUHAHAHA
SINCE METAMOD ;) BUILD 2053
// 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 == pHostEdict))
{
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);
}
*/
RETURN_META(MRES_IGNORED);
}
// TODO: Revise this method
void StartFrame(void) {
if (!gpGlobals->deathmatch) return; // bots only work in 'deathmatch mode'
// REALBOT_PRINT("StartFrame", "BEGIN");
edict_t *pPlayer;
static float check_server_cmd = 0.0;
static int i, index, player_index, bot_index;
static float previous_time = -1.0;
static float client_update_time = 0.0;
clientdata_s cd;
char msg[256];
int count = 0;
int waits = 0; // How many bots had to wait.
// When a user - or anything else - specified a higher number of 0 to
// kick bots, then we will do as told.
if (kick_amount_bots > 0) {
int kicking_team = 0; // What team should we kick?
if (kick_bots_team == 0 || kick_bots_team == 6) // No kick_bots_team so we get
kick_bots_team = 6 + RANDOM_LONG(0, 1); // a default one! (random)
if (kick_bots_team == 6) // Its random.. (5 means CT)
{
kicking_team = 1;
kick_bots_team++;
} else if (kick_bots_team == 7) {
kicking_team = 2;
kick_bots_team = 6;
} else
kicking_team = kick_bots_team;
if (kick_bots_team > 7)
kick_bots_team = 6;
if (kick_bots_team == 1)
kicking_team = 1;
if (kick_bots_team == 2)
kicking_team = 2;
for (i = 0; i < 32; i++) {
if (bots[i].bIsUsed) // is this slot used?
{
char cmd[80];
if (bots[i].iTeam == kicking_team) {
sprintf(cmd, "kick \"%s\"\n", bots[i].name);
SERVER_COMMAND(cmd); // kick the bot using (kick "name")
break;
}
} // Used?
} // I
kick_amount_bots--; // next frame we kick another bot
} else {
kick_bots_team = 0; // its always 0 when we have no one to kick
}
// if a new map has started then (MUST BE FIRST IN StartFrame)...
// which is determined by comparing the previously recorded time (at the end of this function)
// with the current time. If the current time somehow was less (before) the previous time, then we
// assume a reset/restart/reload of a map.
if ((gpGlobals->time + 0.1) < previous_time) {
rblog("NEW MAP because time is reset #1\n");
check_server_cmd = 0.0; // reset at start of map
count = 0;
// mark the bots as needing to be respawned...
for (index = 0; index < 32; index++) {
cBot *pBot = &bots[index];
if (count >= prev_num_bots) {
pBot->bIsUsed = false;
pBot->respawn_state = RESPAWN_NONE;
pBot->fKickTime = 0.0;
}
if (pBot->bIsUsed) // is this slot used?
{
pBot->respawn_state = RESPAWN_NEED_TO_RESPAWN;
count++;
}
// check for any bots that were very recently kicked...
if ((pBot->fKickTime + 5.0) > previous_time) {
pBot->respawn_state = RESPAWN_NEED_TO_RESPAWN;
count++;
} else {
pBot->fKickTime = 0.0; // reset to prevent false spawns later
}
// set the respawn time
if (IS_DEDICATED_SERVER()) {
respawn_time = gpGlobals->time + 5.0;
} else {
respawn_time = gpGlobals->time + 20.0;
}
// Send welcome message
welcome_sent = false;
welcome_time = 0.0;
}
NodeMachine.init_players();
NodeMachine.init_round();
NodeMachine.setUpInitialGoals();
NodeMachine.resetCheckedValuesForGoals();
//ChatEngine.fThinkTimer = gpGlobals->time;
client_update_time = gpGlobals->time + 10.0; // start updating client data again
bot_check_time = gpGlobals->time + 30.0;
} // New map/reload/restart
//
// SEND WELCOME MESSAGE
//
if (!welcome_sent) {
int iIndex = 0;
if (welcome_time == 0.0) {
// go through all clients (except bots)
for (iIndex = 1; iIndex <= gpGlobals->maxClients; iIndex++) {
edict_t *pPlayer = INDEXENT(iIndex);
// skip invalid players
if ((pPlayer) && (!pPlayer->free)) {
// we found a player which is alive. w00t
welcome_time = gpGlobals->time + 10.0;
break;
}
}
}
if ((welcome_time > 0.0) && (welcome_time < gpGlobals->time)) {
// let's send a welcome message to this client...
char total_welcome[256];
sprintf(total_welcome, "RealBot - Version %s\nBy Stefan Hendriks\n", rb_version_nr);
int r, g, b;
/*
r = RANDOM_LONG(30, 255);
g = RANDOM_LONG(30, 255);
b = RANDOM_LONG(30, 255);
// Send to listen server
if (pHostEdict)
HUD_DrawString(r,g,b, total_welcome, pHostEdict);
*/
for (iIndex = 1; iIndex <= gpGlobals->maxClients; iIndex++) {
edict_t *pPlayer = INDEXENT(iIndex);
// skip invalid players and skip self (i.e. this bot)
if ((pPlayer) && (!pPlayer->free)) {
// skip bots!
if (UTIL_GetBotPointer(pPlayer))
continue;
// skip fake clients
if ((pPlayer->v.flags & FL_THIRDPARTYBOT)
|| (pPlayer->v.flags & FL_FAKECLIENT))
continue;
// random color
r = RANDOM_LONG(30, 255);
g = RANDOM_LONG(30, 255);
b = RANDOM_LONG(30, 255);
HUD_DrawString(r, g, b, total_welcome, pPlayer);
// use speak command
if (pPlayer == pHostEdict)
UTIL_SpeechSynth(pHostEdict, Game.RandomSentence());
}
}
welcome_sent = true; // clear this so we only do it once
welcome_time = 0.0;
}
}
if (client_update_time <= gpGlobals->time) {
client_update_time = gpGlobals->time + 1.0;
for (i = 0; i < 32; i++) {
if (bots[i].bIsUsed) {
memset(&cd, 0, sizeof(cd));
MDLL_UpdateClientData(bots[i].pEdict, 1, &cd);
// see if a weapon was dropped...
if (bots[i].bot_weapons != cd.weapons) {
bots[i].bot_weapons = cd.weapons;
}
}
}
}
// a few seconds after map load we assign goals
if (f_load_time < gpGlobals->time && f_load_time != 0.0) {
f_load_time = 0.0; // do not load again
rblog("NEW MAP because time is reset #2\n");
Game.DetermineMapGoal();
Game.resetRoundTime();
NodeMachine.setUpInitialGoals();
}
// Fix kill all with new round
if (Game.NewRound()) {
rblog("dll.cpp:740, Game.NewRound\n");
// Send a message to clients about RealBot every round
if (Game.iVersionBroadcasting == BROADCAST_ROUND) {
welcome_sent = false;
welcome_time = gpGlobals->time + 2;
}
NodeMachine.init_round();
NodeMachine.save(); // save information
NodeMachine.experience_save();
NodeMachine.setUpInitialGoals();
end_round = false;
} // new round - before any bots realized yet
// When min players is set
if (min_players > 0 && f_minplayers_think < gpGlobals->time) {
internet_play = false; // when there is min_players set there is NO simulated internet_play!
int iHumans = 0, iBots = 0;
// Search for human players, simple method...
for (int i = 1; i <= gpGlobals->maxClients; i++) {
edict_t *pPlayer = INDEXENT(i);
// skip invalid players and skip self (i.e. this bot)
if ((pPlayer) && (!pPlayer->free)) {
// a bot
if (UTIL_GetBotPointer(pPlayer) != NULL)
iBots++;
else {
if (pPlayer->v.flags & FL_CLIENT)
iHumans++; // it is 'human' (well unless some idiot uses another bot, i cannot detect that!)
}
}
}
// There are not enough humans, so add/remove bots
if (iHumans < min_players) {
int iTotal = iHumans + iBots;
// char msg[80];
// total players is higher then min_players due BOTS, remove a bot
if (iTotal > min_players) {
// find one bot and remove it (one by one)
SERVER_PRINT("RBSERVER: Too many players, kicking one bot.\n");
kick_amount_bots = 1;
}
// total players is lower then min_players due BOTS, add a bot
else if (iTotal < min_players) {
// add a bot
SERVER_PRINT("RBSERVER: Too few player slots filled, adding one bot.\n");
Game.createBot(NULL, NULL, NULL, NULL, NULL);
}
} else {
if (num_bots > 0) {
// there are enough human players, remove any bot
kick_amount_bots = 32;
}
}
// 2 seconds thinking
f_minplayers_think = gpGlobals->time + 2;
}
// INTERNET MODE:
if (internet_play == false)
add_timer = gpGlobals->time + 2.0;
else // ------------ INTERNET MODE IS ON ------------
{
bool max_serverbots = true; // Reached MAX bots?
// When MAX_BOTS disabled...
if (max_bots < 0)
max_serverbots = false;
// When MAX_BOTS enabled...
if (max_bots > -1)
if (num_bots < max_bots)
max_serverbots = false; // we may add bots
if (max_serverbots == false) // we may add bots
if (num_bots + 1 >= gpGlobals->maxClients)
max_serverbots = true; // we are actually full. Give one player the chance to fill in
// Keep adding bots until 'full'
if (max_serverbots == false) {
if (add_timer < gpGlobals->time) {
add_timer =
gpGlobals->time + RANDOM_LONG(internet_min_interval,
internet_max_interval);
internet_addbot = true; // add a bot! w00t
}
}
}
if (internet_addbot) {
// When timer is set, create a new bot.
if (add_timer > gpGlobals->time && internet_addbot) {
internet_addbot = false;
Game.createBot(NULL, NULL, NULL, NULL, NULL);
bot_check_time = gpGlobals->time + 5.0;
}
}
NodeMachine.addNodesForPlayers();
count = 0;
waits = 0;
// -------------------------------------------------------------
// GAME : Update game status first, before we go think about it
// -------------------------------------------------------------
Game.UpdateGameStatus();
for (bot_index = 0; bot_index < gpGlobals->maxClients; bot_index++) {
cBot &bot = bots[bot_index];
if ((bot.bIsUsed) && // is this slot used AND
(bot.respawn_state == RESPAWN_IDLE)) // not respawning
{
if (bot.fUpdateTime < gpGlobals->time) {
BotThink(&bot);
}
count++;
}
} // FOR
// -------------------------------------------------------------
// CHATENGINE : Think now
// -------------------------------------------------------------
ChatEngine.think();
// Keep number of bots up-to-date.
if (count > num_bots)
num_bots = count;
// handle radio commands
if (radio_message) {
BotRadioAction();
radio_message = false;
}
// FIX: Suggested by Greg, unnescesary loops.
if (draw_nodes) {
for (player_index = 1; player_index <= gpGlobals->maxClients;
player_index++) {
pPlayer = INDEXENT(player_index);
if (pPlayer && !pPlayer->free) {
if (FBitSet(pPlayer->v.flags, FL_CLIENT)) {
NodeMachine.draw(pPlayer);
break;
}
}
}
}
if (draw_connodes) {
for (player_index = 1; player_index <= gpGlobals->maxClients;
player_index++) {
pPlayer = INDEXENT(player_index);
if (pPlayer && !pPlayer->free) {
if (FBitSet(pPlayer->v.flags, FL_CLIENT)) {
NodeMachine.connections(pPlayer);
break;
}
}
}
}
// Draw node path
if (draw_nodepath > -1) {
NodeMachine.path_draw(pHostEdict);
}
// Counter-Strike - A new round has started
if (Game.NewRound()) {
rblog("dll.cpp:917, Game.NewRound\n");
NodeMachine.scale_danger(); // Scale danger
NodeMachine.scale_contact(); // same for contact
Game.InitNewRound();
Game.SetNewRound(false);
rblog("dll.cpp:917, Game.NewRound - finished\n");
}
// are we currently respawning bots and is it time to spawn one yet?
if ((respawn_time > 1.0) && (respawn_time <= gpGlobals->time)) {
int index = 0;
// find bot needing to be respawned...
while ((index < 32)
&& (bots[index].respawn_state != RESPAWN_NEED_TO_RESPAWN))
index++;
if (index < 32) {
bots[index].respawn_state = RESPAWN_IS_RESPAWNING;
bots[index].bIsUsed = false; // free up this slot
// respawn 1 bot then wait a while (otherwise engine crashes)
// 01/07/04 - Stefan - Thanks Evy for pointing out this one: On skill 10
// the c_skill (was [2]) would be messed up (perhaps this is some memory related bug later on?)
char c_skill[3];
char c_team[2];
char c_class[3];
sprintf(c_skill, "%d", bots[index].bot_skill);
sprintf(c_team, "%d", bots[index].iTeam);
sprintf(c_class, "%d", bots[index].bot_class);
Game.createBot(NULL, c_team, c_skill, c_class,
bots[index].name);
// 01/07/04 - Stefan - make 100% sure we do not crash on this part with the auto-add function
f_minplayers_think = gpGlobals->time + 15; // do not check this for 15 seconds from now
respawn_time = gpGlobals->time + 2.0; // set next respawn time
bot_check_time = gpGlobals->time + 5.0;
} else {
respawn_time = 0.0;
}
}
if (g_GameRules) {
if (need_to_open_cfg) // have we open bot.cfg file yet?
{
char filename[256];
need_to_open_cfg = FALSE; // only do this once!!!
UTIL_BuildFileNameRB("bot.cfg", filename);
sprintf(msg, "Executing %s\n", filename);
ALERT(at_console, msg);
bot_cfg_fp = fopen(filename, "r");
if (bot_cfg_fp == NULL)
ALERT(at_console, "bot.cfg file not found\n");
if (IS_DEDICATED_SERVER())
bot_cfg_pause_time = gpGlobals->time + 5.0;
else
bot_cfg_pause_time = gpGlobals->time + 20.0;
}
if (!IS_DEDICATED_SERVER() && !spawn_time_reset) {
if (pHostEdict != NULL) {
if (IsAlive(pHostEdict)) {
spawn_time_reset = TRUE;
if (respawn_time >= 1.0)
respawn_time = min(respawn_time, gpGlobals->time + (float) 1.0);
if (bot_cfg_pause_time >= 1.0)
bot_cfg_pause_time =
min(bot_cfg_pause_time,
gpGlobals->time + (float) 1.0);
}
}
}
// DO BOT.CFG crap here
if ((bot_cfg_fp) && (bot_cfg_pause_time >= 1.0)
&& (bot_cfg_pause_time <= gpGlobals->time)) {
// process bot.cfg file options...