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