3 void() havocbot_chooserole;
4 .float havocbot_keyboardskill;
6 vector havocbot_dodge()
8 // LordHavoc: disabled because this is too expensive
12 local vector dodge, v, n;
13 local float danger, bestdanger, vl, d;
16 // check for dangerous objects near bot or approaching bot
17 head = findchainfloat(bot_dodge, TRUE);
20 if (head.owner != self)
22 vl = vlen(head.velocity);
23 if (vl > sv_maxspeed * 0.3)
25 n = normalize(head.velocity);
26 v = self.origin - head.origin;
28 if (d > (0 - head.bot_dodgerating))
29 if (d < (vl * 0.2 + head.bot_dodgerating))
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)
37 // dodge to the side of the object
44 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
45 if (bestdanger < danger)
48 dodge = normalize(self.origin - head.origin);
58 .float havocbot_keyboardtime;
59 .float havocbot_ducktime;
60 .vector havocbot_keyboard;
61 void havocbot_keyboard_movement(vector destorg)
63 local vector keyboard;
64 local float blend, maxspeed;
66 maxspeed = cvar("sv_maxspeed");
68 if (time < self.havocbot_keyboardtime)
71 self.havocbot_keyboardtime =
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)
77 keyboard = self.movement * (1.0 / maxspeed);
79 local float trigger, trigger1;
80 blend = bound(0,skill*0.1,1);
81 trigger = cvar("bot_ai_keyboard_treshold");
82 trigger1 = 0 - trigger;
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)
95 else if (keyboard_x < trigger1 && skill > 1.5)
110 if (keyboard_y > trigger)
112 else if (keyboard_y < trigger1)
117 if (keyboard_z > trigger)
119 else if (keyboard_z < trigger1)
124 self.havocbot_keyboard = keyboard * maxspeed;
125 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
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;
133 //.float havocbotignoretime;
134 //.vector bot_dodgevector;
135 //.float bot_dodgevector_time;
136 //.float bot_dodgevector_jumpbutton;
137 void havocbot_movetogoal()
139 local vector destorg;
142 local vector flatdir;
145 local vector evadeobstacle;
146 local vector evadelava;
148 local float maxspeed;
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");
156 if(self.jumppadcount)
158 if(self.flags & FL_ONGROUND)
159 self.jumppadcount = FALSE;
164 if (self.goalcurrent == world)
167 navigation_poptouchedgoals();
168 if (self.goalcurrent == world)
170 if(self.alternativegoal==world)
172 // ran out of goals, rethink strategy as soon as possible
173 self.bot_strategytime = 0;
176 // try to use the alternative goal
177 navigation_findnearestwaypoint(self.alternativegoal, TRUE);
178 navigation_routetogoal(self.alternativegoal);
179 self.alternativegoal = world;
183 // te_wizspike(self.goalcurrent.origin);
184 // te_lightning2(world, self.origin, self.goalcurrent.origin);
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;
194 dir = normalize(diff);
195 flatdir = diff;flatdir_z = 0;
196 flatdir = normalize(flatdir);
198 //if (self.bot_dodgevector_time < time)
200 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
201 // self.bot_dodgevector_jumpbutton = 1;
202 evadeobstacle = '0 0 0';
206 makevectors(self.v_angle);
207 self.BUTTON_JUMP = 1;
208 // TODO: Make them swim up
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)
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)
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;
230 // avoiding dangers and obstacles
231 // TODO: don't make this check every frame
232 if(self.flags & FL_ONGROUND)
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';
238 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
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)
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))
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;
263 makevectors(self.v_angle_y * '0 1 0');
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);
273 dir = normalize(dir + dodge + evadeobstacle + evadelava);
274 // self.bot_dodgevector = dir;
275 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
278 //dir = self.bot_dodgevector;
279 //if (self.bot_dodgevector_jumpbutton)
280 // self.BUTTON_JUMP = 1;
282 self.movement_x = dir * v_forward * maxspeed;
283 self.movement_y = dir * v_right * maxspeed;
284 self.movement_z = dir * v_up * maxspeed;
286 // Emulate keyboard interface
288 havocbot_keyboard_movement(destorg);
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);
295 .float havocbot_chooseenemy_finished;
296 .float havocbot_stickenemy;
297 void havocbot_chooseenemy()
299 local entity head, best;
300 local float rating, bestrating;
302 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
309 if (!bot_shouldattack(self.enemy))
311 // enemy died or something, find a new target
313 self.havocbot_chooseenemy_finished = time;
315 else if (self.havocbot_stickenemy)
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)
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");
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;
337 if (time < self.havocbot_chooseenemy_finished)
339 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
340 eye = (self.origin + self.view_ofs);
342 bestrating = 100000000;
343 head = findchainfloat(bot_attack, TRUE);
346 v = (head.absmin + head.absmax) * 0.5;
347 rating = vlen(v - eye);
348 if (bestrating > rating)
349 if (bot_shouldattack(head))
351 traceline(eye, v, TRUE, self);
352 if (trace_ent == head || trace_fraction >= 1)
361 self.havocbot_stickenemy = 1;
364 .float bot_chooseweapontime;
365 float(entity e) w_getbestweapon;
366 void havocbot_chooseweapon()
368 // TODO: clean this up by moving it to weapon code
369 if(self.enemy.classname!="player")
371 self.switchweapon = w_getbestweapon(self);
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;
395 local float af, ct, combo_time;
397 traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
398 distancefromfloor = self.enemy.origin_z - trace_endpos_z;
400 af = ATTACK_FINISHED(self);
401 ct = cvar("bot_ai_weapon_combo_threshold");
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));
407 // Custom weapon list based on distance to the enemy
408 local float i; i = 0;
409 if(bot_custom_weapon){
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)
420 self.switchweapon = w;
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)
436 self.switchweapon = w;
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)
451 self.switchweapon = w;
456 // If now weapon was chosen by this system fall back to the previous one
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)
465 if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
466 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
469 if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE) &&
470 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER &&
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;
477 if (client_hasweapon(self, WEP_NEX, TRUE, FALSE) &&
478 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX &&
482 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
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;
490 if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) &&
492 cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER &&
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;
499 if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) &&
500 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO &&
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;
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;
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);
517 if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) &&
518 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN &&
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);
525 if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) &&
526 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER &&
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);
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));
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");
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;
563 // switch if the best weapon would provide a significant damage increase
564 if (bestscore > currentscore*1.5){
565 self.switchweapon = bestweapon;
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"));
576 local vector selfvel, enemyvel;
577 if (time < self.nextaim)
579 self.nextaim = time + 0.1;
580 selfvel = self.velocity;
581 if (!self.waterlevel)
585 enemyvel = self.enemy.velocity;
586 if (!self.enemy.waterlevel)
588 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
591 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
596 if (bot_strategytoken == self)
597 if (!bot_strategytoken_taken)
599 self.havocbot_role();
600 // token has been used this frame
601 bot_strategytoken_taken = TRUE;
606 havocbot_chooseenemy();
607 if (self.bot_chooseweapontime < time )
609 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
610 havocbot_chooseweapon();
614 if (self.bot_aimtarg)
616 weapon_action(self.weapon, WR_AIM);
617 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
619 self.BUTTON_ATCK = FALSE;
620 self.BUTTON_ATCK2 = FALSE;
623 else if (self.goalcurrent)
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)
645 //dprint("walk at:", vtos(v), "\n");
646 //te_lightning2(world, self.origin, self.goalcurrent.origin);
649 havocbot_movetogoal();
652 void havocbot_setupbot()
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();