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