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