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