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