6 #include "navigation.qh"
7 #include "scripting.qh"
8 #include "waypoints.qh"
10 #include "havocbot/havocbot.qh"
11 #include "havocbot/scripting.qh"
13 #include "../../teamplay.qh"
15 #include "../../antilag.qh"
16 #include "../../autocvars.qh"
17 #include "../../campaign.qh"
18 #include "../../client.qh"
19 #include "../../constants.qh"
20 #include "../../defs.qh"
21 #include "../../race.qh"
22 #include <common/t_items.qh>
24 #include "../../mutators/_mod.qh"
26 #include "../../weapons/accuracy.qh"
28 #include <common/physics/player.qh>
29 #include <common/constants.qh>
30 #include <common/mapinfo.qh>
31 #include <common/teams.qh>
32 #include <common/util.qh>
34 #include <server/scores_rules.qh>
36 #include <common/weapons/_all.qh>
38 #include <lib/csqcmodel/sv_model.qh>
40 #include <lib/warpzone/common.qh>
41 #include <lib/warpzone/util_server.qh>
45 entity bot = spawnclient();
48 currentbots = currentbots + 1;
49 bot_setnameandstuff(bot);
51 PutClientInServer(bot);
56 void bot_think(entity this)
58 if (this.bot_nextthink > time)
61 this.flags &= ~FL_GODMODE;
63 this.flags |= FL_GODMODE;
65 this.bot_nextthink = this.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, this.bot_aiskill);
66 //if (this.bot_painintensity > 0)
67 // this.bot_painintensity = this.bot_painintensity - (skill + 1) * 40 * frametime;
69 //this.bot_painintensity = this.bot_painintensity + this.bot_oldhealth - this.health;
70 //this.bot_painintensity = bound(0, this.bot_painintensity, 100);
72 if (!IS_PLAYER(this) || (autocvar_g_campaign && !campaign_bots_may_start))
74 this.bot_nextthink = time + 0.5;
80 this.v_angle = this.angles;
82 this.fixangle = false;
87 this.dmg_inflictor = NULL;
89 // calculate an aiming latency based on the skill setting
90 // (simulated network latency + naturally delayed reflexes)
91 //this.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
92 // minimum ping 20+10 random
93 this.ping = bound(0,0.07 - bound(0, (skill + this.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
94 // skill 10 = ping 0.2 (adrenaline)
95 // skill 0 = ping 0.7 (slightly drunk)
98 PHYS_INPUT_BUTTON_ATCK(this) = false;
99 PHYS_INPUT_BUTTON_JUMP(this) = false;
100 PHYS_INPUT_BUTTON_ATCK2(this) = false;
101 PHYS_INPUT_BUTTON_ZOOM(this) = false;
102 PHYS_INPUT_BUTTON_CROUCH(this) = false;
103 PHYS_INPUT_BUTTON_HOOK(this) = false;
104 PHYS_INPUT_BUTTON_INFO(this) = false;
105 PHYS_INPUT_BUTTON_DRAG(this) = false;
106 PHYS_INPUT_BUTTON_CHAT(this) = false;
107 PHYS_INPUT_BUTTON_USE(this) = false;
109 if (time < game_starttime)
111 // block the bot during the countdown to game start
112 this.movement = '0 0 0';
113 this.bot_nextthink = game_starttime;
117 // if dead, just wait until we can respawn
120 if (this.deadflag == DEAD_DEAD)
122 PHYS_INPUT_BUTTON_JUMP(this) = true; // press jump to respawn
123 this.bot_strategytime = 0;
126 else if(this.aistatus & AI_STATUS_STUCK)
127 navigation_unstuck(this);
129 // now call the current bot AI (havocbot for example)
133 void bot_setnameandstuff(entity this)
136 float file, tokens, prio;
138 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
139 string name, prefix, suffix;
141 if(autocvar_g_campaign)
148 prefix = autocvar_bot_prefix;
149 suffix = autocvar_bot_suffix;
152 file = fopen(autocvar_bot_config_file, FILE_READ);
156 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
161 RandomSelection_Init();
162 while((readfile = fgets(file)))
164 if(substring(readfile, 0, 2) == "//")
166 if(substring(readfile, 0, 1) == "#")
168 tokens = tokenizebyseparator(readfile, "\t");
173 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
174 if(s == it.cleanname)
180 RandomSelection_Add(NULL, 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 this.bot_forced_team = stof(argv(5));
206 #define READSKILL(f,w,r) if(argv(prio) != "") this.f = stof(argv(prio)) * (w); else this.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 this.bot_config_loaded = true;
228 // this is really only a default, JoinBestTeam is called later
229 setcolor(this, stof(bot_shirt) * 16 + stof(bot_pants));
230 this.bot_preferredcolors = this.clientcolors;
233 if (autocvar_bot_usemodelnames)
238 // number bots with identical names
240 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
241 if(it.cleanname == name)
245 this.netname = this.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
247 this.netname = this.netname_freeme = strzone(strcat(prefix, name, suffix));
249 this.cleanname = strzone(name);
251 // pick the model and skin
252 if(substring(bot_model, -4, 1) != ".")
253 bot_model = strcat(bot_model, ".iqm");
254 this.playermodel = this.playermodel_freeme = strzone(strcat("models/player/", bot_model));
255 this.playerskin = this.playerskin_freeme = strzone(bot_skin);
257 this.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the NULL
258 this.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
261 void bot_custom_weapon_priority_setup()
265 bot_custom_weapon = false;
267 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
268 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
269 autocvar_bot_ai_custom_weapon_priority_close == "" ||
270 autocvar_bot_ai_custom_weapon_priority_distances == ""
275 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
280 bot_distance_far = stof(argv(0));
281 bot_distance_close = stof(argv(1));
283 if(bot_distance_far < bot_distance_close){
284 bot_distance_far = stof(argv(1));
285 bot_distance_close = stof(argv(0));
288 // Initialize list of weapons
289 bot_weapons_far[0] = -1;
290 bot_weapons_mid[0] = -1;
291 bot_weapons_close[0] = -1;
293 // Parse far distance weapon priorities
294 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
297 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
299 if ( w >= WEP_FIRST && w <= WEP_LAST) {
300 bot_weapons_far[c] = w;
304 if(c < Weapons_COUNT)
305 bot_weapons_far[c] = -1;
307 // Parse mid distance weapon priorities
308 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
311 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
313 if ( w >= WEP_FIRST && w <= WEP_LAST) {
314 bot_weapons_mid[c] = w;
318 if(c < Weapons_COUNT)
319 bot_weapons_mid[c] = -1;
321 // Parse close distance weapon priorities
322 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
325 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
327 if ( w >= WEP_FIRST && w <= WEP_LAST) {
328 bot_weapons_close[c] = w;
332 if(c < Weapons_COUNT)
333 bot_weapons_close[c] = -1;
335 bot_custom_weapon = true;
341 //dprint("bot_endgame\n");
345 setcolor(e, e.bot_preferredcolors);
348 // if dynamic waypoints are ever implemented, save them here
351 void bot_relinkplayerlist()
357 entity prevbot = NULL;
362 if(IS_BOT_CLIENT(it))
365 prevbot.nextbot = it;
369 bot_list.nextbot = NULL;
375 LOG_TRACE("relink: ", ftos(currentbots), " bots seen.");
376 bot_strategytoken = bot_list;
377 bot_strategytoken_taken = true;
380 void bot_clientdisconnect(entity this)
382 if (!IS_BOT_CLIENT(this))
384 bot_clearqueue(this);
386 strunzone(this.cleanname);
387 if(this.netname_freeme)
388 strunzone(this.netname_freeme);
389 if(this.playermodel_freeme)
390 strunzone(this.playermodel_freeme);
391 if(this.playerskin_freeme)
392 strunzone(this.playerskin_freeme);
393 this.cleanname = string_null;
394 this.netname_freeme = string_null;
395 this.playermodel_freeme = string_null;
396 this.playerskin_freeme = string_null;
397 if(this.bot_cmd_current)
398 delete(this.bot_cmd_current);
399 if(bot_waypoint_queue_owner==this)
400 bot_waypoint_queue_owner = NULL;
403 void bot_clientconnect(entity this)
405 if (!IS_BOT_CLIENT(this)) return;
406 this.bot_preferredcolors = this.clientcolors;
407 this.bot_nextthink = time - random();
408 this.lag_func = bot_lagfunc;
410 this.createdtime = this.bot_nextthink;
412 if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
413 bot_setnameandstuff(this);
415 if(this.bot_forced_team==1)
416 this.team = NUM_TEAM_1;
417 else if(this.bot_forced_team==2)
418 this.team = NUM_TEAM_2;
419 else if(this.bot_forced_team==3)
420 this.team = NUM_TEAM_3;
421 else if(this.bot_forced_team==4)
422 this.team = NUM_TEAM_4;
424 JoinBestTeam(this, false, true);
426 havocbot_setupbot(this);
429 void bot_removefromlargestteam()
431 CheckAllowedTeams(NULL);
439 FOREACH_ENTITY_FLOAT(isbot, true,
446 besttime = it.createdtime;
453 case NUM_TEAM_1: thiscount = c1; break;
454 case NUM_TEAM_2: thiscount = c2; break;
455 case NUM_TEAM_3: thiscount = c3; break;
456 case NUM_TEAM_4: thiscount = c4; break;
459 if(thiscount > bestcount)
461 bestcount = thiscount;
462 besttime = it.createdtime;
465 else if(thiscount == bestcount && besttime < it.createdtime)
467 besttime = it.createdtime;
472 return; // no bots to remove
473 currentbots = currentbots - 1;
477 void bot_removenewest()
481 bot_removefromlargestteam();
489 FOREACH_ENTITY_FLOAT(isbot, true,
496 besttime = it.createdtime;
499 if(besttime < it.createdtime)
501 besttime = it.createdtime;
507 return; // no bots to remove
509 currentbots = currentbots - 1;
513 void autoskill(float factor)
520 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
521 if(IS_REAL_CLIENT(it))
522 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
524 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
527 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
528 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
529 if(bestbot < 0 || bestplayer < 0)
531 LOG_TRACE("not doing anything");
532 // don't return, let it reset all counters below
534 else if(bestbot <= bestplayer * factor - 2)
536 if(autocvar_skill < 17)
538 LOG_TRACE("2 frags difference, increasing skill");
539 cvar_set("skill", ftos(autocvar_skill + 1));
540 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
543 else if(bestbot >= bestplayer * factor + 2)
545 if(autocvar_skill > 0)
547 LOG_TRACE("2 frags difference, decreasing skill");
548 cvar_set("skill", ftos(autocvar_skill - 1));
549 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
554 LOG_TRACE("not doing anything");
556 // don't reset counters, wait for them to accumulate
559 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
562 void bot_calculate_stepheightvec()
564 stepheightvec = autocvar_sv_stepheight * '0 0 1';
565 jumpstepheightvec = stepheightvec +
566 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
567 // 0.75 factor is for safety to make the jumps easy
572 int activerealplayers = 0;
574 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
575 activerealplayers = M_ARGV(0, int);
576 realplayers = M_ARGV(1, int);
578 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
586 // add/remove bots if needed to make sure there are at least
587 // minplayers+bot_number, or remove all bots if no one is playing
588 // But don't remove bots immediately on level change, as the real players
589 // usually haven't rejoined yet
590 bots_would_leave = false;
591 if (teamplay && autocvar_bot_vs_human && AvailableTeams() == 2)
592 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
593 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
595 float realminplayers, minplayers;
596 realminplayers = autocvar_minplayers;
597 minplayers = max(0, floor(realminplayers));
599 float realminbots, minbots;
600 realminbots = autocvar_bot_number;
601 minbots = max(0, floor(realminbots));
603 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
605 bots_would_leave = true;
609 // if there are no players, remove bots
613 // only add one bot per frame to avoid utter chaos
614 if(time > botframe_nextthink)
616 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
617 while (currentbots < bots)
619 if (bot_spawn() == NULL)
621 bprint("Can not add bot, server full.\n");
625 while (currentbots > bots)
632 void bot_serverframe()
634 if (intermission_running)
640 bot_calculate_stepheightvec();
641 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
643 if(time > autoskill_nextthink)
646 a = autocvar_skill_auto;
649 autoskill_nextthink = time + 5;
652 if(time > botframe_nextthink)
655 botframe_nextthink = time + 10;
658 bot_ignore_bots = autocvar_bot_ignore_bots;
660 if(botframe_spawnedwaypoints)
662 if(autocvar_waypoint_benchmark)
666 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
667 if (botframe_spawnedwaypoints)
669 if(botframe_cachedwaypointlinks)
671 if(!botframe_loadedforcedlinks)
672 waypoint_load_links_hardwired();
676 // TODO: Make this check cleaner
677 IL_EACH(g_waypoints, time - it.nextthink > 10,
679 waypoint_save_links();
686 botframe_spawnedwaypoints = true;
688 if(!waypoint_load_links())
689 waypoint_schedulerelinkall();
694 // cycle the goal token from one bot to the next each frame
695 // (this prevents them from all doing spawnfunc_waypoint searches on the same
696 // frame, which causes choppy framerates)
697 if (bot_strategytoken_taken)
699 bot_strategytoken_taken = false;
700 if (bot_strategytoken)
701 bot_strategytoken = bot_strategytoken.nextbot;
702 if (!bot_strategytoken)
703 bot_strategytoken = bot_list;
706 if (botframe_nextdangertime < time)
709 interval = autocvar_bot_ai_dangerdetectioninterval;
710 if (botframe_nextdangertime < time - interval * 1.5)
711 botframe_nextdangertime = time;
712 botframe_nextdangertime = botframe_nextdangertime + interval;
713 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
717 if (autocvar_g_waypointeditor)
718 botframe_showwaypointlinks();
720 if (autocvar_g_waypointeditor_auto)
721 botframe_autowaypoints();
723 if(time > bot_cvar_nextthink)
726 bot_custom_weapon_priority_setup();
727 bot_cvar_nextthink = time + 5;