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