5 #include "../navigation.qh"
6 #include "../scripting.qh"
7 #include "../waypoints.qh"
9 #include "../../../common/constants.qh"
10 #include "../../../common/items/all.qh"
12 #include "../../../common/triggers/trigger/jumppads.qh"
14 #include "../../../lib/warpzone/common.qh"
23 if(bot_execute_commands())
26 if (bot_strategytoken == self)
27 if (!bot_strategytoken_taken)
29 if(self.havocbot_blockhead)
31 self.havocbot_blockhead = false;
35 if (!self.jumppadcount)
39 // TODO: tracewalk() should take care of this job (better path finding under water)
40 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
41 if(self.deadflag != DEAD_NO)
42 if(self.goalcurrent==world)
43 if(self.waterlevel==WATERLEVEL_SWIMMING || (self.aistatus & AI_STATUS_OUT_WATER))
45 // Look for the closest waypoint out of water
47 float bestdistance, distance;
51 for (head = findchain(classname, "waypoint"); head; head = head.chain)
53 distance = vlen(head.origin - self.origin);
57 if(head.origin.z < self.origin.z)
60 if(head.origin.z - self.origin.z - self.view_ofs.z > 100)
63 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
66 traceline(self.origin + self.view_ofs , head.origin, true, head);
71 if(distance<bestdistance)
74 bestdistance = distance;
80 // te_wizspike(newgoal.origin);
81 navigation_pushroute(newgoal);
85 // token has been used this frame
86 bot_strategytoken_taken = true;
89 if(self.deadflag != DEAD_NO)
92 havocbot_chooseenemy();
93 if (self.bot_chooseweapontime < time )
95 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
96 havocbot_chooseweapon();
100 if (self.bot_aimtarg)
102 self.aistatus |= AI_STATUS_ATTACKING;
103 self.aistatus &= ~AI_STATUS_ROAMING;
107 Weapon w = get_weaponinfo(self.weapon);
109 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
111 self.BUTTON_ATCK = false;
112 self.BUTTON_ATCK2 = false;
116 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
117 self.lastfiredweapon = self.weapon;
122 if(IS_PLAYER(self.bot_aimtarg))
123 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
126 else if (self.goalcurrent)
128 self.aistatus |= AI_STATUS_ROAMING;
129 self.aistatus &= ~AI_STATUS_ATTACKING;
131 vector now,v,next;//,heading;
132 float aimdistance,skillblend,distanceblend,blend;
133 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
134 aimdistance = vlen(now);
135 //heading = self.velocity;
136 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
138 self.goalstack01 != self && self.goalstack01 != world && ((self.aistatus & AI_STATUS_RUNNING) == 0) &&
139 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
141 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
143 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
144 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
145 blend = skillblend * (1-distanceblend);
146 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
147 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
148 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
149 v = now + blend * (next - now);
150 //dprint(etos(self), " ");
151 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
152 //v = now * (distanceblend) + next * (1-distanceblend);
153 if (self.waterlevel < WATERLEVEL_SWIMMING)
155 //dprint("walk at:", vtos(v), "\n");
156 //te_lightning2(world, self.origin, self.goalcurrent.origin);
159 havocbot_movetogoal();
161 // if the bot is not attacking, consider reloading weapons
162 if (!(self.aistatus & AI_STATUS_ATTACKING))
164 // we are currently holding a weapon that's not fully loaded, reload it
165 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
166 if(self.clip_load < self.clip_size)
167 self.impulse = 20; // "press" the reload button, not sure if this is done right
169 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
170 // the code above executes next frame, starting the reloading then
171 if(skill >= 5) // bots can only look for unloaded weapons past this skill
172 if(self.clip_load >= 0) // only if we're not reloading a weapon already
174 for (int i = WEP_FIRST; i <= WEP_LAST; ++i)
176 entity e = get_weaponinfo(i);
177 if ((self.weapons & WepSet_FromWeapon(i)) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < e.reloading_ammo))
178 self.switchweapon = i;
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) self.BUTTON_CROUCH=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 self.BUTTON_JUMP = 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(self.flags & FL_ONGROUND)
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 self.BUTTON_JUMP = true;
358 self.aistatus |= AI_STATUS_RUNNING;
359 self.BUTTON_JUMP = true;
365 self.bot_timelastseengoal = time;
370 self.bot_timelastseengoal = 0;
374 // Release jump button
375 if(!cvar("sv_pogostick"))
376 if((self.flags & FL_ONGROUND) == 0)
378 if(self.velocity.z < 0 || vlen(self.velocity)<maxspeed)
379 self.BUTTON_JUMP = 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 self.BUTTON_HOOK = true;
483 if(self.navigation_jetpack_point.z - PL_MAX.z + PL_MIN.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 (!(self.flags & FL_ONGROUND))
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 self.BUTTON_HOOK = true;
574 self.BUTTON_HOOK = 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.m_id, true, false))
608 self.movement_x = maxspeed;
610 if(self.rocketjumptime)
612 if(time > self.rocketjumptime)
614 self.BUTTON_ATCK2 = true;
615 self.rocketjumptime = 0;
620 self.switchweapon = WEP_DEVASTATOR.m_id;
622 self.BUTTON_ATCK = 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 self.BUTTON_JUMP = true;
645 self.BUTTON_JUMP = 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 self.BUTTON_JUMP = true;
703 self.BUTTON_JUMP = 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 self.BUTTON_JUMP = 1;
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 self.BUTTON_JUMP = 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((self.flags & FL_ONGROUND) || (self.aistatus & AI_STATUS_RUNNING) || self.BUTTON_JUMP == true)
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 = self.BUTTON_JUMP;
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 // self.BUTTON_JUMP = 1;
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 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
855 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=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 float i, other_weapon_available = false;
996 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
998 Weapon w = get_weaponinfo(i);
999 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
1000 if (w.wr_checkammo1(w) + w.wr_checkammo2(w))
1001 other_weapon_available = true;
1003 if(other_weapon_available)
1010 void havocbot_chooseweapon()
1015 if(g_weaponarena_weapons == WEPSET(TUBA))
1017 self.switchweapon = WEP_TUBA.m_id;
1021 // TODO: clean this up by moving it to weapon code
1022 if(self.enemy==world)
1024 // If no weapon was chosen get the first available weapon
1026 for(i = WEP_FIRST; i <= WEP_LAST; ++i) if(i != WEP_BLASTER.m_id)
1028 if(client_hasweapon(self, i, true, false))
1030 self.switchweapon = i;
1037 // Do not change weapon during the next second after a combo
1038 float f = time - self.lastcombotime;
1043 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1045 // Should it do a weapon combo?
1046 float af, ct, combo_time, combo;
1048 af = ATTACK_FINISHED(self);
1049 ct = autocvar_bot_ai_weapon_combo_threshold;
1051 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1052 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1053 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1057 if(autocvar_bot_ai_weapon_combo)
1058 if(self.weapon == self.lastfiredweapon)
1062 self.lastcombotime = time;
1065 distance *= pow(2, self.bot_rangepreference);
1067 // Custom weapon list based on distance to the enemy
1068 if(bot_custom_weapon){
1070 // Choose weapons for far distance
1071 if ( distance > bot_distance_far ) {
1072 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1073 w = bot_weapons_far[i];
1074 if ( client_hasweapon(self, w, true, false) )
1076 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1078 self.switchweapon = w;
1084 // Choose weapons for mid distance
1085 if ( distance > bot_distance_close) {
1086 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1087 w = bot_weapons_mid[i];
1088 if ( client_hasweapon(self, w, true, false) )
1090 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1092 self.switchweapon = w;
1098 // Choose weapons for close distance
1099 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1100 w = bot_weapons_close[i];
1101 if ( client_hasweapon(self, w, true, false) )
1103 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1105 self.switchweapon = w;
1114 vector selfvel, enemyvel;
1115 // if(self.flags & FL_INWATER)
1117 if (time < self.nextaim)
1119 self.nextaim = time + 0.1;
1120 selfvel = self.velocity;
1121 if (!self.waterlevel)
1125 enemyvel = self.enemy.velocity;
1126 if (!self.enemy.waterlevel)
1128 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1131 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1134 float havocbot_moveto_refresh_route()
1136 // Refresh path to goal if necessary
1138 wp = self.havocbot_personal_waypoint;
1139 navigation_goalrating_start();
1140 navigation_routerating(wp, 10000, 10000);
1141 navigation_goalrating_end();
1142 return self.navigation_hasgoals;
1145 float havocbot_moveto(vector pos)
1149 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1151 // Step 4: Move to waypoint
1152 if(self.havocbot_personal_waypoint==world)
1154 LOG_TRACE("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1155 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1156 return CMD_STATUS_ERROR;
1159 if (!bot_strategytoken_taken)
1160 if(self.havocbot_personal_waypoint_searchtime<time)
1162 bot_strategytoken_taken = true;
1163 if(havocbot_moveto_refresh_route())
1165 LOG_TRACE(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1166 self.havocbot_personal_waypoint_searchtime = time + 10;
1167 self.havocbot_personal_waypoint_failcounter = 0;
1171 self.havocbot_personal_waypoint_failcounter += 1;
1172 self.havocbot_personal_waypoint_searchtime = time + 2;
1173 if(self.havocbot_personal_waypoint_failcounter >= 30)
1175 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1176 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1177 remove(self.havocbot_personal_waypoint);
1178 return CMD_STATUS_ERROR;
1181 LOG_TRACE(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1185 if(autocvar_bot_debug_goalstack)
1189 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1191 bot_aimdir(dir, -1);
1194 havocbot_movetogoal();
1196 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1198 // Step 5: Waypoint reached
1199 LOG_TRACE(self.netname, "'s personal waypoint reached\n");
1200 remove(self.havocbot_personal_waypoint);
1201 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1202 return CMD_STATUS_FINISHED;
1205 return CMD_STATUS_EXECUTING;
1208 // Step 2: Linking waypoint
1209 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1211 // Wait until it is linked
1212 if(!self.havocbot_personal_waypoint.wplinked)
1214 LOG_TRACE(self.netname, " waiting for personal waypoint to be linked\n");
1215 return CMD_STATUS_EXECUTING;
1218 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1219 self.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1220 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1222 // Step 3: Route to waypoint
1223 LOG_TRACE(self.netname, " walking to its personal waypoint\n");
1225 return CMD_STATUS_EXECUTING;
1228 // Step 1: Spawning waypoint
1229 wp = waypoint_spawnpersonal(pos);
1232 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1233 return CMD_STATUS_ERROR;
1236 self.havocbot_personal_waypoint = wp;
1237 self.havocbot_personal_waypoint_failcounter = 0;
1238 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1240 // if pos is inside a teleport, then let's mark it as teleport waypoint
1242 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1244 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1246 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1247 self.lastteleporttime = 0;
1252 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1253 print("routing to a teleporter\n");
1255 print("routing to a non-teleporter\n");
1258 return CMD_STATUS_EXECUTING;
1261 float havocbot_resetgoal()
1263 navigation_clearroute();
1264 return CMD_STATUS_FINISHED;
1267 void havocbot_setupbot()
1269 self.bot_ai = havocbot_ai;
1270 self.cmd_moveto = havocbot_moveto;
1271 self.cmd_resetgoal = havocbot_resetgoal;
1273 havocbot_chooserole();
1276 vector havocbot_dodge()
1278 // LordHavoc: disabled because this is too expensive
1283 float danger, bestdanger, vl, d;
1286 // check for dangerous objects near bot or approaching bot
1287 head = findchainfloat(bot_dodge, true);
1290 if (head.owner != self)
1292 vl = vlen(head.velocity);
1293 if (vl > autocvar_sv_maxspeed * 0.3)
1295 n = normalize(head.velocity);
1296 v = self.origin - head.origin;
1298 if (d > (0 - head.bot_dodgerating))
1299 if (d < (vl * 0.2 + head.bot_dodgerating))
1301 // calculate direction and distance from the flight path, by removing the forward axis
1302 v = v - (n * (v * n));
1303 danger = head.bot_dodgerating - vlen(v);
1304 if (bestdanger < danger)
1306 bestdanger = danger;
1307 // dodge to the side of the object
1308 dodge = normalize(v);
1314 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1315 if (bestdanger < danger)
1317 bestdanger = danger;
1318 dodge = normalize(self.origin - head.origin);