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);
132 print(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
137 RandomSelection_Init();
140 readfile = fgets(file);
143 if(substring(readfile, 0, 2) == "//")
145 if(substring(readfile, 0, 1) == "#")
147 tokens = tokenizebyseparator(readfile, "\t");
152 if(clienttype(p) == CLIENTTYPE_BOT)
159 RandomSelection_Add(world, 0, readfile, 1, prio);
161 readfile = RandomSelection_chosen_string;
165 tokens = tokenizebyseparator(readfile, "\t");
166 if(argv(0) != "") bot_name = argv(0);
167 else bot_name = "Bot";
169 if(argv(1) != "") bot_model = argv(1);
172 if(argv(2) != "") bot_skin = argv(2);
175 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
176 else bot_shirt = ftos(floor(random() * 15));
178 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
179 else bot_pants = ftos(floor(random() * 15));
181 self.bot_forced_team = stof(argv(5));
185 #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
186 //print(bot_name, ": ping=", argv(9), "\n");
188 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
189 READSKILL(bot_moveskill, 2, 0); // move skill
190 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
192 READSKILL(bot_pingskill, 0.5, 0); // ping skill
194 READSKILL(bot_weaponskill, 2, 0); // weapon skill
195 READSKILL(bot_aggresskill, 1, 0); // aggre skill
196 READSKILL(bot_rangepreference, 1, 0); // read skill
198 READSKILL(bot_aimskill, 2, 0); // aim skill
199 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
200 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
202 READSKILL(bot_thinkskill, 1, 0.5); // think skill
203 READSKILL(bot_aiskill, 2, 0); // "ai" skill
205 self.bot_config_loaded = TRUE;
207 // this is really only a default, JoinBestTeam is called later
208 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
209 self.bot_preferredcolors = self.clientcolors;
212 if (autocvar_bot_usemodelnames)
217 // number bots with identical names
222 if(clienttype(p) == CLIENTTYPE_BOT)
223 if(p.cleanname == name)
227 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
229 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
231 self.cleanname = strzone(name);
233 // pick the model and skin
234 if(substring(bot_model, -4, 1) != ".")
235 bot_model = strcat(bot_model, ".iqm");
236 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
237 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
239 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
240 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
243 void bot_custom_weapon_priority_setup()
245 float tokens, i, c, w;
247 bot_custom_weapon = FALSE;
249 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
250 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
251 autocvar_bot_ai_custom_weapon_priority_close == "" ||
252 autocvar_bot_ai_custom_weapon_priority_distances == ""
257 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
262 bot_distance_far = stof(argv(0));
263 bot_distance_close = stof(argv(1));
265 if(bot_distance_far < bot_distance_close){
266 bot_distance_far = stof(argv(1));
267 bot_distance_close = stof(argv(0));
270 // Initialize list of weapons
271 bot_weapons_far[0] = -1;
272 bot_weapons_mid[0] = -1;
273 bot_weapons_close[0] = -1;
275 // Parse far distance weapon priorities
276 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
279 for(i=0; i < tokens && c < WEP_COUNT; ++i){
281 if ( w >= WEP_FIRST && w <= WEP_LAST) {
282 bot_weapons_far[c] = w;
287 bot_weapons_far[c] = -1;
289 // Parse mid distance weapon priorities
290 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
293 for(i=0; i < tokens && c < WEP_COUNT; ++i){
295 if ( w >= WEP_FIRST && w <= WEP_LAST) {
296 bot_weapons_mid[c] = w;
301 bot_weapons_mid[c] = -1;
303 // Parse close distance weapon priorities
304 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
307 for(i=0; i < tokens && i < WEP_COUNT; ++i){
309 if ( w >= WEP_FIRST && w <= WEP_LAST) {
310 bot_weapons_close[c] = w;
315 bot_weapons_close[c] = -1;
317 bot_custom_weapon = TRUE;
323 //dprint("bot_endgame\n");
327 setcolor(e, e.bot_preferredcolors);
330 // if dynamic waypoints are ever implemented, save them here
333 void bot_relinkplayerlist()
339 player_list = e = findchainflags(flags, FL_CLIENT);
344 player_count = player_count + 1;
345 e.nextplayer = e.chain;
346 if (clienttype(e) == CLIENTTYPE_BOT)
353 bot_list.nextbot = world;
356 currentbots = currentbots + 1;
360 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
361 bot_strategytoken = bot_list;
362 bot_strategytoken_taken = TRUE;
365 void bot_clientdisconnect()
367 if (clienttype(self) != CLIENTTYPE_BOT)
370 strunzone(self.cleanname);
371 if(self.netname_freeme)
372 strunzone(self.netname_freeme);
373 if(self.playermodel_freeme)
374 strunzone(self.playermodel_freeme);
375 if(self.playerskin_freeme)
376 strunzone(self.playerskin_freeme);
377 self.cleanname = string_null;
378 self.netname_freeme = string_null;
379 self.playermodel_freeme = string_null;
380 self.playerskin_freeme = string_null;
381 if(self.bot_cmd_current)
382 remove(self.bot_cmd_current);
383 if(bot_waypoint_queue_owner==self)
384 bot_waypoint_queue_owner = world;
387 void bot_clientconnect()
389 if (clienttype(self) != CLIENTTYPE_BOT)
391 self.bot_preferredcolors = self.clientcolors;
392 self.bot_nextthink = time - random();
393 self.lag_func = bot_lagfunc;
395 self.createdtime = self.nextthink;
397 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
398 bot_setnameandstuff();
400 if(self.bot_forced_team==1)
401 self.team = COLOR_TEAM1;
402 else if(self.bot_forced_team==2)
403 self.team = COLOR_TEAM2;
404 else if(self.bot_forced_team==3)
405 self.team = COLOR_TEAM3;
406 else if(self.bot_forced_team==4)
407 self.team = COLOR_TEAM4;
409 JoinBestTeam(self, FALSE, TRUE);
414 void bot_removefromlargestteam()
416 float besttime, bestcount, thiscount;
418 CheckAllowedTeams(world);
419 GetTeamCounts(world);
420 head = findchainfloat(isbot, TRUE);
424 besttime = head.createdtime;
428 if(head.team == COLOR_TEAM1)
430 else if(head.team == COLOR_TEAM2)
432 else if(head.team == COLOR_TEAM3)
434 else if(head.team == COLOR_TEAM4)
438 if (thiscount > bestcount)
440 bestcount = thiscount;
441 besttime = head.createdtime;
444 else if (thiscount == bestcount && besttime < head.createdtime)
446 besttime = head.createdtime;
451 currentbots = currentbots - 1;
455 void bot_removenewest()
462 bot_removefromlargestteam();
466 head = findchainfloat(isbot, TRUE);
470 besttime = head.createdtime;
473 if (besttime < head.createdtime)
475 besttime = head.createdtime;
480 currentbots = currentbots - 1;
484 void autoskill(float factor)
492 FOR_EACH_PLAYER(head)
494 if(clienttype(head) == CLIENTTYPE_REAL)
495 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
497 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
500 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
501 dprint("best bot got ", ftos(bestbot), "; ");
502 if(bestbot < 0 || bestplayer < 0)
504 dprint("not doing anything\n");
505 // don't return, let it reset all counters below
507 else if(bestbot <= bestplayer * factor - 2)
509 if(autocvar_skill < 17)
511 dprint("2 frags difference, increasing skill\n");
512 cvar_set("skill", ftos(autocvar_skill + 1));
513 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
516 else if(bestbot >= bestplayer * factor + 2)
518 if(autocvar_skill > 0)
520 dprint("2 frags difference, decreasing skill\n");
521 cvar_set("skill", ftos(autocvar_skill - 1));
522 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
527 dprint("not doing anything\n");
529 // don't reset counters, wait for them to accumulate
532 FOR_EACH_PLAYER(head)
533 head.totalfrags_lastcheck = head.totalfrags;
536 void bot_calculate_stepheightvec(void)
538 stepheightvec = autocvar_sv_stepheight * '0 0 1';
539 jumpstepheightvec = stepheightvec +
540 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
541 // 0.75 factor is for safety to make the jumps easy
547 float realplayers, bots, activerealplayers;
549 activerealplayers = 0;
552 FOR_EACH_REALCLIENT(head)
554 if(head.classname == "player" || g_lms || g_arena || g_ca)
559 // add/remove bots if needed to make sure there are at least
560 // minplayers+bot_number, or remove all bots if no one is playing
561 // But don't remove bots immediately on level change, as the real players
562 // usually haven't rejoined yet
563 bots_would_leave = FALSE;
564 if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
565 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
566 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
568 float realminplayers, minplayers;
569 realminplayers = autocvar_minplayers;
570 minplayers = max(0, floor(realminplayers));
572 float realminbots, minbots;
573 realminbots = autocvar_bot_number;
574 minbots = max(0, floor(realminbots));
576 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
578 bots_would_leave = TRUE;
582 // if there are no players, remove bots
586 // only add one bot per frame to avoid utter chaos
587 if(time > botframe_nextthink)
589 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
590 while (currentbots < bots)
592 if (bot_spawn() == world)
594 bprint("Can not add bot, server full.\n");
598 while (currentbots > bots)
605 void bot_serverframe()
607 if (intermission_running)
613 bot_calculate_stepheightvec();
614 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
616 if(time > autoskill_nextthink)
619 a = autocvar_skill_auto;
622 autoskill_nextthink = time + 5;
625 if(time > botframe_nextthink)
628 botframe_nextthink = time + 10;
631 bot_ignore_bots = autocvar_bot_ignore_bots;
633 if(botframe_spawnedwaypoints)
635 if(autocvar_waypoint_benchmark)
639 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
640 if (botframe_spawnedwaypoints)
642 if(botframe_cachedwaypointlinks)
644 if(!botframe_loadedforcedlinks)
645 waypoint_load_links_hardwired();
649 // TODO: Make this check cleaner
650 entity wp = findchain(classname, "waypoint");
651 if(time - wp.nextthink > 10)
652 waypoint_save_links();
657 botframe_spawnedwaypoints = TRUE;
659 if(!waypoint_load_links())
660 waypoint_schedulerelinkall();
665 // cycle the goal token from one bot to the next each frame
666 // (this prevents them from all doing spawnfunc_waypoint searches on the same
667 // frame, which causes choppy framerates)
668 if (bot_strategytoken_taken)
670 bot_strategytoken_taken = FALSE;
671 if (bot_strategytoken)
672 bot_strategytoken = bot_strategytoken.nextbot;
673 if (!bot_strategytoken)
674 bot_strategytoken = bot_list;
677 if (botframe_nextdangertime < time)
680 interval = autocvar_bot_ai_dangerdetectioninterval;
681 if (botframe_nextdangertime < time - interval * 1.5)
682 botframe_nextdangertime = time;
683 botframe_nextdangertime = botframe_nextdangertime + interval;
684 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
688 if (autocvar_g_waypointeditor)
689 botframe_showwaypointlinks();
691 if (autocvar_g_waypointeditor_auto)
692 botframe_autowaypoints();
694 if(time > bot_cvar_nextthink)
697 bot_custom_weapon_priority_setup();
698 bot_cvar_nextthink = time + 5;