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