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