4 #include "navigation.qh"
5 #include "scripting.qh"
6 #include "waypoints.qh"
8 #include "havocbot/havocbot.qh"
9 #include "havocbot/scripting.qh"
11 #include "../teamplay.qh"
13 #include "../antilag.qh"
14 #include "../autocvars.qh"
15 #include "../campaign.qh"
16 #include "../cl_client.qh"
17 #include "../constants.qh"
20 #include <common/t_items.qh>
22 #include "../mutators/all.qh"
24 #include "../weapons/accuracy.qh"
26 #include <common/constants.qh>
27 #include <common/mapinfo.qh>
28 #include <common/teams.qh>
29 #include <common/util.qh>
31 #include <common/weapons/all.qh>
33 #include <lib/csqcmodel/sv_model.qh>
35 #include <lib/warpzone/common.qh>
36 #include <lib/warpzone/util_server.qh>
40 entity bot = spawnclient();
43 currentbots = currentbots + 1;
45 bot_setnameandstuff();
55 if (self.bot_nextthink > time)
58 self.flags &= ~FL_GODMODE;
60 self.flags |= FL_GODMODE;
62 self.bot_nextthink = self.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, self.bot_aiskill);
63 //if (self.bot_painintensity > 0)
64 // self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
66 //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
67 //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
69 if (!IS_PLAYER(self) || (autocvar_g_campaign && !campaign_bots_may_start))
71 self.bot_nextthink = time + 0.5;
77 self.v_angle = self.angles;
79 self.fixangle = false;
84 self.dmg_inflictor = world;
86 // calculate an aiming latency based on the skill setting
87 // (simulated network latency + naturally delayed reflexes)
88 //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
89 // minimum ping 20+10 random
90 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
91 // skill 10 = ping 0.2 (adrenaline)
92 // skill 0 = ping 0.7 (slightly drunk)
95 PHYS_INPUT_BUTTON_ATCK(self) = false;
96 PHYS_INPUT_BUTTON_JUMP(self) = false;
97 PHYS_INPUT_BUTTON_ATCK2(self) = false;
98 PHYS_INPUT_BUTTON_ZOOM(self) = false;
99 PHYS_INPUT_BUTTON_CROUCH(self) = false;
100 PHYS_INPUT_BUTTON_HOOK(self) = false;
101 PHYS_INPUT_BUTTON_INFO(self) = false;
102 PHYS_INPUT_BUTTON_DRAG(self) = false;
103 PHYS_INPUT_BUTTON_CHAT(self) = false;
104 PHYS_INPUT_BUTTON_USE(self) = false;
106 if (time < game_starttime)
108 // block the bot during the countdown to game start
109 self.movement = '0 0 0';
110 self.bot_nextthink = game_starttime;
114 // if dead, just wait until we can respawn
117 if (self.deadflag == DEAD_DEAD)
119 PHYS_INPUT_BUTTON_JUMP(self) = true; // press jump to respawn
120 self.bot_strategytime = 0;
123 else if(self.aistatus & AI_STATUS_STUCK)
124 navigation_unstuck();
126 // now call the current bot AI (havocbot for example)
130 void bot_setnameandstuff()
133 float file, tokens, prio;
135 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
136 string name, prefix, suffix;
138 if(autocvar_g_campaign)
145 prefix = autocvar_bot_prefix;
146 suffix = autocvar_bot_suffix;
149 file = fopen(autocvar_bot_config_file, FILE_READ);
153 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
158 RandomSelection_Init();
159 while((readfile = fgets(file)))
161 if(substring(readfile, 0, 2) == "//")
163 if(substring(readfile, 0, 1) == "#")
165 tokens = tokenizebyseparator(readfile, "\t");
170 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
171 if(s == it.cleanname)
177 RandomSelection_Add(world, 0, readfile, 1, prio);
179 readfile = RandomSelection_chosen_string;
183 tokens = tokenizebyseparator(readfile, "\t");
184 if(argv(0) != "") bot_name = argv(0);
185 else bot_name = "Bot";
187 if(argv(1) != "") bot_model = argv(1);
190 if(argv(2) != "") bot_skin = argv(2);
193 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
194 else bot_shirt = ftos(floor(random() * 15));
196 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
197 else bot_pants = ftos(floor(random() * 15));
199 self.bot_forced_team = stof(argv(5));
203 #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
204 //print(bot_name, ": ping=", argv(9), "\n");
206 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
207 READSKILL(bot_moveskill, 2, 0); // move skill
208 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
210 READSKILL(bot_pingskill, 0.5, 0); // ping skill
212 READSKILL(bot_weaponskill, 2, 0); // weapon skill
213 READSKILL(bot_aggresskill, 1, 0); // aggre skill
214 READSKILL(bot_rangepreference, 1, 0); // read skill
216 READSKILL(bot_aimskill, 2, 0); // aim skill
217 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
218 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
220 READSKILL(bot_thinkskill, 1, 0.5); // think skill
221 READSKILL(bot_aiskill, 2, 0); // "ai" skill
223 self.bot_config_loaded = true;
225 // this is really only a default, JoinBestTeam is called later
226 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
227 self.bot_preferredcolors = self.clientcolors;
230 if (autocvar_bot_usemodelnames)
235 // number bots with identical names
237 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
238 if(it.cleanname == name)
242 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
244 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
246 self.cleanname = strzone(name);
248 // pick the model and skin
249 if(substring(bot_model, -4, 1) != ".")
250 bot_model = strcat(bot_model, ".iqm");
251 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
252 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
254 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
255 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
258 void bot_custom_weapon_priority_setup()
262 bot_custom_weapon = false;
264 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
265 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
266 autocvar_bot_ai_custom_weapon_priority_close == "" ||
267 autocvar_bot_ai_custom_weapon_priority_distances == ""
272 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
277 bot_distance_far = stof(argv(0));
278 bot_distance_close = stof(argv(1));
280 if(bot_distance_far < bot_distance_close){
281 bot_distance_far = stof(argv(1));
282 bot_distance_close = stof(argv(0));
285 // Initialize list of weapons
286 bot_weapons_far[0] = -1;
287 bot_weapons_mid[0] = -1;
288 bot_weapons_close[0] = -1;
290 // Parse far distance weapon priorities
291 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
294 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
296 if ( w >= WEP_FIRST && w <= WEP_LAST) {
297 bot_weapons_far[c] = w;
301 if(c < Weapons_COUNT)
302 bot_weapons_far[c] = -1;
304 // Parse mid distance weapon priorities
305 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
308 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
310 if ( w >= WEP_FIRST && w <= WEP_LAST) {
311 bot_weapons_mid[c] = w;
315 if(c < Weapons_COUNT)
316 bot_weapons_mid[c] = -1;
318 // Parse close distance weapon priorities
319 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
322 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
324 if ( w >= WEP_FIRST && w <= WEP_LAST) {
325 bot_weapons_close[c] = w;
329 if(c < Weapons_COUNT)
330 bot_weapons_close[c] = -1;
332 bot_custom_weapon = true;
338 //dprint("bot_endgame\n");
342 setcolor(e, e.bot_preferredcolors);
345 // if dynamic waypoints are ever implemented, save them here
348 void bot_relinkplayerlist()
354 player_list = e = findchainflags(flags, FL_CLIENT);
359 player_count = player_count + 1;
360 e.nextplayer = e.chain;
361 if (IS_BOT_CLIENT(e))
368 bot_list.nextbot = world;
371 currentbots = currentbots + 1;
375 LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
376 bot_strategytoken = bot_list;
377 bot_strategytoken_taken = true;
380 void bot_clientdisconnect()
382 if (!IS_BOT_CLIENT(self))
384 bot_clearqueue(self);
386 strunzone(self.cleanname);
387 if(self.netname_freeme)
388 strunzone(self.netname_freeme);
389 if(self.playermodel_freeme)
390 strunzone(self.playermodel_freeme);
391 if(self.playerskin_freeme)
392 strunzone(self.playerskin_freeme);
393 self.cleanname = string_null;
394 self.netname_freeme = string_null;
395 self.playermodel_freeme = string_null;
396 self.playerskin_freeme = string_null;
397 if(self.bot_cmd_current)
398 remove(self.bot_cmd_current);
399 if(bot_waypoint_queue_owner==self)
400 bot_waypoint_queue_owner = world;
403 void bot_clientconnect(entity this)
405 if (!IS_BOT_CLIENT(this)) return;
406 self.bot_preferredcolors = self.clientcolors;
407 self.bot_nextthink = time - random();
408 self.lag_func = bot_lagfunc;
410 self.createdtime = self.bot_nextthink;
412 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
413 bot_setnameandstuff();
415 if(self.bot_forced_team==1)
416 self.team = NUM_TEAM_1;
417 else if(self.bot_forced_team==2)
418 self.team = NUM_TEAM_2;
419 else if(self.bot_forced_team==3)
420 self.team = NUM_TEAM_3;
421 else if(self.bot_forced_team==4)
422 self.team = NUM_TEAM_4;
424 JoinBestTeam(self, false, true);
429 void bot_removefromlargestteam()
431 float besttime, bestcount, thiscount;
433 CheckAllowedTeams(world);
434 GetTeamCounts(world);
435 head = findchainfloat(isbot, true);
439 besttime = head.createdtime;
443 if(head.team == NUM_TEAM_1)
445 else if(head.team == NUM_TEAM_2)
447 else if(head.team == NUM_TEAM_3)
449 else if(head.team == NUM_TEAM_4)
453 if (thiscount > bestcount)
455 bestcount = thiscount;
456 besttime = head.createdtime;
459 else if (thiscount == bestcount && besttime < head.createdtime)
461 besttime = head.createdtime;
466 currentbots = currentbots - 1;
470 void bot_removenewest()
477 bot_removefromlargestteam();
481 head = findchainfloat(isbot, true);
485 besttime = head.createdtime;
488 if (besttime < head.createdtime)
490 besttime = head.createdtime;
495 currentbots = currentbots - 1;
499 void autoskill(float factor)
506 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
507 if(IS_REAL_CLIENT(it))
508 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
510 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
513 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
514 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
515 if(bestbot < 0 || bestplayer < 0)
517 LOG_TRACE("not doing anything\n");
518 // don't return, let it reset all counters below
520 else if(bestbot <= bestplayer * factor - 2)
522 if(autocvar_skill < 17)
524 LOG_TRACE("2 frags difference, increasing skill\n");
525 cvar_set("skill", ftos(autocvar_skill + 1));
526 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
529 else if(bestbot >= bestplayer * factor + 2)
531 if(autocvar_skill > 0)
533 LOG_TRACE("2 frags difference, decreasing skill\n");
534 cvar_set("skill", ftos(autocvar_skill - 1));
535 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
540 LOG_TRACE("not doing anything\n");
542 // don't reset counters, wait for them to accumulate
545 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
548 void bot_calculate_stepheightvec()
550 stepheightvec = autocvar_sv_stepheight * '0 0 1';
551 jumpstepheightvec = stepheightvec +
552 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
553 // 0.75 factor is for safety to make the jumps easy
558 int activerealplayers = 0;
560 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
561 activerealplayers = bot_activerealplayers;
562 realplayers = bot_realplayers;
564 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
572 // add/remove bots if needed to make sure there are at least
573 // minplayers+bot_number, or remove all bots if no one is playing
574 // But don't remove bots immediately on level change, as the real players
575 // usually haven't rejoined yet
576 bots_would_leave = false;
577 if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
578 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
579 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
581 float realminplayers, minplayers;
582 realminplayers = autocvar_minplayers;
583 minplayers = max(0, floor(realminplayers));
585 float realminbots, minbots;
586 realminbots = autocvar_bot_number;
587 minbots = max(0, floor(realminbots));
589 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
591 bots_would_leave = true;
595 // if there are no players, remove bots
599 // only add one bot per frame to avoid utter chaos
600 if(time > botframe_nextthink)
602 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
603 while (currentbots < bots)
605 if (bot_spawn() == world)
607 bprint("Can not add bot, server full.\n");
611 while (currentbots > bots)
618 void bot_serverframe()
620 if (intermission_running)
626 bot_calculate_stepheightvec();
627 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
629 if(time > autoskill_nextthink)
632 a = autocvar_skill_auto;
635 autoskill_nextthink = time + 5;
638 if(time > botframe_nextthink)
641 botframe_nextthink = time + 10;
644 bot_ignore_bots = autocvar_bot_ignore_bots;
646 if(botframe_spawnedwaypoints)
648 if(autocvar_waypoint_benchmark)
652 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
653 if (botframe_spawnedwaypoints)
655 if(botframe_cachedwaypointlinks)
657 if(!botframe_loadedforcedlinks)
658 waypoint_load_links_hardwired();
662 // TODO: Make this check cleaner
663 entity wp = findchain(classname, "waypoint");
664 if(time - wp.nextthink > 10)
665 waypoint_save_links();
670 botframe_spawnedwaypoints = true;
672 if(!waypoint_load_links())
673 waypoint_schedulerelinkall();
678 // cycle the goal token from one bot to the next each frame
679 // (this prevents them from all doing spawnfunc_waypoint searches on the same
680 // frame, which causes choppy framerates)
681 if (bot_strategytoken_taken)
683 bot_strategytoken_taken = false;
684 if (bot_strategytoken)
685 bot_strategytoken = bot_strategytoken.nextbot;
686 if (!bot_strategytoken)
687 bot_strategytoken = bot_list;
690 if (botframe_nextdangertime < time)
693 interval = autocvar_bot_ai_dangerdetectioninterval;
694 if (botframe_nextdangertime < time - interval * 1.5)
695 botframe_nextdangertime = time;
696 botframe_nextdangertime = botframe_nextdangertime + interval;
697 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
701 if (autocvar_g_waypointeditor)
702 botframe_showwaypointlinks();
704 if (autocvar_g_waypointeditor_auto)
705 botframe_autowaypoints();
707 if(time > bot_cvar_nextthink)
710 bot_custom_weapon_priority_setup();
711 bot_cvar_nextthink = time + 5;