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