2 #include "role_onslaught.qc"
3 #include "role_keyhunt.qc"
4 #include "role_freezetag.qc"
5 #include "role_assault.qc"
13 if(bot_execute_commands())
16 if (bot_strategytoken == self)
17 if (!bot_strategytoken_taken)
19 if(self.havocbot_blockhead)
21 self.havocbot_blockhead = FALSE;
25 if not(self.jumppadcount)
29 // TODO: tracewalk() should take care of this job (better path finding under water)
30 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
31 if(self.deadflag != DEAD_NO)
32 if(self.goalcurrent==world)
33 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
35 // Look for the closest waypoint out of water
37 float bestdistance, distance;
41 for (head = findchain(classname, "waypoint"); head; head = head.chain)
43 distance = vlen(head.origin - self.origin);
47 if(head.origin_z < self.origin_z)
50 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
53 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
56 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
61 if(distance<bestdistance)
64 bestdistance = distance;
70 // te_wizspike(newgoal.origin);
71 navigation_pushroute(newgoal);
75 // token has been used this frame
76 bot_strategytoken_taken = TRUE;
79 if(self.deadflag != DEAD_NO)
82 havocbot_chooseenemy();
83 if (self.bot_chooseweapontime < time )
85 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
86 havocbot_chooseweapon();
92 self.aistatus |= AI_STATUS_ATTACKING;
93 self.aistatus &~= AI_STATUS_ROAMING;
95 if(!WEPSET_EMPTY_E(self))
97 weapon_action(self.weapon, WR_AIM);
98 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
100 self.BUTTON_ATCK = FALSE;
101 self.BUTTON_ATCK2 = FALSE;
105 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
106 self.lastfiredweapon = self.weapon;
111 if(self.bot_aimtarg.classname=="player")
112 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
115 else if (self.goalcurrent)
117 self.aistatus |= AI_STATUS_ROAMING;
118 self.aistatus &~= AI_STATUS_ATTACKING;
120 vector now,v,next;//,heading;
121 float aimdistance,skillblend,distanceblend,blend;
122 next = now = ( (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5) - (self.origin + self.view_ofs);
123 aimdistance = vlen(now);
124 //heading = self.velocity;
125 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
127 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
128 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
130 next = ((self.goalstack01.absmin + self.goalstack01.absmax) * 0.5) - (self.origin + self.view_ofs);
132 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
133 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
134 blend = skillblend * (1-distanceblend);
135 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
136 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
137 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
138 v = now + blend * (next - now);
139 //dprint(etos(self), " ");
140 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
141 //v = now * (distanceblend) + next * (1-distanceblend);
142 if (self.waterlevel < WATERLEVEL_SWIMMING)
144 //dprint("walk at:", vtos(v), "\n");
145 //te_lightning2(world, self.origin, self.goalcurrent.origin);
148 havocbot_movetogoal();
150 // if the bot is not attacking, consider reloading weapons
151 if not(self.aistatus & AI_STATUS_ATTACKING)
156 // we are currently holding a weapon that's not fully loaded, reload it
157 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
158 if(self.clip_load < self.clip_size)
159 self.impulse = 20; // "press" the reload button, not sure if this is done right
161 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
162 // the code above executes next frame, starting the reloading then
163 if(skill >= 5) // bots can only look for unloaded weapons past this skill
164 if(self.clip_load >= 0) // only if we're not reloading a weapon already
166 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
168 e = get_weaponinfo(i);
169 if (WEPSET_CONTAINS_EW(self, i) && (e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < cvar(strcat("g_balance_", e.netname, "_reload_ammo"))))
170 self.switchweapon = i;
176 void havocbot_keyboard_movement(vector destorg)
179 float blend, maxspeed;
182 sk = skill + self.bot_moveskill;
184 maxspeed = autocvar_sv_maxspeed;
186 if (time < self.havocbot_keyboardtime)
189 self.havocbot_keyboardtime =
191 self.havocbot_keyboardtime
192 + 0.05/max(1, sk+self.havocbot_keyboardskill)
193 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
195 keyboard = self.movement * (1.0 / maxspeed);
197 float trigger, trigger1;
198 blend = bound(0,sk*0.1,1);
199 trigger = autocvar_bot_ai_keyboard_threshold;
200 trigger1 = 0 - trigger;
202 // categorize forward movement
203 // at skill < 1.5 only forward
204 // at skill < 2.5 only individual directions
205 // at skill < 4.5 only individual directions, and forward diagonals
206 // at skill >= 4.5, all cases allowed
207 if (keyboard_x > trigger)
213 else if (keyboard_x < trigger1 && sk > 1.5)
228 if (keyboard_y > trigger)
230 else if (keyboard_y < trigger1)
235 if (keyboard_z > trigger)
237 else if (keyboard_z < trigger1)
242 self.havocbot_keyboard = keyboard * maxspeed;
243 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
245 keyboard = self.havocbot_keyboard;
246 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
247 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
248 self.movement = self.movement + (keyboard - self.movement) * blend;
251 void havocbot_bunnyhop(vector dir)
253 float bunnyhopdistance;
258 if(autocvar_g_midair)
261 // Don't jump when attacking
262 if(self.aistatus & AI_STATUS_ATTACKING)
265 if(self.goalcurrent.classname == "player")
268 maxspeed = autocvar_sv_maxspeed;
270 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
272 self.aistatus &~= AI_STATUS_RUNNING;
273 self.BUTTON_JUMP = FALSE;
274 self.bot_canruntogoal = 0;
275 self.bot_timelastseengoal = 0;
279 if(self.waterlevel > WATERLEVEL_WETFEET)
281 self.aistatus &~= AI_STATUS_RUNNING;
285 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
287 self.bot_canruntogoal = 0;
288 self.bot_timelastseengoal = 0;
291 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
292 bunnyhopdistance = vlen(self.origin - gco);
294 // Run only to visible goals
295 if(self.flags & FL_ONGROUND)
296 if(self.speed==maxspeed)
297 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
299 self.bot_lastseengoal = self.goalcurrent;
302 if(self.bot_timelastseengoal)
304 // for a period of time
305 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
308 checkdistance = TRUE;
310 // don't run if it is too close
311 if(self.bot_canruntogoal==0)
313 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
314 self.bot_canruntogoal = 1;
316 self.bot_canruntogoal = -1;
319 if(self.bot_canruntogoal != 1)
322 if(self.aistatus & AI_STATUS_ROAMING)
323 if(self.goalcurrent.classname=="waypoint")
324 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
325 if(fabs(gco_z - self.origin_z) < self.maxs_z - self.mins_z)
326 if(self.goalstack01!=world)
328 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
329 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
330 while (deviation_y < -180) deviation_y = deviation_y + 360;
331 while (deviation_y > 180) deviation_y = deviation_y - 360;
333 if(fabs(deviation_y) < 20)
334 if(bunnyhopdistance < vlen(self.origin - gno))
335 if(fabs(gno_z - gco_z) < self.maxs_z - self.mins_z)
337 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
338 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
340 checkdistance = FALSE;
347 self.aistatus &~= AI_STATUS_RUNNING;
348 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
349 self.BUTTON_JUMP = TRUE;
353 self.aistatus |= AI_STATUS_RUNNING;
354 self.BUTTON_JUMP = TRUE;
360 self.bot_timelastseengoal = time;
365 self.bot_timelastseengoal = 0;
369 // Release jump button
370 if(!cvar("sv_pogostick"))
371 if(self.flags & FL_ONGROUND == 0)
373 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
374 self.BUTTON_JUMP = FALSE;
377 if(self.aistatus & AI_STATUS_RUNNING)
378 if(vlen(self.velocity)>maxspeed)
380 deviation = vectoangles(dir) - vectoangles(self.velocity);
381 while (deviation_y < -180) deviation_y = deviation_y + 360;
382 while (deviation_y > 180) deviation_y = deviation_y - 360;
384 if(fabs(deviation_y)>10)
388 self.movement_y = maxspeed * -1;
389 else if(deviation_y<10)
390 self.movement_y = maxspeed;
397 void havocbot_movetogoal()
405 vector evadeobstacle;
412 //if (self.goalentity)
413 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
414 self.movement = '0 0 0';
415 maxspeed = autocvar_sv_maxspeed;
417 // Jetpack navigation
419 if(self.navigation_jetpack_goal)
420 if(self.goalcurrent==self.navigation_jetpack_goal)
423 if(autocvar_bot_debug_goalstack)
426 te_wizspike(self.navigation_jetpack_point);
430 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
432 // Brake almost completely so it can get a good direction
433 if(vlen(self.velocity)>10)
435 self.aistatus |= AI_STATUS_JETPACK_FLYING;
438 makevectors(self.v_angle_y * '0 1 0');
439 dir = normalize(self.navigation_jetpack_point - self.origin);
442 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
444 // Calculate brake distance in xy
448 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy_z = 0;
450 v = vlen(self.velocity - self.velocity_z * '0 0 1');
451 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
452 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
453 if(d < db || d < 500)
456 if(fabs(self.velocity_x)>maxspeed*0.3)
458 self.movement_x = dir * v_forward * -maxspeed;
461 // Switch to normal mode
462 self.navigation_jetpack_goal = world;
463 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
464 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
468 else if(checkpvs(self.origin,self.goalcurrent))
470 // If I can see the goal switch to landing code
471 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
472 self.aistatus |= AI_STATUS_JETPACK_LANDING;
477 self.BUTTON_HOOK = TRUE;
478 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
480 self.movement_x = dir * v_forward * maxspeed;
481 self.movement_y = dir * v_right * maxspeed;
486 // Handling of jump pads
487 if(self.jumppadcount)
489 // If got stuck on the jump pad try to reach the farthest visible waypoint
490 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
492 if(fabs(self.velocity_z)<50)
494 entity head, newgoal = world;
495 float distance, bestdistance = 0;
497 for (head = findchain(classname, "waypoint"); head; head = head.chain)
500 distance = vlen(head.origin - self.origin);
504 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), TRUE, world);
509 if(distance>bestdistance)
512 bestdistance = distance;
518 self.ignoregoal = self.goalcurrent;
519 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
520 navigation_clearroute();
521 navigation_routetogoal(newgoal, self.origin);
522 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
530 if(self.velocity_z>0)
532 float threshold, sxy;
533 vector velxy = self.velocity; velxy_z = 0;
535 threshold = maxspeed * 0.2;
538 dprint("Warning: ", self.netname, " got stuck on a jumppad (velocity in xy is ", ftos(sxy), "), trying to get out of it now\n");
539 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
544 // Don't chase players while using a jump pad
545 if(self.goalcurrent.classname=="player" || self.goalstack01.classname=="player")
549 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
550 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
552 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
554 if not(self.flags & FL_ONGROUND)
556 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
557 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
558 if(self.items & IT_JETPACK)
560 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
561 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
563 if(self.velocity_z<0)
565 self.BUTTON_HOOK = TRUE;
569 self.BUTTON_HOOK = TRUE;
571 // If there is no goal try to move forward
573 if(self.goalcurrent==world)
576 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
578 vector xyvelocity = self.velocity; xyvelocity_z = 0;
579 float xyspeed = xyvelocity * dir;
581 if(xyspeed < (maxspeed / 2))
583 makevectors(self.v_angle_y * '0 1 0');
584 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
585 if(trace_fraction==1)
587 self.movement_x = dir * v_forward * maxspeed;
588 self.movement_y = dir * v_right * maxspeed;
590 havocbot_keyboard_movement(self.origin + dir * 100);
594 self.havocbot_blockhead = TRUE;
598 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
600 if(self.velocity_z < 0)
601 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
603 self.movement_x = maxspeed;
605 if(self.rocketjumptime)
607 if(time > self.rocketjumptime)
609 self.BUTTON_ATCK2 = TRUE;
610 self.rocketjumptime = 0;
615 self.switchweapon = WEP_ROCKET_LAUNCHER;
617 self.BUTTON_ATCK = TRUE;
618 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
624 // If there is no goal try to move forward
625 if(self.goalcurrent==world)
626 self.movement_x = maxspeed;
630 // If we are under water with no goals, swim up
632 if(self.goalcurrent==world)
635 if(self.waterlevel>WATERLEVEL_SWIMMING)
637 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
638 self.BUTTON_JUMP = TRUE;
640 self.BUTTON_JUMP = FALSE;
641 makevectors(self.v_angle_y * '0 1 0');
642 self.movement_x = dir * v_forward * maxspeed;
643 self.movement_y = dir * v_right * maxspeed;
644 self.movement_z = dir * v_up * maxspeed;
647 // if there is nowhere to go, exit
648 if (self.goalcurrent == world)
651 if (self.goalcurrent)
652 navigation_poptouchedgoals();
654 // if ran out of goals try to use an alternative goal or get a new strategy asap
655 if(self.goalcurrent == world)
657 self.bot_strategytime = 0;
662 if(autocvar_bot_debug_goalstack)
665 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
666 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
667 destorg = self.origin;
668 destorg_x = bound(m1_x, destorg_x, m2_x);
669 destorg_y = bound(m1_y, destorg_y, m2_y);
670 destorg_z = bound(m1_z, destorg_z, m2_z);
671 diff = destorg - self.origin;
673 dir = normalize(diff);
674 flatdir = diff;flatdir_z = 0;
675 flatdir = normalize(flatdir);
676 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
678 //if (self.bot_dodgevector_time < time)
680 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
681 // self.bot_dodgevector_jumpbutton = 1;
682 evadeobstacle = '0 0 0';
687 if(self.waterlevel>WATERLEVEL_SWIMMING)
690 self.aistatus |= AI_STATUS_OUT_WATER;
694 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && gco_z < self.origin_z) &&
695 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
696 self.BUTTON_JUMP = TRUE;
698 self.BUTTON_JUMP = FALSE;
700 dir = normalize(flatdir);
701 makevectors(self.v_angle_y * '0 1 0');
705 if(self.aistatus & AI_STATUS_OUT_WATER)
706 self.aistatus &~= AI_STATUS_OUT_WATER;
708 // jump if going toward an obstacle that doesn't look like stairs we
709 // can walk up directly
710 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
711 if (trace_fraction < 1)
712 if (trace_plane_normal_z < 0.7)
715 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, FALSE, self);
716 if (trace_fraction < s + 0.01)
717 if (trace_plane_normal_z < 0.7)
720 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, FALSE, self);
721 if (trace_fraction > s)
722 self.BUTTON_JUMP = 1;
726 // avoiding dangers and obstacles
727 vector dst_ahead, dst_down;
728 makevectors(self.v_angle_y * '0 1 0');
729 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
730 dst_down = dst_ahead + '0 0 -1500';
733 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
735 // Check head-banging against walls
736 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
738 self.BUTTON_JUMP = TRUE;
739 if(self.facingwalltime && time > self.facingwalltime)
741 self.ignoregoal = self.goalcurrent;
742 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
743 self.bot_strategytime = 0;
748 self.facingwalltime = time + 0.05;
753 self.facingwalltime = 0;
755 if(self.ignoregoal != world && time > self.ignoregoaltime)
757 self.ignoregoal = world;
758 self.ignoregoaltime = 0;
762 // Check for water/slime/lava and dangerous edges
763 // (only when the bot is on the ground or jumping intentionally)
764 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
766 if(trace_fraction == 1 && self.jumppadcount == 0 && !self.goalcurrent.wphardwired )
767 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
770 traceline(dst_ahead , dst_down, TRUE, world);
771 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
772 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
773 if(trace_endpos_z < self.origin_z + self.mins_z)
775 s = pointcontents(trace_endpos + '0 0 1');
776 if (s != CONTENT_SOLID)
777 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
778 evadelava = normalize(self.velocity) * -1;
779 else if (s == CONTENT_SKY)
780 evadeobstacle = normalize(self.velocity) * -1;
781 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
782 self.goalcurrent.absmin, self.goalcurrent.absmax))
784 // if ain't a safe goal with "holes" (like the jumpad on soylent)
785 // and there is a trigger_hurt below
786 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
788 // Remove dangerous dynamic goals from stack
789 dprint("bot ", self.netname, " avoided the goal ", self.goalcurrent.classname, " ", etos(self.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
790 navigation_clearroute();
800 makevectors(self.v_angle_y * '0 1 0');
802 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
803 self.aistatus |= AI_STATUS_DANGER_AHEAD;
806 dodge = havocbot_dodge();
807 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
808 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
809 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), TRUE, world);
810 if(trace_ent.classname == "player")
811 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
813 dir = normalize(dir + dodge + evadeobstacle + evadelava);
814 // self.bot_dodgevector = dir;
815 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
818 if(time < self.ladder_time)
820 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
822 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
827 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
832 //dir = self.bot_dodgevector;
833 //if (self.bot_dodgevector_jumpbutton)
834 // self.BUTTON_JUMP = 1;
835 self.movement_x = dir * v_forward * maxspeed;
836 self.movement_y = dir * v_right * maxspeed;
837 self.movement_z = dir * v_up * maxspeed;
839 // Emulate keyboard interface
841 havocbot_keyboard_movement(destorg);
844 // if(self.aistatus & AI_STATUS_ROAMING)
846 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
847 havocbot_bunnyhop(dir);
849 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
850 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
851 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);
854 void havocbot_chooseenemy()
856 entity head, best, head2;
857 float rating, bestrating, i, hf;
859 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
866 if (!bot_shouldattack(self.enemy))
868 // enemy died or something, find a new target
870 self.havocbot_chooseenemy_finished = time;
872 else if (self.havocbot_stickenemy)
874 // tracking last chosen enemy
875 // if enemy is visible
876 // and not really really far away
877 // and we're not severely injured
878 // then keep tracking for a half second into the future
879 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,FALSE,world);
880 if (trace_ent == self.enemy || trace_fraction == 1)
881 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
882 if (self.health > 30)
884 // remain tracking him for a shot while (case he went after a small corner or pilar
885 self.havocbot_chooseenemy_finished = time + 0.5;
888 // enemy isn't visible, or is far away, or we're injured severely
889 // so stop preferring this enemy
890 // (it will still take a half second until a new one is chosen)
891 self.havocbot_stickenemy = 0;
894 if (time < self.havocbot_chooseenemy_finished)
896 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
897 eye = self.origin + self.view_ofs;
899 bestrating = 100000000;
900 head = head2 = findchainfloat(bot_attack, TRUE);
903 hf = self.dphitcontentsmask;
905 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
907 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
913 v = (head.absmin + head.absmax) * 0.5;
914 rating = vlen(v - eye);
915 if (rating<autocvar_bot_ai_enemydetectionradius)
916 if (bestrating > rating)
917 if (bot_shouldattack(head))
919 traceline(eye, v, TRUE, self);
920 if (trace_ent == head || trace_fraction >= 1)
929 // I want to do a second scan if no enemy was found or I don't have weapons
930 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
931 if(best || !WEPSET_EMPTY_E(self)) // || self.weapon == WEP_RIFLE
936 // Set flags to see through transparent objects
937 self.dphitcontentsmask |= DPCONTENTS_OPAQUE;
943 self.dphitcontentsmask = hf;
946 self.havocbot_stickenemy = TRUE;
949 float havocbot_chooseweapon_checkreload(float new_weapon)
951 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
952 // so skip this for them, or they'll never get to reload their weapons at all.
953 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
957 // if this weapon is scheduled for reloading, don't switch to it during combat
958 if (self.weapon_load[new_weapon] < 0)
960 float i, other_weapon_available = FALSE;
961 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
963 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
964 if (weapon_action(i, WR_CHECKAMMO1) + weapon_action(i, WR_CHECKAMMO2))
965 other_weapon_available = TRUE;
967 if(other_weapon_available)
974 void havocbot_chooseweapon()
979 if(WEPSET_EQ_AW(g_weaponarena_weapons, WEP_TUBA))
981 self.switchweapon = WEP_TUBA;
985 // TODO: clean this up by moving it to weapon code
986 if(self.enemy==world)
988 // If no weapon was chosen get the first available weapon
990 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
992 if(client_hasweapon(self, i, TRUE, FALSE))
994 self.switchweapon = i;
1001 // Do not change weapon during the next second after a combo
1002 i = time - self.lastcombotime;
1007 float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1009 // Should it do a weapon combo?
1010 float af, ct, combo_time, combo;
1012 af = ATTACK_FINISHED(self);
1013 ct = autocvar_bot_ai_weapon_combo_threshold;
1015 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1016 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1017 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1021 if(autocvar_bot_ai_weapon_combo)
1022 if(self.weapon == self.lastfiredweapon)
1026 self.lastcombotime = time;
1029 distance *= pow(2, self.bot_rangepreference);
1031 // Custom weapon list based on distance to the enemy
1032 if(bot_custom_weapon){
1034 // Choose weapons for far distance
1035 if ( distance > bot_distance_far ) {
1036 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1037 w = bot_weapons_far[i];
1038 if ( client_hasweapon(self, w, TRUE, FALSE) )
1040 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1042 self.switchweapon = w;
1048 // Choose weapons for mid distance
1049 if ( distance > bot_distance_close) {
1050 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1051 w = bot_weapons_mid[i];
1052 if ( client_hasweapon(self, w, TRUE, FALSE) )
1054 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1056 self.switchweapon = w;
1062 // Choose weapons for close distance
1063 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1064 w = bot_weapons_close[i];
1065 if ( client_hasweapon(self, w, TRUE, FALSE) )
1067 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1069 self.switchweapon = w;
1078 vector selfvel, enemyvel;
1079 // if(self.flags & FL_INWATER)
1081 if (time < self.nextaim)
1083 self.nextaim = time + 0.1;
1084 selfvel = self.velocity;
1085 if (!self.waterlevel)
1089 enemyvel = self.enemy.velocity;
1090 if (!self.enemy.waterlevel)
1092 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1095 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1098 float havocbot_moveto_refresh_route()
1100 // Refresh path to goal if necessary
1102 wp = self.havocbot_personal_waypoint;
1103 navigation_goalrating_start();
1104 navigation_routerating(wp, 10000, 10000);
1105 navigation_goalrating_end();
1106 return self.navigation_hasgoals;
1109 float havocbot_moveto(vector pos)
1113 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1115 // Step 4: Move to waypoint
1116 if(self.havocbot_personal_waypoint==world)
1118 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1119 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1120 return CMD_STATUS_ERROR;
1123 if (!bot_strategytoken_taken)
1124 if(self.havocbot_personal_waypoint_searchtime<time)
1126 bot_strategytoken_taken = TRUE;
1127 if(havocbot_moveto_refresh_route())
1129 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1130 self.havocbot_personal_waypoint_searchtime = time + 10;
1131 self.havocbot_personal_waypoint_failcounter = 0;
1135 self.havocbot_personal_waypoint_failcounter += 1;
1136 self.havocbot_personal_waypoint_searchtime = time + 2;
1137 if(self.havocbot_personal_waypoint_failcounter >= 30)
1139 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1140 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1141 remove(self.havocbot_personal_waypoint);
1142 return CMD_STATUS_ERROR;
1145 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1149 if(autocvar_bot_debug_goalstack)
1153 vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1155 bot_aimdir(dir, -1);
1158 havocbot_movetogoal();
1160 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1162 // Step 5: Waypoint reached
1163 dprint(self.netname, "'s personal waypoint reached\n");
1164 remove(self.havocbot_personal_waypoint);
1165 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1166 return CMD_STATUS_FINISHED;
1169 return CMD_STATUS_EXECUTING;
1172 // Step 2: Linking waypoint
1173 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1175 // Wait until it is linked
1176 if(!self.havocbot_personal_waypoint.wplinked)
1178 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1179 return CMD_STATUS_EXECUTING;
1182 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1183 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1184 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1186 // Step 3: Route to waypoint
1187 dprint(self.netname, " walking to its personal waypoint\n");
1189 return CMD_STATUS_EXECUTING;
1192 // Step 1: Spawning waypoint
1193 wp = waypoint_spawnpersonal(pos);
1196 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1197 return CMD_STATUS_ERROR;
1200 self.havocbot_personal_waypoint = wp;
1201 self.havocbot_personal_waypoint_failcounter = 0;
1202 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1204 // if pos is inside a teleport, then let's mark it as teleport waypoint
1206 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1208 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1210 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1211 self.lastteleporttime = 0;
1216 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1217 print("routing to a teleporter\n");
1219 print("routing to a non-teleporter\n");
1222 return CMD_STATUS_EXECUTING;
1225 float havocbot_resetgoal()
1227 navigation_clearroute();
1228 return CMD_STATUS_FINISHED;
1231 void havocbot_setupbot()
1233 self.bot_ai = havocbot_ai;
1234 self.cmd_moveto = havocbot_moveto;
1235 self.cmd_resetgoal = havocbot_resetgoal;
1237 havocbot_chooserole();
1240 vector havocbot_dodge()
1242 // LordHavoc: disabled because this is too expensive
1247 float danger, bestdanger, vl, d;
1250 // check for dangerous objects near bot or approaching bot
1251 head = findchainfloat(bot_dodge, TRUE);
1254 if (head.owner != self)
1256 vl = vlen(head.velocity);
1257 if (vl > autocvar_sv_maxspeed * 0.3)
1259 n = normalize(head.velocity);
1260 v = self.origin - head.origin;
1262 if (d > (0 - head.bot_dodgerating))
1263 if (d < (vl * 0.2 + head.bot_dodgerating))
1265 // calculate direction and distance from the flight path, by removing the forward axis
1266 v = v - (n * (v * n));
1267 danger = head.bot_dodgerating - vlen(v);
1268 if (bestdanger < danger)
1270 bestdanger = danger;
1271 // dodge to the side of the object
1272 dodge = normalize(v);
1278 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1279 if (bestdanger < danger)
1281 bestdanger = danger;
1282 dodge = normalize(self.origin - head.origin);