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