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