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