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 "../../cl_client.qh"
19 #include "../../constants.qh"
20 #include "../../defs.qh"
21 #include "../../race.qh"
22 #include <common/t_items.qh>
24 #include "../../mutators/all.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 <common/weapons/all.qh>
36 #include <lib/csqcmodel/sv_model.qh>
38 #include <lib/warpzone/common.qh>
39 #include <lib/warpzone/util_server.qh>
43 entity bot = spawnclient();
46 currentbots = currentbots + 1;
47 bot_setnameandstuff(bot);
49 PutClientInServer(bot);
54 void bot_think(entity this)
56 if (this.bot_nextthink > time)
59 this.flags &= ~FL_GODMODE;
61 this.flags |= FL_GODMODE;
63 this.bot_nextthink = this.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, this.bot_aiskill);
64 //if (this.bot_painintensity > 0)
65 // this.bot_painintensity = this.bot_painintensity - (skill + 1) * 40 * frametime;
67 //this.bot_painintensity = this.bot_painintensity + this.bot_oldhealth - this.health;
68 //this.bot_painintensity = bound(0, this.bot_painintensity, 100);
70 if (!IS_PLAYER(this) || (autocvar_g_campaign && !campaign_bots_may_start))
72 this.bot_nextthink = time + 0.5;
78 this.v_angle = this.angles;
80 this.fixangle = false;
85 this.dmg_inflictor = NULL;
87 // calculate an aiming latency based on the skill setting
88 // (simulated network latency + naturally delayed reflexes)
89 //this.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
90 // minimum ping 20+10 random
91 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
92 // skill 10 = ping 0.2 (adrenaline)
93 // skill 0 = ping 0.7 (slightly drunk)
96 PHYS_INPUT_BUTTON_ATCK(this) = false;
97 PHYS_INPUT_BUTTON_JUMP(this) = false;
98 PHYS_INPUT_BUTTON_ATCK2(this) = false;
99 PHYS_INPUT_BUTTON_ZOOM(this) = false;
100 PHYS_INPUT_BUTTON_CROUCH(this) = false;
101 PHYS_INPUT_BUTTON_HOOK(this) = false;
102 PHYS_INPUT_BUTTON_INFO(this) = false;
103 PHYS_INPUT_BUTTON_DRAG(this) = false;
104 PHYS_INPUT_BUTTON_CHAT(this) = false;
105 PHYS_INPUT_BUTTON_USE(this) = false;
107 if (time < game_starttime)
109 // block the bot during the countdown to game start
110 this.movement = '0 0 0';
111 this.bot_nextthink = game_starttime;
115 // if dead, just wait until we can respawn
118 if (this.deadflag == DEAD_DEAD)
120 PHYS_INPUT_BUTTON_JUMP(this) = true; // press jump to respawn
121 this.bot_strategytime = 0;
124 else if(this.aistatus & AI_STATUS_STUCK)
125 navigation_unstuck(this);
127 // now call the current bot AI (havocbot for example)
131 void bot_setnameandstuff(entity this)
134 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");
171 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
172 if(s == it.cleanname)
178 RandomSelection_Add(NULL, 0, readfile, 1, prio);
180 readfile = RandomSelection_chosen_string;
184 tokens = tokenizebyseparator(readfile, "\t");
185 if(argv(0) != "") bot_name = argv(0);
186 else bot_name = "Bot";
188 if(argv(1) != "") bot_model = argv(1);
191 if(argv(2) != "") bot_skin = argv(2);
194 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
195 else bot_shirt = ftos(floor(random() * 15));
197 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
198 else bot_pants = ftos(floor(random() * 15));
200 this.bot_forced_team = stof(argv(5));
204 #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
205 //print(bot_name, ": ping=", argv(9), "\n");
207 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
208 READSKILL(bot_moveskill, 2, 0); // move skill
209 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
211 READSKILL(bot_pingskill, 0.5, 0); // ping skill
213 READSKILL(bot_weaponskill, 2, 0); // weapon skill
214 READSKILL(bot_aggresskill, 1, 0); // aggre skill
215 READSKILL(bot_rangepreference, 1, 0); // read skill
217 READSKILL(bot_aimskill, 2, 0); // aim skill
218 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
219 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
221 READSKILL(bot_thinkskill, 1, 0.5); // think skill
222 READSKILL(bot_aiskill, 2, 0); // "ai" skill
224 this.bot_config_loaded = true;
226 // this is really only a default, JoinBestTeam is called later
227 setcolor(this, stof(bot_shirt) * 16 + stof(bot_pants));
228 this.bot_preferredcolors = this.clientcolors;
231 if (autocvar_bot_usemodelnames)
236 // number bots with identical names
238 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
239 if(it.cleanname == name)
243 this.netname = this.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
245 this.netname = this.netname_freeme = strzone(strcat(prefix, name, suffix));
247 this.cleanname = strzone(name);
249 // pick the model and skin
250 if(substring(bot_model, -4, 1) != ".")
251 bot_model = strcat(bot_model, ".iqm");
252 this.playermodel = this.playermodel_freeme = strzone(strcat("models/player/", bot_model));
253 this.playerskin = this.playerskin_freeme = strzone(bot_skin);
255 this.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the NULL
256 this.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
259 void bot_custom_weapon_priority_setup()
263 bot_custom_weapon = false;
265 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
266 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
267 autocvar_bot_ai_custom_weapon_priority_close == "" ||
268 autocvar_bot_ai_custom_weapon_priority_distances == ""
273 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
278 bot_distance_far = stof(argv(0));
279 bot_distance_close = stof(argv(1));
281 if(bot_distance_far < bot_distance_close){
282 bot_distance_far = stof(argv(1));
283 bot_distance_close = stof(argv(0));
286 // Initialize list of weapons
287 bot_weapons_far[0] = -1;
288 bot_weapons_mid[0] = -1;
289 bot_weapons_close[0] = -1;
291 // Parse far distance weapon priorities
292 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
295 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
297 if ( w >= WEP_FIRST && w <= WEP_LAST) {
298 bot_weapons_far[c] = w;
302 if(c < Weapons_COUNT)
303 bot_weapons_far[c] = -1;
305 // Parse mid distance weapon priorities
306 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
309 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
311 if ( w >= WEP_FIRST && w <= WEP_LAST) {
312 bot_weapons_mid[c] = w;
316 if(c < Weapons_COUNT)
317 bot_weapons_mid[c] = -1;
319 // Parse close distance weapon priorities
320 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
323 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
325 if ( w >= WEP_FIRST && w <= WEP_LAST) {
326 bot_weapons_close[c] = w;
330 if(c < Weapons_COUNT)
331 bot_weapons_close[c] = -1;
333 bot_custom_weapon = true;
339 //dprint("bot_endgame\n");
343 setcolor(e, e.bot_preferredcolors);
346 // if dynamic waypoints are ever implemented, save them here
349 void bot_relinkplayerlist()
355 entity prevbot = NULL;
360 if(IS_BOT_CLIENT(it))
363 prevbot.nextbot = it;
367 bot_list.nextbot = NULL;
373 LOG_TRACE("relink: ", ftos(currentbots), " bots seen.");
374 bot_strategytoken = bot_list;
375 bot_strategytoken_taken = true;
378 void bot_clientdisconnect(entity this)
380 if (!IS_BOT_CLIENT(this))
382 bot_clearqueue(this);
384 strunzone(this.cleanname);
385 if(this.netname_freeme)
386 strunzone(this.netname_freeme);
387 if(this.playermodel_freeme)
388 strunzone(this.playermodel_freeme);
389 if(this.playerskin_freeme)
390 strunzone(this.playerskin_freeme);
391 this.cleanname = string_null;
392 this.netname_freeme = string_null;
393 this.playermodel_freeme = string_null;
394 this.playerskin_freeme = string_null;
395 if(this.bot_cmd_current)
396 delete(this.bot_cmd_current);
397 if(bot_waypoint_queue_owner==this)
398 bot_waypoint_queue_owner = NULL;
401 void bot_clientconnect(entity this)
403 if (!IS_BOT_CLIENT(this)) return;
404 this.bot_preferredcolors = this.clientcolors;
405 this.bot_nextthink = time - random();
406 this.lag_func = bot_lagfunc;
408 this.createdtime = this.bot_nextthink;
410 if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
411 bot_setnameandstuff(this);
413 if(this.bot_forced_team==1)
414 this.team = NUM_TEAM_1;
415 else if(this.bot_forced_team==2)
416 this.team = NUM_TEAM_2;
417 else if(this.bot_forced_team==3)
418 this.team = NUM_TEAM_3;
419 else if(this.bot_forced_team==4)
420 this.team = NUM_TEAM_4;
422 JoinBestTeam(this, false, true);
424 havocbot_setupbot(this);
427 void bot_removefromlargestteam()
429 CheckAllowedTeams(NULL);
437 FOREACH_ENTITY_FLOAT(isbot, true,
444 besttime = it.createdtime;
451 case NUM_TEAM_1: thiscount = c1; break;
452 case NUM_TEAM_2: thiscount = c2; break;
453 case NUM_TEAM_3: thiscount = c3; break;
454 case NUM_TEAM_4: thiscount = c4; break;
457 if(thiscount > bestcount)
459 bestcount = thiscount;
460 besttime = it.createdtime;
463 else if(thiscount == bestcount && besttime < it.createdtime)
465 besttime = it.createdtime;
470 return; // no bots to remove
471 currentbots = currentbots - 1;
475 void bot_removenewest()
479 bot_removefromlargestteam();
487 FOREACH_ENTITY_FLOAT(isbot, true,
494 besttime = it.createdtime;
497 if(besttime < it.createdtime)
499 besttime = it.createdtime;
505 return; // no bots to remove
507 currentbots = currentbots - 1;
511 void autoskill(float factor)
518 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
519 if(IS_REAL_CLIENT(it))
520 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
522 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
525 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
526 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
527 if(bestbot < 0 || bestplayer < 0)
529 LOG_TRACE("not doing anything");
530 // don't return, let it reset all counters below
532 else if(bestbot <= bestplayer * factor - 2)
534 if(autocvar_skill < 17)
536 LOG_TRACE("2 frags difference, increasing skill");
537 cvar_set("skill", ftos(autocvar_skill + 1));
538 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
541 else if(bestbot >= bestplayer * factor + 2)
543 if(autocvar_skill > 0)
545 LOG_TRACE("2 frags difference, decreasing skill");
546 cvar_set("skill", ftos(autocvar_skill - 1));
547 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
552 LOG_TRACE("not doing anything");
554 // don't reset counters, wait for them to accumulate
557 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
560 void bot_calculate_stepheightvec()
562 stepheightvec = autocvar_sv_stepheight * '0 0 1';
563 jumpstepheightvec = stepheightvec +
564 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
565 // 0.75 factor is for safety to make the jumps easy
570 int activerealplayers = 0;
572 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
573 activerealplayers = M_ARGV(0, int);
574 realplayers = M_ARGV(1, int);
576 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
584 // add/remove bots if needed to make sure there are at least
585 // minplayers+bot_number, or remove all bots if no one is playing
586 // But don't remove bots immediately on level change, as the real players
587 // usually haven't rejoined yet
588 bots_would_leave = false;
589 if (teamplay && autocvar_bot_vs_human && AvailableTeams() == 2)
590 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
591 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
593 float realminplayers, minplayers;
594 realminplayers = autocvar_minplayers;
595 minplayers = max(0, floor(realminplayers));
597 float realminbots, minbots;
598 realminbots = autocvar_bot_number;
599 minbots = max(0, floor(realminbots));
601 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
603 bots_would_leave = true;
607 // if there are no players, remove bots
611 // only add one bot per frame to avoid utter chaos
612 if(time > botframe_nextthink)
614 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
615 while (currentbots < bots)
617 if (bot_spawn() == NULL)
619 bprint("Can not add bot, server full.\n");
623 while (currentbots > bots)
630 void bot_serverframe()
632 if (intermission_running)
638 bot_calculate_stepheightvec();
639 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
641 if(time > autoskill_nextthink)
644 a = autocvar_skill_auto;
647 autoskill_nextthink = time + 5;
650 if(time > botframe_nextthink)
653 botframe_nextthink = time + 10;
656 bot_ignore_bots = autocvar_bot_ignore_bots;
658 if(botframe_spawnedwaypoints)
660 if(autocvar_waypoint_benchmark)
664 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
665 if (botframe_spawnedwaypoints)
667 if(botframe_cachedwaypointlinks)
669 if(!botframe_loadedforcedlinks)
670 waypoint_load_links_hardwired();
674 // TODO: Make this check cleaner
675 IL_EACH(g_waypoints, time - it.nextthink > 10,
677 waypoint_save_links();
684 botframe_spawnedwaypoints = true;
686 if(!waypoint_load_links())
687 waypoint_schedulerelinkall();
692 // cycle the goal token from one bot to the next each frame
693 // (this prevents them from all doing spawnfunc_waypoint searches on the same
694 // frame, which causes choppy framerates)
695 if (bot_strategytoken_taken)
697 bot_strategytoken_taken = false;
698 if (bot_strategytoken)
699 bot_strategytoken = bot_strategytoken.nextbot;
700 if (!bot_strategytoken)
701 bot_strategytoken = bot_list;
704 if (botframe_nextdangertime < time)
707 interval = autocvar_bot_ai_dangerdetectioninterval;
708 if (botframe_nextdangertime < time - interval * 1.5)
709 botframe_nextdangertime = time;
710 botframe_nextdangertime = botframe_nextdangertime + interval;
711 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
715 if (autocvar_g_waypointeditor)
716 botframe_showwaypointlinks();
718 if (autocvar_g_waypointeditor_auto)
719 botframe_autowaypoints();
721 if(time > bot_cvar_nextthink)
724 bot_custom_weapon_priority_setup();
725 bot_cvar_nextthink = time + 5;