5 #include "../navigation.qh"
6 #include "../scripting.qh"
7 #include "../waypoints.qh"
9 #include <common/constants.qh>
10 #include <common/physics/player.qh>
11 #include <common/state.qh>
12 #include <common/items/all.qh>
14 #include <common/triggers/trigger/jumppads.qh>
16 #include <lib/warpzone/common.qh>
25 if(bot_execute_commands())
28 if (bot_strategytoken == self)
29 if (!bot_strategytoken_taken)
31 if(self.havocbot_blockhead)
33 self.havocbot_blockhead = false;
37 if (!self.jumppadcount)
41 // TODO: tracewalk() should take care of this job (better path finding under water)
42 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
44 if(self.goalcurrent==world)
45 if(self.waterlevel==WATERLEVEL_SWIMMING || (self.aistatus & AI_STATUS_OUT_WATER))
47 // Look for the closest waypoint out of water
49 float bestdistance, distance;
53 for (head = findchain(classname, "waypoint"); head; head = head.chain)
55 distance = vlen(head.origin - self.origin);
59 if(head.origin.z < self.origin.z)
62 if(head.origin.z - self.origin.z - self.view_ofs.z > 100)
65 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
68 traceline(self.origin + self.view_ofs , head.origin, true, head);
73 if(distance<bestdistance)
76 bestdistance = distance;
82 // te_wizspike(newgoal.origin);
83 navigation_pushroute(newgoal);
87 // token has been used this frame
88 bot_strategytoken_taken = true;
94 havocbot_chooseenemy();
95 if (self.bot_chooseweapontime < time )
97 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
98 havocbot_chooseweapon();
102 if (self.bot_aimtarg)
104 self.aistatus |= AI_STATUS_ATTACKING;
105 self.aistatus &= ~AI_STATUS_ROAMING;
109 Weapon w = PS(self).m_weapon;
111 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
113 PHYS_INPUT_BUTTON_ATCK(self) = false;
114 PHYS_INPUT_BUTTON_ATCK2(self) = false;
118 if(PHYS_INPUT_BUTTON_ATCK(self) || PHYS_INPUT_BUTTON_ATCK2(self))
119 self.lastfiredweapon = PS(self).m_weapon.m_id;
124 if(IS_PLAYER(self.bot_aimtarg))
125 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
128 else if (self.goalcurrent)
130 self.aistatus |= AI_STATUS_ROAMING;
131 self.aistatus &= ~AI_STATUS_ATTACKING;
133 vector now,v,next;//,heading;
134 float aimdistance,skillblend,distanceblend,blend;
135 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
136 aimdistance = vlen(now);
137 //heading = self.velocity;
138 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
140 self.goalstack01 != self && self.goalstack01 != world && ((self.aistatus & AI_STATUS_RUNNING) == 0) &&
141 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
143 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
145 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
146 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
147 blend = skillblend * (1-distanceblend);
148 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
149 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
150 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
151 v = now + blend * (next - now);
152 //dprint(etos(self), " ");
153 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
154 //v = now * (distanceblend) + next * (1-distanceblend);
155 if (self.waterlevel < WATERLEVEL_SWIMMING)
157 //dprint("walk at:", vtos(v), "\n");
158 //te_lightning2(world, self.origin, self.goalcurrent.origin);
161 havocbot_movetogoal();
163 // if the bot is not attacking, consider reloading weapons
164 if (!(self.aistatus & AI_STATUS_ATTACKING))
166 // we are currently holding a weapon that's not fully loaded, reload it
167 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
168 if(self.clip_load < self.clip_size)
169 self.impulse = 20; // "press" the reload button, not sure if this is done right
171 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
172 // the code above executes next frame, starting the reloading then
173 if(skill >= 5) // bots can only look for unloaded weapons past this skill
174 if(self.clip_load >= 0) // only if we're not reloading a weapon already
176 FOREACH(Weapons, it != WEP_Null, LAMBDA(
177 if((self.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[it.m_id] < it.reloading_ammo))
178 PS(self).m_switchweapon = it;
184 void havocbot_keyboard_movement(vector destorg)
187 float blend, maxspeed;
190 sk = skill + self.bot_moveskill;
192 maxspeed = autocvar_sv_maxspeed;
194 if (time < self.havocbot_keyboardtime)
197 self.havocbot_keyboardtime =
199 self.havocbot_keyboardtime
200 + 0.05/max(1, sk+self.havocbot_keyboardskill)
201 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
203 keyboard = self.movement * (1.0 / maxspeed);
205 float trigger, trigger1;
206 blend = bound(0,sk*0.1,1);
207 trigger = autocvar_bot_ai_keyboard_threshold;
208 trigger1 = 0 - trigger;
210 // categorize forward movement
211 // at skill < 1.5 only forward
212 // at skill < 2.5 only individual directions
213 // at skill < 4.5 only individual directions, and forward diagonals
214 // at skill >= 4.5, all cases allowed
215 if (keyboard.x > trigger)
221 else if (keyboard.x < trigger1 && sk > 1.5)
236 if (keyboard.y > trigger)
238 else if (keyboard.y < trigger1)
243 if (keyboard.z > trigger)
245 else if (keyboard.z < trigger1)
250 self.havocbot_keyboard = keyboard * maxspeed;
251 if (self.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(self) = true;
253 keyboard = self.havocbot_keyboard;
254 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
255 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
256 self.movement = self.movement + (keyboard - self.movement) * blend;
259 void havocbot_bunnyhop(vector dir)
261 float bunnyhopdistance;
266 // Don't jump when attacking
267 if(self.aistatus & AI_STATUS_ATTACKING)
270 if(IS_PLAYER(self.goalcurrent))
273 maxspeed = autocvar_sv_maxspeed;
275 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
277 self.aistatus &= ~AI_STATUS_RUNNING;
278 PHYS_INPUT_BUTTON_JUMP(self) = false;
279 self.bot_canruntogoal = 0;
280 self.bot_timelastseengoal = 0;
284 if(self.waterlevel > WATERLEVEL_WETFEET)
286 self.aistatus &= ~AI_STATUS_RUNNING;
290 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
292 self.bot_canruntogoal = 0;
293 self.bot_timelastseengoal = 0;
296 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
297 bunnyhopdistance = vlen(self.origin - gco);
299 // Run only to visible goals
300 if(IS_ONGROUND(self))
301 if(self.speed==maxspeed)
302 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
304 self.bot_lastseengoal = self.goalcurrent;
307 if(self.bot_timelastseengoal)
309 // for a period of time
310 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
313 checkdistance = true;
315 // don't run if it is too close
316 if(self.bot_canruntogoal==0)
318 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
319 self.bot_canruntogoal = 1;
321 self.bot_canruntogoal = -1;
324 if(self.bot_canruntogoal != 1)
327 if(self.aistatus & AI_STATUS_ROAMING)
328 if(self.goalcurrent.classname=="waypoint")
329 if (!(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
330 if(fabs(gco.z - self.origin.z) < self.maxs.z - self.mins.z)
331 if(self.goalstack01!=world)
333 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
334 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
335 while (deviation.y < -180) deviation.y = deviation.y + 360;
336 while (deviation.y > 180) deviation.y = deviation.y - 360;
338 if(fabs(deviation.y) < 20)
339 if(bunnyhopdistance < vlen(self.origin - gno))
340 if(fabs(gno.z - gco.z) < self.maxs.z - self.mins.z)
342 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
343 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
345 checkdistance = false;
352 self.aistatus &= ~AI_STATUS_RUNNING;
353 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
354 PHYS_INPUT_BUTTON_JUMP(self) = true;
358 self.aistatus |= AI_STATUS_RUNNING;
359 PHYS_INPUT_BUTTON_JUMP(self) = true;
365 self.bot_timelastseengoal = time;
370 self.bot_timelastseengoal = 0;
374 // Release jump button
375 if(!cvar("sv_pogostick"))
376 if((IS_ONGROUND(self)) == 0)
378 if(self.velocity.z < 0 || vlen(self.velocity)<maxspeed)
379 PHYS_INPUT_BUTTON_JUMP(self) = false;
382 if(self.aistatus & AI_STATUS_RUNNING)
383 if(vlen(self.velocity)>maxspeed)
385 deviation = vectoangles(dir) - vectoangles(self.velocity);
386 while (deviation.y < -180) deviation.y = deviation.y + 360;
387 while (deviation.y > 180) deviation.y = deviation.y - 360;
389 if(fabs(deviation.y)>10)
393 self.movement_y = maxspeed * -1;
394 else if(deviation.y<10)
395 self.movement_y = maxspeed;
402 void havocbot_movetogoal()
410 vector evadeobstacle;
417 //if (self.goalentity)
418 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
419 self.movement = '0 0 0';
420 maxspeed = autocvar_sv_maxspeed;
422 // Jetpack navigation
424 if(self.navigation_jetpack_goal)
425 if(self.goalcurrent==self.navigation_jetpack_goal)
428 if(autocvar_bot_debug_goalstack)
431 te_wizspike(self.navigation_jetpack_point);
435 if (!(self.aistatus & AI_STATUS_JETPACK_FLYING))
437 // Brake almost completely so it can get a good direction
438 if(vlen(self.velocity)>10)
440 self.aistatus |= AI_STATUS_JETPACK_FLYING;
443 makevectors(self.v_angle.y * '0 1 0');
444 dir = normalize(self.navigation_jetpack_point - self.origin);
447 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
449 // Calculate brake distance in xy
453 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
455 v = vlen(self.velocity - self.velocity.z * '0 0 1');
456 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
457 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
458 if(d < db || d < 500)
461 if(fabs(self.velocity.x)>maxspeed*0.3)
463 self.movement_x = dir * v_forward * -maxspeed;
466 // Switch to normal mode
467 self.navigation_jetpack_goal = world;
468 self.aistatus &= ~AI_STATUS_JETPACK_LANDING;
469 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
473 else if(checkpvs(self.origin,self.goalcurrent))
475 // If I can see the goal switch to landing code
476 self.aistatus &= ~AI_STATUS_JETPACK_FLYING;
477 self.aistatus |= AI_STATUS_JETPACK_LANDING;
482 PHYS_INPUT_BUTTON_HOOK(self) = true;
483 if(self.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < self.origin.z)
485 self.movement_x = dir * v_forward * maxspeed;
486 self.movement_y = dir * v_right * maxspeed;
491 // Handling of jump pads
492 if(self.jumppadcount)
494 // If got stuck on the jump pad try to reach the farthest visible waypoint
495 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
497 if(fabs(self.velocity.z)<50)
499 entity head, newgoal = world;
500 float distance, bestdistance = 0;
502 for (head = findchain(classname, "waypoint"); head; head = head.chain)
505 distance = vlen(head.origin - self.origin);
509 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), true, world);
514 if(distance>bestdistance)
517 bestdistance = distance;
523 self.ignoregoal = self.goalcurrent;
524 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
525 navigation_clearroute();
526 navigation_routetogoal(newgoal, self.origin);
527 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
535 if(self.velocity.z>0)
537 float threshold, sxy;
538 vector velxy = self.velocity; velxy_z = 0;
540 threshold = maxspeed * 0.2;
543 LOG_TRACE("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
544 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
549 // Don't chase players while using a jump pad
550 if(IS_PLAYER(self.goalcurrent) || IS_PLAYER(self.goalstack01))
554 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
555 self.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
557 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
559 if (!(IS_ONGROUND(self)))
561 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
562 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
563 if(self.items & IT_JETPACK)
565 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
566 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
568 if(self.velocity.z<0)
570 PHYS_INPUT_BUTTON_HOOK(self) = true;
574 PHYS_INPUT_BUTTON_HOOK(self) = true;
576 // If there is no goal try to move forward
578 if(self.goalcurrent==world)
581 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
583 vector xyvelocity = self.velocity; xyvelocity_z = 0;
584 float xyspeed = xyvelocity * dir;
586 if(xyspeed < (maxspeed / 2))
588 makevectors(self.v_angle.y * '0 1 0');
589 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
590 if(trace_fraction==1)
592 self.movement_x = dir * v_forward * maxspeed;
593 self.movement_y = dir * v_right * maxspeed;
595 havocbot_keyboard_movement(self.origin + dir * 100);
599 self.havocbot_blockhead = true;
603 else if(self.health>WEP_CVAR(devastator, damage)*0.5)
605 if(self.velocity.z < 0)
606 if(client_hasweapon(self, WEP_DEVASTATOR, true, false))
608 self.movement_x = maxspeed;
610 if(self.rocketjumptime)
612 if(time > self.rocketjumptime)
614 PHYS_INPUT_BUTTON_ATCK2(self) = true;
615 self.rocketjumptime = 0;
620 PS(self).m_switchweapon = WEP_DEVASTATOR;
622 PHYS_INPUT_BUTTON_ATCK(self) = true;
623 self.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
629 // If there is no goal try to move forward
630 if(self.goalcurrent==world)
631 self.movement_x = maxspeed;
635 // If we are under water with no goals, swim up
637 if(self.goalcurrent==world)
640 if(self.waterlevel>WATERLEVEL_SWIMMING)
642 else if(self.velocity.z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
643 PHYS_INPUT_BUTTON_JUMP(self) = true;
645 PHYS_INPUT_BUTTON_JUMP(self) = false;
646 makevectors(self.v_angle.y * '0 1 0');
647 self.movement_x = dir * v_forward * maxspeed;
648 self.movement_y = dir * v_right * maxspeed;
649 self.movement_z = dir * v_up * maxspeed;
652 // if there is nowhere to go, exit
653 if (self.goalcurrent == world)
656 if (self.goalcurrent)
657 navigation_poptouchedgoals();
659 // if ran out of goals try to use an alternative goal or get a new strategy asap
660 if(self.goalcurrent == world)
662 self.bot_strategytime = 0;
667 if(autocvar_bot_debug_goalstack)
670 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
671 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
672 destorg = self.origin;
673 destorg.x = bound(m1_x, destorg.x, m2_x);
674 destorg.y = bound(m1_y, destorg.y, m2_y);
675 destorg.z = bound(m1_z, destorg.z, m2_z);
676 diff = destorg - self.origin;
678 dir = normalize(diff);
679 flatdir = diff;flatdir.z = 0;
680 flatdir = normalize(flatdir);
681 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
683 //if (self.bot_dodgevector_time < time)
685 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
686 // self.bot_dodgevector_jumpbutton = 1;
687 evadeobstacle = '0 0 0';
692 if(self.waterlevel>WATERLEVEL_SWIMMING)
695 self.aistatus |= AI_STATUS_OUT_WATER;
699 if(self.velocity.z >= 0 && !(self.watertype == CONTENT_WATER && gco.z < self.origin.z) &&
700 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
701 PHYS_INPUT_BUTTON_JUMP(self) = true;
703 PHYS_INPUT_BUTTON_JUMP(self) = false;
705 dir = normalize(flatdir);
706 makevectors(self.v_angle.y * '0 1 0');
710 if(self.aistatus & AI_STATUS_OUT_WATER)
711 self.aistatus &= ~AI_STATUS_OUT_WATER;
713 // jump if going toward an obstacle that doesn't look like stairs we
714 // can walk up directly
715 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, false, self);
716 if (trace_fraction < 1)
717 if (trace_plane_normal.z < 0.7)
720 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, false, self);
721 if (trace_fraction < s + 0.01)
722 if (trace_plane_normal.z < 0.7)
725 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, false, self);
726 if (trace_fraction > s)
727 PHYS_INPUT_BUTTON_JUMP(self) = true;
731 // avoiding dangers and obstacles
732 vector dst_ahead, dst_down;
733 makevectors(self.v_angle.y * '0 1 0');
734 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
735 dst_down = dst_ahead - '0 0 1500';
738 traceline(self.origin + self.view_ofs, dst_ahead, true, world);
740 // Check head-banging against walls
741 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
743 PHYS_INPUT_BUTTON_JUMP(self) = true;
744 if(self.facingwalltime && time > self.facingwalltime)
746 self.ignoregoal = self.goalcurrent;
747 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
748 self.bot_strategytime = 0;
753 self.facingwalltime = time + 0.05;
758 self.facingwalltime = 0;
760 if(self.ignoregoal != world && time > self.ignoregoaltime)
762 self.ignoregoal = world;
763 self.ignoregoaltime = 0;
767 // Check for water/slime/lava and dangerous edges
768 // (only when the bot is on the ground or jumping intentionally)
769 self.aistatus &= ~AI_STATUS_DANGER_AHEAD;
771 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
772 if((IS_ONGROUND(self)) || (self.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(self))
775 traceline(dst_ahead , dst_down, true, world);
776 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
777 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
778 if(trace_endpos.z < self.origin.z + self.mins.z)
780 s = pointcontents(trace_endpos + '0 0 1');
781 if (s != CONTENT_SOLID)
782 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
783 evadelava = normalize(self.velocity) * -1;
784 else if (s == CONTENT_SKY)
785 evadeobstacle = normalize(self.velocity) * -1;
786 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
787 self.goalcurrent.absmin, self.goalcurrent.absmax))
789 // if ain't a safe goal with "holes" (like the jumpad on soylent)
790 // and there is a trigger_hurt below
791 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
793 // Remove dangerous dynamic goals from stack
794 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");
795 navigation_clearroute();
805 makevectors(self.v_angle.y * '0 1 0');
807 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
808 self.aistatus |= AI_STATUS_DANGER_AHEAD;
811 dodge = havocbot_dodge();
812 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
813 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
814 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), true, world);
815 if(IS_PLAYER(trace_ent))
816 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
818 dir = normalize(dir + dodge + evadeobstacle + evadelava);
819 // self.bot_dodgevector = dir;
820 // self.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(self);
823 if(time < self.ladder_time)
825 if(self.goalcurrent.origin.z + self.goalcurrent.mins.z > self.origin.z + self.mins.z)
827 if(self.origin.z + self.mins.z < self.ladder_entity.origin.z + self.ladder_entity.maxs.z)
832 if(self.origin.z + self.mins.z > self.ladder_entity.origin.z + self.ladder_entity.mins.z)
837 //dir = self.bot_dodgevector;
838 //if (self.bot_dodgevector_jumpbutton)
839 // PHYS_INPUT_BUTTON_JUMP(self) = true;
840 self.movement_x = dir * v_forward * maxspeed;
841 self.movement_y = dir * v_right * maxspeed;
842 self.movement_z = dir * v_up * maxspeed;
844 // Emulate keyboard interface
846 havocbot_keyboard_movement(destorg);
849 // if(self.aistatus & AI_STATUS_ROAMING)
851 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
852 havocbot_bunnyhop(dir);
854 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(self))) PHYS_INPUT_BUTTON_JUMP(self) = true;
855 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) PHYS_INPUT_BUTTON_JUMP(self) = true;
856 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);
859 void havocbot_chooseenemy()
861 entity head, best, head2;
862 float rating, bestrating, hf;
864 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
871 if (!bot_shouldattack(self.enemy))
873 // enemy died or something, find a new target
875 self.havocbot_chooseenemy_finished = time;
877 else if (self.havocbot_stickenemy)
879 // tracking last chosen enemy
880 // if enemy is visible
881 // and not really really far away
882 // and we're not severely injured
883 // then keep tracking for a half second into the future
884 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,false,world);
885 if (trace_ent == self.enemy || trace_fraction == 1)
886 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
887 if (self.health > 30)
889 // remain tracking him for a shot while (case he went after a small corner or pilar
890 self.havocbot_chooseenemy_finished = time + 0.5;
893 // enemy isn't visible, or is far away, or we're injured severely
894 // so stop preferring this enemy
895 // (it will still take a half second until a new one is chosen)
896 self.havocbot_stickenemy = 0;
899 if (time < self.havocbot_chooseenemy_finished)
901 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
902 eye = self.origin + self.view_ofs;
904 bestrating = 100000000;
905 head = head2 = findchainfloat(bot_attack, true);
908 hf = self.dphitcontentsmask;
910 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
912 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
914 bool scan_transparent = false;
915 bool scan_secondary_targets = false;
916 bool have_secondary_targets = false;
919 scan_secondary_targets = false;
921 for( ; head; head = head.chain)
923 if(!scan_secondary_targets)
925 if(head.classname == "misc_breakablemodel")
927 have_secondary_targets = true;
933 if(head.classname != "misc_breakablemodel")
937 v = (head.absmin + head.absmax) * 0.5;
938 rating = vlen(v - eye);
939 if (rating<autocvar_bot_ai_enemydetectionradius)
940 if (bestrating > rating)
941 if (bot_shouldattack(head))
943 traceline(eye, v, true, self);
944 if (trace_ent == head || trace_fraction >= 1)
952 if(!best && have_secondary_targets && !scan_secondary_targets)
954 scan_secondary_targets = true;
957 bestrating = 100000000;
961 // I want to do a second scan if no enemy was found or I don't have weapons
962 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
963 if(best || self.weapons) // || self.weapon == WEP_RIFLE.m_id
968 // Set flags to see through transparent objects
969 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
972 scan_transparent = true;
976 self.dphitcontentsmask = hf;
979 self.havocbot_stickenemy = true;
980 if(best && best.classname == "misc_breakablemodel")
981 self.havocbot_stickenemy = false;
984 float havocbot_chooseweapon_checkreload(int new_weapon)
986 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
987 // so skip this for them, or they'll never get to reload their weapons at all.
988 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
992 // if this weapon is scheduled for reloading, don't switch to it during combat
993 if (self.weapon_load[new_weapon] < 0)
995 bool other_weapon_available = false;
996 FOREACH(Weapons, it != WEP_Null, LAMBDA(
997 if(it.wr_checkammo1(it) + it.wr_checkammo2(it))
998 other_weapon_available = true;
1000 if(other_weapon_available)
1007 void havocbot_chooseweapon()
1012 if(g_weaponarena_weapons == WEPSET(TUBA))
1014 PS(self).m_switchweapon = WEP_TUBA;
1018 // TODO: clean this up by moving it to weapon code
1019 if(self.enemy==world)
1021 // If no weapon was chosen get the first available weapon
1022 if(PS(self).m_weapon==WEP_Null)
1023 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1024 if(client_hasweapon(self, it, true, false))
1026 PS(self).m_switchweapon = it;
1033 // Do not change weapon during the next second after a combo
1034 float f = time - self.lastcombotime;
1039 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1041 // Should it do a weapon combo?
1042 float af, ct, combo_time, combo;
1044 af = ATTACK_FINISHED(self, 0);
1045 ct = autocvar_bot_ai_weapon_combo_threshold;
1047 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1048 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1049 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1053 if(autocvar_bot_ai_weapon_combo)
1054 if(PS(self).m_weapon.m_id == self.lastfiredweapon)
1058 self.lastcombotime = time;
1061 distance *= pow(2, self.bot_rangepreference);
1063 // Custom weapon list based on distance to the enemy
1064 if(bot_custom_weapon){
1066 // Choose weapons for far distance
1067 if ( distance > bot_distance_far ) {
1068 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1069 w = bot_weapons_far[i];
1070 if ( client_hasweapon(self, Weapons_from(w), true, false) )
1072 if ((PS(self).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(w))
1074 PS(self).m_switchweapon = Weapons_from(w);
1080 // Choose weapons for mid distance
1081 if ( distance > bot_distance_close) {
1082 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1083 w = bot_weapons_mid[i];
1084 if ( client_hasweapon(self, Weapons_from(w), true, false) )
1086 if ((PS(self).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(w))
1088 PS(self).m_switchweapon = Weapons_from(w);
1094 // Choose weapons for close distance
1095 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1096 w = bot_weapons_close[i];
1097 if ( client_hasweapon(self, Weapons_from(w), true, false) )
1099 if ((PS(self).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(w))
1101 PS(self).m_switchweapon = Weapons_from(w);
1110 vector selfvel, enemyvel;
1111 // if(self.flags & FL_INWATER)
1113 if (time < self.nextaim)
1115 self.nextaim = time + 0.1;
1116 selfvel = self.velocity;
1117 if (!self.waterlevel)
1121 enemyvel = self.enemy.velocity;
1122 if (!self.enemy.waterlevel)
1124 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1127 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1130 float havocbot_moveto_refresh_route()
1132 // Refresh path to goal if necessary
1134 wp = self.havocbot_personal_waypoint;
1135 navigation_goalrating_start();
1136 navigation_routerating(wp, 10000, 10000);
1137 navigation_goalrating_end();
1138 return self.navigation_hasgoals;
1141 float havocbot_moveto(vector pos)
1145 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1147 // Step 4: Move to waypoint
1148 if(self.havocbot_personal_waypoint==world)
1150 LOG_TRACE("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1151 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1152 return CMD_STATUS_ERROR;
1155 if (!bot_strategytoken_taken)
1156 if(self.havocbot_personal_waypoint_searchtime<time)
1158 bot_strategytoken_taken = true;
1159 if(havocbot_moveto_refresh_route())
1161 LOG_TRACE(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1162 self.havocbot_personal_waypoint_searchtime = time + 10;
1163 self.havocbot_personal_waypoint_failcounter = 0;
1167 self.havocbot_personal_waypoint_failcounter += 1;
1168 self.havocbot_personal_waypoint_searchtime = time + 2;
1169 if(self.havocbot_personal_waypoint_failcounter >= 30)
1171 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1172 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1173 remove(self.havocbot_personal_waypoint);
1174 return CMD_STATUS_ERROR;
1177 LOG_TRACE(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1181 if(autocvar_bot_debug_goalstack)
1185 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1187 bot_aimdir(dir, -1);
1190 havocbot_movetogoal();
1192 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1194 // Step 5: Waypoint reached
1195 LOG_TRACE(self.netname, "'s personal waypoint reached\n");
1196 remove(self.havocbot_personal_waypoint);
1197 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1198 return CMD_STATUS_FINISHED;
1201 return CMD_STATUS_EXECUTING;
1204 // Step 2: Linking waypoint
1205 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1207 // Wait until it is linked
1208 if(!self.havocbot_personal_waypoint.wplinked)
1210 LOG_TRACE(self.netname, " waiting for personal waypoint to be linked\n");
1211 return CMD_STATUS_EXECUTING;
1214 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1215 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1216 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1218 // Step 3: Route to waypoint
1219 LOG_TRACE(self.netname, " walking to its personal waypoint\n");
1221 return CMD_STATUS_EXECUTING;
1224 // Step 1: Spawning waypoint
1225 wp = waypoint_spawnpersonal(pos);
1228 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1229 return CMD_STATUS_ERROR;
1232 self.havocbot_personal_waypoint = wp;
1233 self.havocbot_personal_waypoint_failcounter = 0;
1234 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1236 // if pos is inside a teleport, then let's mark it as teleport waypoint
1238 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1240 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1242 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1243 self.lastteleporttime = 0;
1248 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1249 print("routing to a teleporter\n");
1251 print("routing to a non-teleporter\n");
1254 return CMD_STATUS_EXECUTING;
1257 float havocbot_resetgoal()
1259 navigation_clearroute();
1260 return CMD_STATUS_FINISHED;
1263 void havocbot_setupbot()
1265 self.bot_ai = havocbot_ai;
1266 self.cmd_moveto = havocbot_moveto;
1267 self.cmd_resetgoal = havocbot_resetgoal;
1269 havocbot_chooserole();
1272 vector havocbot_dodge()
1274 // LordHavoc: disabled because this is too expensive
1279 float danger, bestdanger, vl, d;
1282 // check for dangerous objects near bot or approaching bot
1283 head = findchainfloat(bot_dodge, true);
1286 if (head.owner != self)
1288 vl = vlen(head.velocity);
1289 if (vl > autocvar_sv_maxspeed * 0.3)
1291 n = normalize(head.velocity);
1292 v = self.origin - head.origin;
1294 if (d > (0 - head.bot_dodgerating))
1295 if (d < (vl * 0.2 + head.bot_dodgerating))
1297 // calculate direction and distance from the flight path, by removing the forward axis
1298 v = v - (n * (v * n));
1299 danger = head.bot_dodgerating - vlen(v);
1300 if (bestdanger < danger)
1302 bestdanger = danger;
1303 // dodge to the side of the object
1304 dodge = normalize(v);
1310 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1311 if (bestdanger < danger)
1313 bestdanger = danger;
1314 dodge = normalize(self.origin - head.origin);