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