8 if(bot_execute_commands())
11 if (bot_strategytoken == self)
12 if (!bot_strategytoken_taken)
14 if(self.havocbot_blockhead)
16 self.havocbot_blockhead = false;
20 if (!self.jumppadcount)
24 // TODO: tracewalk() should take care of this job (better path finding under water)
25 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
26 if(self.deadflag != DEAD_NO)
27 if(self.goalcurrent==world)
28 if(self.waterlevel==WATERLEVEL_SWIMMING || (self.aistatus & AI_STATUS_OUT_WATER))
30 // Look for the closest waypoint out of water
32 float bestdistance, distance;
36 for (head = findchain(classname, "waypoint"); head; head = head.chain)
38 distance = vlen(head.origin - self.origin);
42 if(head.origin.z < self.origin.z)
45 if(head.origin.z - self.origin.z - self.view_ofs.z > 100)
48 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
51 traceline(self.origin + self.view_ofs , head.origin, true, head);
56 if(distance<bestdistance)
59 bestdistance = distance;
65 // te_wizspike(newgoal.origin);
66 navigation_pushroute(newgoal);
70 // token has been used this frame
71 bot_strategytoken_taken = true;
74 if(self.deadflag != DEAD_NO)
77 havocbot_chooseenemy();
78 if (self.bot_chooseweapontime < time )
80 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
81 havocbot_chooseweapon();
87 self.aistatus |= AI_STATUS_ATTACKING;
88 self.aistatus &= ~AI_STATUS_ROAMING;
92 Weapon w = get_weaponinfo(self.weapon);
94 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
96 self.BUTTON_ATCK = false;
97 self.BUTTON_ATCK2 = false;
101 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
102 self.lastfiredweapon = self.weapon;
107 if(IS_PLAYER(self.bot_aimtarg))
108 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
111 else if (self.goalcurrent)
113 self.aistatus |= AI_STATUS_ROAMING;
114 self.aistatus &= ~AI_STATUS_ATTACKING;
116 vector now,v,next;//,heading;
117 float aimdistance,skillblend,distanceblend,blend;
118 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
119 aimdistance = vlen(now);
120 //heading = self.velocity;
121 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
123 self.goalstack01 != self && self.goalstack01 != world && ((self.aistatus & AI_STATUS_RUNNING) == 0) &&
124 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
126 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
128 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
129 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
130 blend = skillblend * (1-distanceblend);
131 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
132 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
133 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
134 v = now + blend * (next - now);
135 //dprint(etos(self), " ");
136 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
137 //v = now * (distanceblend) + next * (1-distanceblend);
138 if (self.waterlevel < WATERLEVEL_SWIMMING)
140 //dprint("walk at:", vtos(v), "\n");
141 //te_lightning2(world, self.origin, self.goalcurrent.origin);
144 havocbot_movetogoal();
146 // if the bot is not attacking, consider reloading weapons
147 if (!(self.aistatus & AI_STATUS_ATTACKING))
149 // we are currently holding a weapon that's not fully loaded, reload it
150 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
151 if(self.clip_load < self.clip_size)
152 self.impulse = 20; // "press" the reload button, not sure if this is done right
154 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
155 // the code above executes next frame, starting the reloading then
156 if(skill >= 5) // bots can only look for unloaded weapons past this skill
157 if(self.clip_load >= 0) // only if we're not reloading a weapon already
159 for (int i = WEP_FIRST; i <= WEP_LAST; ++i)
161 entity e = get_weaponinfo(i);
162 if ((self.weapons & WepSet_FromWeapon(i)) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < e.reloading_ammo))
163 self.switchweapon = i;
169 void havocbot_keyboard_movement(vector destorg)
172 float blend, maxspeed;
175 sk = skill + self.bot_moveskill;
177 maxspeed = autocvar_sv_maxspeed;
179 if (time < self.havocbot_keyboardtime)
182 self.havocbot_keyboardtime =
184 self.havocbot_keyboardtime
185 + 0.05/max(1, sk+self.havocbot_keyboardskill)
186 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
188 keyboard = self.movement * (1.0 / maxspeed);
190 float trigger, trigger1;
191 blend = bound(0,sk*0.1,1);
192 trigger = autocvar_bot_ai_keyboard_threshold;
193 trigger1 = 0 - trigger;
195 // categorize forward movement
196 // at skill < 1.5 only forward
197 // at skill < 2.5 only individual directions
198 // at skill < 4.5 only individual directions, and forward diagonals
199 // at skill >= 4.5, all cases allowed
200 if (keyboard.x > trigger)
206 else if (keyboard.x < trigger1 && sk > 1.5)
221 if (keyboard.y > trigger)
223 else if (keyboard.y < trigger1)
228 if (keyboard.z > trigger)
230 else if (keyboard.z < trigger1)
235 self.havocbot_keyboard = keyboard * maxspeed;
236 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=true;
238 keyboard = self.havocbot_keyboard;
239 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
240 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
241 self.movement = self.movement + (keyboard - self.movement) * blend;
244 void havocbot_bunnyhop(vector dir)
246 float bunnyhopdistance;
251 // Don't jump when attacking
252 if(self.aistatus & AI_STATUS_ATTACKING)
255 if(IS_PLAYER(self.goalcurrent))
258 maxspeed = autocvar_sv_maxspeed;
260 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
262 self.aistatus &= ~AI_STATUS_RUNNING;
263 self.BUTTON_JUMP = false;
264 self.bot_canruntogoal = 0;
265 self.bot_timelastseengoal = 0;
269 if(self.waterlevel > WATERLEVEL_WETFEET)
271 self.aistatus &= ~AI_STATUS_RUNNING;
275 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
277 self.bot_canruntogoal = 0;
278 self.bot_timelastseengoal = 0;
281 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
282 bunnyhopdistance = vlen(self.origin - gco);
284 // Run only to visible goals
285 if(self.flags & FL_ONGROUND)
286 if(self.speed==maxspeed)
287 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
289 self.bot_lastseengoal = self.goalcurrent;
292 if(self.bot_timelastseengoal)
294 // for a period of time
295 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
298 checkdistance = true;
300 // don't run if it is too close
301 if(self.bot_canruntogoal==0)
303 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
304 self.bot_canruntogoal = 1;
306 self.bot_canruntogoal = -1;
309 if(self.bot_canruntogoal != 1)
312 if(self.aistatus & AI_STATUS_ROAMING)
313 if(self.goalcurrent.classname=="waypoint")
314 if (!(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
315 if(fabs(gco.z - self.origin.z) < self.maxs.z - self.mins.z)
316 if(self.goalstack01!=world)
318 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
319 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
320 while (deviation.y < -180) deviation.y = deviation.y + 360;
321 while (deviation.y > 180) deviation.y = deviation.y - 360;
323 if(fabs(deviation.y) < 20)
324 if(bunnyhopdistance < vlen(self.origin - gno))
325 if(fabs(gno.z - gco.z) < self.maxs.z - self.mins.z)
327 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
328 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
330 checkdistance = false;
337 self.aistatus &= ~AI_STATUS_RUNNING;
338 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
339 self.BUTTON_JUMP = true;
343 self.aistatus |= AI_STATUS_RUNNING;
344 self.BUTTON_JUMP = true;
350 self.bot_timelastseengoal = time;
355 self.bot_timelastseengoal = 0;
359 // Release jump button
360 if(!cvar("sv_pogostick"))
361 if((self.flags & FL_ONGROUND) == 0)
363 if(self.velocity.z < 0 || vlen(self.velocity)<maxspeed)
364 self.BUTTON_JUMP = false;
367 if(self.aistatus & AI_STATUS_RUNNING)
368 if(vlen(self.velocity)>maxspeed)
370 deviation = vectoangles(dir) - vectoangles(self.velocity);
371 while (deviation.y < -180) deviation.y = deviation.y + 360;
372 while (deviation.y > 180) deviation.y = deviation.y - 360;
374 if(fabs(deviation.y)>10)
378 self.movement_y = maxspeed * -1;
379 else if(deviation.y<10)
380 self.movement_y = maxspeed;
387 void havocbot_movetogoal()
395 vector evadeobstacle;
402 //if (self.goalentity)
403 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
404 self.movement = '0 0 0';
405 maxspeed = autocvar_sv_maxspeed;
407 // Jetpack navigation
409 if(self.navigation_jetpack_goal)
410 if(self.goalcurrent==self.navigation_jetpack_goal)
413 if(autocvar_bot_debug_goalstack)
416 te_wizspike(self.navigation_jetpack_point);
420 if (!(self.aistatus & AI_STATUS_JETPACK_FLYING))
422 // Brake almost completely so it can get a good direction
423 if(vlen(self.velocity)>10)
425 self.aistatus |= AI_STATUS_JETPACK_FLYING;
428 makevectors(self.v_angle.y * '0 1 0');
429 dir = normalize(self.navigation_jetpack_point - self.origin);
432 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
434 // Calculate brake distance in xy
438 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
440 v = vlen(self.velocity - self.velocity.z * '0 0 1');
441 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
442 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
443 if(d < db || d < 500)
446 if(fabs(self.velocity.x)>maxspeed*0.3)
448 self.movement_x = dir * v_forward * -maxspeed;
451 // Switch to normal mode
452 self.navigation_jetpack_goal = world;
453 self.aistatus &= ~AI_STATUS_JETPACK_LANDING;
454 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
458 else if(checkpvs(self.origin,self.goalcurrent))
460 // If I can see the goal switch to landing code
461 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
462 self.aistatus |= AI_STATUS_JETPACK_LANDING;
467 self.BUTTON_HOOK = true;
468 if(self.navigation_jetpack_point.z - PL_MAX.z + PL_MIN.z < self.origin.z)
470 self.movement_x = dir * v_forward * maxspeed;
471 self.movement_y = dir * v_right * maxspeed;
476 // Handling of jump pads
477 if(self.jumppadcount)
479 // If got stuck on the jump pad try to reach the farthest visible waypoint
480 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
482 if(fabs(self.velocity.z)<50)
484 entity head, newgoal = world;
485 float distance, bestdistance = 0;
487 for (head = findchain(classname, "waypoint"); head; head = head.chain)
490 distance = vlen(head.origin - self.origin);
494 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), true, world);
499 if(distance>bestdistance)
502 bestdistance = distance;
508 self.ignoregoal = self.goalcurrent;
509 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
510 navigation_clearroute();
511 navigation_routetogoal(newgoal, self.origin);
512 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
520 if(self.velocity.z>0)
522 float threshold, sxy;
523 vector velxy = self.velocity; velxy_z = 0;
525 threshold = maxspeed * 0.2;
528 LOG_TRACE("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
529 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
534 // Don't chase players while using a jump pad
535 if(IS_PLAYER(self.goalcurrent) || IS_PLAYER(self.goalstack01))
539 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
540 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
542 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
544 if (!(self.flags & FL_ONGROUND))
546 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
547 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
548 if(self.items & IT_JETPACK)
550 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
551 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
553 if(self.velocity.z<0)
555 self.BUTTON_HOOK = true;
559 self.BUTTON_HOOK = true;
561 // If there is no goal try to move forward
563 if(self.goalcurrent==world)
566 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
568 vector xyvelocity = self.velocity; xyvelocity_z = 0;
569 float xyspeed = xyvelocity * dir;
571 if(xyspeed < (maxspeed / 2))
573 makevectors(self.v_angle.y * '0 1 0');
574 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
575 if(trace_fraction==1)
577 self.movement_x = dir * v_forward * maxspeed;
578 self.movement_y = dir * v_right * maxspeed;
580 havocbot_keyboard_movement(self.origin + dir * 100);
584 self.havocbot_blockhead = true;
588 else if(self.health>WEP_CVAR(devastator, damage)*0.5)
590 if(self.velocity.z < 0)
591 if(client_hasweapon(self, WEP_DEVASTATOR.m_id, true, false))
593 self.movement_x = maxspeed;
595 if(self.rocketjumptime)
597 if(time > self.rocketjumptime)
599 self.BUTTON_ATCK2 = true;
600 self.rocketjumptime = 0;
605 self.switchweapon = WEP_DEVASTATOR.m_id;
607 self.BUTTON_ATCK = true;
608 self.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
614 // If there is no goal try to move forward
615 if(self.goalcurrent==world)
616 self.movement_x = maxspeed;
620 // If we are under water with no goals, swim up
622 if(self.goalcurrent==world)
625 if(self.waterlevel>WATERLEVEL_SWIMMING)
627 else if(self.velocity.z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
628 self.BUTTON_JUMP = true;
630 self.BUTTON_JUMP = false;
631 makevectors(self.v_angle.y * '0 1 0');
632 self.movement_x = dir * v_forward * maxspeed;
633 self.movement_y = dir * v_right * maxspeed;
634 self.movement_z = dir * v_up * maxspeed;
637 // if there is nowhere to go, exit
638 if (self.goalcurrent == world)
641 if (self.goalcurrent)
642 navigation_poptouchedgoals();
644 // if ran out of goals try to use an alternative goal or get a new strategy asap
645 if(self.goalcurrent == world)
647 self.bot_strategytime = 0;
652 if(autocvar_bot_debug_goalstack)
655 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
656 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
657 destorg = self.origin;
658 destorg.x = bound(m1_x, destorg.x, m2_x);
659 destorg.y = bound(m1_y, destorg.y, m2_y);
660 destorg.z = bound(m1_z, destorg.z, m2_z);
661 diff = destorg - self.origin;
663 dir = normalize(diff);
664 flatdir = diff;flatdir.z = 0;
665 flatdir = normalize(flatdir);
666 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
668 //if (self.bot_dodgevector_time < time)
670 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
671 // self.bot_dodgevector_jumpbutton = 1;
672 evadeobstacle = '0 0 0';
677 if(self.waterlevel>WATERLEVEL_SWIMMING)
680 self.aistatus |= AI_STATUS_OUT_WATER;
684 if(self.velocity.z >= 0 && !(self.watertype == CONTENT_WATER && gco.z < self.origin.z) &&
685 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
686 self.BUTTON_JUMP = true;
688 self.BUTTON_JUMP = false;
690 dir = normalize(flatdir);
691 makevectors(self.v_angle.y * '0 1 0');
695 if(self.aistatus & AI_STATUS_OUT_WATER)
696 self.aistatus &= ~AI_STATUS_OUT_WATER;
698 // jump if going toward an obstacle that doesn't look like stairs we
699 // can walk up directly
700 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, false, self);
701 if (trace_fraction < 1)
702 if (trace_plane_normal.z < 0.7)
705 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, false, self);
706 if (trace_fraction < s + 0.01)
707 if (trace_plane_normal.z < 0.7)
710 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, false, self);
711 if (trace_fraction > s)
712 self.BUTTON_JUMP = 1;
716 // avoiding dangers and obstacles
717 vector dst_ahead, dst_down;
718 makevectors(self.v_angle.y * '0 1 0');
719 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
720 dst_down = dst_ahead - '0 0 1500';
723 traceline(self.origin + self.view_ofs, dst_ahead, true, world);
725 // Check head-banging against walls
726 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
728 self.BUTTON_JUMP = true;
729 if(self.facingwalltime && time > self.facingwalltime)
731 self.ignoregoal = self.goalcurrent;
732 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
733 self.bot_strategytime = 0;
738 self.facingwalltime = time + 0.05;
743 self.facingwalltime = 0;
745 if(self.ignoregoal != world && time > self.ignoregoaltime)
747 self.ignoregoal = world;
748 self.ignoregoaltime = 0;
752 // Check for water/slime/lava and dangerous edges
753 // (only when the bot is on the ground or jumping intentionally)
754 self.aistatus &= ~AI_STATUS_DANGER_AHEAD;
756 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
757 if((self.flags & FL_ONGROUND) || (self.aistatus & AI_STATUS_RUNNING) || self.BUTTON_JUMP == true)
760 traceline(dst_ahead , dst_down, true, world);
761 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
762 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
763 if(trace_endpos.z < self.origin.z + self.mins.z)
765 s = pointcontents(trace_endpos + '0 0 1');
766 if (s != CONTENT_SOLID)
767 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
768 evadelava = normalize(self.velocity) * -1;
769 else if (s == CONTENT_SKY)
770 evadeobstacle = normalize(self.velocity) * -1;
771 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
772 self.goalcurrent.absmin, self.goalcurrent.absmax))
774 // if ain't a safe goal with "holes" (like the jumpad on soylent)
775 // and there is a trigger_hurt below
776 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
778 // Remove dangerous dynamic goals from stack
779 LOG_TRACE("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
780 navigation_clearroute();
790 makevectors(self.v_angle.y * '0 1 0');
792 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
793 self.aistatus |= AI_STATUS_DANGER_AHEAD;
796 dodge = havocbot_dodge();
797 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
798 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
799 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), true, world);
800 if(IS_PLAYER(trace_ent))
801 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
803 dir = normalize(dir + dodge + evadeobstacle + evadelava);
804 // self.bot_dodgevector = dir;
805 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
808 if(time < self.ladder_time)
810 if(self.goalcurrent.origin.z + self.goalcurrent.mins.z > self.origin.z + self.mins.z)
812 if(self.origin.z + self.mins.z < self.ladder_entity.origin.z + self.ladder_entity.maxs.z)
817 if(self.origin.z + self.mins.z > self.ladder_entity.origin.z + self.ladder_entity.mins.z)
822 //dir = self.bot_dodgevector;
823 //if (self.bot_dodgevector_jumpbutton)
824 // self.BUTTON_JUMP = 1;
825 self.movement_x = dir * v_forward * maxspeed;
826 self.movement_y = dir * v_right * maxspeed;
827 self.movement_z = dir * v_up * maxspeed;
829 // Emulate keyboard interface
831 havocbot_keyboard_movement(destorg);
834 // if(self.aistatus & AI_STATUS_ROAMING)
836 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
837 havocbot_bunnyhop(dir);
839 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
840 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=true;
841 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);
844 void havocbot_chooseenemy()
846 entity head, best, head2;
847 float rating, bestrating, hf;
849 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
856 if (!bot_shouldattack(self.enemy))
858 // enemy died or something, find a new target
860 self.havocbot_chooseenemy_finished = time;
862 else if (self.havocbot_stickenemy)
864 // tracking last chosen enemy
865 // if enemy is visible
866 // and not really really far away
867 // and we're not severely injured
868 // then keep tracking for a half second into the future
869 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,false,world);
870 if (trace_ent == self.enemy || trace_fraction == 1)
871 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
872 if (self.health > 30)
874 // remain tracking him for a shot while (case he went after a small corner or pilar
875 self.havocbot_chooseenemy_finished = time + 0.5;
878 // enemy isn't visible, or is far away, or we're injured severely
879 // so stop preferring this enemy
880 // (it will still take a half second until a new one is chosen)
881 self.havocbot_stickenemy = 0;
884 if (time < self.havocbot_chooseenemy_finished)
886 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
887 eye = self.origin + self.view_ofs;
889 bestrating = 100000000;
890 head = head2 = findchainfloat(bot_attack, true);
893 hf = self.dphitcontentsmask;
895 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
897 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
899 bool scan_transparent = false;
900 bool scan_secondary_targets = false;
901 bool have_secondary_targets = false;
904 scan_secondary_targets = false;
906 for( ; head; head = head.chain)
908 if(!scan_secondary_targets)
910 if(head.classname == "misc_breakablemodel")
912 have_secondary_targets = true;
918 if(head.classname != "misc_breakablemodel")
922 v = (head.absmin + head.absmax) * 0.5;
923 rating = vlen(v - eye);
924 if (rating<autocvar_bot_ai_enemydetectionradius)
925 if (bestrating > rating)
926 if (bot_shouldattack(head))
928 traceline(eye, v, true, self);
929 if (trace_ent == head || trace_fraction >= 1)
937 if(!best && have_secondary_targets && !scan_secondary_targets)
939 scan_secondary_targets = true;
942 bestrating = 100000000;
946 // I want to do a second scan if no enemy was found or I don't have weapons
947 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
948 if(best || self.weapons) // || self.weapon == WEP_RIFLE.m_id
953 // Set flags to see through transparent objects
954 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
957 scan_transparent = true;
961 self.dphitcontentsmask = hf;
964 self.havocbot_stickenemy = true;
965 if(best && best.classname == "misc_breakablemodel")
966 self.havocbot_stickenemy = false;
969 float havocbot_chooseweapon_checkreload(int new_weapon)
971 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
972 // so skip this for them, or they'll never get to reload their weapons at all.
973 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
977 // if this weapon is scheduled for reloading, don't switch to it during combat
978 if (self.weapon_load[new_weapon] < 0)
980 float i, other_weapon_available = false;
981 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
983 Weapon w = get_weaponinfo(i);
984 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
985 if (w.wr_checkammo1(w) + w.wr_checkammo2(w))
986 other_weapon_available = true;
988 if(other_weapon_available)
995 void havocbot_chooseweapon()
1000 if(g_weaponarena_weapons == WEPSET(TUBA))
1002 self.switchweapon = WEP_TUBA.m_id;
1006 // TODO: clean this up by moving it to weapon code
1007 if(self.enemy==world)
1009 // If no weapon was chosen get the first available weapon
1011 for(i = WEP_FIRST; i <= WEP_LAST; ++i) if(i != WEP_BLASTER.m_id)
1013 if(client_hasweapon(self, i, true, false))
1015 self.switchweapon = i;
1022 // Do not change weapon during the next second after a combo
1023 float f = time - self.lastcombotime;
1028 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1030 // Should it do a weapon combo?
1031 float af, ct, combo_time, combo;
1033 af = ATTACK_FINISHED(self);
1034 ct = autocvar_bot_ai_weapon_combo_threshold;
1036 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1037 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1038 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1042 if(autocvar_bot_ai_weapon_combo)
1043 if(self.weapon == self.lastfiredweapon)
1047 self.lastcombotime = time;
1050 distance *= pow(2, self.bot_rangepreference);
1052 // Custom weapon list based on distance to the enemy
1053 if(bot_custom_weapon){
1055 // Choose weapons for far distance
1056 if ( distance > bot_distance_far ) {
1057 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1058 w = bot_weapons_far[i];
1059 if ( client_hasweapon(self, w, true, false) )
1061 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1063 self.switchweapon = w;
1069 // Choose weapons for mid distance
1070 if ( distance > bot_distance_close) {
1071 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1072 w = bot_weapons_mid[i];
1073 if ( client_hasweapon(self, w, true, false) )
1075 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1077 self.switchweapon = w;
1083 // Choose weapons for close distance
1084 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1085 w = bot_weapons_close[i];
1086 if ( client_hasweapon(self, w, true, false) )
1088 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1090 self.switchweapon = w;
1099 vector selfvel, enemyvel;
1100 // if(self.flags & FL_INWATER)
1102 if (time < self.nextaim)
1104 self.nextaim = time + 0.1;
1105 selfvel = self.velocity;
1106 if (!self.waterlevel)
1110 enemyvel = self.enemy.velocity;
1111 if (!self.enemy.waterlevel)
1113 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1116 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1119 float havocbot_moveto_refresh_route()
1121 // Refresh path to goal if necessary
1123 wp = self.havocbot_personal_waypoint;
1124 navigation_goalrating_start();
1125 navigation_routerating(wp, 10000, 10000);
1126 navigation_goalrating_end();
1127 return self.navigation_hasgoals;
1130 float havocbot_moveto(vector pos)
1134 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1136 // Step 4: Move to waypoint
1137 if(self.havocbot_personal_waypoint==world)
1139 LOG_TRACE("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1140 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1141 return CMD_STATUS_ERROR;
1144 if (!bot_strategytoken_taken)
1145 if(self.havocbot_personal_waypoint_searchtime<time)
1147 bot_strategytoken_taken = true;
1148 if(havocbot_moveto_refresh_route())
1150 LOG_TRACE(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1151 self.havocbot_personal_waypoint_searchtime = time + 10;
1152 self.havocbot_personal_waypoint_failcounter = 0;
1156 self.havocbot_personal_waypoint_failcounter += 1;
1157 self.havocbot_personal_waypoint_searchtime = time + 2;
1158 if(self.havocbot_personal_waypoint_failcounter >= 30)
1160 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1161 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1162 remove(self.havocbot_personal_waypoint);
1163 return CMD_STATUS_ERROR;
1166 LOG_TRACE(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1170 if(autocvar_bot_debug_goalstack)
1174 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1176 bot_aimdir(dir, -1);
1179 havocbot_movetogoal();
1181 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1183 // Step 5: Waypoint reached
1184 LOG_TRACE(self.netname, "'s personal waypoint reached\n");
1185 remove(self.havocbot_personal_waypoint);
1186 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1187 return CMD_STATUS_FINISHED;
1190 return CMD_STATUS_EXECUTING;
1193 // Step 2: Linking waypoint
1194 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1196 // Wait until it is linked
1197 if(!self.havocbot_personal_waypoint.wplinked)
1199 LOG_TRACE(self.netname, " waiting for personal waypoint to be linked\n");
1200 return CMD_STATUS_EXECUTING;
1203 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1204 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1205 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1207 // Step 3: Route to waypoint
1208 LOG_TRACE(self.netname, " walking to its personal waypoint\n");
1210 return CMD_STATUS_EXECUTING;
1213 // Step 1: Spawning waypoint
1214 wp = waypoint_spawnpersonal(pos);
1217 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1218 return CMD_STATUS_ERROR;
1221 self.havocbot_personal_waypoint = wp;
1222 self.havocbot_personal_waypoint_failcounter = 0;
1223 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1225 // if pos is inside a teleport, then let's mark it as teleport waypoint
1227 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1229 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1231 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1232 self.lastteleporttime = 0;
1237 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1238 print("routing to a teleporter\n");
1240 print("routing to a non-teleporter\n");
1243 return CMD_STATUS_EXECUTING;
1246 float havocbot_resetgoal()
1248 navigation_clearroute();
1249 return CMD_STATUS_FINISHED;
1252 void havocbot_setupbot()
1254 self.bot_ai = havocbot_ai;
1255 self.cmd_moveto = havocbot_moveto;
1256 self.cmd_resetgoal = havocbot_resetgoal;
1258 havocbot_chooserole();
1261 vector havocbot_dodge()
1263 // LordHavoc: disabled because this is too expensive
1268 float danger, bestdanger, vl, d;
1271 // check for dangerous objects near bot or approaching bot
1272 head = findchainfloat(bot_dodge, true);
1275 if (head.owner != self)
1277 vl = vlen(head.velocity);
1278 if (vl > autocvar_sv_maxspeed * 0.3)
1280 n = normalize(head.velocity);
1281 v = self.origin - head.origin;
1283 if (d > (0 - head.bot_dodgerating))
1284 if (d < (vl * 0.2 + head.bot_dodgerating))
1286 // calculate direction and distance from the flight path, by removing the forward axis
1287 v = v - (n * (v * n));
1288 danger = head.bot_dodgerating - vlen(v);
1289 if (bestdanger < danger)
1291 bestdanger = danger;
1292 // dodge to the side of the object
1293 dodge = normalize(v);
1299 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1300 if (bestdanger < danger)
1302 bestdanger = danger;
1303 dodge = normalize(self.origin - head.origin);