2 void() havocbot_chooserole;
3 .float havocbot_keyboardskill;
4 .float facingwalltime, ignoregoaltime;
6 .float lastfiredweapon;
8 .float havocbot_blockhead;
10 #ifdef DEBUG_BOT_GOALSTACK
11 void debuggoalstack();
14 vector havocbot_dodge()
16 // LordHavoc: disabled because this is too expensive
20 local vector dodge, v, n;
21 local float danger, bestdanger, vl, d;
24 // check for dangerous objects near bot or approaching bot
25 head = findchainfloat(bot_dodge, TRUE);
28 if (head.owner != self)
30 vl = vlen(head.velocity);
31 if (vl > sv_maxspeed * 0.3)
33 n = normalize(head.velocity);
34 v = self.origin - head.origin;
36 if (d > (0 - head.bot_dodgerating))
37 if (d < (vl * 0.2 + head.bot_dodgerating))
39 // calculate direction and distance from the flight path, by removing the forward axis
40 v = v - (n * (v * n));
41 danger = head.bot_dodgerating - vlen(v);
42 if (bestdanger < danger)
45 // dodge to the side of the object
52 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
53 if (bestdanger < danger)
56 dodge = normalize(self.origin - head.origin);
66 .float havocbot_keyboardtime;
67 .float havocbot_ducktime;
68 .vector havocbot_keyboard;
69 void havocbot_keyboard_movement(vector destorg)
71 local vector keyboard;
72 local float blend, maxspeed;
74 maxspeed = cvar("sv_maxspeed");
76 if (time < self.havocbot_keyboardtime)
79 self.havocbot_keyboardtime =
81 self.havocbot_keyboardtime
82 + bound(0,0.05/(skill+self.havocbot_keyboardskill),0.05)
83 +random()*bound(0,0.025/(skill+self.havocbot_keyboardskill),100)
85 keyboard = self.movement * (1.0 / maxspeed);
87 local float trigger, trigger1;
88 blend = bound(0,skill*0.1,1);
89 trigger = cvar("bot_ai_keyboard_treshold");
90 trigger1 = 0 - trigger;
92 // categorize forward movement
93 // at skill < 1.5 only forward
94 // at skill < 2.5 only individual directions
95 // at skill < 4.5 only individual directions, and forward diagonals
96 // at skill >= 4.5, all cases allowed
97 if (keyboard_x > trigger)
103 else if (keyboard_x < trigger1 && skill > 1.5)
118 if (keyboard_y > trigger)
120 else if (keyboard_y < trigger1)
125 if (keyboard_z > trigger)
127 else if (keyboard_z < trigger1)
132 self.havocbot_keyboard = keyboard * maxspeed;
133 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
135 keyboard = self.havocbot_keyboard;
136 blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
137 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
138 self.movement = self.movement + (keyboard - self.movement) * blend;
141 .entity bot_lastseengoal;
142 .float bot_timelastseengoal;
143 .float bot_canruntogoal;
144 void havocbot_bunnyhop(vector dir)
146 local float maxspeed, bunnyhopdistance;
147 local vector deviation;
148 local entity nextgoal;
153 // Don't jump when using some weapons
154 if(self.aistatus & AI_STATUS_ATTACKING)
155 if(self.weapon & WEP_CAMPINGRIFLE)
158 if(self.goalcurrent.classname == "player")
161 maxspeed = cvar("sv_maxspeed");
163 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
165 self.aistatus &~= AI_STATUS_RUNNING;
166 self.BUTTON_JUMP = FALSE;
167 self.bot_canruntogoal = 0;
168 self.bot_timelastseengoal = 0;
172 if(self.waterlevel > WATERLEVEL_WETFEET)
174 self.aistatus &~= AI_STATUS_RUNNING;
178 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
180 self.bot_canruntogoal = 0;
181 self.bot_timelastseengoal = 0;
184 bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
186 nextgoal = self.goalstack[0];
188 // Run only to visible goals
189 if(self.flags & FL_ONGROUND)
190 if(self.speed==maxspeed)
191 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
193 self.bot_lastseengoal = self.goalcurrent;
196 if(self.bot_timelastseengoal)
198 // for a period of time
199 if(time - self.bot_timelastseengoal > cvar("bot_ai_bunnyhop_firstjumpdelay"))
201 local float checkdistance;
202 checkdistance = TRUE;
204 // don't run if it is too close
205 if(self.bot_canruntogoal==0)
207 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_startdistance"))
208 self.bot_canruntogoal = 1;
210 self.bot_canruntogoal = -1;
213 if(self.bot_canruntogoal != 1)
216 if(self.aistatus & AI_STATUS_ROAMING)
217 if(self.goalcurrent.classname=="waypoint")
218 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
219 if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
222 deviation = vectoangles(nextgoal.origin - self.origin) - vectoangles(self.goalcurrent.origin - self.origin);
223 while (deviation_y < -180) deviation_y = deviation_y + 360;
224 while (deviation_y > 180) deviation_y = deviation_y - 360;
226 if(fabs(deviation_y) < 20)
227 if(bunnyhopdistance < vlen(self.origin - nextgoal.origin))
228 if(fabs(nextgoal.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
230 if(vlen(self.goalcurrent.origin - nextgoal.origin) > cvar("bot_ai_bunnyhop_startdistance"))
231 if(checkpvs(self.origin + self.view_ofs, nextgoal))
233 checkdistance = FALSE;
240 self.aistatus &~= AI_STATUS_RUNNING;
241 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_stopdistance"))
242 self.BUTTON_JUMP = TRUE;
246 self.aistatus |= AI_STATUS_RUNNING;
247 self.BUTTON_JUMP = TRUE;
253 self.bot_timelastseengoal = time;
258 self.bot_timelastseengoal = 0;
261 // Release jump button
262 if(self.flags & FL_ONGROUND == 0)
264 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
265 self.BUTTON_JUMP = FALSE;
268 if(self.aistatus & AI_STATUS_RUNNING)
269 if(vlen(self.velocity)>maxspeed)
271 deviation = vectoangles(dir) - vectoangles(self.velocity);
272 while (deviation_y < -180) deviation_y = deviation_y + 360;
273 while (deviation_y > 180) deviation_y = deviation_y - 360;
275 if(fabs(deviation_y)>10)
279 self.movement_y = maxspeed * -1;
280 else if(deviation_y<10)
281 self.movement_y = maxspeed;
287 //.float havocbotignoretime;
288 //.vector bot_dodgevector;
289 //.float bot_dodgevector_time;
290 //.float bot_dodgevector_jumpbutton;
292 .entity ladder_entity;
293 .float rocketjumptime;
294 void havocbot_movetogoal()
296 local vector destorg;
299 local vector flatdir;
302 local vector evadeobstacle;
303 local vector evadelava;
305 local float maxspeed;
308 //if (self.goalentity)
309 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
310 self.movement = '0 0 0';
311 maxspeed = cvar("sv_maxspeed");
313 // Jetpack navigation
314 if(self.navigation_jetpack_goal)
315 if(self.goalcurrent==self.navigation_jetpack_goal)
318 #ifdef DEBUG_BOT_GOALSTACK
320 te_wizspike(self.navigation_jetpack_point);
324 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
326 // Brake almost completely so it can get a good direction
327 if(vlen(self.velocity)>10)
329 self.aistatus |= AI_STATUS_JETPACK_FLYING;
332 makevectors(self.v_angle_y * '0 1 0');
333 dir = normalize(self.navigation_jetpack_point - self.origin);
336 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
338 // Calculate brake distance in xy
342 dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
344 v = vlen(self.velocity - self.velocity_z * '0 0 1');
345 db = (pow(v,2) / (cvar("g_jetpack_acceleration_side") * 2)) + 100;
346 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
347 if(d < db || d < 500)
350 if(fabs(self.velocity_x)>maxspeed*0.3)
352 self.movement_x = dir * v_forward * -maxspeed;
355 // Switch to normal mode
356 self.navigation_jetpack_goal = world;
357 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
358 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
362 else if(checkpvs(self.origin,self.goalcurrent))
364 // If I can see the goal switch to landing code
365 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
366 self.aistatus |= AI_STATUS_JETPACK_LANDING;
371 self.BUTTON_HOOK = TRUE;
372 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
374 self.movement_x = dir * v_forward * maxspeed;
375 self.movement_y = dir * v_right * maxspeed;
380 // Handling of jump pads
381 if(self.jumppadcount)
383 if(self.flags & FL_ONGROUND)
385 self.jumppadcount = FALSE;
386 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
387 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
390 // If got stuck on the jump pad try to reach the farther visible item
391 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
393 if(fabs(self.velocity_z)<50)
395 local entity head, newgoal;
396 local float distance, bestdistance;
398 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
400 if(head.classname=="worldspawn")
403 distance = vlen(head.origin - self.origin);
407 traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
412 if(distance>bestdistance)
415 bestdistance = distance;
421 self.ignoregoal = self.goalcurrent;
422 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
423 navigation_clearroute();
424 navigation_routetogoal(newgoal, self.origin);
425 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
433 if(self.velocity_z>0)
435 local float threshold;
436 threshold = maxspeed * 0.2;
437 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
438 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
444 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
446 if not(self.flags & FL_ONGROUND)
448 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
449 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
450 if(self.items & IT_JETPACK)
452 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
453 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
455 if(self.velocity_z<0)
457 self.BUTTON_HOOK = TRUE;
461 self.BUTTON_HOOK = TRUE;
463 // If there is no goal try to move forward
465 if(self.goalcurrent==world)
468 dir = normalize(self.goalcurrent.origin - self.origin);
470 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
471 local float xyspeed = xyvelocity * dir;
473 if(xyspeed < (maxspeed / 2))
475 makevectors(self.v_angle_y * '0 1 0');
476 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
477 if(trace_fraction==1)
479 self.movement_x = dir * v_forward * maxspeed;
480 self.movement_y = dir * v_right * maxspeed;
482 havocbot_keyboard_movement(self.origin + dir * 100);
486 self.havocbot_blockhead = TRUE;
490 else if(self.health>cvar("g_balance_rocketlauncher_damage")*0.5)
492 if(self.velocity_z < 0)
493 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
495 self.movement_x = maxspeed;
497 if(self.rocketjumptime)
499 if(time > self.rocketjumptime)
501 self.BUTTON_ATCK2 = TRUE;
502 self.rocketjumptime = 0;
507 self.switchweapon = WEP_ROCKET_LAUNCHER;
509 self.BUTTON_ATCK = TRUE;
510 self.rocketjumptime = time + cvar("g_balance_rocketlauncher_detonatedelay");
516 // If there is no goal try to move forward
517 if(self.goalcurrent==world)
518 self.movement_x = maxspeed;
522 // If we are under water with no goals, swim up
524 if(self.goalcurrent==world)
527 if(self.waterlevel>WATERLEVEL_SWIMMING)
529 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
530 self.BUTTON_JUMP = TRUE;
532 self.BUTTON_JUMP = FALSE;
533 makevectors(self.v_angle_y * '0 1 0');
534 self.movement_x = dir * v_forward * maxspeed;
535 self.movement_y = dir * v_right * maxspeed;
536 self.movement_z = dir * v_up * maxspeed;
539 // if there is nowhere to go, exit
540 if (self.goalcurrent == world)
543 if (self.goalcurrent)
544 navigation_poptouchedgoals();
546 // if ran out of goals try to use an alternative goal or get a new strategy asap
547 if(self.goalcurrent == world)
549 self.bot_strategytime = 0;
553 #ifdef DEBUG_BOT_GOALSTACK
557 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
558 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
559 destorg = self.origin;
560 destorg_x = bound(m1_x, destorg_x, m2_x);
561 destorg_y = bound(m1_y, destorg_y, m2_y);
562 destorg_z = bound(m1_z, destorg_z, m2_z);
563 diff = destorg - self.origin;
565 dir = normalize(diff);
566 flatdir = diff;flatdir_z = 0;
567 flatdir = normalize(flatdir);
569 //if (self.bot_dodgevector_time < time)
571 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
572 // self.bot_dodgevector_jumpbutton = 1;
573 evadeobstacle = '0 0 0';
578 if(self.waterlevel>WATERLEVEL_SWIMMING)
581 self.aistatus |= AI_STATUS_OUT_WATER;
585 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
586 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
587 self.BUTTON_JUMP = TRUE;
589 self.BUTTON_JUMP = FALSE;
591 dir = normalize(flatdir);
592 makevectors(self.v_angle_y * '0 1 0');
596 if(self.aistatus & AI_STATUS_OUT_WATER)
597 self.aistatus &~= AI_STATUS_OUT_WATER;
599 // jump if going toward an obstacle that doesn't look like stairs we
600 // can walk up directly
601 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
602 if (trace_fraction < 1)
603 if (trace_plane_normal_z < 0.7)
606 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
607 if (trace_fraction < s + 0.01)
608 if (trace_plane_normal_z < 0.7)
611 tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
612 if (trace_fraction > s)
613 self.BUTTON_JUMP = 1;
617 // avoiding dangers and obstacles
618 local vector dst_ahead, dst_down;
619 makevectors(self.v_angle_y * '0 1 0');
620 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
621 dst_down = dst_ahead + '0 0 -1500';
624 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
626 // Check head-banging against walls
627 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
629 self.BUTTON_JUMP = TRUE;
630 if(self.facingwalltime && time > self.facingwalltime)
632 self.ignoregoal = self.goalcurrent;
633 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
634 self.bot_strategytime = 0;
639 self.facingwalltime = time + 0.05;
644 self.facingwalltime = 0;
646 if(self.ignoregoal != world && time > self.ignoregoaltime)
648 self.ignoregoal = world;
649 self.ignoregoaltime = 0;
653 // Check for water/slime/lava and dangerous edges
654 // (only when the bot is on the ground or jumping intentionally)
655 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
657 if(trace_fraction == 1)
658 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
661 traceline(dst_ahead , dst_down, TRUE, world);
662 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
663 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
664 if(trace_endpos_z < self.origin_z + self.mins_z)
666 s = pointcontents(trace_endpos + '0 0 1');
667 if (s != CONTENT_SOLID)
668 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
669 evadelava = normalize(self.velocity) * -1;
670 else if (s == CONTENT_SKY)
671 evadeobstacle = normalize(self.velocity) * -1;
672 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
673 self.goalcurrent.absmin, self.goalcurrent.absmax))
675 // if ain't a safe goal with "holes" (like the jumpad on soylent)
676 // and there is a trigger_hurt below
677 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
679 // Remove dangerous dynamic goals from stack
680 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
681 navigation_poproute();
684 evadeobstacle = normalize(self.velocity) * -1;
693 makevectors(self.v_angle_y * '0 1 0');
695 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
696 self.aistatus |= AI_STATUS_DANGER_AHEAD;
699 dodge = havocbot_dodge();
700 dodge = dodge * bound(0,3+skill*0.1,1);
701 evadelava = evadelava * bound(1,3-skill,3); //Noobs fear lava a lot and take more distance from it
702 traceline(self.origin, self.enemy.origin, TRUE, world);
703 if(trace_ent.classname == "player")
704 dir = dir * bound(0,skill/7,1);
706 dir = normalize(dir + dodge + evadeobstacle + evadelava);
707 // self.bot_dodgevector = dir;
708 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
711 if(time < self.ladder_time)
713 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
715 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
720 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
725 //dir = self.bot_dodgevector;
726 //if (self.bot_dodgevector_jumpbutton)
727 // self.BUTTON_JUMP = 1;
728 self.movement_x = dir * v_forward * maxspeed;
729 self.movement_y = dir * v_right * maxspeed;
730 self.movement_z = dir * v_up * maxspeed;
732 // Emulate keyboard interface
734 havocbot_keyboard_movement(destorg);
737 // if(self.aistatus & AI_STATUS_ROAMING)
738 if(skill >= cvar("bot_ai_bunnyhop_skilloffset"))
739 havocbot_bunnyhop(dir);
741 if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
742 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill)*0.1,1)) self.BUTTON_JUMP=TRUE;
743 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);
746 .float havocbot_chooseenemy_finished;
747 .float havocbot_stickenemy;
748 void havocbot_chooseenemy()
750 local entity head, best, head2;
751 local float rating, bestrating, i, f;
753 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
760 if (!bot_shouldattack(self.enemy))
762 // enemy died or something, find a new target
764 self.havocbot_chooseenemy_finished = time;
766 else if (self.havocbot_stickenemy)
768 // tracking last chosen enemy
769 // if enemy is visible
770 // and not really really far away
771 // and we're not severely injured
772 // then keep tracking for a half second into the future
773 traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
774 if (trace_ent == self.enemy || trace_fraction == 1)
775 if (vlen(self.enemy.origin - self.origin) < 1000)
776 if (self.health > 30)
778 // remain tracking him for a shot while (case he went after a small corner or pilar
779 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
782 // enemy isn't visible, or is far away, or we're injured severely
783 // so stop preferring this enemy
784 // (it will still take a half second until a new one is chosen)
785 self.havocbot_stickenemy = 0;
788 if (time < self.havocbot_chooseenemy_finished)
790 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
791 eye = self.origin + self.view_ofs;
793 bestrating = 100000000;
794 head = head2 = findchainfloat(bot_attack, TRUE);
796 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
801 v = (head.absmin + head.absmax) * 0.5;
802 rating = vlen(v - eye);
803 if (rating<cvar("bot_ai_enemydetectionradius"))
804 if (bestrating > rating)
805 if (bot_shouldattack(head))
807 traceline(eye, v, TRUE, self);
808 if (trace_ent == head || trace_fraction >= 1)
817 // I want to do a second scan if no enemy was found or I don't have weapons
818 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
819 if(best || self.weapons) // || self.weapon == WEP_CAMPINGRIFLE
824 // Set flags to see through transparent objects
825 f = self.dphitcontentsmask;
826 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
832 // Restore hit flags if needed
834 self.dphitcontentsmask = f;
837 self.havocbot_stickenemy = TRUE;
840 .float bot_chooseweapontime;
841 float(entity e) w_getbestweapon;
842 void havocbot_chooseweapon()
847 if(g_weaponarena == WEPBIT_TUBA)
849 self.switchweapon = WEP_TUBA;
853 // TODO: clean this up by moving it to weapon code
854 if(self.enemy==world)
856 // If no weapon was chosen get the first available weapon
858 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
860 if(client_hasweapon(self, i, TRUE, FALSE))
862 self.switchweapon = i;
869 // Do not change weapon during the next second after a combo
870 i = time - self.lastcombotime;
874 // Workaround for rifle reloading (..)
875 if(self.weapon == WEP_CAMPINGRIFLE)
876 if(i < cvar("g_balance_campingrifle_reloadtime") + 1)
880 local float rocket ; rocket =-1000;
881 local float nex ; nex =-1000;
882 local float hagar ; hagar =-1000;
883 local float grenade ; grenade =-1000;
884 local float electro ; electro =-1000;
885 local float crylink ; crylink =-1000;
886 local float uzi ; uzi =-1000;
887 local float shotgun ; shotgun =-1000;
888 local float campingrifle ; campingrifle =-1000;
889 local float laser ; laser =-1000;
890 local float minstanex ; minstanex =-1000;
891 local float bestscore; bestscore = 0;
892 local float bestweapon; bestweapon=self.switchweapon;
893 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
894 local float maxdelaytime=0.5;
895 local float spreadpenalty=10;
897 // Should it do a weapon combo?
898 local float af, ct, combo_time, combo;
900 af = ATTACK_FINISHED(self);
901 ct = cvar("bot_ai_weapon_combo_threshold");
903 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
904 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
905 combo_time = time + ct + (ct * ((-0.3*skill)+3));
909 if(cvar("bot_ai_weapon_combo"))
910 if(self.weapon == self.lastfiredweapon)
914 self.lastcombotime = time;
917 // Custom weapon list based on distance to the enemy
918 if(bot_custom_weapon){
920 // Choose weapons for far distance
921 if ( distance > bot_distance_far ) {
922 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
923 w = bot_weapons_far[i];
924 if ( client_hasweapon(self, w, TRUE, FALSE) ){
925 if ( self.weapon == w && combo)
927 self.switchweapon = w;
933 // Choose weapons for mid distance
934 if ( distance > bot_distance_close ) {
935 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
936 w = bot_weapons_mid[i];
937 if ( client_hasweapon(self, w, TRUE, FALSE) ){
938 if ( self.weapon == w && combo)
940 self.switchweapon = w;
946 // Choose weapons for close distance
947 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
948 w = bot_weapons_close[i];
949 if ( client_hasweapon(self, w, TRUE, FALSE) ){
950 if ( self.weapon == w && combo)
952 self.switchweapon = w;
959 // TODO: This disabled code is not working well and got replaced by custom weapon priorities.
960 // However, this logic should be refactored and moved to weapons code so each new weapon can be
961 // evaluated dynamically by bots without updating the "ai" or config files. --mand1nga
962 float s, distancefromfloor, currentscore;
966 // (Damage/Sec * Weapon spefic change to get that damage)
967 // *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
968 // *(Spread change of hit) // if it applies
969 // *(Penality for target beeing in air)
972 traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
973 distancefromfloor = self.enemy.origin_z - trace_endpos_z;
975 if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
976 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
979 if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE) &&
980 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER &&
984 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
985 * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
987 if (client_hasweapon(self, WEP_NEX, TRUE, FALSE) &&
988 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX &&
992 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
995 if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // &&
996 // !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR && time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
997 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
998 * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
1000 if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) &&
1002 cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER &&
1006 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
1007 * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
1009 if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) &&
1010 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO &&
1014 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
1015 * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
1017 if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // &&
1018 // !( self.weapon == WEP_CRYLINK && time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
1019 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
1020 * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
1022 if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // &&
1023 // !( self.weapon == WEP_UZI && time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
1024 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
1025 * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
1027 if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) &&
1028 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN &&
1032 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
1033 * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
1035 if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) &&
1036 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER &&
1040 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
1041 * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
1043 if((self.enemy.flags & FL_ONGROUND)==FALSE){
1044 rocket = rocket * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius" ),0.9)); //slight bigger change
1045 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
1046 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius" ),0.95));
1047 laser = laser * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius" ),0.95));
1050 dprint("Floor distance: ",ftos(distancefromfloor),"\n");
1051 dprint("Rocket: " , ftos(rocket ), "\n");
1052 dprint("Nex: " , ftos(nex ), "\n");
1053 dprint("Hagar: " , ftos(hagar ), "\n");
1054 dprint("Grenade: ", ftos(grenade ), "\n");
1055 dprint("Electro: ", ftos(electro ), "\n");
1056 dprint("Crylink: ", ftos(crylink ), "\n");
1057 dprint("Uzi: " , ftos(uzi ), "\n");
1058 dprint("Shotgun :", ftos(shotgun ), "\n");
1059 dprint("Laser :", ftos(laser ), "\n\n");
1062 w = WEP_MINSTANEX ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1063 w = WEP_ROCKET_LAUNCHER ;s = rocket ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1064 w = WEP_NEX ;s = nex ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1065 w = WEP_HAGAR ;s = hagar ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1066 w = WEP_GRENADE_LAUNCHER ;s = grenade ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1067 w = WEP_ELECTRO ;s = electro ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1068 w = WEP_CRYLINK ;s = crylink ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1069 w = WEP_UZI ;s = uzi ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1070 w = WEP_SHOTGUN ;s = shotgun ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1071 w = WEP_LASER ;s = laser ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1073 // switch if the best weapon would provide a significant damage increase
1074 if (bestscore > currentscore*1.5){
1075 self.switchweapon = bestweapon;
1077 // buys time for detonating the rocket. not tested yet
1078 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
1079 self.bot_chooseweapontime += (distance / cvar("g_balance_rocketlauncher_speed"));
1087 local vector selfvel, enemyvel;
1088 // if(self.flags & FL_INWATER)
1090 if (time < self.nextaim)
1092 self.nextaim = time + 0.1;
1093 selfvel = self.velocity;
1094 if (!self.waterlevel)
1098 enemyvel = self.enemy.velocity;
1099 if (!self.enemy.waterlevel)
1101 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1104 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1113 if(bot_execute_commands())
1116 if (bot_strategytoken == self)
1117 if (!bot_strategytoken_taken)
1119 if(self.havocbot_blockhead)
1121 self.havocbot_blockhead = FALSE;
1125 self.havocbot_role();
1128 // TODO: tracewalk() should take care of this job (better path finding under water)
1129 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
1130 if(self.deadflag != DEAD_NO)
1131 if(self.goalcurrent==world)
1132 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
1134 // Look for the closest waypoint out of water
1135 local entity newgoal, head;
1136 local float bestdistance, distance;
1139 bestdistance = 10000;
1140 for (head = findchain(classname, "waypoint"); head; head = head.chain)
1142 distance = vlen(head.origin - self.origin);
1146 if(head.origin_z < self.origin_z)
1149 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
1152 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
1155 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
1157 if(trace_fraction<1)
1160 if(distance<bestdistance)
1163 bestdistance = distance;
1169 // te_wizspike(newgoal.origin);
1170 navigation_pushroute(newgoal);
1174 // token has been used this frame
1175 bot_strategytoken_taken = TRUE;
1178 if(self.deadflag != DEAD_NO)
1181 havocbot_chooseenemy();
1182 if (self.bot_chooseweapontime < time )
1184 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
1185 havocbot_chooseweapon();
1189 if (self.bot_aimtarg)
1191 self.aistatus |= AI_STATUS_ATTACKING;
1192 self.aistatus &~= AI_STATUS_ROAMING;
1196 weapon_action(self.weapon, WR_AIM);
1197 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
1199 self.BUTTON_ATCK = FALSE;
1200 self.BUTTON_ATCK2 = FALSE;
1204 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
1205 self.lastfiredweapon = self.weapon;
1210 if(self.bot_aimtarg.classname=="player")
1211 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
1214 else if (self.goalcurrent)
1216 self.aistatus |= AI_STATUS_ROAMING;
1217 self.aistatus &~= AI_STATUS_ATTACKING;
1219 local vector now,v,next;//,heading;
1220 local float aimdistance,skillblend,distanceblend,blend;
1221 local entity nextgoal;
1223 nextgoal = self.goalstack[0];
1224 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
1225 aimdistance = vlen(now);
1226 //heading = self.velocity;
1227 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
1229 nextgoal != self && nextgoal != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
1230 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
1232 next = nextgoal.origin - (self.origin + self.view_ofs);
1234 skillblend=bound(0,(skill-2.5)*0.5,1); //lower skill player can't preturn
1235 distanceblend=bound(0,aimdistance/cvar("bot_ai_keyboard_distance"),1);
1236 blend = skillblend * (1-distanceblend);
1237 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
1238 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
1239 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
1240 v = now + blend * (next - now);
1241 //dprint(etos(self), " ");
1242 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
1243 //v = now * (distanceblend) + next * (1-distanceblend);
1244 if (self.waterlevel < WATERLEVEL_SWIMMING)
1246 //dprint("walk at:", vtos(v), "\n");
1247 //te_lightning2(world, self.origin, self.goalcurrent.origin);
1250 havocbot_movetogoal();
1253 .entity havocbot_personal_waypoint;
1254 .float havocbot_personal_waypoint_searchtime;
1255 float havocbot_moveto_refresh_route()
1257 // Refresh path to goal if necessary
1259 wp = self.havocbot_personal_waypoint;
1260 navigation_goalrating_start();
1261 navigation_routerating(wp, 10000, 10000);
1262 navigation_goalrating_end();
1263 return self.navigation_hasgoals;
1266 .float havocbot_personal_waypoint_failcounter;
1267 float havocbot_moveto(vector pos)
1271 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1273 // Step 4: Move to waypoint
1274 if(self.havocbot_personal_waypoint==world)
1276 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1277 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1278 return CMD_STATUS_ERROR;
1281 if (!bot_strategytoken_taken)
1282 if(self.havocbot_personal_waypoint_searchtime<time)
1284 bot_strategytoken_taken = TRUE;
1285 if(havocbot_moveto_refresh_route())
1287 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1288 self.havocbot_personal_waypoint_searchtime = time + 10;
1289 self.havocbot_personal_waypoint_failcounter = 0;
1293 self.havocbot_personal_waypoint_failcounter += 1;
1294 self.havocbot_personal_waypoint_searchtime = time + 2;
1295 if(self.havocbot_personal_waypoint_failcounter >= 30)
1297 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1298 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1299 remove(self.havocbot_personal_waypoint);
1300 return CMD_STATUS_ERROR;
1303 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1307 #ifdef DEBUG_BOT_GOALSTACK
1312 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1314 bot_aimdir(dir, -1);
1317 havocbot_movetogoal();
1319 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1321 // Step 5: Waypoint reached
1322 dprint(self.netname, "'s personal waypoint reached\n");
1323 remove(self.havocbot_personal_waypoint);
1324 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1325 return CMD_STATUS_FINISHED;
1328 return CMD_STATUS_EXECUTING;
1331 // Step 2: Linking waypoint
1332 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1334 // Wait until it is linked
1335 if(!self.havocbot_personal_waypoint.wplinked)
1337 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1338 return CMD_STATUS_EXECUTING;
1341 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1342 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1343 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1345 // Step 3: Route to waypoint
1346 dprint(self.netname, " walking to its personal waypoint\n");
1348 return CMD_STATUS_EXECUTING;
1351 // Step 1: Spawning waypoint
1352 wp = waypoint_spawnpersonal(pos);
1355 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1356 return CMD_STATUS_ERROR;
1359 self.havocbot_personal_waypoint = wp;
1360 self.havocbot_personal_waypoint_failcounter = 0;
1361 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1363 return CMD_STATUS_EXECUTING;
1366 float havocbot_resetgoal()
1368 navigation_clearroute();
1369 return CMD_STATUS_FINISHED;
1372 void havocbot_setupbot()
1374 self.bot_ai = havocbot_ai;
1375 self.cmd_moveto = havocbot_moveto;
1376 self.cmd_resetgoal = havocbot_resetgoal;
1378 // will be updated by think code
1379 //Generate some random skill levels
1380 self.havocbot_keyboardskill=random()-0.5;
1381 havocbot_chooserole();
1385 #ifdef DEBUG_BOT_GOALSTACK
1388 .vector lastposition;
1390 // Debug the goal stack visually
1391 void debuggoalstack()
1393 local entity target;
1396 if(self.goalcounter==0)
1397 target=self.goalcurrent;
1398 else if(self.goalcounter<=GOALSTACK_LENGTH)
1399 target = self.goalstack[self.goalcounter-1];
1403 self.goalcounter = 0;
1404 self.lastposition='0 0 0';
1408 if(self.lastposition=='0 0 0')
1411 org = self.lastposition;
1414 te_lightning2(world, org, target.origin);
1415 self.lastposition = target.origin;