]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/havocbot.qc
0613ab403d9f73e7c9a2ba03aa42580668d740d0
[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 != NULL && ((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!=NULL)
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                                                 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
368                                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
369                                         }
370                                         else
371                                         {
372                                                 this.aistatus |= AI_STATUS_RUNNING;
373                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
374                                         }
375                                 }
376                         }
377                         else
378                         {
379                                 this.bot_timelastseengoal = time;
380                         }
381         }
382         else
383         {
384                 this.bot_timelastseengoal = 0;
385         }
386
387 #if 0
388         // Release jump button
389         if(!cvar("sv_pogostick"))
390         if((IS_ONGROUND(this)) == 0)
391         {
392                 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
393                         PHYS_INPUT_BUTTON_JUMP(this) = false;
394
395                 // Strafe
396                 if(this.aistatus & AI_STATUS_RUNNING)
397                 if(vlen(this.velocity)>maxspeed)
398                 {
399                         deviation = vectoangles(dir) - vectoangles(this.velocity);
400                         while (deviation.y < -180) deviation.y = deviation.y + 360;
401                         while (deviation.y > 180) deviation.y = deviation.y - 360;
402
403                         if(fabs(deviation.y)>10)
404                                 this.movement_x = 0;
405
406                         if(deviation.y>10)
407                                 this.movement_y = maxspeed * -1;
408                         else if(deviation.y<10)
409                                 this.movement_y = maxspeed;
410
411                 }
412         }
413 #endif
414 }
415
416 void havocbot_movetogoal(entity this)
417 {
418         vector destorg;
419         vector diff;
420         vector dir;
421         vector flatdir;
422         vector m1;
423         vector m2;
424         vector evadeobstacle;
425         vector evadelava;
426         float s;
427         float maxspeed;
428         vector gco;
429         //float dist;
430         vector dodge;
431         //if (this.goalentity)
432         //      te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
433         this.movement = '0 0 0';
434         maxspeed = autocvar_sv_maxspeed;
435
436         // Jetpack navigation
437         if(this.goalcurrent)
438         if(this.navigation_jetpack_goal)
439         if(this.goalcurrent==this.navigation_jetpack_goal)
440         if(this.ammo_fuel)
441         {
442                 if(autocvar_bot_debug_goalstack)
443                 {
444                         debuggoalstack(this);
445                         te_wizspike(this.navigation_jetpack_point);
446                 }
447
448                 // Take off
449                 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
450                 {
451                         // Brake almost completely so it can get a good direction
452                         if(vdist(this.velocity, >, 10))
453                                 return;
454                         this.aistatus |= AI_STATUS_JETPACK_FLYING;
455                 }
456
457                 makevectors(this.v_angle.y * '0 1 0');
458                 dir = normalize(this.navigation_jetpack_point - this.origin);
459
460                 // Landing
461                 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
462                 {
463                         // Calculate brake distance in xy
464                         float db, v, d;
465                         vector dxy;
466
467                         dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
468                         d = vlen(dxy);
469                         v = vlen(this.velocity -  this.velocity.z * '0 0 1');
470                         db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
471                 //      dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
472                         if(d < db || d < 500)
473                         {
474                                 // Brake
475                                 if(fabs(this.velocity.x)>maxspeed*0.3)
476                                 {
477                                         this.movement_x = dir * v_forward * -maxspeed;
478                                         return;
479                                 }
480                                 // Switch to normal mode
481                                 this.navigation_jetpack_goal = NULL;
482                                 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
483                                 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
484                                 return;
485                         }
486                 }
487                 else if(checkpvs(this.origin,this.goalcurrent))
488                 {
489                         // If I can see the goal switch to landing code
490                         this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
491                         this.aistatus |= AI_STATUS_JETPACK_LANDING;
492                         return;
493                 }
494
495                 // Flying
496                 PHYS_INPUT_BUTTON_HOOK(this) = true;
497                 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
498                 {
499                         this.movement_x = dir * v_forward * maxspeed;
500                         this.movement_y = dir * v_right * maxspeed;
501                 }
502                 return;
503         }
504
505         // Handling of jump pads
506         if(this.jumppadcount)
507         {
508                 // If got stuck on the jump pad try to reach the farthest visible waypoint
509                 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
510                 {
511                         if(fabs(this.velocity.z)<50)
512                         {
513                                 entity newgoal = NULL;
514                                 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
515                                 {
516                                         traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
517
518                                         if(trace_fraction < 1)
519                                                 continue;
520
521                                         if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
522                                                 newgoal = it;
523                                 });
524
525                                 if(newgoal)
526                                 {
527                                         this.ignoregoal = this.goalcurrent;
528                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
529                                         navigation_clearroute(this);
530                                         navigation_routetogoal(this, newgoal, this.origin);
531                                         this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
532                                 }
533                         }
534                         else
535                                 return;
536                 }
537                 else
538                 {
539                         if(this.velocity.z>0)
540                         {
541                                 float threshold;
542                                 vector velxy = this.velocity; velxy_z = 0;
543                                 threshold = maxspeed * 0.2;
544                                 if(vdist(velxy, <, threshold))
545                                 {
546                                         LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
547                                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
548                                 }
549                                 return;
550                         }
551
552                         // Don't chase players while using a jump pad
553                         if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
554                                 return;
555                 }
556         }
557         else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
558                 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
559
560         // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
561         if(skill>6)
562         if (!(IS_ONGROUND(this)))
563         {
564                 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
565                 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
566                 if(this.items & IT_JETPACK)
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 + '0 0 1' ))
570                         {
571                                 if(this.velocity.z<0)
572                                 {
573                                         PHYS_INPUT_BUTTON_HOOK(this) = true;
574                                 }
575                         }
576                         else
577                                 PHYS_INPUT_BUTTON_HOOK(this) = true;
578
579                         // If there is no goal try to move forward
580
581                         if(this.goalcurrent==NULL)
582                                 dir = v_forward;
583                         else
584                                 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
585
586                         vector xyvelocity = this.velocity; xyvelocity_z = 0;
587                         float xyspeed = xyvelocity * dir;
588
589                         if(xyspeed < (maxspeed / 2))
590                         {
591                                 makevectors(this.v_angle.y * '0 1 0');
592                                 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
593                                 if(trace_fraction==1)
594                                 {
595                                         this.movement_x = dir * v_forward * maxspeed;
596                                         this.movement_y = dir * v_right * maxspeed;
597                                         if (skill < 10)
598                                                 havocbot_keyboard_movement(this, this.origin + dir * 100);
599                                 }
600                         }
601
602                         this.havocbot_blockhead = true;
603
604                         return;
605                 }
606                 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
607                 {
608                         if(this.velocity.z < 0)
609                         {
610                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
611                                 {
612                                         .entity weaponentity = weaponentities[slot];
613
614                                         if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
615                                                 continue;
616
617                                         if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
618                                         {
619                                                 this.movement_x = maxspeed;
620
621                                                 if(this.rocketjumptime)
622                                                 {
623                                                         if(time > this.rocketjumptime)
624                                                         {
625                                                                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
626                                                                 this.rocketjumptime = 0;
627                                                         }
628                                                         return;
629                                                 }
630
631                                                 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
632                                                 this.v_angle_x = 90;
633                                                 PHYS_INPUT_BUTTON_ATCK(this) = true;
634                                                 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
635                                                 return;
636                                         }
637                                 }
638                         }
639                 }
640                 else
641                 {
642                         // If there is no goal try to move forward
643                         if(this.goalcurrent==NULL)
644                                 this.movement_x = maxspeed;
645                 }
646         }
647
648         // If we are under water with no goals, swim up
649         if(this.waterlevel)
650         if(this.goalcurrent==NULL)
651         {
652                 dir = '0 0 0';
653                 if(this.waterlevel>WATERLEVEL_SWIMMING)
654                         dir.z = 1;
655                 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
656                         PHYS_INPUT_BUTTON_JUMP(this) = true;
657                 else
658                         PHYS_INPUT_BUTTON_JUMP(this) = false;
659                 makevectors(this.v_angle.y * '0 1 0');
660                 this.movement_x = dir * v_forward * maxspeed;
661                 this.movement_y = dir * v_right * maxspeed;
662                 this.movement_z = dir * v_up * maxspeed;
663         }
664
665         // if there is nowhere to go, exit
666         if (this.goalcurrent == NULL)
667                 return;
668
669         if (this.goalcurrent)
670                 navigation_poptouchedgoals(this);
671
672         // if ran out of goals try to use an alternative goal or get a new strategy asap
673         if(this.goalcurrent == NULL)
674         {
675                 this.bot_strategytime = 0;
676                 return;
677         }
678
679
680         if(autocvar_bot_debug_goalstack)
681                 debuggoalstack(this);
682
683         m1 = this.goalcurrent.origin + this.goalcurrent.mins;
684         m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
685         destorg = this.origin;
686         destorg.x = bound(m1_x, destorg.x, m2_x);
687         destorg.y = bound(m1_y, destorg.y, m2_y);
688         destorg.z = bound(m1_z, destorg.z, m2_z);
689         diff = destorg - this.origin;
690         //dist = vlen(diff);
691         dir = normalize(diff);
692         flatdir = diff;flatdir.z = 0;
693         flatdir = normalize(flatdir);
694         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
695
696         //if (this.bot_dodgevector_time < time)
697         {
698         //      this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
699         //      this.bot_dodgevector_jumpbutton = 1;
700                 evadeobstacle = '0 0 0';
701                 evadelava = '0 0 0';
702
703                 if (this.waterlevel)
704                 {
705                         if(this.waterlevel>WATERLEVEL_SWIMMING)
706                         {
707                         //      flatdir_z = 1;
708                                 this.aistatus |= AI_STATUS_OUT_WATER;
709                         }
710                         else
711                         {
712                                 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
713                                         ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
714                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
715                                 else
716                                         PHYS_INPUT_BUTTON_JUMP(this) = false;
717                         }
718                         dir = normalize(flatdir);
719                         makevectors(this.v_angle.y * '0 1 0');
720                 }
721                 else
722                 {
723                         if(this.aistatus & AI_STATUS_OUT_WATER)
724                                 this.aistatus &= ~AI_STATUS_OUT_WATER;
725
726                         // jump if going toward an obstacle that doesn't look like stairs we
727                         // can walk up directly
728                         tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
729                         if (trace_fraction < 1)
730                         if (trace_plane_normal.z < 0.7)
731                         {
732                                 s = trace_fraction;
733                                 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
734                                 if (trace_fraction < s + 0.01)
735                                 if (trace_plane_normal.z < 0.7)
736                                 {
737                                         s = trace_fraction;
738                                         tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
739                                         if (trace_fraction > s)
740                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
741                                 }
742                         }
743
744                         // avoiding dangers and obstacles
745                         vector dst_ahead, dst_down;
746                         makevectors(this.v_angle.y * '0 1 0');
747                         dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
748                         dst_down = dst_ahead - '0 0 1500';
749
750                         // Look ahead
751                         traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
752
753                         // Check head-banging against walls
754                         if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
755                         {
756                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
757                                 if(this.facingwalltime && time > this.facingwalltime)
758                                 {
759                                         this.ignoregoal = this.goalcurrent;
760                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
761                                         this.bot_strategytime = 0;
762                                         return;
763                                 }
764                                 else
765                                 {
766                                         this.facingwalltime = time + 0.05;
767                                 }
768                         }
769                         else
770                         {
771                                 this.facingwalltime = 0;
772
773                                 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
774                                 {
775                                         this.ignoregoal = NULL;
776                                         this.ignoregoaltime = 0;
777                                 }
778                         }
779
780                         // Check for water/slime/lava and dangerous edges
781                         // (only when the bot is on the ground or jumping intentionally)
782                         this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
783
784                         if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
785                         if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
786                         {
787                                 // Look downwards
788                                 traceline(dst_ahead , dst_down, true, NULL);
789                         //      te_lightning2(NULL, this.origin, dst_ahead);    // Draw "ahead" look
790                         //      te_lightning2(NULL, dst_ahead, dst_down);               // Draw "downwards" look
791                                 if(trace_endpos.z < this.origin.z + this.mins.z)
792                                 {
793                                         s = pointcontents(trace_endpos + '0 0 1');
794                                         if (s != CONTENT_SOLID)
795                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
796                                                 evadelava = normalize(this.velocity) * -1;
797                                         else if (s == CONTENT_SKY)
798                                                 evadeobstacle = normalize(this.velocity) * -1;
799                                         else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
800                                                                 this.goalcurrent.absmin, this.goalcurrent.absmax))
801                                         {
802                                                 // if ain't a safe goal with "holes" (like the jumpad on soylent)
803                                                 // and there is a trigger_hurt below
804                                                 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
805                                                 {
806                                                         // Remove dangerous dynamic goals from stack
807                                                         LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
808                                                         navigation_clearroute(this);
809                                                         return;
810                                                 }
811                                         }
812                                 }
813                         }
814
815                         dir = flatdir;
816                         evadeobstacle.z = 0;
817                         evadelava.z = 0;
818                         makevectors(this.v_angle.y * '0 1 0');
819
820                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
821                                 this.aistatus |= AI_STATUS_DANGER_AHEAD;
822                 }
823
824                 dodge = havocbot_dodge(this);
825                 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
826                 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
827                 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
828                 if(IS_PLAYER(trace_ent))
829                         dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
830
831                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
832         //      this.bot_dodgevector = dir;
833         //      this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
834         }
835
836         if(time < this.ladder_time)
837         {
838                 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
839                 {
840                         if(this.origin.z + this.mins.z  < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
841                                 dir.z = 1;
842                 }
843                 else
844                 {
845                         if(this.origin.z + this.mins.z  > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
846                                 dir.z = -1;
847                 }
848         }
849
850         //dir = this.bot_dodgevector;
851         //if (this.bot_dodgevector_jumpbutton)
852         //      PHYS_INPUT_BUTTON_JUMP(this) = true;
853         this.movement_x = dir * v_forward * maxspeed;
854         this.movement_y = dir * v_right * maxspeed;
855         this.movement_z = dir * v_up * maxspeed;
856
857         // Emulate keyboard interface
858         if (skill < 10)
859                 havocbot_keyboard_movement(this, destorg);
860
861         // Bunnyhop!
862 //      if(this.aistatus & AI_STATUS_ROAMING)
863         if(this.goalcurrent)
864         if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
865                 havocbot_bunnyhop(this, dir);
866
867         if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
868         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;
869         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);
870 }
871
872 entity havocbot_gettarget(entity this, bool secondary)
873 {
874         entity best = NULL;
875         vector eye = CENTER_OR_VIEWOFS(this);
876         IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
877         {
878                 vector v = CENTER_OR_VIEWOFS(it);
879                 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
880                 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
881                 if(bot_shouldattack(this, it))
882                 {
883                         traceline(eye, v, true, this);
884                         if (trace_ent == it || trace_fraction >= 1)
885                                 best = it;
886                 }
887         });
888
889         return best;
890 }
891
892 void havocbot_chooseenemy(entity this)
893 {
894         entity head, best, head2;
895         float rating, bestrating, hf;
896         vector eye, v;
897         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
898         {
899                 this.enemy = NULL;
900                 return;
901         }
902         if (this.enemy)
903         {
904                 if (!bot_shouldattack(this, this.enemy))
905                 {
906                         // enemy died or something, find a new target
907                         this.enemy = NULL;
908                         this.havocbot_chooseenemy_finished = time;
909                 }
910                 else if (this.havocbot_stickenemy)
911                 {
912                         // tracking last chosen enemy
913                         // if enemy is visible
914                         // and not really really far away
915                         // and we're not severely injured
916                         // then keep tracking for a half second into the future
917                         traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
918                         if (trace_ent == this.enemy || trace_fraction == 1)
919                         if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
920                         if (this.health > 30)
921                         {
922                                 // remain tracking him for a shot while (case he went after a small corner or pilar
923                                 this.havocbot_chooseenemy_finished = time + 0.5;
924                                 return;
925                         }
926                         // enemy isn't visible, or is far away, or we're injured severely
927                         // so stop preferring this enemy
928                         // (it will still take a half second until a new one is chosen)
929                         this.havocbot_stickenemy = 0;
930                 }
931         }
932         if (time < this.havocbot_chooseenemy_finished)
933                 return;
934         this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
935         eye = this.origin + this.view_ofs;
936         best = NULL;
937         bestrating = 100000000;
938         head = head2 = findchainfloat(bot_attack, true);
939
940         // Backup hit flags
941         hf = this.dphitcontentsmask;
942
943         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
944
945         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
946
947         bool scan_transparent = false;
948         bool scan_secondary_targets = false;
949         bool have_secondary_targets = false;
950         while(true)
951         {
952                 scan_secondary_targets = false;
953 LABEL(scan_targets)
954                 for( ; head; head = head.chain)
955                 {
956                         if(!scan_secondary_targets)
957                         {
958                                 if(head.classname == "misc_breakablemodel")
959                                 {
960                                         have_secondary_targets = true;
961                                         continue;
962                                 }
963                         }
964                         else
965                         {
966                                 if(head.classname != "misc_breakablemodel")
967                                         continue;
968                         }
969
970                         v = (head.absmin + head.absmax) * 0.5;
971                         rating = vlen(v - eye);
972                         if (rating<autocvar_bot_ai_enemydetectionradius)
973                         if (bestrating > rating)
974                         if (bot_shouldattack(this, head))
975                         {
976                                 traceline(eye, v, true, this);
977                                 if (trace_ent == head || trace_fraction >= 1)
978                                 {
979                                         best = head;
980                                         bestrating = rating;
981                                 }
982                         }
983                 }
984
985                 if(!best && have_secondary_targets && !scan_secondary_targets)
986                 {
987                         scan_secondary_targets = true;
988                         // restart the loop
989                         head = head2;
990                         bestrating = 100000000;
991                         goto scan_targets;
992                 }
993
994                 // I want to do a second scan if no enemy was found or I don't have weapons
995                 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
996                 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
997                         break;
998                 if(scan_transparent)
999                         break;
1000
1001                 // Set flags to see through transparent objects
1002                 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1003
1004                 head = head2;
1005                 scan_transparent = true;
1006         }
1007
1008         // Restore hit flags
1009         this.dphitcontentsmask = hf;
1010
1011         this.enemy = best;
1012         this.havocbot_stickenemy = true;
1013         if(best && best.classname == "misc_breakablemodel")
1014                 this.havocbot_stickenemy = false;
1015 }
1016
1017 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1018 {
1019         // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1020         // so skip this for them, or they'll never get to reload their weapons at all.
1021         // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1022         if(skill < 5)
1023                 return false;
1024
1025         // if this weapon is scheduled for reloading, don't switch to it during combat
1026         if (this.(weaponentity).weapon_load[new_weapon] < 0)
1027         {
1028                 bool other_weapon_available = false;
1029                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1030                         if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1031                                 other_weapon_available = true;
1032                 ));
1033                 if(other_weapon_available)
1034                         return true;
1035         }
1036
1037         return false;
1038 }
1039
1040 void havocbot_chooseweapon(entity this, .entity weaponentity)
1041 {
1042         int i;
1043
1044         // ;)
1045         if(g_weaponarena_weapons == WEPSET(TUBA))
1046         {
1047                 this.(weaponentity).m_switchweapon = WEP_TUBA;
1048                 return;
1049         }
1050
1051         // TODO: clean this up by moving it to weapon code
1052         if(this.enemy==NULL)
1053         {
1054                 // If no weapon was chosen get the first available weapon
1055                 if(this.(weaponentity).m_weapon==WEP_Null)
1056                 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1057                         if(client_hasweapon(this, it, weaponentity, true, false))
1058                         {
1059                                 this.(weaponentity).m_switchweapon = it;
1060                                 return;
1061                         }
1062                 ));
1063                 return;
1064         }
1065
1066         // Do not change weapon during the next second after a combo
1067         float f = time - this.lastcombotime;
1068         if(f < 1)
1069                 return;
1070
1071         float w;
1072         float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1073
1074         // Should it do a weapon combo?
1075         float af, ct, combo_time, combo;
1076
1077         af = ATTACK_FINISHED(this, 0);
1078         ct = autocvar_bot_ai_weapon_combo_threshold;
1079
1080         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1081         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1082         combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1083
1084         combo = false;
1085
1086         if(autocvar_bot_ai_weapon_combo)
1087         if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1088         if(af > combo_time)
1089         {
1090                 combo = true;
1091                 this.lastcombotime = time;
1092         }
1093
1094         distance *= pow(2, this.bot_rangepreference);
1095
1096         // Custom weapon list based on distance to the enemy
1097         if(bot_custom_weapon){
1098
1099                 // Choose weapons for far distance
1100                 if ( distance > bot_distance_far ) {
1101                         for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1102                                 w = bot_weapons_far[i];
1103                                 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1104                                 {
1105                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1106                                                 continue;
1107                                         this.(weaponentity).m_switchweapon = Weapons_from(w);
1108                                         return;
1109                                 }
1110                         }
1111                 }
1112
1113                 // Choose weapons for mid distance
1114                 if ( distance > bot_distance_close) {
1115                         for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1116                                 w = bot_weapons_mid[i];
1117                                 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1118                                 {
1119                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1120                                                 continue;
1121                                         this.(weaponentity).m_switchweapon = Weapons_from(w);
1122                                         return;
1123                                 }
1124                         }
1125                 }
1126
1127                 // Choose weapons for close distance
1128                 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1129                         w = bot_weapons_close[i];
1130                         if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1131                         {
1132                                 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1133                                         continue;
1134                                 this.(weaponentity).m_switchweapon = Weapons_from(w);
1135                                 return;
1136                         }
1137                 }
1138         }
1139 }
1140
1141 void havocbot_aim(entity this)
1142 {
1143         vector myvel, enemyvel;
1144 //      if(this.flags & FL_INWATER)
1145 //              return;
1146         if (time < this.nextaim)
1147                 return;
1148         this.nextaim = time + 0.1;
1149         myvel = this.velocity;
1150         if (!this.waterlevel)
1151                 myvel.z = 0;
1152         if (this.enemy)
1153         {
1154                 enemyvel = this.enemy.velocity;
1155                 if (!this.enemy.waterlevel)
1156                         enemyvel.z = 0;
1157                 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1158         }
1159         else
1160                 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1161 }
1162
1163 bool havocbot_moveto_refresh_route(entity this)
1164 {
1165         // Refresh path to goal if necessary
1166         entity wp;
1167         wp = this.havocbot_personal_waypoint;
1168         navigation_goalrating_start(this);
1169         navigation_routerating(this, wp, 10000, 10000);
1170         navigation_goalrating_end(this);
1171         return this.navigation_hasgoals;
1172 }
1173
1174 float havocbot_moveto(entity this, vector pos)
1175 {
1176         entity wp;
1177
1178         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1179         {
1180                 // Step 4: Move to waypoint
1181                 if(this.havocbot_personal_waypoint==NULL)
1182                 {
1183                         LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1184                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1185                         return CMD_STATUS_ERROR;
1186                 }
1187
1188                 if (!bot_strategytoken_taken)
1189                 if(this.havocbot_personal_waypoint_searchtime<time)
1190                 {
1191                         bot_strategytoken_taken = true;
1192                         if(havocbot_moveto_refresh_route(this))
1193                         {
1194                                 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1195                                 this.havocbot_personal_waypoint_searchtime = time + 10;
1196                                 this.havocbot_personal_waypoint_failcounter = 0;
1197                         }
1198                         else
1199                         {
1200                                 this.havocbot_personal_waypoint_failcounter += 1;
1201                                 this.havocbot_personal_waypoint_searchtime = time + 2;
1202                                 if(this.havocbot_personal_waypoint_failcounter >= 30)
1203                                 {
1204                                         LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1205                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1206                                         delete(this.havocbot_personal_waypoint);
1207                                         return CMD_STATUS_ERROR;
1208                                 }
1209                                 else
1210                                         LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1211                         }
1212                 }
1213
1214                 if(autocvar_bot_debug_goalstack)
1215                         debuggoalstack(this);
1216
1217                 // Heading
1218                 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1219                 dir.z = 0;
1220                 bot_aimdir(this, dir, -1);
1221
1222                 // Go!
1223                 havocbot_movetogoal(this);
1224
1225                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1226                 {
1227                         // Step 5: Waypoint reached
1228                         LOG_TRACE(this.netname, "'s personal waypoint reached");
1229                         delete(this.havocbot_personal_waypoint);
1230                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1231                         return CMD_STATUS_FINISHED;
1232                 }
1233
1234                 return CMD_STATUS_EXECUTING;
1235         }
1236
1237         // Step 2: Linking waypoint
1238         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1239         {
1240                 // Wait until it is linked
1241                 if(!this.havocbot_personal_waypoint.wplinked)
1242                 {
1243                         LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1244                         return CMD_STATUS_EXECUTING;
1245                 }
1246
1247                 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1248                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1249                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1250
1251                 // Step 3: Route to waypoint
1252                 LOG_TRACE(this.netname, " walking to its personal waypoint");
1253
1254                 return CMD_STATUS_EXECUTING;
1255         }
1256
1257         // Step 1: Spawning waypoint
1258         wp = waypoint_spawnpersonal(this, pos);
1259         if(wp==NULL)
1260         {
1261                 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1262                 return CMD_STATUS_ERROR;
1263         }
1264
1265         this.havocbot_personal_waypoint = wp;
1266         this.havocbot_personal_waypoint_failcounter = 0;
1267         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1268
1269         // if pos is inside a teleport, then let's mark it as teleport waypoint
1270         IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1271         {
1272                 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1273                 this.lastteleporttime = 0;
1274         });
1275
1276 /*
1277         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1278                 print("routing to a teleporter\n");
1279         else
1280                 print("routing to a non-teleporter\n");
1281 */
1282
1283         return CMD_STATUS_EXECUTING;
1284 }
1285
1286 float havocbot_resetgoal(entity this)
1287 {
1288         navigation_clearroute(this);
1289         return CMD_STATUS_FINISHED;
1290 }
1291
1292 void havocbot_setupbot(entity this)
1293 {
1294         this.bot_ai = havocbot_ai;
1295         this.cmd_moveto = havocbot_moveto;
1296         this.cmd_resetgoal = havocbot_resetgoal;
1297
1298         havocbot_chooserole(this);
1299 }
1300
1301 vector havocbot_dodge(entity this)
1302 {
1303         // LordHavoc: disabled because this is too expensive
1304         return '0 0 0';
1305 #if 0
1306         entity head;
1307         vector dodge, v, n;
1308         float danger, bestdanger, vl, d;
1309         dodge = '0 0 0';
1310         bestdanger = -20;
1311         // check for dangerous objects near bot or approaching bot
1312         head = findchainfloat(bot_dodge, true);
1313         while(head)
1314         {
1315                 if (head.owner != this)
1316                 {
1317                         vl = vlen(head.velocity);
1318                         if (vl > autocvar_sv_maxspeed * 0.3)
1319                         {
1320                                 n = normalize(head.velocity);
1321                                 v = this.origin - head.origin;
1322                                 d = v * n;
1323                                 if (d > (0 - head.bot_dodgerating))
1324                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1325                                 {
1326                                         // calculate direction and distance from the flight path, by removing the forward axis
1327                                         v = v - (n * (v * n));
1328                                         danger = head.bot_dodgerating - vlen(v);
1329                                         if (bestdanger < danger)
1330                                         {
1331                                                 bestdanger = danger;
1332                                                 // dodge to the side of the object
1333                                                 dodge = normalize(v);
1334                                         }
1335                                 }
1336                         }
1337                         else
1338                         {
1339                                 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1340                                 if (bestdanger < danger)
1341                                 {
1342                                         bestdanger = danger;
1343                                         dodge = normalize(this.origin - head.origin);
1344                                 }
1345                         }
1346                 }
1347                 head = head.chain;
1348         }
1349         return dodge;
1350 #endif
1351 }