]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/havocbot.qc
Replaced water levels with meaningful constants
[divverent/nexuiz.git] / data / qcsrc / server / havocbot.qc
1 .void() havocbot_role;
2 void() havocbot_chooserole;
3 .float havocbot_keyboardskill;
4 .float facingwalltime, ignoregoaltime;
5 .entity ignoregoal;
6
7 #ifdef DEBUG_BOT_GOALSTACK
8 void debuggoalstack();
9 #endif
10
11 vector havocbot_dodge()
12 {
13         // LordHavoc: disabled because this is too expensive
14         return '0 0 0';
15         /*
16         local entity head;
17         local vector dodge, v, n;
18         local float danger, bestdanger, vl, d;
19         dodge = '0 0 0';
20         bestdanger = -20;
21         // check for dangerous objects near bot or approaching bot
22         head = findchainfloat(bot_dodge, TRUE);
23         while(head)
24         {
25                 if (head.owner != self)
26                 {
27                         vl = vlen(head.velocity);
28                         if (vl > sv_maxspeed * 0.3)
29                         {
30                                 n = normalize(head.velocity);
31                                 v = self.origin - head.origin;
32                                 d = v * n;
33                                 if (d > (0 - head.bot_dodgerating))
34                                 if (d < (vl * 0.2 + head.bot_dodgerating))
35                                 {
36                                         // calculate direction and distance from the flight path, by removing the forward axis
37                                         v = v - (n * (v * n));
38                                         danger = head.bot_dodgerating - vlen(v);
39                                         if (bestdanger < danger)
40                                         {
41                                                 bestdanger = danger;
42                                                 // dodge to the side of the object
43                                                 dodge = normalize(v);
44                                         }
45                                 }
46                         }
47                         else
48                         {
49                                 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
50                                 if (bestdanger < danger)
51                                 {
52                                         bestdanger = danger;
53                                         dodge = normalize(self.origin - head.origin);
54                                 }
55                         }
56                 }
57                 head = head.chain;
58         }
59         return dodge;
60         */
61 };
62
63 .float havocbot_keyboardtime;
64 .float havocbot_ducktime;
65 .vector havocbot_keyboard;
66 void havocbot_keyboard_movement(vector destorg)
67 {
68         local vector keyboard;
69         local float blend, maxspeed;
70
71         maxspeed = cvar("sv_maxspeed");
72
73         if (time < self.havocbot_keyboardtime)
74                 return;
75
76         self.havocbot_keyboardtime =
77                 max(
78                         self.havocbot_keyboardtime
79                                 + bound(0,0.05/(skill+self.havocbot_keyboardskill),0.05)
80                                 +random()*bound(0,0.025/(skill+self.havocbot_keyboardskill),100)
81                 , time);
82         keyboard = self.movement * (1.0 / maxspeed);
83
84         local float trigger, trigger1;
85         blend = bound(0,skill*0.1,1);
86         trigger = cvar("bot_ai_keyboard_treshold");
87         trigger1 = 0 - trigger;
88
89         // categorize forward movement
90         // at skill < 1.5 only forward
91         // at skill < 2.5 only individual directions
92         // at skill < 4.5 only individual directions, and forward diagonals
93         // at skill >= 4.5, all cases allowed
94         if (keyboard_x > trigger)
95         {
96                 keyboard_x = 1;
97                 if (skill < 2.5)
98                         keyboard_y = 0;
99         }
100         else if (keyboard_x < trigger1 && skill > 1.5)
101         {
102                 keyboard_x = -1;
103                 if (skill < 4.5)
104                         keyboard_y = 0;
105         }
106         else
107         {
108                 keyboard_x = 0;
109                 if (skill < 1.5)
110                         keyboard_y = 0;
111         }
112         if (skill < 4.5)
113                 keyboard_z = 0;
114
115         if (keyboard_y > trigger)
116                 keyboard_y = 1;
117         else if (keyboard_y < trigger1)
118                 keyboard_y = -1;
119         else
120                 keyboard_y = 0;
121
122         if (keyboard_z > trigger)
123                 keyboard_z = 1;
124         else if (keyboard_z < trigger1)
125                 keyboard_z = -1;
126         else
127                 keyboard_z = 0;
128
129         self.havocbot_keyboard = keyboard * maxspeed;
130         if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
131
132         keyboard = self.havocbot_keyboard;
133         blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
134         //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
135         self.movement = self.movement + (keyboard - self.movement) * blend;
136 };
137
138 .entity bot_lastseengoal;
139 .float bot_timelastseengoal;
140 .float bot_canruntogoal;
141 void havocbot_bunnyhop(vector dir)
142 {
143         local float bunnyhopdistance;
144         local vector deviation;
145
146         if(self.goalcurrent.classname == "player")
147                 return;
148
149         if(self.aistatus & AI_STATUS_DANGER_AHEAD)
150         {
151                 self.aistatus &~= AI_STATUS_RUNNING;
152                 self.BUTTON_JUMP = FALSE;
153                 return;
154         }
155
156         if(self.waterlevel > WATERLEVEL_WETFEET)
157         {
158                 self.aistatus &~= AI_STATUS_RUNNING;
159                 return;
160         }
161
162         if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
163         {
164                 self.bot_canruntogoal = 0;
165                 self.bot_timelastseengoal = 0;
166         }
167
168         bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
169
170         // Run only to visible goals
171         traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
172         if(self.flags & FL_ONGROUND)
173         if(trace_fraction == 1)
174         {
175                         self.bot_lastseengoal = self.goalcurrent;
176
177                         // seen it before
178                         if(self.bot_timelastseengoal)
179                         {
180                                 // for a period of time
181                                 if(time - self.bot_timelastseengoal > cvar("bot_ai_bunnyhop_firstjumpdelay"))
182                                 {
183                                         local float checkdistance;
184                                         checkdistance = TRUE;
185
186                                         // don't run if it is too close
187                                         if(self.bot_canruntogoal==0)
188                                         {
189                                                 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_startdistance"))
190                                                         self.bot_canruntogoal = 1;
191                                                 else
192                                                         self.bot_canruntogoal = -1;
193                                         }
194
195                                         if(self.bot_canruntogoal != 1)
196                                                 return;
197
198                                         if(self.aistatus & AI_STATUS_ROAMING)
199                                         if(self.goalcurrent.classname=="waypoint")
200                                         if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
201                                         if(self.goalstack01!=world)
202                                         {
203                                                 deviation = self.goalstack01.origin - (self.origin + self.view_ofs);
204                                                 deviation = vectoangles(deviation) - self.v_angle;
205                                                 while (deviation_y < -180) deviation_y = deviation_y + 360;
206                                                 while (deviation_y > 180) deviation_y = deviation_y - 360;
207
208                                                 if(fabs(deviation_y) < 15)
209                                                 if(bunnyhopdistance < vlen(self.origin - self.goalstack01.origin))
210                                                 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
211                                                 {
212                                                         traceline(self.origin + self.view_ofs , self.goalstack01.origin, TRUE, world);
213                                                         if(trace_fraction==1)
214                                                         {
215                                                                 checkdistance = FALSE;
216                                                         }
217                                                 }
218                                         }
219
220                                         if(checkdistance)
221                                         {
222                                                 self.aistatus &~= AI_STATUS_RUNNING;
223                                                 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_stopdistance"))
224                                                         self.BUTTON_JUMP = TRUE;
225                                         }
226                                         else
227                                         {
228                                                 self.aistatus |= AI_STATUS_RUNNING;
229                                                 self.BUTTON_JUMP = TRUE;
230                                         }
231                                 }
232                         }
233                         else
234                         {
235                                 self.bot_timelastseengoal = time;
236                         }
237         }
238         else
239         {
240                 self.bot_timelastseengoal = 0;
241         }
242
243         // Release jump button
244         if(self.flags & FL_ONGROUND == 0)
245         {
246                 if(self.velocity_z < 0)
247                         self.BUTTON_JUMP = FALSE;
248
249                 // Strafe
250                 local float maxspeed;
251                 maxspeed = cvar("sv_maxspeed");
252
253                 if(self.aistatus & AI_STATUS_RUNNING)
254                 if(vlen(self.velocity)>maxspeed)
255                 {
256                         deviation = vectoangles(dir) - self.v_angle;
257                         while (deviation_y < -180) deviation_y = deviation_y + 360;
258                         while (deviation_y > 180) deviation_y = deviation_y - 360;
259
260                         if ( deviation_y > 2 )
261                                 self.movement_y = maxspeed;
262                         if ( deviation_y < -2 )
263                                 self.movement_y = maxspeed * -1;
264                 }
265         }
266 };
267
268 //.float havocbotignoretime;
269 //.vector bot_dodgevector;
270 //.float bot_dodgevector_time;
271 //.float bot_dodgevector_jumpbutton;
272 .float ladder_time;
273 .entity ladder_entity;
274 void havocbot_movetogoal()
275 {
276         local vector destorg;
277         local vector diff;
278         local vector dir;
279         local vector flatdir;
280         local vector m1;
281         local vector m2;
282         local vector evadeobstacle;
283         local vector evadelava;
284         local float s;
285         local float maxspeed;
286         //local float dist;
287         local vector dodge;
288         //if (self.goalentity)
289         //      te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
290         self.movement = '0 0 0';
291         maxspeed = cvar("sv_maxspeed");
292
293         if(self.jumppadcount)
294         {
295                 if(self.flags & FL_ONGROUND)
296                 {
297                         self.jumppadcount = FALSE;
298                         if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
299                                 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
300                 }
301
302                 // If got stuck on the jump pad try to reach the farther visible item
303                 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
304                 {
305                         if(fabs(self.velocity_z)<50)
306                         {
307                                 local entity head, newgoal;
308                                 local float distance, bestdistance;
309
310                                 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
311                                 {
312                                         if(head.classname=="worldspawn")
313                                                 continue;
314
315                                         distance = vlen(head.origin - self.origin);
316                                         if(distance>1000)
317                                                 continue;
318
319                                         traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
320
321                                         if(trace_fraction<1)
322                                                 continue;
323
324                                         if(distance>bestdistance)
325                                         {
326                                                 newgoal = head;
327                                                 bestdistance = distance;
328                                         }
329                                 }
330
331                                 if(newgoal)
332                                 {
333                                         self.ignoregoal = self.goalcurrent;
334                                         self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
335                                         navigation_routetogoal(newgoal);
336                                         self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
337                                 }
338                         }
339                         else
340                                 return;
341                 }
342                 else
343                 {
344                         if(self.velocity_z>0)
345                         {
346                                 local float threshold;
347                                 threshold = maxspeed * 0.2;
348                                 if(fabs(self.velocity_x) < threshold  &&  fabs(self.velocity_y) < threshold)
349                                         self.aistatus |= AI_STATUS_OUT_JUMPPAD;
350                                 return;
351                         }
352                 }
353         }
354
355         // If we are under water with no goals, swim up
356         if(self.waterlevel)
357         if(self.goalcurrent==world)
358         {
359                 dir = '0 0 0';
360                 if(self.waterlevel>WATERLEVEL_SWIMMING)
361                         dir_z = 1;
362                 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
363                         self.BUTTON_JUMP = TRUE;
364                 else
365                         self.BUTTON_JUMP = FALSE;
366                 makevectors(self.v_angle_y * '0 1 0');
367                 self.movement_x = dir * v_forward * maxspeed;
368                 self.movement_y = dir * v_right * maxspeed;
369                 self.movement_z = dir * v_up * maxspeed;
370         }
371
372         // if there is nowhere to go, exit
373         if (self.goalcurrent == world)
374                 return;
375
376         if (self.goalcurrent)
377                 navigation_poptouchedgoals();
378
379         // if ran out of goals try to use an alternative goal or get a new strategy asap
380         if(self.goalcurrent == world)
381         if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
382         {
383                 if(self.alternativegoal==world)
384                 {
385                         // ran out of goals
386                         self.bot_strategytime = 0;
387                         return;
388                 }
389
390                 // use the alternative goal
391                 navigation_findnearestwaypoint(self.alternativegoal, TRUE);
392                 navigation_routetogoal(self.alternativegoal);
393                 self.alternativegoal = world;
394                 bot_strategytoken_taken = TRUE;
395
396                 return;
397         }
398
399 #ifdef DEBUG_BOT_GOALSTACK
400         debuggoalstack();
401 #endif
402
403         m1 = self.goalcurrent.origin + self.goalcurrent.mins;
404         m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
405         destorg = self.origin;
406         destorg_x = bound(m1_x, destorg_x, m2_x);
407         destorg_y = bound(m1_y, destorg_y, m2_y);
408         destorg_z = bound(m1_z, destorg_z, m2_z);
409         diff = destorg - self.origin;
410         //dist = vlen(diff);
411         dir = normalize(diff);
412         flatdir = diff;flatdir_z = 0;
413         flatdir = normalize(flatdir);
414
415         //if (self.bot_dodgevector_time < time)
416         {
417         //      self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
418         //      self.bot_dodgevector_jumpbutton = 1;
419                 evadeobstacle = '0 0 0';
420                 evadelava = '0 0 0';
421
422                 if (self.waterlevel)
423                 {
424                         if(self.waterlevel>WATERLEVEL_SWIMMING)
425                         {
426                         //      flatdir_z = 1;
427                                 self.aistatus |= AI_STATUS_OUT_WATER;
428                         }
429                         else
430                         {
431                                 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
432                                         ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
433                                         self.BUTTON_JUMP = TRUE;
434                                 else
435                                         self.BUTTON_JUMP = FALSE;
436                         }
437                         dir = normalize(flatdir);
438                         makevectors(self.v_angle_y * '0 1 0');
439                 }
440                 else
441                 {
442                         if(self.aistatus & AI_STATUS_OUT_WATER)
443                                 self.aistatus &~= AI_STATUS_OUT_WATER;
444
445                         // jump if going toward an obstacle that doesn't look like stairs we
446                         // can walk up directly
447                         tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
448                         if (trace_fraction < 1)
449                         if (trace_plane_normal_z < 0.7)
450                         {
451                                 s = trace_fraction;
452                                 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
453                                 if (trace_fraction < s + 0.01)
454                                 if (trace_plane_normal_z < 0.7)
455                                 {
456                                         s = trace_fraction;
457                                         tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
458                                         if (trace_fraction > s)
459                                                 self.BUTTON_JUMP = 1;
460                                 }
461                         }
462
463                         // avoiding dangers and obstacles
464                         local vector dst_ahead, dst_down;
465                         dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.32);
466                         dst_down = dst_ahead + '0 0 -1500';
467
468                         // Look ahead
469                         traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
470
471                         // Check head-banging against walls
472                         if(vlen(self.origin + self.view_ofs - trace_endpos) < 2 && !(self.aistatus & AI_STATUS_OUT_WATER))
473                         {
474                                 if(self.facingwalltime && time > self.facingwalltime)
475                                 {
476                                         self.ignoregoal = self.goalcurrent;
477                                         self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
478                                         navigation_poproute();
479                                 }
480                                 else
481                                 {
482                                         self.facingwalltime = time + 0.05;
483                                 }
484                         }
485                         else
486                         {
487                                 self.facingwalltime = 0;
488
489                                 if(self.ignoregoal != world && time > self.ignoregoaltime)
490                                 {
491                                         self.ignoregoal = world;
492                                         self.ignoregoaltime = 0;
493                                 }
494                         }
495
496                         // Check for water/slime/lava and dangerous edges
497                         // (only when the bot is on the ground or jumping intentionally)
498                         self.aistatus &~= AI_STATUS_DANGER_AHEAD;
499
500                         if(trace_fraction == 1)
501                         if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
502                         {
503                                 // Look downwards
504                                 traceline(dst_ahead , dst_down, TRUE, world);
505                                 //      te_lightning2(world, self.origin, dst_ahead);   // Draw "ahead" look
506                                 //      te_lightning2(world, dst_ahead, dst_down);              // Draw "downwards" look
507                                 if(trace_endpos_z < self.origin_z + self.mins_z)
508                                 {
509                                         s = pointcontents(trace_endpos + '0 0 1');
510                                         if (s != CONTENT_SOLID)
511                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
512                                                 evadelava = normalize(self.velocity) * -1;
513                                         else if (s == CONTENT_SKY)
514                                                 evadeobstacle = normalize(self.velocity) * -1;
515                                         else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
516                                                                 self.goalcurrent.absmin, self.goalcurrent.absmax))
517                                         {
518                                                 // if ain't a safe goal with "holes" (like the jumpad on soylent)
519                                                 // and there is a trigger_hurt below
520                                                 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
521                                                 {
522                                                         // Remove dangerous dynamic goals from stack
523                                                         if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
524                                                                 navigation_poproute();
525                                                         // try to stop
526                                                         flatdir = '0 0 0';
527                                                         evadeobstacle = normalize(self.velocity) * -1;
528                                                 }
529                                         }
530                                 }
531                         }
532
533                         dir = flatdir;
534                         evadeobstacle_z = 0;
535                         evadelava_z = 0;
536                         makevectors(self.v_angle_y * '0 1 0');
537
538                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
539                                 self.aistatus |= AI_STATUS_DANGER_AHEAD;
540                 }
541
542                 dodge = havocbot_dodge();
543                 dodge = dodge * bound(0,3+skill*0.1,1);
544                 evadelava = evadelava * bound(1,3-skill,3); //Noobs fear lava a lot and take more distance from it
545                 traceline(self.origin, self.enemy.origin, TRUE, world);
546                 if(trace_ent.classname == "player")
547                         dir = dir * bound(0,skill/7,1);
548
549                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
550         //      self.bot_dodgevector = dir;
551         //      self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
552         }
553
554         if(time < self.ladder_time)
555         {
556                 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
557                 {
558                         if(self.origin_z + self.mins_z  < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
559                                 dir = '0 0 1';
560                 }
561                 else
562                 {
563                         if(self.origin_z + self.mins_z  > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
564                                 dir = '0 0 -1';
565                 }
566         }
567
568         //dir = self.bot_dodgevector;
569         //if (self.bot_dodgevector_jumpbutton)
570         //      self.BUTTON_JUMP = 1;
571         self.movement_x = dir * v_forward * maxspeed;
572         self.movement_y = dir * v_right * maxspeed;
573         self.movement_z = dir * v_up * maxspeed;
574
575         // Emulate keyboard interface
576         if (skill < 10)
577                 havocbot_keyboard_movement(destorg);
578
579         // Bunnyhop!
580 //      if(self.aistatus & AI_STATUS_ROAMING)
581         if(skill >= cvar("bot_ai_bunnyhop_skilloffset"))
582                 havocbot_bunnyhop(dir);
583
584         if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
585         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill)*0.1,1)) self.BUTTON_JUMP=TRUE;
586         if (((dodge * v_up) < 0) && random()*frametime >= 0.5*bound(0,(10-skill)*0.1,1)) self.havocbot_ducktime=time+0.3/bound(0.1,skill,10);
587 };
588
589 .float havocbot_chooseenemy_finished;
590 .float havocbot_stickenemy;
591 void havocbot_chooseenemy()
592 {
593         local entity head, best;
594         local float rating, bestrating;
595         local vector eye, v;
596         if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
597         {
598                 self.enemy = world;
599                 return;
600         }
601         if (self.enemy)
602         {
603                 if (!bot_shouldattack(self.enemy))
604                 {
605                         // enemy died or something, find a new target
606                         self.enemy = world;
607                         self.havocbot_chooseenemy_finished = time;
608                 }
609                 else if (self.havocbot_stickenemy)
610                 {
611                         // tracking last chosen enemy
612                         // if enemy is visible
613                         // and not really really far away
614                         // and we're not severely injured
615                         // then keep tracking for a half second into the future
616                         traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
617                         if (trace_ent == self.enemy || trace_fraction == 1)
618                         if (vlen(self.enemy.origin - self.origin) < 1000)
619                         if (self.health > 30)
620                         {
621                                 // remain tracking him for a shot while (case he went after a small corner or pilar
622                                 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
623                                 return;
624                         }
625                         // enemy isn't visible, or is far away, or we're injured severely
626                         // so stop preferring this enemy
627                         // (it will still take a half second until a new one is chosen)
628                         self.havocbot_stickenemy = 0;
629                 }
630         }
631         if (time < self.havocbot_chooseenemy_finished)
632                 return;
633         self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
634         eye = (self.origin + self.view_ofs);
635         best = world;
636         bestrating = 100000000;
637         head = findchainfloat(bot_attack, TRUE);
638         while (head)
639         {
640                 v = (head.absmin + head.absmax) * 0.5;
641                 rating = vlen(v - eye);
642                 if (bestrating > rating)
643                 if (bot_shouldattack(head))
644                 {
645                         traceline(eye, v, TRUE, self);
646                         if (trace_ent == head || trace_fraction >= 1)
647                         {
648                                 best = head;
649                                 bestrating = rating;
650                         }
651                 }
652                 head = head.chain;
653         }
654         self.enemy = best;
655         self.havocbot_stickenemy = TRUE;
656 };
657
658 .float bot_chooseweapontime;
659 float(entity e) w_getbestweapon;
660 void havocbot_chooseweapon()
661 {
662         // TODO: clean this up by moving it to weapon code
663         if(self.enemy.classname!="player")
664         {
665         //      self.switchweapon = w_getbestweapon(self);
666                 if(client_hasweapon(self, WEP_LASER, TRUE, FALSE))
667                         self.switchweapon = WEP_LASER;
668                 return;
669         }
670
671         local float w, s;
672         local float rocket  ; rocket   =-1000;
673         local float nex     ; nex      =-1000;
674         local float hagar   ; hagar    =-1000;
675         local float grenade ; grenade  =-1000;
676         local float electro ; electro  =-1000;
677         local float crylink ; crylink  =-1000;
678         local float uzi     ; uzi      =-1000;
679         local float shotgun ; shotgun  =-1000;
680         local float campingrifle ; campingrifle  =-1000;
681         local float laser   ; laser    =-1000;
682         local float minstanex ; minstanex =-1000;
683         local float currentscore;
684         local float bestscore; bestscore = 0;
685         local float bestweapon; bestweapon=self.switchweapon;
686         local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
687         local float maxdelaytime=0.5;
688         local float spreadpenalty=10;
689         local float distancefromfloor;
690
691         local float af, ct, combo_time;
692
693         traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
694         distancefromfloor = self.enemy.origin_z - trace_endpos_z;
695
696         af = ATTACK_FINISHED(self);
697         ct = cvar("bot_ai_weapon_combo_threshold");
698
699         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
700         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
701         combo_time = time + ct + (ct * ((-0.3*skill)+3));
702
703         // Custom weapon list based on distance to the enemy
704         local float i; i = 0;
705         if(bot_custom_weapon){
706
707                 // Choose weapons for far distance
708                 if ( distance > bot_distance_far ) {
709                         for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
710                                 w = bot_weapons_far[i];
711                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
712                                         if ( self.weapon == w){
713                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
714                                                         continue;
715                                         } else {
716                                                 self.switchweapon = w;
717                                         }
718                                         return;
719                                 }
720                         }
721                 }
722
723                 // Choose weapons for mid distance
724                 if ( distance > bot_distance_close ) {
725                         for(i= 0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
726                                 w = bot_weapons_mid[i];
727                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
728                                         if ( self.weapon == w){
729                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
730                                                         continue;
731                                         } else {
732                                                 self.switchweapon = w;
733                                         }
734                                         return;
735                                 }
736                         }
737                 }
738
739                 // Choose weapons for close distance
740                 for(i= 0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
741                         w = bot_weapons_close[i];
742                         if ( client_hasweapon(self, w, TRUE, FALSE) ){
743                                 if ( self.weapon == w){
744                                         if( cvar("bot_ai_weapon_combo") && af > combo_time)
745                                                 continue;
746                                 } else {
747                                         self.switchweapon = w;
748                                 }
749                                 return;
750                         }
751                 }
752                 // If now weapon was chosen by this system fall back to the previous one
753         }
754
755         // Formula:
756         //      (Damage/Sec * Weapon spefic change to get that damage)
757         //      *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
758         //      *(Spread change of hit) // if it applies
759         //      *(Penality for target beeing in air)
760         // %weaponaddpoint
761         if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
762                 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
763                         * (0.5);
764
765         if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE)  &&
766                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER &&
767                         af > combo_time
768                 )
769         )
770                 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
771                         * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
772
773         if (client_hasweapon(self, WEP_NEX, TRUE, FALSE)  &&
774                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX &&
775                         af > combo_time
776                 )
777         )
778                 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
779                         * (0.5);
780
781         if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // &&
782         //      !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR &&  time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
783                 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
784                         * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
785
786         if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) &&
787                 !(
788                         cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER &&
789                         af > combo_time
790                 )
791         )
792                 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
793                         * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
794
795         if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) &&
796                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO &&
797                         af > combo_time
798                 )
799         )
800                 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
801                         * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
802
803         if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // &&
804         //      !( self.weapon == WEP_CRYLINK &&  time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
805                 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
806                         * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
807
808         if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // &&
809         //      !( self.weapon == WEP_UZI &&  time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
810                 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
811                         * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
812
813         if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) &&
814                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN &&
815                         af > combo_time
816                 )
817         )
818                 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
819                         * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
820
821         if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) &&
822                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER &&
823                         af > combo_time
824                 )
825         )
826                 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
827                         * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
828
829         if((self.enemy.flags & FL_ONGROUND)==FALSE){
830                 rocket = rocket   * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius"         ),0.9)); //slight bigger change
831                 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
832                 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius"        ),0.95));
833                 laser = laser     * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius"                  ),0.95));
834         }
835         /*
836         dprint("Floor distance: ",ftos(distancefromfloor),"\n");
837         dprint("Rocket: " , ftos(rocket  ), "\n");
838         dprint("Nex: "    , ftos(nex     ), "\n");
839         dprint("Hagar: "  , ftos(hagar   ), "\n");
840         dprint("Grenade: ", ftos(grenade ), "\n");
841         dprint("Electro: ", ftos(electro ), "\n");
842         dprint("Crylink: ", ftos(crylink ), "\n");
843         dprint("Uzi: "    , ftos(uzi     ), "\n");
844         dprint("Shotgun :", ftos(shotgun ), "\n");
845         dprint("Laser   :", ftos(laser   ), "\n\n");
846         */
847         currentscore = -1;
848         w = WEP_MINSTANEX        ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
849         w = WEP_ROCKET_LAUNCHER  ;s = rocket   ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
850         w = WEP_NEX              ;s = nex      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
851         w = WEP_HAGAR            ;s = hagar    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
852         w = WEP_GRENADE_LAUNCHER ;s = grenade  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
853         w = WEP_ELECTRO          ;s = electro  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
854         w = WEP_CRYLINK          ;s = crylink  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
855         w = WEP_UZI              ;s = uzi      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
856         w = WEP_SHOTGUN          ;s = shotgun  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
857         w = WEP_LASER            ;s = laser    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
858
859         // switch if the best weapon would provide a significant damage increase
860         if (bestscore > currentscore*1.5){
861                 self.switchweapon = bestweapon;
862
863                 // buys time for detonating the rocket. not tested yet
864                 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
865                         self.bot_chooseweapontime += (distance  / cvar("g_balance_rocketlauncher_speed"));
866         }
867 };
868
869 .float nextaim;
870 void havocbot_aim()
871 {
872         local vector selfvel, enemyvel;
873 //      if(self.flags & FL_INWATER)
874 //              return;
875         if (time < self.nextaim)
876                 return;
877         self.nextaim = time + 0.1;
878         selfvel = self.velocity;
879         if (!self.waterlevel)
880                 selfvel_z = 0;
881         if (self.enemy)
882         {
883                 enemyvel = self.enemy.velocity;
884                 if (!self.enemy.waterlevel)
885                         enemyvel_z = 0;
886                 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
887         }
888         else
889                 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
890 };
891
892 void havocbot_ai()
893 {
894         if (bot_strategytoken == self)
895         if (!bot_strategytoken_taken)
896         {
897                 self.havocbot_role();
898
899                 // TODO: tracewalk() should take care of this job (better path finding under water)
900                 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
901                 if(self.goalcurrent==world)
902                 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
903                 {
904                         // Look for the closest waypoint out of water
905                         local entity newgoal, head;
906                         local float bestdistance, distance;
907
908                         newgoal = world;
909                         bestdistance = 10000;
910                         for (head = findchain(classname, "waypoint"); head; head = head.chain)
911                         {
912                                 distance = vlen(head.origin - self.origin);
913                                 if(distance>10000)
914                                         continue;
915
916                                 if(head.origin_z < self.origin_z)
917                                         continue;
918
919                                 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
920                                         continue;
921
922                                 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
923                                         continue;
924
925                                 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
926
927                                 if(trace_fraction<1)
928                                         continue;
929
930                                 if(distance<bestdistance)
931                                 {
932                                         newgoal = head;
933                                         bestdistance = distance;
934                                 }
935                         }
936
937                         if(newgoal)
938                         {
939                         //      te_wizspike(newgoal.origin);
940                                 navigation_pushroute(newgoal);
941                         }
942                 }
943
944                 // token has been used this frame
945                 bot_strategytoken_taken = TRUE;
946         }
947         havocbot_chooseenemy();
948         if (self.bot_chooseweapontime < time )
949         {
950                 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
951                 havocbot_chooseweapon();
952         }
953         havocbot_aim();
954         lag_update();
955         if (self.bot_aimtarg)
956         {
957                 self.aistatus |= AI_STATUS_ATTACKING;
958                 self.aistatus &~= AI_STATUS_ROAMING;
959
960                 weapon_action(self.weapon, WR_AIM);
961                 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
962                 {
963                         self.BUTTON_ATCK = FALSE;
964                         self.BUTTON_ATCK2 = FALSE;
965                 }
966         }
967         else if (self.goalcurrent)
968         {
969                 self.aistatus |= AI_STATUS_ROAMING;
970                 self.aistatus &~= AI_STATUS_ATTACKING;
971
972                 local vector now,v,next;//,heading;
973                 local float aimdistance,skillblend,distanceblend,blend;
974                 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
975                 aimdistance = vlen(now);
976                 //heading = self.velocity;
977                 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
978                 if(self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0)
979                         next = self.goalstack01.origin - (self.origin + self.view_ofs);
980                 skillblend=bound(0,(skill-2.5)*0.5,1); //lower skill player can't preturn
981                 distanceblend=bound(0,aimdistance/cvar("bot_ai_keyboard_distance"),1);
982                 blend = skillblend * (1-distanceblend);
983                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
984                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
985                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
986                 v = now + blend * (next - now);
987                 //dprint(etos(self), " ");
988                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
989                 //v = now * (distanceblend) + next * (1-distanceblend);
990                 if (self.waterlevel < WATERLEVEL_SWIMMING)
991                         v_z = 0;
992                 //dprint("walk at:", vtos(v), "\n");
993                 //te_lightning2(world, self.origin, self.goalcurrent.origin);
994                 bot_aimdir(v, -1);
995         }
996         havocbot_movetogoal();
997 };
998
999 void havocbot_setupbot()
1000 {
1001         self.bot_ai = havocbot_ai;
1002         // will be updated by think code
1003         //Generate some random skill levels
1004         self.havocbot_keyboardskill=random()-0.5;
1005         havocbot_chooserole();
1006 }
1007
1008
1009 #ifdef DEBUG_BOT_GOALSTACK
1010
1011 .float goalcounter;
1012 .vector lastposition;
1013
1014 // Debug the goal stack visually
1015 void debuggoalstack()
1016 {
1017         local entity target;
1018         local vector org;
1019
1020         if(self.goalcounter==0)target=self.goalcurrent;
1021         else if(self.goalcounter==1)target=self.goalstack01;
1022         else if(self.goalcounter==2)target=self.goalstack02;
1023         else if(self.goalcounter==3)target=self.goalstack03;
1024         else if(self.goalcounter==4)target=self.goalstack04;
1025         else if(self.goalcounter==5)target=self.goalstack05;
1026         else if(self.goalcounter==6)target=self.goalstack06;
1027         else if(self.goalcounter==7)target=self.goalstack07;
1028         else if(self.goalcounter==8)target=self.goalstack08;
1029         else if(self.goalcounter==9)target=self.goalstack09;
1030         else if(self.goalcounter==10)target=self.goalstack10;
1031         else if(self.goalcounter==11)target=self.goalstack11;
1032         else if(self.goalcounter==12)target=self.goalstack12;
1033         else if(self.goalcounter==13)target=self.goalstack13;
1034         else if(self.goalcounter==14)target=self.goalstack14;
1035         else if(self.goalcounter==15)target=self.goalstack15;
1036         else if(self.goalcounter==16)target=self.goalstack16;
1037         else if(self.goalcounter==17)target=self.goalstack17;
1038         else if(self.goalcounter==18)target=self.goalstack18;
1039         else if(self.goalcounter==19)target=self.goalstack19;
1040         else if(self.goalcounter==20)target=self.goalstack20;
1041         else if(self.goalcounter==21)target=self.goalstack21;
1042         else if(self.goalcounter==22)target=self.goalstack22;
1043         else if(self.goalcounter==23)target=self.goalstack23;
1044         else if(self.goalcounter==24)target=self.goalstack24;
1045         else if(self.goalcounter==25)target=self.goalstack25;
1046         else if(self.goalcounter==26)target=self.goalstack26;
1047         else if(self.goalcounter==27)target=self.goalstack27;
1048         else if(self.goalcounter==28)target=self.goalstack28;
1049         else if(self.goalcounter==29)target=self.goalstack29;
1050         else if(self.goalcounter==30)target=self.goalstack30;
1051         else if(self.goalcounter==31)target=self.goalstack31;
1052
1053         if(target==world)
1054         {
1055                 self.goalcounter = 0;
1056                 self.lastposition='0 0 0';
1057                 return;
1058         }
1059
1060         if(self.lastposition=='0 0 0')
1061                 org = self.origin;
1062         else
1063                 org = self.lastposition;
1064
1065
1066         te_lightning2(world, org, target.origin);
1067         self.lastposition = target.origin;
1068
1069         self.goalcounter++;
1070 }
1071
1072 #endif