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