4 #include "navigation.qh"
5 #include "scripting.qh"
6 #include "waypoints.qh"
8 #include "havocbot/havocbot.qh"
9 #include "havocbot/scripting.qh"
11 #include "../teamplay.qh"
13 #include "../antilag.qh"
14 #include "../autocvars.qh"
15 #include "../campaign.qh"
16 #include "../cl_client.qh"
17 #include "../constants.qh"
20 #include <common/t_items.qh>
22 #include "../mutators/all.qh"
24 #include "../weapons/accuracy.qh"
26 #include <common/physics/player.qh>
27 #include <common/constants.qh>
28 #include <common/mapinfo.qh>
29 #include <common/teams.qh>
30 #include <common/util.qh>
32 #include <common/weapons/all.qh>
34 #include <lib/csqcmodel/sv_model.qh>
36 #include <lib/warpzone/common.qh>
37 #include <lib/warpzone/util_server.qh>
41 entity bot = spawnclient();
44 currentbots = currentbots + 1;
45 bot_setnameandstuff(bot);
46 WITHSELF(bot, ClientConnect());
47 WITHSELF(bot, PutClientInServer());
52 void bot_think(entity this)
54 if (this.bot_nextthink > time)
57 this.flags &= ~FL_GODMODE;
59 this.flags |= FL_GODMODE;
61 this.bot_nextthink = this.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, this.bot_aiskill);
62 //if (this.bot_painintensity > 0)
63 // this.bot_painintensity = this.bot_painintensity - (skill + 1) * 40 * frametime;
65 //this.bot_painintensity = this.bot_painintensity + this.bot_oldhealth - this.health;
66 //this.bot_painintensity = bound(0, this.bot_painintensity, 100);
68 if (!IS_PLAYER(this) || (autocvar_g_campaign && !campaign_bots_may_start))
70 this.bot_nextthink = time + 0.5;
76 this.v_angle = this.angles;
78 this.fixangle = false;
83 this.dmg_inflictor = NULL;
85 // calculate an aiming latency based on the skill setting
86 // (simulated network latency + naturally delayed reflexes)
87 //this.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
88 // minimum ping 20+10 random
89 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
90 // skill 10 = ping 0.2 (adrenaline)
91 // skill 0 = ping 0.7 (slightly drunk)
94 PHYS_INPUT_BUTTON_ATCK(this) = false;
95 PHYS_INPUT_BUTTON_JUMP(this) = false;
96 PHYS_INPUT_BUTTON_ATCK2(this) = false;
97 PHYS_INPUT_BUTTON_ZOOM(this) = false;
98 PHYS_INPUT_BUTTON_CROUCH(this) = false;
99 PHYS_INPUT_BUTTON_HOOK(this) = false;
100 PHYS_INPUT_BUTTON_INFO(this) = false;
101 PHYS_INPUT_BUTTON_DRAG(this) = false;
102 PHYS_INPUT_BUTTON_CHAT(this) = false;
103 PHYS_INPUT_BUTTON_USE(this) = false;
105 if (time < game_starttime)
107 // block the bot during the countdown to game start
108 this.movement = '0 0 0';
109 this.bot_nextthink = game_starttime;
113 // if dead, just wait until we can respawn
116 if (this.deadflag == DEAD_DEAD)
118 PHYS_INPUT_BUTTON_JUMP(this) = true; // press jump to respawn
119 this.bot_strategytime = 0;
122 else if(this.aistatus & AI_STATUS_STUCK)
123 navigation_unstuck(this);
125 // now call the current bot AI (havocbot for example)
129 void bot_setnameandstuff(entity this)
132 float file, tokens, prio;
134 string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
135 string name, prefix, suffix;
137 if(autocvar_g_campaign)
144 prefix = autocvar_bot_prefix;
145 suffix = autocvar_bot_suffix;
148 file = fopen(autocvar_bot_config_file, FILE_READ);
152 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
157 RandomSelection_Init();
158 while((readfile = fgets(file)))
160 if(substring(readfile, 0, 2) == "//")
162 if(substring(readfile, 0, 1) == "#")
164 tokens = tokenizebyseparator(readfile, "\t");
169 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
170 if(s == it.cleanname)
176 RandomSelection_Add(NULL, 0, readfile, 1, prio);
178 readfile = RandomSelection_chosen_string;
182 tokens = tokenizebyseparator(readfile, "\t");
183 if(argv(0) != "") bot_name = argv(0);
184 else bot_name = "Bot";
186 if(argv(1) != "") bot_model = argv(1);
189 if(argv(2) != "") bot_skin = argv(2);
192 if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
193 else bot_shirt = ftos(floor(random() * 15));
195 if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
196 else bot_pants = ftos(floor(random() * 15));
198 this.bot_forced_team = stof(argv(5));
202 #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
203 //print(bot_name, ": ping=", argv(9), "\n");
205 READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
206 READSKILL(bot_moveskill, 2, 0); // move skill
207 READSKILL(bot_dodgeskill, 2, 0); // dodge skill
209 READSKILL(bot_pingskill, 0.5, 0); // ping skill
211 READSKILL(bot_weaponskill, 2, 0); // weapon skill
212 READSKILL(bot_aggresskill, 1, 0); // aggre skill
213 READSKILL(bot_rangepreference, 1, 0); // read skill
215 READSKILL(bot_aimskill, 2, 0); // aim skill
216 READSKILL(bot_offsetskill, 2, 0.5); // offset skill
217 READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
219 READSKILL(bot_thinkskill, 1, 0.5); // think skill
220 READSKILL(bot_aiskill, 2, 0); // "ai" skill
222 this.bot_config_loaded = true;
224 // this is really only a default, JoinBestTeam is called later
225 setcolor(this, stof(bot_shirt) * 16 + stof(bot_pants));
226 this.bot_preferredcolors = this.clientcolors;
229 if (autocvar_bot_usemodelnames)
234 // number bots with identical names
236 FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
237 if(it.cleanname == name)
241 this.netname = this.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
243 this.netname = this.netname_freeme = strzone(strcat(prefix, name, suffix));
245 this.cleanname = strzone(name);
247 // pick the model and skin
248 if(substring(bot_model, -4, 1) != ".")
249 bot_model = strcat(bot_model, ".iqm");
250 this.playermodel = this.playermodel_freeme = strzone(strcat("models/player/", bot_model));
251 this.playerskin = this.playerskin_freeme = strzone(bot_skin);
253 this.cvar_cl_accuracy_data_share = 1; // share the bots weapon accuracy data with the NULL
254 this.cvar_cl_accuracy_data_receive = 0; // don't receive any weapon accuracy data
257 void bot_custom_weapon_priority_setup()
261 bot_custom_weapon = false;
263 if( autocvar_bot_ai_custom_weapon_priority_far == "" ||
264 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
265 autocvar_bot_ai_custom_weapon_priority_close == "" ||
266 autocvar_bot_ai_custom_weapon_priority_distances == ""
271 tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
276 bot_distance_far = stof(argv(0));
277 bot_distance_close = stof(argv(1));
279 if(bot_distance_far < bot_distance_close){
280 bot_distance_far = stof(argv(1));
281 bot_distance_close = stof(argv(0));
284 // Initialize list of weapons
285 bot_weapons_far[0] = -1;
286 bot_weapons_mid[0] = -1;
287 bot_weapons_close[0] = -1;
289 // Parse far distance weapon priorities
290 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
293 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
295 if ( w >= WEP_FIRST && w <= WEP_LAST) {
296 bot_weapons_far[c] = w;
300 if(c < Weapons_COUNT)
301 bot_weapons_far[c] = -1;
303 // Parse mid distance weapon priorities
304 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
307 for(i=0; i < tokens && c < Weapons_COUNT; ++i){
309 if ( w >= WEP_FIRST && w <= WEP_LAST) {
310 bot_weapons_mid[c] = w;
314 if(c < Weapons_COUNT)
315 bot_weapons_mid[c] = -1;
317 // Parse close distance weapon priorities
318 tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
321 for(i=0; i < tokens && i < Weapons_COUNT; ++i){
323 if ( w >= WEP_FIRST && w <= WEP_LAST) {
324 bot_weapons_close[c] = w;
328 if(c < Weapons_COUNT)
329 bot_weapons_close[c] = -1;
331 bot_custom_weapon = true;
337 //dprint("bot_endgame\n");
341 setcolor(e, e.bot_preferredcolors);
344 // if dynamic waypoints are ever implemented, save them here
347 void bot_relinkplayerlist()
353 player_list = e = findchainflags(flags, FL_CLIENT);
358 player_count = player_count + 1;
359 e.nextplayer = e.chain;
360 if (IS_BOT_CLIENT(e))
367 bot_list.nextbot = NULL;
370 currentbots = currentbots + 1;
374 LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
375 bot_strategytoken = bot_list;
376 bot_strategytoken_taken = true;
379 void bot_clientdisconnect(entity this)
381 if (!IS_BOT_CLIENT(this))
383 bot_clearqueue(this);
385 strunzone(this.cleanname);
386 if(this.netname_freeme)
387 strunzone(this.netname_freeme);
388 if(this.playermodel_freeme)
389 strunzone(this.playermodel_freeme);
390 if(this.playerskin_freeme)
391 strunzone(this.playerskin_freeme);
392 this.cleanname = string_null;
393 this.netname_freeme = string_null;
394 this.playermodel_freeme = string_null;
395 this.playerskin_freeme = string_null;
396 if(this.bot_cmd_current)
397 remove(this.bot_cmd_current);
398 if(bot_waypoint_queue_owner==this)
399 bot_waypoint_queue_owner = NULL;
402 void bot_clientconnect(entity this)
404 if (!IS_BOT_CLIENT(this)) return;
405 this.bot_preferredcolors = this.clientcolors;
406 this.bot_nextthink = time - random();
407 this.lag_func = bot_lagfunc;
409 this.createdtime = this.bot_nextthink;
411 if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
412 bot_setnameandstuff(this);
414 if(this.bot_forced_team==1)
415 this.team = NUM_TEAM_1;
416 else if(this.bot_forced_team==2)
417 this.team = NUM_TEAM_2;
418 else if(this.bot_forced_team==3)
419 this.team = NUM_TEAM_3;
420 else if(this.bot_forced_team==4)
421 this.team = NUM_TEAM_4;
423 JoinBestTeam(this, false, true);
425 havocbot_setupbot(this);
428 void bot_removefromlargestteam()
430 float besttime, bestcount, thiscount;
432 CheckAllowedTeams(NULL);
434 head = findchainfloat(isbot, true);
438 besttime = head.createdtime;
442 if(head.team == NUM_TEAM_1)
444 else if(head.team == NUM_TEAM_2)
446 else if(head.team == NUM_TEAM_3)
448 else if(head.team == NUM_TEAM_4)
452 if (thiscount > bestcount)
454 bestcount = thiscount;
455 besttime = head.createdtime;
458 else if (thiscount == bestcount && besttime < head.createdtime)
460 besttime = head.createdtime;
465 currentbots = currentbots - 1;
469 void bot_removenewest()
476 bot_removefromlargestteam();
480 head = findchainfloat(isbot, true);
484 besttime = head.createdtime;
487 if (besttime < head.createdtime)
489 besttime = head.createdtime;
494 currentbots = currentbots - 1;
498 void autoskill(float factor)
505 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
506 if(IS_REAL_CLIENT(it))
507 bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
509 bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
512 LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
513 LOG_TRACE("best bot got ", ftos(bestbot), "; ");
514 if(bestbot < 0 || bestplayer < 0)
516 LOG_TRACE("not doing anything\n");
517 // don't return, let it reset all counters below
519 else if(bestbot <= bestplayer * factor - 2)
521 if(autocvar_skill < 17)
523 LOG_TRACE("2 frags difference, increasing skill\n");
524 cvar_set("skill", ftos(autocvar_skill + 1));
525 bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
528 else if(bestbot >= bestplayer * factor + 2)
530 if(autocvar_skill > 0)
532 LOG_TRACE("2 frags difference, decreasing skill\n");
533 cvar_set("skill", ftos(autocvar_skill - 1));
534 bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
539 LOG_TRACE("not doing anything\n");
541 // don't reset counters, wait for them to accumulate
544 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
547 void bot_calculate_stepheightvec()
549 stepheightvec = autocvar_sv_stepheight * '0 0 1';
550 jumpstepheightvec = stepheightvec +
551 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
552 // 0.75 factor is for safety to make the jumps easy
557 int activerealplayers = 0;
559 if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
560 activerealplayers = M_ARGV(0, int);
561 realplayers = M_ARGV(1, int);
563 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
571 // add/remove bots if needed to make sure there are at least
572 // minplayers+bot_number, or remove all bots if no one is playing
573 // But don't remove bots immediately on level change, as the real players
574 // usually haven't rejoined yet
575 bots_would_leave = false;
576 if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
577 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
578 else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
580 float realminplayers, minplayers;
581 realminplayers = autocvar_minplayers;
582 minplayers = max(0, floor(realminplayers));
584 float realminbots, minbots;
585 realminbots = autocvar_bot_number;
586 minbots = max(0, floor(realminbots));
588 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
590 bots_would_leave = true;
594 // if there are no players, remove bots
598 // only add one bot per frame to avoid utter chaos
599 if(time > botframe_nextthink)
601 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
602 while (currentbots < bots)
604 if (bot_spawn() == NULL)
606 bprint("Can not add bot, server full.\n");
610 while (currentbots > bots)
617 void bot_serverframe()
619 if (intermission_running)
625 bot_calculate_stepheightvec();
626 bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
628 if(time > autoskill_nextthink)
631 a = autocvar_skill_auto;
634 autoskill_nextthink = time + 5;
637 if(time > botframe_nextthink)
640 botframe_nextthink = time + 10;
643 bot_ignore_bots = autocvar_bot_ignore_bots;
645 if(botframe_spawnedwaypoints)
647 if(autocvar_waypoint_benchmark)
651 if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
652 if (botframe_spawnedwaypoints)
654 if(botframe_cachedwaypointlinks)
656 if(!botframe_loadedforcedlinks)
657 waypoint_load_links_hardwired();
661 // TODO: Make this check cleaner
662 entity wp = findchain(classname, "waypoint");
663 if(time - wp.nextthink > 10)
664 waypoint_save_links();
669 botframe_spawnedwaypoints = true;
671 if(!waypoint_load_links())
672 waypoint_schedulerelinkall();
677 // cycle the goal token from one bot to the next each frame
678 // (this prevents them from all doing spawnfunc_waypoint searches on the same
679 // frame, which causes choppy framerates)
680 if (bot_strategytoken_taken)
682 bot_strategytoken_taken = false;
683 if (bot_strategytoken)
684 bot_strategytoken = bot_strategytoken.nextbot;
685 if (!bot_strategytoken)
686 bot_strategytoken = bot_list;
689 if (botframe_nextdangertime < time)
692 interval = autocvar_bot_ai_dangerdetectioninterval;
693 if (botframe_nextdangertime < time - interval * 1.5)
694 botframe_nextdangertime = time;
695 botframe_nextdangertime = botframe_nextdangertime + interval;
696 botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
700 if (autocvar_g_waypointeditor)
701 botframe_showwaypointlinks();
703 if (autocvar_g_waypointeditor_auto)
704 botframe_autowaypoints();
706 if(time > bot_cvar_nextthink)
709 bot_custom_weapon_priority_setup();
710 bot_cvar_nextthink = time + 5;