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