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