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 = this.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, this.bot_aiskill);
68 if(this.bot_nextthink < time)
69 this.bot_nextthink = time + autocvar_bot_ai_thinkinterval * pow(0.5, this.bot_aiskill);
70 //if (this.bot_painintensity > 0)
71 // this.bot_painintensity = this.bot_painintensity - (skill + 1) * 40 * frametime;
73 //this.bot_painintensity = this.bot_painintensity + this.bot_oldhealth - this.health;
74 //this.bot_painintensity = bound(0, this.bot_painintensity, 100);
76 if (!IS_PLAYER(this) || (autocvar_g_campaign && !campaign_bots_may_start))
78 this.movement = '0 0 0';
79 this.bot_nextthink = time + 0.5;
85 this.v_angle = this.angles;
87 this.fixangle = false;
92 this.dmg_inflictor = NULL;
94 // calculate an aiming latency based on the skill setting
95 // (simulated network latency + naturally delayed reflexes)
96 //this.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
97 // minimum ping 20+10 random
98 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
99 // skill 10 = ping 0.2 (adrenaline)
100 // skill 0 = ping 0.7 (slightly drunk)
103 PHYS_INPUT_BUTTON_ATCK(this) = false;
104 PHYS_INPUT_BUTTON_JUMP(this) = false;
105 PHYS_INPUT_BUTTON_ATCK2(this) = false;
106 PHYS_INPUT_BUTTON_ZOOM(this) = false;
107 PHYS_INPUT_BUTTON_CROUCH(this) = false;
108 PHYS_INPUT_BUTTON_HOOK(this) = false;
109 PHYS_INPUT_BUTTON_INFO(this) = false;
110 PHYS_INPUT_BUTTON_DRAG(this) = false;
111 PHYS_INPUT_BUTTON_CHAT(this) = false;
112 PHYS_INPUT_BUTTON_USE(this) = false;
114 if (time < game_starttime)
116 // block the bot during the countdown to game start
117 this.movement = '0 0 0';
118 this.bot_nextthink = game_starttime;
122 // if dead, just wait until we can respawn
125 this.movement = '0 0 0';
126 if (this.deadflag == DEAD_DEAD)
128 PHYS_INPUT_BUTTON_JUMP(this) = true; // press jump to respawn
129 this.bot_strategytime = 0;
132 else if(this.aistatus & AI_STATUS_STUCK)
133 navigation_unstuck(this);
135 // now call the current bot AI (havocbot for example)
139 void bot_setnameandstuff(entity this)
142 float file, tokens, prio;
144 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
145 string name, prefix, suffix;
147 if(autocvar_g_campaign)
154 prefix = autocvar_bot_prefix;
155 suffix = autocvar_bot_suffix;
158 file = fopen(autocvar_bot_config_file, FILE_READ);
162 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
167 RandomSelection_Init();
168 while((readfile = fgets(file)))
170 if(substring(readfile, 0, 2) == "//")
172 if(substring(readfile, 0, 1) == "#")
174 tokens = tokenizebyseparator(readfile, "\t");
179 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
180 if(s == it.cleanname)
186 RandomSelection_AddString(readfile, 1, prio);
188 readfile = RandomSelection_chosen_string;
192 tokens = tokenizebyseparator(readfile, "\t");
193 if(argv(0) != "") bot_name = argv(0);
194 else bot_name = "Bot";
196 if(argv(1) != "") bot_model = argv(1);
199 if(argv(2) != "") bot_skin = argv(2);
202 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
203 else bot_shirt = ftos(floor(random() * 15));
205 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
206 else bot_pants = ftos(floor(random() * 15));
208 this.bot_forced_team = stof(argv(5));
212 #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
213 //print(bot_name, ": ping=", argv(9), "\n");
215 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
216 READSKILL(bot_moveskill, 2, 0); // move skill
217 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
219 READSKILL(bot_pingskill, 0.5, 0); // ping skill
221 READSKILL(bot_weaponskill, 2, 0); // weapon skill
222 READSKILL(bot_aggresskill, 1, 0); // aggre skill
223 READSKILL(bot_rangepreference, 1, 0); // read skill
225 READSKILL(bot_aimskill, 2, 0); // aim skill
226 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
227 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
229 READSKILL(bot_thinkskill, 1, 0.5); // think skill
230 READSKILL(bot_aiskill, 2, 0); // "ai" skill
232 this.bot_config_loaded = true;
234 // this is really only a default, JoinBestTeam is called later
235 setcolor(this, stof(bot_shirt) * 16 + stof(bot_pants));
236 this.bot_preferredcolors = this.clientcolors;
239 if (autocvar_bot_usemodelnames)
244 // number bots with identical names
246 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
247 if(it.cleanname == name)
251 this.netname = this.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
253 this.netname = this.netname_freeme = strzone(strcat(prefix, name, suffix));
255 this.cleanname = strzone(name);
257 // pick the model and skin
258 if(substring(bot_model, -4, 1) != ".")
259 bot_model = strcat(bot_model, ".iqm");
260 this.playermodel = this.playermodel_freeme = strzone(strcat("models/player/", bot_model));
261 this.playerskin = this.playerskin_freeme = strzone(bot_skin);
263 this.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the NULL
264 this.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
267 void bot_custom_weapon_priority_setup()
271 bot_custom_weapon = false;
273 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
274 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
275 autocvar_bot_ai_custom_weapon_priority_close == "" ||
276 autocvar_bot_ai_custom_weapon_priority_distances == ""
281 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
286 bot_distance_far = stof(argv(0));
287 bot_distance_close = stof(argv(1));
289 if(bot_distance_far < bot_distance_close){
290 bot_distance_far = stof(argv(1));
291 bot_distance_close = stof(argv(0));
294 // Initialize list of weapons
295 bot_weapons_far[0] = -1;
296 bot_weapons_mid[0] = -1;
297 bot_weapons_close[0] = -1;
299 // Parse far distance weapon priorities
300 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
303 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
305 if ( w >= WEP_FIRST && w <= WEP_LAST) {
306 bot_weapons_far[c] = w;
310 if(c < Weapons_COUNT)
311 bot_weapons_far[c] = -1;
313 // Parse mid distance weapon priorities
314 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
317 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
319 if ( w >= WEP_FIRST && w <= WEP_LAST) {
320 bot_weapons_mid[c] = w;
324 if(c < Weapons_COUNT)
325 bot_weapons_mid[c] = -1;
327 // Parse close distance weapon priorities
328 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
331 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
333 if ( w >= WEP_FIRST && w <= WEP_LAST) {
334 bot_weapons_close[c] = w;
338 if(c < Weapons_COUNT)
339 bot_weapons_close[c] = -1;
341 bot_custom_weapon = true;
346 bot_relinkplayerlist();
350 setcolor(e, e.bot_preferredcolors);
353 // if dynamic waypoints are ever implemented, save them here
356 void bot_relinkplayerlist()
362 entity prevbot = NULL;
367 if(IS_BOT_CLIENT(it))
370 prevbot.nextbot = it;
374 bot_list.nextbot = NULL;
380 LOG_TRACE("relink: ", ftos(currentbots), " bots seen.");
381 bot_strategytoken = bot_list;
382 bot_strategytoken_taken = true;
385 void bot_clientdisconnect(entity this)
387 if (!IS_BOT_CLIENT(this))
389 bot_clearqueue(this);
391 strunzone(this.cleanname);
392 if(this.netname_freeme)
393 strunzone(this.netname_freeme);
394 if(this.playermodel_freeme)
395 strunzone(this.playermodel_freeme);
396 if(this.playerskin_freeme)
397 strunzone(this.playerskin_freeme);
398 this.cleanname = string_null;
399 this.netname_freeme = string_null;
400 this.playermodel_freeme = string_null;
401 this.playerskin_freeme = string_null;
402 if(this.bot_cmd_current)
403 delete(this.bot_cmd_current);
404 if(bot_waypoint_queue_owner == this)
405 bot_waypoint_queue_owner = NULL;
408 void bot_clientconnect(entity this)
410 if (!IS_BOT_CLIENT(this)) return;
411 this.bot_preferredcolors = this.clientcolors;
412 this.bot_nextthink = time - random();
413 this.lag_func = bot_lagfunc;
415 this.createdtime = this.bot_nextthink;
417 if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
418 bot_setnameandstuff(this);
420 if(this.bot_forced_team==1)
421 this.team = NUM_TEAM_1;
422 else if(this.bot_forced_team==2)
423 this.team = NUM_TEAM_2;
424 else if(this.bot_forced_team==3)
425 this.team = NUM_TEAM_3;
426 else if(this.bot_forced_team==4)
427 this.team = NUM_TEAM_4;
429 JoinBestTeam(this, false, true);
431 havocbot_setupbot(this);
434 void bot_removefromlargestteam()
436 CheckAllowedTeams(NULL);
444 FOREACH_CLIENT(it.isbot,
451 besttime = it.createdtime;
458 case NUM_TEAM_1: thiscount = c1; break;
459 case NUM_TEAM_2: thiscount = c2; break;
460 case NUM_TEAM_3: thiscount = c3; break;
461 case NUM_TEAM_4: thiscount = c4; break;
464 if(thiscount > bestcount)
466 bestcount = thiscount;
467 besttime = it.createdtime;
470 else if(thiscount == bestcount && besttime < it.createdtime)
472 besttime = it.createdtime;
477 return; // no bots to remove
478 currentbots = currentbots - 1;
482 void bot_removenewest()
486 bot_removefromlargestteam();
494 FOREACH_CLIENT(it.isbot,
501 besttime = it.createdtime;
504 if(besttime < it.createdtime)
506 besttime = it.createdtime;
512 return; // no bots to remove
514 currentbots = currentbots - 1;
518 void autoskill(float factor)
525 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
526 if(IS_REAL_CLIENT(it))
527 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
529 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
532 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
533 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
534 if(bestbot < 0 || bestplayer < 0)
536 LOG_TRACE("not doing anything");
537 // don't return, let it reset all counters below
539 else if(bestbot <= bestplayer * factor - 2)
541 if(autocvar_skill < 17)
543 LOG_TRACE("2 frags difference, increasing skill");
544 cvar_set("skill", ftos(autocvar_skill + 1));
545 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
548 else if(bestbot >= bestplayer * factor + 2)
550 if(autocvar_skill > 0)
552 LOG_TRACE("2 frags difference, decreasing skill");
553 cvar_set("skill", ftos(autocvar_skill - 1));
554 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
559 LOG_TRACE("not doing anything");
561 // don't reset counters, wait for them to accumulate
564 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
567 void bot_calculate_stepheightvec()
569 stepheightvec = autocvar_sv_stepheight * '0 0 1';
570 jumpstepheightvec = stepheightvec +
571 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
572 // 0.75 factor is for safety to make the jumps easy
577 int activerealplayers = 0;
579 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
580 activerealplayers = M_ARGV(0, int);
581 realplayers = M_ARGV(1, int);
583 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
591 // add/remove bots if needed to make sure there are at least
592 // minplayers+bot_number, or remove all bots if no one is playing
593 // But don't remove bots immediately on level change, as the real players
594 // usually haven't rejoined yet
595 bots_would_leave = false;
596 if (teamplay && autocvar_bot_vs_human && AvailableTeams() == 2)
597 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
598 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
600 float realminplayers, minplayers;
601 realminplayers = autocvar_minplayers;
602 minplayers = max(0, floor(realminplayers));
604 float realminbots, minbots;
605 realminbots = autocvar_bot_number;
606 minbots = max(0, floor(realminbots));
608 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
610 bots_would_leave = true;
614 // if there are no players, remove bots
618 // only add one bot per frame to avoid utter chaos
619 if(time > botframe_nextthink)
621 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
622 while (currentbots < bots)
624 if (bot_spawn() == NULL)
626 bprint("Can not add bot, server full.\n");
630 while (currentbots > bots)
637 void bot_remove_from_bot_list(entity this)
640 entity prev_bot = NULL;
646 bot_list = this.nextbot;
648 prev_bot.nextbot = this.nextbot;
649 if(bot_strategytoken == this)
651 bot_strategytoken = this.nextbot;
652 bot_strategytoken_taken = true;
662 void bot_clear(entity this)
664 bot_remove_from_bot_list(this);
665 if(bot_waypoint_queue_owner == this)
666 bot_waypoint_queue_owner = NULL;
667 this.aistatus &= ~AI_STATUS_STUCK; // otherwise bot_waypoint_queue_owner will be set again to this by navigation_unstuck
670 void bot_serverframe()
678 bot_calculate_stepheightvec();
679 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
681 if(time > autoskill_nextthink)
684 a = autocvar_skill_auto;
687 autoskill_nextthink = time + 5;
690 if(time > botframe_nextthink)
693 botframe_nextthink = time + 10;
696 bot_ignore_bots = autocvar_bot_ignore_bots;
698 if(botframe_spawnedwaypoints)
700 if(autocvar_waypoint_benchmark)
704 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
705 if (botframe_spawnedwaypoints)
707 if(botframe_cachedwaypointlinks)
709 if(!botframe_loadedforcedlinks)
710 waypoint_load_links_hardwired();
714 // TODO: Make this check cleaner
715 IL_EACH(g_waypoints, time - it.nextthink > 10,
717 waypoint_save_links();
724 botframe_spawnedwaypoints = true;
726 if(!waypoint_load_links())
727 waypoint_schedulerelinkall();
732 // cycle the goal token from one bot to the next each frame
733 // (this prevents them from all doing spawnfunc_waypoint searches on the same
734 // frame, which causes choppy framerates)
735 if (bot_strategytoken_taken)
737 bot_strategytoken_taken = false;
738 if (bot_strategytoken)
739 bot_strategytoken = bot_strategytoken.nextbot;
740 if (!bot_strategytoken)
741 bot_strategytoken = bot_list;
744 if (botframe_nextdangertime < time)
747 interval = autocvar_bot_ai_dangerdetectioninterval;
748 if (botframe_nextdangertime < time - interval * 1.5)
749 botframe_nextdangertime = time;
750 botframe_nextdangertime = botframe_nextdangertime + interval;
751 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
755 if (autocvar_g_waypointeditor)
756 botframe_showwaypointlinks();
758 if (autocvar_g_waypointeditor_auto)
759 botframe_autowaypoints();
761 if(time > bot_cvar_nextthink)
764 bot_custom_weapon_priority_setup();
765 bot_cvar_nextthink = time + 5;