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