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