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