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