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(strcat(prefix, s, suffix) == p.netname)
145 RandomSelection_Add(world, 0, readfile, 1, prio);
147 readfile = RandomSelection_chosen_string;
151 tokens = tokenizebyseparator(readfile, "\t");
152 if(argv(0) != "") bot_name = argv(0);
153 else bot_name = "Bot";
155 if(argv(1) != "") bot_model = argv(1);
158 if(argv(2) != "") bot_skin = argv(2);
161 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
162 else bot_shirt = ftos(floor(random() * 15));
164 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
165 else bot_pants = ftos(floor(random() * 15));
167 self.bot_forced_team = stof(argv(5));
171 #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
172 //print(bot_name, ": ping=", argv(9), "\n");
174 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
175 READSKILL(bot_moveskill, 2, 0); // move skill
176 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
178 READSKILL(bot_pingskill, 0.5, 0); // ping skill
180 READSKILL(bot_weaponskill, 2, 0); // weapon skill
181 READSKILL(bot_aggresskill, 1, 0); // aggre skill
182 READSKILL(bot_rangepreference, 1, 0); // read skill
184 READSKILL(bot_aimskill, 2, 0); // aim skill
185 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
186 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
188 READSKILL(bot_thinkskill, 1, 0.5); // think skill
189 READSKILL(bot_aiskill, 2, 0); // "ai" skill
191 self.bot_config_loaded = TRUE;
193 // this is really only a default, JoinBestTeam is called later
194 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
195 self.bot_preferredcolors = self.clientcolors;
198 if (cvar("bot_usemodelnames"))
203 self.cleanname = strzone(name);
205 // number bots with identical names
210 if(clienttype(p) == CLIENTTYPE_BOT)
211 if(p.cleanname == name)
215 name = strcat(name, "(", ftos(i), ")");
217 // pick the model and skin
218 if(substring(bot_model, -4, 1) != ".")
219 bot_model = strcat(bot_model, ".iqm");
220 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
221 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
223 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
225 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
226 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
229 void bot_custom_weapon_priority_setup()
231 local float tokens, i, c, w;
233 bot_custom_weapon = FALSE;
235 if( cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
236 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
237 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
238 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
243 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
248 bot_distance_far = stof(argv(0));
249 bot_distance_close = stof(argv(1));
251 if(bot_distance_far < bot_distance_close){
252 bot_distance_far = stof(argv(1));
253 bot_distance_close = stof(argv(0));
256 // Initialize list of weapons
257 bot_weapons_far[0] = -1;
258 bot_weapons_mid[0] = -1;
259 bot_weapons_close[0] = -1;
261 // Parse far distance weapon priorities
262 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_far"))," ");
265 for(i=0; i < tokens && c < WEP_COUNT; ++i){
267 if ( w >= WEP_FIRST && w <= WEP_LAST) {
268 bot_weapons_far[c] = w;
273 bot_weapons_far[c] = -1;
275 // Parse mid distance weapon priorities
276 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_mid"))," ");
279 for(i=0; i < tokens && c < WEP_COUNT; ++i){
281 if ( w >= WEP_FIRST && w <= WEP_LAST) {
282 bot_weapons_mid[c] = w;
287 bot_weapons_mid[c] = -1;
289 // Parse close distance weapon priorities
290 tokens = tokenizebyseparator(W_NumberWeaponOrder(cvar_string("bot_ai_custom_weapon_priority_close"))," ");
293 for(i=0; i < tokens && i < WEP_COUNT; ++i){
295 if ( w >= WEP_FIRST && w <= WEP_LAST) {
296 bot_weapons_close[c] = w;
301 bot_weapons_close[c] = -1;
303 bot_custom_weapon = TRUE;
309 //dprint("bot_endgame\n");
313 setcolor(e, e.bot_preferredcolors);
316 // if dynamic waypoints are ever implemented, save them here
319 void bot_relinkplayerlist()
322 local entity prevbot;
325 player_list = e = findchainflags(flags, FL_CLIENT);
330 player_count = player_count + 1;
331 e.nextplayer = e.chain;
332 if (clienttype(e) == CLIENTTYPE_BOT)
339 bot_list.nextbot = world;
342 currentbots = currentbots + 1;
346 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
347 bot_strategytoken = bot_list;
348 bot_strategytoken_taken = TRUE;
351 void bot_clientdisconnect()
353 if (clienttype(self) != CLIENTTYPE_BOT)
356 strunzone(self.cleanname);
357 if(self.netname_freeme)
358 strunzone(self.netname_freeme);
359 if(self.playermodel_freeme)
360 strunzone(self.playermodel_freeme);
361 if(self.playerskin_freeme)
362 strunzone(self.playerskin_freeme);
363 self.cleanname = string_null;
364 self.netname_freeme = string_null;
365 self.playermodel_freeme = string_null;
366 self.playerskin_freeme = string_null;
367 remove(self.bot_cmd_current);
370 void bot_clientconnect()
372 if (clienttype(self) != CLIENTTYPE_BOT)
374 self.bot_preferredcolors = self.clientcolors;
375 self.bot_nextthink = time - random();
376 self.lag_func = bot_lagfunc;
378 self.createdtime = self.nextthink;
380 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
381 bot_setnameandstuff();
383 if(self.bot_forced_team==1)
384 self.team = COLOR_TEAM1;
385 else if(self.bot_forced_team==2)
386 self.team = COLOR_TEAM2;
387 else if(self.bot_forced_team==3)
388 self.team = COLOR_TEAM3;
389 else if(self.bot_forced_team==4)
390 self.team = COLOR_TEAM4;
392 JoinBestTeam(self, FALSE, TRUE);
397 void bot_removefromlargestteam()
399 local float besttime, bestcount, thiscount;
400 local entity best, head;
401 CheckAllowedTeams(world);
402 GetTeamCounts(world);
403 head = findchainfloat(isbot, TRUE);
407 besttime = head.createdtime;
411 if(head.team == COLOR_TEAM1)
413 else if(head.team == COLOR_TEAM2)
415 else if(head.team == COLOR_TEAM3)
417 else if(head.team == COLOR_TEAM4)
421 if (thiscount > bestcount)
423 bestcount = thiscount;
424 besttime = head.createdtime;
427 else if (thiscount == bestcount && besttime < head.createdtime)
429 besttime = head.createdtime;
434 currentbots = currentbots - 1;
438 void bot_removenewest()
440 local float besttime;
441 local entity best, head;
445 bot_removefromlargestteam();
449 head = findchainfloat(isbot, TRUE);
453 besttime = head.createdtime;
456 if (besttime < head.createdtime)
458 besttime = head.createdtime;
463 currentbots = currentbots - 1;
467 void autoskill(float factor)
475 FOR_EACH_PLAYER(head)
477 if(clienttype(head) == CLIENTTYPE_REAL)
478 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
480 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
483 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
484 dprint("best bot got ", ftos(bestbot), "; ");
485 if(bestbot < 0 || bestplayer < 0)
487 dprint("not doing anything\n");
488 // don't return, let it reset all counters below
490 else if(bestbot <= bestplayer * factor - 2)
492 if(cvar("skill") < 17)
494 dprint("2 frags difference, increasing skill\n");
495 cvar_set("skill", ftos(cvar("skill") + 1));
496 bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
499 else if(bestbot >= bestplayer * factor + 2)
501 if(cvar("skill") > 0)
503 dprint("2 frags difference, decreasing skill\n");
504 cvar_set("skill", ftos(cvar("skill") - 1));
505 bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
510 dprint("not doing anything\n");
512 // don't reset counters, wait for them to accumulate
515 FOR_EACH_PLAYER(head)
516 head.totalfrags_lastcheck = head.totalfrags;
519 void bot_serverframe()
521 float realplayers, bots, activerealplayers;
524 if (intermission_running)
530 stepheightvec = cvar("sv_stepheight") * '0 0 1';
531 bot_navigation_movemode = ((cvar("bot_navigation_ignoreplayers")) ? MOVE_NOMONSTERS : MOVE_NORMAL);
533 if(time > autoskill_nextthink)
536 a = cvar("skill_auto");
539 autoskill_nextthink = time + 5;
542 activerealplayers = 0;
545 FOR_EACH_REALCLIENT(head)
547 if(head.classname == "player" || g_lms || g_arena || g_ca)
552 // add/remove bots if needed to make sure there are at least
553 // minplayers+bot_number, or remove all bots if no one is playing
554 // But don't remove bots immediately on level change, as the real players
555 // usually haven't rejoined yet
556 bots_would_leave = FALSE;
557 if ((realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5)))
559 float realminplayers, minplayers;
560 realminplayers = cvar("minplayers");
561 minplayers = max(0, floor(realminplayers));
563 float realminbots, minbots;
564 if(teamplay && cvar("bot_vs_human"))
565 realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
567 realminbots = cvar("bot_number");
568 minbots = max(0, floor(realminbots));
570 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
572 bots_would_leave = TRUE;
576 // if there are no players, remove bots
580 bot_ignore_bots = cvar("bot_ignore_bots");
582 // only add one bot per frame to avoid utter chaos
583 if(time > botframe_nextthink)
585 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
586 while (currentbots < bots)
588 if (bot_spawn() == world)
590 bprint("Can not add bot, server full.\n");
591 botframe_nextthink = time + 10;
595 while (currentbots > bots)
599 if(botframe_spawnedwaypoints)
601 if(cvar("waypoint_benchmark"))
605 if (currentbots > 0 || cvar("g_waypointeditor"))
606 if (botframe_spawnedwaypoints)
608 if(botframe_cachedwaypointlinks)
610 if(!botframe_loadedforcedlinks)
611 waypoint_load_links_hardwired();
615 // TODO: Make this check cleaner
616 local entity wp = findchain(classname, "waypoint");
617 if(time - wp.nextthink > 10)
618 waypoint_save_links();
623 botframe_spawnedwaypoints = TRUE;
625 if(!waypoint_load_links())
626 waypoint_schedulerelinkall();
631 // cycle the goal token from one bot to the next each frame
632 // (this prevents them from all doing spawnfunc_waypoint searches on the same
633 // frame, which causes choppy framerates)
634 if (bot_strategytoken_taken)
636 bot_strategytoken_taken = FALSE;
637 if (bot_strategytoken)
638 bot_strategytoken = bot_strategytoken.nextbot;
639 if (!bot_strategytoken)
640 bot_strategytoken = bot_list;
643 if (botframe_nextdangertime < time)
645 local float interval;
646 interval = cvar("bot_ai_dangerdetectioninterval");
647 if (botframe_nextdangertime < time - interval * 1.5)
648 botframe_nextdangertime = time;
649 botframe_nextdangertime = botframe_nextdangertime + interval;
650 botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
654 if (cvar("g_waypointeditor"))
655 botframe_showwaypointlinks();
657 if(time > bot_cvar_nextthink)
660 bot_custom_weapon_priority_setup();
661 bot_cvar_nextthink = time + 5;