Merge branch 'master' into Mario/weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / havocbot / havocbot.qc
1 #include "havocbot.qh"
2 #include "role_onslaught.qc"
3 #include "role_keyhunt.qc"
4 #include "roles.qc"
5
6 void havocbot_ai()
7 {
8         if(self.draggedby)
9                 return;
10
11         if(bot_execute_commands())
12                 return;
13
14         if (bot_strategytoken == self)
15         if (!bot_strategytoken_taken)
16         {
17                 if(self.havocbot_blockhead)
18                 {
19                         self.havocbot_blockhead = FALSE;
20                 }
21                 else
22                 {
23                         if (!self.jumppadcount)
24                                 self.havocbot_role();
25                 }
26
27                 // TODO: tracewalk() should take care of this job (better path finding under water)
28                 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
29                 if(self.deadflag != DEAD_NO)
30                 if(self.goalcurrent==world)
31                 if(self.waterlevel==WATERLEVEL_SWIMMING || (self.aistatus & AI_STATUS_OUT_WATER))
32                 {
33                         // Look for the closest waypoint out of water
34                         entity newgoal, head;
35                         float bestdistance, distance;
36
37                         newgoal = world;
38                         bestdistance = 10000;
39                         for (head = findchain(classname, "waypoint"); head; head = head.chain)
40                         {
41                                 distance = vlen(head.origin - self.origin);
42                                 if(distance>10000)
43                                         continue;
44
45                                 if(head.origin_z < self.origin_z)
46                                         continue;
47
48                                 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
49                                         continue;
50
51                                 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
52                                         continue;
53
54                                 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
55
56                                 if(trace_fraction<1)
57                                         continue;
58
59                                 if(distance<bestdistance)
60                                 {
61                                         newgoal = head;
62                                         bestdistance = distance;
63                                 }
64                         }
65
66                         if(newgoal)
67                         {
68                         //      te_wizspike(newgoal.origin);
69                                 navigation_pushroute(newgoal);
70                         }
71                 }
72
73                 // token has been used this frame
74                 bot_strategytoken_taken = TRUE;
75         }
76
77         if(self.deadflag != DEAD_NO)
78                 return;
79
80         havocbot_chooseenemy();
81         if (self.bot_chooseweapontime < time )
82         {
83                 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
84                 havocbot_chooseweapon();
85         }
86         havocbot_aim();
87         lag_update();
88         if (self.bot_aimtarg)
89         {
90                 self.aistatus |= AI_STATUS_ATTACKING;
91                 self.aistatus &= ~AI_STATUS_ROAMING;
92
93                 if(self.weapons)
94                 {
95                         WEP_ACTION(self.weapon, WR_AIM);
96                         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
97                         {
98                                 self.BUTTON_ATCK = FALSE;
99                                 self.BUTTON_ATCK2 = FALSE;
100                         }
101                         else
102                         {
103                                 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
104                                         self.lastfiredweapon = self.weapon;
105                         }
106                 }
107                 else
108                 {
109                         if(IS_PLAYER(self.bot_aimtarg))
110                                 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
111                 }
112         }
113         else if (self.goalcurrent)
114         {
115                 self.aistatus |= AI_STATUS_ROAMING;
116                 self.aistatus &= ~AI_STATUS_ATTACKING;
117
118                 vector now,v,next;//,heading;
119                 float aimdistance,skillblend,distanceblend,blend;
120                 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
121                 aimdistance = vlen(now);
122                 //heading = self.velocity;
123                 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
124                 if(
125                         self.goalstack01 != self && self.goalstack01 != world && ((self.aistatus & AI_STATUS_RUNNING) == 0) &&
126                         !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
127                 )
128                         next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
129
130                 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
131                 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
132                 blend = skillblend * (1-distanceblend);
133                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
134                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
135                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
136                 v = now + blend * (next - now);
137                 //dprint(etos(self), " ");
138                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
139                 //v = now * (distanceblend) + next * (1-distanceblend);
140                 if (self.waterlevel < WATERLEVEL_SWIMMING)
141                         v_z = 0;
142                 //dprint("walk at:", vtos(v), "\n");
143                 //te_lightning2(world, self.origin, self.goalcurrent.origin);
144                 bot_aimdir(v, -1);
145         }
146         havocbot_movetogoal();
147
148         // if the bot is not attacking, consider reloading weapons
149         if (!(self.aistatus & AI_STATUS_ATTACKING))
150         {
151                 float i;
152                 entity e;
153
154                 // we are currently holding a weapon that's not fully loaded, reload it
155                 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
156                 if(self.clip_load < self.clip_size)
157                         self.impulse = 20; // "press" the reload button, not sure if this is done right
158
159                 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
160                 // the code above executes next frame, starting the reloading then
161                 if(skill >= 5) // bots can only look for unloaded weapons past this skill
162                 if(self.clip_load >= 0) // only if we're not reloading a weapon already
163                 {
164                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
165                         {
166                                 e = get_weaponinfo(i);
167                                 if ((self.weapons & WepSet_FromWeapon(i)) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < e.reloading_ammo))
168                                         self.switchweapon = i;
169                         }
170                 }
171         }
172 }
173
174 void havocbot_keyboard_movement(vector destorg)
175 {
176         vector keyboard;
177         float blend, maxspeed;
178         float sk;
179
180         sk = skill + self.bot_moveskill;
181
182         maxspeed = autocvar_sv_maxspeed;
183
184         if (time < self.havocbot_keyboardtime)
185                 return;
186
187         self.havocbot_keyboardtime =
188                 max(
189                         self.havocbot_keyboardtime
190                                 + 0.05/max(1, sk+self.havocbot_keyboardskill)
191                                 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
192                 , time);
193         keyboard = self.movement * (1.0 / maxspeed);
194
195         float trigger, trigger1;
196         blend = bound(0,sk*0.1,1);
197         trigger = autocvar_bot_ai_keyboard_threshold;
198         trigger1 = 0 - trigger;
199
200         // categorize forward movement
201         // at skill < 1.5 only forward
202         // at skill < 2.5 only individual directions
203         // at skill < 4.5 only individual directions, and forward diagonals
204         // at skill >= 4.5, all cases allowed
205         if (keyboard_x > trigger)
206         {
207                 keyboard_x = 1;
208                 if (sk < 2.5)
209                         keyboard_y = 0;
210         }
211         else if (keyboard_x < trigger1 && sk > 1.5)
212         {
213                 keyboard_x = -1;
214                 if (sk < 4.5)
215                         keyboard_y = 0;
216         }
217         else
218         {
219                 keyboard_x = 0;
220                 if (sk < 1.5)
221                         keyboard_y = 0;
222         }
223         if (sk < 4.5)
224                 keyboard_z = 0;
225
226         if (keyboard_y > trigger)
227                 keyboard_y = 1;
228         else if (keyboard_y < trigger1)
229                 keyboard_y = -1;
230         else
231                 keyboard_y = 0;
232
233         if (keyboard_z > trigger)
234                 keyboard_z = 1;
235         else if (keyboard_z < trigger1)
236                 keyboard_z = -1;
237         else
238                 keyboard_z = 0;
239
240         self.havocbot_keyboard = keyboard * maxspeed;
241         if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
242
243         keyboard = self.havocbot_keyboard;
244         blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
245         //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
246         self.movement = self.movement + (keyboard - self.movement) * blend;
247 }
248
249 void havocbot_bunnyhop(vector dir)
250 {
251         float bunnyhopdistance;
252         vector deviation;
253         float maxspeed;
254         vector gco, gno;
255
256         // Don't jump when attacking
257         if(self.aistatus & AI_STATUS_ATTACKING)
258                 return;
259
260         if(IS_PLAYER(self.goalcurrent))
261                 return;
262
263         maxspeed = autocvar_sv_maxspeed;
264
265         if(self.aistatus & AI_STATUS_DANGER_AHEAD)
266         {
267                 self.aistatus &= ~AI_STATUS_RUNNING;
268                 self.BUTTON_JUMP = FALSE;
269                 self.bot_canruntogoal = 0;
270                 self.bot_timelastseengoal = 0;
271                 return;
272         }
273
274         if(self.waterlevel > WATERLEVEL_WETFEET)
275         {
276                 self.aistatus &= ~AI_STATUS_RUNNING;
277                 return;
278         }
279
280         if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
281         {
282                 self.bot_canruntogoal = 0;
283                 self.bot_timelastseengoal = 0;
284         }
285
286         gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
287         bunnyhopdistance = vlen(self.origin - gco);
288
289         // Run only to visible goals
290         if(self.flags & FL_ONGROUND)
291         if(self.speed==maxspeed)
292         if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
293         {
294                         self.bot_lastseengoal = self.goalcurrent;
295
296                         // seen it before
297                         if(self.bot_timelastseengoal)
298                         {
299                                 // for a period of time
300                                 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
301                                 {
302                                         float checkdistance;
303                                         checkdistance = TRUE;
304
305                                         // don't run if it is too close
306                                         if(self.bot_canruntogoal==0)
307                                         {
308                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
309                                                         self.bot_canruntogoal = 1;
310                                                 else
311                                                         self.bot_canruntogoal = -1;
312                                         }
313
314                                         if(self.bot_canruntogoal != 1)
315                                                 return;
316
317                                         if(self.aistatus & AI_STATUS_ROAMING)
318                                         if(self.goalcurrent.classname=="waypoint")
319                                         if (!(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
320                                         if(fabs(gco_z - self.origin_z) < self.maxs_z - self.mins_z)
321                                         if(self.goalstack01!=world)
322                                         {
323                                                 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
324                                                 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
325                                                 while (deviation_y < -180) deviation_y = deviation_y + 360;
326                                                 while (deviation_y > 180) deviation_y = deviation_y - 360;
327
328                                                 if(fabs(deviation_y) < 20)
329                                                 if(bunnyhopdistance < vlen(self.origin - gno))
330                                                 if(fabs(gno_z - gco_z) < self.maxs_z - self.mins_z)
331                                                 {
332                                                         if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
333                                                         if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
334                                                         {
335                                                                 checkdistance = FALSE;
336                                                         }
337                                                 }
338                                         }
339
340                                         if(checkdistance)
341                                         {
342                                                 self.aistatus &= ~AI_STATUS_RUNNING;
343                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
344                                                         self.BUTTON_JUMP = TRUE;
345                                         }
346                                         else
347                                         {
348                                                 self.aistatus |= AI_STATUS_RUNNING;
349                                                 self.BUTTON_JUMP = TRUE;
350                                         }
351                                 }
352                         }
353                         else
354                         {
355                                 self.bot_timelastseengoal = time;
356                         }
357         }
358         else
359         {
360                 self.bot_timelastseengoal = 0;
361         }
362
363 #if 0
364         // Release jump button
365         if(!cvar("sv_pogostick"))
366         if((self.flags & FL_ONGROUND) == 0)
367         {
368                 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
369                         self.BUTTON_JUMP = FALSE;
370
371                 // Strafe
372                 if(self.aistatus & AI_STATUS_RUNNING)
373                 if(vlen(self.velocity)>maxspeed)
374                 {
375                         deviation = vectoangles(dir) - vectoangles(self.velocity);
376                         while (deviation_y < -180) deviation_y = deviation_y + 360;
377                         while (deviation_y > 180) deviation_y = deviation_y - 360;
378
379                         if(fabs(deviation_y)>10)
380                                 self.movement_x = 0;
381
382                         if(deviation_y>10)
383                                 self.movement_y = maxspeed * -1;
384                         else if(deviation_y<10)
385                                 self.movement_y = maxspeed;
386
387                 }
388         }
389 #endif
390 }
391
392 void havocbot_movetogoal()
393 {
394         vector destorg;
395         vector diff;
396         vector dir;
397         vector flatdir;
398         vector m1;
399         vector m2;
400         vector evadeobstacle;
401         vector evadelava;
402         float s;
403         float maxspeed;
404         vector gco;
405         //float dist;
406         vector dodge;
407         //if (self.goalentity)
408         //      te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
409         self.movement = '0 0 0';
410         maxspeed = autocvar_sv_maxspeed;
411
412         // Jetpack navigation
413         if(self.goalcurrent)
414         if(self.navigation_jetpack_goal)
415         if(self.goalcurrent==self.navigation_jetpack_goal)
416         if(self.ammo_fuel)
417         {
418                 if(autocvar_bot_debug_goalstack)
419                 {
420                         debuggoalstack();
421                         te_wizspike(self.navigation_jetpack_point);
422                 }
423
424                 // Take off
425                 if (!(self.aistatus & AI_STATUS_JETPACK_FLYING))
426                 {
427                         // Brake almost completely so it can get a good direction
428                         if(vlen(self.velocity)>10)
429                                 return;
430                         self.aistatus |= AI_STATUS_JETPACK_FLYING;
431                 }
432
433                 makevectors(self.v_angle_y * '0 1 0');
434                 dir = normalize(self.navigation_jetpack_point - self.origin);
435
436                 // Landing
437                 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
438                 {
439                         // Calculate brake distance in xy
440                         float db, v, d;
441                         vector dxy;
442
443                         dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy_z = 0;
444                         d = vlen(dxy);
445                         v = vlen(self.velocity -  self.velocity_z * '0 0 1');
446                         db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
447                 //      dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
448                         if(d < db || d < 500)
449                         {
450                                 // Brake
451                                 if(fabs(self.velocity_x)>maxspeed*0.3)
452                                 {
453                                         self.movement_x = dir * v_forward * -maxspeed;
454                                         return;
455                                 }
456                                 // Switch to normal mode
457                                 self.navigation_jetpack_goal = world;
458                                 self.aistatus &= ~AI_STATUS_JETPACK_LANDING;
459                                 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
460                                 return;
461                         }
462                 }
463                 else if(checkpvs(self.origin,self.goalcurrent))
464                 {
465                         // If I can see the goal switch to landing code
466                         self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
467                         self.aistatus |= AI_STATUS_JETPACK_LANDING;
468                         return;
469                 }
470
471                 // Flying
472                 self.BUTTON_HOOK = TRUE;
473                 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
474                 {
475                         self.movement_x = dir * v_forward * maxspeed;
476                         self.movement_y = dir * v_right * maxspeed;
477                 }
478                 return;
479         }
480
481         // Handling of jump pads
482         if(self.jumppadcount)
483         {
484                 // If got stuck on the jump pad try to reach the farthest visible waypoint
485                 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
486                 {
487                         if(fabs(self.velocity_z)<50)
488                         {
489                                 entity head, newgoal = world;
490                                 float distance, bestdistance = 0;
491
492                                 for (head = findchain(classname, "waypoint"); head; head = head.chain)
493                                 {
494
495                                         distance = vlen(head.origin - self.origin);
496                                         if(distance>1000)
497                                                 continue;
498
499                                         traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), TRUE, world);
500
501                                         if(trace_fraction<1)
502                                                 continue;
503
504                                         if(distance>bestdistance)
505                                         {
506                                                 newgoal = head;
507                                                 bestdistance = distance;
508                                         }
509                                 }
510
511                                 if(newgoal)
512                                 {
513                                         self.ignoregoal = self.goalcurrent;
514                                         self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
515                                         navigation_clearroute();
516                                         navigation_routetogoal(newgoal, self.origin);
517                                         self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
518                                 }
519                         }
520                         else
521                                 return;
522                 }
523                 else
524                 {
525                         if(self.velocity_z>0)
526                         {
527                                 float threshold, sxy;
528                                 vector velxy = self.velocity; velxy_z = 0;
529                                 sxy = vlen(velxy);
530                                 threshold = maxspeed * 0.2;
531                                 if(sxy < threshold)
532                                 {
533                                         dprint("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
534                                         self.aistatus |= AI_STATUS_OUT_JUMPPAD;
535                                 }
536                                 return;
537                         }
538
539                         // Don't chase players while using a jump pad
540                         if(IS_PLAYER(self.goalcurrent) || IS_PLAYER(self.goalstack01))
541                                 return;
542                 }
543         }
544         else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
545                 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
546
547         // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
548         if(skill>6)
549         if (!(self.flags & FL_ONGROUND))
550         {
551                 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
552                 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
553                 if(self.items & IT_JETPACK)
554                 {
555                         tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
556                         if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
557                         {
558                                 if(self.velocity_z<0)
559                                 {
560                                         self.BUTTON_HOOK = TRUE;
561                                 }
562                         }
563                         else
564                                 self.BUTTON_HOOK = TRUE;
565
566                         // If there is no goal try to move forward
567
568                         if(self.goalcurrent==world)
569                                 dir = v_forward;
570                         else
571                                 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
572
573                         vector xyvelocity = self.velocity; xyvelocity_z = 0;
574                         float xyspeed = xyvelocity * dir;
575
576                         if(xyspeed < (maxspeed / 2))
577                         {
578                                 makevectors(self.v_angle_y * '0 1 0');
579                                 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
580                                 if(trace_fraction==1)
581                                 {
582                                         self.movement_x = dir * v_forward * maxspeed;
583                                         self.movement_y = dir * v_right * maxspeed;
584                                         if (skill < 10)
585                                                 havocbot_keyboard_movement(self.origin + dir * 100);
586                                 }
587                         }
588
589                         self.havocbot_blockhead = TRUE;
590
591                         return;
592                 }
593                 else if(self.health>WEP_CVAR(devastator, damage)*0.5)
594                 {
595                         if(self.velocity_z < 0)
596                         if(client_hasweapon(self, WEP_DEVASTATOR, TRUE, FALSE))
597                         {
598                                 self.movement_x = maxspeed;
599
600                                 if(self.rocketjumptime)
601                                 {
602                                         if(time > self.rocketjumptime)
603                                         {
604                                                 self.BUTTON_ATCK2 = TRUE;
605                                                 self.rocketjumptime = 0;
606                                         }
607                                         return;
608                                 }
609
610                                 self.switchweapon = WEP_DEVASTATOR;
611                                 self.v_angle_x = 90;
612                                 self.BUTTON_ATCK = TRUE;
613                                 self.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
614                                 return;
615                         }
616                 }
617                 else
618                 {
619                         // If there is no goal try to move forward
620                         if(self.goalcurrent==world)
621                                 self.movement_x = maxspeed;
622                 }
623         }
624
625         // If we are under water with no goals, swim up
626         if(self.waterlevel)
627         if(self.goalcurrent==world)
628         {
629                 dir = '0 0 0';
630                 if(self.waterlevel>WATERLEVEL_SWIMMING)
631                         dir_z = 1;
632                 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
633                         self.BUTTON_JUMP = TRUE;
634                 else
635                         self.BUTTON_JUMP = FALSE;
636                 makevectors(self.v_angle_y * '0 1 0');
637                 self.movement_x = dir * v_forward * maxspeed;
638                 self.movement_y = dir * v_right * maxspeed;
639                 self.movement_z = dir * v_up * maxspeed;
640         }
641
642         // if there is nowhere to go, exit
643         if (self.goalcurrent == world)
644                 return;
645
646         if (self.goalcurrent)
647                 navigation_poptouchedgoals();
648
649         // if ran out of goals try to use an alternative goal or get a new strategy asap
650         if(self.goalcurrent == world)
651         {
652                 self.bot_strategytime = 0;
653                 return;
654         }
655
656
657         if(autocvar_bot_debug_goalstack)
658                 debuggoalstack();
659
660         m1 = self.goalcurrent.origin + self.goalcurrent.mins;
661         m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
662         destorg = self.origin;
663         destorg_x = bound(m1_x, destorg_x, m2_x);
664         destorg_y = bound(m1_y, destorg_y, m2_y);
665         destorg_z = bound(m1_z, destorg_z, m2_z);
666         diff = destorg - self.origin;
667         //dist = vlen(diff);
668         dir = normalize(diff);
669         flatdir = diff;flatdir_z = 0;
670         flatdir = normalize(flatdir);
671         gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
672
673         //if (self.bot_dodgevector_time < time)
674         {
675         //      self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
676         //      self.bot_dodgevector_jumpbutton = 1;
677                 evadeobstacle = '0 0 0';
678                 evadelava = '0 0 0';
679
680                 if (self.waterlevel)
681                 {
682                         if(self.waterlevel>WATERLEVEL_SWIMMING)
683                         {
684                         //      flatdir_z = 1;
685                                 self.aistatus |= AI_STATUS_OUT_WATER;
686                         }
687                         else
688                         {
689                                 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && gco_z < self.origin_z) &&
690                                         ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
691                                         self.BUTTON_JUMP = TRUE;
692                                 else
693                                         self.BUTTON_JUMP = FALSE;
694                         }
695                         dir = normalize(flatdir);
696                         makevectors(self.v_angle_y * '0 1 0');
697                 }
698                 else
699                 {
700                         if(self.aistatus & AI_STATUS_OUT_WATER)
701                                 self.aistatus &= ~AI_STATUS_OUT_WATER;
702
703                         // jump if going toward an obstacle that doesn't look like stairs we
704                         // can walk up directly
705                         tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
706                         if (trace_fraction < 1)
707                         if (trace_plane_normal_z < 0.7)
708                         {
709                                 s = trace_fraction;
710                                 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, FALSE, self);
711                                 if (trace_fraction < s + 0.01)
712                                 if (trace_plane_normal_z < 0.7)
713                                 {
714                                         s = trace_fraction;
715                                         tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, FALSE, self);
716                                         if (trace_fraction > s)
717                                                 self.BUTTON_JUMP = 1;
718                                 }
719                         }
720
721                         // avoiding dangers and obstacles
722                         vector dst_ahead, dst_down;
723                         makevectors(self.v_angle_y * '0 1 0');
724                         dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
725                         dst_down = dst_ahead - '0 0 1500';
726
727                         // Look ahead
728                         traceline(self.origin + self.view_ofs, dst_ahead, TRUE, world);
729
730                         // Check head-banging against walls
731                         if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
732                         {
733                                 self.BUTTON_JUMP = TRUE;
734                                 if(self.facingwalltime && time > self.facingwalltime)
735                                 {
736                                         self.ignoregoal = self.goalcurrent;
737                                         self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
738                                         self.bot_strategytime = 0;
739                                         return;
740                                 }
741                                 else
742                                 {
743                                         self.facingwalltime = time + 0.05;
744                                 }
745                         }
746                         else
747                         {
748                                 self.facingwalltime = 0;
749
750                                 if(self.ignoregoal != world && time > self.ignoregoaltime)
751                                 {
752                                         self.ignoregoal = world;
753                                         self.ignoregoaltime = 0;
754                                 }
755                         }
756
757                         // Check for water/slime/lava and dangerous edges
758                         // (only when the bot is on the ground or jumping intentionally)
759                         self.aistatus &= ~AI_STATUS_DANGER_AHEAD;
760
761                         if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
762                         if((self.flags & FL_ONGROUND) || (self.aistatus & AI_STATUS_RUNNING) || self.BUTTON_JUMP == TRUE)
763                         {
764                                 // Look downwards
765                                 traceline(dst_ahead , dst_down, TRUE, world);
766                         //      te_lightning2(world, self.origin, dst_ahead);   // Draw "ahead" look
767                         //      te_lightning2(world, dst_ahead, dst_down);              // Draw "downwards" look
768                                 if(trace_endpos_z < self.origin_z + self.mins_z)
769                                 {
770                                         s = pointcontents(trace_endpos + '0 0 1');
771                                         if (s != CONTENT_SOLID)
772                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
773                                                 evadelava = normalize(self.velocity) * -1;
774                                         else if (s == CONTENT_SKY)
775                                                 evadeobstacle = normalize(self.velocity) * -1;
776                                         else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
777                                                                 self.goalcurrent.absmin, self.goalcurrent.absmax))
778                                         {
779                                                 // if ain't a safe goal with "holes" (like the jumpad on soylent)
780                                                 // and there is a trigger_hurt below
781                                                 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
782                                                 {
783                                                         // Remove dangerous dynamic goals from stack
784                                                         dprint("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
785                                                         navigation_clearroute();
786                                                         return;
787                                                 }
788                                         }
789                                 }
790                         }
791
792                         dir = flatdir;
793                         evadeobstacle_z = 0;
794                         evadelava_z = 0;
795                         makevectors(self.v_angle_y * '0 1 0');
796
797                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
798                                 self.aistatus |= AI_STATUS_DANGER_AHEAD;
799                 }
800
801                 dodge = havocbot_dodge();
802                 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
803                 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
804                 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), TRUE, world);
805                 if(IS_PLAYER(trace_ent))
806                         dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
807
808                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
809         //      self.bot_dodgevector = dir;
810         //      self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
811         }
812
813         if(time < self.ladder_time)
814         {
815                 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
816                 {
817                         if(self.origin_z + self.mins_z  < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
818                                 dir_z = 1;
819                 }
820                 else
821                 {
822                         if(self.origin_z + self.mins_z  > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
823                                 dir_z = -1;
824                 }
825         }
826
827         //dir = self.bot_dodgevector;
828         //if (self.bot_dodgevector_jumpbutton)
829         //      self.BUTTON_JUMP = 1;
830         self.movement_x = dir * v_forward * maxspeed;
831         self.movement_y = dir * v_right * maxspeed;
832         self.movement_z = dir * v_up * maxspeed;
833
834         // Emulate keyboard interface
835         if (skill < 10)
836                 havocbot_keyboard_movement(destorg);
837
838         // Bunnyhop!
839 //      if(self.aistatus & AI_STATUS_ROAMING)
840         if(self.goalcurrent)
841         if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
842                 havocbot_bunnyhop(dir);
843
844         if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
845         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
846         if (((dodge * v_up) < 0) && random()*frametime >= 0.5*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.havocbot_ducktime=time+0.3/bound(0.1,skill+self.bot_dodgeskill,10);
847 }
848
849 void havocbot_chooseenemy()
850 {
851         entity head, best, head2;
852         float rating, bestrating, i, hf;
853         vector eye, v;
854         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
855         {
856                 self.enemy = world;
857                 return;
858         }
859         if (self.enemy)
860         {
861                 if (!bot_shouldattack(self.enemy))
862                 {
863                         // enemy died or something, find a new target
864                         self.enemy = world;
865                         self.havocbot_chooseenemy_finished = time;
866                 }
867                 else if (self.havocbot_stickenemy)
868                 {
869                         // tracking last chosen enemy
870                         // if enemy is visible
871                         // and not really really far away
872                         // and we're not severely injured
873                         // then keep tracking for a half second into the future
874                         traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,FALSE,world);
875                         if (trace_ent == self.enemy || trace_fraction == 1)
876                         if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
877                         if (self.health > 30)
878                         {
879                                 // remain tracking him for a shot while (case he went after a small corner or pilar
880                                 self.havocbot_chooseenemy_finished = time + 0.5;
881                                 return;
882                         }
883                         // enemy isn't visible, or is far away, or we're injured severely
884                         // so stop preferring this enemy
885                         // (it will still take a half second until a new one is chosen)
886                         self.havocbot_stickenemy = 0;
887                 }
888         }
889         if (time < self.havocbot_chooseenemy_finished)
890                 return;
891         self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
892         eye = self.origin + self.view_ofs;
893         best = world;
894         bestrating = 100000000;
895         head = head2 = findchainfloat(bot_attack, TRUE);
896
897         // Backup hit flags
898         hf = self.dphitcontentsmask;
899
900         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
901
902         self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
903
904         for(i = 0; ; ++i)
905         {
906                 while (head)
907                 {
908                         v = (head.absmin + head.absmax) * 0.5;
909                         rating = vlen(v - eye);
910                         if (rating<autocvar_bot_ai_enemydetectionradius)
911                         if (bestrating > rating)
912                         if (bot_shouldattack(head))
913                         {
914                                 traceline(eye, v, TRUE, self);
915                                 if (trace_ent == head || trace_fraction >= 1)
916                                 {
917                                         best = head;
918                                         bestrating = rating;
919                                 }
920                         }
921                         head = head.chain;
922                 }
923
924                 // I want to do a second scan if no enemy was found or I don't have weapons
925                 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
926                 if(best || self.weapons) // || self.weapon == WEP_RIFLE
927                         break;
928                 if(i)
929                         break;
930
931                 // Set flags to see through transparent objects
932                 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
933
934                 head = head2;
935         }
936
937         // Restore hit flags
938         self.dphitcontentsmask = hf;
939
940         self.enemy = best;
941         self.havocbot_stickenemy = TRUE;
942 }
943
944 float havocbot_chooseweapon_checkreload(float new_weapon)
945 {
946         // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
947         // so skip this for them, or they'll never get to reload their weapons at all.
948         // this also allows bots under this skill to be more stupid, and reload more often during combat :)
949         if(skill < 5)
950                 return FALSE;
951
952         // if this weapon is scheduled for reloading, don't switch to it during combat
953         if (self.weapon_load[new_weapon] < 0)
954         {
955                 float i, other_weapon_available = FALSE;
956                 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
957                 {
958                         // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
959                         if (WEP_ACTION(i, WR_CHECKAMMO1) + WEP_ACTION(i, WR_CHECKAMMO2))
960                                 other_weapon_available = TRUE;
961                 }
962                 if(other_weapon_available)
963                         return TRUE;
964         }
965
966         return FALSE;
967 }
968
969 void havocbot_chooseweapon()
970 {
971         float i;
972
973         // ;)
974         if(g_weaponarena_weapons == WEPSET_TUBA)
975         {
976                 self.switchweapon = WEP_TUBA;
977                 return;
978         }
979
980         // TODO: clean this up by moving it to weapon code
981         if(self.enemy==world)
982         {
983                 // If no weapon was chosen get the first available weapon
984                 if(self.weapon==0)
985                 for(i=WEP_BLASTER + 1; i < WEP_COUNT ; ++i) // Samual: This seems strange compared to other weapon loops...
986                 {
987                         if(client_hasweapon(self, i, TRUE, FALSE))
988                         {
989                                 self.switchweapon = i;
990                                 return;
991                         }
992                 }
993                 return;
994         }
995
996         // Do not change weapon during the next second after a combo
997         i = time - self.lastcombotime;
998         if(i < 1)
999                 return;
1000
1001         float w;
1002         float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1003
1004         // Should it do a weapon combo?
1005         float af, ct, combo_time, combo;
1006
1007         af = ATTACK_FINISHED(self);
1008         ct = autocvar_bot_ai_weapon_combo_threshold;
1009
1010         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1011         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1012         combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1013
1014         combo = FALSE;
1015
1016         if(autocvar_bot_ai_weapon_combo)
1017         if(self.weapon == self.lastfiredweapon)
1018         if(af > combo_time)
1019         {
1020                 combo = TRUE;
1021                 self.lastcombotime = time;
1022         }
1023
1024         distance *= pow(2, self.bot_rangepreference);
1025
1026         // Custom weapon list based on distance to the enemy
1027         if(bot_custom_weapon){
1028
1029                 // Choose weapons for far distance
1030                 if ( distance > bot_distance_far ) {
1031                         for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1032                                 w = bot_weapons_far[i];
1033                                 if ( client_hasweapon(self, w, TRUE, FALSE) )
1034                                 {
1035                                         if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1036                                                 continue;
1037                                         self.switchweapon = w;
1038                                         return;
1039                                 }
1040                         }
1041                 }
1042
1043                 // Choose weapons for mid distance
1044                 if ( distance > bot_distance_close) {
1045                         for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1046                                 w = bot_weapons_mid[i];
1047                                 if ( client_hasweapon(self, w, TRUE, FALSE) )
1048                                 {
1049                                         if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1050                                                 continue;
1051                                         self.switchweapon = w;
1052                                         return;
1053                                 }
1054                         }
1055                 }
1056
1057                 // Choose weapons for close distance
1058                 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1059                         w = bot_weapons_close[i];
1060                         if ( client_hasweapon(self, w, TRUE, FALSE) )
1061                         {
1062                                 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1063                                         continue;
1064                                 self.switchweapon = w;
1065                                 return;
1066                         }
1067                 }
1068         }
1069 }
1070
1071 void havocbot_aim()
1072 {
1073         vector selfvel, enemyvel;
1074 //      if(self.flags & FL_INWATER)
1075 //              return;
1076         if (time < self.nextaim)
1077                 return;
1078         self.nextaim = time + 0.1;
1079         selfvel = self.velocity;
1080         if (!self.waterlevel)
1081                 selfvel_z = 0;
1082         if (self.enemy)
1083         {
1084                 enemyvel = self.enemy.velocity;
1085                 if (!self.enemy.waterlevel)
1086                         enemyvel_z = 0;
1087                 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1088         }
1089         else
1090                 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1091 }
1092
1093 float havocbot_moveto_refresh_route()
1094 {
1095         // Refresh path to goal if necessary
1096         entity wp;
1097         wp = self.havocbot_personal_waypoint;
1098         navigation_goalrating_start();
1099         navigation_routerating(wp, 10000, 10000);
1100         navigation_goalrating_end();
1101         return self.navigation_hasgoals;
1102 }
1103
1104 float havocbot_moveto(vector pos)
1105 {
1106         entity wp;
1107
1108         if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1109         {
1110                 // Step 4: Move to waypoint
1111                 if(self.havocbot_personal_waypoint==world)
1112                 {
1113                         dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1114                         self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1115                         return CMD_STATUS_ERROR;
1116                 }
1117
1118                 if (!bot_strategytoken_taken)
1119                 if(self.havocbot_personal_waypoint_searchtime<time)
1120                 {
1121                         bot_strategytoken_taken = TRUE;
1122                         if(havocbot_moveto_refresh_route())
1123                         {
1124                                 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1125                                 self.havocbot_personal_waypoint_searchtime = time + 10;
1126                                 self.havocbot_personal_waypoint_failcounter = 0;
1127                         }
1128                         else
1129                         {
1130                                 self.havocbot_personal_waypoint_failcounter += 1;
1131                                 self.havocbot_personal_waypoint_searchtime = time + 2;
1132                                 if(self.havocbot_personal_waypoint_failcounter >= 30)
1133                                 {
1134                                         dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1135                                         self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1136                                         remove(self.havocbot_personal_waypoint);
1137                                         return CMD_STATUS_ERROR;
1138                                 }
1139                                 else
1140                                         dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1141                         }
1142                 }
1143
1144                 if(autocvar_bot_debug_goalstack)
1145                         debuggoalstack();
1146
1147                 // Heading
1148                 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1149                 dir_z = 0;
1150                 bot_aimdir(dir, -1);
1151
1152                 // Go!
1153                 havocbot_movetogoal();
1154
1155                 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1156                 {
1157                         // Step 5: Waypoint reached
1158                         dprint(self.netname, "'s personal waypoint reached\n");
1159                         remove(self.havocbot_personal_waypoint);
1160                         self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1161                         return CMD_STATUS_FINISHED;
1162                 }
1163
1164                 return CMD_STATUS_EXECUTING;
1165         }
1166
1167         // Step 2: Linking waypoint
1168         if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1169         {
1170                 // Wait until it is linked
1171                 if(!self.havocbot_personal_waypoint.wplinked)
1172                 {
1173                         dprint(self.netname, " waiting for personal waypoint to be linked\n");
1174                         return CMD_STATUS_EXECUTING;
1175                 }
1176
1177                 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1178                 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1179                 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1180
1181                 // Step 3: Route to waypoint
1182                 dprint(self.netname, " walking to its personal waypoint\n");
1183
1184                 return CMD_STATUS_EXECUTING;
1185         }
1186
1187         // Step 1: Spawning waypoint
1188         wp = waypoint_spawnpersonal(pos);
1189         if(wp==world)
1190         {
1191                 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1192                 return CMD_STATUS_ERROR;
1193         }
1194
1195         self.havocbot_personal_waypoint = wp;
1196         self.havocbot_personal_waypoint_failcounter = 0;
1197         self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1198
1199         // if pos is inside a teleport, then let's mark it as teleport waypoint
1200         entity head;
1201         for(head = world; (head = find(head, classname, "trigger_teleport")); )
1202         {
1203                 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1204                 {
1205                         wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1206                         self.lastteleporttime = 0;
1207                 }
1208         }
1209
1210 /*
1211         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1212                 print("routing to a teleporter\n");
1213         else
1214                 print("routing to a non-teleporter\n");
1215 */
1216
1217         return CMD_STATUS_EXECUTING;
1218 }
1219
1220 float havocbot_resetgoal()
1221 {
1222         navigation_clearroute();
1223         return CMD_STATUS_FINISHED;
1224 }
1225
1226 void havocbot_setupbot()
1227 {
1228         self.bot_ai = havocbot_ai;
1229         self.cmd_moveto = havocbot_moveto;
1230         self.cmd_resetgoal = havocbot_resetgoal;
1231
1232         havocbot_chooserole();
1233 }
1234
1235 vector havocbot_dodge()
1236 {
1237         // LordHavoc: disabled because this is too expensive
1238         return '0 0 0';
1239 #if 0
1240         entity head;
1241         vector dodge, v, n;
1242         float danger, bestdanger, vl, d;
1243         dodge = '0 0 0';
1244         bestdanger = -20;
1245         // check for dangerous objects near bot or approaching bot
1246         head = findchainfloat(bot_dodge, TRUE);
1247         while(head)
1248         {
1249                 if (head.owner != self)
1250                 {
1251                         vl = vlen(head.velocity);
1252                         if (vl > autocvar_sv_maxspeed * 0.3)
1253                         {
1254                                 n = normalize(head.velocity);
1255                                 v = self.origin - head.origin;
1256                                 d = v * n;
1257                                 if (d > (0 - head.bot_dodgerating))
1258                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1259                                 {
1260                                         // calculate direction and distance from the flight path, by removing the forward axis
1261                                         v = v - (n * (v * n));
1262                                         danger = head.bot_dodgerating - vlen(v);
1263                                         if (bestdanger < danger)
1264                                         {
1265                                                 bestdanger = danger;
1266                                                 // dodge to the side of the object
1267                                                 dodge = normalize(v);
1268                                         }
1269                                 }
1270                         }
1271                         else
1272                         {
1273                                 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1274                                 if (bestdanger < danger)
1275                                 {
1276                                         bestdanger = danger;
1277                                         dodge = normalize(self.origin - head.origin);
1278                                 }
1279                         }
1280                 }
1281                 head = head.chain;
1282         }
1283         return dodge;
1284 #endif
1285 }