]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/havocbot.qc
Phase out miscfunctions.qc from the server codebase, preferring more fitting location...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / havocbot / havocbot.qc
1 #include "havocbot.qh"
2
3 #include "roles.qh"
4
5 #include <server/bot/api.qh>
6 #include <server/client.qh>
7 #include <common/weapons/_all.qh>
8 #include <common/stats.qh>
9 #include <server/damage.qh>
10 #include <server/items/items.qh>
11 #include <server/mutators/_mod.qh>
12 #include <server/weapons/selection.qh>
13 #include <server/weapons/weaponsystem.qh>
14 #include <server/world.qh>
15 #include "../cvars.qh"
16
17 #include "../aim.qh"
18 #include "../bot.qh"
19 #include "../navigation.qh"
20 #include "../scripting.qh"
21 #include "../waypoints.qh"
22
23 #include <common/constants.qh>
24 #include <common/impulses/all.qh>
25 #include <common/net_linked.qh>
26 #include <common/physics/player.qh>
27 #include <common/state.qh>
28 #include <common/items/_mod.qh>
29 #include <common/wepent.qh>
30
31 #include <common/mapobjects/func/ladder.qh>
32 #include <common/mapobjects/teleporters.qh>
33 #include <common/mapobjects/trigger/hurt.qh>
34 #include <common/mapobjects/trigger/jumppads.qh>
35
36 #include <lib/warpzone/common.qh>
37
38 void havocbot_ai(entity this)
39 {
40         if(this.draggedby)
41                 return;
42
43         this.bot_aimdir_executed = false;
44         // lock aim if teleported or passing through a warpzone
45         if (this.lastteleporttime && !this.jumppadcount)
46                 this.bot_aimdir_executed = true;
47
48         if(bot_execute_commands(this))
49                 return;
50
51         if (bot_strategytoken == this && !bot_strategytoken_taken)
52         {
53                 if(this.havocbot_blockhead)
54                 {
55                         this.havocbot_blockhead = false;
56                 }
57                 else
58                 {
59                         if (!this.jumppadcount && !STAT(FROZEN, this)
60                                 && !(this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP) && !IS_ONGROUND(this)))
61                         {
62                                 // find a new goal
63                                 this.havocbot_role(this); // little too far down the rabbit hole
64                         }
65                 }
66
67                 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
68                 if(!(IS_DEAD(this) || STAT(FROZEN, this)))
69                 if(!this.goalcurrent)
70                 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
71                 {
72                         // Look for the closest waypoint out of water
73                         entity newgoal = NULL;
74                         IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
75                         {
76                                 if(it.origin.z < this.origin.z)
77                                         continue;
78
79                                 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
80                                         continue;
81
82                                 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
83                                         continue;
84
85                                 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
86
87                                 if(trace_fraction < 1)
88                                         continue;
89
90                                 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
91                                         newgoal = it;
92                         });
93
94                         if(newgoal)
95                         {
96                         //      te_wizspike(newgoal.origin);
97                                 navigation_pushroute(this, newgoal);
98                         }
99                 }
100
101                 // token has been used this frame
102                 bot_strategytoken_taken = true;
103         }
104
105         if (this.goalcurrent && wasfreed(this.goalcurrent))
106         {
107                 navigation_clearroute(this);
108                 navigation_goalrating_timeout_force(this);
109                 return;
110         }
111
112         if(IS_DEAD(this) || STAT(FROZEN, this))
113         {
114                 if (this.goalcurrent)
115                         navigation_clearroute(this);
116                 this.enemy = NULL;
117                 this.bot_aimtarg = NULL;
118                 return;
119         }
120
121         havocbot_chooseenemy(this);
122
123         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
124         {
125                 .entity weaponentity = weaponentities[slot];
126                 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
127                 if(this.(weaponentity).bot_chooseweapontime < time)
128                 {
129                         this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
130                         havocbot_chooseweapon(this, weaponentity);
131                 }
132         }
133         havocbot_aim(this);
134         lag_update(this);
135
136         if (this.bot_aimtarg)
137         {
138                 this.aistatus |= AI_STATUS_ATTACKING;
139                 this.aistatus &= ~AI_STATUS_ROAMING;
140
141                 if(STAT(WEAPONS, this))
142                 {
143                         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
144                         {
145                                 PHYS_INPUT_BUTTON_ATCK(this) = false;
146                                 PHYS_INPUT_BUTTON_ATCK2(this) = false;
147                         }
148                         else
149                         {
150                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
151                                 {
152                                         .entity weaponentity = weaponentities[slot];
153                                         Weapon w = this.(weaponentity).m_weapon;
154                                         if(w == WEP_Null && slot != 0)
155                                                 continue;
156                                         w.wr_aim(w, this, weaponentity);
157                                         if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
158                                                 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
159                                 }
160                         }
161                 }
162                 else
163                 {
164                         if(IS_PLAYER(this.bot_aimtarg))
165                                 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs, 0);
166                 }
167         }
168         else if (this.goalcurrent)
169         {
170                 this.aistatus |= AI_STATUS_ROAMING;
171                 this.aistatus &= ~AI_STATUS_ATTACKING;
172         }
173
174         havocbot_movetogoal(this);
175         if (!this.bot_aimdir_executed && this.goalcurrent)
176         {
177                 // Heading
178                 vector dir = get_closer_dest(this.goalcurrent, this.origin);
179                 dir -= this.origin + this.view_ofs;
180                 dir.z = 0;
181                 bot_aimdir(this, dir, 0);
182         }
183
184         // if the bot is not attacking, consider reloading weapons
185         if (!(this.aistatus & AI_STATUS_ATTACKING))
186         {
187                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
188                 {
189                         .entity weaponentity = weaponentities[slot];
190
191                         if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
192                                 continue;
193
194                         // we are currently holding a weapon that's not fully loaded, reload it
195                         if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
196                         if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
197                                 CS(this).impulse = IMP_weapon_reload.impulse; // not sure if this is done right
198
199                         // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
200                         // the code above executes next frame, starting the reloading then
201                         if(skill >= 5) // bots can only look for unloaded weapons past this skill
202                         if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
203                         {
204                                 FOREACH(Weapons, it != WEP_Null, {
205                                         if((STAT(WEAPONS, this) & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
206                                         {
207                                                 this.(weaponentity).m_switchweapon = it;
208                                                 break;
209                                         }
210                                 });
211                         }
212                 }
213         }
214 }
215
216 void havocbot_bunnyhop(entity this, vector dir)
217 {
218         bool can_run = false;
219         if (!(this.aistatus & AI_STATUS_ATTACKING) && this.goalcurrent && !IS_PLAYER(this.goalcurrent)
220                 && vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed) && !(this.aistatus & AI_STATUS_DANGER_AHEAD)
221                 && this.waterlevel <= WATERLEVEL_WETFEET && !IS_DUCKED(this)
222                 && IS_ONGROUND(this) && !(this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP)))
223         {
224                 vector vel_angles = vectoangles(this.velocity);
225                 vector deviation = vel_angles - vectoangles(dir);
226                 while (deviation.y < -180) deviation.y = deviation.y + 360;
227                 while (deviation.y > 180) deviation.y = deviation.y - 360;
228                 if (fabs(deviation.y) < autocvar_bot_ai_bunnyhop_dir_deviation_max)
229                 {
230                         vector gco = get_closer_dest(this.goalcurrent, this.origin);
231                         float vel = vlen(vec2(this.velocity));
232
233                         // with the current physics, jump distance grows linearly with the speed
234                         float jump_distance = 52.661 + 0.606 * vel;
235                         jump_distance += this.origin.z - gco.z; // roughly take into account vertical distance too
236                         if (vdist(vec2(gco - this.origin), >, max(0, jump_distance)))
237                                 can_run = true;
238                         else if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP)
239                                 && !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
240                                 && this.goalstack01 && !wasfreed(this.goalstack01) && !(this.goalstack01.wpflags & WAYPOINTFLAG_JUMP)
241                                 && vdist(vec2(gco - this.goalstack01.origin), >, 70))
242                         {
243                                 vector gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
244                                 vector ang = vectoangles(gco - this.origin);
245                                 deviation = vectoangles(gno - gco) - vel_angles;
246                                 while (deviation.y < -180) deviation.y = deviation.y + 360;
247                                 while (deviation.y > 180) deviation.y = deviation.y - 360;
248
249                                 float max_turn_angle = autocvar_bot_ai_bunnyhop_turn_angle_max;
250                                 max_turn_angle -= autocvar_bot_ai_bunnyhop_turn_angle_reduction * ((vel - autocvar_sv_maxspeed) / autocvar_sv_maxspeed);
251                                 if ((ang.x < 90 || ang.x > 360 - autocvar_bot_ai_bunnyhop_downward_pitch_max)
252                                         && fabs(deviation.y) < max(autocvar_bot_ai_bunnyhop_turn_angle_min, max_turn_angle))
253                                 {
254                                         can_run = true;
255                                 }
256                         }
257                 }
258         }
259
260         if (can_run)
261         {
262                 PHYS_INPUT_BUTTON_JUMP(this) = true;
263                 this.bot_jump_time = time;
264                 this.aistatus |= AI_STATUS_RUNNING;
265         }
266         else
267         {
268                 if (IS_ONGROUND(this) || this.waterlevel > WATERLEVEL_WETFEET)
269                         this.aistatus &= ~AI_STATUS_RUNNING;
270         }
271 }
272
273 void havocbot_keyboard_movement(entity this, vector destorg)
274 {
275         if(time <= this.havocbot_keyboardtime)
276                 return;
277
278         float sk = skill + this.bot_moveskill;
279         this.havocbot_keyboardtime =
280                 max(
281                         this.havocbot_keyboardtime
282                                 + 0.05 / max(1, sk + this.havocbot_keyboardskill)
283                                 + random() * 0.025 / max(0.00025, skill + this.havocbot_keyboardskill)
284                 , time);
285         vector keyboard = CS(this).movement / autocvar_sv_maxspeed;
286
287         float trigger = autocvar_bot_ai_keyboard_threshold;
288
289         // categorize forward movement
290         // at skill < 1.5 only forward
291         // at skill < 2.5 only individual directions
292         // at skill < 4.5 only individual directions, and forward diagonals
293         // at skill >= 4.5, all cases allowed
294         if (keyboard.x > trigger)
295         {
296                 keyboard.x = 1;
297                 if (sk < 2.5)
298                         keyboard.y = 0;
299         }
300         else if (keyboard.x < -trigger && sk > 1.5)
301         {
302                 keyboard.x = -1;
303                 if (sk < 4.5)
304                         keyboard.y = 0;
305         }
306         else
307         {
308                 keyboard.x = 0;
309                 if (sk < 1.5)
310                         keyboard.y = 0;
311         }
312         if (sk < 4.5)
313                 keyboard.z = 0;
314
315         if (keyboard.y > trigger)
316                 keyboard.y = 1;
317         else if (keyboard.y < -trigger)
318                 keyboard.y = -1;
319         else
320                 keyboard.y = 0;
321
322         if (keyboard.z > trigger)
323                 keyboard.z = 1;
324         else if (keyboard.z < -trigger)
325                 keyboard.z = -1;
326         else
327                 keyboard.z = 0;
328
329         // make sure bots don't get stuck if havocbot_keyboardtime is very high
330         if (keyboard == '0 0 0')
331                 this.havocbot_keyboardtime = min(this.havocbot_keyboardtime, time + 0.2);
332
333         this.havocbot_keyboard = keyboard * autocvar_sv_maxspeed;
334         if (this.havocbot_ducktime > time)
335                 PHYS_INPUT_BUTTON_CROUCH(this) = true;
336
337         keyboard = this.havocbot_keyboard;
338         float blend = bound(0, vlen(destorg - this.origin) / autocvar_bot_ai_keyboard_distance, 1); // When getting close move with 360 degree
339         //dprint("movement ", vtos(CS(this).movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
340         CS(this).movement = CS(this).movement + (keyboard - CS(this).movement) * blend;
341 }
342
343 // return true when bot isn't getting closer to the current goal
344 bool havocbot_checkgoaldistance(entity this, vector gco)
345 {
346         if (this.bot_stop_moving_timeout > time)
347                 return false;
348         float curr_dist_z = max(20, fabs(this.origin.z - gco.z));
349         float curr_dist_2d = max(20, vlen(vec2(this.origin - gco)));
350         float distance_time = this.goalcurrent_distance_time;
351         if(distance_time < 0)
352                 distance_time = -distance_time;
353         if(curr_dist_z >= this.goalcurrent_distance_z && curr_dist_2d >= this.goalcurrent_distance_2d)
354         {
355                 if(!distance_time)
356                         this.goalcurrent_distance_time = time;
357                 else if (time - distance_time > 0.5)
358                         return true;
359         }
360         else
361         {
362                 // reduce it a little bit so it works even with very small approaches to the goal
363                 this.goalcurrent_distance_z = max(20, curr_dist_z - 10);
364                 this.goalcurrent_distance_2d = max(20, curr_dist_2d - 10);
365                 this.goalcurrent_distance_time = 0;
366         }
367         return false;
368 }
369
370 entity havocbot_select_an_item_of_group(entity this, int gr)
371 {
372         entity selected = NULL;
373         float selected_dist2 = 0;
374         // select farthest item of this group from bot's position
375         IL_EACH(g_items, it.item_group == gr && it.solid,
376         {
377                 float dist2 = vlen2(this.origin - it.origin);
378                 if (dist2 < 600 ** 2 && dist2 > selected_dist2)
379                 {
380                         selected = it;
381                         selected_dist2 = vlen2(this.origin - selected.origin);
382                 }
383         });
384
385         if (!selected)
386                 return NULL;
387
388         set_tracewalk_dest(selected, this.origin, false);
389         if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
390                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
391         {
392                 return NULL;
393         }
394
395         return selected;
396 }
397
398 void havocbot_movetogoal(entity this)
399 {
400         vector diff;
401         vector dir;
402         vector flatdir;
403         float dodge_enemy_factor = 1;
404         float maxspeed;
405         //float dist;
406         vector dodge;
407         //if (this.goalentity)
408         //      te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
409         CS(this).movement = '0 0 0';
410         maxspeed = autocvar_sv_maxspeed;
411
412         PHYS_INPUT_BUTTON_CROUCH(this) = boolean(this.goalcurrent.wpflags & WAYPOINTFLAG_CROUCH);
413
414         PHYS_INPUT_BUTTON_JETPACK(this) = false;
415         // Jetpack navigation
416         if(this.navigation_jetpack_goal)
417         if(this.goalcurrent==this.navigation_jetpack_goal)
418         if(GetResource(this, RES_FUEL))
419         {
420                 if(autocvar_bot_debug_goalstack)
421                 {
422                         debuggoalstack(this);
423                         te_wizspike(this.navigation_jetpack_point);
424                 }
425
426                 // Take off
427                 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
428                 {
429                         // Brake almost completely so it can get a good direction
430                         if(vdist(this.velocity, >, 10))
431                                 return;
432                         this.aistatus |= AI_STATUS_JETPACK_FLYING;
433                 }
434
435                 makevectors(this.v_angle.y * '0 1 0');
436                 dir = normalize(this.navigation_jetpack_point - this.origin);
437
438                 // Landing
439                 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
440                 {
441                         // Calculate brake distance in xy
442                         float d = vlen(vec2(this.origin - (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5));
443                         float vel2 = vlen2(vec2(this.velocity));
444                         float db = (vel2 / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
445                         //LOG_INFOF("distance %d, velocity %d, brake at %d ", ceil(d), ceil(v), ceil(db));
446                         if(d < db || d < 500)
447                         {
448                                 // Brake
449                                 if (vel2 > (maxspeed * 0.3) ** 2)
450                                 {
451                                         CS(this).movement_x = dir * v_forward * -maxspeed;
452                                         return;
453                                 }
454                                 // Switch to normal mode
455                                 this.navigation_jetpack_goal = NULL;
456                                 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
457                                 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
458                                 return;
459                         }
460                 }
461                 else if(checkpvs(this.origin,this.goalcurrent))
462                 {
463                         // If I can see the goal switch to landing code
464                         this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
465                         this.aistatus |= AI_STATUS_JETPACK_LANDING;
466                         return;
467                 }
468
469                 // Flying
470                 PHYS_INPUT_BUTTON_JETPACK(this) = true;
471                 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
472                 {
473                         CS(this).movement_x = dir * v_forward * maxspeed;
474                         CS(this).movement_y = dir * v_right * maxspeed;
475                 }
476                 return;
477         }
478
479         // Handling of jump pads
480         if(this.jumppadcount)
481         {
482                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
483                 {
484                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
485                         if(navigation_poptouchedgoals(this))
486                                 return;
487                 }
488                 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
489                 {
490                         // If got stuck on the jump pad try to reach the farthest visible waypoint
491                         // but with some randomness so it can try out different paths
492                         if(!this.goalcurrent)
493                         {
494                                 entity newgoal = NULL;
495                                 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
496                                 {
497                                         if(it.wpflags & WAYPOINTFLAG_TELEPORT)
498                                         if(it.origin.z < this.origin.z - 100 && vdist(vec2(it.origin - this.origin), <, 100))
499                                                 continue;
500
501                                         traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
502
503                                         if(trace_fraction < 1)
504                                                 continue;
505
506                                         if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
507                                                 newgoal = it;
508                                 });
509
510                                 if(newgoal)
511                                 {
512                                         this.ignoregoal = this.goalcurrent;
513                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
514                                         navigation_clearroute(this);
515                                         navigation_routetogoal(this, newgoal, this.origin);
516                                         if(autocvar_bot_debug_goalstack)
517                                                 debuggoalstack(this);
518                                         this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
519                                 }
520                         }
521                         else //if (this.goalcurrent)
522                         {
523                                 if (this.goalcurrent.bot_pickup)
524                                 {
525                                         entity jumppad_wp = this.goalcurrent_prev;
526                                         navigation_poptouchedgoals(this);
527                                         if(!this.goalcurrent && jumppad_wp.wp00)
528                                         {
529                                                 // head to the jumppad destination once bot reaches the goal item
530                                                 navigation_pushroute(this, jumppad_wp.wp00);
531                                         }
532                                 }
533                                 vector gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
534                                 if (this.origin.z > gco.z && vdist(vec2(this.velocity), <, autocvar_sv_maxspeed))
535                                 {
536                                         if (this.velocity.z < 0)
537                                                 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
538                                 }
539                                 else if(havocbot_checkgoaldistance(this, gco))
540                                 {
541                                         navigation_clearroute(this);
542                                         navigation_goalrating_timeout_force(this);
543                                 }
544                                 else
545                                         return;
546                         }
547                 }
548                 else //if (!(this.aistatus & AI_STATUS_OUT_JUMPPAD))
549                 {
550                         if(this.velocity.z > 0 && this.origin.z - this.lastteleport_origin.z > (this.maxs.z - this.mins.z) * 0.5)
551                         {
552                                 vector velxy = this.velocity; velxy_z = 0;
553                                 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
554                                 {
555                                         LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
556                                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
557                                 }
558                                 return;
559                         }
560
561                         // Don't chase players while using a jump pad
562                         if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
563                                 return;
564                 }
565         }
566         else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
567                 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
568
569         // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
570         if (skill > 6 && !(IS_ONGROUND(this)))
571         {
572                 #define ROCKETJUMP_DAMAGE() WEP_CVAR(devastator, damage) * 0.8 \
573                         * ((STAT(STRENGTH_FINISHED, this) > time) ? autocvar_g_balance_powerup_strength_selfdamage : 1) \
574                         * ((STAT(INVINCIBLE_FINISHED, this) > time) ? autocvar_g_balance_powerup_invincible_takedamage : 1)
575
576                 // save some CPU cycles by checking trigger_hurt after checking
577                 // that something can be done to evade it (cheaper checks)
578                 int action_for_trigger_hurt = 0;
579                 if (this.items & IT_JETPACK)
580                         action_for_trigger_hurt = 1;
581                 else if (!this.jumppadcount && !waypoint_is_hardwiredlink(this.goalcurrent_prev, this.goalcurrent)
582                         && !(this.goalcurrent_prev && this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP)
583                         && GetResource(this, RES_HEALTH) + GetResource(this, RES_ARMOR) > ROCKETJUMP_DAMAGE())
584                 {
585                         action_for_trigger_hurt = 2;
586                 }
587                 else if (!this.goalcurrent)
588                         action_for_trigger_hurt = 3;
589
590                 if (action_for_trigger_hurt)
591                 {
592                         tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
593                         if(!tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos))
594                                 action_for_trigger_hurt = 0;
595                 }
596
597                 if(action_for_trigger_hurt == 1) // jetpack
598                 {
599                         tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
600                         if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
601                         {
602                                 if(this.velocity.z<0)
603                                         PHYS_INPUT_BUTTON_JETPACK(this) = true;
604                         }
605                         else
606                                 PHYS_INPUT_BUTTON_JETPACK(this) = true;
607
608                         // If there is no goal try to move forward
609
610                         if(this.goalcurrent==NULL)
611                                 dir = v_forward;
612                         else
613                                 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
614
615                         vector xyvelocity = this.velocity; xyvelocity_z = 0;
616                         float xyspeed = xyvelocity * dir;
617
618                         if(xyspeed < (maxspeed / 2))
619                         {
620                                 makevectors(this.v_angle.y * '0 1 0');
621                                 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
622                                 if(trace_fraction==1)
623                                 {
624                                         CS(this).movement_x = dir * v_forward * maxspeed;
625                                         CS(this).movement_y = dir * v_right * maxspeed;
626                                         if (skill < 10)
627                                                 havocbot_keyboard_movement(this, this.origin + dir * 100);
628                                 }
629                         }
630
631                         this.havocbot_blockhead = true;
632
633                         return;
634                 }
635                 else if(action_for_trigger_hurt == 2) // rocketjump
636                 {
637                         if(this.velocity.z < 0)
638                         {
639                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
640                                 {
641                                         .entity weaponentity = weaponentities[slot];
642
643                                         if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
644                                                 continue;
645
646                                         if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
647                                         {
648                                                 CS(this).movement_x = maxspeed;
649
650                                                 if(this.rocketjumptime)
651                                                 {
652                                                         if(time > this.rocketjumptime)
653                                                         {
654                                                                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
655                                                                 this.rocketjumptime = 0;
656                                                         }
657                                                         return;
658                                                 }
659
660                                                 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
661                                                 this.v_angle_x = 90;
662                                                 PHYS_INPUT_BUTTON_ATCK(this) = true;
663                                                 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
664                                                 return;
665                                         }
666                                 }
667                         }
668                 }
669                 else if(action_for_trigger_hurt == 3) // no goal
670                 {
671                         // If there is no goal try to move forward
672                         CS(this).movement_x = maxspeed;
673                 }
674         }
675
676         // If we are under water with no goals, swim up
677         if(this.waterlevel && !this.goalcurrent)
678         {
679                 dir = '0 0 0';
680                 if(this.waterlevel>WATERLEVEL_SWIMMING)
681                         dir.z = 1;
682                 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
683                         PHYS_INPUT_BUTTON_JUMP(this) = true;
684                 makevectors(this.v_angle.y * '0 1 0');
685                 vector v = dir * maxspeed;
686                 CS(this).movement.x = v * v_forward;
687                 CS(this).movement.y = v * v_right;
688                 CS(this).movement.z = v * v_up;
689         }
690
691         // if there is nowhere to go, exit
692         if (this.goalcurrent == NULL)
693                 return;
694
695
696         bool locked_goal = false;
697         if((this.goalentity && wasfreed(this.goalentity))
698                 || (this.goalcurrent == this.goalentity && this.goalentity.tag_entity))
699         {
700                 navigation_clearroute(this);
701                 navigation_goalrating_timeout_force(this);
702                 return;
703         }
704         else if(this.goalentity.tag_entity)
705         {
706                 navigation_goalrating_timeout_expire(this, 2);
707         }
708         else if(this.goalentity.bot_pickup)
709         {
710                 if(this.goalentity.bot_pickup_respawning)
711                 {
712                         if(this.goalentity.solid) // item respawned
713                                 this.goalentity.bot_pickup_respawning = false;
714                         else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
715                         {
716                                 if(checkpvs(this.origin, this.goalentity))
717                                 {
718                                         this.goalentity.bot_pickup_respawning = false;
719                                         navigation_goalrating_timeout_expire(this, random());
720                                 }
721                                 locked_goal = true; // wait for item to respawn
722                         }
723                         else if(this.goalentity == this.goalcurrent)
724                                 locked_goal = true; // wait for item to respawn
725                 }
726                 else if(!this.goalentity.solid && !boxesoverlap(this.goalentity.absmin, this.goalentity.absmax, this.absmin, this.absmax))
727                 {
728                         if(checkpvs(this.origin, this.goalentity))
729                         {
730                                 navigation_goalrating_timeout_expire(this, random());
731                         }
732                 }
733         }
734         if (this.goalcurrent == this.goalentity && this.goalentity_lock_timeout > time)
735                 locked_goal = true;
736
737         if (navigation_shortenpath(this))
738         {
739                 if (vdist(this.origin - this.goalcurrent_prev.origin, <, 50)
740                         && navigation_goalrating_timeout_can_be_anticipated(this))
741                 {
742                         navigation_goalrating_timeout_force(this);
743                 }
744         }
745
746         bool goalcurrent_can_be_removed = false;
747         if (IS_PLAYER(this.goalcurrent) || IS_MONSTER(this.goalcurrent))
748         {
749                 bool freeze_state_changed = (boolean(STAT(FROZEN, this.goalentity)) != this.goalentity_shouldbefrozen);
750                 if (IS_DEAD(this.goalcurrent) || (this.goalentity == this.goalcurrent && freeze_state_changed))
751                 {
752                         goalcurrent_can_be_removed = true;
753                         // don't remove if not visible
754                         if (checkpvs(this.origin + this.view_ofs, this.goalcurrent))
755                         {
756                                 if (IS_DEAD(this.goalcurrent))
757                                 {
758                                         IL_EACH(g_items, it.enemy == this.goalcurrent && Item_IsLoot(it),
759                                         {
760                                                 if (vdist(it.origin - this.goalcurrent.death_origin, <, 50))
761                                                 {
762                                                         navigation_clearroute(this);
763                                                         navigation_pushroute(this, it);
764                                                         // loot can't be immediately rated since it isn't on ground yet
765                                                         // it will be rated after a second when on ground, meanwhile head to it
766                                                         navigation_goalrating_timeout_expire(this, 1);
767                                                         return;
768                                                 }
769                                         });
770                                 }
771                                 if (!Item_IsLoot(this.goalcurrent))
772                                 {
773                                         navigation_goalrating_timeout_force(this);
774                                         return;
775                                 }
776                         }
777                 }
778                 else if (!(STAT(FROZEN, this.goalentity)) && this.bot_tracewalk_time < time)
779                 {
780                         set_tracewalk_dest(this.goalcurrent, this.origin, true);
781                         if (!(trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
782                                 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
783                         {
784                                 navigation_goalrating_timeout_force(this);
785                                 return;
786                         }
787                         this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
788                 }
789         }
790
791         if(!locked_goal)
792         {
793                 // optimize path finding by anticipating goalrating when bot is near a waypoint;
794                 // in this case path finding can start directly from a waypoint instead of
795                 // looking for all the reachable waypoints up to a certain distance
796                 if (navigation_poptouchedgoals(this))
797                 {
798                         if (this.goalcurrent)
799                         {
800                                 if (goalcurrent_can_be_removed)
801                                 {
802                                         // remove even if not visible
803                                         navigation_goalrating_timeout_force(this);
804                                         return;
805                                 }
806                                 else if (navigation_goalrating_timeout_can_be_anticipated(this))
807                                         navigation_goalrating_timeout_force(this);
808                         }
809                         else
810                         {
811                                 entity old_goal = this.goalcurrent_prev;
812                                 if (old_goal.item_group && this.item_group != old_goal.item_group)
813                                 {
814                                         // Avoid multiple costly calls of path finding code that selects one of the closest
815                                         // item of the group by telling the bot to head directly to the farthest item.
816                                         // Next time we let the bot select a goal as usual which can be another item
817                                         // of this group (the closest one) and so on
818                                         this.item_group = old_goal.item_group;
819                                         entity new_goal = havocbot_select_an_item_of_group(this, old_goal.item_group);
820                                         if (new_goal)
821                                                 navigation_pushroute(this, new_goal);
822                                 }
823                         }
824                 }
825         }
826
827         // if ran out of goals try to use an alternative goal or get a new strategy asap
828         if(this.goalcurrent == NULL)
829         {
830                 navigation_goalrating_timeout_force(this);
831                 return;
832         }
833
834
835         if(autocvar_bot_debug_goalstack)
836                 debuggoalstack(this);
837
838         bool bunnyhop_forbidden = false;
839         vector destorg = get_closer_dest(this.goalcurrent, this.origin);
840         if (this.jumppadcount && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
841         {
842                 // if bot used the jumppad, push towards jumppad origin until jumppad waypoint gets removed
843                 destorg = this.goalcurrent.origin;
844         }
845         else if (this.goalcurrent.wpisbox)
846         {
847                 // if bot is inside the teleport waypoint, head to teleport origin until teleport gets used
848                 // do it even if bot is on a ledge above a teleport/jumppad so it doesn't get stuck
849                 if (boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin + eZ * this.mins.z, this.origin + eZ * this.maxs.z)
850                         || (this.absmin.z > destorg.z && destorg.x == this.origin.x && destorg.y == this.origin.y))
851                 {
852                         bunnyhop_forbidden = true;
853                         destorg = this.goalcurrent.origin;
854                         if(destorg.z > this.origin.z)
855                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
856                 }
857         }
858
859         diff = destorg - this.origin;
860
861         if (time < this.bot_stop_moving_timeout
862                 || (this.goalcurrent == this.goalentity && time < this.goalentity_lock_timeout && vdist(diff, <, 10)))
863         {
864                 // stop if the locked goal has been reached
865                 destorg = this.origin;
866                 diff = dir = '0 0 0';
867         }
868         else if (IS_PLAYER(this.goalcurrent) || IS_MONSTER(this.goalcurrent))
869         {
870                 if (vdist(diff, <, 80))
871                 {
872                         // stop if too close to target player (even if frozen)
873                         destorg = this.origin;
874                         diff = dir = '0 0 0';
875                 }
876                 else
877                 {
878                         // move destorg out of target players, otherwise bot will consider them
879                         // an obstacle that needs to be jumped (especially if frozen)
880                         dir = normalize(diff);
881                         destorg -= dir * PL_MAX_CONST.x * M_SQRT2;
882                         diff = destorg - this.origin;
883                 }
884         }
885         else
886                 dir = normalize(diff);
887         flatdir = (diff.z == 0) ? dir : normalize(vec2(diff));
888
889         bool danger_detected = false;
890         vector do_break = '0 0 0';
891
892         //if (this.bot_dodgevector_time < time)
893         {
894                 //this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
895                 //this.bot_dodgevector_jumpbutton = 1;
896
897                 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
898                 makevectors(this.v_angle.y * '0 1 0');
899                 if (this.waterlevel > WATERLEVEL_WETFEET)
900                 {
901                         if (this.waterlevel > WATERLEVEL_SWIMMING)
902                         {
903                                 if(!this.goalcurrent)
904                                         this.aistatus |= AI_STATUS_OUT_WATER;
905                                 else if(destorg.z > this.origin.z)
906                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
907                         }
908                         else
909                         {
910                                 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && destorg.z < this.origin.z) &&
911                                         (this.aistatus & AI_STATUS_OUT_WATER))
912                                 {
913                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
914                                         dir = flatdir;
915                                 }
916                                 else
917                                 {
918                                         if (destorg.z > this.origin.z)
919                                                 dir = flatdir;
920                                 }
921                         }
922                 }
923                 else
924                 {
925                         float s;
926                         vector offset;
927                         if(this.aistatus & AI_STATUS_OUT_WATER)
928                                 this.aistatus &= ~AI_STATUS_OUT_WATER;
929
930                         // jump if going toward an obstacle that doesn't look like stairs we
931                         // can walk up directly
932                         vector deviation = '0 0 0';
933                         float current_speed = vlen(vec2(this.velocity));
934                         if (current_speed < maxspeed * 0.2)
935                                 current_speed = maxspeed * 0.2;
936                         else
937                         {
938                                 deviation = vectoangles(diff) - vectoangles(this.velocity);
939                                 while (deviation.y < -180) deviation.y += 360;
940                                 while (deviation.y > 180) deviation.y -= 360;
941                         }
942                         float turning = false;
943                         vector flat_diff = vec2(diff);
944                         offset = max(32, current_speed * cos(deviation.y * DEG2RAD) * 0.3) * flatdir;
945                         vector actual_destorg = this.origin + offset;
946                         if (this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP))
947                         {
948                                 if (time > this.bot_stop_moving_timeout
949                                         && fabs(deviation.y) > 20 && current_speed > maxspeed * 0.4
950                                         && vdist(vec2(this.origin - this.goalcurrent_prev.origin), <, 50))
951                                 {
952                                         this.bot_stop_moving_timeout = time + 0.1;
953                                 }
954                                 if (current_speed > autocvar_sv_maxspeed * 0.9
955                                         && vlen2(flat_diff) < vlen2(vec2(this.goalcurrent_prev.origin - destorg))
956                                         && vdist(vec2(this.origin - this.goalcurrent_prev.origin), >, 50)
957                                         && vdist(vec2(this.origin - this.goalcurrent_prev.origin), <, 150)
958                                 )
959                                 {
960                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
961                                         this.bot_jump_time = time;
962                                 }
963                         }
964                         else if (!this.goalstack01 || (this.goalcurrent.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_LADDER)))
965                         {
966                                 if (vlen2(flat_diff) < vlen2(offset))
967                                 {
968                                         if ((this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP) && this.goalstack01)
969                                         {
970                                                 // oblique warpzones need a jump otherwise bots gets stuck
971                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
972                                         }
973                                         else
974                                         {
975                                                 actual_destorg.x = destorg.x;
976                                                 actual_destorg.y = destorg.y;
977                                         }
978                                 }
979                         }
980                         else if (vdist(flat_diff, <, 32) && diff.z < -16) // destination is under the bot
981                         {
982                                 actual_destorg.x = destorg.x;
983                                 actual_destorg.y = destorg.y;
984                         }
985                         else if (vlen2(flat_diff) < vlen2(offset))
986                         {
987                                 vector next_goal_org = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
988                                 vector next_dir = normalize(vec2(next_goal_org - destorg));
989                                 float dist = vlen(vec2(this.origin + offset - destorg));
990                                 // if current and next goal are close to each other make sure
991                                 // actual_destorg isn't set beyond next_goal_org
992                                 if (dist ** 2 > vlen2(vec2(next_goal_org - destorg)))
993                                         actual_destorg = next_goal_org;
994                                 else
995                                         actual_destorg = vec2(destorg) + dist * next_dir;
996                                 actual_destorg.z = this.origin.z;
997                                 turning = true;
998                         }
999
1000                         LABEL(jumpobstacle_check);
1001                         dir = flatdir = normalize(actual_destorg - this.origin);
1002
1003                         bool jump_forbidden = false;
1004                         if (!turning && fabs(deviation.y) > 50)
1005                                 jump_forbidden = true;
1006                         else if (IS_DUCKED(this))
1007                         {
1008                                 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, false, this);
1009                                 if (trace_startsolid)
1010                                         jump_forbidden = true;
1011                         }
1012
1013                         if (!jump_forbidden)
1014                         {
1015                                 tracebox(this.origin, this.mins, this.maxs, actual_destorg, false, this);
1016                                 if (trace_fraction < 1 && trace_plane_normal.z < 0.7)
1017                                 {
1018                                         s = trace_fraction;
1019                                         tracebox(this.origin + stepheightvec, this.mins, this.maxs, actual_destorg + stepheightvec, false, this);
1020                                         if (trace_fraction < s + 0.01 && trace_plane_normal.z < 0.7)
1021                                         {
1022                                                 // found an obstacle
1023                                                 if (turning && fabs(deviation.y) > 5)
1024                                                 {
1025                                                         // check if the obstacle is still there without turning
1026                                                         actual_destorg = destorg;
1027                                                         turning = false;
1028                                                         this.bot_tracewalk_time = time + 0.25;
1029                                                         goto jumpobstacle_check;
1030                                                 }
1031                                                 s = trace_fraction;
1032                                                 // don't artificially reduce max jump height in real-time
1033                                                 // (jumpstepheightvec is reduced a bit to make the jumps easy in tracewalk)
1034                                                 vector jump_height = (IS_ONGROUND(this)) ? stepheightvec + jumpheight_vec : jumpstepheightvec;
1035                                                 tracebox(this.origin + jump_height, this.mins, this.maxs, actual_destorg + jump_height, false, this);
1036                                                 if (trace_fraction > s)
1037                                                 {
1038                                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
1039                                                         this.bot_jump_time = time;
1040                                                 }
1041                                                 else
1042                                                 {
1043                                                         jump_height = stepheightvec + jumpheight_vec / 2;
1044                                                         tracebox(this.origin + jump_height, this.mins, this.maxs, actual_destorg + jump_height, false, this);
1045                                                         if (trace_fraction > s)
1046                                                         {
1047                                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
1048                                                                 this.bot_jump_time = time;
1049                                                         }
1050                                                 }
1051                                         }
1052                                 }
1053                         }
1054
1055                         // if bot for some reason doesn't get close to the current goal find another one
1056                         if(!this.jumppadcount && !IS_PLAYER(this.goalcurrent))
1057                         if(!(locked_goal && this.goalcurrent_distance_z < 50 && this.goalcurrent_distance_2d < 50))
1058                         if(havocbot_checkgoaldistance(this, destorg))
1059                         {
1060                                 if(this.goalcurrent_distance_time < 0) // can't get close for the second time
1061                                 {
1062                                         navigation_clearroute(this);
1063                                         navigation_goalrating_timeout_force(this);
1064                                         return;
1065                                 }
1066
1067                                 set_tracewalk_dest(this.goalcurrent, this.origin, false);
1068                                 if (!tracewalk(this, this.origin, this.mins, this.maxs,
1069                                         tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1070                                 {
1071                                         navigation_clearroute(this);
1072                                         navigation_goalrating_timeout_force(this);
1073                                         return;
1074                                 }
1075
1076                                 // give bot only another chance to prevent bot getting stuck
1077                                 // in case it thinks it can walk but actually can't
1078                                 this.goalcurrent_distance_z = FLOAT_MAX;
1079                                 this.goalcurrent_distance_2d = FLOAT_MAX;
1080                                 this.goalcurrent_distance_time = -time; // mark second try
1081                         }
1082
1083                         if (skill + this.bot_moveskill <= 3 && time > this.bot_stop_moving_timeout
1084                                 && current_speed > maxspeed * 0.9 && fabs(deviation.y) > 70)
1085                         {
1086                                 this.bot_stop_moving_timeout = time + 0.4 + random() * 0.2;
1087                         }
1088
1089                         // Check for water/slime/lava and dangerous edges
1090                         // (only when the bot is on the ground or jumping intentionally)
1091
1092                         offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : flatdir * 32);
1093                         vector dst_ahead = this.origin + this.view_ofs + offset;
1094                         vector dst_down = dst_ahead - '0 0 3000';
1095                         traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
1096
1097                         bool unreachable = false;
1098                         s = CONTENT_SOLID;
1099                         if (trace_fraction == 1 && !this.jumppadcount
1100                                 && !waypoint_is_hardwiredlink(this.goalcurrent_prev, this.goalcurrent)
1101                                 && !(this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP)))
1102                         if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
1103                         {
1104                                 // Look downwards
1105                                 traceline(dst_ahead , dst_down, true, NULL);
1106                                 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
1107                                 //te_lightning2(NULL, dst_ahead, trace_endpos); // Draw "downwards" look
1108                                 if(trace_endpos.z < this.origin.z + this.mins.z)
1109                                 {
1110                                         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1111                                                 danger_detected = true;
1112                                         else if (trace_endpos.z < min(this.origin.z + this.mins.z, this.goalcurrent.origin.z) - 100)
1113                                                 danger_detected = true;
1114                                         else
1115                                         {
1116                                                 s = pointcontents(trace_endpos + '0 0 1');
1117                                                 if (s != CONTENT_SOLID)
1118                                                 {
1119                                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
1120                                                                 danger_detected = true;
1121                                                         else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
1122                                                         {
1123                                                                 // the traceline check isn't enough but is good as optimization,
1124                                                                 // when not true (most of the time) this tracebox call is avoided
1125                                                                 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
1126                                                                 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
1127                                                                 {
1128                                                                         if (destorg.z > this.origin.z + jumpstepheightvec.z)
1129                                                                         {
1130                                                                                 // the goal is probably on an upper platform, assume bot can't get there
1131                                                                                 unreachable = true;
1132                                                                         }
1133                                                                         else
1134                                                                                 danger_detected = true;
1135                                                                 }
1136                                                         }
1137                                                 }
1138                                         }
1139                                 }
1140                         }
1141
1142                         dir = flatdir;
1143                         makevectors(this.v_angle.y * '0 1 0');
1144
1145                         if (danger_detected || (s == CONTENT_WATER))
1146                         {
1147                                 this.aistatus |= AI_STATUS_DANGER_AHEAD;
1148                                 if(IS_PLAYER(this.goalcurrent))
1149                                         unreachable = true;
1150                         }
1151
1152                         // slow down if bot is in the air and goal is under it
1153                         if (!waypoint_is_hardwiredlink(this.goalcurrent_prev, this.goalcurrent)
1154                                 && vdist(flat_diff, <, 250) && this.origin.z - destorg.z > 120
1155                                 && (!IS_ONGROUND(this) || vdist(vec2(this.velocity), >, maxspeed * 0.3)))
1156                         {
1157                                 // tracebox wouldn't work when bot is still on the ledge
1158                                 traceline(this.origin, this.origin - '0 0 200', true, this);
1159                                 if (this.origin.z - trace_endpos.z > 120)
1160                                         do_break = normalize(this.velocity) * -1;
1161                         }
1162
1163                         if(unreachable)
1164                         {
1165                                 navigation_clearroute(this);
1166                                 navigation_goalrating_timeout_force(this);
1167                                 this.ignoregoal = this.goalcurrent;
1168                                 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
1169                         }
1170                 }
1171
1172                 dodge = havocbot_dodge(this);
1173                 if (dodge)
1174                         dodge *= bound(0, 0.5 + (skill + this.bot_dodgeskill) * 0.1, 1);
1175                 if (this.enemy)
1176                 {
1177                         traceline(this.origin, (this.enemy.absmin + this.enemy.absmax) * 0.5, true, NULL);
1178                         if (IS_PLAYER(trace_ent))
1179                                 dodge_enemy_factor = bound(0, (skill + this.bot_dodgeskill) / 7, 1);
1180                 }
1181         //      this.bot_dodgevector = dir;
1182         //      this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
1183         }
1184
1185         float ladder_zdir = 0;
1186         if(this.ladder_entity)
1187         {
1188                 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
1189                 {
1190                         if(this.origin.z + this.mins.z  < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
1191                                 ladder_zdir = 1;
1192                 }
1193                 else
1194                 {
1195                         if(this.origin.z + this.mins.z  > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
1196                                 ladder_zdir = -1;
1197                 }
1198                 if (ladder_zdir)
1199                 {
1200                         if (vdist(vec2(diff), <, 40))
1201                                 dir.z = ladder_zdir * 4;
1202                         else
1203                                 dir.z = ladder_zdir * 2;
1204                         dir = normalize(dir);
1205                 }
1206         }
1207
1208         if (this.goalcurrent.wpisbox
1209                 && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin, this.origin))
1210         {
1211                 // bot is inside teleport waypoint but hasn't touched the real teleport yet
1212                 // head to teleport origin
1213                 dir = (this.goalcurrent.origin - this.origin);
1214                 dir.z = 0;
1215                 dir = normalize(dir);
1216         }
1217
1218         // already executed when bot targets an enemy
1219         if (!this.bot_aimdir_executed)
1220         {
1221                 if (time < this.bot_stop_moving_timeout)
1222                         bot_aimdir(this, normalize(this.goalcurrent.origin - this.origin), 0);
1223                 else
1224                         bot_aimdir(this, dir, 0);
1225         }
1226
1227         vector evadedanger = '0 0 0';
1228         if (!ladder_zdir)
1229         {
1230                 dir *= dodge_enemy_factor;
1231                 if (danger_detected && vdist(this.velocity, >, maxspeed * 0.8) && this.goalcurrent_prev
1232                         && this.goalcurrent.classname == "waypoint")
1233                 {
1234                         vector p = this.origin + this.velocity * 0.2;
1235                         vector evadedanger = point_line_vec(p, vec2(this.goalcurrent_prev.origin) + eZ * p.z,
1236                                 vec2(destorg - this.goalcurrent_prev.origin));
1237                         if (vdist(evadedanger, >, 20))
1238                         {
1239                                 if (vdist(evadedanger, >, 40))
1240                                         do_break = normalize(this.velocity) * -1;
1241                                 evadedanger = normalize(evadedanger);
1242                                 evadedanger *= bound(1, 3 - (skill + this.bot_dodgeskill), 3); // Noobs fear dangers a lot and take more distance from them
1243                         }
1244                         else
1245                                 evadedanger = '0 0 0';
1246                 }
1247                 dir = normalize(dir + dodge + do_break + evadedanger);
1248         }
1249
1250         makevectors(this.v_angle);
1251         //dir = this.bot_dodgevector;
1252         //if (this.bot_dodgevector_jumpbutton)
1253         //      PHYS_INPUT_BUTTON_JUMP(this) = true;
1254         CS(this).movement_x = dir * v_forward * maxspeed;
1255         CS(this).movement_y = dir * v_right * maxspeed;
1256         CS(this).movement_z = dir * v_up * maxspeed;
1257
1258         // Emulate keyboard interface
1259         if (skill < 10)
1260                 havocbot_keyboard_movement(this, destorg);
1261
1262         // Bunnyhop!
1263         if (!bunnyhop_forbidden && !evadedanger && !do_break && skill + this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
1264                 havocbot_bunnyhop(this, dir);
1265
1266         if (dir * v_up >= autocvar_sv_jumpvelocity * 0.5 && IS_ONGROUND(this))
1267                 PHYS_INPUT_BUTTON_JUMP(this) = true;
1268         if (dodge)
1269         {
1270                 if (dodge * v_up > 0 && random() * frametime >= 0.2 * bound(0, (10 - skill - this.bot_dodgeskill) * 0.1, 1))
1271                         PHYS_INPUT_BUTTON_JUMP(this) = true;
1272                 if (dodge * v_up < 0 && random() * frametime >= 0.5 * bound(0, (10 - skill - this.bot_dodgeskill) * 0.1, 1))
1273                         this.havocbot_ducktime = time + 0.3 / bound(0.1, skill + this.bot_dodgeskill, 10);
1274         }
1275 }
1276
1277 entity havocbot_gettarget(entity this, bool secondary)
1278 {
1279         entity best = NULL;
1280         vector eye = CENTER_OR_VIEWOFS(this);
1281         IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
1282         {
1283                 vector v = CENTER_OR_VIEWOFS(it);
1284                 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1285                 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
1286                 if(bot_shouldattack(this, it))
1287                 {
1288                         traceline(eye, v, true, this);
1289                         if (trace_ent == it || trace_fraction >= 1)
1290                                 best = it;
1291                 }
1292         });
1293
1294         return best;
1295 }
1296
1297 void havocbot_chooseenemy(entity this)
1298 {
1299         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
1300         {
1301                 this.enemy = NULL;
1302                 return;
1303         }
1304
1305         if (this.enemy)
1306         {
1307                 if (!bot_shouldattack(this, this.enemy))
1308                 {
1309                         // enemy died or something, find a new target
1310                         this.enemy = NULL;
1311                         this.havocbot_chooseenemy_finished = time;
1312                 }
1313                 else if (this.havocbot_stickenemy_time && time < this.havocbot_stickenemy_time)
1314                 {
1315                         // tracking last chosen enemy
1316                         vector targ_pos = (this.enemy.absmin + this.enemy.absmax) * 0.5;
1317                         traceline(this.origin + this.view_ofs, targ_pos, false, NULL);
1318                         if (trace_ent == this.enemy || trace_fraction == 1)
1319                         if (vdist(targ_pos - this.origin, <, 1000))
1320                         {
1321                                 // remain tracking him for a shot while (case he went after a small corner or pilar
1322                                 this.havocbot_chooseenemy_finished = time + 0.5;
1323                                 return;
1324                         }
1325
1326                         // stop preferring this enemy
1327                         this.havocbot_stickenemy_time = 0;
1328                 }
1329         }
1330         if (time < this.havocbot_chooseenemy_finished)
1331                 return;
1332         this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
1333         vector eye = this.origin + this.view_ofs;
1334         entity best = NULL;
1335         float bestrating = autocvar_bot_ai_enemydetectionradius ** 2;
1336
1337         // Backup hit flags
1338         int hf = this.dphitcontentsmask;
1339
1340         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1341
1342         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1343
1344         bool scan_transparent = false;
1345         bool scan_secondary_targets = false;
1346         bool have_secondary_targets = false;
1347         while(true)
1348         {
1349                 scan_secondary_targets = false;
1350 LABEL(scan_targets)
1351                 IL_EACH(g_bot_targets, it.bot_attack,
1352                 {
1353                         if(!scan_secondary_targets)
1354                         {
1355                                 if(it.classname == "misc_breakablemodel")
1356                                 {
1357                                         have_secondary_targets = true;
1358                                         continue;
1359                                 }
1360                         }
1361                         else if(it.classname != "misc_breakablemodel")
1362                                 continue;
1363
1364                         vector v = (it.absmin + it.absmax) * 0.5;
1365                         float rating = vlen2(v - eye);
1366                         if (rating < bestrating && bot_shouldattack(this, it))
1367                         {
1368                                 traceline(eye, v, true, this);
1369                                 if (trace_ent == it || trace_fraction >= 1)
1370                                 {
1371                                         best = it;
1372                                         bestrating = rating;
1373                                 }
1374                         }
1375                 });
1376
1377                 if(!best && have_secondary_targets && !scan_secondary_targets)
1378                 {
1379                         scan_secondary_targets = true;
1380                         // restart the loop
1381                         bestrating = autocvar_bot_ai_enemydetectionradius ** 2;
1382                         goto scan_targets;
1383                 }
1384
1385                 // I want to do a second scan if no enemy was found or I don't have weapons
1386                 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1387                 if(best || STAT(WEAPONS, this)) // || this.weapon == WEP_RIFLE.m_id
1388                         break;
1389                 if(scan_transparent)
1390                         break;
1391
1392                 // Set flags to see through transparent objects
1393                 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1394
1395                 scan_transparent = true;
1396         }
1397
1398         // Restore hit flags
1399         this.dphitcontentsmask = hf;
1400
1401         this.enemy = best;
1402         this.havocbot_stickenemy_time = time + autocvar_bot_ai_enemydetectioninterval_stickingtoenemy;
1403         if(best && best.classname == "misc_breakablemodel")
1404                 this.havocbot_stickenemy_time = 0;
1405 }
1406
1407 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1408 {
1409         // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1410         // so skip this for them, or they'll never get to reload their weapons at all.
1411         // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1412         if(skill < 5)
1413                 return false;
1414
1415         // if this weapon is scheduled for reloading, don't switch to it during combat
1416         if (this.(weaponentity).weapon_load[new_weapon] < 0)
1417         {
1418                 FOREACH(Weapons, it != WEP_Null, {
1419                         if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1420                                 return true; // other weapon available
1421                 });
1422         }
1423
1424         return false;
1425 }
1426
1427 void havocbot_chooseweapon(entity this, .entity weaponentity)
1428 {
1429         int i;
1430
1431         // ;)
1432         if(g_weaponarena_weapons == WEPSET(TUBA))
1433         {
1434                 this.(weaponentity).m_switchweapon = WEP_TUBA;
1435                 return;
1436         }
1437
1438         // TODO: clean this up by moving it to weapon code
1439         if(this.enemy==NULL)
1440         {
1441                 // If no weapon was chosen get the first available weapon
1442                 if(this.(weaponentity).m_weapon==WEP_Null)
1443                 FOREACH(Weapons, it != WEP_Null, {
1444                         if(client_hasweapon(this, it, weaponentity, true, false))
1445                         {
1446                                 this.(weaponentity).m_switchweapon = it;
1447                                 return;
1448                         }
1449                 });
1450                 return;
1451         }
1452
1453         // Do not change weapon during the next second after a combo
1454         float f = time - this.lastcombotime;
1455         if(f < 1)
1456                 return;
1457
1458         float w;
1459         float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1460
1461         // Should it do a weapon combo?
1462         float af, ct, combo_time, combo;
1463
1464         af = ATTACK_FINISHED(this, weaponentity);
1465         ct = autocvar_bot_ai_weapon_combo_threshold;
1466
1467         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1468         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1469         combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1470
1471         combo = false;
1472
1473         if(autocvar_bot_ai_weapon_combo)
1474         if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1475         if(af > combo_time)
1476         {
1477                 combo = true;
1478                 this.lastcombotime = time;
1479         }
1480
1481         distance *= (2 ** this.bot_rangepreference);
1482
1483         // Custom weapon list based on distance to the enemy
1484         if(bot_custom_weapon){
1485
1486                 // Choose weapons for far distance
1487                 if ( distance > bot_distance_far ) {
1488                         for(i=0; i < REGISTRY_COUNT(Weapons) && bot_weapons_far[i] != -1 ; ++i){
1489                                 w = bot_weapons_far[i];
1490                                 if ( client_hasweapon(this, REGISTRY_GET(Weapons, w), weaponentity, true, false) )
1491                                 {
1492                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1493                                                 continue;
1494                                         this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, w);
1495                                         return;
1496                                 }
1497                         }
1498                 }
1499
1500                 // Choose weapons for mid distance
1501                 if ( distance > bot_distance_close) {
1502                         for(i=0; i < REGISTRY_COUNT(Weapons) && bot_weapons_mid[i] != -1 ; ++i){
1503                                 w = bot_weapons_mid[i];
1504                                 if ( client_hasweapon(this, REGISTRY_GET(Weapons, w), weaponentity, true, false) )
1505                                 {
1506                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1507                                                 continue;
1508                                         this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, w);
1509                                         return;
1510                                 }
1511                         }
1512                 }
1513
1514                 // Choose weapons for close distance
1515                 for(i=0; i < REGISTRY_COUNT(Weapons) && bot_weapons_close[i] != -1 ; ++i){
1516                         w = bot_weapons_close[i];
1517                         if ( client_hasweapon(this, REGISTRY_GET(Weapons, w), weaponentity, true, false) )
1518                         {
1519                                 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1520                                         continue;
1521                                 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, w);
1522                                 return;
1523                         }
1524                 }
1525         }
1526 }
1527
1528 void havocbot_aim(entity this)
1529 {
1530         if (time < this.nextaim)
1531                 return;
1532         this.nextaim = time + 0.1;
1533         vector myvel = this.velocity;
1534         if (!this.waterlevel)
1535                 myvel.z = 0;
1536         if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1537         else if (this.enemy)
1538         {
1539                 vector enemyvel = this.enemy.velocity;
1540                 if (!this.enemy.waterlevel)
1541                         enemyvel.z = 0;
1542                 lag_additem(this, time + CS(this).ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1543         }
1544         else
1545                 lag_additem(this, time + CS(this).ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1546 }
1547
1548 bool havocbot_moveto_refresh_route(entity this)
1549 {
1550         // Refresh path to goal if necessary
1551         entity wp;
1552         wp = this.havocbot_personal_waypoint;
1553         navigation_goalrating_start(this);
1554         navigation_routerating(this, wp, 10000, 10000);
1555         navigation_goalrating_end(this);
1556         return (this.goalentity != NULL);
1557 }
1558
1559 float havocbot_moveto(entity this, vector pos)
1560 {
1561         entity wp;
1562
1563         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1564         {
1565                 // Step 4: Move to waypoint
1566                 if(this.havocbot_personal_waypoint==NULL)
1567                 {
1568                         LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1569                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1570                         return CMD_STATUS_ERROR;
1571                 }
1572
1573                 if (!bot_strategytoken_taken)
1574                 if(this.havocbot_personal_waypoint_searchtime<time)
1575                 {
1576                         bot_strategytoken_taken = true;
1577                         if(havocbot_moveto_refresh_route(this))
1578                         {
1579                                 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1580                                 this.havocbot_personal_waypoint_searchtime = time + 10;
1581                                 this.havocbot_personal_waypoint_failcounter = 0;
1582                         }
1583                         else
1584                         {
1585                                 this.havocbot_personal_waypoint_failcounter += 1;
1586                                 this.havocbot_personal_waypoint_searchtime = time + 2;
1587                                 if(this.havocbot_personal_waypoint_failcounter >= 30)
1588                                 {
1589                                         LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1590                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1591                                         delete(this.havocbot_personal_waypoint);
1592                                         return CMD_STATUS_ERROR;
1593                                 }
1594                                 else
1595                                         LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1596                         }
1597                 }
1598
1599                 if(autocvar_bot_debug_goalstack)
1600                         debuggoalstack(this);
1601
1602
1603                 // Go!
1604                 havocbot_movetogoal(this);
1605
1606                 if (!this.bot_aimdir_executed && this.goalcurrent)
1607                 {
1608                         // Heading
1609                         vector dir = get_closer_dest(this.goalcurrent, this.origin);
1610                         dir -= this.origin + this.view_ofs;
1611                         dir.z = 0;
1612                         bot_aimdir(this, dir, 0);
1613                 }
1614
1615                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1616                 {
1617                         // Step 5: Waypoint reached
1618                         LOG_TRACE(this.netname, "'s personal waypoint reached");
1619                         waypoint_remove(this.havocbot_personal_waypoint);
1620                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1621                         return CMD_STATUS_FINISHED;
1622                 }
1623
1624                 return CMD_STATUS_EXECUTING;
1625         }
1626
1627         // Step 2: Linking waypoint
1628         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1629         {
1630                 // Wait until it is linked
1631                 if(!this.havocbot_personal_waypoint.wplinked)
1632                 {
1633                         LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1634                         return CMD_STATUS_EXECUTING;
1635                 }
1636
1637                 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1638                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1639                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1640
1641                 // Step 3: Route to waypoint
1642                 LOG_TRACE(this.netname, " walking to its personal waypoint");
1643
1644                 return CMD_STATUS_EXECUTING;
1645         }
1646
1647         // Step 1: Spawning waypoint
1648         wp = waypoint_spawnpersonal(this, pos);
1649         if(wp==NULL)
1650         {
1651                 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1652                 return CMD_STATUS_ERROR;
1653         }
1654
1655         this.havocbot_personal_waypoint = wp;
1656         this.havocbot_personal_waypoint_failcounter = 0;
1657         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1658
1659         // if pos is inside a teleport, then let's mark it as teleport waypoint
1660         IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1661         {
1662                 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1663                 this.lastteleporttime = 0;
1664         });
1665
1666 /*
1667         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1668                 print("routing to a teleporter\n");
1669         else
1670                 print("routing to a non-teleporter\n");
1671 */
1672
1673         return CMD_STATUS_EXECUTING;
1674 }
1675
1676 float havocbot_resetgoal(entity this)
1677 {
1678         navigation_clearroute(this);
1679         return CMD_STATUS_FINISHED;
1680 }
1681
1682 void havocbot_setupbot(entity this)
1683 {
1684         this.bot_ai = havocbot_ai;
1685         this.cmd_moveto = havocbot_moveto;
1686         this.cmd_resetgoal = havocbot_resetgoal;
1687
1688         // NOTE: bot is not player yet
1689         havocbot_chooserole(this);
1690 }
1691
1692 vector havocbot_dodge(entity this)
1693 {
1694         // LordHavoc: disabled because this is too expensive
1695         return '0 0 0';
1696 #if 0
1697         entity head;
1698         vector dodge, v, n;
1699         float danger, bestdanger, vl, d;
1700         dodge = '0 0 0';
1701         bestdanger = -20;
1702         // check for dangerous objects near bot or approaching bot
1703         head = findchainfloat(bot_dodge, true);
1704         while(head)
1705         {
1706                 if (head.owner != this)
1707                 {
1708                         vl = vlen(head.velocity);
1709                         if (vl > autocvar_sv_maxspeed * 0.3)
1710                         {
1711                                 n = normalize(head.velocity);
1712                                 v = this.origin - head.origin;
1713                                 d = v * n;
1714                                 if (d > (0 - head.bot_dodgerating))
1715                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1716                                 {
1717                                         // calculate direction and distance from the flight path, by removing the forward axis
1718                                         v = v - (n * (v * n));
1719                                         danger = head.bot_dodgerating - vlen(v);
1720                                         if (bestdanger < danger)
1721                                         {
1722                                                 bestdanger = danger;
1723                                                 // dodge to the side of the object
1724                                                 dodge = normalize(v);
1725                                         }
1726                                 }
1727                         }
1728                         else
1729                         {
1730                                 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1731                                 if (bestdanger < danger)
1732                                 {
1733                                         bestdanger = danger;
1734                                         dodge = normalize(this.origin - head.origin);
1735                                 }
1736                         }
1737                 }
1738                 head = head.chain;
1739         }
1740         return dodge;
1741 #endif
1742 }