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.absmin + self.goalcurrent.absmax) * 0.5) - (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.absmin + self.goalstack01.absmax) * 0.5) - (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;
260 if(autocvar_g_midair)
263 // Don't jump when using some weapons
265 if(self.aistatus & AI_STATUS_ATTACKING)
266 if(self.weapon == WEP_RIFLE)
269 if(self.goalcurrent.classname == "player")
273 maxspeed = autocvar_sv_maxspeed;
275 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
277 self.aistatus &~= AI_STATUS_RUNNING;
278 self.BUTTON_JUMP = FALSE;
279 self.bot_canruntogoal = 0;
280 self.bot_timelastseengoal = 0;
284 if(self.waterlevel > WATERLEVEL_WETFEET)
286 self.aistatus &~= AI_STATUS_RUNNING;
290 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
292 self.bot_canruntogoal = 0;
293 self.bot_timelastseengoal = 0;
296 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
297 bunnyhopdistance = vlen(self.origin - gco);
299 // Run only to visible goals
300 if(self.flags & FL_ONGROUND)
301 if(self.speed==maxspeed)
302 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
304 self.bot_lastseengoal = self.goalcurrent;
307 if(self.bot_timelastseengoal)
309 // for a period of time
310 if(time - self.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
312 local float checkdistance;
313 checkdistance = TRUE;
315 // don't run if it is too close
316 if(self.bot_canruntogoal==0)
318 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
319 self.bot_canruntogoal = 1;
321 self.bot_canruntogoal = -1;
324 if(self.bot_canruntogoal != 1)
327 if(self.aistatus & AI_STATUS_ROAMING)
328 if(self.goalcurrent.classname=="waypoint")
329 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
330 if(fabs(gco_z - self.origin_z) < self.maxs_z - self.mins_z)
331 if(self.goalstack01!=world)
333 gno = (self.goalstack01.absmin + self.goalstack01.absmax) * 0.5;
334 deviation = vectoangles(gno - self.origin) - vectoangles(gco - self.origin);
335 while (deviation_y < -180) deviation_y = deviation_y + 360;
336 while (deviation_y > 180) deviation_y = deviation_y - 360;
338 if(fabs(deviation_y) < 20)
339 if(bunnyhopdistance < vlen(self.origin - gno))
340 if(fabs(gno_z - gco_z) < self.maxs_z - self.mins_z)
342 if(vlen(gco - gno) > autocvar_bot_ai_bunnyhop_startdistance)
343 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
345 checkdistance = FALSE;
352 self.aistatus &~= AI_STATUS_RUNNING;
353 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
354 self.BUTTON_JUMP = TRUE;
358 self.aistatus |= AI_STATUS_RUNNING;
359 self.BUTTON_JUMP = TRUE;
365 self.bot_timelastseengoal = time;
370 self.bot_timelastseengoal = 0;
373 // Release jump button
374 if(!cvar("sv_pogostick"))
375 if(self.flags & FL_ONGROUND == 0)
377 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
378 self.BUTTON_JUMP = FALSE;
381 if(self.aistatus & AI_STATUS_RUNNING)
382 if(vlen(self.velocity)>maxspeed)
384 deviation = vectoangles(dir) - vectoangles(self.velocity);
385 while (deviation_y < -180) deviation_y = deviation_y + 360;
386 while (deviation_y > 180) deviation_y = deviation_y - 360;
388 if(fabs(deviation_y)>10)
392 self.movement_y = maxspeed * -1;
393 else if(deviation_y<10)
394 self.movement_y = maxspeed;
400 void havocbot_movetogoal()
402 local vector destorg;
405 local vector flatdir;
408 local vector evadeobstacle;
409 local vector evadelava;
411 local float maxspeed;
415 //if (self.goalentity)
416 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
417 self.movement = '0 0 0';
418 maxspeed = autocvar_sv_maxspeed;
420 // Jetpack navigation
422 if(self.navigation_jetpack_goal)
423 if(self.goalcurrent==self.navigation_jetpack_goal)
426 if(autocvar_bot_debug_goalstack)
429 te_wizspike(self.navigation_jetpack_point);
433 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
435 // Brake almost completely so it can get a good direction
436 if(vlen(self.velocity)>10)
438 self.aistatus |= AI_STATUS_JETPACK_FLYING;
441 makevectors(self.v_angle_y * '0 1 0');
442 dir = normalize(self.navigation_jetpack_point - self.origin);
445 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
447 // Calculate brake distance in xy
451 dxy = self.origin - ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ); dxy_z = 0;
453 v = vlen(self.velocity - self.velocity_z * '0 0 1');
454 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
455 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
456 if(d < db || d < 500)
459 if(fabs(self.velocity_x)>maxspeed*0.3)
461 self.movement_x = dir * v_forward * -maxspeed;
464 // Switch to normal mode
465 self.navigation_jetpack_goal = world;
466 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
467 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
471 else if(checkpvs(self.origin,self.goalcurrent))
473 // If I can see the goal switch to landing code
474 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
475 self.aistatus |= AI_STATUS_JETPACK_LANDING;
480 self.BUTTON_HOOK = TRUE;
481 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
483 self.movement_x = dir * v_forward * maxspeed;
484 self.movement_y = dir * v_right * maxspeed;
489 // Handling of jump pads
490 if(self.jumppadcount)
492 // If got stuck on the jump pad try to reach the farthest visible item
493 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
495 if(fabs(self.velocity_z)<50)
497 local entity head, newgoal;
498 local float distance, bestdistance;
500 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
502 if(head.classname=="worldspawn")
505 distance = vlen(head.origin - self.origin);
509 traceline(self.origin + self.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), TRUE, world);
514 if(distance>bestdistance)
517 bestdistance = distance;
523 self.ignoregoal = self.goalcurrent;
524 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
525 navigation_clearroute();
526 navigation_routetogoal(newgoal, self.origin);
527 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
535 if(self.velocity_z>0)
537 local float threshold;
538 threshold = maxspeed * 0.2;
539 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
541 dprint("Warning: ", self.netname, " got stuck on a jumppad, trying to get out of it now\n");
542 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
547 // Don't chase players while using a jump pad
548 if(self.goalcurrent.classname=="player" || self.goalstack01.classname=="player")
552 else if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
553 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
555 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
557 if not(self.flags & FL_ONGROUND)
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 ))
561 if(self.items & IT_JETPACK)
563 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
564 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
566 if(self.velocity_z<0)
568 self.BUTTON_HOOK = TRUE;
572 self.BUTTON_HOOK = TRUE;
574 // If there is no goal try to move forward
576 if(self.goalcurrent==world)
579 dir = normalize(( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - self.origin);
581 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
582 local float xyspeed = xyvelocity * dir;
584 if(xyspeed < (maxspeed / 2))
586 makevectors(self.v_angle_y * '0 1 0');
587 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
588 if(trace_fraction==1)
590 self.movement_x = dir * v_forward * maxspeed;
591 self.movement_y = dir * v_right * maxspeed;
593 havocbot_keyboard_movement(self.origin + dir * 100);
597 self.havocbot_blockhead = TRUE;
601 else if(self.health>autocvar_g_balance_rocketlauncher_damage*0.5)
603 if(self.velocity_z < 0)
604 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
606 self.movement_x = maxspeed;
608 if(self.rocketjumptime)
610 if(time > self.rocketjumptime)
612 self.BUTTON_ATCK2 = TRUE;
613 self.rocketjumptime = 0;
618 self.switchweapon = WEP_ROCKET_LAUNCHER;
620 self.BUTTON_ATCK = TRUE;
621 self.rocketjumptime = time + autocvar_g_balance_rocketlauncher_detonatedelay;
627 // If there is no goal try to move forward
628 if(self.goalcurrent==world)
629 self.movement_x = maxspeed;
633 // If we are under water with no goals, swim up
635 if(self.goalcurrent==world)
638 if(self.waterlevel>WATERLEVEL_SWIMMING)
640 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
641 self.BUTTON_JUMP = TRUE;
643 self.BUTTON_JUMP = FALSE;
644 makevectors(self.v_angle_y * '0 1 0');
645 self.movement_x = dir * v_forward * maxspeed;
646 self.movement_y = dir * v_right * maxspeed;
647 self.movement_z = dir * v_up * maxspeed;
650 // if there is nowhere to go, exit
651 if (self.goalcurrent == world)
654 if (self.goalcurrent)
655 navigation_poptouchedgoals();
657 // if ran out of goals try to use an alternative goal or get a new strategy asap
658 if(self.goalcurrent == world)
660 self.bot_strategytime = 0;
665 if(autocvar_bot_debug_goalstack)
668 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
669 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
670 destorg = self.origin;
671 destorg_x = bound(m1_x, destorg_x, m2_x);
672 destorg_y = bound(m1_y, destorg_y, m2_y);
673 destorg_z = bound(m1_z, destorg_z, m2_z);
674 diff = destorg - self.origin;
676 dir = normalize(diff);
677 flatdir = diff;flatdir_z = 0;
678 flatdir = normalize(flatdir);
679 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
681 //if (self.bot_dodgevector_time < time)
683 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
684 // self.bot_dodgevector_jumpbutton = 1;
685 evadeobstacle = '0 0 0';
690 if(self.waterlevel>WATERLEVEL_SWIMMING)
693 self.aistatus |= AI_STATUS_OUT_WATER;
697 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && gco_z < self.origin_z) &&
698 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
699 self.BUTTON_JUMP = TRUE;
701 self.BUTTON_JUMP = FALSE;
703 dir = normalize(flatdir);
704 makevectors(self.v_angle_y * '0 1 0');
708 if(self.aistatus & AI_STATUS_OUT_WATER)
709 self.aistatus &~= AI_STATUS_OUT_WATER;
711 // jump if going toward an obstacle that doesn't look like stairs we
712 // can walk up directly
713 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
714 if (trace_fraction < 1)
715 if (trace_plane_normal_z < 0.7)
718 tracebox(self.origin + stepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + stepheightvec, FALSE, self);
719 if (trace_fraction < s + 0.01)
720 if (trace_plane_normal_z < 0.7)
723 tracebox(self.origin + jumpstepheightvec, self.mins, self.maxs, self.origin + self.velocity * 0.2 + jumpstepheightvec, FALSE, self);
724 if (trace_fraction > s)
725 self.BUTTON_JUMP = 1;
729 // avoiding dangers and obstacles
730 local vector dst_ahead, dst_down;
731 makevectors(self.v_angle_y * '0 1 0');
732 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
733 dst_down = dst_ahead + '0 0 -1500';
736 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
738 // Check head-banging against walls
739 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
741 self.BUTTON_JUMP = TRUE;
742 if(self.facingwalltime && time > self.facingwalltime)
744 self.ignoregoal = self.goalcurrent;
745 self.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
746 self.bot_strategytime = 0;
751 self.facingwalltime = time + 0.05;
756 self.facingwalltime = 0;
758 if(self.ignoregoal != world && time > self.ignoregoaltime)
760 self.ignoregoal = world;
761 self.ignoregoaltime = 0;
765 // Check for water/slime/lava and dangerous edges
766 // (only when the bot is on the ground or jumping intentionally)
767 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
769 if(trace_fraction == 1 && self.jumppadcount == 0)
770 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
773 traceline(dst_ahead , dst_down, TRUE, world);
774 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
775 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
776 if(trace_endpos_z < self.origin_z + self.mins_z)
778 s = pointcontents(trace_endpos + '0 0 1');
779 if (s != CONTENT_SOLID)
780 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
781 evadelava = normalize(self.velocity) * -1;
782 else if (s == CONTENT_SKY)
783 evadeobstacle = normalize(self.velocity) * -1;
784 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
785 self.goalcurrent.absmin, self.goalcurrent.absmax))
787 // if ain't a safe goal with "holes" (like the jumpad on soylent)
788 // and there is a trigger_hurt below
789 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
791 // Remove dangerous dynamic goals from stack
792 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
794 navigation_poproute();
796 gco = (self.goalcurrent.absmin + self.goalcurrent.absmax) * 0.5;
800 evadeobstacle = normalize(self.velocity) * -1;
809 makevectors(self.v_angle_y * '0 1 0');
811 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
812 self.aistatus |= AI_STATUS_DANGER_AHEAD;
815 dodge = havocbot_dodge();
816 dodge = dodge * bound(0,0.5+(skill+self.bot_dodgeskill)*0.1,1);
817 evadelava = evadelava * bound(1,3-(skill+self.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
818 traceline(self.origin, ( ( self.enemy.absmin + self.enemy.absmax ) * 0.5 ), TRUE, world);
819 if(trace_ent.classname == "player")
820 dir = dir * bound(0,(skill+self.bot_dodgeskill)/7,1);
822 dir = normalize(dir + dodge + evadeobstacle + evadelava);
823 // self.bot_dodgevector = dir;
824 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
827 if(time < self.ladder_time)
829 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
831 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
836 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
841 //dir = self.bot_dodgevector;
842 //if (self.bot_dodgevector_jumpbutton)
843 // self.BUTTON_JUMP = 1;
844 self.movement_x = dir * v_forward * maxspeed;
845 self.movement_y = dir * v_right * maxspeed;
846 self.movement_z = dir * v_up * maxspeed;
848 // Emulate keyboard interface
850 havocbot_keyboard_movement(destorg);
853 // if(self.aistatus & AI_STATUS_ROAMING)
855 if(skill+self.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
856 havocbot_bunnyhop(dir);
858 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
859 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-self.bot_dodgeskill)*0.1,1)) self.BUTTON_JUMP=TRUE;
860 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);
863 void havocbot_chooseenemy()
865 local entity head, best, head2;
866 local float rating, bestrating, i, f;
868 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(self))
875 if (!bot_shouldattack(self.enemy))
877 // enemy died or something, find a new target
879 self.havocbot_chooseenemy_finished = time;
881 else if (self.havocbot_stickenemy)
883 // tracking last chosen enemy
884 // if enemy is visible
885 // and not really really far away
886 // and we're not severely injured
887 // then keep tracking for a half second into the future
888 traceline(self.origin+self.view_ofs, ( self.enemy.absmin + self.enemy.absmax ) * 0.5,FALSE,world);
889 if (trace_ent == self.enemy || trace_fraction == 1)
890 if (vlen((( self.enemy.absmin + self.enemy.absmax ) * 0.5) - self.origin) < 1000)
891 if (self.health > 30)
893 // remain tracking him for a shot while (case he went after a small corner or pilar
894 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
897 // enemy isn't visible, or is far away, or we're injured severely
898 // so stop preferring this enemy
899 // (it will still take a half second until a new one is chosen)
900 self.havocbot_stickenemy = 0;
903 if (time < self.havocbot_chooseenemy_finished)
905 self.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
906 eye = self.origin + self.view_ofs;
908 bestrating = 100000000;
909 head = head2 = findchainfloat(bot_attack, TRUE);
911 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
916 v = (head.absmin + head.absmax) * 0.5;
917 rating = vlen(v - eye);
918 if (rating<autocvar_bot_ai_enemydetectionradius)
919 if (bestrating > rating)
920 if (bot_shouldattack(head))
922 traceline(eye, v, TRUE, self);
923 if (trace_ent == head || trace_fraction >= 1)
932 // I want to do a second scan if no enemy was found or I don't have weapons
933 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
934 if(best || self.weapons) // || self.weapon == WEP_RIFLE
939 // Set flags to see through transparent objects
940 f = self.dphitcontentsmask;
941 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
947 // Restore hit flags if needed
949 self.dphitcontentsmask = f;
952 self.havocbot_stickenemy = TRUE;
955 float havocbot_chooseweapon_checkreload(float new_weapon)
957 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
958 // so skip this for them, or they'll never get to reload their weapons at all.
959 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
963 // if this weapon is scheduled for reloading, don't switch to it during combat
964 if (self.weapon_load[new_weapon] < 0)
966 local float i, other_weapon_available;
967 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
969 // if we are out of ammo for all other weapons, it's an emergency to switch to anything else
970 if (weapon_action(i, WR_CHECKAMMO1) + weapon_action(i, WR_CHECKAMMO2))
971 other_weapon_available = TRUE;
973 if(other_weapon_available)
980 void havocbot_chooseweapon()
985 if(g_weaponarena == WEPBIT_TUBA)
987 self.switchweapon = WEP_TUBA;
991 // TODO: clean this up by moving it to weapon code
992 if(self.enemy==world)
994 // If no weapon was chosen get the first available weapon
996 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
998 if(client_hasweapon(self, i, TRUE, FALSE))
1000 self.switchweapon = i;
1007 // Do not change weapon during the next second after a combo
1008 i = time - self.lastcombotime;
1013 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
1015 // Should it do a weapon combo?
1016 local float af, ct, combo_time, combo;
1018 af = ATTACK_FINISHED(self);
1019 ct = autocvar_bot_ai_weapon_combo_threshold;
1021 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1022 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1023 combo_time = time + ct + (ct * ((-0.3*(skill+self.bot_weaponskill))+3));
1027 if(autocvar_bot_ai_weapon_combo)
1028 if(self.weapon == self.lastfiredweapon)
1032 self.lastcombotime = time;
1035 distance *= pow(2, self.bot_rangepreference);
1037 // Custom weapon list based on distance to the enemy
1038 if(bot_custom_weapon){
1040 // Choose weapons for far distance
1041 if ( distance > bot_distance_far ) {
1042 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
1043 w = bot_weapons_far[i];
1044 if ( client_hasweapon(self, w, TRUE, FALSE) )
1046 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1048 self.switchweapon = w;
1054 // Choose weapons for mid distance
1055 if ( distance > bot_distance_close) {
1056 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1057 w = bot_weapons_mid[i];
1058 if ( client_hasweapon(self, w, TRUE, FALSE) )
1060 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1062 self.switchweapon = w;
1068 // Choose weapons for close distance
1069 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1070 w = bot_weapons_close[i];
1071 if ( client_hasweapon(self, w, TRUE, FALSE) )
1073 if ((self.weapon == w && combo) || havocbot_chooseweapon_checkreload(w))
1075 self.switchweapon = w;
1084 local vector selfvel, enemyvel;
1085 // if(self.flags & FL_INWATER)
1087 if (time < self.nextaim)
1089 self.nextaim = time + 0.1;
1090 selfvel = self.velocity;
1091 if (!self.waterlevel)
1095 enemyvel = self.enemy.velocity;
1096 if (!self.enemy.waterlevel)
1098 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, (self.enemy.absmin + self.enemy.absmax) * 0.5, enemyvel);
1101 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5, '0 0 0');
1104 float havocbot_moveto_refresh_route()
1106 // Refresh path to goal if necessary
1108 wp = self.havocbot_personal_waypoint;
1109 navigation_goalrating_start();
1110 navigation_routerating(wp, 10000, 10000);
1111 navigation_goalrating_end();
1112 return self.navigation_hasgoals;
1115 float havocbot_moveto(vector pos)
1119 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1121 // Step 4: Move to waypoint
1122 if(self.havocbot_personal_waypoint==world)
1124 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1125 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1126 return CMD_STATUS_ERROR;
1129 if (!bot_strategytoken_taken)
1130 if(self.havocbot_personal_waypoint_searchtime<time)
1132 bot_strategytoken_taken = TRUE;
1133 if(havocbot_moveto_refresh_route())
1135 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1136 self.havocbot_personal_waypoint_searchtime = time + 10;
1137 self.havocbot_personal_waypoint_failcounter = 0;
1141 self.havocbot_personal_waypoint_failcounter += 1;
1142 self.havocbot_personal_waypoint_searchtime = time + 2;
1143 if(self.havocbot_personal_waypoint_failcounter >= 30)
1145 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1146 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1147 remove(self.havocbot_personal_waypoint);
1148 return CMD_STATUS_ERROR;
1151 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1155 if(autocvar_bot_debug_goalstack)
1159 local vector dir = ( ( self.goalcurrent.absmin + self.goalcurrent.absmax ) * 0.5 ) - (self.origin + self.view_ofs);
1161 bot_aimdir(dir, -1);
1164 havocbot_movetogoal();
1166 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1168 // Step 5: Waypoint reached
1169 dprint(self.netname, "'s personal waypoint reached\n");
1170 remove(self.havocbot_personal_waypoint);
1171 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1172 return CMD_STATUS_FINISHED;
1175 return CMD_STATUS_EXECUTING;
1178 // Step 2: Linking waypoint
1179 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1181 // Wait until it is linked
1182 if(!self.havocbot_personal_waypoint.wplinked)
1184 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1185 return CMD_STATUS_EXECUTING;
1188 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1189 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1190 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1192 // Step 3: Route to waypoint
1193 dprint(self.netname, " walking to its personal waypoint\n");
1195 return CMD_STATUS_EXECUTING;
1198 // Step 1: Spawning waypoint
1199 wp = waypoint_spawnpersonal(pos);
1202 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1203 return CMD_STATUS_ERROR;
1206 self.havocbot_personal_waypoint = wp;
1207 self.havocbot_personal_waypoint_failcounter = 0;
1208 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1210 // if pos is inside a teleport, then let's mark it as teleport waypoint
1212 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1214 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1216 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1217 self.lastteleporttime = 0;
1222 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1223 print("routing to a teleporter\n");
1225 print("routing to a non-teleporter\n");
1228 return CMD_STATUS_EXECUTING;
1231 float havocbot_resetgoal()
1233 navigation_clearroute();
1234 return CMD_STATUS_FINISHED;
1237 void havocbot_setupbot()
1239 self.bot_ai = havocbot_ai;
1240 self.cmd_moveto = havocbot_moveto;
1241 self.cmd_resetgoal = havocbot_resetgoal;
1243 havocbot_chooserole();
1246 vector havocbot_dodge()
1248 // LordHavoc: disabled because this is too expensive
1252 local vector dodge, v, n;
1253 local float danger, bestdanger, vl, d;
1256 // check for dangerous objects near bot or approaching bot
1257 head = findchainfloat(bot_dodge, TRUE);
1260 if (head.owner != self)
1262 vl = vlen(head.velocity);
1263 if (vl > autocvar_sv_maxspeed * 0.3)
1265 n = normalize(head.velocity);
1266 v = self.origin - head.origin;
1268 if (d > (0 - head.bot_dodgerating))
1269 if (d < (vl * 0.2 + head.bot_dodgerating))
1271 // calculate direction and distance from the flight path, by removing the forward axis
1272 v = v - (n * (v * n));
1273 danger = head.bot_dodgerating - vlen(v);
1274 if (bestdanger < danger)
1276 bestdanger = danger;
1277 // dodge to the side of the object
1278 dodge = normalize(v);
1284 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1285 if (bestdanger < danger)
1287 bestdanger = danger;
1288 dodge = normalize(self.origin - head.origin);