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