]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/bot/default/havocbot/havocbot.qc
Merge branch 'master' into terencehill/bot_waypoints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / bot / default / havocbot / havocbot.qc
1 #include "havocbot.qh"
2
3 #include <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         if(curr_dist > this.goalcurrent_distance)
434         {
435                 if(!this.goalcurrent_distance_time)
436                         this.goalcurrent_distance_time = time;
437                 else if (time - this.goalcurrent_distance_time > 0.5)
438                         return true;
439         }
440         else
441         {
442                 // reduce it a little bit so it works even with very small approaches to the goal
443                 this.goalcurrent_distance = max(20, curr_dist - 15);
444                 this.goalcurrent_distance_time = 0;
445         }
446         return false;
447 }
448
449 void havocbot_movetogoal(entity this)
450 {
451         vector destorg = '0 0 0';
452         vector diff;
453         vector dir;
454         vector flatdir;
455         vector evadeobstacle;
456         vector evadelava;
457         float maxspeed;
458         vector gco;
459         //float dist;
460         vector dodge;
461         //if (this.goalentity)
462         //      te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
463         CS(this).movement = '0 0 0';
464         maxspeed = autocvar_sv_maxspeed;
465
466         // Jetpack navigation
467         if(this.goalcurrent)
468         if(this.navigation_jetpack_goal)
469         if(this.goalcurrent==this.navigation_jetpack_goal)
470         if(this.ammo_fuel)
471         {
472                 if(autocvar_bot_debug_goalstack)
473                 {
474                         debuggoalstack(this);
475                         te_wizspike(this.navigation_jetpack_point);
476                 }
477
478                 // Take off
479                 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
480                 {
481                         // Brake almost completely so it can get a good direction
482                         if(vdist(this.velocity, >, 10))
483                                 return;
484                         this.aistatus |= AI_STATUS_JETPACK_FLYING;
485                 }
486
487                 makevectors(this.v_angle.y * '0 1 0');
488                 dir = normalize(this.navigation_jetpack_point - this.origin);
489
490                 // Landing
491                 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
492                 {
493                         // Calculate brake distance in xy
494                         float db, v, d;
495                         vector dxy;
496
497                         dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
498                         d = vlen(dxy);
499                         v = vlen(this.velocity -  this.velocity.z * '0 0 1');
500                         db = ((v ** 2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
501                 //      dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
502                         if(d < db || d < 500)
503                         {
504                                 // Brake
505                                 if(fabs(this.velocity.x)>maxspeed*0.3)
506                                 {
507                                         CS(this).movement_x = dir * v_forward * -maxspeed;
508                                         return;
509                                 }
510                                 // Switch to normal mode
511                                 this.navigation_jetpack_goal = NULL;
512                                 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
513                                 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
514                                 return;
515                         }
516                 }
517                 else if(checkpvs(this.origin,this.goalcurrent))
518                 {
519                         // If I can see the goal switch to landing code
520                         this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
521                         this.aistatus |= AI_STATUS_JETPACK_LANDING;
522                         return;
523                 }
524
525                 // Flying
526                 PHYS_INPUT_BUTTON_HOOK(this) = true;
527                 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
528                 {
529                         CS(this).movement_x = dir * v_forward * maxspeed;
530                         CS(this).movement_y = dir * v_right * maxspeed;
531                 }
532                 return;
533         }
534
535         // Handling of jump pads
536         if(this.jumppadcount)
537         {
538                 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
539                 {
540                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
541                         navigation_poptouchedgoals(this);
542                         return;
543                 }
544                 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
545                 {
546                         // If got stuck on the jump pad try to reach the farthest visible waypoint
547                         // but with some randomness so it can try out different paths
548                         if(!this.goalcurrent)
549                         {
550                                 entity newgoal = NULL;
551                                 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
552                                 {
553                                         if(it.wpflags & WAYPOINTFLAG_TELEPORT)
554                                         if(it.origin.z < this.origin.z - 100 && vdist(vec2(it.origin - this.origin), <, 100))
555                                                 continue;
556
557                                         traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
558
559                                         if(trace_fraction < 1)
560                                                 continue;
561
562                                         if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
563                                                 newgoal = it;
564                                 });
565
566                                 if(newgoal)
567                                 {
568                                         this.ignoregoal = this.goalcurrent;
569                                         this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
570                                         navigation_clearroute(this);
571                                         navigation_routetogoal(this, newgoal, this.origin);
572                                         if(autocvar_bot_debug_goalstack)
573                                                 debuggoalstack(this);
574                                         this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
575                                 }
576                         }
577                         else
578                         {
579                                 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
580                                 if (this.origin.z > gco.z && vdist(vec2(this.velocity), <, autocvar_sv_maxspeed))
581                                         this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
582                                 else if(havocbot_checkgoaldistance(this, gco))
583                                 {
584                                         navigation_clearroute(this);
585                                         this.bot_strategytime = 0;
586                                 }
587                                 else
588                                         return;
589                         }
590                 }
591                 else
592                 {
593                         if(time - this.lastteleporttime > 0.2 && this.velocity.z > 0)
594                         {
595                                 vector velxy = this.velocity; velxy_z = 0;
596                                 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
597                                 {
598                                         LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
599                                         this.aistatus |= AI_STATUS_OUT_JUMPPAD;
600                                 }
601                                 return;
602                         }
603
604                         // Don't chase players while using a jump pad
605                         if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
606                                 return;
607                 }
608         }
609         else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
610                 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
611
612         // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
613         if(skill>6)
614         if (!(IS_ONGROUND(this)))
615         {
616                 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
617                 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
618                 if(this.items & IT_JETPACK)
619                 {
620                         tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
621                         if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
622                         {
623                                 if(this.velocity.z<0)
624                                 {
625                                         PHYS_INPUT_BUTTON_HOOK(this) = true;
626                                 }
627                         }
628                         else
629                                 PHYS_INPUT_BUTTON_HOOK(this) = true;
630
631                         // If there is no goal try to move forward
632
633                         if(this.goalcurrent==NULL)
634                                 dir = v_forward;
635                         else
636                                 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
637
638                         vector xyvelocity = this.velocity; xyvelocity_z = 0;
639                         float xyspeed = xyvelocity * dir;
640
641                         if(xyspeed < (maxspeed / 2))
642                         {
643                                 makevectors(this.v_angle.y * '0 1 0');
644                                 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
645                                 if(trace_fraction==1)
646                                 {
647                                         CS(this).movement_x = dir * v_forward * maxspeed;
648                                         CS(this).movement_y = dir * v_right * maxspeed;
649                                         if (skill < 10)
650                                                 havocbot_keyboard_movement(this, this.origin + dir * 100);
651                                 }
652                         }
653
654                         this.havocbot_blockhead = true;
655
656                         return;
657                 }
658                 else if(this.health > WEP_CVAR(devastator, damage) * 0.5 * ((this.strength_finished < time) ? autocvar_g_balance_powerup_strength_selfdamage : 1))
659                 {
660                         if(this.velocity.z < 0)
661                         {
662                                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
663                                 {
664                                         .entity weaponentity = weaponentities[slot];
665
666                                         if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
667                                                 continue;
668
669                                         if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
670                                         {
671                                                 CS(this).movement_x = maxspeed;
672
673                                                 if(this.rocketjumptime)
674                                                 {
675                                                         if(time > this.rocketjumptime)
676                                                         {
677                                                                 PHYS_INPUT_BUTTON_ATCK2(this) = true;
678                                                                 this.rocketjumptime = 0;
679                                                         }
680                                                         return;
681                                                 }
682
683                                                 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
684                                                 this.v_angle_x = 90;
685                                                 PHYS_INPUT_BUTTON_ATCK(this) = true;
686                                                 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
687                                                 return;
688                                         }
689                                 }
690                         }
691                 }
692                 else
693                 {
694                         // If there is no goal try to move forward
695                         if(this.goalcurrent==NULL)
696                                 CS(this).movement_x = maxspeed;
697                 }
698         }
699
700         // If we are under water with no goals, swim up
701         if(this.waterlevel)
702         if(this.goalcurrent==NULL)
703         {
704                 dir = '0 0 0';
705                 if(this.waterlevel>WATERLEVEL_SWIMMING)
706                         dir.z = 1;
707                 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
708                         PHYS_INPUT_BUTTON_JUMP(this) = true;
709                 else
710                         PHYS_INPUT_BUTTON_JUMP(this) = false;
711                 makevectors(this.v_angle.y * '0 1 0');
712                 CS(this).movement_x = dir * v_forward * maxspeed;
713                 CS(this).movement_y = dir * v_right * maxspeed;
714                 CS(this).movement_z = dir * v_up * maxspeed;
715         }
716
717         // if there is nowhere to go, exit
718         if (this.goalcurrent == NULL)
719                 return;
720
721
722         bool locked_goal = false;
723         if(this.goalentity && wasfreed(this.goalentity))
724         {
725                 navigation_clearroute(this);
726                 this.bot_strategytime = 0;
727                 return;
728         }
729         else if(this.goalentity.bot_pickup)
730         {
731                 if(this.goalentity.bot_pickup_respawning)
732                 {
733                         if(this.goalentity.solid) // item respawned
734                                 this.goalentity.bot_pickup_respawning = false;
735                         else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
736                         {
737                                 this.goalentity.bot_pickup_respawning = false;
738                                 navigation_clearroute(this);
739                                 this.bot_strategytime = 0;
740                                 return;
741                         }
742                         else if(this.goalentity == this.goalcurrent)
743                                 locked_goal = true; // wait for item to respawn
744                 }
745                 else if(!this.goalentity.solid)
746                 {
747                         navigation_clearroute(this);
748                         this.bot_strategytime = 0;
749                         return;
750                 }
751         }
752         if(!locked_goal)
753                 navigation_poptouchedgoals(this);
754
755         // if ran out of goals try to use an alternative goal or get a new strategy asap
756         if(this.goalcurrent == NULL)
757         {
758                 this.bot_strategytime = 0;
759                 return;
760         }
761
762
763         if(autocvar_bot_debug_goalstack)
764                 debuggoalstack(this);
765
766         bool bunnyhop_forbidden = false;;
767         SET_DESTCOORDS(this.goalcurrent, this.origin, destorg);
768
769         // in case bot ends up inside the teleport waypoint without touching
770         // the teleport itself, head to the teleport origin
771         if(this.goalcurrent.wpisbox && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin + eZ * this.mins.z, this.origin + eZ * this.maxs.z))
772         {
773                 bunnyhop_forbidden = true;
774                 destorg = this.goalcurrent.origin;
775                 if(destorg.z > this.origin.z)
776                         PHYS_INPUT_BUTTON_JUMP(this) = true;
777         }
778
779         diff = destorg - this.origin;
780         //dist = vlen(diff);
781         dir = normalize(diff);
782         flatdir = diff;flatdir.z = 0;
783         flatdir = normalize(flatdir);
784         gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
785
786         //if (this.bot_dodgevector_time < time)
787         {
788                 //this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
789                 //this.bot_dodgevector_jumpbutton = 1;
790                 evadeobstacle = '0 0 0';
791                 evadelava = '0 0 0';
792
793                 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
794                 makevectors(this.v_angle.y * '0 1 0');
795                 if (this.waterlevel)
796                 {
797                         if(this.waterlevel>WATERLEVEL_SWIMMING)
798                         {
799                                 if(!this.goalcurrent)
800                                         this.aistatus |= AI_STATUS_OUT_WATER;
801                                 else if(gco.z > this.origin.z)
802                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
803                         }
804                         else
805                         {
806                                 dir = flatdir;
807                                 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
808                                         ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
809                                         PHYS_INPUT_BUTTON_JUMP(this) = true;
810                                 else
811                                         PHYS_INPUT_BUTTON_JUMP(this) = false;
812                         }
813                 }
814                 else
815                 {
816                         float s;
817                         vector offset;
818                         if(this.aistatus & AI_STATUS_OUT_WATER)
819                                 this.aistatus &= ~AI_STATUS_OUT_WATER;
820
821                         // jump if going toward an obstacle that doesn't look like stairs we
822                         // can walk up directly
823                         offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
824                         tracebox(this.origin, this.mins, this.maxs, this.origin + offset, false, this);
825                         if (trace_fraction < 1)
826                         if (trace_plane_normal.z < 0.7)
827                         {
828                                 s = trace_fraction;
829                                 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + offset + stepheightvec, false, this);
830                                 if (trace_fraction < s + 0.01)
831                                 if (trace_plane_normal.z < 0.7)
832                                 {
833                                         s = trace_fraction;
834                                         tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + offset + jumpstepheightvec, false, this);
835                                         if (trace_fraction > s)
836                                                 PHYS_INPUT_BUTTON_JUMP(this) = true;
837                                 }
838                         }
839
840                         // if bot for some reason doesn't get close to the current goal find another one
841                         if(!this.jumppadcount && !IS_PLAYER(this.goalcurrent) && !(this.goalcurrent.bot_pickup_respawning && this.goalcurrent_distance < 50))
842                         if(havocbot_checkgoaldistance(this, gco))
843                         {
844                                 navigation_clearroute(this);
845                                 this.bot_strategytime = 0;
846                                 return;
847                         }
848
849                         // Check for water/slime/lava and dangerous edges
850                         // (only when the bot is on the ground or jumping intentionally)
851
852                         vector dst_ahead = this.origin + this.view_ofs + offset;
853                         vector dst_down = dst_ahead - '0 0 3000';
854                         traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
855
856                         bool unreachable = false;
857                         s = CONTENT_SOLID;
858                         if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
859                         if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
860                         {
861                                 // Look downwards
862                                 traceline(dst_ahead , dst_down, true, NULL);
863                                 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
864                                 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
865                                 if(trace_endpos.z < this.origin.z + this.mins.z)
866                                 {
867                                         s = pointcontents(trace_endpos + '0 0 1');
868                                         if (s != CONTENT_SOLID)
869                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
870                                                 evadelava = normalize(this.velocity) * -1;
871                                         else if (s == CONTENT_SKY)
872                                                 evadeobstacle = normalize(this.velocity) * -1;
873                                         else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
874                                         {
875                                                 // the traceline check isn't enough but is good as optimization,
876                                                 // when not true (most of the time) this tracebox call is avoided
877                                                 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
878                                                 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
879                                                 {
880                                                         if (gco.z > this.origin.z + jumpstepheightvec.z)
881                                                         {
882                                                                 // the goal is probably on an upper platform, assume bot can't get there
883                                                                 unreachable = true;
884                                                         }
885                                                         else
886                                                                 evadelava = normalize(this.velocity) * -1;
887                                                 }
888                                         }
889                                 }
890                         }
891
892                         dir = flatdir;
893                         evadeobstacle.z = 0;
894                         evadelava.z = 0;
895                         makevectors(this.v_angle.y * '0 1 0');
896
897                         if(evadeobstacle || evadelava || (s == CONTENT_WATER))
898                         {
899                                 this.aistatus |= AI_STATUS_DANGER_AHEAD;
900                                 if(IS_PLAYER(this.goalcurrent))
901                                         unreachable = true;
902                         }
903                         if(unreachable)
904                         {
905                                 navigation_clearroute(this);
906                                 this.bot_strategytime = 0;
907                         }
908                 }
909
910                 dodge = havocbot_dodge(this);
911                 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
912                 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
913                 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
914                 if(IS_PLAYER(trace_ent))
915                         dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
916
917                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
918         //      this.bot_dodgevector = dir;
919         //      this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
920         }
921
922         if(time < this.ladder_time)
923         {
924                 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
925                 {
926                         if(this.origin.z + this.mins.z  < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
927                                 dir.z = 1;
928                 }
929                 else
930                 {
931                         if(this.origin.z + this.mins.z  > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
932                                 dir.z = -1;
933                 }
934         }
935
936         //dir = this.bot_dodgevector;
937         //if (this.bot_dodgevector_jumpbutton)
938         //      PHYS_INPUT_BUTTON_JUMP(this) = true;
939         CS(this).movement_x = dir * v_forward * maxspeed;
940         CS(this).movement_y = dir * v_right * maxspeed;
941         CS(this).movement_z = dir * v_up * maxspeed;
942
943         // Emulate keyboard interface
944         if (skill < 10)
945                 havocbot_keyboard_movement(this, destorg);
946
947         // Bunnyhop!
948         //if(this.aistatus & AI_STATUS_ROAMING)
949         if(!bunnyhop_forbidden && this.goalcurrent)
950         if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
951                 havocbot_bunnyhop(this, dir);
952
953         if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
954         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;
955         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);
956 }
957
958 entity havocbot_gettarget(entity this, bool secondary)
959 {
960         entity best = NULL;
961         vector eye = CENTER_OR_VIEWOFS(this);
962         IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
963         {
964                 vector v = CENTER_OR_VIEWOFS(it);
965                 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
966                 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
967                 if(bot_shouldattack(this, it))
968                 {
969                         traceline(eye, v, true, this);
970                         if (trace_ent == it || trace_fraction >= 1)
971                                 best = it;
972                 }
973         });
974
975         return best;
976 }
977
978 void havocbot_chooseenemy(entity this)
979 {
980         if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
981         {
982                 this.enemy = NULL;
983                 return;
984         }
985         if (this.enemy)
986         {
987                 if (!bot_shouldattack(this, this.enemy))
988                 {
989                         // enemy died or something, find a new target
990                         this.enemy = NULL;
991                         this.havocbot_chooseenemy_finished = time;
992                 }
993                 else if (this.havocbot_stickenemy)
994                 {
995                         // tracking last chosen enemy
996                         // if enemy is visible
997                         // and not really really far away
998                         // and we're not severely injured
999                         // then keep tracking for a half second into the future
1000                         traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
1001                         if (trace_ent == this.enemy || trace_fraction == 1)
1002                         if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
1003                         if (this.health > 30)
1004                         {
1005                                 // remain tracking him for a shot while (case he went after a small corner or pilar
1006                                 this.havocbot_chooseenemy_finished = time + 0.5;
1007                                 return;
1008                         }
1009                         // enemy isn't visible, or is far away, or we're injured severely
1010                         // so stop preferring this enemy
1011                         // (it will still take a half second until a new one is chosen)
1012                         this.havocbot_stickenemy = 0;
1013                 }
1014         }
1015         if (time < this.havocbot_chooseenemy_finished)
1016                 return;
1017         this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
1018         vector eye = this.origin + this.view_ofs;
1019         entity best = NULL;
1020         float bestrating = 100000000;
1021
1022         // Backup hit flags
1023         int hf = this.dphitcontentsmask;
1024
1025         // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1026
1027         this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1028
1029         bool scan_transparent = false;
1030         bool scan_secondary_targets = false;
1031         bool have_secondary_targets = false;
1032         while(true)
1033         {
1034                 scan_secondary_targets = false;
1035 LABEL(scan_targets)
1036                 IL_EACH(g_bot_targets, it.bot_attack,
1037                 {
1038                         if(!scan_secondary_targets)
1039                         {
1040                                 if(it.classname == "misc_breakablemodel")
1041                                 {
1042                                         have_secondary_targets = true;
1043                                         continue;
1044                                 }
1045                         }
1046                         else if(it.classname != "misc_breakablemodel")
1047                                 continue;
1048
1049                         vector v = (it.absmin + it.absmax) * 0.5;
1050                         float rating = vlen2(v - eye);
1051                         if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1052                         if (bestrating > rating)
1053                         if (bot_shouldattack(this, it))
1054                         {
1055                                 traceline(eye, v, true, this);
1056                                 if (trace_ent == it || trace_fraction >= 1)
1057                                 {
1058                                         best = it;
1059                                         bestrating = rating;
1060                                 }
1061                         }
1062                 });
1063
1064                 if(!best && have_secondary_targets && !scan_secondary_targets)
1065                 {
1066                         scan_secondary_targets = true;
1067                         // restart the loop
1068                         bestrating = 100000000;
1069                         goto scan_targets;
1070                 }
1071
1072                 // I want to do a second scan if no enemy was found or I don't have weapons
1073                 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1074                 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1075                         break;
1076                 if(scan_transparent)
1077                         break;
1078
1079                 // Set flags to see through transparent objects
1080                 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1081
1082                 scan_transparent = true;
1083         }
1084
1085         // Restore hit flags
1086         this.dphitcontentsmask = hf;
1087
1088         this.enemy = best;
1089         this.havocbot_stickenemy = true;
1090         if(best && best.classname == "misc_breakablemodel")
1091                 this.havocbot_stickenemy = false;
1092 }
1093
1094 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1095 {
1096         // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1097         // so skip this for them, or they'll never get to reload their weapons at all.
1098         // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1099         if(skill < 5)
1100                 return false;
1101
1102         // if this weapon is scheduled for reloading, don't switch to it during combat
1103         if (this.(weaponentity).weapon_load[new_weapon] < 0)
1104         {
1105                 FOREACH(Weapons, it != WEP_Null, {
1106                         if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1107                                 return true; // other weapon available
1108                 });
1109         }
1110
1111         return false;
1112 }
1113
1114 void havocbot_chooseweapon(entity this, .entity weaponentity)
1115 {
1116         int i;
1117
1118         // ;)
1119         if(g_weaponarena_weapons == WEPSET(TUBA))
1120         {
1121                 this.(weaponentity).m_switchweapon = WEP_TUBA;
1122                 return;
1123         }
1124
1125         // TODO: clean this up by moving it to weapon code
1126         if(this.enemy==NULL)
1127         {
1128                 // If no weapon was chosen get the first available weapon
1129                 if(this.(weaponentity).m_weapon==WEP_Null)
1130                 FOREACH(Weapons, it != WEP_Null, {
1131                         if(client_hasweapon(this, it, weaponentity, true, false))
1132                         {
1133                                 this.(weaponentity).m_switchweapon = it;
1134                                 return;
1135                         }
1136                 });
1137                 return;
1138         }
1139
1140         // Do not change weapon during the next second after a combo
1141         float f = time - this.lastcombotime;
1142         if(f < 1)
1143                 return;
1144
1145         float w;
1146         float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1147
1148         // Should it do a weapon combo?
1149         float af, ct, combo_time, combo;
1150
1151         af = ATTACK_FINISHED(this, 0);
1152         ct = autocvar_bot_ai_weapon_combo_threshold;
1153
1154         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1155         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1156         combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1157
1158         combo = false;
1159
1160         if(autocvar_bot_ai_weapon_combo)
1161         if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1162         if(af > combo_time)
1163         {
1164                 combo = true;
1165                 this.lastcombotime = time;
1166         }
1167
1168         distance *= (2 ** this.bot_rangepreference);
1169
1170         // Custom weapon list based on distance to the enemy
1171         if(bot_custom_weapon){
1172
1173                 // Choose weapons for far distance
1174                 if ( distance > bot_distance_far ) {
1175                         for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1176                                 w = bot_weapons_far[i];
1177                                 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1178                                 {
1179                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1180                                                 continue;
1181                                         this.(weaponentity).m_switchweapon = Weapons_from(w);
1182                                         return;
1183                                 }
1184                         }
1185                 }
1186
1187                 // Choose weapons for mid distance
1188                 if ( distance > bot_distance_close) {
1189                         for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1190                                 w = bot_weapons_mid[i];
1191                                 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1192                                 {
1193                                         if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1194                                                 continue;
1195                                         this.(weaponentity).m_switchweapon = Weapons_from(w);
1196                                         return;
1197                                 }
1198                         }
1199                 }
1200
1201                 // Choose weapons for close distance
1202                 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1203                         w = bot_weapons_close[i];
1204                         if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1205                         {
1206                                 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1207                                         continue;
1208                                 this.(weaponentity).m_switchweapon = Weapons_from(w);
1209                                 return;
1210                         }
1211                 }
1212         }
1213 }
1214
1215 void havocbot_aim(entity this)
1216 {
1217         if (time < this.nextaim)
1218                 return;
1219         this.nextaim = time + 0.1;
1220         vector myvel = this.velocity;
1221         if (!this.waterlevel)
1222                 myvel.z = 0;
1223         if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1224         else if (this.enemy)
1225         {
1226                 vector enemyvel = this.enemy.velocity;
1227                 if (!this.enemy.waterlevel)
1228                         enemyvel.z = 0;
1229                 lag_additem(this, time + CS(this).ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1230         }
1231         else
1232                 lag_additem(this, time + CS(this).ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1233 }
1234
1235 bool havocbot_moveto_refresh_route(entity this)
1236 {
1237         // Refresh path to goal if necessary
1238         entity wp;
1239         wp = this.havocbot_personal_waypoint;
1240         navigation_goalrating_start(this);
1241         navigation_routerating(this, wp, 10000, 10000);
1242         navigation_goalrating_end(this);
1243         return (this.goalentity != NULL);
1244 }
1245
1246 float havocbot_moveto(entity this, vector pos)
1247 {
1248         entity wp;
1249
1250         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1251         {
1252                 // Step 4: Move to waypoint
1253                 if(this.havocbot_personal_waypoint==NULL)
1254                 {
1255                         LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1256                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1257                         return CMD_STATUS_ERROR;
1258                 }
1259
1260                 if (!bot_strategytoken_taken)
1261                 if(this.havocbot_personal_waypoint_searchtime<time)
1262                 {
1263                         bot_strategytoken_taken = true;
1264                         if(havocbot_moveto_refresh_route(this))
1265                         {
1266                                 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1267                                 this.havocbot_personal_waypoint_searchtime = time + 10;
1268                                 this.havocbot_personal_waypoint_failcounter = 0;
1269                         }
1270                         else
1271                         {
1272                                 this.havocbot_personal_waypoint_failcounter += 1;
1273                                 this.havocbot_personal_waypoint_searchtime = time + 2;
1274                                 if(this.havocbot_personal_waypoint_failcounter >= 30)
1275                                 {
1276                                         LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1277                                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1278                                         delete(this.havocbot_personal_waypoint);
1279                                         return CMD_STATUS_ERROR;
1280                                 }
1281                                 else
1282                                         LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1283                         }
1284                 }
1285
1286                 if(autocvar_bot_debug_goalstack)
1287                         debuggoalstack(this);
1288
1289                 // Heading
1290                 vector dir = '0 0 0';
1291                 SET_DESTCOORDS(this.goalcurrent, this.origin, dir);
1292                 dir = dir - (this.origin + this.view_ofs);
1293                 dir.z = 0;
1294                 bot_aimdir(this, dir, -1);
1295
1296                 // Go!
1297                 havocbot_movetogoal(this);
1298
1299                 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1300                 {
1301                         // Step 5: Waypoint reached
1302                         LOG_TRACE(this.netname, "'s personal waypoint reached");
1303                         delete(this.havocbot_personal_waypoint);
1304                         this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1305                         return CMD_STATUS_FINISHED;
1306                 }
1307
1308                 return CMD_STATUS_EXECUTING;
1309         }
1310
1311         // Step 2: Linking waypoint
1312         if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1313         {
1314                 // Wait until it is linked
1315                 if(!this.havocbot_personal_waypoint.wplinked)
1316                 {
1317                         LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1318                         return CMD_STATUS_EXECUTING;
1319                 }
1320
1321                 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1322                 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1323                 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1324
1325                 // Step 3: Route to waypoint
1326                 LOG_TRACE(this.netname, " walking to its personal waypoint");
1327
1328                 return CMD_STATUS_EXECUTING;
1329         }
1330
1331         // Step 1: Spawning waypoint
1332         wp = waypoint_spawnpersonal(this, pos);
1333         if(wp==NULL)
1334         {
1335                 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1336                 return CMD_STATUS_ERROR;
1337         }
1338
1339         this.havocbot_personal_waypoint = wp;
1340         this.havocbot_personal_waypoint_failcounter = 0;
1341         this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1342
1343         // if pos is inside a teleport, then let's mark it as teleport waypoint
1344         IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1345         {
1346                 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1347                 this.lastteleporttime = 0;
1348         });
1349
1350 /*
1351         if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1352                 print("routing to a teleporter\n");
1353         else
1354                 print("routing to a non-teleporter\n");
1355 */
1356
1357         return CMD_STATUS_EXECUTING;
1358 }
1359
1360 float havocbot_resetgoal(entity this)
1361 {
1362         navigation_clearroute(this);
1363         return CMD_STATUS_FINISHED;
1364 }
1365
1366 void havocbot_setupbot(entity this)
1367 {
1368         this.bot_ai = havocbot_ai;
1369         this.cmd_moveto = havocbot_moveto;
1370         this.cmd_resetgoal = havocbot_resetgoal;
1371
1372         havocbot_chooserole(this);
1373 }
1374
1375 vector havocbot_dodge(entity this)
1376 {
1377         // LordHavoc: disabled because this is too expensive
1378         return '0 0 0';
1379 #if 0
1380         entity head;
1381         vector dodge, v, n;
1382         float danger, bestdanger, vl, d;
1383         dodge = '0 0 0';
1384         bestdanger = -20;
1385         // check for dangerous objects near bot or approaching bot
1386         head = findchainfloat(bot_dodge, true);
1387         while(head)
1388         {
1389                 if (head.owner != this)
1390                 {
1391                         vl = vlen(head.velocity);
1392                         if (vl > autocvar_sv_maxspeed * 0.3)
1393                         {
1394                                 n = normalize(head.velocity);
1395                                 v = this.origin - head.origin;
1396                                 d = v * n;
1397                                 if (d > (0 - head.bot_dodgerating))
1398                                 if (d < (vl * 0.2 + head.bot_dodgerating))
1399                                 {
1400                                         // calculate direction and distance from the flight path, by removing the forward axis
1401                                         v = v - (n * (v * n));
1402                                         danger = head.bot_dodgerating - vlen(v);
1403                                         if (bestdanger < danger)
1404                                         {
1405                                                 bestdanger = danger;
1406                                                 // dodge to the side of the object
1407                                                 dodge = normalize(v);
1408                                         }
1409                                 }
1410                         }
1411                         else
1412                         {
1413                                 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1414                                 if (bestdanger < danger)
1415                                 {
1416                                         bestdanger = danger;
1417                                         dodge = normalize(this.origin - head.origin);
1418                                 }
1419                         }
1420                 }
1421                 head = head.chain;
1422         }
1423         return dodge;
1424 #endif
1425 }