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