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