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