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);
156 else bot_model = "marine";
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));
170 #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
171 //print(bot_name, ": ping=", argv(9), "\n");
173 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboards kill
174 READSKILL(bot_moveskill, 2, 0); // moves kill
175 READSKILL(bot_dodgeskill, 2, 0); // dodges kill
177 READSKILL(bot_pingskill, 0.5, 0); // pings kill
179 READSKILL(bot_weaponskill, 2, 0); // weapons kill
180 READSKILL(bot_aggresskill, 1, 0); // aggres kill
181 READSKILL(bot_rangepreference, 1, 0); // reads kill
183 READSKILL(bot_aimskill, 2, 0); // aims kill
184 READSKILL(bot_offsetskill, 2, 0.5); // offsets kill
185 READSKILL(bot_mouseskill, 1, 0.5); // mouses kill
187 READSKILL(bot_thinkskill, 1, 0.5); // thinks kill
188 READSKILL(bot_aiskill, 2, 0); // "ai"s kill
190 self.bot_config_loaded = TRUE;
192 // this is really only a default, JoinBestTeam is called later
193 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
194 self.bot_preferredcolors = self.clientcolors;
197 if (cvar("bot_usemodelnames"))
202 // pick the model and skin
203 if(substring(bot_model, -4, 1) != ".")
204 bot_model = strcat(bot_model, ".zym");
205 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
206 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
208 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
211 void bot_custom_weapon_priority_setup()
213 local float tokens, i, c, w;
215 bot_custom_weapon = FALSE;
217 if( cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
218 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
219 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
220 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
225 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
230 bot_distance_far = stof(argv(0));
231 bot_distance_close = stof(argv(1));
233 if(bot_distance_far < bot_distance_close){
234 bot_distance_far = stof(argv(1));
235 bot_distance_close = stof(argv(0));
238 // Initialize list of weapons
239 bot_weapons_far[0] = -1;
240 bot_weapons_mid[0] = -1;
241 bot_weapons_close[0] = -1;
243 // Parse far distance weapon priorities
244 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_far")," ");
247 for(i=0; i < tokens && c < WEP_COUNT; ++i){
249 if ( w >= WEP_FIRST && w <= WEP_LAST) {
250 bot_weapons_far[c] = w;
255 bot_weapons_far[c] = -1;
257 // Parse mid distance weapon priorities
258 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_mid")," ");
261 for(i=0; i < tokens && c < WEP_COUNT; ++i){
263 if ( w >= WEP_FIRST && w <= WEP_LAST) {
264 bot_weapons_mid[c] = w;
269 bot_weapons_mid[c] = -1;
271 // Parse close distance weapon priorities
272 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_close")," ");
275 for(i=0; i < tokens && i < WEP_COUNT; ++i){
277 if ( w >= WEP_FIRST && w <= WEP_LAST) {
278 bot_weapons_close[c] = w;
283 bot_weapons_close[c] = -1;
285 bot_custom_weapon = TRUE;
291 //dprint("bot_endgame\n");
295 setcolor(e, e.bot_preferredcolors);
298 // if dynamic waypoints are ever implemented, save them here
301 void bot_relinkplayerlist()
304 local entity prevbot;
307 player_list = e = findchainflags(flags, FL_CLIENT);
312 player_count = player_count + 1;
313 e.nextplayer = e.chain;
314 if (clienttype(e) == CLIENTTYPE_BOT)
321 bot_list.nextbot = world;
324 currentbots = currentbots + 1;
328 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
329 bot_strategytoken = bot_list;
330 bot_strategytoken_taken = TRUE;
333 void bot_clientdisconnect()
335 if (clienttype(self) != CLIENTTYPE_BOT)
337 if(self.netname_freeme)
338 strunzone(self.netname_freeme);
339 if(self.playermodel_freeme)
340 strunzone(self.playermodel_freeme);
341 if(self.playerskin_freeme)
342 strunzone(self.playerskin_freeme);
343 self.netname_freeme = string_null;
344 self.playermodel_freeme = string_null;
345 self.playerskin_freeme = string_null;
346 remove(self.bot_cmd_current);
349 void bot_clientconnect()
351 if (clienttype(self) != CLIENTTYPE_BOT)
353 self.bot_preferredcolors = self.clientcolors;
354 self.bot_nextthink = time - random();
355 self.lag_func = bot_lagfunc;
357 self.createdtime = self.nextthink;
359 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
360 bot_setnameandstuff();
362 if(self.bot_forced_team==1)
363 self.team = COLOR_TEAM1;
364 else if(self.bot_forced_team==2)
365 self.team = COLOR_TEAM2;
366 else if(self.bot_forced_team==3)
367 self.team = COLOR_TEAM3;
368 else if(self.bot_forced_team==4)
369 self.team = COLOR_TEAM4;
371 JoinBestTeam(self, FALSE, TRUE);
376 void bot_removefromlargestteam()
378 local float besttime, bestcount, thiscount;
379 local entity best, head;
380 CheckAllowedTeams(world);
381 GetTeamCounts(world);
382 head = findchainfloat(isbot, TRUE);
386 besttime = head.createdtime;
390 if(head.team == COLOR_TEAM1)
392 else if(head.team == COLOR_TEAM2)
394 else if(head.team == COLOR_TEAM3)
396 else if(head.team == COLOR_TEAM4)
400 if (thiscount > bestcount)
402 bestcount = thiscount;
403 besttime = head.createdtime;
406 else if (thiscount == bestcount && besttime < head.createdtime)
408 besttime = head.createdtime;
413 currentbots = currentbots - 1;
417 void bot_removenewest()
419 local float besttime;
420 local entity best, head;
424 bot_removefromlargestteam();
428 head = findchainfloat(isbot, TRUE);
432 besttime = head.createdtime;
435 if (besttime < head.createdtime)
437 besttime = head.createdtime;
442 currentbots = currentbots - 1;
446 void autoskill(float factor)
454 FOR_EACH_PLAYER(head)
456 if(clienttype(head) == CLIENTTYPE_REAL)
457 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
459 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
462 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
463 dprint("best bot got ", ftos(bestbot), "; ");
464 if(bestbot < 0 || bestplayer < 0)
466 dprint("not doing anything\n");
467 // don't return, let it reset all counters below
469 else if(bestbot <= bestplayer * factor - 2)
471 if(cvar("skill") < 17)
473 dprint("2 frags difference, increasing skill\n");
474 cvar_set("skill", ftos(cvar("skill") + 1));
475 bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
478 else if(bestbot >= bestplayer * factor + 2)
480 if(cvar("skill") > 0)
482 dprint("2 frags difference, decreasing skill\n");
483 cvar_set("skill", ftos(cvar("skill") - 1));
484 bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
489 dprint("not doing anything\n");
491 // don't reset counters, wait for them to accumulate
494 FOR_EACH_PLAYER(head)
495 head.totalfrags_lastcheck = head.totalfrags;
498 void bot_serverframe()
500 float realplayers, bots, activerealplayers;
503 if (intermission_running)
509 stepheightvec = cvar("sv_stepheight") * '0 0 1';
510 bot_navigation_movemode = ((cvar("bot_navigation_ignoreplayers")) ? MOVE_NOMONSTERS : MOVE_NORMAL);
512 if(time > autoskill_nextthink)
515 a = cvar("skill_auto");
518 autoskill_nextthink = time + 5;
521 activerealplayers = 0;
524 FOR_EACH_REALCLIENT(head)
526 if(head.classname == "player" || g_lms || g_arena)
531 // add/remove bots if needed to make sure there are at least
532 // minplayers+bot_number, or remove all bots if no one is playing
533 // But don't remove bots immediately on level change, as the real players
534 // usually haven't rejoined yet
535 bots_would_leave = FALSE;
536 if ((realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5)))
538 float realminplayers, minplayers;
539 realminplayers = cvar("minplayers");
540 minplayers = max(0, floor(realminplayers));
542 float realminbots, minbots;
543 if(teamplay && cvar("bot_vs_human"))
544 realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
546 realminbots = cvar("bot_number");
547 minbots = max(0, floor(realminbots));
549 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
551 bots_would_leave = TRUE;
555 // if there are no players, remove bots
559 bot_ignore_bots = cvar("bot_ignore_bots");
561 // only add one bot per frame to avoid utter chaos
562 if(time > botframe_nextthink)
564 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
565 while (currentbots < bots)
567 if (bot_spawn() == world)
569 bprint("Can not add bot, server full.\n");
570 botframe_nextthink = time + 10;
574 while (currentbots > bots)
578 if(botframe_spawnedwaypoints)
580 if(cvar("waypoint_benchmark"))
584 if (currentbots > 0 || cvar("g_waypointeditor"))
585 if (botframe_spawnedwaypoints)
587 if(botframe_cachedwaypointlinks)
589 if(!botframe_loadedforcedlinks)
590 waypoint_load_links_hardwired();
594 // TODO: Make this check cleaner
595 local entity wp = findchain(classname, "waypoint");
596 if(time - wp.nextthink > 10)
597 waypoint_save_links();
602 botframe_spawnedwaypoints = TRUE;
604 if(!waypoint_load_links())
605 waypoint_schedulerelinkall();
610 // cycle the goal token from one bot to the next each frame
611 // (this prevents them from all doing spawnfunc_waypoint searches on the same
612 // frame, which causes choppy framerates)
613 if (bot_strategytoken_taken)
615 bot_strategytoken_taken = FALSE;
616 if (bot_strategytoken)
617 bot_strategytoken = bot_strategytoken.nextbot;
618 if (!bot_strategytoken)
619 bot_strategytoken = bot_list;
622 if (botframe_nextdangertime < time)
624 local float interval;
625 interval = cvar("bot_ai_dangerdetectioninterval");
626 if (botframe_nextdangertime < time - interval * 1.5)
627 botframe_nextdangertime = time;
628 botframe_nextdangertime = botframe_nextdangertime + interval;
629 botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
633 if (cvar("g_waypointeditor"))
634 botframe_showwaypointlinks();
636 if(time > bot_cvar_nextthink)
639 bot_custom_weapon_priority_setup();
640 bot_cvar_nextthink = time + 5;