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 + cvar("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(time < game_starttime || ((cvar("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 dead, just wait until we can respawn
87 if (self.deadflag == DEAD_DEAD)
89 self.BUTTON_JUMP = 1; // press jump to respawn
90 self.bot_strategytime = 0;
94 // now call the current bot AI (havocbot for example)
98 void bot_setnameandstuff()
101 float file, tokens, prio;
104 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
105 string name, prefix, suffix;
107 if(cvar("g_campaign"))
114 prefix = cvar_string("bot_prefix");
115 suffix = cvar_string("bot_suffix");
118 file = fopen(cvar_string("bot_config_file"), FILE_READ);
121 print(strcat("Error: Can not open the bot configuration file '",cvar_string("bot_config_file"),"'\n"));
124 RandomSelection_Init();
127 readfile = fgets(file);
130 if(substring(readfile, 0, 2) == "//")
132 if(substring(readfile, 0, 1) == "#")
134 tokens = tokenizebyseparator(readfile, "\t");
139 if(clienttype(p) == CLIENTTYPE_BOT)
146 RandomSelection_Add(world, 0, readfile, 1, prio);
148 readfile = RandomSelection_chosen_string;
152 tokens = tokenizebyseparator(readfile, "\t");
153 if(argv(0) != "") bot_name = argv(0);
154 else bot_name = "Bot";
156 if(argv(1) != "") bot_model = argv(1);
159 if(argv(2) != "") bot_skin = argv(2);
162 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
163 else bot_shirt = ftos(floor(random() * 15));
165 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
166 else bot_pants = ftos(floor(random() * 15));
168 self.bot_forced_team = stof(argv(5));
172 #define READSKILL(f,w,r) if(argv(prio) != "") self.f = stof(argv(prio)) * (w); else self.f = (!cvar("g_campaign")) * (2 * random() - 1) * (r) * (w); ++prio
173 //print(bot_name, ": ping=", argv(9), "\n");
175 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
176 READSKILL(bot_moveskill, 2, 0); // move skill
177 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
179 READSKILL(bot_pingskill, 0.5, 0); // ping skill
181 READSKILL(bot_weaponskill, 2, 0); // weapon skill
182 READSKILL(bot_aggresskill, 1, 0); // aggre skill
183 READSKILL(bot_rangepreference, 1, 0); // read skill
185 READSKILL(bot_aimskill, 2, 0); // aim skill
186 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
187 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
189 READSKILL(bot_thinkskill, 1, 0.5); // think skill
190 READSKILL(bot_aiskill, 2, 0); // "ai" skill
192 self.bot_config_loaded = TRUE;
194 // this is really only a default, JoinBestTeam is called later
195 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
196 self.bot_preferredcolors = self.clientcolors;
199 if (cvar("bot_usemodelnames"))
204 // number bots with identical names
209 if(clienttype(p) == CLIENTTYPE_BOT)
210 if(p.cleanname == name)
214 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
216 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
218 self.cleanname = strzone(name);
220 // pick the model and skin
221 if(substring(bot_model, -4, 1) != ".")
222 bot_model = strcat(bot_model, ".iqm");
223 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
224 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
226 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
227 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
230 void bot_custom_weapon_priority_setup()
232 local float tokens, i, c, w;
234 bot_custom_weapon = FALSE;
236 if( cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
237 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
238 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
239 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
244 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
249 bot_distance_far = stof(argv(0));
250 bot_distance_close = stof(argv(1));
252 if(bot_distance_far < bot_distance_close){
253 bot_distance_far = stof(argv(1));
254 bot_distance_close = stof(argv(0));
257 // Initialize list of weapons
258 bot_weapons_far[0] = -1;
259 bot_weapons_mid[0] = -1;
260 bot_weapons_close[0] = -1;
262 // Parse far distance weapon priorities
263 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_far"))," ");
266 for(i=0; i < tokens && c < WEP_COUNT; ++i){
268 if ( w >= WEP_FIRST && w <= WEP_LAST) {
269 bot_weapons_far[c] = w;
274 bot_weapons_far[c] = -1;
276 // Parse mid distance weapon priorities
277 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_mid"))," ");
280 for(i=0; i < tokens && c < WEP_COUNT; ++i){
282 if ( w >= WEP_FIRST && w <= WEP_LAST) {
283 bot_weapons_mid[c] = w;
288 bot_weapons_mid[c] = -1;
290 // Parse close distance weapon priorities
291 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_close"))," ");
294 for(i=0; i < tokens && i < WEP_COUNT; ++i){
296 if ( w >= WEP_FIRST && w <= WEP_LAST) {
297 bot_weapons_close[c] = w;
302 bot_weapons_close[c] = -1;
304 bot_custom_weapon = TRUE;
310 //dprint("bot_endgame\n");
314 setcolor(e, e.bot_preferredcolors);
317 // if dynamic waypoints are ever implemented, save them here
320 void bot_relinkplayerlist()
323 local entity prevbot;
326 player_list = e = findchainflags(flags, FL_CLIENT);
331 player_count = player_count + 1;
332 e.nextplayer = e.chain;
333 if (clienttype(e) == CLIENTTYPE_BOT)
340 bot_list.nextbot = world;
343 currentbots = currentbots + 1;
347 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
348 bot_strategytoken = bot_list;
349 bot_strategytoken_taken = TRUE;
352 void bot_clientdisconnect()
354 if (clienttype(self) != CLIENTTYPE_BOT)
357 strunzone(self.cleanname);
358 if(self.netname_freeme)
359 strunzone(self.netname_freeme);
360 if(self.playermodel_freeme)
361 strunzone(self.playermodel_freeme);
362 if(self.playerskin_freeme)
363 strunzone(self.playerskin_freeme);
364 self.cleanname = string_null;
365 self.netname_freeme = string_null;
366 self.playermodel_freeme = string_null;
367 self.playerskin_freeme = string_null;
368 remove(self.bot_cmd_current);
371 void bot_clientconnect()
373 if (clienttype(self) != CLIENTTYPE_BOT)
375 self.bot_preferredcolors = self.clientcolors;
376 self.bot_nextthink = time - random();
377 self.lag_func = bot_lagfunc;
379 self.createdtime = self.nextthink;
381 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
382 bot_setnameandstuff();
384 if(self.bot_forced_team==1)
385 self.team = COLOR_TEAM1;
386 else if(self.bot_forced_team==2)
387 self.team = COLOR_TEAM2;
388 else if(self.bot_forced_team==3)
389 self.team = COLOR_TEAM3;
390 else if(self.bot_forced_team==4)
391 self.team = COLOR_TEAM4;
393 JoinBestTeam(self, FALSE, TRUE);
398 void bot_removefromlargestteam()
400 local float besttime, bestcount, thiscount;
401 local entity best, head;
402 CheckAllowedTeams(world);
403 GetTeamCounts(world);
404 head = findchainfloat(isbot, TRUE);
408 besttime = head.createdtime;
412 if(head.team == COLOR_TEAM1)
414 else if(head.team == COLOR_TEAM2)
416 else if(head.team == COLOR_TEAM3)
418 else if(head.team == COLOR_TEAM4)
422 if (thiscount > bestcount)
424 bestcount = thiscount;
425 besttime = head.createdtime;
428 else if (thiscount == bestcount && besttime < head.createdtime)
430 besttime = head.createdtime;
435 currentbots = currentbots - 1;
439 void bot_removenewest()
441 local float besttime;
442 local entity best, head;
446 bot_removefromlargestteam();
450 head = findchainfloat(isbot, TRUE);
454 besttime = head.createdtime;
457 if (besttime < head.createdtime)
459 besttime = head.createdtime;
464 currentbots = currentbots - 1;
468 void autoskill(float factor)
476 FOR_EACH_PLAYER(head)
478 if(clienttype(head) == CLIENTTYPE_REAL)
479 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
481 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
484 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
485 dprint("best bot got ", ftos(bestbot), "; ");
486 if(bestbot < 0 || bestplayer < 0)
488 dprint("not doing anything\n");
489 // don't return, let it reset all counters below
491 else if(bestbot <= bestplayer * factor - 2)
493 if(cvar("skill") < 17)
495 dprint("2 frags difference, increasing skill\n");
496 cvar_set("skill", ftos(cvar("skill") + 1));
497 bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
500 else if(bestbot >= bestplayer * factor + 2)
502 if(cvar("skill") > 0)
504 dprint("2 frags difference, decreasing skill\n");
505 cvar_set("skill", ftos(cvar("skill") - 1));
506 bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
511 dprint("not doing anything\n");
513 // don't reset counters, wait for them to accumulate
516 FOR_EACH_PLAYER(head)
517 head.totalfrags_lastcheck = head.totalfrags;
520 void bot_serverframe()
522 float realplayers, bots, activerealplayers;
525 if (intermission_running)
531 stepheightvec = cvar("sv_stepheight") * '0 0 1';
532 bot_navigation_movemode = ((cvar("bot_navigation_ignoreplayers")) ? MOVE_NOMONSTERS : MOVE_NORMAL);
534 if(time > autoskill_nextthink)
537 a = cvar("skill_auto");
540 autoskill_nextthink = time + 5;
543 activerealplayers = 0;
546 FOR_EACH_REALCLIENT(head)
548 if(head.classname == "player" || g_lms || g_arena || g_ca)
553 // add/remove bots if needed to make sure there are at least
554 // minplayers+bot_number, or remove all bots if no one is playing
555 // But don't remove bots immediately on level change, as the real players
556 // usually haven't rejoined yet
557 bots_would_leave = FALSE;
558 if ((realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5)))
560 float realminplayers, minplayers;
561 realminplayers = cvar("minplayers");
562 minplayers = max(0, floor(realminplayers));
564 float realminbots, minbots;
565 if(teamplay && cvar("bot_vs_human"))
566 realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
568 realminbots = cvar("bot_number");
569 minbots = max(0, floor(realminbots));
571 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
573 bots_would_leave = TRUE;
577 // if there are no players, remove bots
581 bot_ignore_bots = cvar("bot_ignore_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");
592 botframe_nextthink = time + 10;
596 while (currentbots > bots)
600 if(botframe_spawnedwaypoints)
602 if(cvar("waypoint_benchmark"))
606 if (currentbots > 0 || cvar("g_waypointeditor"))
607 if (botframe_spawnedwaypoints)
609 if(botframe_cachedwaypointlinks)
611 if(!botframe_loadedforcedlinks)
612 waypoint_load_links_hardwired();
616 // TODO: Make this check cleaner
617 local entity wp = findchain(classname, "waypoint");
618 if(time - wp.nextthink > 10)
619 waypoint_save_links();
624 botframe_spawnedwaypoints = TRUE;
626 if(!waypoint_load_links())
627 waypoint_schedulerelinkall();
632 // cycle the goal token from one bot to the next each frame
633 // (this prevents them from all doing spawnfunc_waypoint searches on the same
634 // frame, which causes choppy framerates)
635 if (bot_strategytoken_taken)
637 bot_strategytoken_taken = FALSE;
638 if (bot_strategytoken)
639 bot_strategytoken = bot_strategytoken.nextbot;
640 if (!bot_strategytoken)
641 bot_strategytoken = bot_list;
644 if (botframe_nextdangertime < time)
646 local float interval;
647 interval = cvar("bot_ai_dangerdetectioninterval");
648 if (botframe_nextdangertime < time - interval * 1.5)
649 botframe_nextdangertime = time;
650 botframe_nextdangertime = botframe_nextdangertime + interval;
651 botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
655 if (cvar("g_waypointeditor"))
656 botframe_showwaypointlinks();
658 if(time > bot_cvar_nextthink)
661 bot_custom_weapon_priority_setup();
662 bot_cvar_nextthink = time + 5;