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