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