]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/havocbot.qc
Bots: Check for dangers when running
[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
7 #ifdef DEBUG_BOT_GOALSTACK
8 void debuggoalstack();
9 #endif
10
11 vector havocbot_dodge()
12 {
13         // LordHavoc: disabled because this is too expensive
14         return '0 0 0';
15         /*
16         local entity head;
17         local vector dodge, v, n;
18         local float danger, bestdanger, vl, d;
19         dodge = '0 0 0';
20         bestdanger = -20;
21         // check for dangerous objects near bot or approaching bot
22         head = findchainfloat(bot_dodge, TRUE);
23         while(head)
24         {
25                 if (head.owner != self)
26                 {
27                         vl = vlen(head.velocity);
28                         if (vl > sv_maxspeed * 0.3)
29                         {
30                                 n = normalize(head.velocity);
31                                 v = self.origin - head.origin;
32                                 d = v * n;
33                                 if (d > (0 - head.bot_dodgerating))
34                                 if (d < (vl * 0.2 + head.bot_dodgerating))
35                                 {
36                                         // calculate direction and distance from the flight path, by removing the forward axis
37                                         v = v - (n * (v * n));
38                                         danger = head.bot_dodgerating - vlen(v);
39                                         if (bestdanger < danger)
40                                         {
41                                                 bestdanger = danger;
42                                                 // dodge to the side of the object
43                                                 dodge = normalize(v);
44                                         }
45                                 }
46                         }
47                         else
48                         {
49                                 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
50                                 if (bestdanger < danger)
51                                 {
52                                         bestdanger = danger;
53                                         dodge = normalize(self.origin - head.origin);
54                                 }
55                         }
56                 }
57                 head = head.chain;
58         }
59         return dodge;
60         */
61 };
62
63 .float havocbot_keyboardtime;
64 .float havocbot_ducktime;
65 .vector havocbot_keyboard;
66 void havocbot_keyboard_movement(vector destorg)
67 {
68         local vector keyboard;
69         local float blend, maxspeed;
70
71         maxspeed = cvar("sv_maxspeed"); 
72
73         if (time < self.havocbot_keyboardtime)
74                 return;
75
76         self.havocbot_keyboardtime =
77                 max(
78                         self.havocbot_keyboardtime
79                                 + bound(0,0.05/(skill+self.havocbot_keyboardskill),0.05)
80                                 +random()*bound(0,0.025/(skill+self.havocbot_keyboardskill),100)
81                 , time);
82         keyboard = self.movement * (1.0 / maxspeed);
83
84         local float trigger, trigger1;
85         blend = bound(0,skill*0.1,1);
86         trigger = cvar("bot_ai_keyboard_treshold");
87         trigger1 = 0 - trigger;
88
89         // categorize forward movement
90         // at skill < 1.5 only forward
91         // at skill < 2.5 only individual directions
92         // at skill < 4.5 only individual directions, and forward diagonals
93         // at skill >= 4.5, all cases allowed
94         if (keyboard_x > trigger)
95         {
96                 keyboard_x = 1;
97                 if (skill < 2.5)
98                         keyboard_y = 0;
99         }
100         else if (keyboard_x < trigger1 && skill > 1.5)
101         {
102                 keyboard_x = -1;
103                 if (skill < 4.5)
104                         keyboard_y = 0;
105         }
106         else
107         {
108                 keyboard_x = 0;
109                 if (skill < 1.5)
110                         keyboard_y = 0;
111         }
112         if (skill < 4.5)
113                 keyboard_z = 0;
114
115         if (keyboard_y > trigger)
116                 keyboard_y = 1;
117         else if (keyboard_y < trigger1)
118                 keyboard_y = -1;
119         else
120                 keyboard_y = 0;
121
122         if (keyboard_z > trigger)
123                 keyboard_z = 1;
124         else if (keyboard_z < trigger1)
125                 keyboard_z = -1;
126         else
127                 keyboard_z = 0;
128
129         self.havocbot_keyboard = keyboard * maxspeed;
130         if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
131
132         keyboard = self.havocbot_keyboard;
133         blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
134         //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
135         self.movement = self.movement + (keyboard - self.movement) * blend;
136 };
137
138 .entity bot_lastseengoal;
139 .float bot_timelastseengoal;
140 .float bot_canruntogoal;
141 void havocbot_bunnyhop(vector dir)
142 {
143         local float distance;
144         local vector deviation;
145
146         if(self.goalcurrent.classname == "player")
147                 return;
148
149         if(self.aistatus & AI_STATUS_DANGER_AHEAD)
150                 return;
151
152         if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
153         {
154                 self.bot_canruntogoal = 0;
155                 self.bot_timelastseengoal = 0;
156         }
157
158         distance = vlen(self.origin - self.goalcurrent.origin);
159
160         // Run only to visible goals
161         traceline(self.origin + self.view_ofs , self.goalcurrent.origin, TRUE, world);
162         if(self.flags & FL_ONGROUND)
163         if(trace_fraction == 1)
164         {
165                         self.bot_lastseengoal = self.goalcurrent;
166
167                         // seen it before
168                         if(self.bot_timelastseengoal)
169                         {
170                                 // for a period of time
171                                 if(time - self.bot_timelastseengoal > cvar("bot_ai_bunnyhop_firstjumpdelay"))
172                                 {
173                                         local float checkdistance;
174                                         checkdistance = TRUE;
175
176                                         // don't run if it is too close
177                                         if(self.bot_canruntogoal==0)
178                                         {
179                                                 if(distance > cvar("bot_ai_bunnyhop_startdistance"))
180                                                         self.bot_canruntogoal = 1;
181                                                 else    
182                                                         self.bot_canruntogoal = -1;
183                                         }
184
185                                         if(self.bot_canruntogoal != 1)
186                                                 return;
187
188                                         if(self.aistatus & AI_STATUS_ROAMING)
189                                         if(self.goalcurrent.classname=="waypoint")
190                                         if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
191                                         if(self.goalstack01!=world)
192                                         {
193                                                 deviation = self.goalstack01.origin - (self.origin + self.view_ofs);
194                                                 deviation = vectoangles(deviation) - self.v_angle;
195                                                 while (deviation_y < -180) deviation_y = deviation_y + 360;
196                                                 while (deviation_y > 180) deviation_y = deviation_y - 360;
197
198                                                 if(deviation_y < 20 && deviation_y > -20)
199                                                 if(distance < vlen(self.origin - self.goalstack01.origin))
200                                                 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
201                                                 {
202                                                         traceline(self.origin + self.view_ofs , self.goalstack01.origin, TRUE, world);
203                                                         if(trace_fraction==1)
204                                                         {
205                                                                 checkdistance = FALSE;
206                                                         }
207                                                 }
208                                         }
209
210                                         if(checkdistance)
211                                         {
212                                                 self.aistatus &~= AI_STATUS_RUNNING;
213                                                 if(distance > cvar("bot_ai_bunnyhop_stopdistance"))
214                                                         self.BUTTON_JUMP = TRUE;
215                                         }
216                                         else
217                                         {
218                                                 self.aistatus |= AI_STATUS_RUNNING;
219                                                 self.BUTTON_JUMP = TRUE;
220                                         }
221                                 }
222                         }
223                         else
224                         {
225                                 self.bot_timelastseengoal = time;
226                         }
227         }
228         else
229         {
230                 self.bot_timelastseengoal = 0;
231         }
232
233         // Release jump button
234         if(self.flags & FL_ONGROUND == 0)
235         {
236                 if(self.velocity_z < 0)
237                         self.BUTTON_JUMP = FALSE;
238
239                 // Strafe 
240                 local float maxspeed;
241                 maxspeed = cvar("sv_maxspeed");
242
243                 if(self.aistatus & AI_STATUS_RUNNING)
244                 if(vlen(self.velocity)>maxspeed)
245                 {
246                         deviation = vectoangles(dir) - self.v_angle;
247                         while (deviation_y < -180) deviation_y = deviation_y + 360;
248                         while (deviation_y > 180) deviation_y = deviation_y - 360;
249
250                         if ( deviation_y > 2 )
251                                 self.movement_y = maxspeed;
252                         if ( deviation_y < -2 )
253                                 self.movement_y = maxspeed * -1;
254                 }
255         }
256 };
257
258 //.float havocbotignoretime;
259 //.vector bot_dodgevector;
260 //.float bot_dodgevector_time;
261 //.float bot_dodgevector_jumpbutton;
262 void havocbot_movetogoal()
263 {
264         local vector destorg;
265         local vector diff;
266         local vector dir;
267         local vector flatdir;
268         local vector m1;
269         local vector m2;
270         local vector evadeobstacle;
271         local vector evadelava;
272         local float s;
273         local float maxspeed;
274         //local float dist;
275         local vector dodge;
276         //if (self.goalentity)
277         //      te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
278         self.movement = '0 0 0';
279         maxspeed = cvar("sv_maxspeed");
280
281         if(self.jumppadcount)
282         {
283                 if(self.flags & FL_ONGROUND)
284                         self.jumppadcount = FALSE;
285                 else
286                         return;
287         }
288
289         if (self.goalcurrent == world)
290                 return;
291
292         navigation_poptouchedgoals();
293         if (self.goalcurrent == world)
294         {
295                 if(self.alternativegoal==world)
296                 {
297                         // ran out of goals, rethink strategy as soon as possible
298                         self.bot_strategytime = 0;
299                         return;
300                 }
301                 // try to use the alternative goal
302                 navigation_findnearestwaypoint(self.alternativegoal, TRUE);
303                 navigation_routetogoal(self.alternativegoal);
304                 self.alternativegoal = world;
305                 return;
306         }
307
308 #ifdef DEBUG_BOT_GOALSTACK
309         debuggoalstack();
310 #endif
311
312         m1 = self.goalcurrent.origin + self.goalcurrent.mins;
313         m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
314         destorg = self.origin;
315         destorg_x = bound(m1_x, destorg_x, m2_x);
316         destorg_y = bound(m1_y, destorg_y, m2_y);
317         destorg_z = bound(m1_z, destorg_z, m2_z);
318         diff = destorg - self.origin;
319         //dist = vlen(diff);
320         dir = normalize(diff);
321         flatdir = diff;flatdir_z = 0;
322         flatdir = normalize(flatdir);
323
324         //if (self.bot_dodgevector_time < time)
325         {
326         //      self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
327         //      self.bot_dodgevector_jumpbutton = 1;
328                 evadeobstacle = '0 0 0';
329                 evadelava = '0 0 0';
330                 if (self.waterlevel)
331                 {
332                         makevectors(self.v_angle);
333                         self.BUTTON_JUMP = TRUE;
334                         evadelava_z = 1;
335                 }
336                 else
337                 {
338                         // jump if going toward an obstacle that doesn't look like stairs we
339                         // can walk up directly
340                         tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
341                         if (trace_fraction < 1)
342                         if (trace_plane_normal_z < 0.7)
343                         {
344                                 s = trace_fraction;
345                                 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
346                                 if (trace_fraction < s + 0.01)
347                                 if (trace_plane_normal_z < 0.7)
348                                 {
349                                         s = trace_fraction;
350                                         tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
351                                         if (trace_fraction > s)
352                                                 self.BUTTON_JUMP = 1;
353                                 }
354                         }
355
356                         // avoiding dangers and obstacles
357                         // TODO: don't make this check every frame
358                         local vector dst_ahead, dst_down;
359                         dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.32);
360                         dst_down = dst_ahead + '0 0 -1500';                             
361                         
362                         traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
363
364                         // Check head-banging against walls
365                         if(vlen(self.origin + self.view_ofs - trace_endpos) < 2)
366                         {
367                                 if(self.facingwalltime && time > self.facingwalltime)
368                                 {
369                                         self.ignoregoal = self.goalcurrent;
370                                         self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
371                                         navigation_poproute();
372                                 }
373                                 else
374                                 {
375                                         self.facingwalltime = time + 0.05;
376                                 }
377                         }
378                         else
379                         {
380                                 self.facingwalltime = 0;
381
382                                 if(self.ignoregoal != world && time > self.ignoregoaltime)
383                                 {
384                                         self.ignoregoal = world;
385                                         self.ignoregoaltime = 0;
386                                 }
387                         }
388                 
389                         // Check for water/slime/lava and edges
390                         self.aistatus &~= AI_STATUS_DANGER_AHEAD;
391                         if(trace_fraction == 1){
392                                 traceline(dst_ahead , dst_down, TRUE, world);
393                                 //      te_lightning2(world, self.origin, dst_ahead);   // Draw "ahead" look 
394                                 s = pointcontents(trace_endpos + '0 0 1');
395                                 if (s != CONTENT_SOLID)
396                                 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
397                                         evadelava = normalize(self.velocity) * -1;
398                                 else if (s == CONTENT_SKY)
399                                         evadeobstacle = normalize(self.velocity) * -1;
400                                 else if (tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
401                                 {
402                                         //      te_lightning2(world, dst_ahead, dst_down);      // Draw "downwards" look
403                                         // if ain't a safe goal with "holes" (like the soylent jumpad)
404                                         if(!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs, 
405                                                         self.goalcurrent.absmin, self.goalcurrent.absmax))
406                                         {
407                                                 // Remove dangerous dynamic goals from stack
408                                                 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
409                                                         navigation_poproute();
410                                                 // try to stop
411                                                 flatdir = '0 0 0';
412                                                 evadeobstacle = normalize(self.velocity) * -1;
413                                         }
414                                 }
415                         }
416
417                         dir = flatdir;
418                         evadeobstacle_z = 0;
419                         evadelava_z = 0;
420                         makevectors(self.v_angle_y * '0 1 0');
421
422                         if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
423                                 self.aistatus |= AI_STATUS_DANGER_AHEAD;
424                 }
425
426                 dodge = havocbot_dodge();
427                 dodge = dodge * bound(0,3+skill*0.1,1);
428                 evadelava = evadelava * bound(1,3-skill,3); //Noobs fear lava a lot and take more distance from it
429                 traceline(self.origin, self.enemy.origin, TRUE, world);
430                 if(trace_ent.classname == "player")
431                         dir = dir * bound(0,skill/7,1);
432
433                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
434         //      self.bot_dodgevector = dir;
435         //      self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
436         }
437
438         //dir = self.bot_dodgevector;
439         //if (self.bot_dodgevector_jumpbutton)
440         //      self.BUTTON_JUMP = 1;
441         self.movement_x = dir * v_forward * maxspeed;
442         self.movement_y = dir * v_right * maxspeed;
443         self.movement_z = dir * v_up * maxspeed;
444
445         // Emulate keyboard interface
446         if (skill < 10)
447                 havocbot_keyboard_movement(destorg);
448
449         // Bunnyhop!
450 //      if(self.aistatus & AI_STATUS_ROAMING)
451         if(skill >= cvar("bot_ai_bunnyhop_skilloffset"))
452                 havocbot_bunnyhop(dir);
453
454         if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
455         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill)*0.1,1)) self.BUTTON_JUMP=TRUE;
456         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);
457 };
458
459 .float havocbot_chooseenemy_finished;
460 .float havocbot_stickenemy;
461 void havocbot_chooseenemy()
462 {
463         local entity head, best;
464         local float rating, bestrating;
465         local vector eye, v;
466         if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
467         {
468                 self.enemy = world;
469                 return;
470         }
471         if (self.enemy)
472         {
473                 if (!bot_shouldattack(self.enemy))
474                 {
475                         // enemy died or something, find a new target
476                         self.enemy = world;
477                         self.havocbot_chooseenemy_finished = time;
478                 }
479                 else if (self.havocbot_stickenemy)
480                 {
481                         // tracking last chosen enemy
482                         // if enemy is visible
483                         // and not really really far away
484                         // and we're not severely injured
485                         // then keep tracking for a half second into the future
486                         traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
487                         if (trace_ent == self.enemy || trace_fraction == 1)
488                         if (vlen(self.enemy.origin - self.origin) < 1000)
489                         if (self.health > 30)
490                         {
491                                 // remain tracking him for a shot while (case he went after a small corner or pilar
492                                 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
493                                 return;
494                         }
495                         // enemy isn't visible, or is far away, or we're injured severely
496                         // so stop preferring this enemy
497                         // (it will still take a half second until a new one is chosen)
498                         self.havocbot_stickenemy = 0;
499                 }
500         }
501         if (time < self.havocbot_chooseenemy_finished)
502                 return;
503         self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
504         eye = (self.origin + self.view_ofs);
505         best = world;
506         bestrating = 100000000;
507         head = findchainfloat(bot_attack, TRUE);
508         while (head)
509         {
510                 v = (head.absmin + head.absmax) * 0.5;
511                 rating = vlen(v - eye);
512                 if (bestrating > rating)
513                 if (bot_shouldattack(head))
514                 {
515                         traceline(eye, v, TRUE, self);
516                         if (trace_ent == head || trace_fraction >= 1)
517                         {
518                                 best = head;
519                                 bestrating = rating;
520                         }
521                 }
522                 head = head.chain;
523         }
524         self.enemy = best;
525         self.havocbot_stickenemy = TRUE;
526 };
527
528 .float bot_chooseweapontime;
529 float(entity e) w_getbestweapon;
530 void havocbot_chooseweapon()
531 {
532         // TODO: clean this up by moving it to weapon code
533         if(self.enemy.classname!="player")
534         {
535                 self.switchweapon = w_getbestweapon(self);
536                 return;
537         }
538
539         local float w, s;
540         local float rocket  ; rocket   =-1000;
541         local float nex     ; nex      =-1000;
542         local float hagar   ; hagar    =-1000;
543         local float grenade ; grenade  =-1000;
544         local float electro ; electro  =-1000;
545         local float crylink ; crylink  =-1000;
546         local float uzi     ; uzi      =-1000;
547         local float shotgun ; shotgun  =-1000;
548         local float campingrifle ; campingrifle  =-1000;
549         local float laser   ; laser    =-1000;
550         local float minstanex ; minstanex =-1000;
551         local float currentscore;
552         local float bestscore; bestscore = 0;
553         local float bestweapon; bestweapon=self.switchweapon;
554         local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
555         local float maxdelaytime=0.5;
556         local float spreadpenalty=10;
557         local float distancefromfloor;
558
559         local float af, ct, combo_time;
560
561         traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
562         distancefromfloor = self.enemy.origin_z - trace_endpos_z;
563         
564         af = ATTACK_FINISHED(self);
565         ct = cvar("bot_ai_weapon_combo_threshold");
566
567         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
568         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
569         combo_time = time + ct + (ct * ((-0.3*skill)+3));
570
571         // Custom weapon list based on distance to the enemy
572         local float i; i = 0;
573         if(bot_custom_weapon){
574
575                 // Choose weapons for far distance
576                 if ( distance > bot_distance_far ) {
577                         for(i=0; i < WEP_LAST && bot_weapons_far[i] != -1 ; ++i){
578                                 w = bot_weapons_far[i];
579                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
580                                         if ( self.weapon == w){
581                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
582                                                         continue;
583                                         } else {
584                                                 self.switchweapon = w;
585                                         }
586                                         return;
587                                 }
588                         }
589                 }
590
591                 // Choose weapons for mid distance
592                 if ( distance > bot_distance_close ) {
593                         for(i=0; i < WEP_LAST && bot_weapons_mid[i] != -1 ; ++i){
594                                 w = bot_weapons_mid[i];
595                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
596                                         if ( self.weapon == w){
597                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
598                                                         continue;
599                                         } else {
600                                                 self.switchweapon = w;
601                                         }
602                                         return;
603                                 }
604                         }
605                 }
606
607                 // Choose weapons for close distance
608                 for(i=0; i < WEP_LAST && bot_weapons_close[i] != -1 ; ++i){
609                         w = bot_weapons_close[i];
610                         if ( client_hasweapon(self, w, TRUE, FALSE) ){
611                                 if ( self.weapon == w){
612                                         if( cvar("bot_ai_weapon_combo") && af > combo_time)
613                                                 continue;
614                                 } else {
615                                         self.switchweapon = w;
616                                 }
617                                 return;
618                         }
619                 }
620                 // If now weapon was chosen by this system fall back to the previous one
621         }
622
623         // Formula:
624         //      (Damage/Sec * Weapon spefic change to get that damage)
625         //      *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
626         //      *(Spread change of hit) // if it applies
627         //      *(Penality for target beeing in air)
628         // %weaponaddpoint
629         if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
630                 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
631                         * (0.5);
632
633         if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE)  && 
634                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER && 
635                         af > combo_time
636                 )
637         )
638                 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
639                         * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
640                         
641         if (client_hasweapon(self, WEP_NEX, TRUE, FALSE)  && 
642                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX && 
643                         af > combo_time
644                 )
645         )
646                 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
647                         * (0.5);
648                         
649         if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // && 
650         //      !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR &&  time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
651                 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
652                         * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
653                         
654         if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) && 
655                 !( 
656                         cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER && 
657                         af > combo_time
658                 )
659         )
660                 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
661                         * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
662                         
663         if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) && 
664                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO && 
665                         af > combo_time
666                 )
667         )
668                 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
669                         * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
670                         
671         if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // && 
672         //      !( self.weapon == WEP_CRYLINK &&  time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
673                 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
674                         * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
675                         
676         if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // && 
677         //      !( self.weapon == WEP_UZI &&  time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
678                 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
679                         * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
680                         
681         if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) && 
682                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN && 
683                         af > combo_time
684                 )
685         )
686                 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
687                         * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
688                         
689         if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) && 
690                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER && 
691                         af > combo_time
692                 )
693         )
694                 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
695                         * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
696                         
697         if((self.enemy.flags & FL_ONGROUND)==FALSE){
698                 rocket = rocket   * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius"         ),0.9)); //slight bigger change
699                 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
700                 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius"        ),0.95));
701                 laser = laser     * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius"                  ),0.95));
702         }
703         /*
704         dprint("Floor distance: ",ftos(distancefromfloor),"\n");
705         dprint("Rocket: " , ftos(rocket  ), "\n");
706         dprint("Nex: "    , ftos(nex     ), "\n");
707         dprint("Hagar: "  , ftos(hagar   ), "\n");
708         dprint("Grenade: ", ftos(grenade ), "\n");
709         dprint("Electro: ", ftos(electro ), "\n");
710         dprint("Crylink: ", ftos(crylink ), "\n");
711         dprint("Uzi: "    , ftos(uzi     ), "\n");
712         dprint("Shotgun :", ftos(shotgun ), "\n");
713         dprint("Laser   :", ftos(laser   ), "\n\n");
714         */
715         currentscore = -1;
716         w = WEP_MINSTANEX        ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
717         w = WEP_ROCKET_LAUNCHER  ;s = rocket   ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
718         w = WEP_NEX              ;s = nex      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
719         w = WEP_HAGAR            ;s = hagar    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
720         w = WEP_GRENADE_LAUNCHER ;s = grenade  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
721         w = WEP_ELECTRO          ;s = electro  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
722         w = WEP_CRYLINK          ;s = crylink  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
723         w = WEP_UZI              ;s = uzi      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
724         w = WEP_SHOTGUN          ;s = shotgun  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
725         w = WEP_LASER            ;s = laser    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
726
727         // switch if the best weapon would provide a significant damage increase
728         if (bestscore > currentscore*1.5){
729                 self.switchweapon = bestweapon;
730                 
731                 // buys time for detonating the rocket. not tested yet
732                 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
733                         self.bot_chooseweapontime += (distance  / cvar("g_balance_rocketlauncher_speed"));
734         }
735 };
736
737 .float nextaim;
738 void havocbot_aim()
739 {
740         local vector selfvel, enemyvel;
741         if(self.flags & FL_INWATER)
742                 return;
743         if (time < self.nextaim)
744                 return;
745         self.nextaim = time + 0.1;
746         selfvel = self.velocity;
747         if (!self.waterlevel)
748                 selfvel_z = 0;
749         if (self.enemy)
750         {
751                 enemyvel = self.enemy.velocity;
752                 if (!self.enemy.waterlevel)
753                         enemyvel_z = 0;
754                 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
755         }
756         else
757                 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
758 };
759
760 void havocbot_ai()
761 {
762         if (bot_strategytoken == self)
763         if (!bot_strategytoken_taken)
764         {
765                 self.havocbot_role();
766                 // token has been used this frame
767                 bot_strategytoken_taken = TRUE;
768         }
769         if(self.deadflag)
770                 return;
771
772         havocbot_chooseenemy();
773         if (self.bot_chooseweapontime < time )
774         {
775                 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
776                 havocbot_chooseweapon();
777         }
778         havocbot_aim();
779         lag_update();
780         if (self.bot_aimtarg)
781         {
782                 self.aistatus |= AI_STATUS_ATTACKING;
783                 self.aistatus &~= AI_STATUS_ROAMING;
784
785                 weapon_action(self.weapon, WR_AIM);
786                 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
787                 {
788                         self.BUTTON_ATCK = FALSE;
789                         self.BUTTON_ATCK2 = FALSE;
790                 }
791         }
792         else if (self.goalcurrent)
793         {
794                 self.aistatus |= AI_STATUS_ROAMING;
795                 self.aistatus &~= AI_STATUS_ATTACKING;
796
797                 local vector now,v,next;//,heading;
798                 local float distance,skillblend,distanceblend,blend;
799                 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
800                 distance = vlen(now);
801                 //heading = self.velocity;
802                 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
803                 if(self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0)
804                         next = self.goalstack01.origin - (self.origin + self.view_ofs);
805                 skillblend=bound(0,(skill-2.5)*0.5,1); //lower skill player can't preturn
806                 distanceblend=bound(0,distance/cvar("bot_ai_keyboard_distance"),1);
807                 blend = skillblend * (1-distanceblend);
808                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
809                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
810                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
811                 v = now + blend * (next - now);
812                 //dprint(etos(self), " ");
813                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
814                 //v = now * (distanceblend) + next * (1-distanceblend);
815                 if (self.waterlevel < 2)
816                         v_z = 0;
817                 //dprint("walk at:", vtos(v), "\n");
818                 //te_lightning2(world, self.origin, self.goalcurrent.origin);
819                 bot_aimdir(v, -1);
820         }
821         havocbot_movetogoal();
822 };
823
824 void havocbot_setupbot()
825 {
826         self.bot_ai = havocbot_ai;
827         // will be updated by think code
828         //Generate some random skill levels
829         self.havocbot_keyboardskill=random()-0.5;
830         havocbot_chooserole();
831 }
832
833
834 #ifdef DEBUG_BOT_GOALSTACK
835
836 .float goalcounter;
837 .vector lastposition;
838
839 // Debug the goal stack visually
840 void debuggoalstack()
841 {
842         local entity target;
843         local vector org;
844
845         if(self.goalcounter==0)target=self.goalcurrent;
846         else if(self.goalcounter==1)target=self.goalstack01;
847         else if(self.goalcounter==2)target=self.goalstack02;
848         else if(self.goalcounter==3)target=self.goalstack03;
849         else if(self.goalcounter==4)target=self.goalstack04;
850         else if(self.goalcounter==5)target=self.goalstack05;
851         else if(self.goalcounter==6)target=self.goalstack06;
852         else if(self.goalcounter==7)target=self.goalstack07;
853         else if(self.goalcounter==8)target=self.goalstack08;
854         else if(self.goalcounter==9)target=self.goalstack09;
855         else if(self.goalcounter==10)target=self.goalstack10;
856         else if(self.goalcounter==11)target=self.goalstack11;
857         else if(self.goalcounter==12)target=self.goalstack12;
858         else if(self.goalcounter==13)target=self.goalstack13;
859         else if(self.goalcounter==14)target=self.goalstack14;
860         else if(self.goalcounter==15)target=self.goalstack15;
861         else if(self.goalcounter==16)target=self.goalstack16;
862         else if(self.goalcounter==17)target=self.goalstack17;
863         else if(self.goalcounter==18)target=self.goalstack18;
864         else if(self.goalcounter==19)target=self.goalstack19;
865         else if(self.goalcounter==20)target=self.goalstack20;
866         else if(self.goalcounter==21)target=self.goalstack21;
867         else if(self.goalcounter==22)target=self.goalstack22;
868         else if(self.goalcounter==23)target=self.goalstack23;
869         else if(self.goalcounter==24)target=self.goalstack24;
870         else if(self.goalcounter==25)target=self.goalstack25;
871         else if(self.goalcounter==26)target=self.goalstack26;
872         else if(self.goalcounter==27)target=self.goalstack27;
873         else if(self.goalcounter==28)target=self.goalstack28;
874         else if(self.goalcounter==29)target=self.goalstack29;
875         else if(self.goalcounter==30)target=self.goalstack30;
876         else if(self.goalcounter==31)target=self.goalstack31;
877
878         if(target==world)
879         {
880                 self.goalcounter = 0;
881                 self.lastposition='0 0 0';
882                 return;
883         }
884
885         if(self.lastposition=='0 0 0')
886                 org = self.origin;
887         else
888                 org = self.lastposition;
889
890
891         te_lightning2(world, org, target.origin);
892         self.lastposition = target.origin;
893
894         self.goalcounter++;
895 }
896
897 #endif