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