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