3 #include "role_onslaught.qc"
4 #include "role_keyhunt.qc"
14 if(bot_execute_commands())
17 // execute the vore AI for bots
19 if(self.predator.classname == "player")
20 return; // an eaten bot has nothing to do outside of the vore AI
22 if (bot_strategytoken == self)
23 if (!bot_strategytoken_taken)
25 if(self.havocbot_blockhead)
27 self.havocbot_blockhead = FALSE;
34 // TODO: tracewalk() should take care of this job (better path finding under water)
35 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
36 if(self.deadflag != DEAD_NO)
37 if(self.goalcurrent==world)
38 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
40 // Look for the closest waypoint out of water
41 local entity newgoal, head;
42 local float bestdistance, distance;
46 for (head = findchain(classname, "waypoint"); head; head = head.chain)
48 distance = vlen(head.origin - self.origin);
52 if(head.origin_z < self.origin_z)
55 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
58 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
61 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
66 if(distance<bestdistance)
69 bestdistance = distance;
75 // te_wizspike(newgoal.origin);
76 navigation_pushroute(newgoal);
80 // token has been used this frame
81 bot_strategytoken_taken = TRUE;
84 if(self.deadflag != DEAD_NO)
87 havocbot_chooseenemy();
88 if (self.bot_chooseweapontime < time )
90 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
91 havocbot_chooseweapon();
97 self.aistatus |= AI_STATUS_ATTACKING;
98 self.aistatus &~= AI_STATUS_ROAMING;
102 weapon_action(self.weapon, WR_AIM);
103 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
105 self.BUTTON_ATCK = FALSE;
106 self.BUTTON_ATCK2 = FALSE;
110 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
111 self.lastfiredweapon = self.weapon;
116 if(self.bot_aimtarg.classname=="player")
117 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
120 else if (self.goalcurrent)
122 self.aistatus |= AI_STATUS_ROAMING;
123 self.aistatus &~= AI_STATUS_ATTACKING;
125 local vector now,v,next;//,heading;
126 local float aimdistance,skillblend,distanceblend,blend;
127 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
128 aimdistance = vlen(now);
129 //heading = self.velocity;
130 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
132 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
133 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
135 next = self.goalstack01.origin - (self.origin + self.view_ofs);
137 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
138 distanceblend=bound(0,aimdistance/cvar("bot_ai_keyboard_distance"),1);
139 blend = skillblend * (1-distanceblend);
140 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
141 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
142 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
143 v = now + blend * (next - now);
144 //dprint(etos(self), " ");
145 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
146 //v = now * (distanceblend) + next * (1-distanceblend);
147 if (self.waterlevel < WATERLEVEL_SWIMMING)
149 //dprint("walk at:", vtos(v), "\n");
150 //te_lightning2(world, self.origin, self.goalcurrent.origin);
153 havocbot_movetogoal();
156 void havocbot_keyboard_movement(vector destorg)
158 local vector keyboard;
159 local float blend, maxspeed;
162 sk = skill + self.bot_moveskill;
164 maxspeed = cvar("sv_maxspeed");
166 if (time < self.havocbot_keyboardtime)
169 self.havocbot_keyboardtime =
171 self.havocbot_keyboardtime
172 + 0.05/max(1, sk+self.havocbot_keyboardskill)
173 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
175 keyboard = self.movement * (1.0 / maxspeed);
177 local float trigger, trigger1;
178 blend = bound(0,sk*0.1,1);
179 trigger = cvar("bot_ai_keyboard_treshold");
180 trigger1 = 0 - trigger;
182 // categorize forward movement
183 // at skill < 1.5 only forward
184 // at skill < 2.5 only individual directions
185 // at skill < 4.5 only individual directions, and forward diagonals
186 // at skill >= 4.5, all cases allowed
187 if (keyboard_x > trigger)
193 else if (keyboard_x < trigger1 && sk > 1.5)
208 if (keyboard_y > trigger)
210 else if (keyboard_y < trigger1)
215 if (keyboard_z > trigger)
217 else if (keyboard_z < trigger1)
222 self.havocbot_keyboard = keyboard * maxspeed;
223 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
225 keyboard = self.havocbot_keyboard;
226 blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
227 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
228 self.movement = self.movement + (keyboard - self.movement) * blend;
231 void havocbot_bunnyhop(vector dir)
233 local float bunnyhopdistance;
234 local vector deviation;
235 local float maxspeed;
240 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(self.flags & FL_ONGROUND == 0)
344 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
345 self.BUTTON_JUMP = FALSE;
348 if(self.aistatus & AI_STATUS_RUNNING)
349 if(vlen(self.velocity)>maxspeed)
351 deviation = vectoangles(dir) - vectoangles(self.velocity);
352 while (deviation_y < -180) deviation_y = deviation_y + 360;
353 while (deviation_y > 180) deviation_y = deviation_y - 360;
355 if(fabs(deviation_y)>10)
359 self.movement_y = maxspeed * -1;
360 else if(deviation_y<10)
361 self.movement_y = maxspeed;
367 void havocbot_movetogoal()
369 local vector destorg;
372 local vector flatdir;
375 local vector evadeobstacle;
376 local vector evadelava;
378 local float maxspeed;
381 //if (self.goalentity)
382 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
383 self.movement = '0 0 0';
384 maxspeed = cvar("sv_maxspeed");
386 // Jetpack navigation
388 if(self.navigation_jetpack_goal)
389 if(self.goalcurrent==self.navigation_jetpack_goal)
392 #ifdef DEBUG_BOT_GOALSTACK
394 te_wizspike(self.navigation_jetpack_point);
398 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
400 // Brake almost completely so it can get a good direction
401 if(vlen(self.velocity)>10)
403 self.aistatus |= AI_STATUS_JETPACK_FLYING;
406 makevectors(self.v_angle_y * '0 1 0');
407 dir = normalize(self.navigation_jetpack_point - self.origin);
410 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
412 // Calculate brake distance in xy
416 dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
418 v = vlen(self.velocity - self.velocity_z * '0 0 1');
419 db = (pow(v,2) / (cvar("g_jetpack_acceleration_side") * 2)) + 100;
420 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
421 if(d < db || d < 500)
424 if(fabs(self.velocity_x)>maxspeed*0.3)
426 self.movement_x = dir * v_forward * -maxspeed;
429 // Switch to normal mode
430 self.navigation_jetpack_goal = world;
431 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
432 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
436 else if(checkpvs(self.origin,self.goalcurrent))
438 // If I can see the goal switch to landing code
439 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
440 self.aistatus |= AI_STATUS_JETPACK_LANDING;
445 self.BUTTON_JETPACK = TRUE;
446 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
448 self.movement_x = dir * v_forward * maxspeed;
449 self.movement_y = dir * v_right * maxspeed;
454 // Handling of jump pads
455 if(self.jumppadcount)
457 if(self.flags & FL_ONGROUND)
459 self.jumppadcount = FALSE;
460 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
461 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
464 // If got stuck on the jump pad try to reach the farther visible item
465 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
467 if(fabs(self.velocity_z)<50)
469 local entity head, newgoal;
470 local float distance, bestdistance;
472 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
474 if(head.classname=="worldspawn")
477 distance = vlen(head.origin - self.origin);
481 traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
486 if(distance>bestdistance)
489 bestdistance = distance;
495 self.ignoregoal = self.goalcurrent;
496 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
497 navigation_clearroute();
498 navigation_routetogoal(newgoal, self.origin);
499 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
507 if(self.velocity_z>0)
509 local float threshold;
510 threshold = maxspeed * 0.2;
511 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
512 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
518 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
520 if not(self.flags & FL_ONGROUND)
522 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
523 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
524 if(self.items & IT_JETPACK)
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 + '0 0 1' ))
529 if(self.velocity_z<0)
531 self.BUTTON_JETPACK = TRUE;
535 self.BUTTON_JETPACK = TRUE;
537 // If there is no goal try to move forward
539 if(self.goalcurrent==world)
542 dir = normalize(self.goalcurrent.origin - self.origin);
544 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
545 local float xyspeed = xyvelocity * dir;
547 if(xyspeed < (maxspeed / 2))
549 makevectors(self.v_angle_y * '0 1 0');
550 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
551 if(trace_fraction==1)
553 self.movement_x = dir * v_forward * maxspeed;
554 self.movement_y = dir * v_right * maxspeed;
556 havocbot_keyboard_movement(self.origin + dir * 100);
560 self.havocbot_blockhead = TRUE;
566 // If there is no goal try to move forward
567 if(self.goalcurrent==world)
568 self.movement_x = maxspeed;
572 // If we are under water with no goals, swim up
574 if(self.goalcurrent==world)
577 if(self.waterlevel>WATERLEVEL_SWIMMING)
579 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
580 self.BUTTON_JUMP = TRUE;
582 self.BUTTON_JUMP = FALSE;
583 makevectors(self.v_angle_y * '0 1 0');
584 self.movement_x = dir * v_forward * maxspeed;
585 self.movement_y = dir * v_right * maxspeed;
586 self.movement_z = dir * v_up * maxspeed;
589 // if there is nowhere to go, exit
590 if (self.goalcurrent == world)
593 if (self.goalcurrent)
594 navigation_poptouchedgoals();
596 // if ran out of goals try to use an alternative goal or get a new strategy asap
597 if(self.goalcurrent == world)
599 self.bot_strategytime = 0;
603 #ifdef DEBUG_BOT_GOALSTACK
607 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
608 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
609 destorg = self.origin;
610 destorg_x = bound(m1_x, destorg_x, m2_x);
611 destorg_y = bound(m1_y, destorg_y, m2_y);
612 destorg_z = bound(m1_z, destorg_z, m2_z);
613 diff = destorg - self.origin;
615 dir = normalize(diff);
616 flatdir = diff;flatdir_z = 0;
617 flatdir = normalize(flatdir);
619 //if (self.bot_dodgevector_time < time)
621 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
622 // self.bot_dodgevector_jumpbutton = 1;
623 evadeobstacle = '0 0 0';
628 if(self.waterlevel>WATERLEVEL_SWIMMING)
631 self.aistatus |= AI_STATUS_OUT_WATER;
635 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
636 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
637 self.BUTTON_JUMP = TRUE;
639 self.BUTTON_JUMP = FALSE;
641 dir = normalize(flatdir);
642 makevectors(self.v_angle_y * '0 1 0');
646 if(self.aistatus & AI_STATUS_OUT_WATER)
647 self.aistatus &~= AI_STATUS_OUT_WATER;
649 // jump if going toward an obstacle that doesn't look like stairs we
650 // can walk up directly
651 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
652 if (trace_fraction < 1)
653 if (trace_plane_normal_z < 0.7)
656 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
657 if (trace_fraction < s + 0.01)
658 if (trace_plane_normal_z < 0.7)
661 tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
662 if (trace_fraction > s)
663 self.BUTTON_JUMP = 1;
667 // avoiding dangers and obstacles
668 local vector dst_ahead, dst_down;
669 makevectors(self.v_angle_y * '0 1 0');
670 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
671 dst_down = dst_ahead + '0 0 -1500';
674 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
676 // Check head-banging against walls
677 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
679 self.BUTTON_JUMP = TRUE;
680 if(self.facingwalltime && time > self.facingwalltime)
682 self.ignoregoal = self.goalcurrent;
683 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
684 self.bot_strategytime = 0;
689 self.facingwalltime = time + 0.05;
694 self.facingwalltime = 0;
696 if(self.ignoregoal != world && time > self.ignoregoaltime)
698 self.ignoregoal = world;
699 self.ignoregoaltime = 0;
703 // Check for water/slime/lava and dangerous edges
704 // (only when the bot is on the ground or jumping intentionally)
705 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
707 if(trace_fraction == 1)
708 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
711 traceline(dst_ahead , dst_down, TRUE, world);
712 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
713 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
714 if(trace_endpos_z < self.origin_z + self.mins_z)
716 s = pointcontents(trace_endpos + '0 0 1');
717 if (s != CONTENT_SOLID)
718 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
719 evadelava = normalize(self.velocity) * -1;
720 else if (s == CONTENT_SKY)
721 evadeobstacle = normalize(self.velocity) * -1;
722 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
723 self.goalcurrent.absmin, self.goalcurrent.absmax))
725 // if ain't a safe goal with "holes" (like the jumpad on soylent)
726 // and there is a trigger_hurt below
727 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
729 // Remove dangerous dynamic goals from stack
730 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
731 navigation_poproute();
734 evadeobstacle = normalize(self.velocity) * -1;
743 makevectors(self.v_angle_y * '0 1 0');
745 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
746 self.aistatus |= AI_STATUS_DANGER_AHEAD;
749 dodge = havocbot_dodge();
750 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
751 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
752 traceline(self.origin, self.enemy.origin, TRUE, world);
753 if(trace_ent.classname == "player")
754 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
756 dir = normalize(dir + dodge + evadeobstacle + evadelava);
757 // self.bot_dodgevector = dir;
758 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
761 if(time < self.ladder_time)
763 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
765 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
770 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
775 //dir = self.bot_dodgevector;
776 //if (self.bot_dodgevector_jumpbutton)
777 // self.BUTTON_JUMP = 1;
778 self.movement_x = dir * v_forward * maxspeed;
779 self.movement_y = dir * v_right * maxspeed;
780 self.movement_z = dir * v_up * maxspeed;
782 // Emulate keyboard interface
784 havocbot_keyboard_movement(destorg);
787 // if(self.aistatus & AI_STATUS_ROAMING)
789 if(skill+self.bot_moveskill >= cvar("bot_ai_bunnyhop_skilloffset"))
790 havocbot_bunnyhop(dir);
792 if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
793 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
794 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);
797 void havocbot_chooseenemy()
799 local entity head, best, head2;
800 local float rating, bestrating, i, f;
802 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
809 if (!bot_shouldattack(self.enemy))
811 // enemy died or something, find a new target
813 self.havocbot_chooseenemy_finished = time;
815 else if (self.havocbot_stickenemy)
817 // tracking last chosen enemy
818 // if enemy is visible
819 // and not really really far away
820 // and we're not severely injured
821 // then keep tracking for a half second into the future
822 traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
823 if (trace_ent == self.enemy || trace_fraction == 1)
824 if (vlen(self.enemy.origin - self.origin) < 1000)
825 if (self.health > 30)
827 // remain tracking him for a shot while (case he went after a small corner or pilar
828 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
831 // enemy isn't visible, or is far away, or we're injured severely
832 // so stop preferring this enemy
833 // (it will still take a half second until a new one is chosen)
834 self.havocbot_stickenemy = 0;
837 if (time < self.havocbot_chooseenemy_finished)
839 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
840 eye = self.origin + self.view_ofs;
842 bestrating = 100000000;
843 head = head2 = findchainfloat(bot_attack, TRUE);
845 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
850 v = (head.absmin + head.absmax) * 0.5;
851 rating = vlen(v - eye);
852 if (rating<cvar("bot_ai_enemydetectionradius"))
853 if (bestrating > rating)
854 if (bot_shouldattack(head))
856 traceline(eye, v, TRUE, self);
857 if (trace_ent == head || trace_fraction >= 1)
866 // I want to do a second scan if no enemy was found or I don't have weapons
867 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
868 if(best || self.weapons)
873 // Set flags to see through transparent objects
874 f = self.dphitcontentsmask;
875 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
881 // Restore hit flags if needed
883 self.dphitcontentsmask = f;
886 self.havocbot_stickenemy = TRUE;
889 void havocbot_chooseweapon()
895 // TODO: clean this up by moving it to weapon code
896 if(self.enemy==world)
898 // If no weapon was chosen get the first available weapon
900 for(i=WEP_GRABBER + 1; i < WEP_COUNT ; ++i)
902 if(client_hasweapon(self, i, TRUE, FALSE))
904 self.switchweapon = i;
911 // Do not change weapon during the next second after a combo
912 i = time - self.lastcombotime;
917 local float grabbergun ; grabbergun =-1000;
918 local float bestscore; bestscore = 0;
919 local float bestweapon; bestweapon=self.switchweapon;
920 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
921 local float maxdelaytime=0.5;
922 local float spreadpenalty=10;
924 // Should it do a weapon combo?
925 local float af, ct, combo_time, combo;
927 af = ATTACK_FINISHED(self);
928 ct = cvar("bot_ai_weapon_combo_threshold");
930 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
931 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
932 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
936 if(cvar("bot_ai_weapon_combo"))
937 if(self.weapon == self.lastfiredweapon)
941 self.lastcombotime = time;
944 distance *= pow(2, self.bot_rangepreference);
946 // Custom weapon list based on distance to the enemy
947 if(bot_custom_weapon){
949 // Choose weapons for far distance
950 if ( distance > bot_distance_far ) {
951 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
952 w = bot_weapons_far[i];
953 if ( client_hasweapon(self, w, TRUE, FALSE) ){
954 if ( self.weapon == w && combo)
956 self.switchweapon = w;
962 // Choose weapons for mid distance
963 if ( distance > bot_distance_close) {
964 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
965 w = bot_weapons_mid[i];
966 if ( client_hasweapon(self, w, TRUE, FALSE) ){
967 if ( self.weapon == w && combo)
969 self.switchweapon = w;
975 // Choose weapons for close distance
976 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
977 w = bot_weapons_close[i];
978 if ( client_hasweapon(self, w, TRUE, FALSE) ){
979 if ( self.weapon == w && combo)
981 self.switchweapon = w;
988 // TODO: This disabled code is not working well and got replaced by custom weapon priorities.
989 // However, this logic should be refactored and moved to weapons code so each new weapon can be
990 // evaluated dynamically by bots without updating the "ai" or config files. --mand1nga
991 float s, distancefromfloor, currentscore;
995 // (Damage/Sec * Weapon spefic change to get that damage)
996 // *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
997 // *(Spread change of hit) // if it applies
998 // *(Penality for target beeing in air)
1001 traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
1002 distancefromfloor = self.enemy.origin_z - trace_endpos_z;
1004 if (client_hasweapon(self, WEP_GRABBER, TRUE, FALSE) )
1005 grabbergun = (cvar("g_balance_grabber_primary_animtime")/cvar("g_balance_grabber_primary_refire")*1.0);
1007 dprint("Floor distance: ",ftos(distancefromfloor),"\n");
1008 dprint("Grabber: " , ftos(grabbergun ), "\n");
1011 w = WEP_GRABBER ;s = grabbergun;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1013 // switch if the best weapon would provide a significant damage increase
1014 if (bestscore > currentscore*1.5){
1015 self.switchweapon = bestweapon;
1022 local vector selfvel, enemyvel;
1023 // if(self.flags & FL_INWATER)
1025 if (time < self.nextaim)
1027 self.nextaim = time + 0.1;
1028 selfvel = self.velocity;
1029 if (!self.waterlevel)
1033 enemyvel = self.enemy.velocity;
1034 if (!self.enemy.waterlevel)
1036 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1039 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1042 float havocbot_moveto_refresh_route()
1044 // Refresh path to goal if necessary
1046 wp = self.havocbot_personal_waypoint;
1047 navigation_goalrating_start();
1048 navigation_routerating(wp, 10000, 10000);
1049 navigation_goalrating_end();
1050 return self.navigation_hasgoals;
1053 float havocbot_moveto(vector pos)
1057 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1059 // Step 4: Move to waypoint
1060 if(self.havocbot_personal_waypoint==world)
1062 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1063 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1064 return CMD_STATUS_ERROR;
1067 if (!bot_strategytoken_taken)
1068 if(self.havocbot_personal_waypoint_searchtime<time)
1070 bot_strategytoken_taken = TRUE;
1071 if(havocbot_moveto_refresh_route())
1073 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1074 self.havocbot_personal_waypoint_searchtime = time + 10;
1075 self.havocbot_personal_waypoint_failcounter = 0;
1079 self.havocbot_personal_waypoint_failcounter += 1;
1080 self.havocbot_personal_waypoint_searchtime = time + 2;
1081 if(self.havocbot_personal_waypoint_failcounter >= 30)
1083 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1084 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1085 remove(self.havocbot_personal_waypoint);
1086 return CMD_STATUS_ERROR;
1089 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1093 #ifdef DEBUG_BOT_GOALSTACK
1098 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1100 bot_aimdir(dir, -1);
1103 havocbot_movetogoal();
1105 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1107 // Step 5: Waypoint reached
1108 dprint(self.netname, "'s personal waypoint reached\n");
1109 remove(self.havocbot_personal_waypoint);
1110 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1111 return CMD_STATUS_FINISHED;
1114 return CMD_STATUS_EXECUTING;
1117 // Step 2: Linking waypoint
1118 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1120 // Wait until it is linked
1121 if(!self.havocbot_personal_waypoint.wplinked)
1123 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1124 return CMD_STATUS_EXECUTING;
1127 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1128 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1129 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1131 // Step 3: Route to waypoint
1132 dprint(self.netname, " walking to its personal waypoint\n");
1134 return CMD_STATUS_EXECUTING;
1137 // Step 1: Spawning waypoint
1138 wp = waypoint_spawnpersonal(pos);
1141 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1142 return CMD_STATUS_ERROR;
1145 self.havocbot_personal_waypoint = wp;
1146 self.havocbot_personal_waypoint_failcounter = 0;
1147 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1149 // if pos is inside a teleport, then let's mark it as teleport waypoint
1151 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1153 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1155 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1156 self.lastteleporttime = 0;
1161 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1162 print("routing to a teleporter\n");
1164 print("routing to a non-teleporter\n");
1167 return CMD_STATUS_EXECUTING;
1170 float havocbot_resetgoal()
1172 navigation_clearroute();
1173 return CMD_STATUS_FINISHED;
1176 void havocbot_setupbot()
1178 self.bot_ai = havocbot_ai;
1179 self.cmd_moveto = havocbot_moveto;
1180 self.cmd_resetgoal = havocbot_resetgoal;
1182 havocbot_chooserole();
1185 vector havocbot_dodge()
1187 // LordHavoc: disabled because this is too expensive
1191 local vector dodge, v, n;
1192 local float danger, bestdanger, vl, d;
1195 // check for dangerous objects near bot or approaching bot
1196 head = findchainfloat(bot_dodge, TRUE);
1199 if (head.owner != self)
1201 vl = vlen(head.velocity);
1202 if (vl > sv_maxspeed * 0.3)
1204 n = normalize(head.velocity);
1205 v = self.origin - head.origin;
1207 if (d > (0 - head.bot_dodgerating))
1208 if (d < (vl * 0.2 + head.bot_dodgerating))
1210 // calculate direction and distance from the flight path, by removing the forward axis
1211 v = v - (n * (v * n));
1212 danger = head.bot_dodgerating - vlen(v);
1213 if (bestdanger < danger)
1215 bestdanger = danger;
1216 // dodge to the side of the object
1217 dodge = normalize(v);
1223 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1224 if (bestdanger < danger)
1226 bestdanger = danger;
1227 dodge = normalize(self.origin - head.origin);