]> de.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.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.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                 for(;;)
139                 {
140                         readfile = fgets(file);
141                         if(!readfile)
142                                 break;
143                         if(substring(readfile, 0, 2) == "//")
144                                 continue;
145                         if(substring(readfile, 0, 1) == "#")
146                                 continue;
147                         tokens = tokenizebyseparator(readfile, "\t");
148                         s = argv(0);
149                         prio = 1;
150                         FOR_EACH_CLIENT(p)
151                         {
152                                 if(clienttype(p) == CLIENTTYPE_BOT)
153                                 if(s == p.cleanname)
154                                 {
155                                         prio = 0;
156                                         break;
157                                 }
158                         }
159                         RandomSelection_Add(world, 0, readfile, 1, prio);
160                 }
161                 readfile = RandomSelection_chosen_string;
162                 fclose(file);
163         }
164
165         tokens = tokenizebyseparator(readfile, "\t");
166         if(argv(0) != "") bot_name = argv(0);
167         else bot_name = "Bot";
168
169         if(argv(1) != "") bot_model = argv(1);
170         else bot_model = "";
171
172         if(argv(2) != "") bot_skin = argv(2);
173         else bot_skin = "0";
174
175         if(argv(3) != "" && stof(argv(3)) >= 0) bot_shirt = argv(3);
176         else bot_shirt = ftos(floor(random() * 15));
177
178         if(argv(4) != "" && stof(argv(4)) >= 0) bot_pants = argv(4);
179         else bot_pants = ftos(floor(random() * 15));
180
181         self.bot_forced_team = stof(argv(5));
182
183         prio = 6;
184
185         #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
186         //print(bot_name, ": ping=", argv(9), "\n");
187
188         READSKILL(havocbot_keyboardskill, 0.5, 0.5); // keyboard skill
189         READSKILL(bot_moveskill, 2, 0); // move skill
190         READSKILL(bot_dodgeskill, 2, 0); // dodge skill
191
192         READSKILL(bot_pingskill, 0.5, 0); // ping skill
193
194         READSKILL(bot_weaponskill, 2, 0); // weapon skill
195         READSKILL(bot_aggresskill, 1, 0); // aggre skill
196         READSKILL(bot_rangepreference, 1, 0); // read skill
197
198         READSKILL(bot_aimskill, 2, 0); // aim skill
199         READSKILL(bot_offsetskill, 2, 0.5); // offset skill
200         READSKILL(bot_mouseskill, 1, 0.5); // mouse skill
201
202         READSKILL(bot_thinkskill, 1, 0.5); // think skill
203         READSKILL(bot_aiskill, 2, 0); // "ai" skill
204
205         self.bot_config_loaded = TRUE;
206
207         // this is really only a default, JoinBestTeam is called later
208         setcolor(self, stof(bot_shirt) * 16 + stof(bot_pants));
209         self.bot_preferredcolors = self.clientcolors;
210
211         // pick the name
212         if (autocvar_bot_usemodelnames)
213                 name = bot_model;
214         else
215                 name = bot_name;
216
217         // number bots with identical names
218         float i;
219         i = 0;
220         FOR_EACH_CLIENT(p)
221         {
222                 if(clienttype(p) == CLIENTTYPE_BOT)
223                         if(p.cleanname == name)
224                                 ++i;
225         }
226         if (i)
227                 self.netname = self.netname_freeme = strzone(strcat(prefix, name, "(", ftos(i), ")", suffix));
228         else
229                 self.netname = self.netname_freeme = strzone(strcat(prefix, name, suffix));
230
231         self.cleanname = strzone(name);
232
233         // pick the model and skin
234         if(substring(bot_model, -4, 1) != ".")
235                 bot_model = strcat(bot_model, ".iqm");
236         self.playermodel = self.playermodel_freeme = strzone(strcat("models/player/", bot_model));
237         self.playerskin = self.playerskin_freeme = strzone(bot_skin);
238
239         self.cvar_cl_accuracy_data_share = 1;  // share the bots weapon accuracy data with the world
240         self.cvar_cl_accuracy_data_receive = 0;  // don't receive any weapon accuracy data
241 }
242
243 void bot_custom_weapon_priority_setup()
244 {
245         float tokens, i, c, w;
246
247         bot_custom_weapon = FALSE;
248
249         if(     autocvar_bot_ai_custom_weapon_priority_far == "" ||
250                 autocvar_bot_ai_custom_weapon_priority_mid == "" ||
251                 autocvar_bot_ai_custom_weapon_priority_close == "" ||
252                 autocvar_bot_ai_custom_weapon_priority_distances == ""
253         )
254                 return;
255
256         // Parse distances
257         tokens = tokenizebyseparator(autocvar_bot_ai_custom_weapon_priority_distances," ");
258
259         if (tokens!=2)
260                 return;
261
262         bot_distance_far = stof(argv(0));
263         bot_distance_close = stof(argv(1));
264
265         if(bot_distance_far < bot_distance_close){
266                 bot_distance_far = stof(argv(1));
267                 bot_distance_close = stof(argv(0));
268         }
269
270         // Initialize list of weapons
271         bot_weapons_far[0] = -1;
272         bot_weapons_mid[0] = -1;
273         bot_weapons_close[0] = -1;
274
275         // Parse far distance weapon priorities
276         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_far)," ");
277
278         c = 0;
279         for(i=0; i < tokens && c < WEP_COUNT; ++i){
280                 w = stof(argv(i));
281                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
282                         bot_weapons_far[c] = w;
283                         ++c;
284                 }
285         }
286         if(c < WEP_COUNT)
287                 bot_weapons_far[c] = -1;
288
289         // Parse mid distance weapon priorities
290         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_mid)," ");
291
292         c = 0;
293         for(i=0; i < tokens && c < WEP_COUNT; ++i){
294                 w = stof(argv(i));
295                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
296                         bot_weapons_mid[c] = w;
297                         ++c;
298                 }
299         }
300         if(c < WEP_COUNT)
301                 bot_weapons_mid[c] = -1;
302
303         // Parse close distance weapon priorities
304         tokens = tokenizebyseparator(W_NumberWeaponOrder(autocvar_bot_ai_custom_weapon_priority_close)," ");
305
306         c = 0;
307         for(i=0; i < tokens && i < WEP_COUNT; ++i){
308                 w = stof(argv(i));
309                 if ( w >= WEP_FIRST && w <= WEP_LAST) {
310                         bot_weapons_close[c] = w;
311                         ++c;
312                 }
313         }
314         if(c < WEP_COUNT)
315                 bot_weapons_close[c] = -1;
316
317         bot_custom_weapon = TRUE;
318 }
319
320 void bot_endgame()
321 {
322         entity e;
323         //dprint("bot_endgame\n");
324         e = bot_list;
325         while (e)
326         {
327                 setcolor(e, e.bot_preferredcolors);
328                 e = e.nextbot;
329         }
330         // if dynamic waypoints are ever implemented, save them here
331 }
332
333 void bot_relinkplayerlist()
334 {
335         entity e;
336         entity prevbot;
337         player_count = 0;
338         currentbots = 0;
339         player_list = e = findchainflags(flags, FL_CLIENT);
340         bot_list = world;
341         prevbot = world;
342         while (e)
343         {
344                 player_count = player_count + 1;
345                 e.nextplayer = e.chain;
346                 if (clienttype(e) == CLIENTTYPE_BOT)
347                 {
348                         if (prevbot)
349                                 prevbot.nextbot = e;
350                         else
351                         {
352                                 bot_list = e;
353                                 bot_list.nextbot = world;
354                         }
355                         prevbot = e;
356                         currentbots = currentbots + 1;
357                 }
358                 e = e.chain;
359         }
360         dprint(strcat("relink: ", ftos(currentbots), " bots seen.\n"));
361         bot_strategytoken = bot_list;
362         bot_strategytoken_taken = TRUE;
363 }
364
365 void bot_clientdisconnect()
366 {
367         if (clienttype(self) != CLIENTTYPE_BOT)
368                 return;
369         bot_clearqueue(self);
370         if(self.cleanname)
371                 strunzone(self.cleanname);
372         if(self.netname_freeme)
373                 strunzone(self.netname_freeme);
374         if(self.playermodel_freeme)
375                 strunzone(self.playermodel_freeme);
376         if(self.playerskin_freeme)
377                 strunzone(self.playerskin_freeme);
378         self.cleanname = string_null;
379         self.netname_freeme = string_null;
380         self.playermodel_freeme = string_null;
381         self.playerskin_freeme = string_null;
382         if(self.bot_cmd_current)
383                 remove(self.bot_cmd_current);
384         if(bot_waypoint_queue_owner==self)
385                 bot_waypoint_queue_owner = world;
386 }
387
388 void bot_clientconnect()
389 {
390         if (clienttype(self) != CLIENTTYPE_BOT)
391                 return;
392         self.bot_preferredcolors = self.clientcolors;
393         self.bot_nextthink = time - random();
394         self.lag_func = bot_lagfunc;
395         self.isbot = TRUE;
396         self.createdtime = self.nextthink;
397
398         if(!self.bot_config_loaded) // This is needed so team overrider doesn't break between matches
399                 bot_setnameandstuff();
400
401         if(self.bot_forced_team==1)
402                 self.team = FL_TEAM_1;
403         else if(self.bot_forced_team==2)
404                 self.team = FL_TEAM_2;
405         else if(self.bot_forced_team==3)
406                 self.team = FL_TEAM_3;
407         else if(self.bot_forced_team==4)
408                 self.team = FL_TEAM_4;
409         else
410                 JoinBestTeam(self, FALSE, TRUE);
411
412         havocbot_setupbot();
413 }
414
415 void bot_removefromlargestteam()
416 {
417         float besttime, bestcount, thiscount;
418         entity best, head;
419         CheckAllowedTeams(world);
420         GetTeamCounts(world);
421         head = findchainfloat(isbot, TRUE);
422         if (!head)
423                 return;
424         best = head;
425         besttime = head.createdtime;
426         bestcount = 0;
427         while (head)
428         {
429                 if(head.team == FL_TEAM_1)
430                         thiscount = c1;
431                 else if(head.team == FL_TEAM_2)
432                         thiscount = c2;
433                 else if(head.team == FL_TEAM_3)
434                         thiscount = c3;
435                 else if(head.team == FL_TEAM_4)
436                         thiscount = c4;
437                 else
438                         thiscount = 0;
439                 if (thiscount > bestcount)
440                 {
441                         bestcount = thiscount;
442                         besttime = head.createdtime;
443                         best = head;
444                 }
445                 else if (thiscount == bestcount && besttime < head.createdtime)
446                 {
447                         besttime = head.createdtime;
448                         best = head;
449                 }
450                 head = head.chain;
451         }
452         currentbots = currentbots - 1;
453         dropclient(best);
454 }
455
456 void bot_removenewest()
457 {
458         float besttime;
459         entity best, head;
460
461         if(teamplay)
462         {
463                 bot_removefromlargestteam();
464                 return;
465         }
466
467         head = findchainfloat(isbot, TRUE);
468         if (!head)
469                 return;
470         best = head;
471         besttime = head.createdtime;
472         while (head)
473         {
474                 if (besttime < head.createdtime)
475                 {
476                         besttime = head.createdtime;
477                         best = head;
478                 }
479                 head = head.chain;
480         }
481         currentbots = currentbots - 1;
482         dropclient(best);
483 }
484
485 void autoskill(float factor)
486 {
487         float bestbot;
488         float bestplayer;
489         entity head;
490
491         bestbot = -1;
492         bestplayer = -1;
493         FOR_EACH_PLAYER(head)
494         {
495                 if(clienttype(head) == CLIENTTYPE_REAL)
496                         bestplayer = max(bestplayer, head.totalfrags - head.totalfrags_lastcheck);
497                 else
498                         bestbot = max(bestbot, head.totalfrags - head.totalfrags_lastcheck);
499         }
500
501         dprint("autoskill: best player got ", ftos(bestplayer), ", ");
502         dprint("best bot got ", ftos(bestbot), "; ");
503         if(bestbot < 0 || bestplayer < 0)
504         {
505                 dprint("not doing anything\n");
506                 // don't return, let it reset all counters below
507         }
508         else if(bestbot <= bestplayer * factor - 2)
509         {
510                 if(autocvar_skill < 17)
511                 {
512                         dprint("2 frags difference, increasing skill\n");
513                         cvar_set("skill", ftos(autocvar_skill + 1));
514                         bprint("^2SKILL UP!^7 Now at level ", ftos(autocvar_skill), "\n");
515                 }
516         }
517         else if(bestbot >= bestplayer * factor + 2)
518         {
519                 if(autocvar_skill > 0)
520                 {
521                         dprint("2 frags difference, decreasing skill\n");
522                         cvar_set("skill", ftos(autocvar_skill - 1));
523                         bprint("^1SKILL DOWN!^7 Now at level ", ftos(autocvar_skill), "\n");
524                 }
525         }
526         else
527         {
528                 dprint("not doing anything\n");
529                 return;
530                 // don't reset counters, wait for them to accumulate
531         }
532
533         FOR_EACH_PLAYER(head)
534                 head.totalfrags_lastcheck = head.totalfrags;
535 }
536
537 void bot_calculate_stepheightvec(void)
538 {
539         stepheightvec = autocvar_sv_stepheight * '0 0 1';
540         jumpstepheightvec = stepheightvec +
541                 ((autocvar_sv_jumpvelocity * autocvar_sv_jumpvelocity) / (2 * autocvar_sv_gravity)) * '0 0 0.85';
542                 // 0.75 factor is for safety to make the jumps easy
543 }
544
545 float bot_fixcount()
546 {
547         entity head;
548         float realplayers, bots, activerealplayers;
549
550         activerealplayers = 0;
551         realplayers = 0;
552
553         FOR_EACH_REALCLIENT(head)
554         {
555                 if(head.classname == "player" || g_lms || g_arena || g_ca)
556                         ++activerealplayers;
557                 ++realplayers;
558         }
559
560         // add/remove bots if needed to make sure there are at least
561         // minplayers+bot_number, or remove all bots if no one is playing
562         // But don't remove bots immediately on level change, as the real players
563         // usually haven't rejoined yet
564         bots_would_leave = FALSE;
565         if (teamplay && autocvar_bot_vs_human && (c3==-1 && c4==-1))
566                 bots = min(ceil(fabs(autocvar_bot_vs_human) * activerealplayers), maxclients - realplayers);
567         else if ((realplayers || autocvar_bot_join_empty || (currentbots > 0 && time < 5)))
568         {
569                 float realminplayers, minplayers;
570                 realminplayers = autocvar_minplayers;
571                 minplayers = max(0, floor(realminplayers));
572
573                 float realminbots, minbots;
574                 realminbots = autocvar_bot_number;
575                 minbots = max(0, floor(realminbots));
576
577                 bots = min(max(minbots, minplayers - activerealplayers), maxclients - realplayers);
578                 if(bots > minbots)
579                         bots_would_leave = TRUE;
580         }
581         else
582         {
583                 // if there are no players, remove bots
584                 bots = 0;
585         }
586
587         // only add one bot per frame to avoid utter chaos
588         if(time > botframe_nextthink)
589         {
590                 //dprint(ftos(bots), " ? ", ftos(currentbots), "\n");
591                 while (currentbots < bots)
592                 {
593                         if (bot_spawn() == world)
594                         {
595                                 bprint("Can not add bot, server full.\n");
596                                 return FALSE;
597                         }
598                 }
599                 while (currentbots > bots)
600                         bot_removenewest();
601         }
602
603         return TRUE;
604 }
605
606 void bot_serverframe()
607 {
608         if (intermission_running)
609                 return;
610
611         if (time < 2)
612                 return;
613
614         bot_calculate_stepheightvec();
615         bot_navigation_movemode = ((autocvar_bot_navigation_ignoreplayers) ? MOVE_NOMONSTERS : MOVE_NORMAL);
616
617         if(time > autoskill_nextthink)
618         {
619                 float a;
620                 a = autocvar_skill_auto;
621                 if(a)
622                         autoskill(a);
623                 autoskill_nextthink = time + 5;
624         }
625
626         if(time > botframe_nextthink)
627         {
628                 if(!bot_fixcount())
629                         botframe_nextthink = time + 10;
630         }
631
632         bot_ignore_bots = autocvar_bot_ignore_bots;
633
634         if(botframe_spawnedwaypoints)
635         {
636                 if(autocvar_waypoint_benchmark)
637                         localcmd("quit\n");
638         }
639
640         if (currentbots > 0 || autocvar_g_waypointeditor || autocvar_g_waypointeditor_auto)
641         if (botframe_spawnedwaypoints)
642         {
643                 if(botframe_cachedwaypointlinks)
644                 {
645                         if(!botframe_loadedforcedlinks)
646                                 waypoint_load_links_hardwired();
647                 }
648                 else
649                 {
650                         // TODO: Make this check cleaner
651                         entity wp = findchain(classname, "waypoint");
652                         if(time - wp.nextthink > 10)
653                                 waypoint_save_links();
654                 }
655         }
656         else
657         {
658                 botframe_spawnedwaypoints = TRUE;
659                 waypoint_loadall();
660                 if(!waypoint_load_links())
661                         waypoint_schedulerelinkall();
662         }
663
664         if (bot_list)
665         {
666                 // cycle the goal token from one bot to the next each frame
667                 // (this prevents them from all doing spawnfunc_waypoint searches on the same
668                 //  frame, which causes choppy framerates)
669                 if (bot_strategytoken_taken)
670                 {
671                         bot_strategytoken_taken = FALSE;
672                         if (bot_strategytoken)
673                                 bot_strategytoken = bot_strategytoken.nextbot;
674                         if (!bot_strategytoken)
675                                 bot_strategytoken = bot_list;
676                 }
677
678                 if (botframe_nextdangertime < time)
679                 {
680                         float interval;
681                         interval = autocvar_bot_ai_dangerdetectioninterval;
682                         if (botframe_nextdangertime < time - interval * 1.5)
683                                 botframe_nextdangertime = time;
684                         botframe_nextdangertime = botframe_nextdangertime + interval;
685                         botframe_updatedangerousobjects(autocvar_bot_ai_dangerdetectionupdates);
686                 }
687         }
688
689         if (autocvar_g_waypointeditor)
690                 botframe_showwaypointlinks();
691
692         if (autocvar_g_waypointeditor_auto)
693                 botframe_autowaypoints();
694
695         if(time > bot_cvar_nextthink)
696         {
697                 if(currentbots>0)
698                         bot_custom_weapon_priority_setup();
699                 bot_cvar_nextthink = time + 5;
700         }
701 }