5 #include "navigation.qh"
6 #include "scripting.qh"
7 #include "waypoints.qh"
9 #include "havocbot/havocbot.qh"
10 #include "havocbot/scripting.qh"
12 #include "../antilag.qh"
13 #include "../autocvars.qh"
14 #include "../campaign.qh"
15 #include "../cl_client.qh"
16 #include "../constants.qh"
19 #include "../t_items.qh"
21 #include "../mutators/mutators_include.qh"
23 #include "../weapons/accuracy.qh"
25 #include "../../common/constants.qh"
26 #include "../../common/mapinfo.qh"
27 #include "../../common/teams.qh"
28 #include "../../common/util.qh"
30 #include "../../common/weapons/all.qh"
32 #include "../../csqcmodellib/sv_model.qh"
34 #include "../../warpzonelib/common.qh"
35 #include "../../warpzonelib/util_server.qh"
39 entity bot = spawnclient();
42 currentbots = currentbots + 1;
44 bot_setnameandstuff();
54 if (self.bot_nextthink > time)
57 self.flags &= ~FL_GODMODE;
59 self.flags |= FL_GODMODE;
61 self.bot_nextthink = self.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, self.bot_aiskill);
62 //if (self.bot_painintensity > 0)
63 // self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
65 //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
66 //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
68 if (!IS_PLAYER(self) || (autocvar_g_campaign && !campaign_bots_may_start))
70 self.bot_nextthink = time + 0.5;
76 self.v_angle = self.angles;
78 self.fixangle = false;
83 self.dmg_inflictor = world;
85 // calculate an aiming latency based on the skill setting
86 // (simulated network latency + naturally delayed reflexes)
87 //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
88 // minimum ping 20+10 random
89 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
90 // skill 10 = ping 0.2 (adrenaline)
91 // skill 0 = ping 0.7 (slightly drunk)
97 self.BUTTON_ATCK2 = 0;
99 self.BUTTON_CROUCH = 0;
100 self.BUTTON_HOOK = 0;
101 self.BUTTON_INFO = 0;
103 self.BUTTON_CHAT = 0;
106 if (time < game_starttime)
108 // block the bot during the countdown to game start
109 self.movement = '0 0 0';
110 self.bot_nextthink = game_starttime;
114 // if dead, just wait until we can respawn
117 if (self.deadflag == DEAD_DEAD)
119 self.BUTTON_JUMP = 1; // press jump to respawn
120 self.bot_strategytime = 0;
123 else if(self.aistatus & AI_STATUS_STUCK)
124 navigation_unstuck();
126 // now call the current bot AI (havocbot for example)
130 void bot_setnameandstuff()
133 float file, tokens, prio;
136 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
137 string name, prefix, suffix;
139 if(autocvar_g_campaign)
146 prefix = autocvar_bot_prefix;
147 suffix = autocvar_bot_suffix;
150 file = fopen(autocvar_bot_config_file, FILE_READ);
154 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
159 RandomSelection_Init();
160 while((readfile = fgets(file)))
162 if(substring(readfile, 0, 2) == "//")
164 if(substring(readfile, 0, 1) == "#")
166 tokens = tokenizebyseparator(readfile, "\t");
180 RandomSelection_Add(world, 0, readfile, 1, prio);
182 readfile = RandomSelection_chosen_string;
186 tokens = tokenizebyseparator(readfile, "\t");
187 if(argv(0) != "") bot_name = argv(0);
188 else bot_name = "Bot";
190 if(argv(1) != "") bot_model = argv(1);
193 if(argv(2) != "") bot_skin = argv(2);
196 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
197 else bot_shirt = ftos(floor(random() * 15));
199 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
200 else bot_pants = ftos(floor(random() * 15));
202 self.bot_forced_team = stof(argv(5));
206 #define READSKILL(f,w,r) if(argv(prio) != "") self.f = stof(argv(prio)) * (w); else self.f = (!autocvar_g_campaign) * (2 * random() - 1) * (r) * (w); ++prio
207 //print(bot_name, ": ping=", argv(9), "\n");
209 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
210 READSKILL(bot_moveskill, 2, 0); // move skill
211 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
213 READSKILL(bot_pingskill, 0.5, 0); // ping skill
215 READSKILL(bot_weaponskill, 2, 0); // weapon skill
216 READSKILL(bot_aggresskill, 1, 0); // aggre skill
217 READSKILL(bot_rangepreference, 1, 0); // read skill
219 READSKILL(bot_aimskill, 2, 0); // aim skill
220 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
221 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
223 READSKILL(bot_thinkskill, 1, 0.5); // think skill
224 READSKILL(bot_aiskill, 2, 0); // "ai" skill
226 self.bot_config_loaded = true;
228 // this is really only a default, JoinBestTeam is called later
229 setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
230 self.bot_preferredcolors = self.clientcolors;
233 if (autocvar_bot_usemodelnames)
238 // number bots with identical names
244 if(p.cleanname == name)
248 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
250 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
252 self.cleanname = strzone(name);
254 // pick the model and skin
255 if(substring(bot_model, -4, 1) != ".")
256 bot_model = strcat(bot_model, ".iqm");
257 self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
258 self.playerskin = self.playerskin_freeme = strzone(bot_skin);
260 self.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the world
261 self.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
264 void bot_custom_weapon_priority_setup()
268 bot_custom_weapon = false;
270 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
271 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
272 autocvar_bot_ai_custom_weapon_priority_close == "" ||
273 autocvar_bot_ai_custom_weapon_priority_distances == ""
278 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
283 bot_distance_far = stof(argv(0));
284 bot_distance_close = stof(argv(1));
286 if(bot_distance_far < bot_distance_close){
287 bot_distance_far = stof(argv(1));
288 bot_distance_close = stof(argv(0));
291 // Initialize list of weapons
292 bot_weapons_far[0] = -1;
293 bot_weapons_mid[0] = -1;
294 bot_weapons_close[0] = -1;
296 // Parse far distance weapon priorities
297 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
300 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
302 if ( w >= WEP_FIRST && w <= WEP_LAST) {
303 bot_weapons_far[c] = w;
307 if(c < Weapons_COUNT)
308 bot_weapons_far[c] = -1;
310 // Parse mid distance weapon priorities
311 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
314 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
316 if ( w >= WEP_FIRST && w <= WEP_LAST) {
317 bot_weapons_mid[c] = w;
321 if(c < Weapons_COUNT)
322 bot_weapons_mid[c] = -1;
324 // Parse close distance weapon priorities
325 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
328 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
330 if ( w >= WEP_FIRST && w <= WEP_LAST) {
331 bot_weapons_close[c] = w;
335 if(c < Weapons_COUNT)
336 bot_weapons_close[c] = -1;
338 bot_custom_weapon = true;
344 //dprint("bot_endgame\n");
348 setcolor(e, e.bot_preferredcolors);
351 // if dynamic waypoints are ever implemented, save them here
354 void bot_relinkplayerlist()
360 player_list = e = findchainflags(flags, FL_CLIENT);
365 player_count = player_count + 1;
366 e.nextplayer = e.chain;
367 if (IS_BOT_CLIENT(e))
374 bot_list.nextbot = world;
377 currentbots = currentbots + 1;
381 LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
382 bot_strategytoken = bot_list;
383 bot_strategytoken_taken = true;
386 void bot_clientdisconnect()
388 if (!IS_BOT_CLIENT(self))
390 bot_clearqueue(self);
392 strunzone(self.cleanname);
393 if(self.netname_freeme)
394 strunzone(self.netname_freeme);
395 if(self.playermodel_freeme)
396 strunzone(self.playermodel_freeme);
397 if(self.playerskin_freeme)
398 strunzone(self.playerskin_freeme);
399 self.cleanname = string_null;
400 self.netname_freeme = string_null;
401 self.playermodel_freeme = string_null;
402 self.playerskin_freeme = string_null;
403 if(self.bot_cmd_current)
404 remove(self.bot_cmd_current);
405 if(bot_waypoint_queue_owner==self)
406 bot_waypoint_queue_owner = world;
409 void bot_clientconnect()
411 if (!IS_BOT_CLIENT(self))
413 self.bot_preferredcolors = self.clientcolors;
414 self.bot_nextthink = time - random();
415 self.lag_func = bot_lagfunc;
417 self.createdtime = self.bot_nextthink;
419 if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
420 bot_setnameandstuff();
422 if(self.bot_forced_team==1)
423 self.team = NUM_TEAM_1;
424 else if(self.bot_forced_team==2)
425 self.team = NUM_TEAM_2;
426 else if(self.bot_forced_team==3)
427 self.team = NUM_TEAM_3;
428 else if(self.bot_forced_team==4)
429 self.team = NUM_TEAM_4;
431 JoinBestTeam(self, false, true);
436 void bot_removefromlargestteam()
438 float besttime, bestcount, thiscount;
440 CheckAllowedTeams(world);
441 GetTeamCounts(world);
442 head = findchainfloat(isbot, true);
446 besttime = head.createdtime;
450 if(head.team == NUM_TEAM_1)
452 else if(head.team == NUM_TEAM_2)
454 else if(head.team == NUM_TEAM_3)
456 else if(head.team == NUM_TEAM_4)
460 if (thiscount > bestcount)
462 bestcount = thiscount;
463 besttime = head.createdtime;
466 else if (thiscount == bestcount && besttime < head.createdtime)
468 besttime = head.createdtime;
473 currentbots = currentbots - 1;
477 void bot_removenewest()
484 bot_removefromlargestteam();
488 head = findchainfloat(isbot, true);
492 besttime = head.createdtime;
495 if (besttime < head.createdtime)
497 besttime = head.createdtime;
502 currentbots = currentbots - 1;
506 void autoskill(float factor)
514 FOR_EACH_PLAYER(head)
516 if(IS_REAL_CLIENT(head))
517 bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
519 bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
522 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
523 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
524 if(bestbot < 0 || bestplayer < 0)
526 LOG_TRACE("not doing anything\n");
527 // don't return, let it reset all counters below
529 else if(bestbot <= bestplayer * factor - 2)
531 if(autocvar_skill < 17)
533 LOG_TRACE("2 frags difference, increasing skill\n");
534 cvar_set("skill", ftos(autocvar_skill + 1));
535 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
538 else if(bestbot >= bestplayer * factor + 2)
540 if(autocvar_skill > 0)
542 LOG_TRACE("2 frags difference, decreasing skill\n");
543 cvar_set("skill", ftos(autocvar_skill - 1));
544 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
549 LOG_TRACE("not doing anything\n");
551 // don't reset counters, wait for them to accumulate
554 FOR_EACH_PLAYER(head)
555 head.totalfrags_lastcheck = head.totalfrags;
558 void bot_calculate_stepheightvec(void)
560 stepheightvec = autocvar_sv_stepheight * '0 0 1';
561 jumpstepheightvec = stepheightvec +
562 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
563 // 0.75 factor is for safety to make the jumps easy
569 float realplayers, bots, activerealplayers;
571 activerealplayers = 0;
574 FOR_EACH_REALCLIENT(head)
576 if(IS_PLAYER(head) || g_lms || head.caplayer == 1)
581 // add/remove bots if needed to make sure there are at least
582 // minplayers+bot_number, or remove all bots if no one is playing
583 // But don't remove bots immediately on level change, as the real players
584 // usually haven't rejoined yet
585 bots_would_leave = false;
586 if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
587 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
588 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
590 float realminplayers, minplayers;
591 realminplayers = autocvar_minplayers;
592 minplayers = max(0, floor(realminplayers));
594 float realminbots, minbots;
595 realminbots = autocvar_bot_number;
596 minbots = max(0, floor(realminbots));
598 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
600 bots_would_leave = true;
604 // if there are no players, remove bots
608 // only add one bot per frame to avoid utter chaos
609 if(time > botframe_nextthink)
611 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
612 while (currentbots < bots)
614 if (bot_spawn() == world)
616 bprint("Can not add bot, server full.\n");
620 while (currentbots > bots)
627 void bot_serverframe()
629 if (intermission_running)
635 bot_calculate_stepheightvec();
636 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
638 if(time > autoskill_nextthink)
641 a = autocvar_skill_auto;
644 autoskill_nextthink = time + 5;
647 if(time > botframe_nextthink)
650 botframe_nextthink = time + 10;
653 bot_ignore_bots = autocvar_bot_ignore_bots;
655 if(botframe_spawnedwaypoints)
657 if(autocvar_waypoint_benchmark)
661 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
662 if (botframe_spawnedwaypoints)
664 if(botframe_cachedwaypointlinks)
666 if(!botframe_loadedforcedlinks)
667 waypoint_load_links_hardwired();
671 // TODO: Make this check cleaner
672 entity wp = findchain(classname, "waypoint");
673 if(time - wp.nextthink > 10)
674 waypoint_save_links();
679 botframe_spawnedwaypoints = true;
681 if(!waypoint_load_links())
682 waypoint_schedulerelinkall();
687 // cycle the goal token from one bot to the next each frame
688 // (this prevents them from all doing spawnfunc_waypoint searches on the same
689 // frame, which causes choppy framerates)
690 if (bot_strategytoken_taken)
692 bot_strategytoken_taken = false;
693 if (bot_strategytoken)
694 bot_strategytoken = bot_strategytoken.nextbot;
695 if (!bot_strategytoken)
696 bot_strategytoken = bot_list;
699 if (botframe_nextdangertime < time)
702 interval = autocvar_bot_ai_dangerdetectioninterval;
703 if (botframe_nextdangertime < time - interval * 1.5)
704 botframe_nextdangertime = time;
705 botframe_nextdangertime = botframe_nextdangertime + interval;
706 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
710 if (autocvar_g_waypointeditor)
711 botframe_showwaypointlinks();
713 if (autocvar_g_waypointeditor_auto)
714 botframe_autowaypoints();
716 if(time > bot_cvar_nextthink)
719 bot_custom_weapon_priority_setup();
720 bot_cvar_nextthink = time + 5;