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