3 #include "role_onslaught.qc"
4 #include "role_keyhunt.qc"
12 if(bot_execute_commands())
15 if (bot_strategytoken == self)
16 if (!bot_strategytoken_taken)
18 if(self.havocbot_blockhead)
20 self.havocbot_blockhead = FALSE;
24 if not(self.jumppadcount)
28 // TODO: tracewalk() should take care of this job (better path finding under water)
29 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
30 if(self.deadflag != DEAD_NO)
31 if(self.goalcurrent==world)
32 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
34 // Look for the closest waypoint out of water
35 local entity newgoal, head;
36 local float bestdistance, distance;
40 for (head = findchain(classname, "waypoint"); head; head = head.chain)
42 distance = vlen(head.origin - self.origin);
46 if(head.origin_z < self.origin_z)
49 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
52 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
55 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
60 if(distance<bestdistance)
63 bestdistance = distance;
69 // te_wizspike(newgoal.origin);
70 navigation_pushroute(newgoal);
74 // token has been used this frame
75 bot_strategytoken_taken = TRUE;
78 if(self.deadflag != DEAD_NO)
81 havocbot_chooseenemy();
82 if (self.bot_chooseweapontime < time )
84 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
85 havocbot_chooseweapon();
91 self.aistatus |= AI_STATUS_ATTACKING;
92 self.aistatus &~= AI_STATUS_ROAMING;
96 weapon_action(self.weapon, WR_AIM);
97 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
99 self.BUTTON_ATCK = FALSE;
100 self.BUTTON_ATCK2 = FALSE;
104 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
105 self.lastfiredweapon = self.weapon;
110 if(self.bot_aimtarg.classname=="player")
111 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
114 else if (self.goalcurrent)
116 self.aistatus |= AI_STATUS_ROAMING;
117 self.aistatus &~= AI_STATUS_ATTACKING;
119 local vector now,v,next;//,heading;
120 local float aimdistance,skillblend,distanceblend,blend;
121 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
122 aimdistance = vlen(now);
123 //heading = self.velocity;
124 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
126 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
127 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
129 next = self.goalstack01.origin - (self.origin + self.view_ofs);
131 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
132 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
133 blend = skillblend * (1-distanceblend);
134 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
135 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
136 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
137 v = now + blend * (next - now);
138 //dprint(etos(self), " ");
139 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
140 //v = now * (distanceblend) + next * (1-distanceblend);
141 if (self.waterlevel < WATERLEVEL_SWIMMING)
143 //dprint("walk at:", vtos(v), "\n");
144 //te_lightning2(world, self.origin, self.goalcurrent.origin);
147 havocbot_movetogoal();
150 void havocbot_keyboard_movement(vector destorg)
152 local vector keyboard;
153 local float blend, maxspeed;
156 sk = skill + self.bot_moveskill;
158 maxspeed = autocvar_sv_maxspeed;
160 if (time < self.havocbot_keyboardtime)
163 self.havocbot_keyboardtime =
165 self.havocbot_keyboardtime
166 + 0.05/max(1, sk+self.havocbot_keyboardskill)
167 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
169 keyboard = self.movement * (1.0 / maxspeed);
171 local float trigger, trigger1;
172 blend = bound(0,sk*0.1,1);
173 trigger = autocvar_bot_ai_keyboard_threshold;
174 trigger1 = 0 - trigger;
176 // categorize forward movement
177 // at skill < 1.5 only forward
178 // at skill < 2.5 only individual directions
179 // at skill < 4.5 only individual directions, and forward diagonals
180 // at skill >= 4.5, all cases allowed
181 if (keyboard_x > trigger)
187 else if (keyboard_x < trigger1 && sk > 1.5)
202 if (keyboard_y > trigger)
204 else if (keyboard_y < trigger1)
209 if (keyboard_z > trigger)
211 else if (keyboard_z < trigger1)
216 self.havocbot_keyboard = keyboard * maxspeed;
217 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
219 keyboard = self.havocbot_keyboard;
220 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
221 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
222 self.movement = self.movement + (keyboard - self.movement) * blend;
225 void havocbot_bunnyhop(vector dir)
227 local float bunnyhopdistance;
228 local vector deviation;
229 local float maxspeed;
231 if(autocvar_g_midair)
234 // Don't jump when using some weapons
236 if(self.aistatus & AI_STATUS_ATTACKING)
237 if(self.weapon == WEP_SNIPERRIFLE)
240 if(self.goalcurrent.classname == "player")
244 maxspeed = autocvar_sv_maxspeed;
246 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
248 self.aistatus &~= AI_STATUS_RUNNING;
249 self.BUTTON_JUMP = FALSE;
250 self.bot_canruntogoal = 0;
251 self.bot_timelastseengoal = 0;
255 if(self.waterlevel > WATERLEVEL_WETFEET)
257 self.aistatus &~= AI_STATUS_RUNNING;
261 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
263 self.bot_canruntogoal = 0;
264 self.bot_timelastseengoal = 0;
267 bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
269 // Run only to visible goals
270 if(self.flags & FL_ONGROUND)
271 if(self.speed==maxspeed)
272 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
274 self.bot_lastseengoal = self.goalcurrent;
277 if(self.bot_timelastseengoal)
279 // for a period of time
280 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
282 local float checkdistance;
283 checkdistance = TRUE;
285 // don't run if it is too close
286 if(self.bot_canruntogoal==0)
288 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
289 self.bot_canruntogoal = 1;
291 self.bot_canruntogoal = -1;
294 if(self.bot_canruntogoal != 1)
297 if(self.aistatus & AI_STATUS_ROAMING)
298 if(self.goalcurrent.classname=="waypoint")
299 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
300 if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
301 if(self.goalstack01!=world)
303 deviation = vectoangles(self.goalstack01.origin - self.origin) - vectoangles(self.goalcurrent.origin - self.origin);
304 while (deviation_y < -180) deviation_y = deviation_y + 360;
305 while (deviation_y > 180) deviation_y = deviation_y - 360;
307 if(fabs(deviation_y) < 20)
308 if(bunnyhopdistance < vlen(self.origin - self.goalstack01.origin))
309 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
311 if(vlen(self.goalcurrent.origin - self.goalstack01.origin) > autocvar_bot_ai_bunnyhop_startdistance)
312 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
314 checkdistance = FALSE;
321 self.aistatus &~= AI_STATUS_RUNNING;
322 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
323 self.BUTTON_JUMP = TRUE;
327 self.aistatus |= AI_STATUS_RUNNING;
328 self.BUTTON_JUMP = TRUE;
334 self.bot_timelastseengoal = time;
339 self.bot_timelastseengoal = 0;
342 // Release jump button
343 if(!cvar("sv_pogostick"))
344 if(self.flags & FL_ONGROUND == 0)
346 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
347 self.BUTTON_JUMP = FALSE;
350 if(self.aistatus & AI_STATUS_RUNNING)
351 if(vlen(self.velocity)>maxspeed)
353 deviation = vectoangles(dir) - vectoangles(self.velocity);
354 while (deviation_y < -180) deviation_y = deviation_y + 360;
355 while (deviation_y > 180) deviation_y = deviation_y - 360;
357 if(fabs(deviation_y)>10)
361 self.movement_y = maxspeed * -1;
362 else if(deviation_y<10)
363 self.movement_y = maxspeed;
369 void havocbot_movetogoal()
371 local vector destorg;
374 local vector flatdir;
377 local vector evadeobstacle;
378 local vector evadelava;
380 local float maxspeed;
383 //if (self.goalentity)
384 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
385 self.movement = '0 0 0';
386 maxspeed = autocvar_sv_maxspeed;
388 // Jetpack navigation
390 if(self.navigation_jetpack_goal)
391 if(self.goalcurrent==self.navigation_jetpack_goal)
394 #ifdef DEBUG_BOT_GOALSTACK
396 te_wizspike(self.navigation_jetpack_point);
400 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
402 // Brake almost completely so it can get a good direction
403 if(vlen(self.velocity)>10)
405 self.aistatus |= AI_STATUS_JETPACK_FLYING;
408 makevectors(self.v_angle_y * '0 1 0');
409 dir = normalize(self.navigation_jetpack_point - self.origin);
412 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
414 // Calculate brake distance in xy
418 dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
420 v = vlen(self.velocity - self.velocity_z * '0 0 1');
421 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
422 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
423 if(d < db || d < 500)
426 if(fabs(self.velocity_x)>maxspeed*0.3)
428 self.movement_x = dir * v_forward * -maxspeed;
431 // Switch to normal mode
432 self.navigation_jetpack_goal = world;
433 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
434 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
438 else if(checkpvs(self.origin,self.goalcurrent))
440 // If I can see the goal switch to landing code
441 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
442 self.aistatus |= AI_STATUS_JETPACK_LANDING;
447 self.BUTTON_HOOK = TRUE;
448 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
450 self.movement_x = dir * v_forward * maxspeed;
451 self.movement_y = dir * v_right * maxspeed;
456 // Handling of jump pads
457 if(self.jumppadcount)
459 // If got stuck on the jump pad try to reach the farthest visible item
460 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
462 if(fabs(self.velocity_z)<50)
464 local entity head, newgoal;
465 local float distance, bestdistance;
467 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
469 if(head.classname=="worldspawn")
472 distance = vlen(head.origin - self.origin);
476 traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
481 if(distance>bestdistance)
484 bestdistance = distance;
490 self.ignoregoal = self.goalcurrent;
491 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
492 navigation_clearroute();
493 navigation_routetogoal(newgoal, self.origin);
494 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
502 if(self.velocity_z>0)
504 local float threshold;
505 threshold = maxspeed * 0.2;
506 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
508 dprint("Warning: ", self.netname, " got stuck on a jumppad, trying to get out of it now\n");
509 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
514 // Don't chase players while using a jump pad
515 if(self.goalcurrent.classname=="player" || self.goalstack01.classname=="player")
519 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
520 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
522 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
524 if not(self.flags & FL_ONGROUND)
526 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
527 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
528 if(self.items & IT_JETPACK)
530 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
531 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
533 if(self.velocity_z<0)
535 self.BUTTON_HOOK = TRUE;
539 self.BUTTON_HOOK = TRUE;
541 // If there is no goal try to move forward
543 if(self.goalcurrent==world)
546 dir = normalize(self.goalcurrent.origin - self.origin);
548 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
549 local float xyspeed = xyvelocity * dir;
551 if(xyspeed < (maxspeed / 2))
553 makevectors(self.v_angle_y * '0 1 0');
554 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
555 if(trace_fraction==1)
557 self.movement_x = dir * v_forward * maxspeed;
558 self.movement_y = dir * v_right * maxspeed;
560 havocbot_keyboard_movement(self.origin + dir * 100);
564 self.havocbot_blockhead = TRUE;
568 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
570 if(self.velocity_z < 0)
571 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
573 self.movement_x = maxspeed;
575 if(self.rocketjumptime)
577 if(time > self.rocketjumptime)
579 self.BUTTON_ATCK2 = TRUE;
580 self.rocketjumptime = 0;
585 self.switchweapon = WEP_ROCKET_LAUNCHER;
587 self.BUTTON_ATCK = TRUE;
588 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
594 // If there is no goal try to move forward
595 if(self.goalcurrent==world)
596 self.movement_x = maxspeed;
600 // If we are under water with no goals, swim up
602 if(self.goalcurrent==world)
605 if(self.waterlevel>WATERLEVEL_SWIMMING)
607 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
608 self.BUTTON_JUMP = TRUE;
610 self.BUTTON_JUMP = FALSE;
611 makevectors(self.v_angle_y * '0 1 0');
612 self.movement_x = dir * v_forward * maxspeed;
613 self.movement_y = dir * v_right * maxspeed;
614 self.movement_z = dir * v_up * maxspeed;
617 // if there is nowhere to go, exit
618 if (self.goalcurrent == world)
621 if (self.goalcurrent)
622 navigation_poptouchedgoals();
624 // if ran out of goals try to use an alternative goal or get a new strategy asap
625 if(self.goalcurrent == world)
627 self.bot_strategytime = 0;
631 #ifdef DEBUG_BOT_GOALSTACK
635 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
636 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
637 destorg = self.origin;
638 destorg_x = bound(m1_x, destorg_x, m2_x);
639 destorg_y = bound(m1_y, destorg_y, m2_y);
640 destorg_z = bound(m1_z, destorg_z, m2_z);
641 diff = destorg - self.origin;
643 dir = normalize(diff);
644 flatdir = diff;flatdir_z = 0;
645 flatdir = normalize(flatdir);
647 //if (self.bot_dodgevector_time < time)
649 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
650 // self.bot_dodgevector_jumpbutton = 1;
651 evadeobstacle = '0 0 0';
656 if(self.waterlevel>WATERLEVEL_SWIMMING)
659 self.aistatus |= AI_STATUS_OUT_WATER;
663 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
664 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
665 self.BUTTON_JUMP = TRUE;
667 self.BUTTON_JUMP = FALSE;
669 dir = normalize(flatdir);
670 makevectors(self.v_angle_y * '0 1 0');
674 if(self.aistatus & AI_STATUS_OUT_WATER)
675 self.aistatus &~= AI_STATUS_OUT_WATER;
677 // jump if going toward an obstacle that doesn't look like stairs we
678 // can walk up directly
679 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
680 if (trace_fraction < 1)
681 if (trace_plane_normal_z < 0.7)
684 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
685 if (trace_fraction < s + 0.01)
686 if (trace_plane_normal_z < 0.7)
689 tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
690 if (trace_fraction > s)
691 self.BUTTON_JUMP = 1;
695 // avoiding dangers and obstacles
696 local vector dst_ahead, dst_down;
697 makevectors(self.v_angle_y * '0 1 0');
698 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
699 dst_down = dst_ahead + '0 0 -1500';
702 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
704 // Check head-banging against walls
705 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
707 self.BUTTON_JUMP = TRUE;
708 if(self.facingwalltime && time > self.facingwalltime)
710 self.ignoregoal = self.goalcurrent;
711 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
712 self.bot_strategytime = 0;
717 self.facingwalltime = time + 0.05;
722 self.facingwalltime = 0;
724 if(self.ignoregoal != world && time > self.ignoregoaltime)
726 self.ignoregoal = world;
727 self.ignoregoaltime = 0;
731 // Check for water/slime/lava and dangerous edges
732 // (only when the bot is on the ground or jumping intentionally)
733 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
735 if(trace_fraction == 1 && self.jumppadcount == 0)
736 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
739 traceline(dst_ahead , dst_down, TRUE, world);
740 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
741 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
742 if(trace_endpos_z < self.origin_z + self.mins_z)
744 s = pointcontents(trace_endpos + '0 0 1');
745 if (s != CONTENT_SOLID)
746 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
747 evadelava = normalize(self.velocity) * -1;
748 else if (s == CONTENT_SKY)
749 evadeobstacle = normalize(self.velocity) * -1;
750 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
751 self.goalcurrent.absmin, self.goalcurrent.absmax))
753 // if ain't a safe goal with "holes" (like the jumpad on soylent)
754 // and there is a trigger_hurt below
755 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
757 // Remove dangerous dynamic goals from stack
758 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
759 navigation_poproute();
762 evadeobstacle = normalize(self.velocity) * -1;
771 makevectors(self.v_angle_y * '0 1 0');
773 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
774 self.aistatus |= AI_STATUS_DANGER_AHEAD;
777 dodge = havocbot_dodge();
778 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
779 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
780 traceline(self.origin, self.enemy.origin, TRUE, world);
781 if(trace_ent.classname == "player")
782 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
784 dir = normalize(dir + dodge + evadeobstacle + evadelava);
785 // self.bot_dodgevector = dir;
786 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
789 if(time < self.ladder_time)
791 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
793 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
798 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
803 //dir = self.bot_dodgevector;
804 //if (self.bot_dodgevector_jumpbutton)
805 // self.BUTTON_JUMP = 1;
806 self.movement_x = dir * v_forward * maxspeed;
807 self.movement_y = dir * v_right * maxspeed;
808 self.movement_z = dir * v_up * maxspeed;
810 // Emulate keyboard interface
812 havocbot_keyboard_movement(destorg);
815 // if(self.aistatus & AI_STATUS_ROAMING)
817 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
818 havocbot_bunnyhop(dir);
820 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
821 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
822 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);
825 void havocbot_chooseenemy()
827 local entity head, best, head2;
828 local float rating, bestrating, i, f;
830 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
837 if (!bot_shouldattack(self.enemy))
839 // enemy died or something, find a new target
841 self.havocbot_chooseenemy_finished = time;
843 else if (self.havocbot_stickenemy)
845 // tracking last chosen enemy
846 // if enemy is visible
847 // and not really really far away
848 // and we're not severely injured
849 // then keep tracking for a half second into the future
850 traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
851 if (trace_ent == self.enemy || trace_fraction == 1)
852 if (vlen(self.enemy.origin - self.origin) < 1000)
853 if (self.health > 30)
855 // remain tracking him for a shot while (case he went after a small corner or pilar
856 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
859 // enemy isn't visible, or is far away, or we're injured severely
860 // so stop preferring this enemy
861 // (it will still take a half second until a new one is chosen)
862 self.havocbot_stickenemy = 0;
865 if (time < self.havocbot_chooseenemy_finished)
867 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
868 eye = self.origin + self.view_ofs;
870 bestrating = 100000000;
871 head = head2 = findchainfloat(bot_attack, TRUE);
873 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
878 v = (head.absmin + head.absmax) * 0.5;
879 rating = vlen(v - eye);
880 if (rating<autocvar_bot_ai_enemydetectionradius)
881 if (bestrating > rating)
882 if (bot_shouldattack(head))
884 traceline(eye, v, TRUE, self);
885 if (trace_ent == head || trace_fraction >= 1)
894 // I want to do a second scan if no enemy was found or I don't have weapons
895 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
896 if(best || self.weapons) // || self.weapon == WEP_SNIPERRIFLE
901 // Set flags to see through transparent objects
902 f = self.dphitcontentsmask;
903 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
909 // Restore hit flags if needed
911 self.dphitcontentsmask = f;
914 self.havocbot_stickenemy = TRUE;
917 void havocbot_chooseweapon()
922 if(g_weaponarena == WEPBIT_TUBA)
924 self.switchweapon = WEP_TUBA;
928 // TODO: clean this up by moving it to weapon code
929 if(self.enemy==world)
931 // If no weapon was chosen get the first available weapon
933 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
935 if(client_hasweapon(self, i, TRUE, FALSE))
937 self.switchweapon = i;
944 // Do not change weapon during the next second after a combo
945 i = time - self.lastcombotime;
950 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
952 // Should it do a weapon combo?
953 local float af, ct, combo_time, combo;
955 af = ATTACK_FINISHED(self);
956 ct = autocvar_bot_ai_weapon_combo_threshold;
958 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
959 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
960 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
964 if(autocvar_bot_ai_weapon_combo)
965 if(self.weapon == self.lastfiredweapon)
969 self.lastcombotime = time;
972 distance *= pow(2, self.bot_rangepreference);
974 // Custom weapon list based on distance to the enemy
975 if(bot_custom_weapon){
977 // Choose weapons for far distance
978 if ( distance > bot_distance_far ) {
979 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
980 w = bot_weapons_far[i];
981 if ( client_hasweapon(self, w, TRUE, FALSE) ){
982 if ( self.weapon == w && combo)
984 self.switchweapon = w;
990 // Choose weapons for mid distance
991 if ( distance > bot_distance_close) {
992 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
993 w = bot_weapons_mid[i];
994 if ( client_hasweapon(self, w, TRUE, FALSE) ){
995 if ( self.weapon == w && combo)
997 self.switchweapon = w;
1003 // Choose weapons for close distance
1004 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1005 w = bot_weapons_close[i];
1006 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1007 if ( self.weapon == w && combo)
1009 self.switchweapon = w;
1018 local vector selfvel, enemyvel;
1019 // if(self.flags & FL_INWATER)
1021 if (time < self.nextaim)
1023 self.nextaim = time + 0.1;
1024 selfvel = self.velocity;
1025 if (!self.waterlevel)
1029 enemyvel = self.enemy.velocity;
1030 if (!self.enemy.waterlevel)
1032 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1035 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1038 float havocbot_moveto_refresh_route()
1040 // Refresh path to goal if necessary
1042 wp = self.havocbot_personal_waypoint;
1043 navigation_goalrating_start();
1044 navigation_routerating(wp, 10000, 10000);
1045 navigation_goalrating_end();
1046 return self.navigation_hasgoals;
1049 float havocbot_moveto(vector pos)
1053 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1055 // Step 4: Move to waypoint
1056 if(self.havocbot_personal_waypoint==world)
1058 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1059 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1060 return CMD_STATUS_ERROR;
1063 if (!bot_strategytoken_taken)
1064 if(self.havocbot_personal_waypoint_searchtime<time)
1066 bot_strategytoken_taken = TRUE;
1067 if(havocbot_moveto_refresh_route())
1069 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1070 self.havocbot_personal_waypoint_searchtime = time + 10;
1071 self.havocbot_personal_waypoint_failcounter = 0;
1075 self.havocbot_personal_waypoint_failcounter += 1;
1076 self.havocbot_personal_waypoint_searchtime = time + 2;
1077 if(self.havocbot_personal_waypoint_failcounter >= 30)
1079 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1080 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1081 remove(self.havocbot_personal_waypoint);
1082 return CMD_STATUS_ERROR;
1085 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1089 #ifdef DEBUG_BOT_GOALSTACK
1094 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1096 bot_aimdir(dir, -1);
1099 havocbot_movetogoal();
1101 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1103 // Step 5: Waypoint reached
1104 dprint(self.netname, "'s personal waypoint reached\n");
1105 remove(self.havocbot_personal_waypoint);
1106 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1107 return CMD_STATUS_FINISHED;
1110 return CMD_STATUS_EXECUTING;
1113 // Step 2: Linking waypoint
1114 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1116 // Wait until it is linked
1117 if(!self.havocbot_personal_waypoint.wplinked)
1119 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1120 return CMD_STATUS_EXECUTING;
1123 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1124 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1125 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1127 // Step 3: Route to waypoint
1128 dprint(self.netname, " walking to its personal waypoint\n");
1130 return CMD_STATUS_EXECUTING;
1133 // Step 1: Spawning waypoint
1134 wp = waypoint_spawnpersonal(pos);
1137 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1138 return CMD_STATUS_ERROR;
1141 self.havocbot_personal_waypoint = wp;
1142 self.havocbot_personal_waypoint_failcounter = 0;
1143 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1145 // if pos is inside a teleport, then let's mark it as teleport waypoint
1147 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1149 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1151 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1152 self.lastteleporttime = 0;
1157 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1158 print("routing to a teleporter\n");
1160 print("routing to a non-teleporter\n");
1163 return CMD_STATUS_EXECUTING;
1166 float havocbot_resetgoal()
1168 navigation_clearroute();
1169 return CMD_STATUS_FINISHED;
1172 void havocbot_setupbot()
1174 self.bot_ai = havocbot_ai;
1175 self.cmd_moveto = havocbot_moveto;
1176 self.cmd_resetgoal = havocbot_resetgoal;
1178 havocbot_chooserole();
1181 vector havocbot_dodge()
1183 // LordHavoc: disabled because this is too expensive
1187 local vector dodge, v, n;
1188 local float danger, bestdanger, vl, d;
1191 // check for dangerous objects near bot or approaching bot
1192 head = findchainfloat(bot_dodge, TRUE);
1195 if (head.owner != self)
1197 vl = vlen(head.velocity);
1198 if (vl > autocvar_sv_maxspeed * 0.3)
1200 n = normalize(head.velocity);
1201 v = self.origin - head.origin;
1203 if (d > (0 - head.bot_dodgerating))
1204 if (d < (vl * 0.2 + head.bot_dodgerating))
1206 // calculate direction and distance from the flight path, by removing the forward axis
1207 v = v - (n * (v * n));
1208 danger = head.bot_dodgerating - vlen(v);
1209 if (bestdanger < danger)
1211 bestdanger = danger;
1212 // dodge to the side of the object
1213 dodge = normalize(v);
1219 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1220 if (bestdanger < danger)
1222 bestdanger = danger;
1223 dodge = normalize(self.origin - head.origin);