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