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