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