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