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