2 #include "role_onslaught.qc"
3 #include "role_keyhunt.qc"
11 if(bot_execute_commands())
14 if (bot_strategytoken == self)
15 if (!bot_strategytoken_taken)
17 if(self.havocbot_blockhead)
19 self.havocbot_blockhead = false;
23 if (!self.jumppadcount)
27 // TODO: tracewalk() should take care of this job (better path finding under water)
28 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
29 if(self.deadflag != DEAD_NO)
30 if(self.goalcurrent==world)
31 if(self.waterlevel==WATERLEVEL_SWIMMING || (self.aistatus & AI_STATUS_OUT_WATER))
33 // Look for the closest waypoint out of water
35 float bestdistance, distance;
39 for (head = findchain(classname, "waypoint"); head; head = head.chain)
41 distance = vlen(head.origin - self.origin);
45 if(head.origin.z < self.origin.z)
48 if(head.origin.z - self.origin.z - self.view_ofs.z > 100)
51 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
54 traceline(self.origin + self.view_ofs , head.origin, true, head);
59 if(distance<bestdistance)
62 bestdistance = distance;
68 // te_wizspike(newgoal.origin);
69 navigation_pushroute(newgoal);
73 // token has been used this frame
74 bot_strategytoken_taken = true;
77 if(self.deadflag != DEAD_NO)
80 havocbot_chooseenemy();
81 if (self.bot_chooseweapontime < time )
83 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
84 havocbot_chooseweapon();
90 self.aistatus |= AI_STATUS_ATTACKING;
91 self.aistatus &= ~AI_STATUS_ROAMING;
95 WEP_ACTION(self.weapon, WR_AIM);
96 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
98 self.BUTTON_ATCK = false;
99 self.BUTTON_ATCK2 = false;
103 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
104 self.lastfiredweapon = self.weapon;
109 if(IS_PLAYER(self.bot_aimtarg))
110 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
113 else if (self.goalcurrent)
115 self.aistatus |= AI_STATUS_ROAMING;
116 self.aistatus &= ~AI_STATUS_ATTACKING;
118 vector now,v,next;//,heading;
119 float aimdistance,skillblend,distanceblend,blend;
120 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
121 aimdistance = vlen(now);
122 //heading = self.velocity;
123 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
125 self.goalstack01 != self && self.goalstack01 != world && ((self.aistatus & AI_STATUS_RUNNING) == 0) &&
126 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
128 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
130 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
131 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
132 blend = skillblend * (1-distanceblend);
133 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
134 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
135 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
136 v = now + blend * (next - now);
137 //dprint(etos(self), " ");
138 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
139 //v = now * (distanceblend) + next * (1-distanceblend);
140 if (self.waterlevel < WATERLEVEL_SWIMMING)
142 //dprint("walk at:", vtos(v), "\n");
143 //te_lightning2(world, self.origin, self.goalcurrent.origin);
146 havocbot_movetogoal();
148 // if the bot is not attacking, consider reloading weapons
149 if (!(self.aistatus & AI_STATUS_ATTACKING))
151 // we are currently holding a weapon that's not fully loaded, reload it
152 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
153 if(self.clip_load < self.clip_size)
154 self.impulse = 20; // "press" the reload button, not sure if this is done right
156 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
157 // the code above executes next frame, starting the reloading then
158 if(skill >= 5) // bots can only look for unloaded weapons past this skill
159 if(self.clip_load >= 0) // only if we're not reloading a weapon already
161 for (int i = WEP_FIRST; i <= WEP_LAST; ++i)
163 entity e = get_weaponinfo(i);
164 if ((self.weapons & WepSet_FromWeapon(i)) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < e.reloading_ammo))
165 self.switchweapon = i;
171 void havocbot_keyboard_movement(vector destorg)
174 float blend, maxspeed;
177 sk = skill + self.bot_moveskill;
179 maxspeed = autocvar_sv_maxspeed;
181 if (time < self.havocbot_keyboardtime)
184 self.havocbot_keyboardtime =
186 self.havocbot_keyboardtime
187 + 0.05/max(1, sk+self.havocbot_keyboardskill)
188 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
190 keyboard = self.movement * (1.0 / maxspeed);
192 float trigger, trigger1;
193 blend = bound(0,sk*0.1,1);
194 trigger = autocvar_bot_ai_keyboard_threshold;
195 trigger1 = 0 - trigger;
197 // categorize forward movement
198 // at skill < 1.5 only forward
199 // at skill < 2.5 only individual directions
200 // at skill < 4.5 only individual directions, and forward diagonals
201 // at skill >= 4.5, all cases allowed
202 if (keyboard.x > trigger)
208 else if (keyboard.x < trigger1 && sk > 1.5)
223 if (keyboard.y > trigger)
225 else if (keyboard.y < trigger1)
230 if (keyboard.z > trigger)
232 else if (keyboard.z < trigger1)
237 self.havocbot_keyboard = keyboard * maxspeed;
238 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=true;
240 keyboard = self.havocbot_keyboard;
241 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
242 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
243 self.movement = self.movement + (keyboard - self.movement) * blend;
246 void havocbot_bunnyhop(vector dir)
248 float bunnyhopdistance;
253 // Don't jump when attacking
254 if(self.aistatus & AI_STATUS_ATTACKING)
257 if(IS_PLAYER(self.goalcurrent))
260 maxspeed = autocvar_sv_maxspeed;
262 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
264 self.aistatus &= ~AI_STATUS_RUNNING;
265 self.BUTTON_JUMP = false;
266 self.bot_canruntogoal = 0;
267 self.bot_timelastseengoal = 0;
271 if(self.waterlevel > WATERLEVEL_WETFEET)
273 self.aistatus &= ~AI_STATUS_RUNNING;
277 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
279 self.bot_canruntogoal = 0;
280 self.bot_timelastseengoal = 0;
283 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
284 bunnyhopdistance = vlen(self.origin - gco);
286 // Run only to visible goals
287 if(self.flags & FL_ONGROUND)
288 if(self.speed==maxspeed)
289 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
291 self.bot_lastseengoal = self.goalcurrent;
294 if(self.bot_timelastseengoal)
296 // for a period of time
297 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
300 checkdistance = true;
302 // don't run if it is too close
303 if(self.bot_canruntogoal==0)
305 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
306 self.bot_canruntogoal = 1;
308 self.bot_canruntogoal = -1;
311 if(self.bot_canruntogoal != 1)
314 if(self.aistatus & AI_STATUS_ROAMING)
315 if(self.goalcurrent.classname=="waypoint")
316 if (!(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
317 if(fabs(gco.z - self.origin.z) < self.maxs.z - self.mins.z)
318 if(self.goalstack01!=world)
320 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
321 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
322 while (deviation.y < -180) deviation.y = deviation.y + 360;
323 while (deviation.y > 180) deviation.y = deviation.y - 360;
325 if(fabs(deviation.y) < 20)
326 if(bunnyhopdistance < vlen(self.origin - gno))
327 if(fabs(gno.z - gco.z) < self.maxs.z - self.mins.z)
329 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
330 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
332 checkdistance = false;
339 self.aistatus &= ~AI_STATUS_RUNNING;
340 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
341 self.BUTTON_JUMP = true;
345 self.aistatus |= AI_STATUS_RUNNING;
346 self.BUTTON_JUMP = true;
352 self.bot_timelastseengoal = time;
357 self.bot_timelastseengoal = 0;
361 // Release jump button
362 if(!cvar("sv_pogostick"))
363 if((self.flags & FL_ONGROUND) == 0)
365 if(self.velocity.z < 0 || vlen(self.velocity)<maxspeed)
366 self.BUTTON_JUMP = false;
369 if(self.aistatus & AI_STATUS_RUNNING)
370 if(vlen(self.velocity)>maxspeed)
372 deviation = vectoangles(dir) - vectoangles(self.velocity);
373 while (deviation.y < -180) deviation.y = deviation.y + 360;
374 while (deviation.y > 180) deviation.y = deviation.y - 360;
376 if(fabs(deviation.y)>10)
380 self.movement_y = maxspeed * -1;
381 else if(deviation.y<10)
382 self.movement_y = maxspeed;
389 void havocbot_movetogoal()
397 vector evadeobstacle;
404 //if (self.goalentity)
405 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
406 self.movement = '0 0 0';
407 maxspeed = autocvar_sv_maxspeed;
409 // Jetpack navigation
411 if(self.navigation_jetpack_goal)
412 if(self.goalcurrent==self.navigation_jetpack_goal)
415 if(autocvar_bot_debug_goalstack)
418 te_wizspike(self.navigation_jetpack_point);
422 if (!(self.aistatus & AI_STATUS_JETPACK_FLYING))
424 // Brake almost completely so it can get a good direction
425 if(vlen(self.velocity)>10)
427 self.aistatus |= AI_STATUS_JETPACK_FLYING;
430 makevectors(self.v_angle.y * '0 1 0');
431 dir = normalize(self.navigation_jetpack_point - self.origin);
434 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
436 // Calculate brake distance in xy
440 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
442 v = vlen(self.velocity - self.velocity.z * '0 0 1');
443 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
444 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
445 if(d < db || d < 500)
448 if(fabs(self.velocity.x)>maxspeed*0.3)
450 self.movement_x = dir * v_forward * -maxspeed;
453 // Switch to normal mode
454 self.navigation_jetpack_goal = world;
455 self.aistatus &= ~AI_STATUS_JETPACK_LANDING;
456 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
460 else if(checkpvs(self.origin,self.goalcurrent))
462 // If I can see the goal switch to landing code
463 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
464 self.aistatus |= AI_STATUS_JETPACK_LANDING;
469 self.BUTTON_HOOK = true;
470 if(self.navigation_jetpack_point.z - PL_MAX_z + PL_MIN_z < self.origin.z)
472 self.movement_x = dir * v_forward * maxspeed;
473 self.movement_y = dir * v_right * maxspeed;
478 // Handling of jump pads
479 if(self.jumppadcount)
481 // If got stuck on the jump pad try to reach the farthest visible waypoint
482 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
484 if(fabs(self.velocity.z)<50)
486 entity head, newgoal = world;
487 float distance, bestdistance = 0;
489 for (head = findchain(classname, "waypoint"); head; head = head.chain)
492 distance = vlen(head.origin - self.origin);
496 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), true, world);
501 if(distance>bestdistance)
504 bestdistance = distance;
510 self.ignoregoal = self.goalcurrent;
511 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
512 navigation_clearroute();
513 navigation_routetogoal(newgoal, self.origin);
514 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
522 if(self.velocity.z>0)
524 float threshold, sxy;
525 vector velxy = self.velocity; velxy_z = 0;
527 threshold = maxspeed * 0.2;
530 dprint("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
531 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
536 // Don't chase players while using a jump pad
537 if(IS_PLAYER(self.goalcurrent) || IS_PLAYER(self.goalstack01))
541 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
542 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
544 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
546 if (!(self.flags & FL_ONGROUND))
548 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
549 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
550 if(self.items & IT_JETPACK)
552 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
553 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
555 if(self.velocity.z<0)
557 self.BUTTON_HOOK = true;
561 self.BUTTON_HOOK = true;
563 // If there is no goal try to move forward
565 if(self.goalcurrent==world)
568 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
570 vector xyvelocity = self.velocity; xyvelocity_z = 0;
571 float xyspeed = xyvelocity * dir;
573 if(xyspeed < (maxspeed / 2))
575 makevectors(self.v_angle.y * '0 1 0');
576 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
577 if(trace_fraction==1)
579 self.movement_x = dir * v_forward * maxspeed;
580 self.movement_y = dir * v_right * maxspeed;
582 havocbot_keyboard_movement(self.origin + dir * 100);
586 self.havocbot_blockhead = true;
590 else if(self.health>WEP_CVAR(devastator, damage)*0.5)
592 if(self.velocity.z < 0)
593 if(client_hasweapon(self, WEP_DEVASTATOR, true, false))
595 self.movement_x = maxspeed;
597 if(self.rocketjumptime)
599 if(time > self.rocketjumptime)
601 self.BUTTON_ATCK2 = true;
602 self.rocketjumptime = 0;
607 self.switchweapon = WEP_DEVASTATOR;
609 self.BUTTON_ATCK = true;
610 self.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
616 // If there is no goal try to move forward
617 if(self.goalcurrent==world)
618 self.movement_x = maxspeed;
622 // If we are under water with no goals, swim up
624 if(self.goalcurrent==world)
627 if(self.waterlevel>WATERLEVEL_SWIMMING)
629 else if(self.velocity.z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
630 self.BUTTON_JUMP = true;
632 self.BUTTON_JUMP = false;
633 makevectors(self.v_angle.y * '0 1 0');
634 self.movement_x = dir * v_forward * maxspeed;
635 self.movement_y = dir * v_right * maxspeed;
636 self.movement_z = dir * v_up * maxspeed;
639 // if there is nowhere to go, exit
640 if (self.goalcurrent == world)
643 if (self.goalcurrent)
644 navigation_poptouchedgoals();
646 // if ran out of goals try to use an alternative goal or get a new strategy asap
647 if(self.goalcurrent == world)
649 self.bot_strategytime = 0;
654 if(autocvar_bot_debug_goalstack)
657 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
658 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
659 destorg = self.origin;
660 destorg.x = bound(m1_x, destorg.x, m2_x);
661 destorg.y = bound(m1_y, destorg.y, m2_y);
662 destorg.z = bound(m1_z, destorg.z, m2_z);
663 diff = destorg - self.origin;
665 dir = normalize(diff);
666 flatdir = diff;flatdir.z = 0;
667 flatdir = normalize(flatdir);
668 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
670 //if (self.bot_dodgevector_time < time)
672 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
673 // self.bot_dodgevector_jumpbutton = 1;
674 evadeobstacle = '0 0 0';
679 if(self.waterlevel>WATERLEVEL_SWIMMING)
682 self.aistatus |= AI_STATUS_OUT_WATER;
686 if(self.velocity.z >= 0 && !(self.watertype == CONTENT_WATER && gco.z < self.origin.z) &&
687 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
688 self.BUTTON_JUMP = true;
690 self.BUTTON_JUMP = false;
692 dir = normalize(flatdir);
693 makevectors(self.v_angle.y * '0 1 0');
697 if(self.aistatus & AI_STATUS_OUT_WATER)
698 self.aistatus &= ~AI_STATUS_OUT_WATER;
700 // jump if going toward an obstacle that doesn't look like stairs we
701 // can walk up directly
702 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, false, self);
703 if (trace_fraction < 1)
704 if (trace_plane_normal.z < 0.7)
707 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, false, self);
708 if (trace_fraction < s + 0.01)
709 if (trace_plane_normal.z < 0.7)
712 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, false, self);
713 if (trace_fraction > s)
714 self.BUTTON_JUMP = 1;
718 // avoiding dangers and obstacles
719 vector dst_ahead, dst_down;
720 makevectors(self.v_angle.y * '0 1 0');
721 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
722 dst_down = dst_ahead - '0 0 1500';
725 traceline(self.origin + self.view_ofs, dst_ahead, true, world);
727 // Check head-banging against walls
728 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
730 self.BUTTON_JUMP = true;
731 if(self.facingwalltime && time > self.facingwalltime)
733 self.ignoregoal = self.goalcurrent;
734 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
735 self.bot_strategytime = 0;
740 self.facingwalltime = time + 0.05;
745 self.facingwalltime = 0;
747 if(self.ignoregoal != world && time > self.ignoregoaltime)
749 self.ignoregoal = world;
750 self.ignoregoaltime = 0;
754 // Check for water/slime/lava and dangerous edges
755 // (only when the bot is on the ground or jumping intentionally)
756 self.aistatus &= ~AI_STATUS_DANGER_AHEAD;
758 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
759 if((self.flags & FL_ONGROUND) || (self.aistatus & AI_STATUS_RUNNING) || self.BUTTON_JUMP == true)
762 traceline(dst_ahead , dst_down, true, world);
763 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
764 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
765 if(trace_endpos.z < self.origin.z + self.mins.z)
767 s = pointcontents(trace_endpos + '0 0 1');
768 if (s != CONTENT_SOLID)
769 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
770 evadelava = normalize(self.velocity) * -1;
771 else if (s == CONTENT_SKY)
772 evadeobstacle = normalize(self.velocity) * -1;
773 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
774 self.goalcurrent.absmin, self.goalcurrent.absmax))
776 // if ain't a safe goal with "holes" (like the jumpad on soylent)
777 // and there is a trigger_hurt below
778 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
780 // Remove dangerous dynamic goals from stack
781 dprint("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
782 navigation_clearroute();
792 makevectors(self.v_angle.y * '0 1 0');
794 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
795 self.aistatus |= AI_STATUS_DANGER_AHEAD;
798 dodge = havocbot_dodge();
799 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
800 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
801 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), true, world);
802 if(IS_PLAYER(trace_ent))
803 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
805 dir = normalize(dir + dodge + evadeobstacle + evadelava);
806 // self.bot_dodgevector = dir;
807 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
810 if(time < self.ladder_time)
812 if(self.goalcurrent.origin.z + self.goalcurrent.mins.z > self.origin.z + self.mins.z)
814 if(self.origin.z + self.mins.z < self.ladder_entity.origin.z + self.ladder_entity.maxs.z)
819 if(self.origin.z + self.mins.z > self.ladder_entity.origin.z + self.ladder_entity.mins.z)
824 //dir = self.bot_dodgevector;
825 //if (self.bot_dodgevector_jumpbutton)
826 // self.BUTTON_JUMP = 1;
827 self.movement_x = dir * v_forward * maxspeed;
828 self.movement_y = dir * v_right * maxspeed;
829 self.movement_z = dir * v_up * maxspeed;
831 // Emulate keyboard interface
833 havocbot_keyboard_movement(destorg);
836 // if(self.aistatus & AI_STATUS_ROAMING)
838 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
839 havocbot_bunnyhop(dir);
841 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
842 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=true;
843 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);
846 void havocbot_chooseenemy()
848 entity head, best, head2;
849 float rating, bestrating, i, hf;
851 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
858 if (!bot_shouldattack(self.enemy))
860 // enemy died or something, find a new target
862 self.havocbot_chooseenemy_finished = time;
864 else if (self.havocbot_stickenemy)
866 // tracking last chosen enemy
867 // if enemy is visible
868 // and not really really far away
869 // and we're not severely injured
870 // then keep tracking for a half second into the future
871 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,false,world);
872 if (trace_ent == self.enemy || trace_fraction == 1)
873 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
874 if (self.health > 30)
876 // remain tracking him for a shot while (case he went after a small corner or pilar
877 self.havocbot_chooseenemy_finished = time + 0.5;
880 // enemy isn't visible, or is far away, or we're injured severely
881 // so stop preferring this enemy
882 // (it will still take a half second until a new one is chosen)
883 self.havocbot_stickenemy = 0;
886 if (time < self.havocbot_chooseenemy_finished)
888 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
889 eye = self.origin + self.view_ofs;
891 bestrating = 100000000;
892 head = head2 = findchainfloat(bot_attack, true);
895 hf = self.dphitcontentsmask;
897 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
899 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
905 v = (head.absmin + head.absmax) * 0.5;
906 rating = vlen(v - eye);
907 if (rating<autocvar_bot_ai_enemydetectionradius)
908 if (bestrating > rating)
909 if (bot_shouldattack(head))
911 traceline(eye, v, true, self);
912 if (trace_ent == head || trace_fraction >= 1)
921 // I want to do a second scan if no enemy was found or I don't have weapons
922 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
923 if(best || self.weapons) // || self.weapon == WEP_RIFLE
928 // Set flags to see through transparent objects
929 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
935 self.dphitcontentsmask = hf;
938 self.havocbot_stickenemy = true;
941 float havocbot_chooseweapon_checkreload(int new_weapon)
943 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
944 // so skip this for them, or they'll never get to reload their weapons at all.
945 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
949 // if this weapon is scheduled for reloading, don't switch to it during combat
950 if (self.weapon_load[new_weapon] < 0)
952 float i, other_weapon_available = false;
953 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
955 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
956 if (WEP_ACTION(i, WR_CHECKAMMO1) + WEP_ACTION(i, WR_CHECKAMMO2))
957 other_weapon_available = true;
959 if(other_weapon_available)
966 void havocbot_chooseweapon()
971 if(g_weaponarena_weapons == WEPSET_TUBA)
973 self.switchweapon = WEP_TUBA;
977 // TODO: clean this up by moving it to weapon code
978 if(self.enemy==world)
980 // If no weapon was chosen get the first available weapon
982 for(i = WEP_FIRST; i <= WEP_LAST; ++i) if(i != WEP_BLASTER)
984 if(client_hasweapon(self, i, true, false))
986 self.switchweapon = i;
993 // Do not change weapon during the next second after a combo
994 float f = time - self.lastcombotime;
999 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1001 // Should it do a weapon combo?
1002 float af, ct, combo_time, combo;
1004 af = ATTACK_FINISHED(self);
1005 ct = autocvar_bot_ai_weapon_combo_threshold;
1007 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1008 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1009 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1013 if(autocvar_bot_ai_weapon_combo)
1014 if(self.weapon == self.lastfiredweapon)
1018 self.lastcombotime = time;
1021 distance *= pow(2, self.bot_rangepreference);
1023 // Custom weapon list based on distance to the enemy
1024 if(bot_custom_weapon){
1026 // Choose weapons for far distance
1027 if ( distance > bot_distance_far ) {
1028 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1029 w = bot_weapons_far[i];
1030 if ( client_hasweapon(self, w, true, false) )
1032 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1034 self.switchweapon = w;
1040 // Choose weapons for mid distance
1041 if ( distance > bot_distance_close) {
1042 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1043 w = bot_weapons_mid[i];
1044 if ( client_hasweapon(self, w, true, false) )
1046 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1048 self.switchweapon = w;
1054 // Choose weapons for close distance
1055 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1056 w = bot_weapons_close[i];
1057 if ( client_hasweapon(self, w, true, false) )
1059 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1061 self.switchweapon = w;
1070 vector selfvel, enemyvel;
1071 // if(self.flags & FL_INWATER)
1073 if (time < self.nextaim)
1075 self.nextaim = time + 0.1;
1076 selfvel = self.velocity;
1077 if (!self.waterlevel)
1081 enemyvel = self.enemy.velocity;
1082 if (!self.enemy.waterlevel)
1084 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1087 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1090 float havocbot_moveto_refresh_route()
1092 // Refresh path to goal if necessary
1094 wp = self.havocbot_personal_waypoint;
1095 navigation_goalrating_start();
1096 navigation_routerating(wp, 10000, 10000);
1097 navigation_goalrating_end();
1098 return self.navigation_hasgoals;
1101 float havocbot_moveto(vector pos)
1105 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1107 // Step 4: Move to waypoint
1108 if(self.havocbot_personal_waypoint==world)
1110 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1111 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1112 return CMD_STATUS_ERROR;
1115 if (!bot_strategytoken_taken)
1116 if(self.havocbot_personal_waypoint_searchtime<time)
1118 bot_strategytoken_taken = true;
1119 if(havocbot_moveto_refresh_route())
1121 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1122 self.havocbot_personal_waypoint_searchtime = time + 10;
1123 self.havocbot_personal_waypoint_failcounter = 0;
1127 self.havocbot_personal_waypoint_failcounter += 1;
1128 self.havocbot_personal_waypoint_searchtime = time + 2;
1129 if(self.havocbot_personal_waypoint_failcounter >= 30)
1131 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1132 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1133 remove(self.havocbot_personal_waypoint);
1134 return CMD_STATUS_ERROR;
1137 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1141 if(autocvar_bot_debug_goalstack)
1145 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1147 bot_aimdir(dir, -1);
1150 havocbot_movetogoal();
1152 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1154 // Step 5: Waypoint reached
1155 dprint(self.netname, "'s personal waypoint reached\n");
1156 remove(self.havocbot_personal_waypoint);
1157 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1158 return CMD_STATUS_FINISHED;
1161 return CMD_STATUS_EXECUTING;
1164 // Step 2: Linking waypoint
1165 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1167 // Wait until it is linked
1168 if(!self.havocbot_personal_waypoint.wplinked)
1170 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1171 return CMD_STATUS_EXECUTING;
1174 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1175 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1176 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1178 // Step 3: Route to waypoint
1179 dprint(self.netname, " walking to its personal waypoint\n");
1181 return CMD_STATUS_EXECUTING;
1184 // Step 1: Spawning waypoint
1185 wp = waypoint_spawnpersonal(pos);
1188 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1189 return CMD_STATUS_ERROR;
1192 self.havocbot_personal_waypoint = wp;
1193 self.havocbot_personal_waypoint_failcounter = 0;
1194 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1196 // if pos is inside a teleport, then let's mark it as teleport waypoint
1198 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1200 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1202 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1203 self.lastteleporttime = 0;
1208 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1209 print("routing to a teleporter\n");
1211 print("routing to a non-teleporter\n");
1214 return CMD_STATUS_EXECUTING;
1217 float havocbot_resetgoal()
1219 navigation_clearroute();
1220 return CMD_STATUS_FINISHED;
1223 void havocbot_setupbot()
1225 self.bot_ai = havocbot_ai;
1226 self.cmd_moveto = havocbot_moveto;
1227 self.cmd_resetgoal = havocbot_resetgoal;
1229 havocbot_chooserole();
1232 vector havocbot_dodge()
1234 // LordHavoc: disabled because this is too expensive
1239 float danger, bestdanger, vl, d;
1242 // check for dangerous objects near bot or approaching bot
1243 head = findchainfloat(bot_dodge, true);
1246 if (head.owner != self)
1248 vl = vlen(head.velocity);
1249 if (vl > autocvar_sv_maxspeed * 0.3)
1251 n = normalize(head.velocity);
1252 v = self.origin - head.origin;
1254 if (d > (0 - head.bot_dodgerating))
1255 if (d < (vl * 0.2 + head.bot_dodgerating))
1257 // calculate direction and distance from the flight path, by removing the forward axis
1258 v = v - (n * (v * n));
1259 danger = head.bot_dodgerating - vlen(v);
1260 if (bestdanger < danger)
1262 bestdanger = danger;
1263 // dodge to the side of the object
1264 dodge = normalize(v);
1270 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1271 if (bestdanger < danger)
1273 bestdanger = danger;
1274 dodge = normalize(self.origin - head.origin);