]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/havocbot.qc
Bots: Evaluate the risks of picking up items or chasing players before rating them...
[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 = TRUE;
208                         evadelava_z = 1;
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                                         s = pointcontents(trace_endpos + '0 0 1');
244                                         if (s != CONTENT_SOLID)
245                                         if (s == CONTENT_LAVA || s == CONTENT_SLIME)
246                                                 evadelava = normalize(self.velocity) * -1;
247                                         else if (s == CONTENT_SKY)
248                                                 evadeobstacle = normalize(self.velocity) * -1;
249                                         else if (tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
250                                         {
251                                                 //      te_lightning2(world, dst_ahead, dst_down);      // Draw "downwards" look
252                                                 // if ain't a safe goal with "holes" (like the soylent jumpad)
253                                                 if(!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs, 
254                                                                 self.goalcurrent.absmin, self.goalcurrent.absmax))
255                                                 {
256                                                         // Remove dangerous dynamic goals from stack
257                                                         if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
258                                                                 navigation_poproute();
259                                                         // try to stop
260                                                         flatdir = '0 0 0';
261                                                         evadeobstacle = normalize(self.velocity) * -1;
262                                                 }
263                                         }
264                                 }
265                         }
266
267                         dir = flatdir;
268                         evadeobstacle_z = 0;
269                         evadelava_z = 0;
270                         makevectors(self.v_angle_y * '0 1 0');
271                 }
272
273                 dodge = havocbot_dodge();
274                 dodge = dodge * bound(0,3+skill*0.1,1);
275                 evadelava = evadelava * bound(1,3-skill,3); //Noobs fear lava a lot and take more distance from it
276                 traceline(self.origin, self.enemy.origin, TRUE, world);
277                 if(trace_ent.classname == "player")
278                         dir = dir * bound(0,skill/7,1);
279
280                 dir = normalize(dir + dodge + evadeobstacle + evadelava);
281         //      self.bot_dodgevector = dir;
282         //      self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
283         }
284
285         //dir = self.bot_dodgevector;
286         //if (self.bot_dodgevector_jumpbutton)
287         //      self.BUTTON_JUMP = 1;
288
289         self.movement_x = dir * v_forward * maxspeed;
290         self.movement_y = dir * v_right * maxspeed;
291         self.movement_z = dir * v_up * maxspeed;
292
293         // Emulate keyboard interface
294         if (skill < 10)
295                 havocbot_keyboard_movement(destorg);
296
297         if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
298         if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill)*0.1,1)) self.BUTTON_JUMP=TRUE;
299         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);
300 };
301
302 .float havocbot_chooseenemy_finished;
303 .float havocbot_stickenemy;
304 void havocbot_chooseenemy()
305 {
306         local entity head, best;
307         local float rating, bestrating;
308         local vector eye, v;
309         if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
310         {
311                 self.enemy = world;
312                 return;
313         }
314         if (self.enemy)
315         {
316                 if (!bot_shouldattack(self.enemy))
317                 {
318                         // enemy died or something, find a new target
319                         self.enemy = world;
320                         self.havocbot_chooseenemy_finished = time;
321                 }
322                 else if (self.havocbot_stickenemy)
323                 {
324                         // tracking last chosen enemy
325                         // if enemy is visible
326                         // and not really really far away
327                         // and we're not severely injured
328                         // then keep tracking for a half second into the future
329                         traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
330                         if (trace_ent == self.enemy || trace_fraction == 1)
331                         if (vlen(self.enemy.origin - self.origin) < 1000)
332                         if (self.health > 30)
333                         {
334                                 // remain tracking him for a shot while (case he went after a small corner or pilar
335                                 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
336                                 return;
337                         }
338                         // enemy isn't visible, or is far away, or we're injured severely
339                         // so stop preferring this enemy
340                         // (it will still take a half second until a new one is chosen)
341                         self.havocbot_stickenemy = 0;
342                 }
343         }
344         if (time < self.havocbot_chooseenemy_finished)
345                 return;
346         self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
347         eye = (self.origin + self.view_ofs);
348         best = world;
349         bestrating = 100000000;
350         head = findchainfloat(bot_attack, TRUE);
351         while (head)
352         {
353                 v = (head.absmin + head.absmax) * 0.5;
354                 rating = vlen(v - eye);
355                 if (bestrating > rating)
356                 if (bot_shouldattack(head))
357                 {
358                         traceline(eye, v, TRUE, self);
359                         if (trace_ent == head || trace_fraction >= 1)
360                         {
361                                 best = head;
362                                 bestrating = rating;
363                         }
364                 }
365                 head = head.chain;
366         }
367         self.enemy = best;
368         self.havocbot_stickenemy = TRUE;
369 };
370
371 .float bot_chooseweapontime;
372 float(entity e) w_getbestweapon;
373 void havocbot_chooseweapon()
374 {
375         // TODO: clean this up by moving it to weapon code
376         if(self.enemy.classname!="player")
377         {
378                 self.switchweapon = w_getbestweapon(self);
379                 return;
380         }
381
382         local float w, s;
383         local float rocket  ; rocket   =-1000;
384         local float nex     ; nex      =-1000;
385         local float hagar   ; hagar    =-1000;
386         local float grenade ; grenade  =-1000;
387         local float electro ; electro  =-1000;
388         local float crylink ; crylink  =-1000;
389         local float uzi     ; uzi      =-1000;
390         local float shotgun ; shotgun  =-1000;
391         local float campingrifle ; campingrifle  =-1000;
392         local float laser   ; laser    =-1000;
393         local float minstanex ; minstanex =-1000;
394         local float currentscore;
395         local float bestscore; bestscore = 0;
396         local float bestweapon; bestweapon=self.switchweapon;
397         local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
398         local float maxdelaytime=0.5;
399         local float spreadpenalty=10;
400         local float distancefromfloor;
401
402         local float af, ct, combo_time;
403
404         traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
405         distancefromfloor = self.enemy.origin_z - trace_endpos_z;
406         
407         af = ATTACK_FINISHED(self);
408         ct = cvar("bot_ai_weapon_combo_threshold");
409
410         // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
411         // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
412         combo_time = time + ct + (ct * ((-0.3*skill)+3));
413
414         // Custom weapon list based on distance to the enemy
415         local float i; i = 0;
416         if(bot_custom_weapon){
417
418                 // Choose weapons for far distance
419                 if ( distance > bot_distance_far ) {
420                         for(i=0; i < WEP_LAST && bot_weapons_far[i] != -1 ; ++i){
421                                 w = bot_weapons_far[i];
422                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
423                                         if ( self.weapon == w){
424                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
425                                                         continue;
426                                         } else {
427                                                 self.switchweapon = w;
428                                         }
429                                         return;
430                                 }
431                         }
432                 }
433
434                 // Choose weapons for mid distance
435                 if ( distance > bot_distance_close ) {
436                         for(i=0; i < WEP_LAST && bot_weapons_mid[i] != -1 ; ++i){
437                                 w = bot_weapons_mid[i];
438                                 if ( client_hasweapon(self, w, TRUE, FALSE) ){
439                                         if ( self.weapon == w){
440                                                 if( cvar("bot_ai_weapon_combo") && af > combo_time)
441                                                         continue;
442                                         } else {
443                                                 self.switchweapon = w;
444                                         }
445                                         return;
446                                 }
447                         }
448                 }
449
450                 // Choose weapons for close distance
451                 for(i=0; i < WEP_LAST && bot_weapons_close[i] != -1 ; ++i){
452                         w = bot_weapons_close[i];
453                         if ( client_hasweapon(self, w, TRUE, FALSE) ){
454                                 if ( self.weapon == w){
455                                         if( cvar("bot_ai_weapon_combo") && af > combo_time)
456                                                 continue;
457                                 } else {
458                                         self.switchweapon = w;
459                                 }
460                                 return;
461                         }
462                 }
463                 // If now weapon was chosen by this system fall back to the previous one
464         }
465
466         // Formula:
467         //      (Damage/Sec * Weapon spefic change to get that damage)
468         //      *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
469         //      *(Spread change of hit) // if it applies
470         //      *(Penality for target beeing in air)
471         // %weaponaddpoint
472         if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
473                 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
474                         * (0.5);
475
476         if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE)  && 
477                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER && 
478                         af > combo_time
479                 )
480         )
481                 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
482                         * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
483                         
484         if (client_hasweapon(self, WEP_NEX, TRUE, FALSE)  && 
485                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX && 
486                         af > combo_time
487                 )
488         )
489                 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
490                         * (0.5);
491                         
492         if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // && 
493         //      !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR &&  time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
494                 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
495                         * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
496                         
497         if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) && 
498                 !( 
499                         cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER && 
500                         af > combo_time
501                 )
502         )
503                 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
504                         * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
505                         
506         if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) && 
507                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO && 
508                         af > combo_time
509                 )
510         )
511                 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
512                         * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
513                         
514         if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // && 
515         //      !( self.weapon == WEP_CRYLINK &&  time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
516                 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
517                         * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
518                         
519         if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // && 
520         //      !( self.weapon == WEP_UZI &&  time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
521                 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
522                         * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
523                         
524         if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) && 
525                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN && 
526                         af > combo_time
527                 )
528         )
529                 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
530                         * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
531                         
532         if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) && 
533                 !(      cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER && 
534                         af > combo_time
535                 )
536         )
537                 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
538                         * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
539                         
540         if((self.enemy.flags & FL_ONGROUND)==FALSE){
541                 rocket = rocket   * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius"         ),0.9)); //slight bigger change
542                 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
543                 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius"        ),0.95));
544                 laser = laser     * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius"                  ),0.95));
545         }
546         /*
547         dprint("Floor distance: ",ftos(distancefromfloor),"\n");
548         dprint("Rocket: " , ftos(rocket  ), "\n");
549         dprint("Nex: "    , ftos(nex     ), "\n");
550         dprint("Hagar: "  , ftos(hagar   ), "\n");
551         dprint("Grenade: ", ftos(grenade ), "\n");
552         dprint("Electro: ", ftos(electro ), "\n");
553         dprint("Crylink: ", ftos(crylink ), "\n");
554         dprint("Uzi: "    , ftos(uzi     ), "\n");
555         dprint("Shotgun :", ftos(shotgun ), "\n");
556         dprint("Laser   :", ftos(laser   ), "\n\n");
557         */
558         currentscore = -1;
559         w = WEP_MINSTANEX        ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
560         w = WEP_ROCKET_LAUNCHER  ;s = rocket   ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
561         w = WEP_NEX              ;s = nex      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
562         w = WEP_HAGAR            ;s = hagar    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
563         w = WEP_GRENADE_LAUNCHER ;s = grenade  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
564         w = WEP_ELECTRO          ;s = electro  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
565         w = WEP_CRYLINK          ;s = crylink  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
566         w = WEP_UZI              ;s = uzi      ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
567         w = WEP_SHOTGUN          ;s = shotgun  ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
568         w = WEP_LASER            ;s = laser    ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
569
570         // switch if the best weapon would provide a significant damage increase
571         if (bestscore > currentscore*1.5){
572                 self.switchweapon = bestweapon;
573                 
574                 // buys time for detonating the rocket. not tested yet
575                 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
576                         self.bot_chooseweapontime += (distance  / cvar("g_balance_rocketlauncher_speed"));
577         }
578 };
579
580 .float nextaim;
581 void havocbot_aim()
582 {
583         local vector selfvel, enemyvel;
584         if(self.flags & FL_INWATER)
585                 return;
586         if (time < self.nextaim)
587                 return;
588         self.nextaim = time + 0.1;
589         selfvel = self.velocity;
590         if (!self.waterlevel)
591                 selfvel_z = 0;
592         if (self.enemy)
593         {
594                 enemyvel = self.enemy.velocity;
595                 if (!self.enemy.waterlevel)
596                         enemyvel_z = 0;
597                 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
598         }
599         else
600                 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
601 };
602
603 void havocbot_ai()
604 {
605         if (bot_strategytoken == self)
606         if (!bot_strategytoken_taken)
607         {
608                 self.havocbot_role();
609                 // token has been used this frame
610                 bot_strategytoken_taken = TRUE;
611         }
612         if(self.deadflag)
613                 return;
614
615         havocbot_chooseenemy();
616         if (self.bot_chooseweapontime < time )
617         {
618                 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
619                 havocbot_chooseweapon();
620         }
621         havocbot_aim();
622         lag_update();
623         if (self.bot_aimtarg)
624         {
625                 weapon_action(self.weapon, WR_AIM);
626                 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
627                 {
628                         self.BUTTON_ATCK = FALSE;
629                         self.BUTTON_ATCK2 = FALSE;
630                 }
631         }
632         else if (self.goalcurrent)
633         {
634                 local vector now,v,next;//,heading;
635                 local float distance,skillblend,distanceblend,blend;
636                 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
637                 distance = vlen(now);
638                 //heading = self.velocity;
639                 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
640                 if(self.goalstack01 != self && self.goalstack01 != world)
641                         next = self.goalstack01.origin - (self.origin + self.view_ofs);
642                 skillblend=bound(0,(skill-2.5)*0.5,1); //lower skill player can't preturn
643                 distanceblend=bound(0,distance/cvar("bot_ai_keyboard_distance"),1);
644                 blend = skillblend * (1-distanceblend);
645                 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
646                 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
647                 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
648                 v = now + blend * (next - now);
649                 //dprint(etos(self), " ");
650                 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
651                 //v = now * (distanceblend) + next * (1-distanceblend);
652                 if (self.waterlevel < 2)
653                         v_z = 0;
654                 //dprint("walk at:", vtos(v), "\n");
655                 //te_lightning2(world, self.origin, self.goalcurrent.origin);
656                 bot_aimdir(v, -1);
657         }
658         havocbot_movetogoal();
659 };
660
661 void havocbot_setupbot()
662 {
663         self.bot_ai = havocbot_ai;
664         // will be updated by think code
665         //Generate some random skill levels
666         self.havocbot_keyboardskill=random()-0.5;
667         havocbot_chooserole();
668 }