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");
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 * 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));
168 self.bot_config_loaded = TRUE;
170 // this is really only a default, JoinBestTeam is called later
171 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
172 self.bot_preferredcolors = self.clientcolors;
175 if (cvar("bot_usemodelnames"))
180 // pick the model and skin
181 if(substring(bot_model, -4, 1) != ".")
182 bot_model = strcat(bot_model, ".zym");
183 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
184 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
186 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
189 void bot_custom_weapon_priority_setup()
191 local float tokens, i, c, w;
193 bot_custom_weapon = FALSE;
195 if( cvar_string("bot_ai_custom_weapon_priority_far") == "" ||
196 cvar_string("bot_ai_custom_weapon_priority_mid") == "" ||
197 cvar_string("bot_ai_custom_weapon_priority_close") == "" ||
198 cvar_string("bot_ai_custom_weapon_priority_distances") == ""
203 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_distances")," ");
208 bot_distance_far = stof(argv(0));
209 bot_distance_close = stof(argv(1));
211 if(bot_distance_far < bot_distance_close){
212 bot_distance_far = stof(argv(1));
213 bot_distance_close = stof(argv(0));
216 // Initialize list of weapons
217 bot_weapons_far[0] = -1;
218 bot_weapons_mid[0] = -1;
219 bot_weapons_close[0] = -1;
221 // Parse far distance weapon priorities
222 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_far")," ");
225 for(i=0; i < tokens && c < WEP_COUNT; ++i){
227 if ( w >= WEP_FIRST && w <= WEP_LAST) {
228 bot_weapons_far[c] = w;
233 bot_weapons_far[c] = -1;
235 // Parse mid distance weapon priorities
236 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_mid")," ");
239 for(i=0; i < tokens && c < WEP_COUNT; ++i){
241 if ( w >= WEP_FIRST && w <= WEP_LAST) {
242 bot_weapons_mid[c] = w;
247 bot_weapons_mid[c] = -1;
249 // Parse close distance weapon priorities
250 tokens = tokenizebyseparator(cvar_string("bot_ai_custom_weapon_priority_close")," ");
253 for(i=0; i < tokens && i < WEP_COUNT; ++i){
255 if ( w >= WEP_FIRST && w <= WEP_LAST) {
256 bot_weapons_close[c] = w;
261 bot_weapons_close[c] = -1;
263 bot_custom_weapon = TRUE;
269 //dprint("bot_endgame\n");
273 setcolor(e, e.bot_preferredcolors);
276 // if dynamic waypoints are ever implemented, save them here
279 void bot_relinkplayerlist()
282 local entity prevbot;
285 player_list = e = findchainflags(flags, FL_CLIENT);
290 player_count = player_count + 1;
291 e.nextplayer = e.chain;
292 if (clienttype(e) == CLIENTTYPE_BOT)
299 bot_list.nextbot = world;
302 currentbots = currentbots + 1;
306 dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
307 bot_strategytoken = bot_list;
308 bot_strategytoken_taken = TRUE;
311 void bot_clientdisconnect()
313 if (clienttype(self) != CLIENTTYPE_BOT)
315 if(self.netname_freeme)
316 strunzone(self.netname_freeme);
317 if(self.playermodel_freeme)
318 strunzone(self.playermodel_freeme);
319 if(self.playerskin_freeme)
320 strunzone(self.playerskin_freeme);
321 self.netname_freeme = string_null;
322 self.playermodel_freeme = string_null;
323 self.playerskin_freeme = string_null;
326 void bot_clientconnect()
328 if (clienttype(self) != CLIENTTYPE_BOT)
330 self.bot_preferredcolors = self.clientcolors;
331 self.bot_nextthink = time - random();
332 self.lag_func = bot_lagfunc;
334 self.createdtime = self.nextthink;
336 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
337 bot_setnameandstuff();
339 if(self.bot_forced_team==1)
340 self.team = COLOR_TEAM1;
341 else if(self.bot_forced_team==2)
342 self.team = COLOR_TEAM2;
343 else if(self.bot_forced_team==3)
344 self.team = COLOR_TEAM3;
345 else if(self.bot_forced_team==4)
346 self.team = COLOR_TEAM4;
348 JoinBestTeam(self, FALSE, TRUE);
351 self.bot_mouseskill=random()-0.5;
352 self.bot_thinkskill=random()-0.5;
353 self.bot_predictionskill=random()-0.5;
354 self.bot_offsetskill=random()-0.5;
357 void bot_removefromlargestteam()
359 local float besttime, bestcount, thiscount;
360 local entity best, head;
361 CheckAllowedTeams(world);
362 GetTeamCounts(world);
363 head = findchainfloat(isbot, TRUE);
367 besttime = head.createdtime;
371 if(head.team == COLOR_TEAM1)
373 else if(head.team == COLOR_TEAM2)
375 else if(head.team == COLOR_TEAM3)
377 else if(head.team == COLOR_TEAM4)
381 if (thiscount > bestcount)
383 bestcount = thiscount;
384 besttime = head.createdtime;
387 else if (thiscount == bestcount && besttime < head.createdtime)
389 besttime = head.createdtime;
394 currentbots = currentbots - 1;
398 void bot_removenewest()
400 local float besttime;
401 local entity best, head;
405 bot_removefromlargestteam();
409 head = findchainfloat(isbot, TRUE);
413 besttime = head.createdtime;
416 if (besttime < head.createdtime)
418 besttime = head.createdtime;
423 currentbots = currentbots - 1;
427 void autoskill(float factor)
435 FOR_EACH_PLAYER(head)
437 if(clienttype(head) == CLIENTTYPE_REAL)
438 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
440 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
443 dprint("autoskill: best player got ", ftos(bestplayer), ", ");
444 dprint("best bot got ", ftos(bestbot), "; ");
445 if(bestbot < 0 || bestplayer < 0)
447 dprint("not doing anything\n");
448 // don't return, let it reset all counters below
450 else if(bestbot <= bestplayer * factor - 2)
452 if(cvar("skill") < 17)
454 dprint("2 frags difference, increasing skill\n");
455 cvar_set("skill", ftos(cvar("skill") + 1));
456 bprint("^2SKILL UP!^7 Now at level ", ftos(cvar("skill")), "\n");
459 else if(bestbot >= bestplayer * factor + 2)
461 if(cvar("skill") > 0)
463 dprint("2 frags difference, decreasing skill\n");
464 cvar_set("skill", ftos(cvar("skill") - 1));
465 bprint("^1SKILL DOWN!^7 Now at level ", ftos(cvar("skill")), "\n");
470 dprint("not doing anything\n");
472 // don't reset counters, wait for them to accumulate
475 FOR_EACH_PLAYER(head)
476 head.totalfrags_lastcheck = head.totalfrags;
479 void bot_serverframe()
481 float realplayers, bots, activerealplayers;
484 if (intermission_running)
490 stepheightvec = cvar("sv_stepheight") * '0 0 1';
491 bot_navigation_movemode = ((cvar("bot_navigation_ignoreplayers")) ? MOVE_NOMONSTERS : MOVE_NORMAL);
493 if(time > autoskill_nextthink)
496 a = cvar("skill_auto");
499 autoskill_nextthink = time + 5;
502 activerealplayers = 0;
505 FOR_EACH_REALCLIENT(head)
507 if(head.classname == "player" || g_lms || g_arena)
512 // add/remove bots if needed to make sure there are at least
513 // minplayers+bot_number, or remove all bots if no one is playing
514 // But don't remove bots immediately on level change, as the real players
515 // usually haven't rejoined yet
516 bots_would_leave = FALSE;
517 if ((realplayers || cvar("bot_join_empty") || (currentbots > 0 && time < 5)))
519 float realminplayers, minplayers;
520 realminplayers = cvar("minplayers");
521 minplayers = max(0, floor(realminplayers));
523 float realminbots, minbots;
524 if(teamplay && cvar("bot_vs_human"))
525 realminbots = ceil(fabs(cvar("bot_vs_human")) * activerealplayers);
527 realminbots = cvar("bot_number");
528 minbots = max(0, floor(realminbots));
530 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
532 bots_would_leave = TRUE;
536 // if there are no players, remove bots
540 bot_ignore_bots = cvar("bot_ignore_bots");
542 // only add one bot per frame to avoid utter chaos
543 if(time > botframe_nextthink)
545 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
546 while (currentbots < bots)
548 if (bot_spawn() == world)
550 bprint("Can not add bot, server full.\n");
551 botframe_nextthink = time + 10;
555 while (currentbots > bots)
559 if(botframe_spawnedwaypoints)
561 if(cvar("waypoint_benchmark"))
565 if (currentbots > 0 || cvar("g_waypointeditor"))
566 if (botframe_spawnedwaypoints)
568 if(botframe_cachedwaypointlinks)
570 if(!botframe_loadedforcedlinks)
571 waypoint_load_links_hardwired();
575 // TODO: Make this check cleaner
576 local entity wp = findchain(classname, "waypoint");
577 if(time - wp.nextthink > 10)
578 waypoint_save_links();
583 botframe_spawnedwaypoints = TRUE;
585 if(!waypoint_load_links())
586 waypoint_schedulerelinkall();
591 // cycle the goal token from one bot to the next each frame
592 // (this prevents them from all doing spawnfunc_waypoint searches on the same
593 // frame, which causes choppy framerates)
594 if (bot_strategytoken_taken)
596 bot_strategytoken_taken = FALSE;
597 if (bot_strategytoken)
598 bot_strategytoken = bot_strategytoken.nextbot;
599 if (!bot_strategytoken)
600 bot_strategytoken = bot_list;
603 if (botframe_nextdangertime < time)
605 local float interval;
606 interval = cvar("bot_ai_dangerdetectioninterval");
607 if (botframe_nextdangertime < time - interval * 1.5)
608 botframe_nextdangertime = time;
609 botframe_nextdangertime = botframe_nextdangertime + interval;
610 botframe_updatedangerousobjects(cvar("bot_ai_dangerdetectionupdates"));
614 if (cvar("g_waypointeditor"))
615 botframe_showwaypointlinks();
617 if(time > bot_cvar_nextthink)
620 bot_custom_weapon_priority_setup();
621 bot_cvar_nextthink = time + 5;