2 #include "role_keyhunt.qc"
10 if(bot_execute_commands())
13 if (bot_strategytoken == self)
14 if (!bot_strategytoken_taken)
16 if(self.havocbot_blockhead)
18 self.havocbot_blockhead = FALSE;
22 if (!self.jumppadcount)
26 // TODO: tracewalk() should take care of this job (better path finding under water)
27 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
28 if(self.deadflag != DEAD_NO)
29 if(self.goalcurrent==world)
30 if(self.waterlevel==WATERLEVEL_SWIMMING || (self.aistatus & AI_STATUS_OUT_WATER))
32 // Look for the closest waypoint out of water
34 float bestdistance, distance;
38 for (head = findchain(classname, "waypoint"); head; head = head.chain)
40 distance = vlen(head.origin - self.origin);
44 if(head.origin_z < self.origin_z)
47 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
50 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
53 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
58 if(distance<bestdistance)
61 bestdistance = distance;
67 // te_wizspike(newgoal.origin);
68 navigation_pushroute(newgoal);
72 // token has been used this frame
73 bot_strategytoken_taken = TRUE;
76 if(self.deadflag != DEAD_NO)
79 havocbot_chooseenemy();
80 if (self.bot_chooseweapontime < time )
82 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
83 havocbot_chooseweapon();
89 self.aistatus |= AI_STATUS_ATTACKING;
90 self.aistatus &= ~AI_STATUS_ROAMING;
94 weapon_action(self.weapon, WR_AIM);
95 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
97 self.BUTTON_ATCK = FALSE;
98 self.BUTTON_ATCK2 = FALSE;
102 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
103 self.lastfiredweapon = self.weapon;
108 if(IS_PLAYER(self.bot_aimtarg))
109 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
112 else if (self.goalcurrent)
114 self.aistatus |= AI_STATUS_ROAMING;
115 self.aistatus &= ~AI_STATUS_ATTACKING;
117 vector now,v,next;//,heading;
118 float aimdistance,skillblend,distanceblend,blend;
119 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
120 aimdistance = vlen(now);
121 //heading = self.velocity;
122 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
124 self.goalstack01 != self && self.goalstack01 != world && ((self.aistatus & AI_STATUS_RUNNING) == 0) &&
125 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
127 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
129 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
130 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
131 blend = skillblend * (1-distanceblend);
132 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
133 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
134 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
135 v = now + blend * (next - now);
136 //dprint(etos(self), " ");
137 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
138 //v = now * (distanceblend) + next * (1-distanceblend);
139 if (self.waterlevel < WATERLEVEL_SWIMMING)
141 //dprint("walk at:", vtos(v), "\n");
142 //te_lightning2(world, self.origin, self.goalcurrent.origin);
145 havocbot_movetogoal();
147 // if the bot is not attacking, consider reloading weapons
148 if (!(self.aistatus & AI_STATUS_ATTACKING))
153 // we are currently holding a weapon that's not fully loaded, reload it
154 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
155 if(self.clip_load < self.clip_size)
156 self.impulse = 20; // "press" the reload button, not sure if this is done right
158 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
159 // the code above executes next frame, starting the reloading then
160 if(skill >= 5) // bots can only look for unloaded weapons past this skill
161 if(self.clip_load >= 0) // only if we're not reloading a weapon already
163 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
165 e = get_weaponinfo(i);
166 if ((self.weapons & WepSet_FromWeapon(i)) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < cvar(strcat("g_balance_", e.netname, "_reload_ammo"))))
167 self.switchweapon = i;
173 void havocbot_keyboard_movement(vector destorg)
176 float blend, maxspeed;
179 sk = skill + self.bot_moveskill;
181 maxspeed = autocvar_sv_maxspeed;
183 if (time < self.havocbot_keyboardtime)
186 self.havocbot_keyboardtime =
188 self.havocbot_keyboardtime
189 + 0.05/max(1, sk+self.havocbot_keyboardskill)
190 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
192 keyboard = self.movement * (1.0 / maxspeed);
194 float trigger, trigger1;
195 blend = bound(0,sk*0.1,1);
196 trigger = autocvar_bot_ai_keyboard_threshold;
197 trigger1 = 0 - trigger;
199 // categorize forward movement
200 // at skill < 1.5 only forward
201 // at skill < 2.5 only individual directions
202 // at skill < 4.5 only individual directions, and forward diagonals
203 // at skill >= 4.5, all cases allowed
204 if (keyboard_x > trigger)
210 else if (keyboard_x < trigger1 && sk > 1.5)
225 if (keyboard_y > trigger)
227 else if (keyboard_y < trigger1)
232 if (keyboard_z > trigger)
234 else if (keyboard_z < trigger1)
239 self.havocbot_keyboard = keyboard * maxspeed;
240 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
242 keyboard = self.havocbot_keyboard;
243 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
244 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
245 self.movement = self.movement + (keyboard - self.movement) * blend;
248 void havocbot_bunnyhop(vector dir)
250 float bunnyhopdistance;
255 // Don't jump when attacking
256 if(self.aistatus & AI_STATUS_ATTACKING)
259 if(IS_PLAYER(self.goalcurrent))
262 maxspeed = autocvar_sv_maxspeed;
264 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
266 self.aistatus &= ~AI_STATUS_RUNNING;
267 self.BUTTON_JUMP = FALSE;
268 self.bot_canruntogoal = 0;
269 self.bot_timelastseengoal = 0;
273 if(self.waterlevel > WATERLEVEL_WETFEET)
275 self.aistatus &= ~AI_STATUS_RUNNING;
279 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
281 self.bot_canruntogoal = 0;
282 self.bot_timelastseengoal = 0;
285 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
286 bunnyhopdistance = vlen(self.origin - gco);
288 // Run only to visible goals
289 if(self.flags & FL_ONGROUND)
290 if(self.speed==maxspeed)
291 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
293 self.bot_lastseengoal = self.goalcurrent;
296 if(self.bot_timelastseengoal)
298 // for a period of time
299 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
302 checkdistance = TRUE;
304 // don't run if it is too close
305 if(self.bot_canruntogoal==0)
307 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
308 self.bot_canruntogoal = 1;
310 self.bot_canruntogoal = -1;
313 if(self.bot_canruntogoal != 1)
316 if(self.aistatus & AI_STATUS_ROAMING)
317 if(self.goalcurrent.classname=="waypoint")
318 if (!(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
319 if(fabs(gco_z - self.origin_z) < self.maxs_z - self.mins_z)
320 if(self.goalstack01!=world)
322 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
323 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
324 while (deviation_y < -180) deviation_y = deviation_y + 360;
325 while (deviation_y > 180) deviation_y = deviation_y - 360;
327 if(fabs(deviation_y) < 20)
328 if(bunnyhopdistance < vlen(self.origin - gno))
329 if(fabs(gno_z - gco_z) < self.maxs_z - self.mins_z)
331 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
332 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
334 checkdistance = FALSE;
341 self.aistatus &= ~AI_STATUS_RUNNING;
342 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
343 self.BUTTON_JUMP = TRUE;
347 self.aistatus |= AI_STATUS_RUNNING;
348 self.BUTTON_JUMP = TRUE;
354 self.bot_timelastseengoal = time;
359 self.bot_timelastseengoal = 0;
363 // Release jump button
364 if(!cvar("sv_pogostick"))
365 if((self.flags & FL_ONGROUND) == 0)
367 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
368 self.BUTTON_JUMP = FALSE;
371 if(self.aistatus & AI_STATUS_RUNNING)
372 if(vlen(self.velocity)>maxspeed)
374 deviation = vectoangles(dir) - vectoangles(self.velocity);
375 while (deviation_y < -180) deviation_y = deviation_y + 360;
376 while (deviation_y > 180) deviation_y = deviation_y - 360;
378 if(fabs(deviation_y)>10)
382 self.movement_y = maxspeed * -1;
383 else if(deviation_y<10)
384 self.movement_y = maxspeed;
391 void havocbot_movetogoal()
399 vector evadeobstacle;
406 //if (self.goalentity)
407 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
408 self.movement = '0 0 0';
409 maxspeed = autocvar_sv_maxspeed;
411 // Jetpack navigation
413 if(self.navigation_jetpack_goal)
414 if(self.goalcurrent==self.navigation_jetpack_goal)
417 if(autocvar_bot_debug_goalstack)
420 te_wizspike(self.navigation_jetpack_point);
424 if (!(self.aistatus & AI_STATUS_JETPACK_FLYING))
426 // Brake almost completely so it can get a good direction
427 if(vlen(self.velocity)>10)
429 self.aistatus |= AI_STATUS_JETPACK_FLYING;
432 makevectors(self.v_angle_y * '0 1 0');
433 dir = normalize(self.navigation_jetpack_point - self.origin);
436 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
438 // Calculate brake distance in xy
442 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy_z = 0;
444 v = vlen(self.velocity - self.velocity_z * '0 0 1');
445 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
446 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
447 if(d < db || d < 500)
450 if(fabs(self.velocity_x)>maxspeed*0.3)
452 self.movement_x = dir * v_forward * -maxspeed;
455 // Switch to normal mode
456 self.navigation_jetpack_goal = world;
457 self.aistatus &= ~AI_STATUS_JETPACK_LANDING;
458 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
462 else if(checkpvs(self.origin,self.goalcurrent))
464 // If I can see the goal switch to landing code
465 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
466 self.aistatus |= AI_STATUS_JETPACK_LANDING;
471 self.BUTTON_HOOK = TRUE;
472 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
474 self.movement_x = dir * v_forward * maxspeed;
475 self.movement_y = dir * v_right * maxspeed;
480 // Handling of jump pads
481 if(self.jumppadcount)
483 // If got stuck on the jump pad try to reach the farthest visible waypoint
484 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
486 if(fabs(self.velocity_z)<50)
488 entity head, newgoal = world;
489 float distance, bestdistance = 0;
491 for (head = findchain(classname, "waypoint"); head; head = head.chain)
494 distance = vlen(head.origin - self.origin);
498 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), TRUE, world);
503 if(distance>bestdistance)
506 bestdistance = distance;
512 self.ignoregoal = self.goalcurrent;
513 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
514 navigation_clearroute();
515 navigation_routetogoal(newgoal, self.origin);
516 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
524 if(self.velocity_z>0)
526 float threshold, sxy;
527 vector velxy = self.velocity; velxy_z = 0;
529 threshold = maxspeed * 0.2;
532 dprint("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
533 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
538 // Don't chase players while using a jump pad
539 if(IS_PLAYER(self.goalcurrent) || IS_PLAYER(self.goalstack01))
543 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
544 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
546 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
548 if (!(self.flags & FL_ONGROUND))
550 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
551 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
552 if(self.items & IT_JETPACK)
554 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
555 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
557 if(self.velocity_z<0)
559 self.BUTTON_HOOK = TRUE;
563 self.BUTTON_HOOK = TRUE;
565 // If there is no goal try to move forward
567 if(self.goalcurrent==world)
570 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
572 vector xyvelocity = self.velocity; xyvelocity_z = 0;
573 float xyspeed = xyvelocity * dir;
575 if(xyspeed < (maxspeed / 2))
577 makevectors(self.v_angle_y * '0 1 0');
578 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
579 if(trace_fraction==1)
581 self.movement_x = dir * v_forward * maxspeed;
582 self.movement_y = dir * v_right * maxspeed;
584 havocbot_keyboard_movement(self.origin + dir * 100);
588 self.havocbot_blockhead = TRUE;
592 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
594 if(self.velocity_z < 0)
595 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
597 self.movement_x = maxspeed;
599 if(self.rocketjumptime)
601 if(time > self.rocketjumptime)
603 self.BUTTON_ATCK2 = TRUE;
604 self.rocketjumptime = 0;
609 self.switchweapon = WEP_ROCKET_LAUNCHER;
611 self.BUTTON_ATCK = TRUE;
612 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
618 // If there is no goal try to move forward
619 if(self.goalcurrent==world)
620 self.movement_x = maxspeed;
624 // If we are under water with no goals, swim up
626 if(self.goalcurrent==world)
629 if(self.waterlevel>WATERLEVEL_SWIMMING)
631 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
632 self.BUTTON_JUMP = TRUE;
634 self.BUTTON_JUMP = FALSE;
635 makevectors(self.v_angle_y * '0 1 0');
636 self.movement_x = dir * v_forward * maxspeed;
637 self.movement_y = dir * v_right * maxspeed;
638 self.movement_z = dir * v_up * maxspeed;
641 // if there is nowhere to go, exit
642 if (self.goalcurrent == world)
645 if (self.goalcurrent)
646 navigation_poptouchedgoals();
648 // if ran out of goals try to use an alternative goal or get a new strategy asap
649 if(self.goalcurrent == world)
651 self.bot_strategytime = 0;
656 if(autocvar_bot_debug_goalstack)
659 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
660 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
661 destorg = self.origin;
662 destorg_x = bound(m1_x, destorg_x, m2_x);
663 destorg_y = bound(m1_y, destorg_y, m2_y);
664 destorg_z = bound(m1_z, destorg_z, m2_z);
665 diff = destorg - self.origin;
667 dir = normalize(diff);
668 flatdir = diff;flatdir_z = 0;
669 flatdir = normalize(flatdir);
670 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
672 //if (self.bot_dodgevector_time < time)
674 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
675 // self.bot_dodgevector_jumpbutton = 1;
676 evadeobstacle = '0 0 0';
681 if(self.waterlevel>WATERLEVEL_SWIMMING)
684 self.aistatus |= AI_STATUS_OUT_WATER;
688 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && gco_z < self.origin_z) &&
689 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
690 self.BUTTON_JUMP = TRUE;
692 self.BUTTON_JUMP = FALSE;
694 dir = normalize(flatdir);
695 makevectors(self.v_angle_y * '0 1 0');
699 if(self.aistatus & AI_STATUS_OUT_WATER)
700 self.aistatus &= ~AI_STATUS_OUT_WATER;
702 // jump if going toward an obstacle that doesn't look like stairs we
703 // can walk up directly
704 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
705 if (trace_fraction < 1)
706 if (trace_plane_normal_z < 0.7)
709 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, FALSE, self);
710 if (trace_fraction < s + 0.01)
711 if (trace_plane_normal_z < 0.7)
714 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, FALSE, self);
715 if (trace_fraction > s)
716 self.BUTTON_JUMP = 1;
720 // avoiding dangers and obstacles
721 vector dst_ahead, dst_down;
722 makevectors(self.v_angle_y * '0 1 0');
723 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
724 dst_down = dst_ahead - '0 0 1500';
727 traceline(self.origin + self.view_ofs, dst_ahead, TRUE, world);
729 // Check head-banging against walls
730 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
732 self.BUTTON_JUMP = TRUE;
733 if(self.facingwalltime && time > self.facingwalltime)
735 self.ignoregoal = self.goalcurrent;
736 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
737 self.bot_strategytime = 0;
742 self.facingwalltime = time + 0.05;
747 self.facingwalltime = 0;
749 if(self.ignoregoal != world && time > self.ignoregoaltime)
751 self.ignoregoal = world;
752 self.ignoregoaltime = 0;
756 // Check for water/slime/lava and dangerous edges
757 // (only when the bot is on the ground or jumping intentionally)
758 self.aistatus &= ~AI_STATUS_DANGER_AHEAD;
760 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
761 if((self.flags & FL_ONGROUND) || (self.aistatus & AI_STATUS_RUNNING) || self.BUTTON_JUMP == TRUE)
764 traceline(dst_ahead , dst_down, TRUE, world);
765 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
766 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
767 if(trace_endpos_z < self.origin_z + self.mins_z)
769 s = pointcontents(trace_endpos + '0 0 1');
770 if (s != CONTENT_SOLID)
771 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
772 evadelava = normalize(self.velocity) * -1;
773 else if (s == CONTENT_SKY)
774 evadeobstacle = normalize(self.velocity) * -1;
775 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
776 self.goalcurrent.absmin, self.goalcurrent.absmax))
778 // if ain't a safe goal with "holes" (like the jumpad on soylent)
779 // and there is a trigger_hurt below
780 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
782 // Remove dangerous dynamic goals from stack
783 dprint("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
784 navigation_clearroute();
794 makevectors(self.v_angle_y * '0 1 0');
796 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
797 self.aistatus |= AI_STATUS_DANGER_AHEAD;
800 dodge = havocbot_dodge();
801 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
802 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
803 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), TRUE, world);
804 if(IS_PLAYER(trace_ent))
805 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
807 dir = normalize(dir + dodge + evadeobstacle + evadelava);
808 // self.bot_dodgevector = dir;
809 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
812 if(time < self.ladder_time)
814 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
816 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
821 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
826 //dir = self.bot_dodgevector;
827 //if (self.bot_dodgevector_jumpbutton)
828 // self.BUTTON_JUMP = 1;
829 self.movement_x = dir * v_forward * maxspeed;
830 self.movement_y = dir * v_right * maxspeed;
831 self.movement_z = dir * v_up * maxspeed;
833 // Emulate keyboard interface
835 havocbot_keyboard_movement(destorg);
838 // if(self.aistatus & AI_STATUS_ROAMING)
840 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
841 havocbot_bunnyhop(dir);
843 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
844 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
845 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);
848 void havocbot_chooseenemy()
850 entity head, best, head2;
851 float rating, bestrating, i, hf;
853 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
860 if (!bot_shouldattack(self.enemy))
862 // enemy died or something, find a new target
864 self.havocbot_chooseenemy_finished = time;
866 else if (self.havocbot_stickenemy)
868 // tracking last chosen enemy
869 // if enemy is visible
870 // and not really really far away
871 // and we're not severely injured
872 // then keep tracking for a half second into the future
873 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,FALSE,world);
874 if (trace_ent == self.enemy || trace_fraction == 1)
875 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
876 if (self.health > 30)
878 // remain tracking him for a shot while (case he went after a small corner or pilar
879 self.havocbot_chooseenemy_finished = time + 0.5;
882 // enemy isn't visible, or is far away, or we're injured severely
883 // so stop preferring this enemy
884 // (it will still take a half second until a new one is chosen)
885 self.havocbot_stickenemy = 0;
888 if (time < self.havocbot_chooseenemy_finished)
890 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
891 eye = self.origin + self.view_ofs;
893 bestrating = 100000000;
894 head = head2 = findchainfloat(bot_attack, TRUE);
897 hf = self.dphitcontentsmask;
899 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
901 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
907 v = (head.absmin + head.absmax) * 0.5;
908 rating = vlen(v - eye);
909 if (rating<autocvar_bot_ai_enemydetectionradius)
910 if (bestrating > rating)
911 if (bot_shouldattack(head))
913 traceline(eye, v, TRUE, self);
914 if (trace_ent == head || trace_fraction >= 1)
923 // I want to do a second scan if no enemy was found or I don't have weapons
924 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
925 if(best || self.weapons) // || self.weapon == WEP_RIFLE
930 // Set flags to see through transparent objects
931 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
937 self.dphitcontentsmask = hf;
940 self.havocbot_stickenemy = TRUE;
943 float havocbot_chooseweapon_checkreload(float new_weapon)
945 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
946 // so skip this for them, or they'll never get to reload their weapons at all.
947 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
951 // if this weapon is scheduled for reloading, don't switch to it during combat
952 if (self.weapon_load[new_weapon] < 0)
954 float i, other_weapon_available = FALSE;
955 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
957 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
958 if (weapon_action(i, WR_CHECKAMMO1) + weapon_action(i, WR_CHECKAMMO2))
959 other_weapon_available = TRUE;
961 if(other_weapon_available)
968 void havocbot_chooseweapon()
973 if(g_weaponarena_weapons == WEPSET_TUBA)
975 self.switchweapon = WEP_TUBA;
979 // TODO: clean this up by moving it to weapon code
980 if(self.enemy==world)
982 // If no weapon was chosen get the first available weapon
984 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
986 if(client_hasweapon(self, i, TRUE, FALSE))
988 self.switchweapon = i;
995 // Do not change weapon during the next second after a combo
996 i = time - self.lastcombotime;
1001 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1003 // Should it do a weapon combo?
1004 float af, ct, combo_time, combo;
1006 af = ATTACK_FINISHED(self);
1007 ct = autocvar_bot_ai_weapon_combo_threshold;
1009 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1010 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1011 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1015 if(autocvar_bot_ai_weapon_combo)
1016 if(self.weapon == self.lastfiredweapon)
1020 self.lastcombotime = time;
1023 distance *= pow(2, self.bot_rangepreference);
1025 // Custom weapon list based on distance to the enemy
1026 if(bot_custom_weapon){
1028 // Choose weapons for far distance
1029 if ( distance > bot_distance_far ) {
1030 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1031 w = bot_weapons_far[i];
1032 if ( client_hasweapon(self, w, TRUE, FALSE) )
1034 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1036 self.switchweapon = w;
1042 // Choose weapons for mid distance
1043 if ( distance > bot_distance_close) {
1044 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1045 w = bot_weapons_mid[i];
1046 if ( client_hasweapon(self, w, TRUE, FALSE) )
1048 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1050 self.switchweapon = w;
1056 // Choose weapons for close distance
1057 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1058 w = bot_weapons_close[i];
1059 if ( client_hasweapon(self, w, TRUE, FALSE) )
1061 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1063 self.switchweapon = w;
1072 vector selfvel, enemyvel;
1073 // if(self.flags & FL_INWATER)
1075 if (time < self.nextaim)
1077 self.nextaim = time + 0.1;
1078 selfvel = self.velocity;
1079 if (!self.waterlevel)
1083 enemyvel = self.enemy.velocity;
1084 if (!self.enemy.waterlevel)
1086 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1089 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1092 float havocbot_moveto_refresh_route()
1094 // Refresh path to goal if necessary
1096 wp = self.havocbot_personal_waypoint;
1097 navigation_goalrating_start();
1098 navigation_routerating(wp, 10000, 10000);
1099 navigation_goalrating_end();
1100 return self.navigation_hasgoals;
1103 float havocbot_moveto(vector pos)
1107 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1109 // Step 4: Move to waypoint
1110 if(self.havocbot_personal_waypoint==world)
1112 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1113 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1114 return CMD_STATUS_ERROR;
1117 if (!bot_strategytoken_taken)
1118 if(self.havocbot_personal_waypoint_searchtime<time)
1120 bot_strategytoken_taken = TRUE;
1121 if(havocbot_moveto_refresh_route())
1123 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1124 self.havocbot_personal_waypoint_searchtime = time + 10;
1125 self.havocbot_personal_waypoint_failcounter = 0;
1129 self.havocbot_personal_waypoint_failcounter += 1;
1130 self.havocbot_personal_waypoint_searchtime = time + 2;
1131 if(self.havocbot_personal_waypoint_failcounter >= 30)
1133 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1134 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1135 remove(self.havocbot_personal_waypoint);
1136 return CMD_STATUS_ERROR;
1139 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1143 if(autocvar_bot_debug_goalstack)
1147 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1149 bot_aimdir(dir, -1);
1152 havocbot_movetogoal();
1154 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1156 // Step 5: Waypoint reached
1157 dprint(self.netname, "'s personal waypoint reached\n");
1158 remove(self.havocbot_personal_waypoint);
1159 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1160 return CMD_STATUS_FINISHED;
1163 return CMD_STATUS_EXECUTING;
1166 // Step 2: Linking waypoint
1167 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1169 // Wait until it is linked
1170 if(!self.havocbot_personal_waypoint.wplinked)
1172 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1173 return CMD_STATUS_EXECUTING;
1176 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1177 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1178 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1180 // Step 3: Route to waypoint
1181 dprint(self.netname, " walking to its personal waypoint\n");
1183 return CMD_STATUS_EXECUTING;
1186 // Step 1: Spawning waypoint
1187 wp = waypoint_spawnpersonal(pos);
1190 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1191 return CMD_STATUS_ERROR;
1194 self.havocbot_personal_waypoint = wp;
1195 self.havocbot_personal_waypoint_failcounter = 0;
1196 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1198 // if pos is inside a teleport, then let's mark it as teleport waypoint
1200 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1202 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1204 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1205 self.lastteleporttime = 0;
1210 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1211 print("routing to a teleporter\n");
1213 print("routing to a non-teleporter\n");
1216 return CMD_STATUS_EXECUTING;
1219 float havocbot_resetgoal()
1221 navigation_clearroute();
1222 return CMD_STATUS_FINISHED;
1225 void havocbot_setupbot()
1227 self.bot_ai = havocbot_ai;
1228 self.cmd_moveto = havocbot_moveto;
1229 self.cmd_resetgoal = havocbot_resetgoal;
1231 havocbot_chooserole();
1234 vector havocbot_dodge()
1236 // LordHavoc: disabled because this is too expensive
1241 float danger, bestdanger, vl, d;
1244 // check for dangerous objects near bot or approaching bot
1245 head = findchainfloat(bot_dodge, TRUE);
1248 if (head.owner != self)
1250 vl = vlen(head.velocity);
1251 if (vl > autocvar_sv_maxspeed * 0.3)
1253 n = normalize(head.velocity);
1254 v = self.origin - head.origin;
1256 if (d > (0 - head.bot_dodgerating))
1257 if (d < (vl * 0.2 + head.bot_dodgerating))
1259 // calculate direction and distance from the flight path, by removing the forward axis
1260 v = v - (n * (v * n));
1261 danger = head.bot_dodgerating - vlen(v);
1262 if (bestdanger < danger)
1264 bestdanger = danger;
1265 // dodge to the side of the object
1266 dodge = normalize(v);
1272 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1273 if (bestdanger < danger)
1275 bestdanger = danger;
1276 dodge = normalize(self.origin - head.origin);