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 * (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;
383 prevbot.nextbot = NULL;
384 LOG_TRACE("relink: ", ftos(currentbots), " bots seen.");
385 bot_strategytoken = bot_list;
386 bot_strategytoken_taken = true;
389 void bot_clientdisconnect(entity this)
391 if (!IS_BOT_CLIENT(this))
393 bot_clearqueue(this);
395 strunzone(this.cleanname);
396 if(this.netname_freeme)
397 strunzone(this.netname_freeme);
398 if(this.playermodel_freeme)
399 strunzone(this.playermodel_freeme);
400 if(this.playerskin_freeme)
401 strunzone(this.playerskin_freeme);
402 this.cleanname = string_null;
403 this.netname_freeme = string_null;
404 this.playermodel_freeme = string_null;
405 this.playerskin_freeme = string_null;
406 if(this.bot_cmd_current)
407 delete(this.bot_cmd_current);
408 if(bot_waypoint_queue_owner == this)
409 bot_waypoint_queue_owner = NULL;
412 void bot_clientconnect(entity this)
414 if (!IS_BOT_CLIENT(this)) return;
415 this.bot_preferredcolors = this.clientcolors;
416 this.bot_nextthink = time - random();
417 this.lag_func = bot_lagfunc;
419 this.createdtime = this.bot_nextthink;
421 if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
422 bot_setnameandstuff(this);
424 if(this.bot_forced_team==1)
425 this.team = NUM_TEAM_1;
426 else if(this.bot_forced_team==2)
427 this.team = NUM_TEAM_2;
428 else if(this.bot_forced_team==3)
429 this.team = NUM_TEAM_3;
430 else if(this.bot_forced_team==4)
431 this.team = NUM_TEAM_4;
433 JoinBestTeam(this, false, true);
435 havocbot_setupbot(this);
438 void bot_removefromlargestteam()
440 CheckAllowedTeams(NULL);
448 FOREACH_CLIENT(it.isbot,
455 besttime = it.createdtime;
462 case NUM_TEAM_1: thiscount = c1; break;
463 case NUM_TEAM_2: thiscount = c2; break;
464 case NUM_TEAM_3: thiscount = c3; break;
465 case NUM_TEAM_4: thiscount = c4; break;
468 if(thiscount > bestcount)
470 bestcount = thiscount;
471 besttime = it.createdtime;
474 else if(thiscount == bestcount && besttime < it.createdtime)
476 besttime = it.createdtime;
481 return; // no bots to remove
482 currentbots = currentbots - 1;
486 void bot_removenewest()
490 bot_removefromlargestteam();
498 FOREACH_CLIENT(it.isbot,
505 besttime = it.createdtime;
508 if(besttime < it.createdtime)
510 besttime = it.createdtime;
516 return; // no bots to remove
518 currentbots = currentbots - 1;
522 void autoskill(float factor)
529 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
530 if(IS_REAL_CLIENT(it))
531 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
533 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
536 LOG_DEBUG("autoskill: best player got ", ftos(bestplayer), ", ");
537 LOG_DEBUG("best bot got ", ftos(bestbot), "; ");
538 if(bestbot < 0 || bestplayer < 0)
540 LOG_DEBUG("not doing anything");
541 // don't return, let it reset all counters below
543 else if(bestbot <= bestplayer * factor - 2)
545 if(autocvar_skill < 17)
547 LOG_DEBUG("2 frags difference, increasing skill");
548 cvar_set("skill", ftos(autocvar_skill + 1));
549 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
552 else if(bestbot >= bestplayer * factor + 2)
554 if(autocvar_skill > 0)
556 LOG_DEBUG("2 frags difference, decreasing skill");
557 cvar_set("skill", ftos(autocvar_skill - 1));
558 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
563 LOG_DEBUG("not doing anything");
565 // don't reset counters, wait for them to accumulate
568 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
571 void bot_calculate_stepheightvec()
573 stepheightvec = autocvar_sv_stepheight * '0 0 1';
574 jumpheight_vec = (autocvar_sv_jumpvelocity ** 2) / (2 * autocvar_sv_gravity) * '0 0 1';
575 jumpstepheightvec = stepheightvec + jumpheight_vec * 0.85; // reduce it a bit to make the jumps easy
580 int activerealplayers = 0;
582 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
583 activerealplayers = M_ARGV(0, int);
584 realplayers = M_ARGV(1, int);
586 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
594 // add/remove bots if needed to make sure there are at least
595 // minplayers+bot_number, or remove all bots if no one is playing
596 // But don't remove bots immediately on level change, as the real players
597 // usually haven't rejoined yet
598 bots_would_leave = false;
599 if (teamplay && autocvar_bot_vs_human && AvailableTeams() == 2)
600 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
601 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
603 float realminplayers, minplayers;
604 realminplayers = autocvar_minplayers;
605 minplayers = max(0, floor(realminplayers));
607 float realminbots, minbots;
608 realminbots = autocvar_bot_number;
609 minbots = max(0, floor(realminbots));
611 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
613 bots_would_leave = true;
617 // if there are no players, remove bots
621 // only add one bot per frame to avoid utter chaos
622 if(time > botframe_nextthink)
624 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
625 while (currentbots < bots)
627 if (bot_spawn() == NULL)
629 bprint("Can not add bot, server full.\n");
633 while (currentbots > bots)
640 void bot_remove_from_bot_list(entity this)
643 entity prev_bot = NULL;
649 bot_list = this.nextbot;
651 prev_bot.nextbot = this.nextbot;
652 if(bot_strategytoken == this)
654 bot_strategytoken = this.nextbot;
655 bot_strategytoken_taken = true;
665 void bot_clear(entity this)
667 bot_remove_from_bot_list(this);
668 if(bot_waypoint_queue_owner == this)
669 bot_waypoint_queue_owner = NULL;
670 this.aistatus &= ~AI_STATUS_STUCK; // otherwise bot_waypoint_queue_owner will be set again to this by navigation_unstuck
673 void bot_serverframe()
681 bot_calculate_stepheightvec();
682 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
684 if(time > autoskill_nextthink)
687 a = autocvar_skill_auto;
690 autoskill_nextthink = time + 5;
693 if(time > botframe_nextthink)
696 botframe_nextthink = time + 10;
699 bot_ignore_bots = autocvar_bot_ignore_bots;
701 if(botframe_spawnedwaypoints)
703 if(autocvar_waypoint_benchmark)
707 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
708 if (botframe_spawnedwaypoints)
710 if(botframe_cachedwaypointlinks)
712 if(!botframe_loadedforcedlinks)
713 waypoint_load_links_hardwired();
717 // TODO: Make this check cleaner
718 IL_EACH(g_waypoints, time - it.nextthink > 10,
720 waypoint_save_links();
727 botframe_spawnedwaypoints = true;
729 if(!waypoint_load_links())
730 waypoint_schedulerelinkall(); // link all the autogenerated waypoints (teleporters)
735 // cycle the goal token from one bot to the next each frame
736 // (this prevents them from all doing spawnfunc_waypoint searches on the same
737 // frame, which causes choppy framerates)
738 if (bot_strategytoken_taken)
740 bot_strategytoken_taken = false;
741 if (bot_strategytoken)
742 bot_strategytoken = bot_strategytoken.nextbot;
743 if (!bot_strategytoken)
744 bot_strategytoken = bot_list;
747 if (botframe_nextdangertime < time)
750 interval = autocvar_bot_ai_dangerdetectioninterval;
751 if (botframe_nextdangertime < time - interval * 1.5)
752 botframe_nextdangertime = time;
753 botframe_nextdangertime = botframe_nextdangertime + interval;
754 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
758 if (autocvar_g_waypointeditor)
759 botframe_showwaypointlinks();
761 if (autocvar_g_waypointeditor_auto)
762 botframe_autowaypoints();
764 if(time > bot_cvar_nextthink)
767 bot_custom_weapon_priority_setup();
768 bot_cvar_nextthink = time + 5;