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