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