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