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