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