3 #include "navigation.qh"
4 #include "waypoints.qh"
7 #include "navigation.qc"
8 #include "waypoints.qc"
9 #include "scripting.qc"
11 #include "havocbot/havocbot.qc"
19 currentbots = currentbots + 1;
22 bot_setnameandstuff();
32 if (self.bot_nextthink > time)
35 self.flags &~= FL_GODMODE;
37 self.flags |= FL_GODMODE;
39 self.bot_nextthink = self.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, self.bot_aiskill);
40 //if (self.bot_painintensity > 0)
41 // self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
43 //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
44 //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
46 if (autocvar_g_campaign && !campaign_bots_may_start)
48 self.nextthink = time + 0.5;
54 self.v_angle = self.angles;
56 self.fixangle = FALSE;
61 self.dmg_inflictor = world;
63 // calculate an aiming latency based on the skill setting
64 // (simulated network latency + naturally delayed reflexes)
65 //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
66 // minimum ping 20+10 random
67 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
68 // skill 10 = ping 0.2 (adrenaline)
69 // skill 0 = ping 0.7 (slightly drunk)
75 self.BUTTON_ATCK2 = 0;
77 self.BUTTON_CROUCH = 0;
84 if (time < game_starttime)
86 // block the bot during the countdown to game start
87 self.movement = '0 0 0';
88 self.nextthink = game_starttime;
92 // if dead, just wait until we can respawn
95 if (self.deadflag == DEAD_DEAD)
97 self.BUTTON_JUMP = 1; // press jump to respawn
98 self.bot_strategytime = 0;
101 else if(self.aistatus & AI_STATUS_STUCK)
102 navigation_unstuck();
104 // now call the current bot AI (havocbot for example)
108 void bot_setnameandstuff()
111 float file, tokens, prio;
114 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
115 string name, prefix, suffix;
117 if(autocvar_g_campaign)
124 prefix = autocvar_bot_prefix;
125 suffix = autocvar_bot_suffix;
128 file = fopen(autocvar_bot_config_file, FILE_READ);
131 print(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
134 RandomSelection_Init();
137 readfile = fgets(file);
140 if(substring(readfile, 0, 2) == "//")
142 if(substring(readfile, 0, 1) == "#")
144 tokens = tokenizebyseparator(readfile, "\t");
149 if(clienttype(p) == CLIENTTYPE_BOT)
156 RandomSelection_Add(world, 0, readfile, 1, prio);
158 readfile = RandomSelection_chosen_string;
162 tokens = tokenizebyseparator(readfile, "\t");
163 if(argv(0) != "") bot_name = argv(0);
164 else bot_name = "Bot";
166 if(argv(1) != "") bot_model = argv(1);
169 if(argv(2) != "") bot_skin = argv(2);
172 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
173 else bot_shirt = ftos(floor(random() * 15));
175 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
176 else bot_pants = ftos(floor(random() * 15));
178 self.bot_forced_team = stof(argv(5));
182 #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
183 //print(bot_name, ": ping=", argv(9), "\n");
185 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
186 READSKILL(bot_moveskill, 2, 0); // move skill
187 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
189 READSKILL(bot_pingskill, 0.5, 0); // ping skill
191 READSKILL(bot_weaponskill, 2, 0); // weapon skill
192 READSKILL(bot_aggresskill, 1, 0); // aggre skill
193 READSKILL(bot_rangepreference, 1, 0); // read skill
195 READSKILL(bot_aimskill, 2, 0); // aim skill
196 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
197 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
199 READSKILL(bot_thinkskill, 1, 0.5); // think skill
200 READSKILL(bot_aiskill, 2, 0); // "ai" skill
202 self.bot_config_loaded = TRUE;
204 // this is really only a default, JoinBestTeam is called later
205 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
206 self.bot_preferredcolors = self.clientcolors;
209 if (autocvar_bot_usemodelnames)
214 // number bots with identical names
219 if(clienttype(p) == CLIENTTYPE_BOT)
220 if(p.cleanname == name)
224 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
226 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
228 self.cleanname = strzone(name);
230 // pick the model and skin
231 if(substring(bot_model, -4, 1) != ".")
232 bot_model = strcat(bot_model, ".iqm");
233 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
234 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
236 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
237 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
240 void bot_custom_weapon_priority_setup()
242 float tokens, i, c, w;
244 bot_custom_weapon = FALSE;
246 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
247 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
248 autocvar_bot_ai_custom_weapon_priority_close == "" ||
249 autocvar_bot_ai_custom_weapon_priority_distances == ""
254 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
259 bot_distance_far = stof(argv(0));
260 bot_distance_close = stof(argv(1));
262 if(bot_distance_far < bot_distance_close){
263 bot_distance_far = stof(argv(1));
264 bot_distance_close = stof(argv(0));
267 // Initialize list of weapons
268 bot_weapons_far[0] = -1;
269 bot_weapons_mid[0] = -1;
270 bot_weapons_close[0] = -1;
272 // Parse far distance weapon priorities
273 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
276 for(i=0; i < tokens && c < WEP_COUNT; ++i){
278 if ( w >= WEP_FIRST && w <= WEP_LAST) {
279 bot_weapons_far[c] = w;
284 bot_weapons_far[c] = -1;
286 // Parse mid distance weapon priorities
287 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
290 for(i=0; i < tokens && c < WEP_COUNT; ++i){
292 if ( w >= WEP_FIRST && w <= WEP_LAST) {
293 bot_weapons_mid[c] = w;
298 bot_weapons_mid[c] = -1;
300 // Parse close distance weapon priorities
301 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
304 for(i=0; i < tokens && i < WEP_COUNT; ++i){
306 if ( w >= WEP_FIRST && w <= WEP_LAST) {
307 bot_weapons_close[c] = w;
312 bot_weapons_close[c] = -1;
314 bot_custom_weapon = TRUE;
320 //dprint("bot_endgame\n");
324 setcolor(e, e.bot_preferredcolors);
327 // if dynamic waypoints are ever implemented, save them here
330 void bot_relinkplayerlist()
336 player_list = e = findchainflags(flags, FL_CLIENT);
341 player_count = player_count + 1;
342 e.nextplayer = e.chain;
343 if (clienttype(e) == CLIENTTYPE_BOT)
350 bot_list.nextbot = world;
353 currentbots = currentbots + 1;
357 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
358 bot_strategytoken = bot_list;
359 bot_strategytoken_taken = TRUE;
362 void bot_clientdisconnect()
364 if (clienttype(self) != CLIENTTYPE_BOT)
367 strunzone(self.cleanname);
368 if(self.netname_freeme)
369 strunzone(self.netname_freeme);
370 if(self.playermodel_freeme)
371 strunzone(self.playermodel_freeme);
372 if(self.playerskin_freeme)
373 strunzone(self.playerskin_freeme);
374 self.cleanname = string_null;
375 self.netname_freeme = string_null;
376 self.playermodel_freeme = string_null;
377 self.playerskin_freeme = string_null;
378 if(self.bot_cmd_current)
379 remove(self.bot_cmd_current);
380 if(bot_waypoint_queue_owner==self)
381 bot_waypoint_queue_owner = world;
384 void bot_clientconnect()
386 if (clienttype(self) != CLIENTTYPE_BOT)
388 self.bot_preferredcolors = self.clientcolors;
389 self.bot_nextthink = time - random();
390 self.lag_func = bot_lagfunc;
392 self.createdtime = self.nextthink;
394 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
395 bot_setnameandstuff();
397 if(self.bot_forced_team==1)
398 self.team = COLOR_TEAM1;
399 else if(self.bot_forced_team==2)
400 self.team = COLOR_TEAM2;
401 else if(self.bot_forced_team==3)
402 self.team = COLOR_TEAM3;
403 else if(self.bot_forced_team==4)
404 self.team = COLOR_TEAM4;
406 JoinBestTeam(self, FALSE, TRUE);
411 void bot_removefromlargestteam()
413 float besttime, bestcount, thiscount;
415 CheckAllowedTeams(world);
416 GetTeamCounts(world);
417 head = findchainfloat(isbot, TRUE);
421 besttime = head.createdtime;
425 if(head.team == COLOR_TEAM1)
427 else if(head.team == COLOR_TEAM2)
429 else if(head.team == COLOR_TEAM3)
431 else if(head.team == COLOR_TEAM4)
435 if (thiscount > bestcount)
437 bestcount = thiscount;
438 besttime = head.createdtime;
441 else if (thiscount == bestcount && besttime < head.createdtime)
443 besttime = head.createdtime;
448 currentbots = currentbots - 1;
452 void bot_removenewest()
459 bot_removefromlargestteam();
463 head = findchainfloat(isbot, TRUE);
467 besttime = head.createdtime;
470 if (besttime < head.createdtime)
472 besttime = head.createdtime;
477 currentbots = currentbots - 1;
481 void autoskill(float factor)
489 FOR_EACH_PLAYER(head)
491 if(clienttype(head) == CLIENTTYPE_REAL)
492 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
494 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
497 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
498 dprint("best bot got ", ftos(bestbot), "; ");
499 if(bestbot < 0 || bestplayer < 0)
501 dprint("not doing anything\n");
502 // don't return, let it reset all counters below
504 else if(bestbot <= bestplayer * factor - 2)
506 if(autocvar_skill < 17)
508 dprint("2 frags difference, increasing skill\n");
509 cvar_set("skill", ftos(autocvar_skill + 1));
510 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
513 else if(bestbot >= bestplayer * factor + 2)
515 if(autocvar_skill > 0)
517 dprint("2 frags difference, decreasing skill\n");
518 cvar_set("skill", ftos(autocvar_skill - 1));
519 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
524 dprint("not doing anything\n");
526 // don't reset counters, wait for them to accumulate
529 FOR_EACH_PLAYER(head)
530 head.totalfrags_lastcheck = head.totalfrags;
533 void bot_calculate_stepheightvec(void)
535 stepheightvec = autocvar_sv_stepheight * '0 0 1';
536 jumpstepheightvec = stepheightvec +
537 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
538 // 0.75 factor is for safety to make the jumps easy
544 float realplayers, bots, activerealplayers;
546 activerealplayers = 0;
549 FOR_EACH_REALCLIENT(head)
551 if(head.classname == "player" || g_lms || g_arena || g_ca)
556 // add/remove bots if needed to make sure there are at least
557 // minplayers+bot_number, or remove all bots if no one is playing
558 // But don't remove bots immediately on level change, as the real players
559 // usually haven't rejoined yet
560 bots_would_leave = FALSE;
561 if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
562 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
563 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
565 float realminplayers, minplayers;
566 realminplayers = autocvar_minplayers;
567 minplayers = max(0, floor(realminplayers));
569 float realminbots, minbots;
570 realminbots = autocvar_bot_number;
571 minbots = max(0, floor(realminbots));
573 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
575 bots_would_leave = TRUE;
579 // if there are no players, remove bots
583 // only add one bot per frame to avoid utter chaos
584 if(time > botframe_nextthink)
586 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
587 while (currentbots < bots)
589 if (bot_spawn() == world)
591 bprint("Can not add bot, server full.\n");
595 while (currentbots > bots)
602 void bot_serverframe()
604 if (intermission_running)
610 bot_calculate_stepheightvec();
611 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
613 if(time > autoskill_nextthink)
616 a = autocvar_skill_auto;
619 autoskill_nextthink = time + 5;
622 if(time > botframe_nextthink)
625 botframe_nextthink = time + 10;
628 bot_ignore_bots = autocvar_bot_ignore_bots;
630 if(botframe_spawnedwaypoints)
632 if(autocvar_waypoint_benchmark)
636 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
637 if (botframe_spawnedwaypoints)
639 if(botframe_cachedwaypointlinks)
641 if(!botframe_loadedforcedlinks)
642 waypoint_load_links_hardwired();
646 // TODO: Make this check cleaner
647 entity wp = findchain(classname, "waypoint");
648 if(time - wp.nextthink > 10)
649 waypoint_save_links();
654 botframe_spawnedwaypoints = TRUE;
656 if(!waypoint_load_links())
657 waypoint_schedulerelinkall();
662 // cycle the goal token from one bot to the next each frame
663 // (this prevents them from all doing spawnfunc_waypoint searches on the same
664 // frame, which causes choppy framerates)
665 if (bot_strategytoken_taken)
667 bot_strategytoken_taken = FALSE;
668 if (bot_strategytoken)
669 bot_strategytoken = bot_strategytoken.nextbot;
670 if (!bot_strategytoken)
671 bot_strategytoken = bot_list;
674 if (botframe_nextdangertime < time)
677 interval = autocvar_bot_ai_dangerdetectioninterval;
678 if (botframe_nextdangertime < time - interval * 1.5)
679 botframe_nextdangertime = time;
680 botframe_nextdangertime = botframe_nextdangertime + interval;
681 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
685 if (autocvar_g_waypointeditor)
686 botframe_showwaypointlinks();
688 if (autocvar_g_waypointeditor_auto)
689 botframe_autowaypoints();
691 if(time > bot_cvar_nextthink)
694 bot_custom_weapon_priority_setup();
695 bot_cvar_nextthink = time + 5;