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