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/physics/player.qh>
27 #include <common/constants.qh>
28 #include <common/mapinfo.qh>
29 #include <common/teams.qh>
30 #include <common/util.qh>
32 #include <common/weapons/all.qh>
34 #include <lib/csqcmodel/sv_model.qh>
36 #include <lib/warpzone/common.qh>
37 #include <lib/warpzone/util_server.qh>
41 entity bot = spawnclient();
44 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)
96 PHYS_INPUT_BUTTON_ATCK(self) = false;
97 PHYS_INPUT_BUTTON_JUMP(self) = false;
98 PHYS_INPUT_BUTTON_ATCK2(self) = false;
99 PHYS_INPUT_BUTTON_ZOOM(self) = false;
100 PHYS_INPUT_BUTTON_CROUCH(self) = false;
101 PHYS_INPUT_BUTTON_HOOK(self) = false;
102 PHYS_INPUT_BUTTON_INFO(self) = false;
103 PHYS_INPUT_BUTTON_DRAG(self) = false;
104 PHYS_INPUT_BUTTON_CHAT(self) = false;
105 PHYS_INPUT_BUTTON_USE(self) = false;
107 if (time < game_starttime)
109 // block the bot during the countdown to game start
110 self.movement = '0 0 0';
111 self.bot_nextthink = game_starttime;
115 // if dead, just wait until we can respawn
118 if (self.deadflag == DEAD_DEAD)
120 PHYS_INPUT_BUTTON_JUMP(self) = true; // press jump to respawn
121 self.bot_strategytime = 0;
124 else if(self.aistatus & AI_STATUS_STUCK)
125 navigation_unstuck();
127 // now call the current bot AI (havocbot for example)
131 void bot_setnameandstuff()
134 float file, tokens, prio;
136 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
137 string name, prefix, suffix;
139 if(autocvar_g_campaign)
146 prefix = autocvar_bot_prefix;
147 suffix = autocvar_bot_suffix;
150 file = fopen(autocvar_bot_config_file, FILE_READ);
154 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
159 RandomSelection_Init();
160 while((readfile = fgets(file)))
162 if(substring(readfile, 0, 2) == "//")
164 if(substring(readfile, 0, 1) == "#")
166 tokens = tokenizebyseparator(readfile, "\t");
171 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
172 if(s == it.cleanname)
178 RandomSelection_Add(world, 0, readfile, 1, prio);
180 readfile = RandomSelection_chosen_string;
184 tokens = tokenizebyseparator(readfile, "\t");
185 if(argv(0) != "") bot_name = argv(0);
186 else bot_name = "Bot";
188 if(argv(1) != "") bot_model = argv(1);
191 if(argv(2) != "") bot_skin = argv(2);
194 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
195 else bot_shirt = ftos(floor(random() * 15));
197 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
198 else bot_pants = ftos(floor(random() * 15));
200 self.bot_forced_team = stof(argv(5));
204 #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
205 //print(bot_name, ": ping=", argv(9), "\n");
207 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
208 READSKILL(bot_moveskill, 2, 0); // move skill
209 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
211 READSKILL(bot_pingskill, 0.5, 0); // ping skill
213 READSKILL(bot_weaponskill, 2, 0); // weapon skill
214 READSKILL(bot_aggresskill, 1, 0); // aggre skill
215 READSKILL(bot_rangepreference, 1, 0); // read skill
217 READSKILL(bot_aimskill, 2, 0); // aim skill
218 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
219 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
221 READSKILL(bot_thinkskill, 1, 0.5); // think skill
222 READSKILL(bot_aiskill, 2, 0); // "ai" skill
224 self.bot_config_loaded = true;
226 // this is really only a default, JoinBestTeam is called later
227 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
228 self.bot_preferredcolors = self.clientcolors;
231 if (autocvar_bot_usemodelnames)
236 // number bots with identical names
238 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
239 if(it.cleanname == name)
243 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
245 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
247 self.cleanname = strzone(name);
249 // pick the model and skin
250 if(substring(bot_model, -4, 1) != ".")
251 bot_model = strcat(bot_model, ".iqm");
252 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
253 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
255 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
256 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
259 void bot_custom_weapon_priority_setup()
263 bot_custom_weapon = false;
265 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
266 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
267 autocvar_bot_ai_custom_weapon_priority_close == "" ||
268 autocvar_bot_ai_custom_weapon_priority_distances == ""
273 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
278 bot_distance_far = stof(argv(0));
279 bot_distance_close = stof(argv(1));
281 if(bot_distance_far < bot_distance_close){
282 bot_distance_far = stof(argv(1));
283 bot_distance_close = stof(argv(0));
286 // Initialize list of weapons
287 bot_weapons_far[0] = -1;
288 bot_weapons_mid[0] = -1;
289 bot_weapons_close[0] = -1;
291 // Parse far distance weapon priorities
292 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
295 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
297 if ( w >= WEP_FIRST && w <= WEP_LAST) {
298 bot_weapons_far[c] = w;
302 if(c < Weapons_COUNT)
303 bot_weapons_far[c] = -1;
305 // Parse mid distance weapon priorities
306 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
309 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
311 if ( w >= WEP_FIRST && w <= WEP_LAST) {
312 bot_weapons_mid[c] = w;
316 if(c < Weapons_COUNT)
317 bot_weapons_mid[c] = -1;
319 // Parse close distance weapon priorities
320 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
323 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
325 if ( w >= WEP_FIRST && w <= WEP_LAST) {
326 bot_weapons_close[c] = w;
330 if(c < Weapons_COUNT)
331 bot_weapons_close[c] = -1;
333 bot_custom_weapon = true;
339 //dprint("bot_endgame\n");
343 setcolor(e, e.bot_preferredcolors);
346 // if dynamic waypoints are ever implemented, save them here
349 void bot_relinkplayerlist()
355 player_list = e = findchainflags(flags, FL_CLIENT);
360 player_count = player_count + 1;
361 e.nextplayer = e.chain;
362 if (IS_BOT_CLIENT(e))
369 bot_list.nextbot = world;
372 currentbots = currentbots + 1;
376 LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
377 bot_strategytoken = bot_list;
378 bot_strategytoken_taken = true;
381 void bot_clientdisconnect()
383 if (!IS_BOT_CLIENT(self))
385 bot_clearqueue(self);
387 strunzone(self.cleanname);
388 if(self.netname_freeme)
389 strunzone(self.netname_freeme);
390 if(self.playermodel_freeme)
391 strunzone(self.playermodel_freeme);
392 if(self.playerskin_freeme)
393 strunzone(self.playerskin_freeme);
394 self.cleanname = string_null;
395 self.netname_freeme = string_null;
396 self.playermodel_freeme = string_null;
397 self.playerskin_freeme = string_null;
398 if(self.bot_cmd_current)
399 remove(self.bot_cmd_current);
400 if(bot_waypoint_queue_owner==self)
401 bot_waypoint_queue_owner = world;
404 void bot_clientconnect(entity this)
406 if (!IS_BOT_CLIENT(this)) return;
407 self.bot_preferredcolors = self.clientcolors;
408 self.bot_nextthink = time - random();
409 self.lag_func = bot_lagfunc;
411 self.createdtime = self.bot_nextthink;
413 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
414 bot_setnameandstuff();
416 if(self.bot_forced_team==1)
417 setteam(self, NUM_TEAM_1);
418 else if(self.bot_forced_team==2)
419 setteam(self, NUM_TEAM_2);
420 else if(self.bot_forced_team==3)
421 setteam(self, NUM_TEAM_3);
422 else if(self.bot_forced_team==4)
423 setteam(self, NUM_TEAM_4);
425 JoinBestTeam(self, false, true);
430 void bot_removefromlargestteam()
432 float besttime, bestcount, thiscount;
434 CheckAllowedTeams(world);
435 GetTeamCounts(world);
436 head = findchainfloat(isbot, true);
440 besttime = head.createdtime;
444 if(TMID(head.team) == NUM_TEAM_1)
446 else if(TMID(head.team) == NUM_TEAM_2)
448 else if(TMID(head.team) == NUM_TEAM_3)
450 else if(TMID(head.team) == NUM_TEAM_4)
454 if (thiscount > bestcount)
456 bestcount = thiscount;
457 besttime = head.createdtime;
460 else if (thiscount == bestcount && besttime < head.createdtime)
462 besttime = head.createdtime;
467 currentbots = currentbots - 1;
471 void bot_removenewest()
478 bot_removefromlargestteam();
482 head = findchainfloat(isbot, true);
486 besttime = head.createdtime;
489 if (besttime < head.createdtime)
491 besttime = head.createdtime;
496 currentbots = currentbots - 1;
500 void autoskill(float factor)
507 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
508 if(IS_REAL_CLIENT(it))
509 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
511 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
514 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
515 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
516 if(bestbot < 0 || bestplayer < 0)
518 LOG_TRACE("not doing anything\n");
519 // don't return, let it reset all counters below
521 else if(bestbot <= bestplayer * factor - 2)
523 if(autocvar_skill < 17)
525 LOG_TRACE("2 frags difference, increasing skill\n");
526 cvar_set("skill", ftos(autocvar_skill + 1));
527 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
530 else if(bestbot >= bestplayer * factor + 2)
532 if(autocvar_skill > 0)
534 LOG_TRACE("2 frags difference, decreasing skill\n");
535 cvar_set("skill", ftos(autocvar_skill - 1));
536 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
541 LOG_TRACE("not doing anything\n");
543 // don't reset counters, wait for them to accumulate
546 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
549 void bot_calculate_stepheightvec()
551 stepheightvec = autocvar_sv_stepheight * '0 0 1';
552 jumpstepheightvec = stepheightvec +
553 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
554 // 0.75 factor is for safety to make the jumps easy
559 int activerealplayers = 0;
561 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
562 activerealplayers = bot_activerealplayers;
563 realplayers = bot_realplayers;
565 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
573 // add/remove bots if needed to make sure there are at least
574 // minplayers+bot_number, or remove all bots if no one is playing
575 // But don't remove bots immediately on level change, as the real players
576 // usually haven't rejoined yet
577 bots_would_leave = false;
578 if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
579 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
580 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
582 float realminplayers, minplayers;
583 realminplayers = autocvar_minplayers;
584 minplayers = max(0, floor(realminplayers));
586 float realminbots, minbots;
587 realminbots = autocvar_bot_number;
588 minbots = max(0, floor(realminbots));
590 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
592 bots_would_leave = true;
596 // if there are no players, remove bots
600 // only add one bot per frame to avoid utter chaos
601 if(time > botframe_nextthink)
603 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
604 while (currentbots < bots)
606 if (bot_spawn() == world)
608 bprint("Can not add bot, server full.\n");
612 while (currentbots > bots)
619 void bot_serverframe()
621 if (intermission_running)
627 bot_calculate_stepheightvec();
628 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
630 if(time > autoskill_nextthink)
633 a = autocvar_skill_auto;
636 autoskill_nextthink = time + 5;
639 if(time > botframe_nextthink)
642 botframe_nextthink = time + 10;
645 bot_ignore_bots = autocvar_bot_ignore_bots;
647 if(botframe_spawnedwaypoints)
649 if(autocvar_waypoint_benchmark)
653 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
654 if (botframe_spawnedwaypoints)
656 if(botframe_cachedwaypointlinks)
658 if(!botframe_loadedforcedlinks)
659 waypoint_load_links_hardwired();
663 // TODO: Make this check cleaner
664 entity wp = findchain(classname, "waypoint");
665 if(time - wp.nextthink > 10)
666 waypoint_save_links();
671 botframe_spawnedwaypoints = true;
673 if(!waypoint_load_links())
674 waypoint_schedulerelinkall();
679 // cycle the goal token from one bot to the next each frame
680 // (this prevents them from all doing spawnfunc_waypoint searches on the same
681 // frame, which causes choppy framerates)
682 if (bot_strategytoken_taken)
684 bot_strategytoken_taken = false;
685 if (bot_strategytoken)
686 bot_strategytoken = bot_strategytoken.nextbot;
687 if (!bot_strategytoken)
688 bot_strategytoken = bot_list;
691 if (botframe_nextdangertime < time)
694 interval = autocvar_bot_ai_dangerdetectioninterval;
695 if (botframe_nextdangertime < time - interval * 1.5)
696 botframe_nextdangertime = time;
697 botframe_nextdangertime = botframe_nextdangertime + interval;
698 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
702 if (autocvar_g_waypointeditor)
703 botframe_showwaypointlinks();
705 if (autocvar_g_waypointeditor_auto)
706 botframe_autowaypoints();
708 if(time > bot_cvar_nextthink)
711 bot_custom_weapon_priority_setup();
712 bot_cvar_nextthink = time + 5;