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