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