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