]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/havocbot.qc
80dc706e95643311ccc5bc8da771b86fd1c9fbca
[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                                                 {
782                                                         if (gco.z > this.origin.z + jumpstepheightvec.z)
783                                                         { 
784                                                                 // the goal is probably on an upper platform, assume bot can't get there
785                                                                 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
786                                                                 navigation_clearroute(this);
787                                                                 this.bot_strategytime = 0;
788                                                         }
789                                                         else
790                                                                 evadelava = normalize(this.velocity) * -1;
791                                                 }
792                                         }
793                                 }
794                         }
795
796                         dir = flatdir;
797                         evadeobstacle.z = 0;
798                         evadelava.z = 0;
799                         makevectors(this.v_angle.y * '0 1 0');
800
801                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
802                                 this.aistatus |= AI_STATUS_DANGER_AHEAD;
803                 }
804
805                 dodge = havocbot_dodge(this);
806                 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
807                 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
808                 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
809                 if(IS_PLAYER(trace_ent))
810                         dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
811
812                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
813         //      this.bot_dodgevector = dir;
814         //      this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
815         }
816
817         if(time < this.ladder_time)
818         {
819                 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
820                 {
821                         if(this.origin.z + this.mins.z  < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
822                                 dir.z = 1;
823                 }
824                 else
825                 {
826                         if(this.origin.z + this.mins.z  > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
827                                 dir.z = -1;
828                 }
829         }
830
831         //dir = this.bot_dodgevector;
832         //if (this.bot_dodgevector_jumpbutton)
833         //      PHYS_INPUT_BUTTON_JUMP(this) = true;
834         this.movement_x = dir * v_forward * maxspeed;
835         this.movement_y = dir * v_right * maxspeed;
836         this.movement_z = dir * v_up * maxspeed;
837
838         // Emulate keyboard interface
839         if (skill < 10)
840                 havocbot_keyboard_movement(this, destorg);
841
842         // Bunnyhop!
843 //      if(this.aistatus & AI_STATUS_ROAMING)
844         if(this.goalcurrent)
845         if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
846                 havocbot_bunnyhop(this, dir);
847
848         if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
849         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;
850         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);
851 }
852
853 entity havocbot_gettarget(entity this, bool secondary)
854 {
855         entity best = NULL;
856         vector eye = CENTER_OR_VIEWOFS(this);
857         IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
858         {
859                 vector v = CENTER_OR_VIEWOFS(it);
860                 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
861                 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
862                 if(bot_shouldattack(this, it))
863                 {
864                         traceline(eye, v, true, this);
865                         if (trace_ent == it || trace_fraction >= 1)
866                                 best = it;
867                 }
868         });
869
870         return best;
871 }
872
873 void havocbot_chooseenemy(entity this)
874 {
875         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
876         {
877                 this.enemy = NULL;
878                 return;
879         }
880         if (this.enemy)
881         {
882                 if (!bot_shouldattack(this, this.enemy))
883                 {
884                         // enemy died or something, find a new target
885                         this.enemy = NULL;
886                         this.havocbot_chooseenemy_finished = time;
887                 }
888                 else if (this.havocbot_stickenemy)
889                 {
890                         // tracking last chosen enemy
891                         // if enemy is visible
892                         // and not really really far away
893                         // and we're not severely injured
894                         // then keep tracking for a half second into the future
895                         traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
896                         if (trace_ent == this.enemy || trace_fraction == 1)
897                         if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
898                         if (this.health > 30)
899                         {
900                                 // remain tracking him for a shot while (case he went after a small corner or pilar
901                                 this.havocbot_chooseenemy_finished = time + 0.5;
902                                 return;
903                         }
904                         // enemy isn't visible, or is far away, or we're injured severely
905                         // so stop preferring this enemy
906                         // (it will still take a half second until a new one is chosen)
907                         this.havocbot_stickenemy = 0;
908                 }
909         }
910         if (time < this.havocbot_chooseenemy_finished)
911                 return;
912         this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
913         vector eye = this.origin + this.view_ofs;
914         entity best = NULL;
915         float bestrating = 100000000;
916
917         // Backup hit flags
918         int hf = this.dphitcontentsmask;
919
920         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
921
922         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
923
924         bool scan_transparent = false;
925         bool scan_secondary_targets = false;
926         bool have_secondary_targets = false;
927         while(true)
928         {
929                 scan_secondary_targets = false;
930 LABEL(scan_targets)
931                 IL_EACH(g_bot_targets, it.bot_attack,
932                 {
933                         if(!scan_secondary_targets)
934                         {
935                                 if(it.classname == "misc_breakablemodel")
936                                 {
937                                         have_secondary_targets = true;
938                                         continue;
939                                 }
940                         }
941                         else if(it.classname != "misc_breakablemodel")
942                                 continue;
943
944                         vector v = (it.absmin + it.absmax) * 0.5;
945                         float rating = vlen2(v - eye);
946                         if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
947                         if (bestrating > rating)
948                         if (bot_shouldattack(this, it))
949                         {
950                                 traceline(eye, v, true, this);
951                                 if (trace_ent == it || trace_fraction >= 1)
952                                 {
953                                         best = it;
954                                         bestrating = rating;
955                                 }
956                         }
957                 });
958
959                 if(!best && have_secondary_targets && !scan_secondary_targets)
960                 {
961                         scan_secondary_targets = true;
962                         // restart the loop
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                 scan_transparent = true;
978         }
979
980         // Restore hit flags
981         this.dphitcontentsmask = hf;
982
983         this.enemy = best;
984         this.havocbot_stickenemy = true;
985         if(best && best.classname == "misc_breakablemodel")
986                 this.havocbot_stickenemy = false;
987 }
988
989 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
990 {
991         // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
992         // so skip this for them, or they'll never get to reload their weapons at all.
993         // this also allows bots under this skill to be more stupid, and reload more often during combat :)
994         if(skill < 5)
995                 return false;
996
997         // if this weapon is scheduled for reloading, don't switch to it during combat
998         if (this.weapon_load[new_weapon] < 0)
999         {
1000                 bool other_weapon_available = false;
1001                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1002                         if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
1003                                 other_weapon_available = true;
1004                 ));
1005                 if(other_weapon_available)
1006                         return true;
1007         }
1008
1009         return false;
1010 }
1011
1012 void havocbot_chooseweapon(entity this)
1013 {
1014         int i;
1015
1016         // ;)
1017         if(g_weaponarena_weapons == WEPSET(TUBA))
1018         {
1019                 PS(this).m_switchweapon = WEP_TUBA;
1020                 return;
1021         }
1022
1023         // TODO: clean this up by moving it to weapon code
1024         if(this.enemy==NULL)
1025         {
1026                 // If no weapon was chosen get the first available weapon
1027                 if(PS(this).m_weapon==WEP_Null)
1028                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1029                         if(client_hasweapon(this, it, true, false))
1030                         {
1031                                 PS(this).m_switchweapon = it;
1032                                 return;
1033                         }
1034                 ));
1035                 return;
1036         }
1037
1038         // Do not change weapon during the next second after a combo
1039         float f = time - this.lastcombotime;
1040         if(f < 1)
1041                 return;
1042
1043         float w;
1044         float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1045
1046         // Should it do a weapon combo?
1047         float af, ct, combo_time, combo;
1048
1049         af = ATTACK_FINISHED(this, 0);
1050         ct = autocvar_bot_ai_weapon_combo_threshold;
1051
1052         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1053         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1054         combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1055
1056         combo = false;
1057
1058         if(autocvar_bot_ai_weapon_combo)
1059         if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1060         if(af > combo_time)
1061         {
1062                 combo = true;
1063                 this.lastcombotime = time;
1064         }
1065
1066         distance *= pow(2, this.bot_rangepreference);
1067
1068         // Custom weapon list based on distance to the enemy
1069         if(bot_custom_weapon){
1070
1071                 // Choose weapons for far distance
1072                 if ( distance > bot_distance_far ) {
1073                         for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1074                                 w = bot_weapons_far[i];
1075                                 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1076                                 {
1077                                         if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1078                                                 continue;
1079                                         PS(this).m_switchweapon = Weapons_from(w);
1080                                         return;
1081                                 }
1082                         }
1083                 }
1084
1085                 // Choose weapons for mid distance
1086                 if ( distance > bot_distance_close) {
1087                         for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1088                                 w = bot_weapons_mid[i];
1089                                 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1090                                 {
1091                                         if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1092                                                 continue;
1093                                         PS(this).m_switchweapon = Weapons_from(w);
1094                                         return;
1095                                 }
1096                         }
1097                 }
1098
1099                 // Choose weapons for close distance
1100                 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1101                         w = bot_weapons_close[i];
1102                         if ( client_hasweapon(this, Weapons_from(w), true, false) )
1103                         {
1104                                 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1105                                         continue;
1106                                 PS(this).m_switchweapon = Weapons_from(w);
1107                                 return;
1108                         }
1109                 }
1110         }
1111 }
1112
1113 void havocbot_aim(entity this)
1114 {
1115         vector myvel, enemyvel;
1116 //      if(this.flags & FL_INWATER)
1117 //              return;
1118         if (time < this.nextaim)
1119                 return;
1120         this.nextaim = time + 0.1;
1121         myvel = this.velocity;
1122         if (!this.waterlevel)
1123                 myvel.z = 0;
1124         if (this.enemy)
1125         {
1126                 enemyvel = this.enemy.velocity;
1127                 if (!this.enemy.waterlevel)
1128                         enemyvel.z = 0;
1129                 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1130         }
1131         else
1132                 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1133 }
1134
1135 bool havocbot_moveto_refresh_route(entity this)
1136 {
1137         // Refresh path to goal if necessary
1138         entity wp;
1139         wp = this.havocbot_personal_waypoint;
1140         navigation_goalrating_start(this);
1141         navigation_routerating(this, wp, 10000, 10000);
1142         navigation_goalrating_end(this);
1143         return this.navigation_hasgoals;
1144 }
1145
1146 float havocbot_moveto(entity this, vector pos)
1147 {
1148         entity wp;
1149
1150         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1151         {
1152                 // Step 4: Move to waypoint
1153                 if(this.havocbot_personal_waypoint==NULL)
1154                 {
1155                         LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1156                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1157                         return CMD_STATUS_ERROR;
1158                 }
1159
1160                 if (!bot_strategytoken_taken)
1161                 if(this.havocbot_personal_waypoint_searchtime<time)
1162                 {
1163                         bot_strategytoken_taken = true;
1164                         if(havocbot_moveto_refresh_route(this))
1165                         {
1166                                 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1167                                 this.havocbot_personal_waypoint_searchtime = time + 10;
1168                                 this.havocbot_personal_waypoint_failcounter = 0;
1169                         }
1170                         else
1171                         {
1172                                 this.havocbot_personal_waypoint_failcounter += 1;
1173                                 this.havocbot_personal_waypoint_searchtime = time + 2;
1174                                 if(this.havocbot_personal_waypoint_failcounter >= 30)
1175                                 {
1176                                         LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1177                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1178                                         delete(this.havocbot_personal_waypoint);
1179                                         return CMD_STATUS_ERROR;
1180                                 }
1181                                 else
1182                                         LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1183                         }
1184                 }
1185
1186                 if(autocvar_bot_debug_goalstack)
1187                         debuggoalstack(this);
1188
1189                 // Heading
1190                 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1191                 dir.z = 0;
1192                 bot_aimdir(this, dir, -1);
1193
1194                 // Go!
1195                 havocbot_movetogoal(this);
1196
1197                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1198                 {
1199                         // Step 5: Waypoint reached
1200                         LOG_TRACE(this.netname, "'s personal waypoint reached");
1201                         delete(this.havocbot_personal_waypoint);
1202                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1203                         return CMD_STATUS_FINISHED;
1204                 }
1205
1206                 return CMD_STATUS_EXECUTING;
1207         }
1208
1209         // Step 2: Linking waypoint
1210         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1211         {
1212                 // Wait until it is linked
1213                 if(!this.havocbot_personal_waypoint.wplinked)
1214                 {
1215                         LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1216                         return CMD_STATUS_EXECUTING;
1217                 }
1218
1219                 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1220                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1221                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1222
1223                 // Step 3: Route to waypoint
1224                 LOG_TRACE(this.netname, " walking to its personal waypoint");
1225
1226                 return CMD_STATUS_EXECUTING;
1227         }
1228
1229         // Step 1: Spawning waypoint
1230         wp = waypoint_spawnpersonal(this, pos);
1231         if(wp==NULL)
1232         {
1233                 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1234                 return CMD_STATUS_ERROR;
1235         }
1236
1237         this.havocbot_personal_waypoint = wp;
1238         this.havocbot_personal_waypoint_failcounter = 0;
1239         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1240
1241         // if pos is inside a teleport, then let's mark it as teleport waypoint
1242         IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1243         {
1244                 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1245                 this.lastteleporttime = 0;
1246         });
1247
1248 /*
1249         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1250                 print("routing to a teleporter\n");
1251         else
1252                 print("routing to a non-teleporter\n");
1253 */
1254
1255         return CMD_STATUS_EXECUTING;
1256 }
1257
1258 float havocbot_resetgoal(entity this)
1259 {
1260         navigation_clearroute(this);
1261         return CMD_STATUS_FINISHED;
1262 }
1263
1264 void havocbot_setupbot(entity this)
1265 {
1266         this.bot_ai = havocbot_ai;
1267         this.cmd_moveto = havocbot_moveto;
1268         this.cmd_resetgoal = havocbot_resetgoal;
1269
1270         havocbot_chooserole(this);
1271 }
1272
1273 vector havocbot_dodge(entity this)
1274 {
1275         // LordHavoc: disabled because this is too expensive
1276         return '0 0 0';
1277 #if 0
1278         entity head;
1279         vector dodge, v, n;
1280         float danger, bestdanger, vl, d;
1281         dodge = '0 0 0';
1282         bestdanger = -20;
1283         // check for dangerous objects near bot or approaching bot
1284         head = findchainfloat(bot_dodge, true);
1285         while(head)
1286         {
1287                 if (head.owner != this)
1288                 {
1289                         vl = vlen(head.velocity);
1290                         if (vl > autocvar_sv_maxspeed * 0.3)
1291                         {
1292                                 n = normalize(head.velocity);
1293                                 v = this.origin - head.origin;
1294                                 d = v * n;
1295                                 if (d > (0 - head.bot_dodgerating))
1296                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1297                                 {
1298                                         // calculate direction and distance from the flight path, by removing the forward axis
1299                                         v = v - (n * (v * n));
1300                                         danger = head.bot_dodgerating - vlen(v);
1301                                         if (bestdanger < danger)
1302                                         {
1303                                                 bestdanger = danger;
1304                                                 // dodge to the side of the object
1305                                                 dodge = normalize(v);
1306                                         }
1307                                 }
1308                         }
1309                         else
1310                         {
1311                                 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1312                                 if (bestdanger < danger)
1313                                 {
1314                                         bestdanger = danger;
1315                                         dodge = normalize(this.origin - head.origin);
1316                                 }
1317                         }
1318                 }
1319                 head = head.chain;
1320         }
1321         return dodge;
1322 #endif
1323 }