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