]> 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/constants.qh>
27 #include <common/mapinfo.qh>
28 #include <common/teams.qh>
29 #include <common/util.qh>
30
31 #include <common/weapons/all.qh>
32
33 #include <lib/csqcmodel/sv_model.qh>
34
35 #include <lib/warpzone/common.qh>
36 #include <lib/warpzone/util_server.qh>
37
38 entity bot_spawn()
39 {SELFPARAM();
40         entity bot = spawnclient();
41         if (bot)
42         {
43                 currentbots = currentbots + 1;
44                 setself(bot);
45                 bot_setnameandstuff();
46                 ClientConnect();
47                 PutClientInServer();
48                 setself(this);
49         }
50         return bot;
51 }
52
53 void bot_think()
54 {SELFPARAM();
55         if (self.bot_nextthink > time)
56                 return;
57
58         self.flags &= ~FL_GODMODE;
59         if(autocvar_bot_god)
60                 self.flags |= FL_GODMODE;
61
62         self.bot_nextthink = self.bot_nextthink + autocvar_bot_ai_thinkinterval * pow(0.5, self.bot_aiskill);
63         //if (self.bot_painintensity > 0)
64         //      self.bot_painintensity = self.bot_painintensity - (skill + 1) * 40 * frametime;
65
66         //self.bot_painintensity = self.bot_painintensity + self.bot_oldhealth - self.health;
67         //self.bot_painintensity = bound(0, self.bot_painintensity, 100);
68
69         if (!IS_PLAYER(self) || (autocvar_g_campaign && !campaign_bots_may_start))
70         {
71                 self.bot_nextthink = time + 0.5;
72                 return;
73         }
74
75         if (self.fixangle)
76         {
77                 self.v_angle = self.angles;
78                 self.v_angle_z = 0;
79                 self.fixangle = false;
80         }
81
82         self.dmg_take = 0;
83         self.dmg_save = 0;
84         self.dmg_inflictor = world;
85
86         // calculate an aiming latency based on the skill setting
87         // (simulated network latency + naturally delayed reflexes)
88         //self.ping = 0.7 - bound(0, 0.05 * skill, 0.5); // moved the reflexes to bot_aimdir (under the name 'think')
89         // minimum ping 20+10 random
90         self.ping = bound(0,0.07 - bound(0, (skill + self.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
91         // skill 10 = ping 0.2 (adrenaline)
92         // skill 0 = ping 0.7 (slightly drunk)
93
94         // clear buttons
95         PHYS_INPUT_BUTTON_ATCK(self) = false;
96         PHYS_INPUT_BUTTON_JUMP(self) = false;
97         PHYS_INPUT_BUTTON_ATCK2(self) = false;
98         PHYS_INPUT_BUTTON_ZOOM(self) = false;
99         PHYS_INPUT_BUTTON_CROUCH(self) = false;
100         PHYS_INPUT_BUTTON_HOOK(self) = false;
101         PHYS_INPUT_BUTTON_INFO(self) = false;
102         PHYS_INPUT_BUTTON_DRAG(self) = false;
103         PHYS_INPUT_BUTTON_CHAT(self) = false;
104         PHYS_INPUT_BUTTON_USE(self) = false;
105
106         if (time < game_starttime)
107         {
108                 // block the bot during the countdown to game start
109                 self.movement = '0 0 0';
110                 self.bot_nextthink = game_starttime;
111                 return;
112         }
113
114         // if dead, just wait until we can respawn
115         if (IS_DEAD(self))
116         {
117                 if (self.deadflag == DEAD_DEAD)
118                 {
119                         PHYS_INPUT_BUTTON_JUMP(self) = true; // press jump to respawn
120                         self.bot_strategytime = 0;
121                 }
122         }
123         else if(self.aistatus & AI_STATUS_STUCK)
124                 navigation_unstuck();
125
126         // now call the current bot AI (havocbot for example)
127         self.bot_ai();
128 }
129
130 void bot_setnameandstuff()
131 {SELFPARAM();
132         string readfile, s;
133         float file, tokens, prio;
134
135         string bot_name, bot_model, bot_skin, bot_shirt, bot_pants;
136         string name, prefix, suffix;
137
138         if(autocvar_g_campaign)
139         {
140                 prefix = "";
141                 suffix = "";
142         }
143         else
144         {
145                 prefix = autocvar_bot_prefix;
146                 suffix = autocvar_bot_suffix;
147         }
148
149         file = fopen(autocvar_bot_config_file, FILE_READ);
150
151         if(file < 0)
152         {
153                 LOG_INFO(strcat("Error: Can not open the bot configuration file '",autocvar_bot_config_file,"'\n"));
154                 readfile = "";
155         }
156         else
157         {
158                 RandomSelection_Init();
159                 while((readfile = fgets(file)))
160                 {
161                         if(substring(readfile, 0, 2) == "//")
162                                 continue;
163                         if(substring(readfile, 0, 1) == "#")
164                                 continue;
165                         tokens = tokenizebyseparator(readfile, "\t");
166                         if(tokens == 0)
167                                 continue;
168                         s = argv(0);
169                         prio = 1;
170                         FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
171                                 if(s == it.cleanname)
172                                 {
173                                         prio = 0;
174                                         break;
175                                 }
176                         ));
177                         RandomSelection_Add(world, 0, readfile, 1, prio);
178                 }
179                 readfile = RandomSelection_chosen_string;
180                 fclose(file);
181         }
182
183         tokens = tokenizebyseparator(readfile, "\t");
184         if(argv(0) != "") bot_name = argv(0);
185         else bot_name = "Bot";
186
187         if(argv(1) != "") bot_model = argv(1);
188         else bot_model = "";
189
190         if(argv(2) != "") bot_skin = argv(2);
191         else bot_skin = "0";
192
193         if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
194         else bot_shirt = ftos(floor(random() * 15));
195
196         if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
197         else bot_pants = ftos(floor(random() * 15));
198
199         self.bot_forced_team = stof(argv(5));
200
201         prio = 6;
202
203         #define READSKILL(f,w,r) if(argv(prio) != "") self.f = stof(argv(prio)) * (w); else self.f = (!autocvar_g_campaign) * (2 * random() - 1) * (r) * (w); ++prio
204         //print(bot_name, ": ping=", argv(9), "\n");
205
206         READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
207         READSKILL(bot_moveskill, 2, 0); // move skill
208         READSKILL(bot_dodgeskill, 2, 0); // dodge skill
209
210         READSKILL(bot_pingskill, 0.5, 0); // ping skill
211
212         READSKILL(bot_weaponskill, 2, 0); // weapon skill
213         READSKILL(bot_aggresskill, 1, 0); // aggre skill
214         READSKILL(bot_rangepreference, 1, 0); // read skill
215
216         READSKILL(bot_aimskill, 2, 0); // aim skill
217         READSKILL(bot_offsetskill, 2, 0.5); // offset skill
218         READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
219
220         READSKILL(bot_thinkskill, 1, 0.5); // think skill
221         READSKILL(bot_aiskill, 2, 0); // "ai" skill
222
223         self.bot_config_loaded = true;
224
225         // this is really only a default, JoinBestTeam is called later
226         setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
227         self.bot_preferredcolors = self.clientcolors;
228
229         // pick the name
230         if (autocvar_bot_usemodelnames)
231                 name = bot_model;
232         else
233                 name = bot_name;
234
235         // number bots with identical names
236         int j = 0;
237         FOREACH_CLIENT(IS_BOT_CLIENT(it), LAMBDA(
238                 if(it.cleanname == name)
239                         ++j;
240         ));
241         if (j)
242                 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(j), ")", suffix));
243         else
244                 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
245
246         self.cleanname = strzone(name);
247
248         // pick the model and skin
249         if(substring(bot_model, -4, 1) != ".")
250                 bot_model = strcat(bot_model, ".iqm");
251         self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
252         self.playerskin = self.playerskin_freeme = strzone(bot_skin);
253
254         self.cvar_cl_accuracy_data_share = 1;  // share the bots weapon accuracy data with the world
255         self.cvar_cl_accuracy_data_receive = 0;  // don't receive any weapon accuracy data
256 }
257
258 void bot_custom_weapon_priority_setup()
259 {
260         float tokens, i, w;
261
262         bot_custom_weapon = false;
263
264         if(     autocvar_bot_ai_custom_weapon_priority_far == "" ||
265                 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
266                 autocvar_bot_ai_custom_weapon_priority_close == "" ||
267                 autocvar_bot_ai_custom_weapon_priority_distances == ""
268         )
269                 return;
270
271         // Parse distances
272         tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
273
274         if (tokens!=2)
275                 return;
276
277         bot_distance_far = stof(argv(0));
278         bot_distance_close = stof(argv(1));
279
280         if(bot_distance_far < bot_distance_close){
281                 bot_distance_far = stof(argv(1));
282                 bot_distance_close = stof(argv(0));
283         }
284
285         // Initialize list of weapons
286         bot_weapons_far[0] = -1;
287         bot_weapons_mid[0] = -1;
288         bot_weapons_close[0] = -1;
289
290         // Parse far distance weapon priorities
291         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
292
293         int c = 0;
294         for(i=0; i < tokens && c < Weapons_COUNT; ++i){
295                 w = stof(argv(i));
296                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
297                         bot_weapons_far[c] = w;
298                         ++c;
299                 }
300         }
301         if(c < Weapons_COUNT)
302                 bot_weapons_far[c] = -1;
303
304         // Parse mid distance weapon priorities
305         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
306
307         c = 0;
308         for(i=0; i < tokens && c < Weapons_COUNT; ++i){
309                 w = stof(argv(i));
310                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
311                         bot_weapons_mid[c] = w;
312                         ++c;
313                 }
314         }
315         if(c < Weapons_COUNT)
316                 bot_weapons_mid[c] = -1;
317
318         // Parse close distance weapon priorities
319         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
320
321         c = 0;
322         for(i=0; i < tokens && i < Weapons_COUNT; ++i){
323                 w = stof(argv(i));
324                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
325                         bot_weapons_close[c] = w;
326                         ++c;
327                 }
328         }
329         if(c < Weapons_COUNT)
330                 bot_weapons_close[c] = -1;
331
332         bot_custom_weapon = true;
333 }
334
335 void bot_endgame()
336 {
337         entity e;
338         //dprint("bot_endgame\n");
339         e = bot_list;
340         while (e)
341         {
342                 setcolor(e, e.bot_preferredcolors);
343                 e = e.nextbot;
344         }
345         // if dynamic waypoints are ever implemented, save them here
346 }
347
348 void bot_relinkplayerlist()
349 {
350         entity e;
351         entity prevbot;
352         player_count = 0;
353         currentbots = 0;
354         player_list = e = findchainflags(flags, FL_CLIENT);
355         bot_list = world;
356         prevbot = world;
357         while (e)
358         {
359                 player_count = player_count + 1;
360                 e.nextplayer = e.chain;
361                 if (IS_BOT_CLIENT(e))
362                 {
363                         if (prevbot)
364                                 prevbot.nextbot = e;
365                         else
366                         {
367                                 bot_list = e;
368                                 bot_list.nextbot = world;
369                         }
370                         prevbot = e;
371                         currentbots = currentbots + 1;
372                 }
373                 e = e.chain;
374         }
375         LOG_TRACE(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
376         bot_strategytoken = bot_list;
377         bot_strategytoken_taken = true;
378 }
379
380 void bot_clientdisconnect()
381 {SELFPARAM();
382         if (!IS_BOT_CLIENT(self))
383                 return;
384         bot_clearqueue(self);
385         if(self.cleanname)
386                 strunzone(self.cleanname);
387         if(self.netname_freeme)
388                 strunzone(self.netname_freeme);
389         if(self.playermodel_freeme)
390                 strunzone(self.playermodel_freeme);
391         if(self.playerskin_freeme)
392                 strunzone(self.playerskin_freeme);
393         self.cleanname = string_null;
394         self.netname_freeme = string_null;
395         self.playermodel_freeme = string_null;
396         self.playerskin_freeme = string_null;
397         if(self.bot_cmd_current)
398                 remove(self.bot_cmd_current);
399         if(bot_waypoint_queue_owner==self)
400                 bot_waypoint_queue_owner = world;
401 }
402
403 void bot_clientconnect(entity this)
404 {
405         if (!IS_BOT_CLIENT(this)) return;
406         self.bot_preferredcolors = self.clientcolors;
407         self.bot_nextthink = time - random();
408         self.lag_func = bot_lagfunc;
409         self.isbot = true;
410         self.createdtime = self.bot_nextthink;
411
412         if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
413                 bot_setnameandstuff();
414
415         if(self.bot_forced_team==1)
416                 self.team = NUM_TEAM_1;
417         else if(self.bot_forced_team==2)
418                 self.team = NUM_TEAM_2;
419         else if(self.bot_forced_team==3)
420                 self.team = NUM_TEAM_3;
421         else if(self.bot_forced_team==4)
422                 self.team = NUM_TEAM_4;
423         else
424                 JoinBestTeam(self, false, true);
425
426         havocbot_setupbot();
427 }
428
429 void bot_removefromlargestteam()
430 {
431         float besttime, bestcount, thiscount;
432         entity best, head;
433         CheckAllowedTeams(world);
434         GetTeamCounts(world);
435         head = findchainfloat(isbot, true);
436         if (!head)
437                 return;
438         best = head;
439         besttime = head.createdtime;
440         bestcount = 0;
441         while (head)
442         {
443                 if(head.team == NUM_TEAM_1)
444                         thiscount = c1;
445                 else if(head.team == NUM_TEAM_2)
446                         thiscount = c2;
447                 else if(head.team == NUM_TEAM_3)
448                         thiscount = c3;
449                 else if(head.team == NUM_TEAM_4)
450                         thiscount = c4;
451                 else
452                         thiscount = 0;
453                 if (thiscount > bestcount)
454                 {
455                         bestcount = thiscount;
456                         besttime = head.createdtime;
457                         best = head;
458                 }
459                 else if (thiscount == bestcount && besttime < head.createdtime)
460                 {
461                         besttime = head.createdtime;
462                         best = head;
463                 }
464                 head = head.chain;
465         }
466         currentbots = currentbots - 1;
467         dropclient(best);
468 }
469
470 void bot_removenewest()
471 {
472         float besttime;
473         entity best, head;
474
475         if(teamplay)
476         {
477                 bot_removefromlargestteam();
478                 return;
479         }
480
481         head = findchainfloat(isbot, true);
482         if (!head)
483                 return;
484         best = head;
485         besttime = head.createdtime;
486         while (head)
487         {
488                 if (besttime < head.createdtime)
489                 {
490                         besttime = head.createdtime;
491                         best = head;
492                 }
493                 head = head.chain;
494         }
495         currentbots = currentbots - 1;
496         dropclient(best);
497 }
498
499 void autoskill(float factor)
500 {
501         float bestbot;
502         float bestplayer;
503
504         bestbot = -1;
505         bestplayer = -1;
506         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
507                 if(IS_REAL_CLIENT(it))
508                         bestplayer = max(bestplayer, it.totalfrags - it.totalfrags_lastcheck);
509                 else
510                         bestbot = max(bestbot, it.totalfrags - it.totalfrags_lastcheck);
511         ));
512
513         LOG_TRACE("autoskill: best player got ", ftos(bestplayer), ", ");
514         LOG_TRACE("best bot got ", ftos(bestbot), "; ");
515         if(bestbot < 0 || bestplayer < 0)
516         {
517                 LOG_TRACE("not doing anything\n");
518                 // don't return, let it reset all counters below
519         }
520         else if(bestbot <= bestplayer * factor - 2)
521         {
522                 if(autocvar_skill < 17)
523                 {
524                         LOG_TRACE("2 frags difference, increasing skill\n");
525                         cvar_set("skill", ftos(autocvar_skill + 1));
526                         bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
527                 }
528         }
529         else if(bestbot >= bestplayer * factor + 2)
530         {
531                 if(autocvar_skill > 0)
532                 {
533                         LOG_TRACE("2 frags difference, decreasing skill\n");
534                         cvar_set("skill", ftos(autocvar_skill - 1));
535                         bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
536                 }
537         }
538         else
539         {
540                 LOG_TRACE("not doing anything\n");
541                 return;
542                 // don't reset counters, wait for them to accumulate
543         }
544
545         FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(it.totalfrags_lastcheck = it.totalfrags));
546 }
547
548 void bot_calculate_stepheightvec()
549 {
550         stepheightvec = autocvar_sv_stepheight * '0 0 1';
551         jumpstepheightvec = stepheightvec +
552                 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
553                 // 0.75 factor is for safety to make the jumps easy
554 }
555
556 float bot_fixcount()
557 {
558         int activerealplayers = 0;
559         int realplayers = 0;
560         if (MUTATOR_CALLHOOK(Bot_FixCount, activerealplayers, realplayers)) {
561                 activerealplayers = bot_activerealplayers;
562                 realplayers = bot_realplayers;
563         } else {
564                 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
565                         if(IS_PLAYER(it))
566                                 ++activerealplayers;
567                         ++realplayers;
568                 ));
569         }
570
571         int bots;
572         // add/remove bots if needed to make sure there are at least
573         // minplayers+bot_number, or remove all bots if no one is playing
574         // But don't remove bots immediately on level change, as the real players
575         // usually haven't rejoined yet
576         bots_would_leave = false;
577         if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
578                 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
579         else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
580         {
581                 float realminplayers, minplayers;
582                 realminplayers = autocvar_minplayers;
583                 minplayers = max(0, floor(realminplayers));
584
585                 float realminbots, minbots;
586                 realminbots = autocvar_bot_number;
587                 minbots = max(0, floor(realminbots));
588
589                 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
590                 if(bots > minbots)
591                         bots_would_leave = true;
592         }
593         else
594         {
595                 // if there are no players, remove bots
596                 bots = 0;
597         }
598
599         // only add one bot per frame to avoid utter chaos
600         if(time > botframe_nextthink)
601         {
602                 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
603                 while (currentbots < bots)
604                 {
605                         if (bot_spawn() == world)
606                         {
607                                 bprint("Can not add bot, server full.\n");
608                                 return false;
609                         }
610                 }
611                 while (currentbots > bots)
612                         bot_removenewest();
613         }
614
615         return true;
616 }
617
618 void bot_serverframe()
619 {
620         if (intermission_running)
621                 return;
622
623         if (time < 2)
624                 return;
625
626         bot_calculate_stepheightvec();
627         bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
628
629         if(time > autoskill_nextthink)
630         {
631                 float a;
632                 a = autocvar_skill_auto;
633                 if(a)
634                         autoskill(a);
635                 autoskill_nextthink = time + 5;
636         }
637
638         if(time > botframe_nextthink)
639         {
640                 if(!bot_fixcount())
641                         botframe_nextthink = time + 10;
642         }
643
644         bot_ignore_bots = autocvar_bot_ignore_bots;
645
646         if(botframe_spawnedwaypoints)
647         {
648                 if(autocvar_waypoint_benchmark)
649                         localcmd("quit\n");
650         }
651
652         if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
653         if (botframe_spawnedwaypoints)
654         {
655                 if(botframe_cachedwaypointlinks)
656                 {
657                         if(!botframe_loadedforcedlinks)
658                                 waypoint_load_links_hardwired();
659                 }
660                 else
661                 {
662                         // TODO: Make this check cleaner
663                         entity wp = findchain(classname, "waypoint");
664                         if(time - wp.nextthink > 10)
665                                 waypoint_save_links();
666                 }
667         }
668         else
669         {
670                 botframe_spawnedwaypoints = true;
671                 waypoint_loadall();
672                 if(!waypoint_load_links())
673                         waypoint_schedulerelinkall();
674         }
675
676         if (bot_list)
677         {
678                 // cycle the goal token from one bot to the next each frame
679                 // (this prevents them from all doing spawnfunc_waypoint searches on the same
680                 //  frame, which causes choppy framerates)
681                 if (bot_strategytoken_taken)
682                 {
683                         bot_strategytoken_taken = false;
684                         if (bot_strategytoken)
685                                 bot_strategytoken = bot_strategytoken.nextbot;
686                         if (!bot_strategytoken)
687                                 bot_strategytoken = bot_list;
688                 }
689
690                 if (botframe_nextdangertime < time)
691                 {
692                         float interval;
693                         interval = autocvar_bot_ai_dangerdetectioninterval;
694                         if (botframe_nextdangertime < time - interval * 1.5)
695                                 botframe_nextdangertime = time;
696                         botframe_nextdangertime = botframe_nextdangertime + interval;
697                         botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
698                 }
699         }
700
701         if (autocvar_g_waypointeditor)
702                 botframe_showwaypointlinks();
703
704         if (autocvar_g_waypointeditor_auto)
705                 botframe_autowaypoints();
706
707         if(time > bot_cvar_nextthink)
708         {
709                 if(currentbots>0)
710                         bot_custom_weapon_priority_setup();
711                 bot_cvar_nextthink = time + 5;
712         }
713 }