]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/bot.qc
Merge branch 'master' into terencehill/translate_colors_2
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / bot.qc
1 #include "bot.qh"
2
3 #include "aim.qh"
4 #include "navigation.qh"
5 #include "scripting.qh"
6 #include "waypoints.qh"
7
8 #include "havocbot/havocbot.qh"
9 #include "havocbot/scripting.qh"
10
11 #include "../teamplay.qh"
12
13 #include "../antilag.qh"
14 #include "../autocvars.qh"
15 #include "../campaign.qh"
16 #include "../cl_client.qh"
17 #include "../constants.qh"
18 #include "../defs.qh"
19 #include "../race.qh"
20 #include <common/t_items.qh>
21
22 #include "../mutators/all.qh"
23
24 #include "../weapons/accuracy.qh"
25
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>
31
32 #include <common/weapons/all.qh>
33
34 #include <lib/csqcmodel/sv_model.qh>
35
36 #include <lib/warpzone/common.qh>
37 #include <lib/warpzone/util_server.qh>
38
39 entity bot_spawn()
40 {
41         entity bot = spawnclient();
42         if (bot)
43         {
44                 currentbots = currentbots + 1;
45                 bot_setnameandstuff(bot);
46                 WITHSELF(bot, ClientConnect());
47                 WITHSELF(bot, PutClientInServer());
48         }
49         return bot;
50 }
51
52 void bot_think(entity this)
53 {
54         if (this.bot_nextthink > time)
55                 return;
56
57         this.flags &= ~FL_GODMODE;
58         if(autocvar_bot_god)
59                 this.flags |= FL_GODMODE;
60
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;
64
65         //this.bot_painintensity = this.bot_painintensity + this.bot_oldhealth - this.health;
66         //this.bot_painintensity = bound(0, this.bot_painintensity, 100);
67
68         if (!IS_PLAYER(this) || (autocvar_g_campaign && !campaign_bots_may_start))
69         {
70                 this.bot_nextthink = time + 0.5;
71                 return;
72         }
73
74         if (this.fixangle)
75         {
76                 this.v_angle = this.angles;
77                 this.v_angle_z = 0;
78                 this.fixangle = false;
79         }
80
81         this.dmg_take = 0;
82         this.dmg_save = 0;
83         this.dmg_inflictor = world;
84
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)
92
93         // clear buttons
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;
104
105         if (time < game_starttime)
106         {
107                 // block the bot during the countdown to game start
108                 this.movement = '0 0 0';
109                 this.bot_nextthink = game_starttime;
110                 return;
111         }
112
113         // if dead, just wait until we can respawn
114         if (IS_DEAD(this))
115         {
116                 if (this.deadflag == DEAD_DEAD)
117                 {
118                         PHYS_INPUT_BUTTON_JUMP(this) = true; // press jump to respawn
119                         this.bot_strategytime = 0;
120                 }
121         }
122         else if(this.aistatus & AI_STATUS_STUCK)
123                 navigation_unstuck(this);
124
125         // now call the current bot AI (havocbot for example)
126         this.bot_ai(this);
127 }
128
129 void bot_setnameandstuff(entity this)
130 {
131         string readfile, s;
132         float file, tokens, prio;
133
134         string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
135         string name, prefix, suffix;
136
137         if(autocvar_g_campaign)
138         {
139                 prefix = "";
140                 suffix = "";
141         }
142         else
143         {
144                 prefix = autocvar_bot_prefix;
145                 suffix = autocvar_bot_suffix;
146         }
147
148         file = fopen(autocvar_bot_config_file, FILE_READ);
149
150         if(file < 0)
151         {
152                 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
153                 readfile = "";
154         }
155         else
156         {
157                 RandomSelection_Init();
158                 while((readfile = fgets(file)))
159                 {
160                         if(substring(readfile, 0, 2) == "//")
161                                 continue;
162                         if(substring(readfile, 0, 1) == "#")
163                                 continue;
164                         tokens = tokenizebyseparator(readfile, "\t");
165                         if(tokens == 0)
166                                 continue;
167                         s = argv(0);
168                         prio = 1;
169                         FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
170                                 if(s == it.cleanname)
171                                 {
172                                         prio = 0;
173                                         break;
174                                 }
175                         ));
176                         RandomSelection_Add(world, 0, readfile, 1, prio);
177                 }
178                 readfile = RandomSelection_chosen_string;
179                 fclose(file);
180         }
181
182         tokens = tokenizebyseparator(readfile, "\t");
183         if(argv(0) != "") bot_name = argv(0);
184         else bot_name = "Bot";
185
186         if(argv(1) != "") bot_model = argv(1);
187         else bot_model = "";
188
189         if(argv(2) != "") bot_skin = argv(2);
190         else bot_skin = "0";
191
192         if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
193         else bot_shirt = ftos(floor(random() * 15));
194
195         if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
196         else bot_pants = ftos(floor(random() * 15));
197
198         this.bot_forced_team = stof(argv(5));
199
200         prio = 6;
201
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");
204
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
208
209         READSKILL(bot_pingskill, 0.5, 0); // ping skill
210
211         READSKILL(bot_weaponskill, 2, 0); // weapon skill
212         READSKILL(bot_aggresskill, 1, 0); // aggre skill
213         READSKILL(bot_rangepreference, 1, 0); // read skill
214
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
218
219         READSKILL(bot_thinkskill, 1, 0.5); // think skill
220         READSKILL(bot_aiskill, 2, 0); // "ai" skill
221
222         this.bot_config_loaded = true;
223
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;
227
228         // pick the name
229         if (autocvar_bot_usemodelnames)
230                 name = bot_model;
231         else
232                 name = bot_name;
233
234         // number bots with identical names
235         int j = 0;
236         FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
237                 if(it.cleanname == name)
238                         ++j;
239         ));
240         if (j)
241                 this.netname = this.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
242         else
243                 this.netname = this.netname_freeme = strzone(strcat(prefix, name, suffix));
244
245         this.cleanname = strzone(name);
246
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);
252
253         this.cvar_cl_accuracy_data_share = 1;  // share the bots weapon accuracy data with the world
254         this.cvar_cl_accuracy_data_receive = 0;  // don't receive any weapon accuracy data
255 }
256
257 void bot_custom_weapon_priority_setup()
258 {
259         float tokens, i, w;
260
261         bot_custom_weapon = false;
262
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 == ""
267         )
268                 return;
269
270         // Parse distances
271         tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
272
273         if (tokens!=2)
274                 return;
275
276         bot_distance_far = stof(argv(0));
277         bot_distance_close = stof(argv(1));
278
279         if(bot_distance_far < bot_distance_close){
280                 bot_distance_far = stof(argv(1));
281                 bot_distance_close = stof(argv(0));
282         }
283
284         // Initialize list of weapons
285         bot_weapons_far[0] = -1;
286         bot_weapons_mid[0] = -1;
287         bot_weapons_close[0] = -1;
288
289         // Parse far distance weapon priorities
290         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
291
292         int c = 0;
293         for(i=0; i < tokens && c < Weapons_COUNT; ++i){
294                 w = stof(argv(i));
295                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
296                         bot_weapons_far[c] = w;
297                         ++c;
298                 }
299         }
300         if(c < Weapons_COUNT)
301                 bot_weapons_far[c] = -1;
302
303         // Parse mid distance weapon priorities
304         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
305
306         c = 0;
307         for(i=0; i < tokens && c < Weapons_COUNT; ++i){
308                 w = stof(argv(i));
309                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
310                         bot_weapons_mid[c] = w;
311                         ++c;
312                 }
313         }
314         if(c < Weapons_COUNT)
315                 bot_weapons_mid[c] = -1;
316
317         // Parse close distance weapon priorities
318         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
319
320         c = 0;
321         for(i=0; i < tokens && i < Weapons_COUNT; ++i){
322                 w = stof(argv(i));
323                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
324                         bot_weapons_close[c] = w;
325                         ++c;
326                 }
327         }
328         if(c < Weapons_COUNT)
329                 bot_weapons_close[c] = -1;
330
331         bot_custom_weapon = true;
332 }
333
334 void bot_endgame()
335 {
336         entity e;
337         //dprint("bot_endgame\n");
338         e = bot_list;
339         while (e)
340         {
341                 setcolor(e, e.bot_preferredcolors);
342                 e = e.nextbot;
343         }
344         // if dynamic waypoints are ever implemented, save them here
345 }
346
347 void bot_relinkplayerlist()
348 {
349         entity e;
350         entity prevbot;
351         player_count = 0;
352         currentbots = 0;
353         player_list = e = findchainflags(flags, FL_CLIENT);
354         bot_list = world;
355         prevbot = world;
356         while (e)
357         {
358                 player_count = player_count + 1;
359                 e.nextplayer = e.chain;
360                 if (IS_BOT_CLIENT(e))
361                 {
362                         if (prevbot)
363                                 prevbot.nextbot = e;
364                         else
365                         {
366                                 bot_list = e;
367                                 bot_list.nextbot = world;
368                         }
369                         prevbot = e;
370                         currentbots = currentbots + 1;
371                 }
372                 e = e.chain;
373         }
374         LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
375         bot_strategytoken = bot_list;
376         bot_strategytoken_taken = true;
377 }
378
379 void bot_clientdisconnect(entity this)
380 {
381         if (!IS_BOT_CLIENT(this))
382                 return;
383         bot_clearqueue(this);
384         if(this.cleanname)
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 = world;
400 }
401
402 void bot_clientconnect(entity this)
403 {
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;
408         this.isbot = true;
409         this.createdtime = this.bot_nextthink;
410
411         if(!this.bot_config_loaded) // This is needed so team overrider doesn't break between matches
412                 bot_setnameandstuff(this);
413
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;
422         else
423                 JoinBestTeam(this, false, true);
424
425         havocbot_setupbot(this);
426 }
427
428 void bot_removefromlargestteam()
429 {
430         float besttime, bestcount, thiscount;
431         entity best, head;
432         CheckAllowedTeams(world);
433         GetTeamCounts(world);
434         head = findchainfloat(isbot, true);
435         if (!head)
436                 return;
437         best = head;
438         besttime = head.createdtime;
439         bestcount = 0;
440         while (head)
441         {
442                 if(head.team == NUM_TEAM_1)
443                         thiscount = c1;
444                 else if(head.team == NUM_TEAM_2)
445                         thiscount = c2;
446                 else if(head.team == NUM_TEAM_3)
447                         thiscount = c3;
448                 else if(head.team == NUM_TEAM_4)
449                         thiscount = c4;
450                 else
451                         thiscount = 0;
452                 if (thiscount > bestcount)
453                 {
454                         bestcount = thiscount;
455                         besttime = head.createdtime;
456                         best = head;
457                 }
458                 else if (thiscount == bestcount && besttime < head.createdtime)
459                 {
460                         besttime = head.createdtime;
461                         best = head;
462                 }
463                 head = head.chain;
464         }
465         currentbots = currentbots - 1;
466         dropclient(best);
467 }
468
469 void bot_removenewest()
470 {
471         float besttime;
472         entity best, head;
473
474         if(teamplay)
475         {
476                 bot_removefromlargestteam();
477                 return;
478         }
479
480         head = findchainfloat(isbot, true);
481         if (!head)
482                 return;
483         best = head;
484         besttime = head.createdtime;
485         while (head)
486         {
487                 if (besttime < head.createdtime)
488                 {
489                         besttime = head.createdtime;
490                         best = head;
491                 }
492                 head = head.chain;
493         }
494         currentbots = currentbots - 1;
495         dropclient(best);
496 }
497
498 void autoskill(float factor)
499 {
500         float bestbot;
501         float bestplayer;
502
503         bestbot = -1;
504         bestplayer = -1;
505         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
506                 if(IS_REAL_CLIENT(it))
507                         bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
508                 else
509                         bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
510         ));
511
512         LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
513         LOG_TRACE("best bot got ", ftos(bestbot), "; ");
514         if(bestbot < 0 || bestplayer < 0)
515         {
516                 LOG_TRACE("not doing anything\n");
517                 // don't return, let it reset all counters below
518         }
519         else if(bestbot <= bestplayer * factor - 2)
520         {
521                 if(autocvar_skill < 17)
522                 {
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");
526                 }
527         }
528         else if(bestbot >= bestplayer * factor + 2)
529         {
530                 if(autocvar_skill > 0)
531                 {
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");
535                 }
536         }
537         else
538         {
539                 LOG_TRACE("not doing anything\n");
540                 return;
541                 // don't reset counters, wait for them to accumulate
542         }
543
544         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
545 }
546
547 void bot_calculate_stepheightvec()
548 {
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
553 }
554
555 float bot_fixcount()
556 {
557         int activerealplayers = 0;
558         int realplayers = 0;
559         if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
560                 activerealplayers = bot_activerealplayers;
561                 realplayers = bot_realplayers;
562         } else {
563                 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
564                         if(IS_PLAYER(it))
565                                 ++activerealplayers;
566                         ++realplayers;
567                 ));
568         }
569
570         int bots;
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)))
579         {
580                 float realminplayers, minplayers;
581                 realminplayers = autocvar_minplayers;
582                 minplayers = max(0, floor(realminplayers));
583
584                 float realminbots, minbots;
585                 realminbots = autocvar_bot_number;
586                 minbots = max(0, floor(realminbots));
587
588                 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
589                 if(bots > minbots)
590                         bots_would_leave = true;
591         }
592         else
593         {
594                 // if there are no players, remove bots
595                 bots = 0;
596         }
597
598         // only add one bot per frame to avoid utter chaos
599         if(time > botframe_nextthink)
600         {
601                 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
602                 while (currentbots < bots)
603                 {
604                         if (bot_spawn() == world)
605                         {
606                                 bprint("Can not add bot, server full.\n");
607                                 return false;
608                         }
609                 }
610                 while (currentbots > bots)
611                         bot_removenewest();
612         }
613
614         return true;
615 }
616
617 void bot_serverframe()
618 {
619         if (intermission_running)
620                 return;
621
622         if (time < 2)
623                 return;
624
625         bot_calculate_stepheightvec();
626         bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
627
628         if(time > autoskill_nextthink)
629         {
630                 float a;
631                 a = autocvar_skill_auto;
632                 if(a)
633                         autoskill(a);
634                 autoskill_nextthink = time + 5;
635         }
636
637         if(time > botframe_nextthink)
638         {
639                 if(!bot_fixcount())
640                         botframe_nextthink = time + 10;
641         }
642
643         bot_ignore_bots = autocvar_bot_ignore_bots;
644
645         if(botframe_spawnedwaypoints)
646         {
647                 if(autocvar_waypoint_benchmark)
648                         localcmd("quit\n");
649         }
650
651         if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
652         if (botframe_spawnedwaypoints)
653         {
654                 if(botframe_cachedwaypointlinks)
655                 {
656                         if(!botframe_loadedforcedlinks)
657                                 waypoint_load_links_hardwired();
658                 }
659                 else
660                 {
661                         // TODO: Make this check cleaner
662                         entity wp = findchain(classname, "waypoint");
663                         if(time - wp.nextthink > 10)
664                                 waypoint_save_links();
665                 }
666         }
667         else
668         {
669                 botframe_spawnedwaypoints = true;
670                 waypoint_loadall();
671                 if(!waypoint_load_links())
672                         waypoint_schedulerelinkall();
673         }
674
675         if (bot_list)
676         {
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)
681                 {
682                         bot_strategytoken_taken = false;
683                         if (bot_strategytoken)
684                                 bot_strategytoken = bot_strategytoken.nextbot;
685                         if (!bot_strategytoken)
686                                 bot_strategytoken = bot_list;
687                 }
688
689                 if (botframe_nextdangertime < time)
690                 {
691                         float interval;
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);
697                 }
698         }
699
700         if (autocvar_g_waypointeditor)
701                 botframe_showwaypointlinks();
702
703         if (autocvar_g_waypointeditor_auto)
704                 botframe_autowaypoints();
705
706         if(time > bot_cvar_nextthink)
707         {
708                 if(currentbots>0)
709                         bot_custom_weapon_priority_setup();
710                 bot_cvar_nextthink = time + 5;
711         }
712 }