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