3 #include "role_onslaught.qc"
4 #include "role_keyhunt.qc"
5 #include "role_freezetag.qc"
6 #include "role_keepaway.qc"
7 #include "role_assault.qc"
15 if(bot_execute_commands())
18 if (bot_strategytoken == self)
19 if (!bot_strategytoken_taken)
21 if(self.havocbot_blockhead)
23 self.havocbot_blockhead = FALSE;
27 if not(self.jumppadcount)
31 // TODO: tracewalk() should take care of this job (better path finding under water)
32 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
33 if(self.deadflag != DEAD_NO)
34 if(self.goalcurrent==world)
35 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
37 // Look for the closest waypoint out of water
39 float bestdistance, distance;
43 for (head = findchain(classname, "waypoint"); head; head = head.chain)
45 distance = vlen(head.origin - self.origin);
49 if(head.origin_z < self.origin_z)
52 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
55 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
58 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
63 if(distance<bestdistance)
66 bestdistance = distance;
72 // te_wizspike(newgoal.origin);
73 navigation_pushroute(newgoal);
77 // token has been used this frame
78 bot_strategytoken_taken = TRUE;
81 if(self.deadflag != DEAD_NO)
84 havocbot_chooseenemy();
85 if (self.bot_chooseweapontime < time )
87 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
88 havocbot_chooseweapon();
94 self.aistatus |= AI_STATUS_ATTACKING;
95 self.aistatus &~= AI_STATUS_ROAMING;
97 if(!WEPSET_EMPTY_E(self))
99 weapon_action(self.weapon, WR_AIM);
100 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
102 self.BUTTON_ATCK = FALSE;
103 self.BUTTON_ATCK2 = FALSE;
107 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
108 self.lastfiredweapon = self.weapon;
113 if(self.bot_aimtarg.classname=="player")
114 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
117 else if (self.goalcurrent)
119 self.aistatus |= AI_STATUS_ROAMING;
120 self.aistatus &~= AI_STATUS_ATTACKING;
122 vector now,v,next;//,heading;
123 float aimdistance,skillblend,distanceblend,blend;
124 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
125 aimdistance = vlen(now);
126 //heading = self.velocity;
127 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
129 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
130 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
132 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
134 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
135 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
136 blend = skillblend * (1-distanceblend);
137 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
138 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
139 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
140 v = now + blend * (next - now);
141 //dprint(etos(self), " ");
142 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
143 //v = now * (distanceblend) + next * (1-distanceblend);
144 if (self.waterlevel < WATERLEVEL_SWIMMING)
146 //dprint("walk at:", vtos(v), "\n");
147 //te_lightning2(world, self.origin, self.goalcurrent.origin);
150 havocbot_movetogoal();
152 // if the bot is not attacking, consider reloading weapons
153 if not(self.aistatus & AI_STATUS_ATTACKING)
158 // we are currently holding a weapon that's not fully loaded, reload it
159 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
160 if(self.clip_load < self.clip_size)
161 self.impulse = 20; // "press" the reload button, not sure if this is done right
163 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
164 // the code above executes next frame, starting the reloading then
165 if(skill >= 5) // bots can only look for unloaded weapons past this skill
166 if(self.clip_load >= 0) // only if we're not reloading a weapon already
168 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
170 e = get_weaponinfo(i);
171 if (WEPSET_CONTAINS_EW(self, i) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < cvar(strcat("g_balance_", e.netname, "_reload_ammo"))))
172 self.switchweapon = i;
178 void havocbot_keyboard_movement(vector destorg)
181 float blend, maxspeed;
184 sk = skill + self.bot_moveskill;
186 maxspeed = autocvar_sv_maxspeed;
188 if (time < self.havocbot_keyboardtime)
191 self.havocbot_keyboardtime =
193 self.havocbot_keyboardtime
194 + 0.05/max(1, sk+self.havocbot_keyboardskill)
195 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
197 keyboard = self.movement * (1.0 / maxspeed);
199 float trigger, trigger1;
200 blend = bound(0,sk*0.1,1);
201 trigger = autocvar_bot_ai_keyboard_threshold;
202 trigger1 = 0 - trigger;
204 // categorize forward movement
205 // at skill < 1.5 only forward
206 // at skill < 2.5 only individual directions
207 // at skill < 4.5 only individual directions, and forward diagonals
208 // at skill >= 4.5, all cases allowed
209 if (keyboard_x > trigger)
215 else if (keyboard_x < trigger1 && sk > 1.5)
230 if (keyboard_y > trigger)
232 else if (keyboard_y < trigger1)
237 if (keyboard_z > trigger)
239 else if (keyboard_z < trigger1)
244 self.havocbot_keyboard = keyboard * maxspeed;
245 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
247 keyboard = self.havocbot_keyboard;
248 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
249 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
250 self.movement = self.movement + (keyboard - self.movement) * blend;
253 void havocbot_bunnyhop(vector dir)
255 float bunnyhopdistance;
260 if(autocvar_g_midair)
263 // Don't jump when attacking
264 if(self.aistatus & AI_STATUS_ATTACKING)
267 if(self.goalcurrent.classname == "player")
270 maxspeed = autocvar_sv_maxspeed;
272 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
274 self.aistatus &~= AI_STATUS_RUNNING;
275 self.BUTTON_JUMP = FALSE;
276 self.bot_canruntogoal = 0;
277 self.bot_timelastseengoal = 0;
281 if(self.waterlevel > WATERLEVEL_WETFEET)
283 self.aistatus &~= AI_STATUS_RUNNING;
287 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
289 self.bot_canruntogoal = 0;
290 self.bot_timelastseengoal = 0;
293 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
294 bunnyhopdistance = vlen(self.origin - gco);
296 // Run only to visible goals
297 if(self.flags & FL_ONGROUND)
298 if(self.speed==maxspeed)
299 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
301 self.bot_lastseengoal = self.goalcurrent;
304 if(self.bot_timelastseengoal)
306 // for a period of time
307 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
310 checkdistance = TRUE;
312 // don't run if it is too close
313 if(self.bot_canruntogoal==0)
315 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
316 self.bot_canruntogoal = 1;
318 self.bot_canruntogoal = -1;
321 if(self.bot_canruntogoal != 1)
324 if(self.aistatus & AI_STATUS_ROAMING)
325 if(self.goalcurrent.classname=="waypoint")
326 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
327 if(fabs(gco_z - self.origin_z) < self.maxs_z - self.mins_z)
328 if(self.goalstack01!=world)
330 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
331 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
332 while (deviation_y < -180) deviation_y = deviation_y + 360;
333 while (deviation_y > 180) deviation_y = deviation_y - 360;
335 if(fabs(deviation_y) < 20)
336 if(bunnyhopdistance < vlen(self.origin - gno))
337 if(fabs(gno_z - gco_z) < self.maxs_z - self.mins_z)
339 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
340 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
342 checkdistance = FALSE;
349 self.aistatus &~= AI_STATUS_RUNNING;
350 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
351 self.BUTTON_JUMP = TRUE;
355 self.aistatus |= AI_STATUS_RUNNING;
356 self.BUTTON_JUMP = TRUE;
362 self.bot_timelastseengoal = time;
367 self.bot_timelastseengoal = 0;
371 // Release jump button
372 if(!cvar("sv_pogostick"))
373 if(self.flags & FL_ONGROUND == 0)
375 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
376 self.BUTTON_JUMP = FALSE;
379 if(self.aistatus & AI_STATUS_RUNNING)
380 if(vlen(self.velocity)>maxspeed)
382 deviation = vectoangles(dir) - vectoangles(self.velocity);
383 while (deviation_y < -180) deviation_y = deviation_y + 360;
384 while (deviation_y > 180) deviation_y = deviation_y - 360;
386 if(fabs(deviation_y)>10)
390 self.movement_y = maxspeed * -1;
391 else if(deviation_y<10)
392 self.movement_y = maxspeed;
399 void havocbot_movetogoal()
407 vector evadeobstacle;
414 //if (self.goalentity)
415 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
416 self.movement = '0 0 0';
417 maxspeed = autocvar_sv_maxspeed;
419 // Jetpack navigation
421 if(self.navigation_jetpack_goal)
422 if(self.goalcurrent==self.navigation_jetpack_goal)
425 if(autocvar_bot_debug_goalstack)
428 te_wizspike(self.navigation_jetpack_point);
432 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
434 // Brake almost completely so it can get a good direction
435 if(vlen(self.velocity)>10)
437 self.aistatus |= AI_STATUS_JETPACK_FLYING;
440 makevectors(self.v_angle_y * '0 1 0');
441 dir = normalize(self.navigation_jetpack_point - self.origin);
444 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
446 // Calculate brake distance in xy
450 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy_z = 0;
452 v = vlen(self.velocity - self.velocity_z * '0 0 1');
453 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
454 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
455 if(d < db || d < 500)
458 if(fabs(self.velocity_x)>maxspeed*0.3)
460 self.movement_x = dir * v_forward * -maxspeed;
463 // Switch to normal mode
464 self.navigation_jetpack_goal = world;
465 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
466 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
470 else if(checkpvs(self.origin,self.goalcurrent))
472 // If I can see the goal switch to landing code
473 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
474 self.aistatus |= AI_STATUS_JETPACK_LANDING;
479 self.BUTTON_HOOK = TRUE;
480 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
482 self.movement_x = dir * v_forward * maxspeed;
483 self.movement_y = dir * v_right * maxspeed;
488 // Handling of jump pads
489 if(self.jumppadcount)
491 // If got stuck on the jump pad try to reach the farthest visible waypoint
492 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
494 if(fabs(self.velocity_z)<50)
496 entity head, newgoal;
497 float distance, bestdistance;
499 for (head = findchain(classname, "waypoint"); head; head = head.chain)
502 distance = vlen(head.origin - self.origin);
506 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), TRUE, world);
511 if(distance>bestdistance)
514 bestdistance = distance;
520 self.ignoregoal = self.goalcurrent;
521 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
522 navigation_clearroute();
523 navigation_routetogoal(newgoal, self.origin);
524 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
532 if(self.velocity_z>0)
534 float threshold, sxy;
535 vector velxy = self.velocity; velxy_z = 0;
537 threshold = maxspeed * 0.2;
540 dprint("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
541 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
546 // Don't chase players while using a jump pad
547 if(self.goalcurrent.classname=="player" || self.goalstack01.classname=="player")
551 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
552 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
554 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
556 if not(self.flags & FL_ONGROUND)
558 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
559 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
560 if(self.items & IT_JETPACK)
562 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
563 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
565 if(self.velocity_z<0)
567 self.BUTTON_HOOK = TRUE;
571 self.BUTTON_HOOK = TRUE;
573 // If there is no goal try to move forward
575 if(self.goalcurrent==world)
578 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
580 vector xyvelocity = self.velocity; xyvelocity_z = 0;
581 float xyspeed = xyvelocity * dir;
583 if(xyspeed < (maxspeed / 2))
585 makevectors(self.v_angle_y * '0 1 0');
586 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
587 if(trace_fraction==1)
589 self.movement_x = dir * v_forward * maxspeed;
590 self.movement_y = dir * v_right * maxspeed;
592 havocbot_keyboard_movement(self.origin + dir * 100);
596 self.havocbot_blockhead = TRUE;
600 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
602 if(self.velocity_z < 0)
603 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
605 self.movement_x = maxspeed;
607 if(self.rocketjumptime)
609 if(time > self.rocketjumptime)
611 self.BUTTON_ATCK2 = TRUE;
612 self.rocketjumptime = 0;
617 self.switchweapon = WEP_ROCKET_LAUNCHER;
619 self.BUTTON_ATCK = TRUE;
620 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
626 // If there is no goal try to move forward
627 if(self.goalcurrent==world)
628 self.movement_x = maxspeed;
632 // If we are under water with no goals, swim up
634 if(self.goalcurrent==world)
637 if(self.waterlevel>WATERLEVEL_SWIMMING)
639 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
640 self.BUTTON_JUMP = TRUE;
642 self.BUTTON_JUMP = FALSE;
643 makevectors(self.v_angle_y * '0 1 0');
644 self.movement_x = dir * v_forward * maxspeed;
645 self.movement_y = dir * v_right * maxspeed;
646 self.movement_z = dir * v_up * maxspeed;
649 // if there is nowhere to go, exit
650 if (self.goalcurrent == world)
653 if (self.goalcurrent)
654 navigation_poptouchedgoals();
656 // if ran out of goals try to use an alternative goal or get a new strategy asap
657 if(self.goalcurrent == world)
659 self.bot_strategytime = 0;
664 if(autocvar_bot_debug_goalstack)
667 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
668 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
669 destorg = self.origin;
670 destorg_x = bound(m1_x, destorg_x, m2_x);
671 destorg_y = bound(m1_y, destorg_y, m2_y);
672 destorg_z = bound(m1_z, destorg_z, m2_z);
673 diff = destorg - self.origin;
675 dir = normalize(diff);
676 flatdir = diff;flatdir_z = 0;
677 flatdir = normalize(flatdir);
678 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
680 //if (self.bot_dodgevector_time < time)
682 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
683 // self.bot_dodgevector_jumpbutton = 1;
684 evadeobstacle = '0 0 0';
689 if(self.waterlevel>WATERLEVEL_SWIMMING)
692 self.aistatus |= AI_STATUS_OUT_WATER;
696 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && gco_z < self.origin_z) &&
697 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
698 self.BUTTON_JUMP = TRUE;
700 self.BUTTON_JUMP = FALSE;
702 dir = normalize(flatdir);
703 makevectors(self.v_angle_y * '0 1 0');
707 if(self.aistatus & AI_STATUS_OUT_WATER)
708 self.aistatus &~= AI_STATUS_OUT_WATER;
710 // jump if going toward an obstacle that doesn't look like stairs we
711 // can walk up directly
712 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
713 if (trace_fraction < 1)
714 if (trace_plane_normal_z < 0.7)
717 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, FALSE, self);
718 if (trace_fraction < s + 0.01)
719 if (trace_plane_normal_z < 0.7)
722 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, FALSE, self);
723 if (trace_fraction > s)
724 self.BUTTON_JUMP = 1;
728 // avoiding dangers and obstacles
729 vector dst_ahead, dst_down;
730 makevectors(self.v_angle_y * '0 1 0');
731 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
732 dst_down = dst_ahead + '0 0 -1500';
735 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
737 // Check head-banging against walls
738 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
740 self.BUTTON_JUMP = TRUE;
741 if(self.facingwalltime && time > self.facingwalltime)
743 self.ignoregoal = self.goalcurrent;
744 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
745 self.bot_strategytime = 0;
750 self.facingwalltime = time + 0.05;
755 self.facingwalltime = 0;
757 if(self.ignoregoal != world && time > self.ignoregoaltime)
759 self.ignoregoal = world;
760 self.ignoregoaltime = 0;
764 // Check for water/slime/lava and dangerous edges
765 // (only when the bot is on the ground or jumping intentionally)
766 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
768 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
769 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
772 traceline(dst_ahead , dst_down, TRUE, world);
773 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
774 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
775 if(trace_endpos_z < self.origin_z + self.mins_z)
777 s = pointcontents(trace_endpos + '0 0 1');
778 if (s != CONTENT_SOLID)
779 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
780 evadelava = normalize(self.velocity) * -1;
781 else if (s == CONTENT_SKY)
782 evadeobstacle = normalize(self.velocity) * -1;
783 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
784 self.goalcurrent.absmin, self.goalcurrent.absmax))
786 // if ain't a safe goal with "holes" (like the jumpad on soylent)
787 // and there is a trigger_hurt below
788 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
790 // Remove dangerous dynamic goals from stack
791 dprint("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
792 navigation_clearroute();
802 makevectors(self.v_angle_y * '0 1 0');
804 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
805 self.aistatus |= AI_STATUS_DANGER_AHEAD;
808 dodge = havocbot_dodge();
809 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
810 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
811 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), TRUE, world);
812 if(trace_ent.classname == "player")
813 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
815 dir = normalize(dir + dodge + evadeobstacle + evadelava);
816 // self.bot_dodgevector = dir;
817 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
820 if(time < self.ladder_time)
822 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
824 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
829 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
834 //dir = self.bot_dodgevector;
835 //if (self.bot_dodgevector_jumpbutton)
836 // self.BUTTON_JUMP = 1;
837 self.movement_x = dir * v_forward * maxspeed;
838 self.movement_y = dir * v_right * maxspeed;
839 self.movement_z = dir * v_up * maxspeed;
841 // Emulate keyboard interface
843 havocbot_keyboard_movement(destorg);
846 // if(self.aistatus & AI_STATUS_ROAMING)
848 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
849 havocbot_bunnyhop(dir);
851 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
852 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
853 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);
856 void havocbot_chooseenemy()
858 entity head, best, head2;
859 float rating, bestrating, i, hf;
861 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
868 if (!bot_shouldattack(self.enemy))
870 // enemy died or something, find a new target
872 self.havocbot_chooseenemy_finished = time;
874 else if (self.havocbot_stickenemy)
876 // tracking last chosen enemy
877 // if enemy is visible
878 // and not really really far away
879 // and we're not severely injured
880 // then keep tracking for a half second into the future
881 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,FALSE,world);
882 if (trace_ent == self.enemy || trace_fraction == 1)
883 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
884 if (self.health > 30)
886 // remain tracking him for a shot while (case he went after a small corner or pilar
887 self.havocbot_chooseenemy_finished = time + 0.5;
890 // enemy isn't visible, or is far away, or we're injured severely
891 // so stop preferring this enemy
892 // (it will still take a half second until a new one is chosen)
893 self.havocbot_stickenemy = 0;
896 if (time < self.havocbot_chooseenemy_finished)
898 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
899 eye = self.origin + self.view_ofs;
901 bestrating = 100000000;
902 head = head2 = findchainfloat(bot_attack, TRUE);
905 hf = self.dphitcontentsmask;
907 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
909 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
915 v = (head.absmin + head.absmax) * 0.5;
916 rating = vlen(v - eye);
917 if (rating<autocvar_bot_ai_enemydetectionradius)
918 if (bestrating > rating)
919 if (bot_shouldattack(head))
921 traceline(eye, v, TRUE, self);
922 if (trace_ent == head || trace_fraction >= 1)
931 // I want to do a second scan if no enemy was found or I don't have weapons
932 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
933 if(best || !WEPSET_EMPTY_E(self)) // || self.weapon == WEP_RIFLE
938 // Set flags to see through transparent objects
939 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
946 self.dphitcontentsmask = hf;
949 self.havocbot_stickenemy = TRUE;
952 float havocbot_chooseweapon_checkreload(float new_weapon)
954 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
955 // so skip this for them, or they'll never get to reload their weapons at all.
956 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
960 // if this weapon is scheduled for reloading, don't switch to it during combat
961 if (self.weapon_load[new_weapon] < 0)
963 float i, other_weapon_available;
964 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
966 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
967 if (weapon_action(i, WR_CHECKAMMO1) + weapon_action(i, WR_CHECKAMMO2))
968 other_weapon_available = TRUE;
970 if(other_weapon_available)
977 void havocbot_chooseweapon()
982 if(WEPSET_EQ_AW(g_weaponarena_weapons, WEP_TUBA))
984 self.switchweapon = WEP_TUBA;
988 // TODO: clean this up by moving it to weapon code
989 if(self.enemy==world)
991 // If no weapon was chosen get the first available weapon
993 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
995 if(client_hasweapon(self, i, TRUE, FALSE))
997 self.switchweapon = i;
1004 // Do not change weapon during the next second after a combo
1005 i = time - self.lastcombotime;
1010 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1012 // Should it do a weapon combo?
1013 float af, ct, combo_time, combo;
1015 af = ATTACK_FINISHED(self);
1016 ct = autocvar_bot_ai_weapon_combo_threshold;
1018 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1019 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1020 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1024 if(autocvar_bot_ai_weapon_combo)
1025 if(self.weapon == self.lastfiredweapon)
1029 self.lastcombotime = time;
1032 distance *= pow(2, self.bot_rangepreference);
1034 // Custom weapon list based on distance to the enemy
1035 if(bot_custom_weapon){
1037 // Choose weapons for far distance
1038 if ( distance > bot_distance_far ) {
1039 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1040 w = bot_weapons_far[i];
1041 if ( client_hasweapon(self, w, TRUE, FALSE) )
1043 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1045 self.switchweapon = w;
1051 // Choose weapons for mid distance
1052 if ( distance > bot_distance_close) {
1053 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1054 w = bot_weapons_mid[i];
1055 if ( client_hasweapon(self, w, TRUE, FALSE) )
1057 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1059 self.switchweapon = w;
1065 // Choose weapons for close distance
1066 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1067 w = bot_weapons_close[i];
1068 if ( client_hasweapon(self, w, TRUE, FALSE) )
1070 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1072 self.switchweapon = w;
1081 vector selfvel, enemyvel;
1082 // if(self.flags & FL_INWATER)
1084 if (time < self.nextaim)
1086 self.nextaim = time + 0.1;
1087 selfvel = self.velocity;
1088 if (!self.waterlevel)
1092 enemyvel = self.enemy.velocity;
1093 if (!self.enemy.waterlevel)
1095 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1098 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1101 float havocbot_moveto_refresh_route()
1103 // Refresh path to goal if necessary
1105 wp = self.havocbot_personal_waypoint;
1106 navigation_goalrating_start();
1107 navigation_routerating(wp, 10000, 10000);
1108 navigation_goalrating_end();
1109 return self.navigation_hasgoals;
1112 float havocbot_moveto(vector pos)
1116 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1118 // Step 4: Move to waypoint
1119 if(self.havocbot_personal_waypoint==world)
1121 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1122 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1123 return CMD_STATUS_ERROR;
1126 if (!bot_strategytoken_taken)
1127 if(self.havocbot_personal_waypoint_searchtime<time)
1129 bot_strategytoken_taken = TRUE;
1130 if(havocbot_moveto_refresh_route())
1132 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1133 self.havocbot_personal_waypoint_searchtime = time + 10;
1134 self.havocbot_personal_waypoint_failcounter = 0;
1138 self.havocbot_personal_waypoint_failcounter += 1;
1139 self.havocbot_personal_waypoint_searchtime = time + 2;
1140 if(self.havocbot_personal_waypoint_failcounter >= 30)
1142 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1143 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1144 remove(self.havocbot_personal_waypoint);
1145 return CMD_STATUS_ERROR;
1148 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1152 if(autocvar_bot_debug_goalstack)
1156 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1158 bot_aimdir(dir, -1);
1161 havocbot_movetogoal();
1163 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1165 // Step 5: Waypoint reached
1166 dprint(self.netname, "'s personal waypoint reached\n");
1167 remove(self.havocbot_personal_waypoint);
1168 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1169 return CMD_STATUS_FINISHED;
1172 return CMD_STATUS_EXECUTING;
1175 // Step 2: Linking waypoint
1176 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1178 // Wait until it is linked
1179 if(!self.havocbot_personal_waypoint.wplinked)
1181 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1182 return CMD_STATUS_EXECUTING;
1185 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1186 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1187 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1189 // Step 3: Route to waypoint
1190 dprint(self.netname, " walking to its personal waypoint\n");
1192 return CMD_STATUS_EXECUTING;
1195 // Step 1: Spawning waypoint
1196 wp = waypoint_spawnpersonal(pos);
1199 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1200 return CMD_STATUS_ERROR;
1203 self.havocbot_personal_waypoint = wp;
1204 self.havocbot_personal_waypoint_failcounter = 0;
1205 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1207 // if pos is inside a teleport, then let's mark it as teleport waypoint
1209 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1211 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1213 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1214 self.lastteleporttime = 0;
1219 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1220 print("routing to a teleporter\n");
1222 print("routing to a non-teleporter\n");
1225 return CMD_STATUS_EXECUTING;
1228 float havocbot_resetgoal()
1230 navigation_clearroute();
1231 return CMD_STATUS_FINISHED;
1234 void havocbot_setupbot()
1236 self.bot_ai = havocbot_ai;
1237 self.cmd_moveto = havocbot_moveto;
1238 self.cmd_resetgoal = havocbot_resetgoal;
1240 havocbot_chooserole();
1243 vector havocbot_dodge()
1245 // LordHavoc: disabled because this is too expensive
1250 float danger, bestdanger, vl, d;
1253 // check for dangerous objects near bot or approaching bot
1254 head = findchainfloat(bot_dodge, TRUE);
1257 if (head.owner != self)
1259 vl = vlen(head.velocity);
1260 if (vl > autocvar_sv_maxspeed * 0.3)
1262 n = normalize(head.velocity);
1263 v = self.origin - head.origin;
1265 if (d > (0 - head.bot_dodgerating))
1266 if (d < (vl * 0.2 + head.bot_dodgerating))
1268 // calculate direction and distance from the flight path, by removing the forward axis
1269 v = v - (n * (v * n));
1270 danger = head.bot_dodgerating - vlen(v);
1271 if (bestdanger < danger)
1273 bestdanger = danger;
1274 // dodge to the side of the object
1275 dodge = normalize(v);
1281 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1282 if (bestdanger < danger)
1284 bestdanger = danger;
1285 dodge = normalize(self.origin - head.origin);