4 #include "../../dpdefs/progsdefs.qh"
5 #include "../../dpdefs/dpextensions.qh"
6 #include "../sys-post.qh"
7 #include "../../warpzonelib/common.qh"
8 #include "../../warpzonelib/util_server.qh"
9 #include "../../common/constants.qh"
10 #include "../../common/teams.qh"
11 #include "../../common/util.qh"
12 #include "../../common/weapons/weapons.qh"
13 #include "../weapons/accuracy.qh"
14 #include "../t_items.qh"
15 #include "../autocvars.qh"
16 #include "../constants.qh"
18 #include "../mutators/mutators_include.qh"
19 #include "../campaign.qh"
20 #include "../../common/mapinfo.qh"
21 #include "../../csqcmodellib/sv_model.qh"
22 #include "../antilag.qh"
27 #include "navigation.qh"
28 #include "waypoints.qh"
31 #include "navigation.qc"
32 #include "waypoints.qc"
33 #include "scripting.qc"
35 #include "havocbot/havocbot.qc"
43 currentbots = currentbots + 1;
46 bot_setnameandstuff();
56 if (self.bot_nextthink > time)
59 self.flags &= ~FL_GODMODE;
61 self.flags |= FL_GODMODE;
63 self.bot_nextthink = self.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, self.bot_aiskill);
64 //if (self.bot_painintensity > 0)
65 // self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
67 //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
68 //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
70 if (!IS_PLAYER(self) || (autocvar_g_campaign && !campaign_bots_may_start))
72 self.bot_nextthink = time + 0.5;
78 self.v_angle = self.angles;
80 self.fixangle = false;
85 self.dmg_inflictor = world;
87 // calculate an aiming latency based on the skill setting
88 // (simulated network latency + naturally delayed reflexes)
89 //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
90 // minimum ping 20+10 random
91 self.ping = bound(0,0.07 - bound(0, (skill + self.bot_pingskill) * 0.005,0.05)+random()*0.01,0.65); // Now holds real lag to server, and higer skill players take a less laggy server
92 // skill 10 = ping 0.2 (adrenaline)
93 // skill 0 = ping 0.7 (slightly drunk)
99 self.BUTTON_ATCK2 = 0;
100 self.BUTTON_ZOOM = 0;
101 self.BUTTON_CROUCH = 0;
102 self.BUTTON_HOOK = 0;
103 self.BUTTON_INFO = 0;
105 self.BUTTON_CHAT = 0;
108 if (time < game_starttime)
110 // block the bot during the countdown to game start
111 self.movement = '0 0 0';
112 self.bot_nextthink = game_starttime;
116 // if dead, just wait until we can respawn
119 if (self.deadflag == DEAD_DEAD)
121 self.BUTTON_JUMP = 1; // press jump to respawn
122 self.bot_strategytime = 0;
125 else if(self.aistatus & AI_STATUS_STUCK)
126 navigation_unstuck();
128 // now call the current bot AI (havocbot for example)
132 void bot_setnameandstuff()
135 float file, tokens, prio;
138 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
139 string name, prefix, suffix;
141 if(autocvar_g_campaign)
148 prefix = autocvar_bot_prefix;
149 suffix = autocvar_bot_suffix;
152 file = fopen(autocvar_bot_config_file, FILE_READ);
156 print(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
161 RandomSelection_Init();
162 while((readfile = fgets(file)))
164 if(substring(readfile, 0, 2) == "//")
166 if(substring(readfile, 0, 1) == "#")
168 tokens = tokenizebyseparator(readfile, "\t");
182 RandomSelection_Add(world, 0, readfile, 1, prio);
184 readfile = RandomSelection_chosen_string;
188 tokens = tokenizebyseparator(readfile, "\t");
189 if(argv(0) != "") bot_name = argv(0);
190 else bot_name = "Bot";
192 if(argv(1) != "") bot_model = argv(1);
195 if(argv(2) != "") bot_skin = argv(2);
198 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
199 else bot_shirt = ftos(floor(random() * 15));
201 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
202 else bot_pants = ftos(floor(random() * 15));
204 self.bot_forced_team = stof(argv(5));
208 #define READSKILL(f,w,r) if(argv(prio) != "") self.f = stof(argv(prio)) * (w); else self.f = (!autocvar_g_campaign) * (2 * random() - 1) * (r) * (w); ++prio
209 //print(bot_name, ": ping=", argv(9), "\n");
211 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
212 READSKILL(bot_moveskill, 2, 0); // move skill
213 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
215 READSKILL(bot_pingskill, 0.5, 0); // ping skill
217 READSKILL(bot_weaponskill, 2, 0); // weapon skill
218 READSKILL(bot_aggresskill, 1, 0); // aggre skill
219 READSKILL(bot_rangepreference, 1, 0); // read skill
221 READSKILL(bot_aimskill, 2, 0); // aim skill
222 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
223 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
225 READSKILL(bot_thinkskill, 1, 0.5); // think skill
226 READSKILL(bot_aiskill, 2, 0); // "ai" skill
228 self.bot_config_loaded = true;
230 // this is really only a default, JoinBestTeam is called later
231 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
232 self.bot_preferredcolors = self.clientcolors;
235 if (autocvar_bot_usemodelnames)
240 // number bots with identical names
246 if(p.cleanname == name)
250 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
252 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
254 self.cleanname = strzone(name);
256 // pick the model and skin
257 if(substring(bot_model, -4, 1) != ".")
258 bot_model = strcat(bot_model, ".iqm");
259 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
260 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
262 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
263 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
266 void bot_custom_weapon_priority_setup()
268 float tokens, i, c, w;
270 bot_custom_weapon = false;
272 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
273 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
274 autocvar_bot_ai_custom_weapon_priority_close == "" ||
275 autocvar_bot_ai_custom_weapon_priority_distances == ""
280 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
285 bot_distance_far = stof(argv(0));
286 bot_distance_close = stof(argv(1));
288 if(bot_distance_far < bot_distance_close){
289 bot_distance_far = stof(argv(1));
290 bot_distance_close = stof(argv(0));
293 // Initialize list of weapons
294 bot_weapons_far[0] = -1;
295 bot_weapons_mid[0] = -1;
296 bot_weapons_close[0] = -1;
298 // Parse far distance weapon priorities
299 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
302 for(i=0; i < tokens && c < WEP_COUNT; ++i){
304 if ( w >= WEP_FIRST && w <= WEP_LAST) {
305 bot_weapons_far[c] = w;
310 bot_weapons_far[c] = -1;
312 // Parse mid distance weapon priorities
313 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
316 for(i=0; i < tokens && c < WEP_COUNT; ++i){
318 if ( w >= WEP_FIRST && w <= WEP_LAST) {
319 bot_weapons_mid[c] = w;
324 bot_weapons_mid[c] = -1;
326 // Parse close distance weapon priorities
327 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
330 for(i=0; i < tokens && i < WEP_COUNT; ++i){
332 if ( w >= WEP_FIRST && w <= WEP_LAST) {
333 bot_weapons_close[c] = w;
338 bot_weapons_close[c] = -1;
340 bot_custom_weapon = true;
346 //dprint("bot_endgame\n");
350 setcolor(e, e.bot_preferredcolors);
353 // if dynamic waypoints are ever implemented, save them here
356 void bot_relinkplayerlist()
362 player_list = e = findchainflags(flags, FL_CLIENT);
367 player_count = player_count + 1;
368 e.nextplayer = e.chain;
369 if (IS_BOT_CLIENT(e))
376 bot_list.nextbot = world;
379 currentbots = currentbots + 1;
383 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
384 bot_strategytoken = bot_list;
385 bot_strategytoken_taken = true;
388 void bot_clientdisconnect()
390 if (!IS_BOT_CLIENT(self))
392 bot_clearqueue(self);
394 strunzone(self.cleanname);
395 if(self.netname_freeme)
396 strunzone(self.netname_freeme);
397 if(self.playermodel_freeme)
398 strunzone(self.playermodel_freeme);
399 if(self.playerskin_freeme)
400 strunzone(self.playerskin_freeme);
401 self.cleanname = string_null;
402 self.netname_freeme = string_null;
403 self.playermodel_freeme = string_null;
404 self.playerskin_freeme = string_null;
405 if(self.bot_cmd_current)
406 remove(self.bot_cmd_current);
407 if(bot_waypoint_queue_owner==self)
408 bot_waypoint_queue_owner = world;
411 void bot_clientconnect()
413 if (!IS_BOT_CLIENT(self))
415 self.bot_preferredcolors = self.clientcolors;
416 self.bot_nextthink = time - random();
417 self.lag_func = bot_lagfunc;
419 self.createdtime = self.bot_nextthink;
421 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
422 bot_setnameandstuff();
424 if(self.bot_forced_team==1)
425 self.team = NUM_TEAM_1;
426 else if(self.bot_forced_team==2)
427 self.team = NUM_TEAM_2;
428 else if(self.bot_forced_team==3)
429 self.team = NUM_TEAM_3;
430 else if(self.bot_forced_team==4)
431 self.team = NUM_TEAM_4;
433 JoinBestTeam(self, false, true);
438 void bot_removefromlargestteam()
440 float besttime, bestcount, thiscount;
442 CheckAllowedTeams(world);
443 GetTeamCounts(world);
444 head = findchainfloat(isbot, true);
448 besttime = head.createdtime;
452 if(head.team == NUM_TEAM_1)
454 else if(head.team == NUM_TEAM_2)
456 else if(head.team == NUM_TEAM_3)
458 else if(head.team == NUM_TEAM_4)
462 if (thiscount > bestcount)
464 bestcount = thiscount;
465 besttime = head.createdtime;
468 else if (thiscount == bestcount && besttime < head.createdtime)
470 besttime = head.createdtime;
475 currentbots = currentbots - 1;
479 void bot_removenewest()
486 bot_removefromlargestteam();
490 head = findchainfloat(isbot, true);
494 besttime = head.createdtime;
497 if (besttime < head.createdtime)
499 besttime = head.createdtime;
504 currentbots = currentbots - 1;
508 void autoskill(float factor)
516 FOR_EACH_PLAYER(head)
518 if(IS_REAL_CLIENT(head))
519 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
521 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
524 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
525 dprint("best bot got ", ftos(bestbot), "; ");
526 if(bestbot < 0 || bestplayer < 0)
528 dprint("not doing anything\n");
529 // don't return, let it reset all counters below
531 else if(bestbot <= bestplayer * factor - 2)
533 if(autocvar_skill < 17)
535 dprint("2 frags difference, increasing skill\n");
536 cvar_set("skill", ftos(autocvar_skill + 1));
537 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
540 else if(bestbot >= bestplayer * factor + 2)
542 if(autocvar_skill > 0)
544 dprint("2 frags difference, decreasing skill\n");
545 cvar_set("skill", ftos(autocvar_skill - 1));
546 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
551 dprint("not doing anything\n");
553 // don't reset counters, wait for them to accumulate
556 FOR_EACH_PLAYER(head)
557 head.totalfrags_lastcheck = head.totalfrags;
560 void bot_calculate_stepheightvec(void)
562 stepheightvec = autocvar_sv_stepheight * '0 0 1';
563 jumpstepheightvec = stepheightvec +
564 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
565 // 0.75 factor is for safety to make the jumps easy
571 float realplayers, bots, activerealplayers;
573 activerealplayers = 0;
576 FOR_EACH_REALCLIENT(head)
578 if(IS_PLAYER(head) || g_lms || head.caplayer == 1)
583 // add/remove bots if needed to make sure there are at least
584 // minplayers+bot_number, or remove all bots if no one is playing
585 // But don't remove bots immediately on level change, as the real players
586 // usually haven't rejoined yet
587 bots_would_leave = false;
588 if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
589 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
590 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
592 float realminplayers, minplayers;
593 realminplayers = autocvar_minplayers;
594 minplayers = max(0, floor(realminplayers));
596 float realminbots, minbots;
597 realminbots = autocvar_bot_number;
598 minbots = max(0, floor(realminbots));
600 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
602 bots_would_leave = true;
606 // if there are no players, remove bots
610 // only add one bot per frame to avoid utter chaos
611 if(time > botframe_nextthink)
613 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
614 while (currentbots < bots)
616 if (bot_spawn() == world)
618 bprint("Can not add bot, server full.\n");
622 while (currentbots > bots)
629 void bot_serverframe()
631 if (intermission_running)
637 bot_calculate_stepheightvec();
638 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
640 if(time > autoskill_nextthink)
643 a = autocvar_skill_auto;
646 autoskill_nextthink = time + 5;
649 if(time > botframe_nextthink)
652 botframe_nextthink = time + 10;
655 bot_ignore_bots = autocvar_bot_ignore_bots;
657 if(botframe_spawnedwaypoints)
659 if(autocvar_waypoint_benchmark)
663 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
664 if (botframe_spawnedwaypoints)
666 if(botframe_cachedwaypointlinks)
668 if(!botframe_loadedforcedlinks)
669 waypoint_load_links_hardwired();
673 // TODO: Make this check cleaner
674 entity wp = findchain(classname, "waypoint");
675 if(time - wp.nextthink > 10)
676 waypoint_save_links();
681 botframe_spawnedwaypoints = true;
683 if(!waypoint_load_links())
684 waypoint_schedulerelinkall();
689 // cycle the goal token from one bot to the next each frame
690 // (this prevents them from all doing spawnfunc_waypoint searches on the same
691 // frame, which causes choppy framerates)
692 if (bot_strategytoken_taken)
694 bot_strategytoken_taken = false;
695 if (bot_strategytoken)
696 bot_strategytoken = bot_strategytoken.nextbot;
697 if (!bot_strategytoken)
698 bot_strategytoken = bot_list;
701 if (botframe_nextdangertime < time)
704 interval = autocvar_bot_ai_dangerdetectioninterval;
705 if (botframe_nextdangertime < time - interval * 1.5)
706 botframe_nextdangertime = time;
707 botframe_nextdangertime = botframe_nextdangertime + interval;
708 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
712 if (autocvar_g_waypointeditor)
713 botframe_showwaypointlinks();
715 if (autocvar_g_waypointeditor_auto)
716 botframe_autowaypoints();
718 if(time > bot_cvar_nextthink)
721 bot_custom_weapon_priority_setup();
722 bot_cvar_nextthink = time + 5;