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