3 #include "role_onslaught.qc"
4 #include "role_keyhunt.qc"
5 #include "role_freezetag.qc"
6 #include "role_keepaway.qc"
7 #include "role_assault.qc"
15 if(bot_execute_commands())
18 if (bot_strategytoken == self)
19 if (!bot_strategytoken_taken)
21 if(self.havocbot_blockhead)
23 self.havocbot_blockhead = FALSE;
27 if not(self.jumppadcount)
31 // TODO: tracewalk() should take care of this job (better path finding under water)
32 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
33 if(self.deadflag != DEAD_NO)
34 if(self.goalcurrent==world)
35 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
37 // Look for the closest waypoint out of water
38 local entity newgoal, head;
39 local float bestdistance, distance;
43 for (head = findchain(classname, "waypoint"); head; head = head.chain)
45 distance = vlen(head.origin - self.origin);
49 if(head.origin_z < self.origin_z)
52 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
55 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
58 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
63 if(distance<bestdistance)
66 bestdistance = distance;
72 // te_wizspike(newgoal.origin);
73 navigation_pushroute(newgoal);
77 // token has been used this frame
78 bot_strategytoken_taken = TRUE;
81 if(self.deadflag != DEAD_NO)
84 havocbot_chooseenemy();
85 if (self.bot_chooseweapontime < time )
87 self.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
88 havocbot_chooseweapon();
94 self.aistatus |= AI_STATUS_ATTACKING;
95 self.aistatus &~= AI_STATUS_ROAMING;
99 weapon_action(self.weapon, WR_AIM);
100 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
102 self.BUTTON_ATCK = FALSE;
103 self.BUTTON_ATCK2 = FALSE;
107 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
108 self.lastfiredweapon = self.weapon;
113 if(self.bot_aimtarg.classname=="player")
114 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
117 else if (self.goalcurrent)
119 self.aistatus |= AI_STATUS_ROAMING;
120 self.aistatus &~= AI_STATUS_ATTACKING;
122 local vector now,v,next;//,heading;
123 local float aimdistance,skillblend,distanceblend,blend;
124 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
125 aimdistance = vlen(now);
126 //heading = self.velocity;
127 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
129 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
130 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
132 next = self.goalstack01.origin - (self.origin + self.view_ofs);
134 skillblend=bound(0,(skill+self.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
135 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
136 blend = skillblend * (1-distanceblend);
137 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
138 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
139 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
140 v = now + blend * (next - now);
141 //dprint(etos(self), " ");
142 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
143 //v = now * (distanceblend) + next * (1-distanceblend);
144 if (self.waterlevel < WATERLEVEL_SWIMMING)
146 //dprint("walk at:", vtos(v), "\n");
147 //te_lightning2(world, self.origin, self.goalcurrent.origin);
150 havocbot_movetogoal();
152 // if the bot is not attacking, consider reloading weapons
153 if not(self.aistatus & AI_STATUS_ATTACKING)
158 // we are currently holding a weapon that's not fully loaded, reload it
159 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
160 if(self.clip_load < self.clip_size)
161 self.impulse = 20; // "press" the reload button, not sure if this is done right
163 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
164 // the code above executes next frame, starting the reloading then
165 if(skill >= 5) // bots can only look for unloaded weapons past this skill
166 if(self.clip_load >= 0) // only if we're not reloading a weapon already
168 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
170 e = get_weaponinfo(i);
171 if ((e.spawnflags & WEP_FLAG_RELOADABLE) && (self.weapon_load[i] < cvar(strcat("g_balance_", e.netname, "_reload_ammo"))))
172 self.switchweapon = i;
178 void havocbot_keyboard_movement(vector destorg)
180 local vector keyboard;
181 local float blend, maxspeed;
184 sk = skill + self.bot_moveskill;
186 maxspeed = autocvar_sv_maxspeed;
188 if (time < self.havocbot_keyboardtime)
191 self.havocbot_keyboardtime =
193 self.havocbot_keyboardtime
194 + 0.05/max(1, sk+self.havocbot_keyboardskill)
195 + random()*0.025/max(0.00025, skill+self.havocbot_keyboardskill)
197 keyboard = self.movement * (1.0 / maxspeed);
199 local float trigger, trigger1;
200 blend = bound(0,sk*0.1,1);
201 trigger = autocvar_bot_ai_keyboard_threshold;
202 trigger1 = 0 - trigger;
204 // categorize forward movement
205 // at skill < 1.5 only forward
206 // at skill < 2.5 only individual directions
207 // at skill < 4.5 only individual directions, and forward diagonals
208 // at skill >= 4.5, all cases allowed
209 if (keyboard_x > trigger)
215 else if (keyboard_x < trigger1 && sk > 1.5)
230 if (keyboard_y > trigger)
232 else if (keyboard_y < trigger1)
237 if (keyboard_z > trigger)
239 else if (keyboard_z < trigger1)
244 self.havocbot_keyboard = keyboard * maxspeed;
245 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
247 keyboard = self.havocbot_keyboard;
248 blend = bound(0,vlen(destorg-self.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
249 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
250 self.movement = self.movement + (keyboard - self.movement) * blend;
253 void havocbot_bunnyhop(vector dir)
255 local float bunnyhopdistance;
256 local vector deviation;
257 local float maxspeed;
259 if(autocvar_g_midair)
262 // Don't jump when using some weapons
264 if(self.aistatus & AI_STATUS_ATTACKING)
265 if(self.weapon == WEP_RIFLE)
268 if(self.goalcurrent.classname == "player")
272 maxspeed = autocvar_sv_maxspeed;
274 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
276 self.aistatus &~= AI_STATUS_RUNNING;
277 self.BUTTON_JUMP = FALSE;
278 self.bot_canruntogoal = 0;
279 self.bot_timelastseengoal = 0;
283 if(self.waterlevel > WATERLEVEL_WETFEET)
285 self.aistatus &~= AI_STATUS_RUNNING;
289 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
291 self.bot_canruntogoal = 0;
292 self.bot_timelastseengoal = 0;
295 bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
297 // Run only to visible goals
298 if(self.flags & FL_ONGROUND)
299 if(self.speed==maxspeed)
300 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
302 self.bot_lastseengoal = self.goalcurrent;
305 if(self.bot_timelastseengoal)
307 // for a period of time
308 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
310 local float checkdistance;
311 checkdistance = TRUE;
313 // don't run if it is too close
314 if(self.bot_canruntogoal==0)
316 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
317 self.bot_canruntogoal = 1;
319 self.bot_canruntogoal = -1;
322 if(self.bot_canruntogoal != 1)
325 if(self.aistatus & AI_STATUS_ROAMING)
326 if(self.goalcurrent.classname=="waypoint")
327 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
328 if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
329 if(self.goalstack01!=world)
331 deviation = vectoangles(self.goalstack01.origin - self.origin) - vectoangles(self.goalcurrent.origin - self.origin);
332 while (deviation_y < -180) deviation_y = deviation_y + 360;
333 while (deviation_y > 180) deviation_y = deviation_y - 360;
335 if(fabs(deviation_y) < 20)
336 if(bunnyhopdistance < vlen(self.origin - self.goalstack01.origin))
337 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
339 if(vlen(self.goalcurrent.origin - self.goalstack01.origin) > autocvar_bot_ai_bunnyhop_startdistance)
340 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
342 checkdistance = FALSE;
349 self.aistatus &~= AI_STATUS_RUNNING;
350 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
351 self.BUTTON_JUMP = TRUE;
355 self.aistatus |= AI_STATUS_RUNNING;
356 self.BUTTON_JUMP = TRUE;
362 self.bot_timelastseengoal = time;
367 self.bot_timelastseengoal = 0;
370 // Release jump button
371 if(!cvar("sv_pogostick"))
372 if(self.flags & FL_ONGROUND == 0)
374 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
375 self.BUTTON_JUMP = FALSE;
378 if(self.aistatus & AI_STATUS_RUNNING)
379 if(vlen(self.velocity)>maxspeed)
381 deviation = vectoangles(dir) - vectoangles(self.velocity);
382 while (deviation_y < -180) deviation_y = deviation_y + 360;
383 while (deviation_y > 180) deviation_y = deviation_y - 360;
385 if(fabs(deviation_y)>10)
389 self.movement_y = maxspeed * -1;
390 else if(deviation_y<10)
391 self.movement_y = maxspeed;
397 void havocbot_movetogoal()
399 local vector destorg;
402 local vector flatdir;
405 local vector evadeobstacle;
406 local vector evadelava;
408 local float maxspeed;
411 //if (self.goalentity)
412 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
413 self.movement = '0 0 0';
414 maxspeed = autocvar_sv_maxspeed;
416 // Jetpack navigation
418 if(self.navigation_jetpack_goal)
419 if(self.goalcurrent==self.navigation_jetpack_goal)
422 if(autocvar_bot_debug_goalstack)
425 te_wizspike(self.navigation_jetpack_point);
429 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
431 // Brake almost completely so it can get a good direction
432 if(vlen(self.velocity)>10)
434 self.aistatus |= AI_STATUS_JETPACK_FLYING;
437 makevectors(self.v_angle_y * '0 1 0');
438 dir = normalize(self.navigation_jetpack_point - self.origin);
441 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
443 // Calculate brake distance in xy
447 dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
449 v = vlen(self.velocity - self.velocity_z * '0 0 1');
450 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
451 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
452 if(d < db || d < 500)
455 if(fabs(self.velocity_x)>maxspeed*0.3)
457 self.movement_x = dir * v_forward * -maxspeed;
460 // Switch to normal mode
461 self.navigation_jetpack_goal = world;
462 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
463 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
467 else if(checkpvs(self.origin,self.goalcurrent))
469 // If I can see the goal switch to landing code
470 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
471 self.aistatus |= AI_STATUS_JETPACK_LANDING;
476 self.BUTTON_HOOK = TRUE;
477 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
479 self.movement_x = dir * v_forward * maxspeed;
480 self.movement_y = dir * v_right * maxspeed;
485 // Handling of jump pads
486 if(self.jumppadcount)
488 // If got stuck on the jump pad try to reach the farthest visible item
489 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
491 if(fabs(self.velocity_z)<50)
493 local entity head, newgoal;
494 local float distance, bestdistance;
496 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
498 if(head.classname=="worldspawn")
501 distance = vlen(head.origin - self.origin);
505 traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
510 if(distance>bestdistance)
513 bestdistance = distance;
519 self.ignoregoal = self.goalcurrent;
520 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
521 navigation_clearroute();
522 navigation_routetogoal(newgoal, self.origin);
523 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
531 if(self.velocity_z>0)
533 local float threshold;
534 threshold = maxspeed * 0.2;
535 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
537 dprint("Warning: ", self.netname, " got stuck on a jumppad, trying to get out of it now\n");
538 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
543 // Don't chase players while using a jump pad
544 if(self.goalcurrent.classname=="player" || self.goalstack01.classname=="player")
548 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
549 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
551 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
553 if not(self.flags & FL_ONGROUND)
555 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
556 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
557 if(self.items & IT_JETPACK)
559 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
560 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
562 if(self.velocity_z<0)
564 self.BUTTON_HOOK = TRUE;
568 self.BUTTON_HOOK = TRUE;
570 // If there is no goal try to move forward
572 if(self.goalcurrent==world)
575 dir = normalize(self.goalcurrent.origin - self.origin);
577 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
578 local float xyspeed = xyvelocity * dir;
580 if(xyspeed < (maxspeed / 2))
582 makevectors(self.v_angle_y * '0 1 0');
583 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
584 if(trace_fraction==1)
586 self.movement_x = dir * v_forward * maxspeed;
587 self.movement_y = dir * v_right * maxspeed;
589 havocbot_keyboard_movement(self.origin + dir * 100);
593 self.havocbot_blockhead = TRUE;
597 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
599 if(self.velocity_z < 0)
600 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
602 self.movement_x = maxspeed;
604 if(self.rocketjumptime)
606 if(time > self.rocketjumptime)
608 self.BUTTON_ATCK2 = TRUE;
609 self.rocketjumptime = 0;
614 self.switchweapon = WEP_ROCKET_LAUNCHER;
616 self.BUTTON_ATCK = TRUE;
617 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
623 // If there is no goal try to move forward
624 if(self.goalcurrent==world)
625 self.movement_x = maxspeed;
629 // If we are under water with no goals, swim up
631 if(self.goalcurrent==world)
634 if(self.waterlevel>WATERLEVEL_SWIMMING)
636 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
637 self.BUTTON_JUMP = TRUE;
639 self.BUTTON_JUMP = FALSE;
640 makevectors(self.v_angle_y * '0 1 0');
641 self.movement_x = dir * v_forward * maxspeed;
642 self.movement_y = dir * v_right * maxspeed;
643 self.movement_z = dir * v_up * maxspeed;
646 // if there is nowhere to go, exit
647 if (self.goalcurrent == world)
650 if (self.goalcurrent)
651 navigation_poptouchedgoals();
653 // if ran out of goals try to use an alternative goal or get a new strategy asap
654 if(self.goalcurrent == world)
656 self.bot_strategytime = 0;
660 if(autocvar_bot_debug_goalstack)
663 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
664 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
665 destorg = self.origin;
666 destorg_x = bound(m1_x, destorg_x, m2_x);
667 destorg_y = bound(m1_y, destorg_y, m2_y);
668 destorg_z = bound(m1_z, destorg_z, m2_z);
669 diff = destorg - self.origin;
671 dir = normalize(diff);
672 flatdir = diff;flatdir_z = 0;
673 flatdir = normalize(flatdir);
675 //if (self.bot_dodgevector_time < time)
677 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
678 // self.bot_dodgevector_jumpbutton = 1;
679 evadeobstacle = '0 0 0';
684 if(self.waterlevel>WATERLEVEL_SWIMMING)
687 self.aistatus |= AI_STATUS_OUT_WATER;
691 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
692 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
693 self.BUTTON_JUMP = TRUE;
695 self.BUTTON_JUMP = FALSE;
697 dir = normalize(flatdir);
698 makevectors(self.v_angle_y * '0 1 0');
702 if(self.aistatus & AI_STATUS_OUT_WATER)
703 self.aistatus &~= AI_STATUS_OUT_WATER;
705 // jump if going toward an obstacle that doesn't look like stairs we
706 // can walk up directly
707 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
708 if (trace_fraction < 1)
709 if (trace_plane_normal_z < 0.7)
712 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
713 if (trace_fraction < s + 0.01)
714 if (trace_plane_normal_z < 0.7)
717 tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
718 if (trace_fraction > s)
719 self.BUTTON_JUMP = 1;
723 // avoiding dangers and obstacles
724 local vector dst_ahead, dst_down;
725 makevectors(self.v_angle_y * '0 1 0');
726 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
727 dst_down = dst_ahead + '0 0 -1500';
730 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
732 // Check head-banging against walls
733 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
735 self.BUTTON_JUMP = TRUE;
736 if(self.facingwalltime && time > self.facingwalltime)
738 self.ignoregoal = self.goalcurrent;
739 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
740 self.bot_strategytime = 0;
745 self.facingwalltime = time + 0.05;
750 self.facingwalltime = 0;
752 if(self.ignoregoal != world && time > self.ignoregoaltime)
754 self.ignoregoal = world;
755 self.ignoregoaltime = 0;
759 // Check for water/slime/lava and dangerous edges
760 // (only when the bot is on the ground or jumping intentionally)
761 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
763 if(trace_fraction == 1 && self.jumppadcount == 0)
764 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
767 traceline(dst_ahead , dst_down, TRUE, world);
768 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
769 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
770 if(trace_endpos_z < self.origin_z + self.mins_z)
772 s = pointcontents(trace_endpos + '0 0 1');
773 if (s != CONTENT_SOLID)
774 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
775 evadelava = normalize(self.velocity) * -1;
776 else if (s == CONTENT_SKY)
777 evadeobstacle = normalize(self.velocity) * -1;
778 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
779 self.goalcurrent.absmin, self.goalcurrent.absmax))
781 // if ain't a safe goal with "holes" (like the jumpad on soylent)
782 // and there is a trigger_hurt below
783 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
785 // Remove dangerous dynamic goals from stack
786 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
787 navigation_poproute();
790 evadeobstacle = normalize(self.velocity) * -1;
799 makevectors(self.v_angle_y * '0 1 0');
801 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
802 self.aistatus |= AI_STATUS_DANGER_AHEAD;
805 dodge = havocbot_dodge();
806 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
807 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
808 traceline(self.origin, self.enemy.origin, TRUE, world);
809 if(trace_ent.classname == "player")
810 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
812 dir = normalize(dir + dodge + evadeobstacle + evadelava);
813 // self.bot_dodgevector = dir;
814 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
817 if(time < self.ladder_time)
819 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
821 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
826 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
831 //dir = self.bot_dodgevector;
832 //if (self.bot_dodgevector_jumpbutton)
833 // self.BUTTON_JUMP = 1;
834 self.movement_x = dir * v_forward * maxspeed;
835 self.movement_y = dir * v_right * maxspeed;
836 self.movement_z = dir * v_up * maxspeed;
838 // Emulate keyboard interface
840 havocbot_keyboard_movement(destorg);
843 // if(self.aistatus & AI_STATUS_ROAMING)
845 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
846 havocbot_bunnyhop(dir);
848 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
849 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
850 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);
853 void havocbot_chooseenemy()
855 local entity head, best, head2;
856 local float rating, bestrating, i, f;
858 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
865 if (!bot_shouldattack(self.enemy))
867 // enemy died or something, find a new target
869 self.havocbot_chooseenemy_finished = time;
871 else if (self.havocbot_stickenemy)
873 // tracking last chosen enemy
874 // if enemy is visible
875 // and not really really far away
876 // and we're not severely injured
877 // then keep tracking for a half second into the future
878 traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
879 if (trace_ent == self.enemy || trace_fraction == 1)
880 if (vlen(self.enemy.origin - self.origin) < 1000)
881 if (self.health > 30)
883 // remain tracking him for a shot while (case he went after a small corner or pilar
884 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
887 // enemy isn't visible, or is far away, or we're injured severely
888 // so stop preferring this enemy
889 // (it will still take a half second until a new one is chosen)
890 self.havocbot_stickenemy = 0;
893 if (time < self.havocbot_chooseenemy_finished)
895 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
896 eye = self.origin + self.view_ofs;
898 bestrating = 100000000;
899 head = head2 = findchainfloat(bot_attack, TRUE);
901 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
906 v = (head.absmin + head.absmax) * 0.5;
907 rating = vlen(v - eye);
908 if (rating<autocvar_bot_ai_enemydetectionradius)
909 if (bestrating > rating)
910 if (bot_shouldattack(head))
912 traceline(eye, v, TRUE, self);
913 if (trace_ent == head || trace_fraction >= 1)
922 // I want to do a second scan if no enemy was found or I don't have weapons
923 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
924 if(best || self.weapons) // || self.weapon == WEP_RIFLE
929 // Set flags to see through transparent objects
930 f = self.dphitcontentsmask;
931 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
937 // Restore hit flags if needed
939 self.dphitcontentsmask = f;
942 self.havocbot_stickenemy = TRUE;
945 float havocbot_chooseweapon_checkreload(float new_weapon)
947 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
948 // so skip this for them, or they'll never get to reload their weapons at all.
949 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
953 // if this weapon is scheduled for reloading, don't switch to it during combat
954 if (self.weapon_load[new_weapon] < 0)
956 local float i, other_weapon_available;
957 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
959 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
960 if (weapon_action(i, WR_CHECKAMMO1) + weapon_action(i, WR_CHECKAMMO2))
961 other_weapon_available = TRUE;
963 if(other_weapon_available)
970 void havocbot_chooseweapon()
975 if(g_weaponarena == WEPBIT_TUBA)
977 self.switchweapon = WEP_TUBA;
981 // TODO: clean this up by moving it to weapon code
982 if(self.enemy==world)
984 // If no weapon was chosen get the first available weapon
986 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
988 if(client_hasweapon(self, i, TRUE, FALSE))
990 self.switchweapon = i;
997 // Do not change weapon during the next second after a combo
998 i = time - self.lastcombotime;
1003 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1005 // Should it do a weapon combo?
1006 local float af, ct, combo_time, combo;
1008 af = ATTACK_FINISHED(self);
1009 ct = autocvar_bot_ai_weapon_combo_threshold;
1011 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1012 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1013 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1017 if(autocvar_bot_ai_weapon_combo)
1018 if(self.weapon == self.lastfiredweapon)
1022 self.lastcombotime = time;
1025 distance *= pow(2, self.bot_rangepreference);
1027 // Custom weapon list based on distance to the enemy
1028 if(bot_custom_weapon){
1030 // Choose weapons for far distance
1031 if ( distance > bot_distance_far ) {
1032 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1033 w = bot_weapons_far[i];
1034 if ( client_hasweapon(self, w, TRUE, FALSE) )
1036 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1038 self.switchweapon = w;
1044 // Choose weapons for mid distance
1045 if ( distance > bot_distance_close) {
1046 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1047 w = bot_weapons_mid[i];
1048 if ( client_hasweapon(self, w, TRUE, FALSE) )
1050 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1052 self.switchweapon = w;
1058 // Choose weapons for close distance
1059 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1060 w = bot_weapons_close[i];
1061 if ( client_hasweapon(self, w, TRUE, FALSE) )
1063 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1065 self.switchweapon = w;
1074 local vector selfvel, enemyvel;
1075 // if(self.flags & FL_INWATER)
1077 if (time < self.nextaim)
1079 self.nextaim = time + 0.1;
1080 selfvel = self.velocity;
1081 if (!self.waterlevel)
1085 enemyvel = self.enemy.velocity;
1086 if (!self.enemy.waterlevel)
1088 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1091 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1094 float havocbot_moveto_refresh_route()
1096 // Refresh path to goal if necessary
1098 wp = self.havocbot_personal_waypoint;
1099 navigation_goalrating_start();
1100 navigation_routerating(wp, 10000, 10000);
1101 navigation_goalrating_end();
1102 return self.navigation_hasgoals;
1105 float havocbot_moveto(vector pos)
1109 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1111 // Step 4: Move to waypoint
1112 if(self.havocbot_personal_waypoint==world)
1114 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1115 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1116 return CMD_STATUS_ERROR;
1119 if (!bot_strategytoken_taken)
1120 if(self.havocbot_personal_waypoint_searchtime<time)
1122 bot_strategytoken_taken = TRUE;
1123 if(havocbot_moveto_refresh_route())
1125 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1126 self.havocbot_personal_waypoint_searchtime = time + 10;
1127 self.havocbot_personal_waypoint_failcounter = 0;
1131 self.havocbot_personal_waypoint_failcounter += 1;
1132 self.havocbot_personal_waypoint_searchtime = time + 2;
1133 if(self.havocbot_personal_waypoint_failcounter >= 30)
1135 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1136 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1137 remove(self.havocbot_personal_waypoint);
1138 return CMD_STATUS_ERROR;
1141 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1145 if(autocvar_bot_debug_goalstack)
1149 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1151 bot_aimdir(dir, -1);
1154 havocbot_movetogoal();
1156 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1158 // Step 5: Waypoint reached
1159 dprint(self.netname, "'s personal waypoint reached\n");
1160 remove(self.havocbot_personal_waypoint);
1161 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1162 return CMD_STATUS_FINISHED;
1165 return CMD_STATUS_EXECUTING;
1168 // Step 2: Linking waypoint
1169 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1171 // Wait until it is linked
1172 if(!self.havocbot_personal_waypoint.wplinked)
1174 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1175 return CMD_STATUS_EXECUTING;
1178 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1179 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1180 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1182 // Step 3: Route to waypoint
1183 dprint(self.netname, " walking to its personal waypoint\n");
1185 return CMD_STATUS_EXECUTING;
1188 // Step 1: Spawning waypoint
1189 wp = waypoint_spawnpersonal(pos);
1192 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1193 return CMD_STATUS_ERROR;
1196 self.havocbot_personal_waypoint = wp;
1197 self.havocbot_personal_waypoint_failcounter = 0;
1198 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1200 // if pos is inside a teleport, then let's mark it as teleport waypoint
1202 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1204 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1206 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1207 self.lastteleporttime = 0;
1212 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1213 print("routing to a teleporter\n");
1215 print("routing to a non-teleporter\n");
1218 return CMD_STATUS_EXECUTING;
1221 float havocbot_resetgoal()
1223 navigation_clearroute();
1224 return CMD_STATUS_FINISHED;
1227 void havocbot_setupbot()
1229 self.bot_ai = havocbot_ai;
1230 self.cmd_moveto = havocbot_moveto;
1231 self.cmd_resetgoal = havocbot_resetgoal;
1233 havocbot_chooserole();
1236 vector havocbot_dodge()
1238 // LordHavoc: disabled because this is too expensive
1242 local vector dodge, v, n;
1243 local float danger, bestdanger, vl, d;
1246 // check for dangerous objects near bot or approaching bot
1247 head = findchainfloat(bot_dodge, TRUE);
1250 if (head.owner != self)
1252 vl = vlen(head.velocity);
1253 if (vl > autocvar_sv_maxspeed * 0.3)
1255 n = normalize(head.velocity);
1256 v = self.origin - head.origin;
1258 if (d > (0 - head.bot_dodgerating))
1259 if (d < (vl * 0.2 + head.bot_dodgerating))
1261 // calculate direction and distance from the flight path, by removing the forward axis
1262 v = v - (n * (v * n));
1263 danger = head.bot_dodgerating - vlen(v);
1264 if (bestdanger < danger)
1266 bestdanger = danger;
1267 // dodge to the side of the object
1268 dodge = normalize(v);
1274 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1275 if (bestdanger < danger)
1277 bestdanger = danger;
1278 dodge = normalize(self.origin - head.origin);