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