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