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;
27 // TODO: tracewalk() should take care of this job (better path finding under water)
28 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
29 if(self.deadflag != DEAD_NO)
30 if(self.goalcurrent==world)
31 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
33 // Look for the closest waypoint out of water
34 local entity newgoal, head;
35 local float bestdistance, distance;
39 for (head = findchain(classname, "waypoint"); head; head = head.chain)
41 distance = vlen(head.origin - self.origin);
45 if(head.origin_z < self.origin_z)
48 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
51 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
54 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
59 if(distance<bestdistance)
62 bestdistance = distance;
68 // te_wizspike(newgoal.origin);
69 navigation_pushroute(newgoal);
73 // token has been used this frame
74 bot_strategytoken_taken = TRUE;
77 if(self.deadflag != DEAD_NO)
80 havocbot_chooseenemy();
81 if (self.bot_chooseweapontime < time )
83 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
84 havocbot_chooseweapon();
90 self.aistatus |= AI_STATUS_ATTACKING;
91 self.aistatus &~= AI_STATUS_ROAMING;
95 weapon_action(self.weapon, WR_AIM);
96 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
98 self.BUTTON_ATCK = FALSE;
99 self.BUTTON_ATCK2 = FALSE;
103 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
104 self.lastfiredweapon = self.weapon;
109 if(self.bot_aimtarg.classname=="player")
110 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
113 else if (self.goalcurrent)
115 self.aistatus |= AI_STATUS_ROAMING;
116 self.aistatus &~= AI_STATUS_ATTACKING;
118 local vector now,v,next;//,heading;
119 local float aimdistance,skillblend,distanceblend,blend;
120 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
121 aimdistance = vlen(now);
122 //heading = self.velocity;
123 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
125 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
126 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
128 next = self.goalstack01.origin - (self.origin + self.view_ofs);
130 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
131 distanceblend=bound(0,aimdistance/cvar("bot_ai_keyboard_distance"),1);
132 blend = skillblend * (1-distanceblend);
133 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
134 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
135 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
136 v = now + blend * (next - now);
137 //dprint(etos(self), " ");
138 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
139 //v = now * (distanceblend) + next * (1-distanceblend);
140 if (self.waterlevel < WATERLEVEL_SWIMMING)
142 //dprint("walk at:", vtos(v), "\n");
143 //te_lightning2(world, self.origin, self.goalcurrent.origin);
146 havocbot_movetogoal();
149 void havocbot_keyboard_movement(vector destorg)
151 local vector keyboard;
152 local float blend, maxspeed;
155 sk = skill + self.bot_moveskill;
157 maxspeed = cvar("sv_maxspeed");
159 if (time < self.havocbot_keyboardtime)
162 self.havocbot_keyboardtime =
164 self.havocbot_keyboardtime
165 + 0.05/max(1, sk+self.havocbot_keyboardskill)
166 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
168 keyboard = self.movement * (1.0 / maxspeed);
170 local float trigger, trigger1;
171 blend = bound(0,sk*0.1,1);
172 trigger = cvar("bot_ai_keyboard_treshold");
173 trigger1 = 0 - trigger;
175 // categorize forward movement
176 // at skill < 1.5 only forward
177 // at skill < 2.5 only individual directions
178 // at skill < 4.5 only individual directions, and forward diagonals
179 // at skill >= 4.5, all cases allowed
180 if (keyboard_x > trigger)
186 else if (keyboard_x < trigger1 && sk > 1.5)
201 if (keyboard_y > trigger)
203 else if (keyboard_y < trigger1)
208 if (keyboard_z > trigger)
210 else if (keyboard_z < trigger1)
215 self.havocbot_keyboard = keyboard * maxspeed;
216 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
218 keyboard = self.havocbot_keyboard;
219 blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
220 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
221 self.movement = self.movement + (keyboard - self.movement) * blend;
224 void havocbot_bunnyhop(vector dir)
226 local float bunnyhopdistance;
227 local vector deviation;
228 local float maxspeed;
233 // 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")
243 maxspeed = cvar("sv_maxspeed");
245 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
247 self.aistatus &~= AI_STATUS_RUNNING;
248 self.BUTTON_JUMP = FALSE;
249 self.bot_canruntogoal = 0;
250 self.bot_timelastseengoal = 0;
254 if(self.waterlevel > WATERLEVEL_WETFEET)
256 self.aistatus &~= AI_STATUS_RUNNING;
260 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
262 self.bot_canruntogoal = 0;
263 self.bot_timelastseengoal = 0;
266 bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
268 // Run only to visible goals
269 if(self.flags & FL_ONGROUND)
270 if(self.speed==maxspeed)
271 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
273 self.bot_lastseengoal = self.goalcurrent;
276 if(self.bot_timelastseengoal)
278 // for a period of time
279 if(time - self.bot_timelastseengoal > cvar("bot_ai_bunnyhop_firstjumpdelay"))
281 local float checkdistance;
282 checkdistance = TRUE;
284 // don't run if it is too close
285 if(self.bot_canruntogoal==0)
287 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_startdistance"))
288 self.bot_canruntogoal = 1;
290 self.bot_canruntogoal = -1;
293 if(self.bot_canruntogoal != 1)
296 if(self.aistatus & AI_STATUS_ROAMING)
297 if(self.goalcurrent.classname=="waypoint")
298 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
299 if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
300 if(self.goalstack01!=world)
302 deviation = vectoangles(self.goalstack01.origin - self.origin) - vectoangles(self.goalcurrent.origin - self.origin);
303 while (deviation_y < -180) deviation_y = deviation_y + 360;
304 while (deviation_y > 180) deviation_y = deviation_y - 360;
306 if(fabs(deviation_y) < 20)
307 if(bunnyhopdistance < vlen(self.origin - self.goalstack01.origin))
308 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
310 if(vlen(self.goalcurrent.origin - self.goalstack01.origin) > cvar("bot_ai_bunnyhop_startdistance"))
311 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
313 checkdistance = FALSE;
320 self.aistatus &~= AI_STATUS_RUNNING;
321 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_stopdistance"))
322 self.BUTTON_JUMP = TRUE;
326 self.aistatus |= AI_STATUS_RUNNING;
327 self.BUTTON_JUMP = TRUE;
333 self.bot_timelastseengoal = time;
338 self.bot_timelastseengoal = 0;
341 // Release jump button
342 if(!cvar("sv_pogostick"))
343 if(self.flags & FL_ONGROUND == 0)
345 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
346 self.BUTTON_JUMP = FALSE;
349 if(self.aistatus & AI_STATUS_RUNNING)
350 if(vlen(self.velocity)>maxspeed)
352 deviation = vectoangles(dir) - vectoangles(self.velocity);
353 while (deviation_y < -180) deviation_y = deviation_y + 360;
354 while (deviation_y > 180) deviation_y = deviation_y - 360;
356 if(fabs(deviation_y)>10)
360 self.movement_y = maxspeed * -1;
361 else if(deviation_y<10)
362 self.movement_y = maxspeed;
368 void havocbot_movetogoal()
370 local vector destorg;
373 local vector flatdir;
376 local vector evadeobstacle;
377 local vector evadelava;
379 local float maxspeed;
382 //if (self.goalentity)
383 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
384 self.movement = '0 0 0';
385 maxspeed = cvar("sv_maxspeed");
387 // Jetpack navigation
389 if(self.navigation_jetpack_goal)
390 if(self.goalcurrent==self.navigation_jetpack_goal)
393 #ifdef DEBUG_BOT_GOALSTACK
395 te_wizspike(self.navigation_jetpack_point);
399 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
401 // Brake almost completely so it can get a good direction
402 if(vlen(self.velocity)>10)
404 self.aistatus |= AI_STATUS_JETPACK_FLYING;
407 makevectors(self.v_angle_y * '0 1 0');
408 dir = normalize(self.navigation_jetpack_point - self.origin);
411 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
413 // Calculate brake distance in xy
417 dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
419 v = vlen(self.velocity - self.velocity_z * '0 0 1');
420 db = (pow(v,2) / (cvar("g_jetpack_acceleration_side") * 2)) + 100;
421 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
422 if(d < db || d < 500)
425 if(fabs(self.velocity_x)>maxspeed*0.3)
427 self.movement_x = dir * v_forward * -maxspeed;
430 // Switch to normal mode
431 self.navigation_jetpack_goal = world;
432 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
433 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
437 else if(checkpvs(self.origin,self.goalcurrent))
439 // If I can see the goal switch to landing code
440 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
441 self.aistatus |= AI_STATUS_JETPACK_LANDING;
446 self.BUTTON_HOOK = TRUE;
447 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
449 self.movement_x = dir * v_forward * maxspeed;
450 self.movement_y = dir * v_right * maxspeed;
455 // Handling of jump pads
456 if(self.jumppadcount)
458 if(self.flags & FL_ONGROUND)
460 self.jumppadcount = FALSE;
461 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
462 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
465 // If got stuck on the jump pad try to reach the farther visible item
466 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
468 if(fabs(self.velocity_z)<50)
470 local entity head, newgoal;
471 local float distance, bestdistance;
473 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
475 if(head.classname=="worldspawn")
478 distance = vlen(head.origin - self.origin);
482 traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
487 if(distance>bestdistance)
490 bestdistance = distance;
496 self.ignoregoal = self.goalcurrent;
497 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
498 navigation_clearroute();
499 navigation_routetogoal(newgoal, self.origin);
500 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
508 if(self.velocity_z>0)
510 local float threshold;
511 threshold = maxspeed * 0.2;
512 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
513 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
519 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
521 if not(self.flags & FL_ONGROUND)
523 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
524 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
525 if(self.items & IT_JETPACK)
527 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
528 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
530 if(self.velocity_z<0)
532 self.BUTTON_HOOK = TRUE;
536 self.BUTTON_HOOK = TRUE;
538 // If there is no goal try to move forward
540 if(self.goalcurrent==world)
543 dir = normalize(self.goalcurrent.origin - self.origin);
545 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
546 local float xyspeed = xyvelocity * dir;
548 if(xyspeed < (maxspeed / 2))
550 makevectors(self.v_angle_y * '0 1 0');
551 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
552 if(trace_fraction==1)
554 self.movement_x = dir * v_forward * maxspeed;
555 self.movement_y = dir * v_right * maxspeed;
557 havocbot_keyboard_movement(self.origin + dir * 100);
561 self.havocbot_blockhead = TRUE;
565 else if(self.health>cvar("g_balance_rocketlauncher_damage")*0.5)
567 if(self.velocity_z < 0)
568 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
570 self.movement_x = maxspeed;
572 if(self.rocketjumptime)
574 if(time > self.rocketjumptime)
576 self.BUTTON_ATCK2 = TRUE;
577 self.rocketjumptime = 0;
582 self.switchweapon = WEP_ROCKET_LAUNCHER;
584 self.BUTTON_ATCK = TRUE;
585 self.rocketjumptime = time + cvar("g_balance_rocketlauncher_detonatedelay");
591 // If there is no goal try to move forward
592 if(self.goalcurrent==world)
593 self.movement_x = maxspeed;
597 // If we are under water with no goals, swim up
599 if(self.goalcurrent==world)
602 if(self.waterlevel>WATERLEVEL_SWIMMING)
604 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
605 self.BUTTON_JUMP = TRUE;
607 self.BUTTON_JUMP = FALSE;
608 makevectors(self.v_angle_y * '0 1 0');
609 self.movement_x = dir * v_forward * maxspeed;
610 self.movement_y = dir * v_right * maxspeed;
611 self.movement_z = dir * v_up * maxspeed;
614 // if there is nowhere to go, exit
615 if (self.goalcurrent == world)
618 if (self.goalcurrent)
619 navigation_poptouchedgoals();
621 // if ran out of goals try to use an alternative goal or get a new strategy asap
622 if(self.goalcurrent == world)
624 self.bot_strategytime = 0;
628 #ifdef DEBUG_BOT_GOALSTACK
632 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
633 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
634 destorg = self.origin;
635 destorg_x = bound(m1_x, destorg_x, m2_x);
636 destorg_y = bound(m1_y, destorg_y, m2_y);
637 destorg_z = bound(m1_z, destorg_z, m2_z);
638 diff = destorg - self.origin;
640 dir = normalize(diff);
641 flatdir = diff;flatdir_z = 0;
642 flatdir = normalize(flatdir);
644 //if (self.bot_dodgevector_time < time)
646 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
647 // self.bot_dodgevector_jumpbutton = 1;
648 evadeobstacle = '0 0 0';
653 if(self.waterlevel>WATERLEVEL_SWIMMING)
656 self.aistatus |= AI_STATUS_OUT_WATER;
660 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
661 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
662 self.BUTTON_JUMP = TRUE;
664 self.BUTTON_JUMP = FALSE;
666 dir = normalize(flatdir);
667 makevectors(self.v_angle_y * '0 1 0');
671 if(self.aistatus & AI_STATUS_OUT_WATER)
672 self.aistatus &~= AI_STATUS_OUT_WATER;
674 // jump if going toward an obstacle that doesn't look like stairs we
675 // can walk up directly
676 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
677 if (trace_fraction < 1)
678 if (trace_plane_normal_z < 0.7)
681 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
682 if (trace_fraction < s + 0.01)
683 if (trace_plane_normal_z < 0.7)
686 tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
687 if (trace_fraction > s)
688 self.BUTTON_JUMP = 1;
692 // avoiding dangers and obstacles
693 local vector dst_ahead, dst_down;
694 makevectors(self.v_angle_y * '0 1 0');
695 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
696 dst_down = dst_ahead + '0 0 -1500';
699 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
701 // Check head-banging against walls
702 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
704 self.BUTTON_JUMP = TRUE;
705 if(self.facingwalltime && time > self.facingwalltime)
707 self.ignoregoal = self.goalcurrent;
708 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
709 self.bot_strategytime = 0;
714 self.facingwalltime = time + 0.05;
719 self.facingwalltime = 0;
721 if(self.ignoregoal != world && time > self.ignoregoaltime)
723 self.ignoregoal = world;
724 self.ignoregoaltime = 0;
728 // Check for water/slime/lava and dangerous edges
729 // (only when the bot is on the ground or jumping intentionally)
730 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
732 if(trace_fraction == 1)
733 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
736 traceline(dst_ahead , dst_down, TRUE, world);
737 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
738 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
739 if(trace_endpos_z < self.origin_z + self.mins_z)
741 s = pointcontents(trace_endpos + '0 0 1');
742 if (s != CONTENT_SOLID)
743 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
744 evadelava = normalize(self.velocity) * -1;
745 else if (s == CONTENT_SKY)
746 evadeobstacle = normalize(self.velocity) * -1;
747 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
748 self.goalcurrent.absmin, self.goalcurrent.absmax))
750 // if ain't a safe goal with "holes" (like the jumpad on soylent)
751 // and there is a trigger_hurt below
752 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
754 // Remove dangerous dynamic goals from stack
755 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
756 navigation_poproute();
759 evadeobstacle = normalize(self.velocity) * -1;
768 makevectors(self.v_angle_y * '0 1 0');
770 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
771 self.aistatus |= AI_STATUS_DANGER_AHEAD;
774 dodge = havocbot_dodge();
775 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
776 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
777 traceline(self.origin, self.enemy.origin, TRUE, world);
778 if(trace_ent.classname == "player")
779 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
781 dir = normalize(dir + dodge + evadeobstacle + evadelava);
782 // self.bot_dodgevector = dir;
783 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
786 if(time < self.ladder_time)
788 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
790 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
795 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
800 //dir = self.bot_dodgevector;
801 //if (self.bot_dodgevector_jumpbutton)
802 // self.BUTTON_JUMP = 1;
803 self.movement_x = dir * v_forward * maxspeed;
804 self.movement_y = dir * v_right * maxspeed;
805 self.movement_z = dir * v_up * maxspeed;
807 // Emulate keyboard interface
809 havocbot_keyboard_movement(destorg);
812 // if(self.aistatus & AI_STATUS_ROAMING)
814 if(skill+self.bot_moveskill >= cvar("bot_ai_bunnyhop_skilloffset"))
815 havocbot_bunnyhop(dir);
817 if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
818 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
819 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);
822 void havocbot_chooseenemy()
824 local entity head, best, head2;
825 local float rating, bestrating, i, f;
827 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
834 if (!bot_shouldattack(self.enemy))
836 // enemy died or something, find a new target
838 self.havocbot_chooseenemy_finished = time;
840 else if (self.havocbot_stickenemy)
842 // tracking last chosen enemy
843 // if enemy is visible
844 // and not really really far away
845 // and we're not severely injured
846 // then keep tracking for a half second into the future
847 traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
848 if (trace_ent == self.enemy || trace_fraction == 1)
849 if (vlen(self.enemy.origin - self.origin) < 1000)
850 if (self.health > 30)
852 // remain tracking him for a shot while (case he went after a small corner or pilar
853 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
856 // enemy isn't visible, or is far away, or we're injured severely
857 // so stop preferring this enemy
858 // (it will still take a half second until a new one is chosen)
859 self.havocbot_stickenemy = 0;
862 if (time < self.havocbot_chooseenemy_finished)
864 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
865 eye = self.origin + self.view_ofs;
867 bestrating = 100000000;
868 head = head2 = findchainfloat(bot_attack, TRUE);
870 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
875 v = (head.absmin + head.absmax) * 0.5;
876 rating = vlen(v - eye);
877 if (rating<cvar("bot_ai_enemydetectionradius"))
878 if (bestrating > rating)
879 if (bot_shouldattack(head))
881 traceline(eye, v, TRUE, self);
882 if (trace_ent == head || trace_fraction >= 1)
891 // I want to do a second scan if no enemy was found or I don't have weapons
892 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
893 if(best || self.weapons) // || self.weapon == WEP_CAMPINGRIFLE
898 // Set flags to see through transparent objects
899 f = self.dphitcontentsmask;
900 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
906 // Restore hit flags if needed
908 self.dphitcontentsmask = f;
911 self.havocbot_stickenemy = TRUE;
914 void havocbot_chooseweapon()
919 if(g_weaponarena == WEPBIT_TUBA)
921 self.switchweapon = WEP_TUBA;
925 // TODO: clean this up by moving it to weapon code
926 if(self.enemy==world)
928 // If no weapon was chosen get the first available weapon
930 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
932 if(client_hasweapon(self, i, TRUE, FALSE))
934 self.switchweapon = i;
941 // Do not change weapon during the next second after a combo
942 i = time - self.lastcombotime;
946 // Workaround for rifle reloading (..)
947 if(self.weapon == WEP_CAMPINGRIFLE)
948 if(i < cvar("g_balance_campingrifle_reloadtime") + 1)
952 local float rocket ; rocket =-1000;
953 local float nex ; nex =-1000;
954 local float hagar ; hagar =-1000;
955 local float grenade ; grenade =-1000;
956 local float mine ; mine =-1000;
957 local float electro ; electro =-1000;
958 local float crylink ; crylink =-1000;
959 local float uzi ; uzi =-1000;
960 local float shotgun ; shotgun =-1000;
961 local float campingrifle ; campingrifle =-1000;
962 local float laser ; laser =-1000;
963 local float minstanex ; minstanex =-1000;
964 local float bestscore; bestscore = 0;
965 local float bestweapon; bestweapon=self.switchweapon;
966 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
967 local float maxdelaytime=0.5;
968 local float spreadpenalty=10;
970 // Should it do a weapon combo?
971 local float af, ct, combo_time, combo;
973 af = ATTACK_FINISHED(self);
974 ct = cvar("bot_ai_weapon_combo_threshold");
976 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
977 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
978 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
982 if(cvar("bot_ai_weapon_combo"))
983 if(self.weapon == self.lastfiredweapon)
987 self.lastcombotime = time;
990 distance *= pow(2, self.bot_rangepreference);
992 // Custom weapon list based on distance to the enemy
993 if(bot_custom_weapon){
995 // Choose weapons for far distance
996 if ( distance > bot_distance_far ) {
997 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
998 w = bot_weapons_far[i];
999 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1000 if ( self.weapon == w && combo)
1002 self.switchweapon = w;
1008 // Choose weapons for mid distance
1009 if ( distance > bot_distance_close) {
1010 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1011 w = bot_weapons_mid[i];
1012 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1013 if ( self.weapon == w && combo)
1015 self.switchweapon = w;
1021 // Choose weapons for close distance
1022 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1023 w = bot_weapons_close[i];
1024 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1025 if ( self.weapon == w && combo)
1027 self.switchweapon = w;
1034 // TODO: This disabled code is not working well and got replaced by custom weapon priorities.
1035 // However, this logic should be refactored and moved to weapons code so each new weapon can be
1036 // evaluated dynamically by bots without updating the "ai" or config files. --mand1nga
1037 float s, distancefromfloor, currentscore;
1041 // (Damage/Sec * Weapon spefic change to get that damage)
1042 // *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
1043 // *(Spread change of hit) // if it applies
1044 // *(Penality for target beeing in air)
1047 traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
1048 distancefromfloor = self.enemy.origin_z - trace_endpos_z;
1050 if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
1051 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
1054 if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE) &&
1055 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER &&
1059 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
1060 * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
1062 if (client_hasweapon(self, WEP_NEX, TRUE, FALSE) &&
1063 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX &&
1067 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
1070 if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // &&
1071 // !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR && time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
1072 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
1073 * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
1075 if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) &&
1077 cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER &&
1081 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
1082 * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
1084 if (client_hasweapon(self, WEP_MINE_LAYER, TRUE, FALSE) &&
1086 cvar("bot_ai_weapon_combo") && self.weapon == WEP_MINE_LAYER &&
1090 mine = (cvar("g_balance_minelayer_damage")/cvar("g_balance_minelayer_refire")*1.0)
1091 * bound(0,(cvar("g_balance_minelayer_speed")/distance*maxdelaytime),1)*1.1;
1093 if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) &&
1094 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO &&
1098 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
1099 * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
1101 if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // &&
1102 // !( self.weapon == WEP_CRYLINK && time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
1103 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
1104 * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
1106 if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // &&
1107 // !( self.weapon == WEP_UZI && time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
1108 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
1109 * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
1111 if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) &&
1112 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN &&
1116 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
1117 * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
1119 if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) &&
1120 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER &&
1124 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
1125 * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
1127 if((self.enemy.flags & FL_ONGROUND)==FALSE){
1128 rocket = rocket * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius" ),0.9)); //slight bigger change
1129 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
1130 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius" ),0.95));
1131 laser = laser * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius" ),0.95));
1134 dprint("Floor distance: ",ftos(distancefromfloor),"\n");
1135 dprint("Rocket: " , ftos(rocket ), "\n");
1136 dprint("Nex: " , ftos(nex ), "\n");
1137 dprint("Hagar: " , ftos(hagar ), "\n");
1138 dprint("Grenade: ", ftos(grenade ), "\n");
1139 dprint("Mine: " , ftos(mine ), "\n");
1140 dprint("Electro: ", ftos(electro ), "\n");
1141 dprint("Crylink: ", ftos(crylink ), "\n");
1142 dprint("Uzi: " , ftos(uzi ), "\n");
1143 dprint("Shotgun :", ftos(shotgun ), "\n");
1144 dprint("Laser :", ftos(laser ), "\n\n");
1147 w = WEP_MINSTANEX ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1148 w = WEP_ROCKET_LAUNCHER ;s = rocket ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1149 w = WEP_NEX ;s = nex ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1150 w = WEP_HAGAR ;s = hagar ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1151 w = WEP_GRENADE_LAUNCHER ;s = grenade ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1152 w = WEP_MINE_LAYER ;s = mine ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1153 w = WEP_ELECTRO ;s = electro ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1154 w = WEP_CRYLINK ;s = crylink ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1155 w = WEP_UZI ;s = uzi ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1156 w = WEP_SHOTGUN ;s = shotgun ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1157 w = WEP_LASER ;s = laser ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1159 // switch if the best weapon would provide a significant damage increase
1160 if (bestscore > currentscore*1.5){
1161 self.switchweapon = bestweapon;
1163 // buys time for detonating the rocket. not tested yet
1164 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
1165 self.bot_chooseweapontime += (distance / cvar("g_balance_rocketlauncher_speed"));
1172 local vector selfvel, enemyvel;
1173 // if(self.flags & FL_INWATER)
1175 if (time < self.nextaim)
1177 self.nextaim = time + 0.1;
1178 selfvel = self.velocity;
1179 if (!self.waterlevel)
1183 enemyvel = self.enemy.velocity;
1184 if (!self.enemy.waterlevel)
1186 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1189 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1192 float havocbot_moveto_refresh_route()
1194 // Refresh path to goal if necessary
1196 wp = self.havocbot_personal_waypoint;
1197 navigation_goalrating_start();
1198 navigation_routerating(wp, 10000, 10000);
1199 navigation_goalrating_end();
1200 return self.navigation_hasgoals;
1203 float havocbot_moveto(vector pos)
1207 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1209 // Step 4: Move to waypoint
1210 if(self.havocbot_personal_waypoint==world)
1212 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1213 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1214 return CMD_STATUS_ERROR;
1217 if (!bot_strategytoken_taken)
1218 if(self.havocbot_personal_waypoint_searchtime<time)
1220 bot_strategytoken_taken = TRUE;
1221 if(havocbot_moveto_refresh_route())
1223 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1224 self.havocbot_personal_waypoint_searchtime = time + 10;
1225 self.havocbot_personal_waypoint_failcounter = 0;
1229 self.havocbot_personal_waypoint_failcounter += 1;
1230 self.havocbot_personal_waypoint_searchtime = time + 2;
1231 if(self.havocbot_personal_waypoint_failcounter >= 30)
1233 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1234 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1235 remove(self.havocbot_personal_waypoint);
1236 return CMD_STATUS_ERROR;
1239 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1243 #ifdef DEBUG_BOT_GOALSTACK
1248 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1250 bot_aimdir(dir, -1);
1253 havocbot_movetogoal();
1255 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1257 // Step 5: Waypoint reached
1258 dprint(self.netname, "'s personal waypoint reached\n");
1259 remove(self.havocbot_personal_waypoint);
1260 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1261 return CMD_STATUS_FINISHED;
1264 return CMD_STATUS_EXECUTING;
1267 // Step 2: Linking waypoint
1268 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1270 // Wait until it is linked
1271 if(!self.havocbot_personal_waypoint.wplinked)
1273 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1274 return CMD_STATUS_EXECUTING;
1277 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1278 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1279 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1281 // Step 3: Route to waypoint
1282 dprint(self.netname, " walking to its personal waypoint\n");
1284 return CMD_STATUS_EXECUTING;
1287 // Step 1: Spawning waypoint
1288 wp = waypoint_spawnpersonal(pos);
1291 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1292 return CMD_STATUS_ERROR;
1295 self.havocbot_personal_waypoint = wp;
1296 self.havocbot_personal_waypoint_failcounter = 0;
1297 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1299 // if pos is inside a teleport, then let's mark it as teleport waypoint
1301 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1303 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1305 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1306 self.lastteleporttime = 0;
1311 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1312 print("routing to a teleporter\n");
1314 print("routing to a non-teleporter\n");
1317 return CMD_STATUS_EXECUTING;
1320 float havocbot_resetgoal()
1322 navigation_clearroute();
1323 return CMD_STATUS_FINISHED;
1326 void havocbot_setupbot()
1328 self.bot_ai = havocbot_ai;
1329 self.cmd_moveto = havocbot_moveto;
1330 self.cmd_resetgoal = havocbot_resetgoal;
1332 havocbot_chooserole();
1335 vector havocbot_dodge()
1337 // LordHavoc: disabled because this is too expensive
1340 local vector dodge, v, n;
1341 local float danger, bestdanger, vl, d;
1344 // check for dangerous objects near bot or approaching bot
1345 head = findchainfloat(bot_dodge, TRUE);
1348 if (head.owner != self)
1350 vl = vlen(head.velocity);
1351 if (vl > sv_maxspeed * 0.3)
1353 n = normalize(head.velocity);
1354 v = self.origin - head.origin;
1356 if (d > (0 - head.bot_dodgerating))
1357 if (d < (vl * 0.2 + head.bot_dodgerating))
1359 // calculate direction and distance from the flight path, by removing the forward axis
1360 v = v - (n * (v * n));
1361 danger = head.bot_dodgerating - vlen(v);
1362 if (bestdanger < danger)
1364 bestdanger = danger;
1365 // dodge to the side of the object
1366 dodge = normalize(v);
1372 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1373 if (bestdanger < danger)
1375 bestdanger = danger;
1376 dodge = normalize(self.origin - head.origin);