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