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