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