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