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;
78 self.BUTTON_JETPACK = 0;
83 self.BUTTON_DIGEST = 0;
84 self.BUTTON_REGURGITATE = 0;
86 // if dead, just wait until we can respawn
89 if (self.deadflag == DEAD_DEAD)
91 self.BUTTON_JUMP = 1; // press jump to respawn
92 self.bot_strategytime = 0;
96 // now call the current bot AI (havocbot for example)
100 void bot_setnameandstuff()
103 float file, tokens, prio;
106 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
107 string name, prefix, suffix;
109 if(cvar("g_campaign"))
116 prefix = cvar_string("bot_prefix");
117 suffix = cvar_string("bot_suffix");
120 file = fopen(cvar_string("bot_config_file"), FILE_READ);
123 print(strcat("Error: Can not open the bot configuration file '",cvar_string("bot_config_file"),"'\n"));
126 RandomSelection_Init();
129 readfile = fgets(file);
132 if(substring(readfile, 0, 2) == "//")
134 if(substring(readfile, 0, 1) == "#")
136 tokens = tokenizebyseparator(readfile, "\t");
141 if(clienttype(p) == CLIENTTYPE_BOT)
148 RandomSelection_Add(world, 0, readfile, 1, prio);
150 readfile = RandomSelection_chosen_string;
154 tokens = tokenizebyseparator(readfile, "\t");
155 if(argv(0) != "") bot_name = argv(0);
156 else bot_name = "Bot";
158 if(argv(1) != "") bot_model = argv(1);
161 if(argv(2) != "") bot_skin = argv(2);
164 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
165 else bot_shirt = ftos(floor(random() * 15));
167 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
168 else bot_pants = ftos(floor(random() * 15));
170 self.bot_forced_team = stof(argv(5));
174 #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
175 //print(bot_name, ": ping=", argv(9), "\n");
177 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
178 READSKILL(bot_moveskill, 2, 0); // move skill
179 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
181 READSKILL(bot_pingskill, 0.5, 0); // ping skill
183 READSKILL(bot_weaponskill, 2, 0); // weapon skill
184 READSKILL(bot_aggresskill, 1, 0); // aggre skill
185 READSKILL(bot_rangepreference, 1, 0); // read skill
187 READSKILL(bot_aimskill, 2, 0); // aim skill
188 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
189 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
191 READSKILL(bot_thinkskill, 1, 0.5); // think skill
192 READSKILL(bot_aiskill, 2, 0); // "ai" skill
194 READSKILL(bot_vorethinkpred, 1, 0.5); // vore predator skill
195 READSKILL(bot_vorethinkprey, 1, 0.5); // vore prey skill
196 READSKILL(bot_vorefear, 1, 0.5); // vore fear skill
197 READSKILL(bot_voreteamheal, 1, 0.5); // vore teamheal skill
199 self.bot_config_loaded = TRUE;
201 // this is really only a default, JoinBestTeam is called later
202 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
203 self.bot_preferredcolors = self.clientcolors;
206 if (cvar("bot_usemodelnames"))
211 // number bots with identical names
216 if(clienttype(p) == CLIENTTYPE_BOT)
217 if(p.cleanname == name)
221 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
223 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
225 self.cleanname = strzone(name);
227 // pick the model and skin
228 if(substring(bot_model, -4, 1) != ".")
229 bot_model = strcat(bot_model, ".iqm");
230 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
231 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
233 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
234 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
237 void bot_custom_weapon_priority_setup()
239 local float tokens, i, c, w;
241 bot_custom_weapon = FALSE;
243 if( cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
244 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
245 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
246 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
251 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
256 bot_distance_far = stof(argv(0));
257 bot_distance_close = stof(argv(1));
259 if(bot_distance_far < bot_distance_close){
260 bot_distance_far = stof(argv(1));
261 bot_distance_close = stof(argv(0));
264 // Initialize list of weapons
265 bot_weapons_far[0] = -1;
266 bot_weapons_mid[0] = -1;
267 bot_weapons_close[0] = -1;
269 // Parse far distance weapon priorities
270 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_far"))," ");
273 for(i=0; i < tokens && c < WEP_COUNT; ++i){
275 if ( w >= WEP_FIRST && w <= WEP_LAST) {
276 bot_weapons_far[c] = w;
281 bot_weapons_far[c] = -1;
283 // Parse mid distance weapon priorities
284 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_mid"))," ");
287 for(i=0; i < tokens && c < WEP_COUNT; ++i){
289 if ( w >= WEP_FIRST && w <= WEP_LAST) {
290 bot_weapons_mid[c] = w;
295 bot_weapons_mid[c] = -1;
297 // Parse close distance weapon priorities
298 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_close"))," ");
301 for(i=0; i < tokens && i < WEP_COUNT; ++i){
303 if ( w >= WEP_FIRST && w <= WEP_LAST) {
304 bot_weapons_close[c] = w;
309 bot_weapons_close[c] = -1;
311 bot_custom_weapon = TRUE;
317 //dprint("bot_endgame\n");
321 setcolor(e, e.bot_preferredcolors);
324 // if dynamic waypoints are ever implemented, save them here
327 void bot_relinkplayerlist()
330 local entity prevbot;
333 player_list = e = findchainflags(flags, FL_CLIENT);
338 player_count = player_count + 1;
339 e.nextplayer = e.chain;
340 if (clienttype(e) == CLIENTTYPE_BOT)
347 bot_list.nextbot = world;
350 currentbots = currentbots + 1;
354 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
355 bot_strategytoken = bot_list;
356 bot_strategytoken_taken = TRUE;
359 void bot_clientdisconnect()
361 if (clienttype(self) != CLIENTTYPE_BOT)
364 strunzone(self.cleanname);
365 if(self.netname_freeme)
366 strunzone(self.netname_freeme);
367 if(self.playermodel_freeme)
368 strunzone(self.playermodel_freeme);
369 if(self.playerskin_freeme)
370 strunzone(self.playerskin_freeme);
371 self.cleanname = string_null;
372 self.netname_freeme = string_null;
373 self.playermodel_freeme = string_null;
374 self.playerskin_freeme = string_null;
375 remove(self.bot_cmd_current);
378 void bot_clientconnect()
380 if (clienttype(self) != CLIENTTYPE_BOT)
382 self.bot_preferredcolors = self.clientcolors;
383 self.bot_nextthink = time - random();
384 self.lag_func = bot_lagfunc;
386 self.createdtime = self.nextthink;
388 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
389 bot_setnameandstuff();
391 if(self.bot_forced_team==1)
392 self.team = COLOR_TEAM1;
393 else if(self.bot_forced_team==2)
394 self.team = COLOR_TEAM2;
395 else if(self.bot_forced_team==3)
396 self.team = COLOR_TEAM3;
397 else if(self.bot_forced_team==4)
398 self.team = COLOR_TEAM4;
400 JoinBestTeam(self, FALSE, TRUE);
405 void bot_removefromlargestteam()
407 local float besttime, bestcount, thiscount;
408 local entity best, head;
409 CheckAllowedTeams(world);
410 GetTeamCounts(world);
411 head = findchainfloat(isbot, TRUE);
415 besttime = head.createdtime;
419 if(head.team == COLOR_TEAM1)
421 else if(head.team == COLOR_TEAM2)
423 else if(head.team == COLOR_TEAM3)
425 else if(head.team == COLOR_TEAM4)
429 if (thiscount > bestcount)
431 bestcount = thiscount;
432 besttime = head.createdtime;
435 else if (thiscount == bestcount && besttime < head.createdtime)
437 besttime = head.createdtime;
442 currentbots = currentbots - 1;
446 void bot_removenewest()
448 local float besttime;
449 local entity best, head;
453 bot_removefromlargestteam();
457 head = findchainfloat(isbot, TRUE);
461 besttime = head.createdtime;
464 if (besttime < head.createdtime)
466 besttime = head.createdtime;
471 currentbots = currentbots - 1;
475 void autoskill(float factor)
483 FOR_EACH_PLAYER(head)
485 if(clienttype(head) == CLIENTTYPE_REAL)
486 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
488 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
491 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
492 dprint("best bot got ", ftos(bestbot), "; ");
493 if(bestbot < 0 || bestplayer < 0)
495 dprint("not doing anything\n");
496 // don't return, let it reset all counters below
498 else if(bestbot <= bestplayer * factor - 2)
500 if(cvar("skill") < 17)
502 dprint("2 frags difference, increasing skill\n");
503 cvar_set("skill", ftos(cvar("skill") + 1));
504 bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
507 else if(bestbot >= bestplayer * factor + 2)
509 if(cvar("skill") > 0)
511 dprint("2 frags difference, decreasing skill\n");
512 cvar_set("skill", ftos(cvar("skill") - 1));
513 bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
518 dprint("not doing anything\n");
520 // don't reset counters, wait for them to accumulate
523 FOR_EACH_PLAYER(head)
524 head.totalfrags_lastcheck = head.totalfrags;
527 void bot_serverframe()
529 float realplayers, bots, activerealplayers;
532 if (intermission_running)
538 stepheightvec = cvar("sv_stepheight") * '0 0 1';
539 bot_navigation_movemode = ((cvar("bot_navigation_ignoreplayers")) ? MOVE_NOMONSTERS : MOVE_NORMAL);
541 if(time > autoskill_nextthink)
544 a = cvar("skill_auto");
547 autoskill_nextthink = time + 5;
550 activerealplayers = 0;
553 FOR_EACH_REALCLIENT(head)
555 if(head.classname == "player" || g_lms || g_arena || g_ca)
560 // add/remove bots if needed to make sure there are at least
561 // minplayers+bot_number, or remove all bots if no one is playing
562 // But don't remove bots immediately on level change, as the real players
563 // usually haven't rejoined yet
564 bots_would_leave = FALSE;
565 if ((realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5)))
567 float realminplayers, minplayers;
568 realminplayers = cvar("minplayers");
569 minplayers = max(0, floor(realminplayers));
571 float realminbots, minbots;
572 if(teamplay && cvar("bot_vs_human"))
573 realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
575 realminbots = cvar("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 = cvar("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(cvar("waypoint_benchmark"))
613 if (currentbots > 0 || cvar("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 = cvar("bot_ai_dangerdetectioninterval");
655 if (botframe_nextdangertime < time - interval * 1.5)
656 botframe_nextdangertime = time;
657 botframe_nextdangertime = botframe_nextdangertime + interval;
658 botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
662 if (cvar("g_waypointeditor"))
663 botframe_showwaypointlinks();
665 if(time > bot_cvar_nextthink)
668 bot_custom_weapon_priority_setup();
669 bot_cvar_nextthink = time + 5;