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();
49 currentbots = currentbots + 1;
50 bot_setnameandstuff(bot);
52 PutClientInServer(bot);
57 void bot_think(entity this)
59 if (this.bot_nextthink > time)
62 this.flags &= ~FL_GODMODE;
64 this.flags |= FL_GODMODE;
66 this.bot_nextthink = this.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, this.bot_aiskill);
67 //if (this.bot_painintensity > 0)
68 // this.bot_painintensity = this.bot_painintensity - (skill + 1) * 40 * frametime;
70 //this.bot_painintensity = this.bot_painintensity + this.bot_oldhealth - this.health;
71 //this.bot_painintensity = bound(0, this.bot_painintensity, 100);
73 if (!IS_PLAYER(this) || (autocvar_g_campaign && !campaign_bots_may_start))
75 this.bot_nextthink = time + 0.5;
81 this.v_angle = this.angles;
83 this.fixangle = false;
88 this.dmg_inflictor = NULL;
90 // calculate an aiming latency based on the skill setting
91 // (simulated network latency + naturally delayed reflexes)
92 //this.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
93 // minimum ping 20+10 random
94 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
95 // skill 10 = ping 0.2 (adrenaline)
96 // skill 0 = ping 0.7 (slightly drunk)
99 PHYS_INPUT_BUTTON_ATCK(this) = false;
100 PHYS_INPUT_BUTTON_JUMP(this) = false;
101 PHYS_INPUT_BUTTON_ATCK2(this) = false;
102 PHYS_INPUT_BUTTON_ZOOM(this) = false;
103 PHYS_INPUT_BUTTON_CROUCH(this) = false;
104 PHYS_INPUT_BUTTON_HOOK(this) = false;
105 PHYS_INPUT_BUTTON_INFO(this) = false;
106 PHYS_INPUT_BUTTON_DRAG(this) = false;
107 PHYS_INPUT_BUTTON_CHAT(this) = false;
108 PHYS_INPUT_BUTTON_USE(this) = false;
110 if (time < game_starttime)
112 // block the bot during the countdown to game start
113 this.movement = '0 0 0';
114 this.bot_nextthink = game_starttime;
118 // if dead, just wait until we can respawn
121 if (this.deadflag == DEAD_DEAD)
123 PHYS_INPUT_BUTTON_JUMP(this) = true; // press jump to respawn
124 this.bot_strategytime = 0;
127 else if(this.aistatus & AI_STATUS_STUCK)
128 navigation_unstuck(this);
130 // now call the current bot AI (havocbot for example)
134 void bot_setnameandstuff(entity this)
137 float file, tokens, prio;
139 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
140 string name, prefix, suffix;
142 if(autocvar_g_campaign)
149 prefix = autocvar_bot_prefix;
150 suffix = autocvar_bot_suffix;
153 file = fopen(autocvar_bot_config_file, FILE_READ);
157 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
162 RandomSelection_Init();
163 while((readfile = fgets(file)))
165 if(substring(readfile, 0, 2) == "//")
167 if(substring(readfile, 0, 1) == "#")
169 tokens = tokenizebyseparator(readfile, "\t");
174 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
175 if(s == it.cleanname)
181 RandomSelection_AddString(readfile, 1, prio);
183 readfile = RandomSelection_chosen_string;
187 tokens = tokenizebyseparator(readfile, "\t");
188 if(argv(0) != "") bot_name = argv(0);
189 else bot_name = "Bot";
191 if(argv(1) != "") bot_model = argv(1);
194 if(argv(2) != "") bot_skin = argv(2);
197 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
198 else bot_shirt = ftos(floor(random() * 15));
200 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
201 else bot_pants = ftos(floor(random() * 15));
203 this.bot_forced_team = stof(argv(5));
207 #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
208 //print(bot_name, ": ping=", argv(9), "\n");
210 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
211 READSKILL(bot_moveskill, 2, 0); // move skill
212 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
214 READSKILL(bot_pingskill, 0.5, 0); // ping skill
216 READSKILL(bot_weaponskill, 2, 0); // weapon skill
217 READSKILL(bot_aggresskill, 1, 0); // aggre skill
218 READSKILL(bot_rangepreference, 1, 0); // read skill
220 READSKILL(bot_aimskill, 2, 0); // aim skill
221 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
222 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
224 READSKILL(bot_thinkskill, 1, 0.5); // think skill
225 READSKILL(bot_aiskill, 2, 0); // "ai" skill
227 this.bot_config_loaded = true;
229 // this is really only a default, JoinBestTeam is called later
230 setcolor(this, stof(bot_shirt) * 16 + stof(bot_pants));
231 this.bot_preferredcolors = this.clientcolors;
234 if (autocvar_bot_usemodelnames)
239 // number bots with identical names
241 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
242 if(it.cleanname == name)
246 this.netname = this.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
248 this.netname = this.netname_freeme = strzone(strcat(prefix, name, suffix));
250 this.cleanname = strzone(name);
252 // pick the model and skin
253 if(substring(bot_model, -4, 1) != ".")
254 bot_model = strcat(bot_model, ".iqm");
255 this.playermodel = this.playermodel_freeme = strzone(strcat("models/player/", bot_model));
256 this.playerskin = this.playerskin_freeme = strzone(bot_skin);
258 this.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the NULL
259 this.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
262 void bot_custom_weapon_priority_setup()
266 bot_custom_weapon = false;
268 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
269 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
270 autocvar_bot_ai_custom_weapon_priority_close == "" ||
271 autocvar_bot_ai_custom_weapon_priority_distances == ""
276 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
281 bot_distance_far = stof(argv(0));
282 bot_distance_close = stof(argv(1));
284 if(bot_distance_far < bot_distance_close){
285 bot_distance_far = stof(argv(1));
286 bot_distance_close = stof(argv(0));
289 // Initialize list of weapons
290 bot_weapons_far[0] = -1;
291 bot_weapons_mid[0] = -1;
292 bot_weapons_close[0] = -1;
294 // Parse far distance weapon priorities
295 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
298 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
300 if ( w >= WEP_FIRST && w <= WEP_LAST) {
301 bot_weapons_far[c] = w;
305 if(c < Weapons_COUNT)
306 bot_weapons_far[c] = -1;
308 // Parse mid distance weapon priorities
309 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
312 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
314 if ( w >= WEP_FIRST && w <= WEP_LAST) {
315 bot_weapons_mid[c] = w;
319 if(c < Weapons_COUNT)
320 bot_weapons_mid[c] = -1;
322 // Parse close distance weapon priorities
323 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
326 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
328 if ( w >= WEP_FIRST && w <= WEP_LAST) {
329 bot_weapons_close[c] = w;
333 if(c < Weapons_COUNT)
334 bot_weapons_close[c] = -1;
336 bot_custom_weapon = true;
342 //dprint("bot_endgame\n");
346 setcolor(e, e.bot_preferredcolors);
349 // if dynamic waypoints are ever implemented, save them here
352 void bot_relinkplayerlist()
358 entity prevbot = NULL;
363 if(IS_BOT_CLIENT(it))
366 prevbot.nextbot = it;
370 bot_list.nextbot = NULL;
376 LOG_TRACE("relink: ", ftos(currentbots), " bots seen.");
377 bot_strategytoken = bot_list;
378 bot_strategytoken_taken = true;
381 void bot_clientdisconnect(entity this)
383 if (!IS_BOT_CLIENT(this))
385 bot_clearqueue(this);
387 strunzone(this.cleanname);
388 if(this.netname_freeme)
389 strunzone(this.netname_freeme);
390 if(this.playermodel_freeme)
391 strunzone(this.playermodel_freeme);
392 if(this.playerskin_freeme)
393 strunzone(this.playerskin_freeme);
394 this.cleanname = string_null;
395 this.netname_freeme = string_null;
396 this.playermodel_freeme = string_null;
397 this.playerskin_freeme = string_null;
398 if(this.bot_cmd_current)
399 delete(this.bot_cmd_current);
400 if(bot_waypoint_queue_owner==this)
401 bot_waypoint_queue_owner = NULL;
404 void bot_clientconnect(entity this)
406 if (!IS_BOT_CLIENT(this)) return;
407 this.bot_preferredcolors = this.clientcolors;
408 this.bot_nextthink = time - random();
409 this.lag_func = bot_lagfunc;
411 this.createdtime = this.bot_nextthink;
413 if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
414 bot_setnameandstuff(this);
416 if(this.bot_forced_team==1)
417 this.team = NUM_TEAM_1;
418 else if(this.bot_forced_team==2)
419 this.team = NUM_TEAM_2;
420 else if(this.bot_forced_team==3)
421 this.team = NUM_TEAM_3;
422 else if(this.bot_forced_team==4)
423 this.team = NUM_TEAM_4;
425 JoinBestTeam(this, false, true);
427 havocbot_setupbot(this);
430 void bot_removefromlargestteam()
432 CheckAllowedTeams(NULL);
440 FOREACH_CLIENT(it.isbot,
447 besttime = it.createdtime;
454 case NUM_TEAM_1: thiscount = c1; break;
455 case NUM_TEAM_2: thiscount = c2; break;
456 case NUM_TEAM_3: thiscount = c3; break;
457 case NUM_TEAM_4: thiscount = c4; break;
460 if(thiscount > bestcount)
462 bestcount = thiscount;
463 besttime = it.createdtime;
466 else if(thiscount == bestcount && besttime < it.createdtime)
468 besttime = it.createdtime;
473 return; // no bots to remove
474 currentbots = currentbots - 1;
478 void bot_removenewest()
482 bot_removefromlargestteam();
490 FOREACH_CLIENT(it.isbot,
497 besttime = it.createdtime;
500 if(besttime < it.createdtime)
502 besttime = it.createdtime;
508 return; // no bots to remove
510 currentbots = currentbots - 1;
514 void autoskill(float factor)
521 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
522 if(IS_REAL_CLIENT(it))
523 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
525 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
528 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
529 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
530 if(bestbot < 0 || bestplayer < 0)
532 LOG_TRACE("not doing anything");
533 // don't return, let it reset all counters below
535 else if(bestbot <= bestplayer * factor - 2)
537 if(autocvar_skill < 17)
539 LOG_TRACE("2 frags difference, increasing skill");
540 cvar_set("skill", ftos(autocvar_skill + 1));
541 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
544 else if(bestbot >= bestplayer * factor + 2)
546 if(autocvar_skill > 0)
548 LOG_TRACE("2 frags difference, decreasing skill");
549 cvar_set("skill", ftos(autocvar_skill - 1));
550 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
555 LOG_TRACE("not doing anything");
557 // don't reset counters, wait for them to accumulate
560 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
563 void bot_calculate_stepheightvec()
565 stepheightvec = autocvar_sv_stepheight * '0 0 1';
566 jumpstepheightvec = stepheightvec +
567 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
568 // 0.75 factor is for safety to make the jumps easy
573 int activerealplayers = 0;
575 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
576 activerealplayers = M_ARGV(0, int);
577 realplayers = M_ARGV(1, int);
579 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
587 // add/remove bots if needed to make sure there are at least
588 // minplayers+bot_number, or remove all bots if no one is playing
589 // But don't remove bots immediately on level change, as the real players
590 // usually haven't rejoined yet
591 bots_would_leave = false;
592 if (teamplay && autocvar_bot_vs_human && AvailableTeams() == 2)
593 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
594 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
596 float realminplayers, minplayers;
597 realminplayers = autocvar_minplayers;
598 minplayers = max(0, floor(realminplayers));
600 float realminbots, minbots;
601 realminbots = autocvar_bot_number;
602 minbots = max(0, floor(realminbots));
604 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
606 bots_would_leave = true;
610 // if there are no players, remove bots
614 // only add one bot per frame to avoid utter chaos
615 if(time > botframe_nextthink)
617 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
618 while (currentbots < bots)
620 if (bot_spawn() == NULL)
622 bprint("Can not add bot, server full.\n");
626 while (currentbots > bots)
633 void bot_serverframe()
635 if (intermission_running)
641 bot_calculate_stepheightvec();
642 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
644 if(time > autoskill_nextthink)
647 a = autocvar_skill_auto;
650 autoskill_nextthink = time + 5;
653 if(time > botframe_nextthink)
656 botframe_nextthink = time + 10;
659 bot_ignore_bots = autocvar_bot_ignore_bots;
661 if(botframe_spawnedwaypoints)
663 if(autocvar_waypoint_benchmark)
667 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
668 if (botframe_spawnedwaypoints)
670 if(botframe_cachedwaypointlinks)
672 if(!botframe_loadedforcedlinks)
673 waypoint_load_links_hardwired();
677 // TODO: Make this check cleaner
678 IL_EACH(g_waypoints, time - it.nextthink > 10,
680 waypoint_save_links();
687 botframe_spawnedwaypoints = true;
689 if(!waypoint_load_links())
690 waypoint_schedulerelinkall();
695 // cycle the goal token from one bot to the next each frame
696 // (this prevents them from all doing spawnfunc_waypoint searches on the same
697 // frame, which causes choppy framerates)
698 if (bot_strategytoken_taken)
700 bot_strategytoken_taken = false;
701 if (bot_strategytoken)
702 bot_strategytoken = bot_strategytoken.nextbot;
703 if (!bot_strategytoken)
704 bot_strategytoken = bot_list;
707 if (botframe_nextdangertime < time)
710 interval = autocvar_bot_ai_dangerdetectioninterval;
711 if (botframe_nextdangertime < time - interval * 1.5)
712 botframe_nextdangertime = time;
713 botframe_nextdangertime = botframe_nextdangertime + interval;
714 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
718 if (autocvar_g_waypointeditor)
719 botframe_showwaypointlinks();
721 if (autocvar_g_waypointeditor_auto)
722 botframe_autowaypoints();
724 if(time > bot_cvar_nextthink)
727 bot_custom_weapon_priority_setup();
728 bot_cvar_nextthink = time + 5;