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