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