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/net_linked.qh>
31 #include <common/mapinfo.qh>
32 #include <common/teams.qh>
33 #include <common/util.qh>
35 #include <server/scores_rules.qh>
37 #include <common/weapons/_all.qh>
39 #include <lib/csqcmodel/sv_model.qh>
41 #include <lib/warpzone/common.qh>
42 #include <lib/warpzone/util_server.qh>
46 entity bot = spawnclient();
50 currentbots = currentbots + 1;
51 bot_setnameandstuff(bot);
53 PutClientInServer(bot);
58 void bot_think(entity this)
60 if (this.bot_nextthink > time)
63 this.flags &= ~FL_GODMODE;
65 this.flags |= FL_GODMODE;
67 this.bot_nextthink = max(time, this.bot_nextthink) + max(0.01, autocvar_bot_ai_thinkinterval * pow(0.5, this.bot_aiskill) * min(14 / (skill + 14), 1));
69 //if (this.bot_painintensity > 0)
70 // this.bot_painintensity = this.bot_painintensity - (skill + 1) * 40 * frametime;
72 //this.bot_painintensity = this.bot_painintensity + this.bot_oldhealth - this.health;
73 //this.bot_painintensity = bound(0, this.bot_painintensity, 100);
75 if (!IS_PLAYER(this) || (autocvar_g_campaign && !campaign_bots_may_start))
77 this.movement = '0 0 0';
78 this.bot_nextthink = time + 0.5;
84 this.v_angle = this.angles;
86 this.fixangle = false;
91 this.dmg_inflictor = NULL;
93 // calculate an aiming latency based on the skill setting
94 // (simulated network latency + naturally delayed reflexes)
95 //this.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
96 // minimum ping 20+10 random
97 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
98 // skill 10 = ping 0.2 (adrenaline)
99 // skill 0 = ping 0.7 (slightly drunk)
102 PHYS_INPUT_BUTTON_ATCK(this) = false;
103 PHYS_INPUT_BUTTON_JUMP(this) = false;
104 PHYS_INPUT_BUTTON_ATCK2(this) = false;
105 PHYS_INPUT_BUTTON_ZOOM(this) = false;
106 PHYS_INPUT_BUTTON_CROUCH(this) = false;
107 PHYS_INPUT_BUTTON_HOOK(this) = false;
108 PHYS_INPUT_BUTTON_INFO(this) = false;
109 PHYS_INPUT_BUTTON_DRAG(this) = false;
110 PHYS_INPUT_BUTTON_CHAT(this) = false;
111 PHYS_INPUT_BUTTON_USE(this) = false;
113 if (time < game_starttime)
115 // block the bot during the countdown to game start
116 this.movement = '0 0 0';
117 this.bot_nextthink = game_starttime;
121 // if dead, just wait until we can respawn
124 this.movement = '0 0 0';
125 if (this.deadflag == DEAD_DEAD)
127 PHYS_INPUT_BUTTON_JUMP(this) = true; // press jump to respawn
128 this.bot_strategytime = 0;
131 else if(this.aistatus & AI_STATUS_STUCK)
132 navigation_unstuck(this);
134 // now call the current bot AI (havocbot for example)
138 void bot_setnameandstuff(entity this)
141 float file, tokens, prio;
143 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
144 string name, prefix, suffix;
146 if(autocvar_g_campaign)
153 prefix = autocvar_bot_prefix;
154 suffix = autocvar_bot_suffix;
157 file = fopen(autocvar_bot_config_file, FILE_READ);
161 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
166 RandomSelection_Init();
167 while((readfile = fgets(file)))
169 if(substring(readfile, 0, 2) == "//")
171 if(substring(readfile, 0, 1) == "#")
173 tokens = tokenizebyseparator(readfile, "\t");
178 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
179 if(s == it.cleanname)
185 RandomSelection_AddString(readfile, 1, prio);
187 readfile = RandomSelection_chosen_string;
191 tokens = tokenizebyseparator(readfile, "\t");
192 if(argv(0) != "") bot_name = argv(0);
193 else bot_name = "Bot";
195 if(argv(1) != "") bot_model = argv(1);
198 if(argv(2) != "") bot_skin = argv(2);
201 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
202 else bot_shirt = ftos(floor(random() * 15));
204 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
205 else bot_pants = ftos(floor(random() * 15));
207 this.bot_forced_team = stof(argv(5));
211 #define READSKILL(f, w, r) MACRO_BEGIN { \
212 if(argv(prio) != "") \
213 this.f = stof(argv(prio)) * w; \
215 this.f = (!autocvar_g_campaign) * (2 * random() - 1) * r * w; \
218 //print(bot_name, ": ping=", argv(9), "\n");
220 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
221 READSKILL(bot_moveskill, 2, 0); // move skill
222 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
224 READSKILL(bot_pingskill, 0.5, 0); // ping skill
226 READSKILL(bot_weaponskill, 2, 0); // weapon skill
227 READSKILL(bot_aggresskill, 1, 0); // aggre skill
228 READSKILL(bot_rangepreference, 1, 0); // read skill
230 READSKILL(bot_aimskill, 2, 0); // aim skill
231 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
232 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
234 READSKILL(bot_thinkskill, 1, 0.5); // think skill
235 READSKILL(bot_aiskill, 2, 0); // "ai" skill
237 this.bot_config_loaded = true;
239 // this is really only a default, JoinBestTeam is called later
240 setcolor(this, stof(bot_shirt) * 16 + stof(bot_pants));
241 this.bot_preferredcolors = this.clientcolors;
244 if (autocvar_bot_usemodelnames)
249 // number bots with identical names
251 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
252 if(it.cleanname == name)
256 this.netname = this.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
258 this.netname = this.netname_freeme = strzone(strcat(prefix, name, suffix));
260 this.cleanname = strzone(name);
262 // pick the model and skin
263 if(substring(bot_model, -4, 1) != ".")
264 bot_model = strcat(bot_model, ".iqm");
265 this.playermodel = this.playermodel_freeme = strzone(strcat("models/player/", bot_model));
266 this.playerskin = this.playerskin_freeme = strzone(bot_skin);
268 this.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the NULL
269 this.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
272 void bot_custom_weapon_priority_setup()
276 bot_custom_weapon = false;
278 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
279 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
280 autocvar_bot_ai_custom_weapon_priority_close == "" ||
281 autocvar_bot_ai_custom_weapon_priority_distances == ""
286 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
291 bot_distance_far = stof(argv(0));
292 bot_distance_close = stof(argv(1));
294 if(bot_distance_far < bot_distance_close){
295 bot_distance_far = stof(argv(1));
296 bot_distance_close = stof(argv(0));
299 // Initialize list of weapons
300 bot_weapons_far[0] = -1;
301 bot_weapons_mid[0] = -1;
302 bot_weapons_close[0] = -1;
304 // Parse far distance weapon priorities
305 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
308 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
310 if ( w >= WEP_FIRST && w <= WEP_LAST) {
311 bot_weapons_far[c] = w;
315 if(c < Weapons_COUNT)
316 bot_weapons_far[c] = -1;
318 // Parse mid distance weapon priorities
319 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
322 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
324 if ( w >= WEP_FIRST && w <= WEP_LAST) {
325 bot_weapons_mid[c] = w;
329 if(c < Weapons_COUNT)
330 bot_weapons_mid[c] = -1;
332 // Parse close distance weapon priorities
333 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
336 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
338 if ( w >= WEP_FIRST && w <= WEP_LAST) {
339 bot_weapons_close[c] = w;
343 if(c < Weapons_COUNT)
344 bot_weapons_close[c] = -1;
346 bot_custom_weapon = true;
351 bot_relinkplayerlist();
355 setcolor(e, e.bot_preferredcolors);
358 // if dynamic waypoints are ever implemented, save them here
361 void bot_relinkplayerlist()
367 entity prevbot = NULL;
372 if(IS_BOT_CLIENT(it))
375 prevbot.nextbot = it;
379 bot_list.nextbot = NULL;
385 LOG_TRACE("relink: ", ftos(currentbots), " bots seen.");
386 bot_strategytoken = bot_list;
387 bot_strategytoken_taken = true;
390 void bot_clientdisconnect(entity this)
392 if (!IS_BOT_CLIENT(this))
394 bot_clearqueue(this);
396 strunzone(this.cleanname);
397 if(this.netname_freeme)
398 strunzone(this.netname_freeme);
399 if(this.playermodel_freeme)
400 strunzone(this.playermodel_freeme);
401 if(this.playerskin_freeme)
402 strunzone(this.playerskin_freeme);
403 this.cleanname = string_null;
404 this.netname_freeme = string_null;
405 this.playermodel_freeme = string_null;
406 this.playerskin_freeme = string_null;
407 if(this.bot_cmd_current)
408 delete(this.bot_cmd_current);
409 if(bot_waypoint_queue_owner == this)
410 bot_waypoint_queue_owner = NULL;
413 void bot_clientconnect(entity this)
415 if (!IS_BOT_CLIENT(this)) return;
416 this.bot_preferredcolors = this.clientcolors;
417 this.bot_nextthink = time - random();
418 this.lag_func = bot_lagfunc;
420 this.createdtime = this.bot_nextthink;
422 if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
423 bot_setnameandstuff(this);
425 if(this.bot_forced_team==1)
426 this.team = NUM_TEAM_1;
427 else if(this.bot_forced_team==2)
428 this.team = NUM_TEAM_2;
429 else if(this.bot_forced_team==3)
430 this.team = NUM_TEAM_3;
431 else if(this.bot_forced_team==4)
432 this.team = NUM_TEAM_4;
434 JoinBestTeam(this, false, true);
436 havocbot_setupbot(this);
439 void bot_removefromlargestteam()
441 CheckAllowedTeams(NULL);
449 FOREACH_CLIENT(it.isbot,
456 besttime = it.createdtime;
463 case NUM_TEAM_1: thiscount = c1; break;
464 case NUM_TEAM_2: thiscount = c2; break;
465 case NUM_TEAM_3: thiscount = c3; break;
466 case NUM_TEAM_4: thiscount = c4; break;
469 if(thiscount > bestcount)
471 bestcount = thiscount;
472 besttime = it.createdtime;
475 else if(thiscount == bestcount && besttime < it.createdtime)
477 besttime = it.createdtime;
482 return; // no bots to remove
483 currentbots = currentbots - 1;
487 void bot_removenewest()
491 bot_removefromlargestteam();
499 FOREACH_CLIENT(it.isbot,
506 besttime = it.createdtime;
509 if(besttime < it.createdtime)
511 besttime = it.createdtime;
517 return; // no bots to remove
519 currentbots = currentbots - 1;
523 void autoskill(float factor)
530 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
531 if(IS_REAL_CLIENT(it))
532 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
534 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
537 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
538 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
539 if(bestbot < 0 || bestplayer < 0)
541 LOG_TRACE("not doing anything");
542 // don't return, let it reset all counters below
544 else if(bestbot <= bestplayer * factor - 2)
546 if(autocvar_skill < 17)
548 LOG_TRACE("2 frags difference, increasing skill");
549 cvar_set("skill", ftos(autocvar_skill + 1));
550 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
553 else if(bestbot >= bestplayer * factor + 2)
555 if(autocvar_skill > 0)
557 LOG_TRACE("2 frags difference, decreasing skill");
558 cvar_set("skill", ftos(autocvar_skill - 1));
559 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
564 LOG_TRACE("not doing anything");
566 // don't reset counters, wait for them to accumulate
569 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
572 void bot_calculate_stepheightvec()
574 stepheightvec = autocvar_sv_stepheight * '0 0 1';
575 jumpstepheightvec = stepheightvec +
576 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
577 // 0.75 factor is for safety to make the jumps easy
582 int activerealplayers = 0;
584 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
585 activerealplayers = M_ARGV(0, int);
586 realplayers = M_ARGV(1, int);
588 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
596 // add/remove bots if needed to make sure there are at least
597 // minplayers+bot_number, or remove all bots if no one is playing
598 // But don't remove bots immediately on level change, as the real players
599 // usually haven't rejoined yet
600 bots_would_leave = false;
601 if (teamplay && autocvar_bot_vs_human && AvailableTeams() == 2)
602 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
603 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
605 float realminplayers, minplayers;
606 realminplayers = autocvar_minplayers;
607 minplayers = max(0, floor(realminplayers));
609 float realminbots, minbots;
610 realminbots = autocvar_bot_number;
611 minbots = max(0, floor(realminbots));
613 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
615 bots_would_leave = true;
619 // if there are no players, remove bots
623 // only add one bot per frame to avoid utter chaos
624 if(time > botframe_nextthink)
626 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
627 while (currentbots < bots)
629 if (bot_spawn() == NULL)
631 bprint("Can not add bot, server full.\n");
635 while (currentbots > bots)
642 void bot_remove_from_bot_list(entity this)
645 entity prev_bot = NULL;
651 bot_list = this.nextbot;
653 prev_bot.nextbot = this.nextbot;
654 if(bot_strategytoken == this)
656 bot_strategytoken = this.nextbot;
657 bot_strategytoken_taken = true;
667 void bot_clear(entity this)
669 bot_remove_from_bot_list(this);
670 if(bot_waypoint_queue_owner == this)
671 bot_waypoint_queue_owner = NULL;
672 this.aistatus &= ~AI_STATUS_STUCK; // otherwise bot_waypoint_queue_owner will be set again to this by navigation_unstuck
675 void bot_serverframe()
683 bot_calculate_stepheightvec();
684 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
686 if(time > autoskill_nextthink)
689 a = autocvar_skill_auto;
692 autoskill_nextthink = time + 5;
695 if(time > botframe_nextthink)
698 botframe_nextthink = time + 10;
701 bot_ignore_bots = autocvar_bot_ignore_bots;
703 if(botframe_spawnedwaypoints)
705 if(autocvar_waypoint_benchmark)
709 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
710 if (botframe_spawnedwaypoints)
712 if(botframe_cachedwaypointlinks)
714 if(!botframe_loadedforcedlinks)
715 waypoint_load_links_hardwired();
719 // TODO: Make this check cleaner
720 IL_EACH(g_waypoints, time - it.nextthink > 10,
722 waypoint_save_links();
729 botframe_spawnedwaypoints = true;
731 if(!waypoint_load_links())
732 waypoint_schedulerelinkall();
737 // cycle the goal token from one bot to the next each frame
738 // (this prevents them from all doing spawnfunc_waypoint searches on the same
739 // frame, which causes choppy framerates)
740 if (bot_strategytoken_taken)
742 bot_strategytoken_taken = false;
743 if (bot_strategytoken)
744 bot_strategytoken = bot_strategytoken.nextbot;
745 if (!bot_strategytoken)
746 bot_strategytoken = bot_list;
749 if (botframe_nextdangertime < time)
752 interval = autocvar_bot_ai_dangerdetectioninterval;
753 if (botframe_nextdangertime < time - interval * 1.5)
754 botframe_nextdangertime = time;
755 botframe_nextdangertime = botframe_nextdangertime + interval;
756 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
760 if (autocvar_g_waypointeditor)
761 botframe_showwaypointlinks();
763 if (autocvar_g_waypointeditor_auto)
764 botframe_autowaypoints();
766 if(time > bot_cvar_nextthink)
769 bot_custom_weapon_priority_setup();
770 bot_cvar_nextthink = time + 5;