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