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