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