3 #include "role_onslaught.qc"
4 #include "role_keyhunt.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 (!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;
96 WEP_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(IS_PLAYER(self.bot_aimtarg))
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 (!(self.aistatus & AI_STATUS_ATTACKING))
152 // we are currently holding a weapon that's not fully loaded, reload it
153 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
154 if(self.clip_load < self.clip_size)
155 self.impulse = 20; // "press" the reload button, not sure if this is done right
157 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
158 // the code above executes next frame, starting the reloading then
159 if(skill >= 5) // bots can only look for unloaded weapons past this skill
160 if(self.clip_load >= 0) // only if we're not reloading a weapon already
162 for (int i = WEP_FIRST; i <= WEP_LAST; ++i)
164 entity e = get_weaponinfo(i);
165 if ((self.weapons & WepSet_FromWeapon(i)) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < e.reloading_ammo))
166 self.switchweapon = i;
172 void havocbot_keyboard_movement(vector destorg)
175 float blend, maxspeed;
178 sk = skill + self.bot_moveskill;
180 maxspeed = autocvar_sv_maxspeed;
182 if (time < self.havocbot_keyboardtime)
185 self.havocbot_keyboardtime =
187 self.havocbot_keyboardtime
188 + 0.05/max(1, sk+self.havocbot_keyboardskill)
189 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
191 keyboard = self.movement * (1.0 / maxspeed);
193 float trigger, trigger1;
194 blend = bound(0,sk*0.1,1);
195 trigger = autocvar_bot_ai_keyboard_threshold;
196 trigger1 = 0 - trigger;
198 // categorize forward movement
199 // at skill < 1.5 only forward
200 // at skill < 2.5 only individual directions
201 // at skill < 4.5 only individual directions, and forward diagonals
202 // at skill >= 4.5, all cases allowed
203 if (keyboard.x > trigger)
209 else if (keyboard.x < trigger1 && sk > 1.5)
224 if (keyboard.y > trigger)
226 else if (keyboard.y < trigger1)
231 if (keyboard.z > trigger)
233 else if (keyboard.z < trigger1)
238 self.havocbot_keyboard = keyboard * maxspeed;
239 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=true;
241 keyboard = self.havocbot_keyboard;
242 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
243 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
244 self.movement = self.movement + (keyboard - self.movement) * blend;
247 void havocbot_bunnyhop(vector dir)
249 float bunnyhopdistance;
254 // Don't jump when attacking
255 if(self.aistatus & AI_STATUS_ATTACKING)
258 if(IS_PLAYER(self.goalcurrent))
261 maxspeed = autocvar_sv_maxspeed;
263 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
265 self.aistatus &= ~AI_STATUS_RUNNING;
266 self.BUTTON_JUMP = false;
267 self.bot_canruntogoal = 0;
268 self.bot_timelastseengoal = 0;
272 if(self.waterlevel > WATERLEVEL_WETFEET)
274 self.aistatus &= ~AI_STATUS_RUNNING;
278 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
280 self.bot_canruntogoal = 0;
281 self.bot_timelastseengoal = 0;
284 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
285 bunnyhopdistance = vlen(self.origin - gco);
287 // Run only to visible goals
288 if(self.flags & FL_ONGROUND)
289 if(self.speed==maxspeed)
290 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
292 self.bot_lastseengoal = self.goalcurrent;
295 if(self.bot_timelastseengoal)
297 // for a period of time
298 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
301 checkdistance = true;
303 // don't run if it is too close
304 if(self.bot_canruntogoal==0)
306 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
307 self.bot_canruntogoal = 1;
309 self.bot_canruntogoal = -1;
312 if(self.bot_canruntogoal != 1)
315 if(self.aistatus & AI_STATUS_ROAMING)
316 if(self.goalcurrent.classname=="waypoint")
317 if (!(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
318 if(fabs(gco.z - self.origin.z) < self.maxs.z - self.mins.z)
319 if(self.goalstack01!=world)
321 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
322 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
323 while (deviation.y < -180) deviation.y = deviation.y + 360;
324 while (deviation.y > 180) deviation.y = deviation.y - 360;
326 if(fabs(deviation.y) < 20)
327 if(bunnyhopdistance < vlen(self.origin - gno))
328 if(fabs(gno.z - gco.z) < self.maxs.z - self.mins.z)
330 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
331 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
333 checkdistance = false;
340 self.aistatus &= ~AI_STATUS_RUNNING;
341 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
342 self.BUTTON_JUMP = true;
346 self.aistatus |= AI_STATUS_RUNNING;
347 self.BUTTON_JUMP = true;
353 self.bot_timelastseengoal = time;
358 self.bot_timelastseengoal = 0;
362 // Release jump button
363 if(!cvar("sv_pogostick"))
364 if((self.flags & FL_ONGROUND) == 0)
366 if(self.velocity.z < 0 || vlen(self.velocity)<maxspeed)
367 self.BUTTON_JUMP = false;
370 if(self.aistatus & AI_STATUS_RUNNING)
371 if(vlen(self.velocity)>maxspeed)
373 deviation = vectoangles(dir) - vectoangles(self.velocity);
374 while (deviation.y < -180) deviation.y = deviation.y + 360;
375 while (deviation.y > 180) deviation.y = deviation.y - 360;
377 if(fabs(deviation.y)>10)
381 self.movement_y = maxspeed * -1;
382 else if(deviation.y<10)
383 self.movement_y = maxspeed;
390 void havocbot_movetogoal()
398 vector evadeobstacle;
405 //if (self.goalentity)
406 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
407 self.movement = '0 0 0';
408 maxspeed = autocvar_sv_maxspeed;
410 // Jetpack navigation
412 if(self.navigation_jetpack_goal)
413 if(self.goalcurrent==self.navigation_jetpack_goal)
416 if(autocvar_bot_debug_goalstack)
419 te_wizspike(self.navigation_jetpack_point);
423 if (!(self.aistatus & AI_STATUS_JETPACK_FLYING))
425 // Brake almost completely so it can get a good direction
426 if(vlen(self.velocity)>10)
428 self.aistatus |= AI_STATUS_JETPACK_FLYING;
431 makevectors(self.v_angle.y * '0 1 0');
432 dir = normalize(self.navigation_jetpack_point - self.origin);
435 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
437 // Calculate brake distance in xy
441 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
443 v = vlen(self.velocity - self.velocity.z * '0 0 1');
444 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
445 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
446 if(d < db || d < 500)
449 if(fabs(self.velocity.x)>maxspeed*0.3)
451 self.movement_x = dir * v_forward * -maxspeed;
454 // Switch to normal mode
455 self.navigation_jetpack_goal = world;
456 self.aistatus &= ~AI_STATUS_JETPACK_LANDING;
457 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
461 else if(checkpvs(self.origin,self.goalcurrent))
463 // If I can see the goal switch to landing code
464 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
465 self.aistatus |= AI_STATUS_JETPACK_LANDING;
470 self.BUTTON_HOOK = true;
471 if(self.navigation_jetpack_point.z - PL_MAX_z + PL_MIN_z < self.origin.z)
473 self.movement_x = dir * v_forward * maxspeed;
474 self.movement_y = dir * v_right * maxspeed;
479 // Handling of jump pads
480 if(self.jumppadcount)
482 // If got stuck on the jump pad try to reach the farthest visible waypoint
483 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
485 if(fabs(self.velocity.z)<50)
487 entity head, newgoal = world;
488 float distance, bestdistance = 0;
490 for (head = findchain(classname, "waypoint"); head; head = head.chain)
493 distance = vlen(head.origin - self.origin);
497 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), true, world);
502 if(distance>bestdistance)
505 bestdistance = distance;
511 self.ignoregoal = self.goalcurrent;
512 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
513 navigation_clearroute();
514 navigation_routetogoal(newgoal, self.origin);
515 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
523 if(self.velocity.z>0)
525 float threshold, sxy;
526 vector velxy = self.velocity; velxy_z = 0;
528 threshold = maxspeed * 0.2;
531 dprint("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
532 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
537 // Don't chase players while using a jump pad
538 if(IS_PLAYER(self.goalcurrent) || IS_PLAYER(self.goalstack01))
542 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
543 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
545 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
547 if (!(self.flags & FL_ONGROUND))
549 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
550 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
551 if(self.items & IT_JETPACK)
553 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
554 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
556 if(self.velocity.z<0)
558 self.BUTTON_HOOK = true;
562 self.BUTTON_HOOK = true;
564 // If there is no goal try to move forward
566 if(self.goalcurrent==world)
569 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
571 vector xyvelocity = self.velocity; xyvelocity_z = 0;
572 float xyspeed = xyvelocity * dir;
574 if(xyspeed < (maxspeed / 2))
576 makevectors(self.v_angle.y * '0 1 0');
577 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
578 if(trace_fraction==1)
580 self.movement_x = dir * v_forward * maxspeed;
581 self.movement_y = dir * v_right * maxspeed;
583 havocbot_keyboard_movement(self.origin + dir * 100);
587 self.havocbot_blockhead = true;
591 else if(self.health>WEP_CVAR(devastator, damage)*0.5)
593 if(self.velocity.z < 0)
594 if(client_hasweapon(self, WEP_DEVASTATOR, true, false))
596 self.movement_x = maxspeed;
598 if(self.rocketjumptime)
600 if(time > self.rocketjumptime)
602 self.BUTTON_ATCK2 = true;
603 self.rocketjumptime = 0;
608 self.switchweapon = WEP_DEVASTATOR;
610 self.BUTTON_ATCK = true;
611 self.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
617 // If there is no goal try to move forward
618 if(self.goalcurrent==world)
619 self.movement_x = maxspeed;
623 // If we are under water with no goals, swim up
625 if(self.goalcurrent==world)
628 if(self.waterlevel>WATERLEVEL_SWIMMING)
630 else if(self.velocity.z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
631 self.BUTTON_JUMP = true;
633 self.BUTTON_JUMP = false;
634 makevectors(self.v_angle.y * '0 1 0');
635 self.movement_x = dir * v_forward * maxspeed;
636 self.movement_y = dir * v_right * maxspeed;
637 self.movement_z = dir * v_up * maxspeed;
640 // if there is nowhere to go, exit
641 if (self.goalcurrent == world)
644 if (self.goalcurrent)
645 navigation_poptouchedgoals();
647 // if ran out of goals try to use an alternative goal or get a new strategy asap
648 if(self.goalcurrent == world)
650 self.bot_strategytime = 0;
655 if(autocvar_bot_debug_goalstack)
658 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
659 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
660 destorg = self.origin;
661 destorg.x = bound(m1_x, destorg.x, m2_x);
662 destorg.y = bound(m1_y, destorg.y, m2_y);
663 destorg.z = bound(m1_z, destorg.z, m2_z);
664 diff = destorg - self.origin;
666 dir = normalize(diff);
667 flatdir = diff;flatdir.z = 0;
668 flatdir = normalize(flatdir);
669 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
671 //if (self.bot_dodgevector_time < time)
673 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
674 // self.bot_dodgevector_jumpbutton = 1;
675 evadeobstacle = '0 0 0';
680 if(self.waterlevel>WATERLEVEL_SWIMMING)
683 self.aistatus |= AI_STATUS_OUT_WATER;
687 if(self.velocity.z >= 0 && !(self.watertype == CONTENT_WATER && gco.z < self.origin.z) &&
688 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
689 self.BUTTON_JUMP = true;
691 self.BUTTON_JUMP = false;
693 dir = normalize(flatdir);
694 makevectors(self.v_angle.y * '0 1 0');
698 if(self.aistatus & AI_STATUS_OUT_WATER)
699 self.aistatus &= ~AI_STATUS_OUT_WATER;
701 // jump if going toward an obstacle that doesn't look like stairs we
702 // can walk up directly
703 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, false, self);
704 if (trace_fraction < 1)
705 if (trace_plane_normal.z < 0.7)
708 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, false, self);
709 if (trace_fraction < s + 0.01)
710 if (trace_plane_normal.z < 0.7)
713 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, false, self);
714 if (trace_fraction > s)
715 self.BUTTON_JUMP = 1;
719 // avoiding dangers and obstacles
720 vector dst_ahead, dst_down;
721 makevectors(self.v_angle.y * '0 1 0');
722 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
723 dst_down = dst_ahead - '0 0 1500';
726 traceline(self.origin + self.view_ofs, dst_ahead, true, world);
728 // Check head-banging against walls
729 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
731 self.BUTTON_JUMP = true;
732 if(self.facingwalltime && time > self.facingwalltime)
734 self.ignoregoal = self.goalcurrent;
735 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
736 self.bot_strategytime = 0;
741 self.facingwalltime = time + 0.05;
746 self.facingwalltime = 0;
748 if(self.ignoregoal != world && time > self.ignoregoaltime)
750 self.ignoregoal = world;
751 self.ignoregoaltime = 0;
755 // Check for water/slime/lava and dangerous edges
756 // (only when the bot is on the ground or jumping intentionally)
757 self.aistatus &= ~AI_STATUS_DANGER_AHEAD;
759 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
760 if((self.flags & FL_ONGROUND) || (self.aistatus & AI_STATUS_RUNNING) || self.BUTTON_JUMP == true)
763 traceline(dst_ahead , dst_down, true, world);
764 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
765 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
766 if(trace_endpos.z < self.origin.z + self.mins.z)
768 s = pointcontents(trace_endpos + '0 0 1');
769 if (s != CONTENT_SOLID)
770 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
771 evadelava = normalize(self.velocity) * -1;
772 else if (s == CONTENT_SKY)
773 evadeobstacle = normalize(self.velocity) * -1;
774 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
775 self.goalcurrent.absmin, self.goalcurrent.absmax))
777 // if ain't a safe goal with "holes" (like the jumpad on soylent)
778 // and there is a trigger_hurt below
779 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
781 // Remove dangerous dynamic goals from stack
782 dprint("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
783 navigation_clearroute();
793 makevectors(self.v_angle.y * '0 1 0');
795 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
796 self.aistatus |= AI_STATUS_DANGER_AHEAD;
799 dodge = havocbot_dodge();
800 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
801 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
802 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), true, world);
803 if(IS_PLAYER(trace_ent))
804 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
806 dir = normalize(dir + dodge + evadeobstacle + evadelava);
807 // self.bot_dodgevector = dir;
808 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
811 if(time < self.ladder_time)
813 if(self.goalcurrent.origin.z + self.goalcurrent.mins.z > self.origin.z + self.mins.z)
815 if(self.origin.z + self.mins.z < self.ladder_entity.origin.z + self.ladder_entity.maxs.z)
820 if(self.origin.z + self.mins.z > self.ladder_entity.origin.z + self.ladder_entity.mins.z)
825 //dir = self.bot_dodgevector;
826 //if (self.bot_dodgevector_jumpbutton)
827 // self.BUTTON_JUMP = 1;
828 self.movement_x = dir * v_forward * maxspeed;
829 self.movement_y = dir * v_right * maxspeed;
830 self.movement_z = dir * v_up * maxspeed;
832 // Emulate keyboard interface
834 havocbot_keyboard_movement(destorg);
837 // if(self.aistatus & AI_STATUS_ROAMING)
839 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
840 havocbot_bunnyhop(dir);
842 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
843 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=true;
844 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);
847 void havocbot_chooseenemy()
849 entity head, best, head2;
850 float rating, bestrating, i, hf;
852 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
859 if (!bot_shouldattack(self.enemy))
861 // enemy died or something, find a new target
863 self.havocbot_chooseenemy_finished = time;
865 else if (self.havocbot_stickenemy)
867 // tracking last chosen enemy
868 // if enemy is visible
869 // and not really really far away
870 // and we're not severely injured
871 // then keep tracking for a half second into the future
872 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,false,world);
873 if (trace_ent == self.enemy || trace_fraction == 1)
874 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
875 if (self.health > 30)
877 // remain tracking him for a shot while (case he went after a small corner or pilar
878 self.havocbot_chooseenemy_finished = time + 0.5;
881 // enemy isn't visible, or is far away, or we're injured severely
882 // so stop preferring this enemy
883 // (it will still take a half second until a new one is chosen)
884 self.havocbot_stickenemy = 0;
887 if (time < self.havocbot_chooseenemy_finished)
889 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
890 eye = self.origin + self.view_ofs;
892 bestrating = 100000000;
893 head = head2 = findchainfloat(bot_attack, true);
896 hf = self.dphitcontentsmask;
898 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
900 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
906 v = (head.absmin + head.absmax) * 0.5;
907 rating = vlen(v - eye);
908 if (rating<autocvar_bot_ai_enemydetectionradius)
909 if (bestrating > rating)
910 if (bot_shouldattack(head))
912 traceline(eye, v, true, self);
913 if (trace_ent == head || trace_fraction >= 1)
922 // I want to do a second scan if no enemy was found or I don't have weapons
923 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
924 if(best || self.weapons) // || self.weapon == WEP_RIFLE
929 // Set flags to see through transparent objects
930 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
936 self.dphitcontentsmask = hf;
939 self.havocbot_stickenemy = true;
942 float havocbot_chooseweapon_checkreload(int new_weapon)
944 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
945 // so skip this for them, or they'll never get to reload their weapons at all.
946 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
950 // if this weapon is scheduled for reloading, don't switch to it during combat
951 if (self.weapon_load[new_weapon] < 0)
953 float i, other_weapon_available = false;
954 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
956 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
957 if (WEP_ACTION(i, WR_CHECKAMMO1) + WEP_ACTION(i, WR_CHECKAMMO2))
958 other_weapon_available = true;
960 if(other_weapon_available)
967 void havocbot_chooseweapon()
972 if(g_weaponarena_weapons == WEPSET_TUBA)
974 self.switchweapon = WEP_TUBA;
978 // TODO: clean this up by moving it to weapon code
979 if(self.enemy==world)
981 // If no weapon was chosen get the first available weapon
983 for(i = WEP_FIRST; i <= WEP_LAST; ++i) if(i != WEP_BLASTER)
985 if(client_hasweapon(self, i, true, false))
987 self.switchweapon = i;
994 // Do not change weapon during the next second after a combo
995 float f = time - self.lastcombotime;
1000 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1002 // Should it do a weapon combo?
1003 float af, ct, combo_time, combo;
1005 af = ATTACK_FINISHED(self);
1006 ct = autocvar_bot_ai_weapon_combo_threshold;
1008 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1009 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1010 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1014 if(autocvar_bot_ai_weapon_combo)
1015 if(self.weapon == self.lastfiredweapon)
1019 self.lastcombotime = time;
1022 distance *= pow(2, self.bot_rangepreference);
1024 // Custom weapon list based on distance to the enemy
1025 if(bot_custom_weapon){
1027 // Choose weapons for far distance
1028 if ( distance > bot_distance_far ) {
1029 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1030 w = bot_weapons_far[i];
1031 if ( client_hasweapon(self, w, true, false) )
1033 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1035 self.switchweapon = w;
1041 // Choose weapons for mid distance
1042 if ( distance > bot_distance_close) {
1043 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1044 w = bot_weapons_mid[i];
1045 if ( client_hasweapon(self, w, true, false) )
1047 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1049 self.switchweapon = w;
1055 // Choose weapons for close distance
1056 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1057 w = bot_weapons_close[i];
1058 if ( client_hasweapon(self, w, true, false) )
1060 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1062 self.switchweapon = w;
1071 vector selfvel, enemyvel;
1072 // if(self.flags & FL_INWATER)
1074 if (time < self.nextaim)
1076 self.nextaim = time + 0.1;
1077 selfvel = self.velocity;
1078 if (!self.waterlevel)
1082 enemyvel = self.enemy.velocity;
1083 if (!self.enemy.waterlevel)
1085 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1088 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1091 float havocbot_moveto_refresh_route()
1093 // Refresh path to goal if necessary
1095 wp = self.havocbot_personal_waypoint;
1096 navigation_goalrating_start();
1097 navigation_routerating(wp, 10000, 10000);
1098 navigation_goalrating_end();
1099 return self.navigation_hasgoals;
1102 float havocbot_moveto(vector pos)
1106 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1108 // Step 4: Move to waypoint
1109 if(self.havocbot_personal_waypoint==world)
1111 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1112 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1113 return CMD_STATUS_ERROR;
1116 if (!bot_strategytoken_taken)
1117 if(self.havocbot_personal_waypoint_searchtime<time)
1119 bot_strategytoken_taken = true;
1120 if(havocbot_moveto_refresh_route())
1122 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1123 self.havocbot_personal_waypoint_searchtime = time + 10;
1124 self.havocbot_personal_waypoint_failcounter = 0;
1128 self.havocbot_personal_waypoint_failcounter += 1;
1129 self.havocbot_personal_waypoint_searchtime = time + 2;
1130 if(self.havocbot_personal_waypoint_failcounter >= 30)
1132 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1133 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1134 remove(self.havocbot_personal_waypoint);
1135 return CMD_STATUS_ERROR;
1138 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1142 if(autocvar_bot_debug_goalstack)
1146 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1148 bot_aimdir(dir, -1);
1151 havocbot_movetogoal();
1153 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1155 // Step 5: Waypoint reached
1156 dprint(self.netname, "'s personal waypoint reached\n");
1157 remove(self.havocbot_personal_waypoint);
1158 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1159 return CMD_STATUS_FINISHED;
1162 return CMD_STATUS_EXECUTING;
1165 // Step 2: Linking waypoint
1166 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1168 // Wait until it is linked
1169 if(!self.havocbot_personal_waypoint.wplinked)
1171 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1172 return CMD_STATUS_EXECUTING;
1175 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1176 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1177 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1179 // Step 3: Route to waypoint
1180 dprint(self.netname, " walking to its personal waypoint\n");
1182 return CMD_STATUS_EXECUTING;
1185 // Step 1: Spawning waypoint
1186 wp = waypoint_spawnpersonal(pos);
1189 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1190 return CMD_STATUS_ERROR;
1193 self.havocbot_personal_waypoint = wp;
1194 self.havocbot_personal_waypoint_failcounter = 0;
1195 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1197 // if pos is inside a teleport, then let's mark it as teleport waypoint
1199 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1201 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1203 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1204 self.lastteleporttime = 0;
1209 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1210 print("routing to a teleporter\n");
1212 print("routing to a non-teleporter\n");
1215 return CMD_STATUS_EXECUTING;
1218 float havocbot_resetgoal()
1220 navigation_clearroute();
1221 return CMD_STATUS_FINISHED;
1224 void havocbot_setupbot()
1226 self.bot_ai = havocbot_ai;
1227 self.cmd_moveto = havocbot_moveto;
1228 self.cmd_resetgoal = havocbot_resetgoal;
1230 havocbot_chooserole();
1233 vector havocbot_dodge()
1235 // LordHavoc: disabled because this is too expensive
1240 float danger, bestdanger, vl, d;
1243 // check for dangerous objects near bot or approaching bot
1244 head = findchainfloat(bot_dodge, true);
1247 if (head.owner != self)
1249 vl = vlen(head.velocity);
1250 if (vl > autocvar_sv_maxspeed * 0.3)
1252 n = normalize(head.velocity);
1253 v = self.origin - head.origin;
1255 if (d > (0 - head.bot_dodgerating))
1256 if (d < (vl * 0.2 + head.bot_dodgerating))
1258 // calculate direction and distance from the flight path, by removing the forward axis
1259 v = v - (n * (v * n));
1260 danger = head.bot_dodgerating - vlen(v);
1261 if (bestdanger < danger)
1263 bestdanger = danger;
1264 // dodge to the side of the object
1265 dodge = normalize(v);
1271 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1272 if (bestdanger < danger)
1274 bestdanger = danger;
1275 dodge = normalize(self.origin - head.origin);