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"
15 local entity oldself, bot;
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;
102 // now call the current bot AI (havocbot for example)
106 void bot_setnameandstuff()
109 float file, tokens, prio;
112 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
113 string name, prefix, suffix;
115 if(autocvar_g_campaign)
122 prefix = autocvar_bot_prefix;
123 suffix = autocvar_bot_suffix;
126 file = fopen(autocvar_bot_config_file, FILE_READ);
129 print(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
132 RandomSelection_Init();
135 readfile = fgets(file);
138 if(substring(readfile, 0, 2) == "//")
140 if(substring(readfile, 0, 1) == "#")
142 tokens = tokenizebyseparator(readfile, "\t");
147 if(clienttype(p) == CLIENTTYPE_BOT)
154 RandomSelection_Add(world, 0, readfile, 1, prio);
156 readfile = RandomSelection_chosen_string;
160 tokens = tokenizebyseparator(readfile, "\t");
161 if(argv(0) != "") bot_name = argv(0);
162 else bot_name = "Bot";
164 if(argv(1) != "") bot_model = argv(1);
167 if(argv(2) != "") bot_skin = argv(2);
170 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
171 else bot_shirt = ftos(floor(random() * 15));
173 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
174 else bot_pants = ftos(floor(random() * 15));
176 self.bot_forced_team = stof(argv(5));
180 #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
181 //print(bot_name, ": ping=", argv(9), "\n");
183 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
184 READSKILL(bot_moveskill, 2, 0); // move skill
185 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
187 READSKILL(bot_pingskill, 0.5, 0); // ping skill
189 READSKILL(bot_weaponskill, 2, 0); // weapon skill
190 READSKILL(bot_aggresskill, 1, 0); // aggre skill
191 READSKILL(bot_rangepreference, 1, 0); // read skill
193 READSKILL(bot_aimskill, 2, 0); // aim skill
194 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
195 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
197 READSKILL(bot_thinkskill, 1, 0.5); // think skill
198 READSKILL(bot_aiskill, 2, 0); // "ai" skill
200 self.bot_config_loaded = TRUE;
202 // this is really only a default, JoinBestTeam is called later
203 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
204 self.bot_preferredcolors = self.clientcolors;
207 if (autocvar_bot_usemodelnames)
212 // number bots with identical names
217 if(clienttype(p) == CLIENTTYPE_BOT)
218 if(p.cleanname == name)
222 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
224 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
226 self.cleanname = strzone(name);
228 // pick the model and skin
229 if(substring(bot_model, -4, 1) != ".")
230 bot_model = strcat(bot_model, ".iqm");
231 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
232 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
234 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
235 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
238 void bot_custom_weapon_priority_setup()
240 local float tokens, i, c, w;
242 bot_custom_weapon = FALSE;
244 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
245 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
246 autocvar_bot_ai_custom_weapon_priority_close == "" ||
247 autocvar_bot_ai_custom_weapon_priority_distances == ""
252 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
257 bot_distance_far = stof(argv(0));
258 bot_distance_close = stof(argv(1));
260 if(bot_distance_far < bot_distance_close){
261 bot_distance_far = stof(argv(1));
262 bot_distance_close = stof(argv(0));
265 // Initialize list of weapons
266 bot_weapons_far[0] = -1;
267 bot_weapons_mid[0] = -1;
268 bot_weapons_close[0] = -1;
270 // Parse far distance weapon priorities
271 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
274 for(i=0; i < tokens && c < WEP_COUNT; ++i){
276 if ( w >= WEP_FIRST && w <= WEP_LAST) {
277 bot_weapons_far[c] = w;
282 bot_weapons_far[c] = -1;
284 // Parse mid distance weapon priorities
285 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
288 for(i=0; i < tokens && c < WEP_COUNT; ++i){
290 if ( w >= WEP_FIRST && w <= WEP_LAST) {
291 bot_weapons_mid[c] = w;
296 bot_weapons_mid[c] = -1;
298 // Parse close distance weapon priorities
299 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
302 for(i=0; i < tokens && i < WEP_COUNT; ++i){
304 if ( w >= WEP_FIRST && w <= WEP_LAST) {
305 bot_weapons_close[c] = w;
310 bot_weapons_close[c] = -1;
312 bot_custom_weapon = TRUE;
318 //dprint("bot_endgame\n");
322 setcolor(e, e.bot_preferredcolors);
325 // if dynamic waypoints are ever implemented, save them here
328 void bot_relinkplayerlist()
331 local entity prevbot;
334 player_list = e = findchainflags(flags, FL_CLIENT);
339 player_count = player_count + 1;
340 e.nextplayer = e.chain;
341 if (clienttype(e) == CLIENTTYPE_BOT)
348 bot_list.nextbot = world;
351 currentbots = currentbots + 1;
355 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
356 bot_strategytoken = bot_list;
357 bot_strategytoken_taken = TRUE;
360 void bot_clientdisconnect()
362 if (clienttype(self) != CLIENTTYPE_BOT)
365 strunzone(self.cleanname);
366 if(self.netname_freeme)
367 strunzone(self.netname_freeme);
368 if(self.playermodel_freeme)
369 strunzone(self.playermodel_freeme);
370 if(self.playerskin_freeme)
371 strunzone(self.playerskin_freeme);
372 self.cleanname = string_null;
373 self.netname_freeme = string_null;
374 self.playermodel_freeme = string_null;
375 self.playerskin_freeme = string_null;
376 remove(self.bot_cmd_current);
379 void bot_clientconnect()
381 if (clienttype(self) != CLIENTTYPE_BOT)
383 self.bot_preferredcolors = self.clientcolors;
384 self.bot_nextthink = time - random();
385 self.lag_func = bot_lagfunc;
387 self.createdtime = self.nextthink;
389 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
390 bot_setnameandstuff();
392 if(self.bot_forced_team==1)
393 self.team = COLOR_TEAM1;
394 else if(self.bot_forced_team==2)
395 self.team = COLOR_TEAM2;
396 else if(self.bot_forced_team==3)
397 self.team = COLOR_TEAM3;
398 else if(self.bot_forced_team==4)
399 self.team = COLOR_TEAM4;
401 JoinBestTeam(self, FALSE, TRUE);
406 void bot_removefromlargestteam()
408 local float besttime, bestcount, thiscount;
409 local entity best, head;
410 CheckAllowedTeams(world);
411 GetTeamCounts(world);
412 head = findchainfloat(isbot, TRUE);
416 besttime = head.createdtime;
420 if(head.team == COLOR_TEAM1)
422 else if(head.team == COLOR_TEAM2)
424 else if(head.team == COLOR_TEAM3)
426 else if(head.team == COLOR_TEAM4)
430 if (thiscount > bestcount)
432 bestcount = thiscount;
433 besttime = head.createdtime;
436 else if (thiscount == bestcount && besttime < head.createdtime)
438 besttime = head.createdtime;
443 currentbots = currentbots - 1;
447 void bot_removenewest()
449 local float besttime;
450 local entity best, head;
454 bot_removefromlargestteam();
458 head = findchainfloat(isbot, TRUE);
462 besttime = head.createdtime;
465 if (besttime < head.createdtime)
467 besttime = head.createdtime;
472 currentbots = currentbots - 1;
476 void autoskill(float factor)
484 FOR_EACH_PLAYER(head)
486 if(clienttype(head) == CLIENTTYPE_REAL)
487 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
489 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
492 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
493 dprint("best bot got ", ftos(bestbot), "; ");
494 if(bestbot < 0 || bestplayer < 0)
496 dprint("not doing anything\n");
497 // don't return, let it reset all counters below
499 else if(bestbot <= bestplayer * factor - 2)
501 if(autocvar_skill < 17)
503 dprint("2 frags difference, increasing skill\n");
504 cvar_set("skill", ftos(autocvar_skill + 1));
505 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
508 else if(bestbot >= bestplayer * factor + 2)
510 if(autocvar_skill > 0)
512 dprint("2 frags difference, decreasing skill\n");
513 cvar_set("skill", ftos(autocvar_skill - 1));
514 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
519 dprint("not doing anything\n");
521 // don't reset counters, wait for them to accumulate
524 FOR_EACH_PLAYER(head)
525 head.totalfrags_lastcheck = head.totalfrags;
528 void bot_serverframe()
530 float realplayers, bots, activerealplayers;
533 if (intermission_running)
539 stepheightvec = autocvar_sv_stepheight * '0 0 1';
540 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
542 if(time > autoskill_nextthink)
545 a = autocvar_skill_auto;
548 autoskill_nextthink = time + 5;
551 activerealplayers = 0;
554 FOR_EACH_REALCLIENT(head)
556 if(head.classname == "player" || g_lms || g_arena || g_ca)
561 // add/remove bots if needed to make sure there are at least
562 // minplayers+bot_number, or remove all bots if no one is playing
563 // But don't remove bots immediately on level change, as the real players
564 // usually haven't rejoined yet
565 bots_would_leave = FALSE;
566 if (teams_matter && autocvar_bot_vs_human && (c3==-1 && c4==-1))
567 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
568 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
570 float realminplayers, minplayers;
571 realminplayers = autocvar_minplayers;
572 minplayers = max(0, floor(realminplayers));
574 float realminbots, minbots;
575 realminbots = autocvar_bot_number;
576 minbots = max(0, floor(realminbots));
578 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
580 bots_would_leave = TRUE;
584 // if there are no players, remove bots
588 bot_ignore_bots = autocvar_bot_ignore_bots;
590 // only add one bot per frame to avoid utter chaos
591 if(time > botframe_nextthink)
593 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
594 while (currentbots < bots)
596 if (bot_spawn() == world)
598 bprint("Can not add bot, server full.\n");
599 botframe_nextthink = time + 10;
603 while (currentbots > bots)
607 if(botframe_spawnedwaypoints)
609 if(autocvar_waypoint_benchmark)
613 if (currentbots > 0 || autocvar_g_waypointeditor)
614 if (botframe_spawnedwaypoints)
616 if(botframe_cachedwaypointlinks)
618 if(!botframe_loadedforcedlinks)
619 waypoint_load_links_hardwired();
623 // TODO: Make this check cleaner
624 local entity wp = findchain(classname, "waypoint");
625 if(time - wp.nextthink > 10)
626 waypoint_save_links();
631 botframe_spawnedwaypoints = TRUE;
633 if(!waypoint_load_links())
634 waypoint_schedulerelinkall();
639 // cycle the goal token from one bot to the next each frame
640 // (this prevents them from all doing spawnfunc_waypoint searches on the same
641 // frame, which causes choppy framerates)
642 if (bot_strategytoken_taken)
644 bot_strategytoken_taken = FALSE;
645 if (bot_strategytoken)
646 bot_strategytoken = bot_strategytoken.nextbot;
647 if (!bot_strategytoken)
648 bot_strategytoken = bot_list;
651 if (botframe_nextdangertime < time)
653 local float interval;
654 interval = autocvar_bot_ai_dangerdetectioninterval;
655 if (botframe_nextdangertime < time - interval * 1.5)
656 botframe_nextdangertime = time;
657 botframe_nextdangertime = botframe_nextdangertime + interval;
658 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
662 if (autocvar_g_waypointeditor)
663 botframe_showwaypointlinks();
665 if(time > bot_cvar_nextthink)
668 bot_custom_weapon_priority_setup();
669 bot_cvar_nextthink = time + 5;