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