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_treshold;
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
235 if(self.aistatus & AI_STATUS_ATTACKING)
236 if(self.weapon & WEP_CAMPINGRIFLE)
239 if(self.goalcurrent.classname == "player")
242 maxspeed = autocvar_sv_maxspeed;
244 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
246 self.aistatus &~= AI_STATUS_RUNNING;
247 self.BUTTON_JUMP = FALSE;
248 self.bot_canruntogoal = 0;
249 self.bot_timelastseengoal = 0;
253 if(self.waterlevel > WATERLEVEL_WETFEET)
255 self.aistatus &~= AI_STATUS_RUNNING;
259 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
261 self.bot_canruntogoal = 0;
262 self.bot_timelastseengoal = 0;
265 bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
267 // Run only to visible goals
268 if(self.flags & FL_ONGROUND)
269 if(self.speed==maxspeed)
270 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
272 self.bot_lastseengoal = self.goalcurrent;
275 if(self.bot_timelastseengoal)
277 // for a period of time
278 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
280 local float checkdistance;
281 checkdistance = TRUE;
283 // don't run if it is too close
284 if(self.bot_canruntogoal==0)
286 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
287 self.bot_canruntogoal = 1;
289 self.bot_canruntogoal = -1;
292 if(self.bot_canruntogoal != 1)
295 if(self.aistatus & AI_STATUS_ROAMING)
296 if(self.goalcurrent.classname=="waypoint")
297 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
298 if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
299 if(self.goalstack01!=world)
301 deviation = vectoangles(self.goalstack01.origin - self.origin) - vectoangles(self.goalcurrent.origin - self.origin);
302 while (deviation_y < -180) deviation_y = deviation_y + 360;
303 while (deviation_y > 180) deviation_y = deviation_y - 360;
305 if(fabs(deviation_y) < 20)
306 if(bunnyhopdistance < vlen(self.origin - self.goalstack01.origin))
307 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
309 if(vlen(self.goalcurrent.origin - self.goalstack01.origin) > autocvar_bot_ai_bunnyhop_startdistance)
310 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
312 checkdistance = FALSE;
319 self.aistatus &~= AI_STATUS_RUNNING;
320 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
321 self.BUTTON_JUMP = TRUE;
325 self.aistatus |= AI_STATUS_RUNNING;
326 self.BUTTON_JUMP = TRUE;
332 self.bot_timelastseengoal = time;
337 self.bot_timelastseengoal = 0;
340 // Release jump button
341 if(self.flags & FL_ONGROUND == 0)
343 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
344 self.BUTTON_JUMP = FALSE;
347 if(self.aistatus & AI_STATUS_RUNNING)
348 if(vlen(self.velocity)>maxspeed)
350 deviation = vectoangles(dir) - vectoangles(self.velocity);
351 while (deviation_y < -180) deviation_y = deviation_y + 360;
352 while (deviation_y > 180) deviation_y = deviation_y - 360;
354 if(fabs(deviation_y)>10)
358 self.movement_y = maxspeed * -1;
359 else if(deviation_y<10)
360 self.movement_y = maxspeed;
366 void havocbot_movetogoal()
368 local vector destorg;
371 local vector flatdir;
374 local vector evadeobstacle;
375 local vector evadelava;
377 local float maxspeed;
380 //if (self.goalentity)
381 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
382 self.movement = '0 0 0';
383 maxspeed = autocvar_sv_maxspeed;
385 // Jetpack navigation
387 if(self.navigation_jetpack_goal)
388 if(self.goalcurrent==self.navigation_jetpack_goal)
391 #ifdef DEBUG_BOT_GOALSTACK
393 te_wizspike(self.navigation_jetpack_point);
397 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
399 // Brake almost completely so it can get a good direction
400 if(vlen(self.velocity)>10)
402 self.aistatus |= AI_STATUS_JETPACK_FLYING;
405 makevectors(self.v_angle_y * '0 1 0');
406 dir = normalize(self.navigation_jetpack_point - self.origin);
409 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
411 // Calculate brake distance in xy
415 dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
417 v = vlen(self.velocity - self.velocity_z * '0 0 1');
418 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
419 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
420 if(d < db || d < 500)
423 if(fabs(self.velocity_x)>maxspeed*0.3)
425 self.movement_x = dir * v_forward * -maxspeed;
428 // Switch to normal mode
429 self.navigation_jetpack_goal = world;
430 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
431 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
435 else if(checkpvs(self.origin,self.goalcurrent))
437 // If I can see the goal switch to landing code
438 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
439 self.aistatus |= AI_STATUS_JETPACK_LANDING;
444 self.BUTTON_HOOK = TRUE;
445 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
447 self.movement_x = dir * v_forward * maxspeed;
448 self.movement_y = dir * v_right * maxspeed;
453 // Handling of jump pads
454 if(self.jumppadcount)
456 if(self.flags & FL_ONGROUND)
458 self.jumppadcount = FALSE;
459 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
460 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
463 // If got stuck on the jump pad try to reach the farther visible item
464 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
466 if(fabs(self.velocity_z)<50)
468 local entity head, newgoal;
469 local float distance, bestdistance;
471 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
473 if(head.classname=="worldspawn")
476 distance = vlen(head.origin - self.origin);
480 traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
485 if(distance>bestdistance)
488 bestdistance = distance;
494 self.ignoregoal = self.goalcurrent;
495 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
496 navigation_clearroute();
497 navigation_routetogoal(newgoal, self.origin);
498 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
506 if(self.velocity_z>0)
508 local float threshold;
509 threshold = maxspeed * 0.2;
510 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
511 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
517 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
519 if not(self.flags & FL_ONGROUND)
521 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
522 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
523 if(self.items & IT_JETPACK)
525 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
526 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
528 if(self.velocity_z<0)
530 self.BUTTON_HOOK = TRUE;
534 self.BUTTON_HOOK = TRUE;
536 // If there is no goal try to move forward
538 if(self.goalcurrent==world)
541 dir = normalize(self.goalcurrent.origin - self.origin);
543 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
544 local float xyspeed = xyvelocity * dir;
546 if(xyspeed < (maxspeed / 2))
548 makevectors(self.v_angle_y * '0 1 0');
549 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
550 if(trace_fraction==1)
552 self.movement_x = dir * v_forward * maxspeed;
553 self.movement_y = dir * v_right * maxspeed;
555 havocbot_keyboard_movement(self.origin + dir * 100);
559 self.havocbot_blockhead = TRUE;
563 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
565 if(self.velocity_z < 0)
566 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
568 self.movement_x = maxspeed;
570 if(self.rocketjumptime)
572 if(time > self.rocketjumptime)
574 self.BUTTON_ATCK2 = TRUE;
575 self.rocketjumptime = 0;
580 self.switchweapon = WEP_ROCKET_LAUNCHER;
582 self.BUTTON_ATCK = TRUE;
583 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
589 // If there is no goal try to move forward
590 if(self.goalcurrent==world)
591 self.movement_x = maxspeed;
595 // If we are under water with no goals, swim up
597 if(self.goalcurrent==world)
600 if(self.waterlevel>WATERLEVEL_SWIMMING)
602 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
603 self.BUTTON_JUMP = TRUE;
605 self.BUTTON_JUMP = FALSE;
606 makevectors(self.v_angle_y * '0 1 0');
607 self.movement_x = dir * v_forward * maxspeed;
608 self.movement_y = dir * v_right * maxspeed;
609 self.movement_z = dir * v_up * maxspeed;
612 // if there is nowhere to go, exit
613 if (self.goalcurrent == world)
616 if (self.goalcurrent)
617 navigation_poptouchedgoals();
619 // if ran out of goals try to use an alternative goal or get a new strategy asap
620 if(self.goalcurrent == world)
622 self.bot_strategytime = 0;
626 #ifdef DEBUG_BOT_GOALSTACK
630 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
631 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
632 destorg = self.origin;
633 destorg_x = bound(m1_x, destorg_x, m2_x);
634 destorg_y = bound(m1_y, destorg_y, m2_y);
635 destorg_z = bound(m1_z, destorg_z, m2_z);
636 diff = destorg - self.origin;
638 dir = normalize(diff);
639 flatdir = diff;flatdir_z = 0;
640 flatdir = normalize(flatdir);
642 //if (self.bot_dodgevector_time < time)
644 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
645 // self.bot_dodgevector_jumpbutton = 1;
646 evadeobstacle = '0 0 0';
651 if(self.waterlevel>WATERLEVEL_SWIMMING)
654 self.aistatus |= AI_STATUS_OUT_WATER;
658 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
659 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
660 self.BUTTON_JUMP = TRUE;
662 self.BUTTON_JUMP = FALSE;
664 dir = normalize(flatdir);
665 makevectors(self.v_angle_y * '0 1 0');
669 if(self.aistatus & AI_STATUS_OUT_WATER)
670 self.aistatus &~= AI_STATUS_OUT_WATER;
672 // jump if going toward an obstacle that doesn't look like stairs we
673 // can walk up directly
674 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
675 if (trace_fraction < 1)
676 if (trace_plane_normal_z < 0.7)
679 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
680 if (trace_fraction < s + 0.01)
681 if (trace_plane_normal_z < 0.7)
684 tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
685 if (trace_fraction > s)
686 self.BUTTON_JUMP = 1;
690 // avoiding dangers and obstacles
691 local vector dst_ahead, dst_down;
692 makevectors(self.v_angle_y * '0 1 0');
693 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
694 dst_down = dst_ahead + '0 0 -1500';
697 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
699 // Check head-banging against walls
700 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
702 self.BUTTON_JUMP = TRUE;
703 if(self.facingwalltime && time > self.facingwalltime)
705 self.ignoregoal = self.goalcurrent;
706 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
707 self.bot_strategytime = 0;
712 self.facingwalltime = time + 0.05;
717 self.facingwalltime = 0;
719 if(self.ignoregoal != world && time > self.ignoregoaltime)
721 self.ignoregoal = world;
722 self.ignoregoaltime = 0;
726 // Check for water/slime/lava and dangerous edges
727 // (only when the bot is on the ground or jumping intentionally)
728 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
730 if(trace_fraction == 1)
731 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
734 traceline(dst_ahead , dst_down, TRUE, world);
735 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
736 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
737 if(trace_endpos_z < self.origin_z + self.mins_z)
739 s = pointcontents(trace_endpos + '0 0 1');
740 if (s != CONTENT_SOLID)
741 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
742 evadelava = normalize(self.velocity) * -1;
743 else if (s == CONTENT_SKY)
744 evadeobstacle = normalize(self.velocity) * -1;
745 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
746 self.goalcurrent.absmin, self.goalcurrent.absmax))
748 // if ain't a safe goal with "holes" (like the jumpad on soylent)
749 // and there is a trigger_hurt below
750 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
752 // Remove dangerous dynamic goals from stack
753 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
754 navigation_poproute();
757 evadeobstacle = normalize(self.velocity) * -1;
766 makevectors(self.v_angle_y * '0 1 0');
768 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
769 self.aistatus |= AI_STATUS_DANGER_AHEAD;
772 dodge = havocbot_dodge();
773 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
774 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
775 traceline(self.origin, self.enemy.origin, TRUE, world);
776 if(trace_ent.classname == "player")
777 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
779 dir = normalize(dir + dodge + evadeobstacle + evadelava);
780 // self.bot_dodgevector = dir;
781 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
784 if(time < self.ladder_time)
786 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
788 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
793 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
798 //dir = self.bot_dodgevector;
799 //if (self.bot_dodgevector_jumpbutton)
800 // self.BUTTON_JUMP = 1;
801 self.movement_x = dir * v_forward * maxspeed;
802 self.movement_y = dir * v_right * maxspeed;
803 self.movement_z = dir * v_up * maxspeed;
805 // Emulate keyboard interface
807 havocbot_keyboard_movement(destorg);
810 // if(self.aistatus & AI_STATUS_ROAMING)
812 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
813 havocbot_bunnyhop(dir);
815 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
816 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
817 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);
820 void havocbot_chooseenemy()
822 local entity head, best, head2;
823 local float rating, bestrating, i, f;
825 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
832 if (!bot_shouldattack(self.enemy))
834 // enemy died or something, find a new target
836 self.havocbot_chooseenemy_finished = time;
838 else if (self.havocbot_stickenemy)
840 // tracking last chosen enemy
841 // if enemy is visible
842 // and not really really far away
843 // and we're not severely injured
844 // then keep tracking for a half second into the future
845 traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
846 if (trace_ent == self.enemy || trace_fraction == 1)
847 if (vlen(self.enemy.origin - self.origin) < 1000)
848 if (self.health > 30)
850 // remain tracking him for a shot while (case he went after a small corner or pilar
851 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
854 // enemy isn't visible, or is far away, or we're injured severely
855 // so stop preferring this enemy
856 // (it will still take a half second until a new one is chosen)
857 self.havocbot_stickenemy = 0;
860 if (time < self.havocbot_chooseenemy_finished)
862 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
863 eye = self.origin + self.view_ofs;
865 bestrating = 100000000;
866 head = head2 = findchainfloat(bot_attack, TRUE);
868 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
873 v = (head.absmin + head.absmax) * 0.5;
874 rating = vlen(v - eye);
875 if (rating<autocvar_bot_ai_enemydetectionradius)
876 if (bestrating > rating)
877 if (bot_shouldattack(head))
879 traceline(eye, v, TRUE, self);
880 if (trace_ent == head || trace_fraction >= 1)
889 // I want to do a second scan if no enemy was found or I don't have weapons
890 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
891 if(best || self.weapons) // || self.weapon == WEP_CAMPINGRIFLE
896 // Set flags to see through transparent objects
897 f = self.dphitcontentsmask;
898 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
904 // Restore hit flags if needed
906 self.dphitcontentsmask = f;
909 self.havocbot_stickenemy = TRUE;
912 void havocbot_chooseweapon()
917 if(g_weaponarena == WEPBIT_TUBA)
919 self.switchweapon = WEP_TUBA;
923 // TODO: clean this up by moving it to weapon code
924 if(self.enemy==world)
926 // If no weapon was chosen get the first available weapon
928 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
930 if(client_hasweapon(self, i, TRUE, FALSE))
932 self.switchweapon = i;
939 // Do not change weapon during the next second after a combo
940 i = time - self.lastcombotime;
944 // Workaround for rifle reloading (..)
945 if(self.weapon == WEP_CAMPINGRIFLE)
946 if(i < autocvar_g_balance_campingrifle_reloadtime + 1)
950 local float rocket ; rocket =-1000;
951 local float nex ; nex =-1000;
952 local float hagar ; hagar =-1000;
953 local float grenade ; grenade =-1000;
954 local float mine ; mine =-1000;
955 local float electro ; electro =-1000;
956 local float crylink ; crylink =-1000;
957 local float uzi ; uzi =-1000;
958 local float shotgun ; shotgun =-1000;
959 local float campingrifle ; campingrifle =-1000;
960 local float laser ; laser =-1000;
961 local float minstanex ; minstanex =-1000;
962 local float bestscore; bestscore = 0;
963 local float bestweapon; bestweapon=self.switchweapon;
964 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
965 local float maxdelaytime=0.5;
966 local float spreadpenalty=10;
968 // Should it do a weapon combo?
969 local float af, ct, combo_time, combo;
971 af = ATTACK_FINISHED(self);
972 ct = autocvar_bot_ai_weapon_combo_threshold;
974 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
975 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
976 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
980 if(autocvar_bot_ai_weapon_combo)
981 if(self.weapon == self.lastfiredweapon)
985 self.lastcombotime = time;
988 distance *= pow(2, self.bot_rangepreference);
990 // Custom weapon list based on distance to the enemy
991 if(bot_custom_weapon){
993 // Choose weapons for far distance
994 if ( distance > bot_distance_far ) {
995 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
996 w = bot_weapons_far[i];
997 if ( client_hasweapon(self, w, TRUE, FALSE) ){
998 if ( self.weapon == w && combo)
1000 self.switchweapon = w;
1006 // Choose weapons for mid distance
1007 if ( distance > bot_distance_close) {
1008 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1009 w = bot_weapons_mid[i];
1010 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1011 if ( self.weapon == w && combo)
1013 self.switchweapon = w;
1019 // Choose weapons for close distance
1020 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1021 w = bot_weapons_close[i];
1022 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1023 if ( self.weapon == w && combo)
1025 self.switchweapon = w;
1034 local vector selfvel, enemyvel;
1035 // if(self.flags & FL_INWATER)
1037 if (time < self.nextaim)
1039 self.nextaim = time + 0.1;
1040 selfvel = self.velocity;
1041 if (!self.waterlevel)
1045 enemyvel = self.enemy.velocity;
1046 if (!self.enemy.waterlevel)
1048 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1051 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1054 float havocbot_moveto_refresh_route()
1056 // Refresh path to goal if necessary
1058 wp = self.havocbot_personal_waypoint;
1059 navigation_goalrating_start();
1060 navigation_routerating(wp, 10000, 10000);
1061 navigation_goalrating_end();
1062 return self.navigation_hasgoals;
1065 float havocbot_moveto(vector pos)
1069 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1071 // Step 4: Move to waypoint
1072 if(self.havocbot_personal_waypoint==world)
1074 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1075 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1076 return CMD_STATUS_ERROR;
1079 if (!bot_strategytoken_taken)
1080 if(self.havocbot_personal_waypoint_searchtime<time)
1082 bot_strategytoken_taken = TRUE;
1083 if(havocbot_moveto_refresh_route())
1085 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1086 self.havocbot_personal_waypoint_searchtime = time + 10;
1087 self.havocbot_personal_waypoint_failcounter = 0;
1091 self.havocbot_personal_waypoint_failcounter += 1;
1092 self.havocbot_personal_waypoint_searchtime = time + 2;
1093 if(self.havocbot_personal_waypoint_failcounter >= 30)
1095 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1096 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1097 remove(self.havocbot_personal_waypoint);
1098 return CMD_STATUS_ERROR;
1101 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1105 #ifdef DEBUG_BOT_GOALSTACK
1110 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1112 bot_aimdir(dir, -1);
1115 havocbot_movetogoal();
1117 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1119 // Step 5: Waypoint reached
1120 dprint(self.netname, "'s personal waypoint reached\n");
1121 remove(self.havocbot_personal_waypoint);
1122 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1123 return CMD_STATUS_FINISHED;
1126 return CMD_STATUS_EXECUTING;
1129 // Step 2: Linking waypoint
1130 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1132 // Wait until it is linked
1133 if(!self.havocbot_personal_waypoint.wplinked)
1135 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1136 return CMD_STATUS_EXECUTING;
1139 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1140 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1141 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1143 // Step 3: Route to waypoint
1144 dprint(self.netname, " walking to its personal waypoint\n");
1146 return CMD_STATUS_EXECUTING;
1149 // Step 1: Spawning waypoint
1150 wp = waypoint_spawnpersonal(pos);
1153 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1154 return CMD_STATUS_ERROR;
1157 self.havocbot_personal_waypoint = wp;
1158 self.havocbot_personal_waypoint_failcounter = 0;
1159 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1161 // if pos is inside a teleport, then let's mark it as teleport waypoint
1163 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1165 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1167 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1168 self.lastteleporttime = 0;
1173 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1174 print("routing to a teleporter\n");
1176 print("routing to a non-teleporter\n");
1179 return CMD_STATUS_EXECUTING;
1182 float havocbot_resetgoal()
1184 navigation_clearroute();
1185 return CMD_STATUS_FINISHED;
1188 void havocbot_setupbot()
1190 self.bot_ai = havocbot_ai;
1191 self.cmd_moveto = havocbot_moveto;
1192 self.cmd_resetgoal = havocbot_resetgoal;
1194 havocbot_chooserole();
1197 vector havocbot_dodge()
1199 // LordHavoc: disabled because this is too expensive
1203 local vector dodge, v, n;
1204 local float danger, bestdanger, vl, d;
1207 // check for dangerous objects near bot or approaching bot
1208 head = findchainfloat(bot_dodge, TRUE);
1211 if (head.owner != self)
1213 vl = vlen(head.velocity);
1214 if (vl > autocvar_sv_maxspeed * 0.3)
1216 n = normalize(head.velocity);
1217 v = self.origin - head.origin;
1219 if (d > (0 - head.bot_dodgerating))
1220 if (d < (vl * 0.2 + head.bot_dodgerating))
1222 // calculate direction and distance from the flight path, by removing the forward axis
1223 v = v - (n * (v * n));
1224 danger = head.bot_dodgerating - vlen(v);
1225 if (bestdanger < danger)
1227 bestdanger = danger;
1228 // dodge to the side of the object
1229 dodge = normalize(v);
1235 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1236 if (bestdanger < danger)
1238 bestdanger = danger;
1239 dodge = normalize(self.origin - head.origin);