2 #include "role_onslaught.qc"
3 #include "role_keyhunt.qc"
4 #include "role_assault.qc"
12 if(bot_execute_commands())
15 if (bot_strategytoken == self)
16 if (!bot_strategytoken_taken)
18 if(self.havocbot_blockhead)
20 self.havocbot_blockhead = FALSE;
24 if not(self.jumppadcount)
28 // TODO: tracewalk() should take care of this job (better path finding under water)
29 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
30 if(self.deadflag != DEAD_NO)
31 if(self.goalcurrent==world)
32 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
34 // Look for the closest waypoint out of water
36 float bestdistance, distance;
40 for (head = findchain(classname, "waypoint"); head; head = head.chain)
42 distance = vlen(head.origin - self.origin);
46 if(head.origin_z < self.origin_z)
49 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
52 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
55 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
60 if(distance<bestdistance)
63 bestdistance = distance;
69 // te_wizspike(newgoal.origin);
70 navigation_pushroute(newgoal);
74 // token has been used this frame
75 bot_strategytoken_taken = TRUE;
78 if(self.deadflag != DEAD_NO)
81 havocbot_chooseenemy();
82 if (self.bot_chooseweapontime < time )
84 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
85 havocbot_chooseweapon();
91 self.aistatus |= AI_STATUS_ATTACKING;
92 self.aistatus &~= AI_STATUS_ROAMING;
94 if(!WEPSET_EMPTY_E(self))
96 weapon_action(self.weapon, WR_AIM);
97 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
99 self.BUTTON_ATCK = FALSE;
100 self.BUTTON_ATCK2 = FALSE;
104 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
105 self.lastfiredweapon = self.weapon;
110 if(self.bot_aimtarg.classname=="player")
111 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
114 else if (self.goalcurrent)
116 self.aistatus |= AI_STATUS_ROAMING;
117 self.aistatus &~= AI_STATUS_ATTACKING;
119 vector now,v,next;//,heading;
120 float aimdistance,skillblend,distanceblend,blend;
121 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
122 aimdistance = vlen(now);
123 //heading = self.velocity;
124 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
126 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
127 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
129 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
131 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
132 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
133 blend = skillblend * (1-distanceblend);
134 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
135 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
136 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
137 v = now + blend * (next - now);
138 //dprint(etos(self), " ");
139 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
140 //v = now * (distanceblend) + next * (1-distanceblend);
141 if (self.waterlevel < WATERLEVEL_SWIMMING)
143 //dprint("walk at:", vtos(v), "\n");
144 //te_lightning2(world, self.origin, self.goalcurrent.origin);
147 havocbot_movetogoal();
149 // if the bot is not attacking, consider reloading weapons
150 if not(self.aistatus & AI_STATUS_ATTACKING)
155 // we are currently holding a weapon that's not fully loaded, reload it
156 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
157 if(self.clip_load < self.clip_size)
158 self.impulse = 20; // "press" the reload button, not sure if this is done right
160 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
161 // the code above executes next frame, starting the reloading then
162 if(skill >= 5) // bots can only look for unloaded weapons past this skill
163 if(self.clip_load >= 0) // only if we're not reloading a weapon already
165 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
167 e = get_weaponinfo(i);
168 if (WEPSET_CONTAINS_EW(self, i) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < cvar(strcat("g_balance_", e.netname, "_reload_ammo"))))
169 self.switchweapon = i;
175 void havocbot_keyboard_movement(vector destorg)
178 float blend, maxspeed;
181 sk = skill + self.bot_moveskill;
183 maxspeed = autocvar_sv_maxspeed;
185 if (time < self.havocbot_keyboardtime)
188 self.havocbot_keyboardtime =
190 self.havocbot_keyboardtime
191 + 0.05/max(1, sk+self.havocbot_keyboardskill)
192 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
194 keyboard = self.movement * (1.0 / maxspeed);
196 float trigger, trigger1;
197 blend = bound(0,sk*0.1,1);
198 trigger = autocvar_bot_ai_keyboard_threshold;
199 trigger1 = 0 - trigger;
201 // categorize forward movement
202 // at skill < 1.5 only forward
203 // at skill < 2.5 only individual directions
204 // at skill < 4.5 only individual directions, and forward diagonals
205 // at skill >= 4.5, all cases allowed
206 if (keyboard_x > trigger)
212 else if (keyboard_x < trigger1 && sk > 1.5)
227 if (keyboard_y > trigger)
229 else if (keyboard_y < trigger1)
234 if (keyboard_z > trigger)
236 else if (keyboard_z < trigger1)
241 self.havocbot_keyboard = keyboard * maxspeed;
242 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
244 keyboard = self.havocbot_keyboard;
245 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
246 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
247 self.movement = self.movement + (keyboard - self.movement) * blend;
250 void havocbot_bunnyhop(vector dir)
252 float bunnyhopdistance;
257 if(autocvar_g_midair)
260 // Don't jump when attacking
261 if(self.aistatus & AI_STATUS_ATTACKING)
264 if(self.goalcurrent.classname == "player")
267 maxspeed = autocvar_sv_maxspeed;
269 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
271 self.aistatus &~= AI_STATUS_RUNNING;
272 self.BUTTON_JUMP = FALSE;
273 self.bot_canruntogoal = 0;
274 self.bot_timelastseengoal = 0;
278 if(self.waterlevel > WATERLEVEL_WETFEET)
280 self.aistatus &~= AI_STATUS_RUNNING;
284 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
286 self.bot_canruntogoal = 0;
287 self.bot_timelastseengoal = 0;
290 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
291 bunnyhopdistance = vlen(self.origin - gco);
293 // Run only to visible goals
294 if(self.flags & FL_ONGROUND)
295 if(self.speed==maxspeed)
296 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
298 self.bot_lastseengoal = self.goalcurrent;
301 if(self.bot_timelastseengoal)
303 // for a period of time
304 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
307 checkdistance = TRUE;
309 // don't run if it is too close
310 if(self.bot_canruntogoal==0)
312 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
313 self.bot_canruntogoal = 1;
315 self.bot_canruntogoal = -1;
318 if(self.bot_canruntogoal != 1)
321 if(self.aistatus & AI_STATUS_ROAMING)
322 if(self.goalcurrent.classname=="waypoint")
323 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
324 if(fabs(gco_z - self.origin_z) < self.maxs_z - self.mins_z)
325 if(self.goalstack01!=world)
327 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
328 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
329 while (deviation_y < -180) deviation_y = deviation_y + 360;
330 while (deviation_y > 180) deviation_y = deviation_y - 360;
332 if(fabs(deviation_y) < 20)
333 if(bunnyhopdistance < vlen(self.origin - gno))
334 if(fabs(gno_z - gco_z) < self.maxs_z - self.mins_z)
336 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
337 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
339 checkdistance = FALSE;
346 self.aistatus &~= AI_STATUS_RUNNING;
347 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
348 self.BUTTON_JUMP = TRUE;
352 self.aistatus |= AI_STATUS_RUNNING;
353 self.BUTTON_JUMP = TRUE;
359 self.bot_timelastseengoal = time;
364 self.bot_timelastseengoal = 0;
368 // Release jump button
369 if(!cvar("sv_pogostick"))
370 if(self.flags & FL_ONGROUND == 0)
372 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
373 self.BUTTON_JUMP = FALSE;
376 if(self.aistatus & AI_STATUS_RUNNING)
377 if(vlen(self.velocity)>maxspeed)
379 deviation = vectoangles(dir) - vectoangles(self.velocity);
380 while (deviation_y < -180) deviation_y = deviation_y + 360;
381 while (deviation_y > 180) deviation_y = deviation_y - 360;
383 if(fabs(deviation_y)>10)
387 self.movement_y = maxspeed * -1;
388 else if(deviation_y<10)
389 self.movement_y = maxspeed;
396 void havocbot_movetogoal()
404 vector evadeobstacle;
411 //if (self.goalentity)
412 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
413 self.movement = '0 0 0';
414 maxspeed = autocvar_sv_maxspeed;
416 // Jetpack navigation
418 if(self.navigation_jetpack_goal)
419 if(self.goalcurrent==self.navigation_jetpack_goal)
422 if(autocvar_bot_debug_goalstack)
425 te_wizspike(self.navigation_jetpack_point);
429 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
431 // Brake almost completely so it can get a good direction
432 if(vlen(self.velocity)>10)
434 self.aistatus |= AI_STATUS_JETPACK_FLYING;
437 makevectors(self.v_angle_y * '0 1 0');
438 dir = normalize(self.navigation_jetpack_point - self.origin);
441 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
443 // Calculate brake distance in xy
447 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy_z = 0;
449 v = vlen(self.velocity - self.velocity_z * '0 0 1');
450 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
451 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
452 if(d < db || d < 500)
455 if(fabs(self.velocity_x)>maxspeed*0.3)
457 self.movement_x = dir * v_forward * -maxspeed;
460 // Switch to normal mode
461 self.navigation_jetpack_goal = world;
462 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
463 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
467 else if(checkpvs(self.origin,self.goalcurrent))
469 // If I can see the goal switch to landing code
470 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
471 self.aistatus |= AI_STATUS_JETPACK_LANDING;
476 self.BUTTON_HOOK = TRUE;
477 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
479 self.movement_x = dir * v_forward * maxspeed;
480 self.movement_y = dir * v_right * maxspeed;
485 // Handling of jump pads
486 if(self.jumppadcount)
488 // If got stuck on the jump pad try to reach the farthest visible waypoint
489 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
491 if(fabs(self.velocity_z)<50)
493 entity head, newgoal = world;
494 float distance, bestdistance = 0;
496 for (head = findchain(classname, "waypoint"); head; head = head.chain)
499 distance = vlen(head.origin - self.origin);
503 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), TRUE, world);
508 if(distance>bestdistance)
511 bestdistance = distance;
517 self.ignoregoal = self.goalcurrent;
518 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
519 navigation_clearroute();
520 navigation_routetogoal(newgoal, self.origin);
521 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
529 if(self.velocity_z>0)
531 float threshold, sxy;
532 vector velxy = self.velocity; velxy_z = 0;
534 threshold = maxspeed * 0.2;
537 dprint("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
538 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
543 // Don't chase players while using a jump pad
544 if(self.goalcurrent.classname=="player" || self.goalstack01.classname=="player")
548 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
549 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
551 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
553 if not(self.flags & FL_ONGROUND)
555 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
556 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
557 if(self.items & IT_JETPACK)
559 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
560 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
562 if(self.velocity_z<0)
564 self.BUTTON_HOOK = TRUE;
568 self.BUTTON_HOOK = TRUE;
570 // If there is no goal try to move forward
572 if(self.goalcurrent==world)
575 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
577 vector xyvelocity = self.velocity; xyvelocity_z = 0;
578 float xyspeed = xyvelocity * dir;
580 if(xyspeed < (maxspeed / 2))
582 makevectors(self.v_angle_y * '0 1 0');
583 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
584 if(trace_fraction==1)
586 self.movement_x = dir * v_forward * maxspeed;
587 self.movement_y = dir * v_right * maxspeed;
589 havocbot_keyboard_movement(self.origin + dir * 100);
593 self.havocbot_blockhead = TRUE;
597 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
599 if(self.velocity_z < 0)
600 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
602 self.movement_x = maxspeed;
604 if(self.rocketjumptime)
606 if(time > self.rocketjumptime)
608 self.BUTTON_ATCK2 = TRUE;
609 self.rocketjumptime = 0;
614 self.switchweapon = WEP_ROCKET_LAUNCHER;
616 self.BUTTON_ATCK = TRUE;
617 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
623 // If there is no goal try to move forward
624 if(self.goalcurrent==world)
625 self.movement_x = maxspeed;
629 // If we are under water with no goals, swim up
631 if(self.goalcurrent==world)
634 if(self.waterlevel>WATERLEVEL_SWIMMING)
636 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
637 self.BUTTON_JUMP = TRUE;
639 self.BUTTON_JUMP = FALSE;
640 makevectors(self.v_angle_y * '0 1 0');
641 self.movement_x = dir * v_forward * maxspeed;
642 self.movement_y = dir * v_right * maxspeed;
643 self.movement_z = dir * v_up * maxspeed;
646 // if there is nowhere to go, exit
647 if (self.goalcurrent == world)
650 if (self.goalcurrent)
651 navigation_poptouchedgoals();
653 // if ran out of goals try to use an alternative goal or get a new strategy asap
654 if(self.goalcurrent == world)
656 self.bot_strategytime = 0;
661 if(autocvar_bot_debug_goalstack)
664 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
665 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
666 destorg = self.origin;
667 destorg_x = bound(m1_x, destorg_x, m2_x);
668 destorg_y = bound(m1_y, destorg_y, m2_y);
669 destorg_z = bound(m1_z, destorg_z, m2_z);
670 diff = destorg - self.origin;
672 dir = normalize(diff);
673 flatdir = diff;flatdir_z = 0;
674 flatdir = normalize(flatdir);
675 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
677 //if (self.bot_dodgevector_time < time)
679 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
680 // self.bot_dodgevector_jumpbutton = 1;
681 evadeobstacle = '0 0 0';
686 if(self.waterlevel>WATERLEVEL_SWIMMING)
689 self.aistatus |= AI_STATUS_OUT_WATER;
693 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && gco_z < self.origin_z) &&
694 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
695 self.BUTTON_JUMP = TRUE;
697 self.BUTTON_JUMP = FALSE;
699 dir = normalize(flatdir);
700 makevectors(self.v_angle_y * '0 1 0');
704 if(self.aistatus & AI_STATUS_OUT_WATER)
705 self.aistatus &~= AI_STATUS_OUT_WATER;
707 // jump if going toward an obstacle that doesn't look like stairs we
708 // can walk up directly
709 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
710 if (trace_fraction < 1)
711 if (trace_plane_normal_z < 0.7)
714 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, FALSE, self);
715 if (trace_fraction < s + 0.01)
716 if (trace_plane_normal_z < 0.7)
719 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, FALSE, self);
720 if (trace_fraction > s)
721 self.BUTTON_JUMP = 1;
725 // avoiding dangers and obstacles
726 vector dst_ahead, dst_down;
727 makevectors(self.v_angle_y * '0 1 0');
728 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
729 dst_down = dst_ahead + '0 0 -1500';
732 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
734 // Check head-banging against walls
735 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
737 self.BUTTON_JUMP = TRUE;
738 if(self.facingwalltime && time > self.facingwalltime)
740 self.ignoregoal = self.goalcurrent;
741 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
742 self.bot_strategytime = 0;
747 self.facingwalltime = time + 0.05;
752 self.facingwalltime = 0;
754 if(self.ignoregoal != world && time > self.ignoregoaltime)
756 self.ignoregoal = world;
757 self.ignoregoaltime = 0;
761 // Check for water/slime/lava and dangerous edges
762 // (only when the bot is on the ground or jumping intentionally)
763 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
765 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
766 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
769 traceline(dst_ahead , dst_down, TRUE, world);
770 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
771 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
772 if(trace_endpos_z < self.origin_z + self.mins_z)
774 s = pointcontents(trace_endpos + '0 0 1');
775 if (s != CONTENT_SOLID)
776 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
777 evadelava = normalize(self.velocity) * -1;
778 else if (s == CONTENT_SKY)
779 evadeobstacle = normalize(self.velocity) * -1;
780 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
781 self.goalcurrent.absmin, self.goalcurrent.absmax))
783 // if ain't a safe goal with "holes" (like the jumpad on soylent)
784 // and there is a trigger_hurt below
785 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
787 // Remove dangerous dynamic goals from stack
788 dprint("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
789 navigation_clearroute();
799 makevectors(self.v_angle_y * '0 1 0');
801 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
802 self.aistatus |= AI_STATUS_DANGER_AHEAD;
805 dodge = havocbot_dodge();
806 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
807 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
808 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), TRUE, world);
809 if(trace_ent.classname == "player")
810 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
812 dir = normalize(dir + dodge + evadeobstacle + evadelava);
813 // self.bot_dodgevector = dir;
814 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
817 if(time < self.ladder_time)
819 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
821 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
826 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
831 //dir = self.bot_dodgevector;
832 //if (self.bot_dodgevector_jumpbutton)
833 // self.BUTTON_JUMP = 1;
834 self.movement_x = dir * v_forward * maxspeed;
835 self.movement_y = dir * v_right * maxspeed;
836 self.movement_z = dir * v_up * maxspeed;
838 // Emulate keyboard interface
840 havocbot_keyboard_movement(destorg);
843 // if(self.aistatus & AI_STATUS_ROAMING)
845 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
846 havocbot_bunnyhop(dir);
848 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
849 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
850 if (((dodge * v_up) < 0) && random()*frametime >= 0.5*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.havocbot_ducktime=time+0.3/bound(0.1,skill+self.bot_dodgeskill,10);
853 void havocbot_chooseenemy()
855 entity head, best, head2;
856 float rating, bestrating, i, hf;
858 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
865 if (!bot_shouldattack(self.enemy))
867 // enemy died or something, find a new target
869 self.havocbot_chooseenemy_finished = time;
871 else if (self.havocbot_stickenemy)
873 // tracking last chosen enemy
874 // if enemy is visible
875 // and not really really far away
876 // and we're not severely injured
877 // then keep tracking for a half second into the future
878 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,FALSE,world);
879 if (trace_ent == self.enemy || trace_fraction == 1)
880 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
881 if (self.health > 30)
883 // remain tracking him for a shot while (case he went after a small corner or pilar
884 self.havocbot_chooseenemy_finished = time + 0.5;
887 // enemy isn't visible, or is far away, or we're injured severely
888 // so stop preferring this enemy
889 // (it will still take a half second until a new one is chosen)
890 self.havocbot_stickenemy = 0;
893 if (time < self.havocbot_chooseenemy_finished)
895 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
896 eye = self.origin + self.view_ofs;
898 bestrating = 100000000;
899 head = head2 = findchainfloat(bot_attack, TRUE);
902 hf = self.dphitcontentsmask;
904 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
906 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
912 v = (head.absmin + head.absmax) * 0.5;
913 rating = vlen(v - eye);
914 if (rating<autocvar_bot_ai_enemydetectionradius)
915 if (bestrating > rating)
916 if (bot_shouldattack(head))
918 traceline(eye, v, TRUE, self);
919 if (trace_ent == head || trace_fraction >= 1)
928 // I want to do a second scan if no enemy was found or I don't have weapons
929 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
930 if(best || !WEPSET_EMPTY_E(self)) // || self.weapon == WEP_RIFLE
935 // Set flags to see through transparent objects
936 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
942 self.dphitcontentsmask = hf;
945 self.havocbot_stickenemy = TRUE;
948 float havocbot_chooseweapon_checkreload(float new_weapon)
950 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
951 // so skip this for them, or they'll never get to reload their weapons at all.
952 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
956 // if this weapon is scheduled for reloading, don't switch to it during combat
957 if (self.weapon_load[new_weapon] < 0)
959 float i, other_weapon_available = FALSE;
960 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
962 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
963 if (weapon_action(i, WR_CHECKAMMO1) + weapon_action(i, WR_CHECKAMMO2))
964 other_weapon_available = TRUE;
966 if(other_weapon_available)
973 void havocbot_chooseweapon()
978 if(WEPSET_EQ_AW(g_weaponarena_weapons, WEP_TUBA))
980 self.switchweapon = WEP_TUBA;
984 // TODO: clean this up by moving it to weapon code
985 if(self.enemy==world)
987 // If no weapon was chosen get the first available weapon
989 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
991 if(client_hasweapon(self, i, TRUE, FALSE))
993 self.switchweapon = i;
1000 // Do not change weapon during the next second after a combo
1001 i = time - self.lastcombotime;
1006 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1008 // Should it do a weapon combo?
1009 float af, ct, combo_time, combo;
1011 af = ATTACK_FINISHED(self);
1012 ct = autocvar_bot_ai_weapon_combo_threshold;
1014 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1015 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1016 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1020 if(autocvar_bot_ai_weapon_combo)
1021 if(self.weapon == self.lastfiredweapon)
1025 self.lastcombotime = time;
1028 distance *= pow(2, self.bot_rangepreference);
1030 // Custom weapon list based on distance to the enemy
1031 if(bot_custom_weapon){
1033 // Choose weapons for far distance
1034 if ( distance > bot_distance_far ) {
1035 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1036 w = bot_weapons_far[i];
1037 if ( client_hasweapon(self, w, TRUE, FALSE) )
1039 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1041 self.switchweapon = w;
1047 // Choose weapons for mid distance
1048 if ( distance > bot_distance_close) {
1049 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1050 w = bot_weapons_mid[i];
1051 if ( client_hasweapon(self, w, TRUE, FALSE) )
1053 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1055 self.switchweapon = w;
1061 // Choose weapons for close distance
1062 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1063 w = bot_weapons_close[i];
1064 if ( client_hasweapon(self, w, TRUE, FALSE) )
1066 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1068 self.switchweapon = w;
1077 vector selfvel, enemyvel;
1078 // if(self.flags & FL_INWATER)
1080 if (time < self.nextaim)
1082 self.nextaim = time + 0.1;
1083 selfvel = self.velocity;
1084 if (!self.waterlevel)
1088 enemyvel = self.enemy.velocity;
1089 if (!self.enemy.waterlevel)
1091 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1094 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1097 float havocbot_moveto_refresh_route()
1099 // Refresh path to goal if necessary
1101 wp = self.havocbot_personal_waypoint;
1102 navigation_goalrating_start();
1103 navigation_routerating(wp, 10000, 10000);
1104 navigation_goalrating_end();
1105 return self.navigation_hasgoals;
1108 float havocbot_moveto(vector pos)
1112 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1114 // Step 4: Move to waypoint
1115 if(self.havocbot_personal_waypoint==world)
1117 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1118 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1119 return CMD_STATUS_ERROR;
1122 if (!bot_strategytoken_taken)
1123 if(self.havocbot_personal_waypoint_searchtime<time)
1125 bot_strategytoken_taken = TRUE;
1126 if(havocbot_moveto_refresh_route())
1128 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1129 self.havocbot_personal_waypoint_searchtime = time + 10;
1130 self.havocbot_personal_waypoint_failcounter = 0;
1134 self.havocbot_personal_waypoint_failcounter += 1;
1135 self.havocbot_personal_waypoint_searchtime = time + 2;
1136 if(self.havocbot_personal_waypoint_failcounter >= 30)
1138 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1139 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1140 remove(self.havocbot_personal_waypoint);
1141 return CMD_STATUS_ERROR;
1144 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1148 if(autocvar_bot_debug_goalstack)
1152 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1154 bot_aimdir(dir, -1);
1157 havocbot_movetogoal();
1159 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1161 // Step 5: Waypoint reached
1162 dprint(self.netname, "'s personal waypoint reached\n");
1163 remove(self.havocbot_personal_waypoint);
1164 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1165 return CMD_STATUS_FINISHED;
1168 return CMD_STATUS_EXECUTING;
1171 // Step 2: Linking waypoint
1172 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1174 // Wait until it is linked
1175 if(!self.havocbot_personal_waypoint.wplinked)
1177 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1178 return CMD_STATUS_EXECUTING;
1181 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1182 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1183 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1185 // Step 3: Route to waypoint
1186 dprint(self.netname, " walking to its personal waypoint\n");
1188 return CMD_STATUS_EXECUTING;
1191 // Step 1: Spawning waypoint
1192 wp = waypoint_spawnpersonal(pos);
1195 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1196 return CMD_STATUS_ERROR;
1199 self.havocbot_personal_waypoint = wp;
1200 self.havocbot_personal_waypoint_failcounter = 0;
1201 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1203 // if pos is inside a teleport, then let's mark it as teleport waypoint
1205 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1207 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1209 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1210 self.lastteleporttime = 0;
1215 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1216 print("routing to a teleporter\n");
1218 print("routing to a non-teleporter\n");
1221 return CMD_STATUS_EXECUTING;
1224 float havocbot_resetgoal()
1226 navigation_clearroute();
1227 return CMD_STATUS_FINISHED;
1230 void havocbot_setupbot()
1232 self.bot_ai = havocbot_ai;
1233 self.cmd_moveto = havocbot_moveto;
1234 self.cmd_resetgoal = havocbot_resetgoal;
1236 havocbot_chooserole();
1239 vector havocbot_dodge()
1241 // LordHavoc: disabled because this is too expensive
1246 float danger, bestdanger, vl, d;
1249 // check for dangerous objects near bot or approaching bot
1250 head = findchainfloat(bot_dodge, TRUE);
1253 if (head.owner != self)
1255 vl = vlen(head.velocity);
1256 if (vl > autocvar_sv_maxspeed * 0.3)
1258 n = normalize(head.velocity);
1259 v = self.origin - head.origin;
1261 if (d > (0 - head.bot_dodgerating))
1262 if (d < (vl * 0.2 + head.bot_dodgerating))
1264 // calculate direction and distance from the flight path, by removing the forward axis
1265 v = v - (n * (v * n));
1266 danger = head.bot_dodgerating - vlen(v);
1267 if (bestdanger < danger)
1269 bestdanger = danger;
1270 // dodge to the side of the object
1271 dodge = normalize(v);
1277 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1278 if (bestdanger < danger)
1280 bestdanger = danger;
1281 dodge = normalize(self.origin - head.origin);