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