3 #include "role_onslaught.qc"
4 #include "role_keyhunt.qc"
12 if(bot_execute_commands())
15 if (bot_strategytoken == self)
16 if (!bot_strategytoken_taken)
18 if(self.havocbot_blockhead)
20 self.havocbot_blockhead = FALSE;
27 // TODO: tracewalk() should take care of this job (better path finding under water)
28 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
29 if(self.deadflag != DEAD_NO)
30 if(self.goalcurrent==world)
31 if(self.waterlevel==WATERLEVEL_SWIMMING || self.aistatus & AI_STATUS_OUT_WATER)
33 // Look for the closest waypoint out of water
34 local entity newgoal, head;
35 local float bestdistance, distance;
39 for (head = findchain(classname, "waypoint"); head; head = head.chain)
41 distance = vlen(head.origin - self.origin);
45 if(head.origin_z < self.origin_z)
48 if(head.origin_z - self.origin_z - self.view_ofs_z > 100)
51 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
54 traceline(self.origin + self.view_ofs , head.origin, TRUE, head);
59 if(distance<bestdistance)
62 bestdistance = distance;
68 // te_wizspike(newgoal.origin);
69 navigation_pushroute(newgoal);
73 // token has been used this frame
74 bot_strategytoken_taken = TRUE;
77 if(self.deadflag != DEAD_NO)
80 havocbot_chooseenemy();
81 if (self.bot_chooseweapontime < time )
83 self.bot_chooseweapontime = time + cvar("bot_ai_chooseweaponinterval");
84 havocbot_chooseweapon();
90 self.aistatus |= AI_STATUS_ATTACKING;
91 self.aistatus &~= AI_STATUS_ROAMING;
95 weapon_action(self.weapon, WR_AIM);
96 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
98 self.BUTTON_ATCK = FALSE;
99 self.BUTTON_ATCK2 = FALSE;
103 if(self.BUTTON_ATCK||self.BUTTON_ATCK2)
104 self.lastfiredweapon = self.weapon;
109 if(self.bot_aimtarg.classname=="player")
110 bot_aimdir(self.bot_aimtarg.origin + self.bot_aimtarg.view_ofs - self.origin - self.view_ofs , -1);
113 else if (self.goalcurrent)
115 self.aistatus |= AI_STATUS_ROAMING;
116 self.aistatus &~= AI_STATUS_ATTACKING;
118 local vector now,v,next;//,heading;
119 local float aimdistance,skillblend,distanceblend,blend;
120 next = now = self.goalcurrent.origin - (self.origin + self.view_ofs);
121 aimdistance = vlen(now);
122 //heading = self.velocity;
123 //dprint(self.goalstack01.classname,etos(self.goalstack01),"\n");
125 self.goalstack01 != self && self.goalstack01 != world && self.aistatus & AI_STATUS_RUNNING == 0 &&
126 !(self.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
128 next = self.goalstack01.origin - (self.origin + self.view_ofs);
130 skillblend=bound(0,(skill-2.5)*0.5,1); //lower skill player can't preturn
131 distanceblend=bound(0,aimdistance/cvar("bot_ai_keyboard_distance"),1);
132 blend = skillblend * (1-distanceblend);
133 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
134 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
135 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
136 v = now + blend * (next - now);
137 //dprint(etos(self), " ");
138 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
139 //v = now * (distanceblend) + next * (1-distanceblend);
140 if (self.waterlevel < WATERLEVEL_SWIMMING)
142 //dprint("walk at:", vtos(v), "\n");
143 //te_lightning2(world, self.origin, self.goalcurrent.origin);
146 havocbot_movetogoal();
149 void havocbot_keyboard_movement(vector destorg)
151 local vector keyboard;
152 local float blend, maxspeed;
154 maxspeed = cvar("sv_maxspeed");
156 if (time < self.havocbot_keyboardtime)
159 self.havocbot_keyboardtime =
161 self.havocbot_keyboardtime
162 + bound(0,0.05/(skill+self.havocbot_keyboardskill),0.05)
163 +random()*bound(0,0.025/(skill+self.havocbot_keyboardskill),100)
165 keyboard = self.movement * (1.0 / maxspeed);
167 local float trigger, trigger1;
168 blend = bound(0,skill*0.1,1);
169 trigger = cvar("bot_ai_keyboard_treshold");
170 trigger1 = 0 - trigger;
172 // categorize forward movement
173 // at skill < 1.5 only forward
174 // at skill < 2.5 only individual directions
175 // at skill < 4.5 only individual directions, and forward diagonals
176 // at skill >= 4.5, all cases allowed
177 if (keyboard_x > trigger)
183 else if (keyboard_x < trigger1 && skill > 1.5)
198 if (keyboard_y > trigger)
200 else if (keyboard_y < trigger1)
205 if (keyboard_z > trigger)
207 else if (keyboard_z < trigger1)
212 self.havocbot_keyboard = keyboard * maxspeed;
213 if (self.havocbot_ducktime>time) self.BUTTON_CROUCH=TRUE;
215 keyboard = self.havocbot_keyboard;
216 blend = bound(0,vlen(destorg-self.origin)/cvar("bot_ai_keyboard_distance"),1); // When getting close move with 360 degree
217 //dprint("movement ", vtos(self.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
218 self.movement = self.movement + (keyboard - self.movement) * blend;
221 void havocbot_bunnyhop(vector dir)
223 local float bunnyhopdistance;
224 local vector deviation;
225 local float maxspeed;
230 // Don't jump when using some weapons
231 if(self.aistatus & AI_STATUS_ATTACKING)
232 if(self.weapon & WEP_CAMPINGRIFLE)
235 if(self.goalcurrent.classname == "player")
238 maxspeed = cvar("sv_maxspeed");
240 if(self.aistatus & AI_STATUS_DANGER_AHEAD)
242 self.aistatus &~= AI_STATUS_RUNNING;
243 self.BUTTON_JUMP = FALSE;
244 self.bot_canruntogoal = 0;
245 self.bot_timelastseengoal = 0;
249 if(self.waterlevel > WATERLEVEL_WETFEET)
251 self.aistatus &~= AI_STATUS_RUNNING;
255 if(self.bot_lastseengoal != self.goalcurrent && !(self.aistatus & AI_STATUS_RUNNING))
257 self.bot_canruntogoal = 0;
258 self.bot_timelastseengoal = 0;
261 bunnyhopdistance = vlen(self.origin - self.goalcurrent.origin);
263 // Run only to visible goals
264 if(self.flags & FL_ONGROUND)
265 if(self.speed==maxspeed)
266 if(checkpvs(self.origin + self.view_ofs, self.goalcurrent))
268 self.bot_lastseengoal = self.goalcurrent;
271 if(self.bot_timelastseengoal)
273 // for a period of time
274 if(time - self.bot_timelastseengoal > cvar("bot_ai_bunnyhop_firstjumpdelay"))
276 local float checkdistance;
277 checkdistance = TRUE;
279 // don't run if it is too close
280 if(self.bot_canruntogoal==0)
282 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_startdistance"))
283 self.bot_canruntogoal = 1;
285 self.bot_canruntogoal = -1;
288 if(self.bot_canruntogoal != 1)
291 if(self.aistatus & AI_STATUS_ROAMING)
292 if(self.goalcurrent.classname=="waypoint")
293 if not(self.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL)
294 if(fabs(self.goalcurrent.origin_z - self.origin_z) < self.maxs_z - self.mins_z)
295 if(self.goalstack01!=world)
297 deviation = vectoangles(self.goalstack01.origin - self.origin) - vectoangles(self.goalcurrent.origin - self.origin);
298 while (deviation_y < -180) deviation_y = deviation_y + 360;
299 while (deviation_y > 180) deviation_y = deviation_y - 360;
301 if(fabs(deviation_y) < 20)
302 if(bunnyhopdistance < vlen(self.origin - self.goalstack01.origin))
303 if(fabs(self.goalstack01.origin_z - self.goalcurrent.origin_z) < self.maxs_z - self.mins_z)
305 if(vlen(self.goalcurrent.origin - self.goalstack01.origin) > cvar("bot_ai_bunnyhop_startdistance"))
306 if(checkpvs(self.origin + self.view_ofs, self.goalstack01))
308 checkdistance = FALSE;
315 self.aistatus &~= AI_STATUS_RUNNING;
316 if(bunnyhopdistance > cvar("bot_ai_bunnyhop_stopdistance"))
317 self.BUTTON_JUMP = TRUE;
321 self.aistatus |= AI_STATUS_RUNNING;
322 self.BUTTON_JUMP = TRUE;
328 self.bot_timelastseengoal = time;
333 self.bot_timelastseengoal = 0;
336 // Release jump button
337 if(self.flags & FL_ONGROUND == 0)
339 if(self.velocity_z < 0 || vlen(self.velocity)<maxspeed)
340 self.BUTTON_JUMP = FALSE;
343 if(self.aistatus & AI_STATUS_RUNNING)
344 if(vlen(self.velocity)>maxspeed)
346 deviation = vectoangles(dir) - vectoangles(self.velocity);
347 while (deviation_y < -180) deviation_y = deviation_y + 360;
348 while (deviation_y > 180) deviation_y = deviation_y - 360;
350 if(fabs(deviation_y)>10)
354 self.movement_y = maxspeed * -1;
355 else if(deviation_y<10)
356 self.movement_y = maxspeed;
362 void havocbot_movetogoal()
364 local vector destorg;
367 local vector flatdir;
370 local vector evadeobstacle;
371 local vector evadelava;
373 local float maxspeed;
376 //if (self.goalentity)
377 // te_lightning2(self, self.origin, (self.goalentity.absmin + self.goalentity.absmax) * 0.5);
378 self.movement = '0 0 0';
379 maxspeed = cvar("sv_maxspeed");
381 // Jetpack navigation
382 if(self.navigation_jetpack_goal)
383 if(self.goalcurrent==self.navigation_jetpack_goal)
386 #ifdef DEBUG_BOT_GOALSTACK
388 te_wizspike(self.navigation_jetpack_point);
392 if not(self.aistatus & AI_STATUS_JETPACK_FLYING)
394 // Brake almost completely so it can get a good direction
395 if(vlen(self.velocity)>10)
397 self.aistatus |= AI_STATUS_JETPACK_FLYING;
400 makevectors(self.v_angle_y * '0 1 0');
401 dir = normalize(self.navigation_jetpack_point - self.origin);
404 if(self.aistatus & AI_STATUS_JETPACK_LANDING)
406 // Calculate brake distance in xy
410 dxy = self.origin - self.goalcurrent.origin; dxy_z = 0;
412 v = vlen(self.velocity - self.velocity_z * '0 0 1');
413 db = (pow(v,2) / (cvar("g_jetpack_acceleration_side") * 2)) + 100;
414 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
415 if(d < db || d < 500)
418 if(fabs(self.velocity_x)>maxspeed*0.3)
420 self.movement_x = dir * v_forward * -maxspeed;
423 // Switch to normal mode
424 self.navigation_jetpack_goal = world;
425 self.aistatus &~= AI_STATUS_JETPACK_LANDING;
426 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
430 else if(checkpvs(self.origin,self.goalcurrent))
432 // If I can see the goal switch to landing code
433 self.aistatus &~= AI_STATUS_JETPACK_FLYING;
434 self.aistatus |= AI_STATUS_JETPACK_LANDING;
439 self.BUTTON_HOOK = TRUE;
440 if(self.navigation_jetpack_point_z - PL_MAX_z + PL_MIN_z < self.origin_z)
442 self.movement_x = dir * v_forward * maxspeed;
443 self.movement_y = dir * v_right * maxspeed;
448 // Handling of jump pads
449 if(self.jumppadcount)
451 if(self.flags & FL_ONGROUND)
453 self.jumppadcount = FALSE;
454 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
455 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
458 // If got stuck on the jump pad try to reach the farther visible item
459 if(self.aistatus & AI_STATUS_OUT_JUMPPAD)
461 if(fabs(self.velocity_z)<50)
463 local entity head, newgoal;
464 local float distance, bestdistance;
466 for (head = findchainfloat(bot_pickup, TRUE); head; head = head.chain)
468 if(head.classname=="worldspawn")
471 distance = vlen(head.origin - self.origin);
475 traceline(self.origin + self.view_ofs , head.origin, TRUE, world);
480 if(distance>bestdistance)
483 bestdistance = distance;
489 self.ignoregoal = self.goalcurrent;
490 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
491 navigation_clearroute();
492 navigation_routetogoal(newgoal, self.origin);
493 self.aistatus &~= AI_STATUS_OUT_JUMPPAD;
501 if(self.velocity_z>0)
503 local float threshold;
504 threshold = maxspeed * 0.2;
505 if(fabs(self.velocity_x) < threshold && fabs(self.velocity_y) < threshold)
506 self.aistatus |= AI_STATUS_OUT_JUMPPAD;
512 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
514 if not(self.flags & FL_ONGROUND)
516 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 -65536', MOVE_NOMONSTERS, self);
517 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos ))
518 if(self.items & IT_JETPACK)
520 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 65536', MOVE_NOMONSTERS, self);
521 if(tracebox_hits_trigger_hurt(self.origin, self.mins, self.maxs, trace_endpos + '0 0 1' ))
523 if(self.velocity_z<0)
525 self.BUTTON_HOOK = TRUE;
529 self.BUTTON_HOOK = TRUE;
531 // If there is no goal try to move forward
533 if(self.goalcurrent==world)
536 dir = normalize(self.goalcurrent.origin - self.origin);
538 local vector xyvelocity = self.velocity; xyvelocity_z = 0;
539 local float xyspeed = xyvelocity * dir;
541 if(xyspeed < (maxspeed / 2))
543 makevectors(self.v_angle_y * '0 1 0');
544 tracebox(self.origin, self.mins, self.maxs, self.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, self);
545 if(trace_fraction==1)
547 self.movement_x = dir * v_forward * maxspeed;
548 self.movement_y = dir * v_right * maxspeed;
550 havocbot_keyboard_movement(self.origin + dir * 100);
554 self.havocbot_blockhead = TRUE;
558 else if(self.health>cvar("g_balance_rocketlauncher_damage")*0.5)
560 if(self.velocity_z < 0)
561 if(client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE))
563 self.movement_x = maxspeed;
565 if(self.rocketjumptime)
567 if(time > self.rocketjumptime)
569 self.BUTTON_ATCK2 = TRUE;
570 self.rocketjumptime = 0;
575 self.switchweapon = WEP_ROCKET_LAUNCHER;
577 self.BUTTON_ATCK = TRUE;
578 self.rocketjumptime = time + cvar("g_balance_rocketlauncher_detonatedelay");
584 // If there is no goal try to move forward
585 if(self.goalcurrent==world)
586 self.movement_x = maxspeed;
590 // If we are under water with no goals, swim up
592 if(self.goalcurrent==world)
595 if(self.waterlevel>WATERLEVEL_SWIMMING)
597 else if(self.velocity_z >= 0 && !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER))
598 self.BUTTON_JUMP = TRUE;
600 self.BUTTON_JUMP = FALSE;
601 makevectors(self.v_angle_y * '0 1 0');
602 self.movement_x = dir * v_forward * maxspeed;
603 self.movement_y = dir * v_right * maxspeed;
604 self.movement_z = dir * v_up * maxspeed;
607 // if there is nowhere to go, exit
608 if (self.goalcurrent == world)
611 if (self.goalcurrent)
612 navigation_poptouchedgoals();
614 // if ran out of goals try to use an alternative goal or get a new strategy asap
615 if(self.goalcurrent == world)
617 self.bot_strategytime = 0;
621 #ifdef DEBUG_BOT_GOALSTACK
625 m1 = self.goalcurrent.origin + self.goalcurrent.mins;
626 m2 = self.goalcurrent.origin + self.goalcurrent.maxs;
627 destorg = self.origin;
628 destorg_x = bound(m1_x, destorg_x, m2_x);
629 destorg_y = bound(m1_y, destorg_y, m2_y);
630 destorg_z = bound(m1_z, destorg_z, m2_z);
631 diff = destorg - self.origin;
633 dir = normalize(diff);
634 flatdir = diff;flatdir_z = 0;
635 flatdir = normalize(flatdir);
637 //if (self.bot_dodgevector_time < time)
639 // self.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
640 // self.bot_dodgevector_jumpbutton = 1;
641 evadeobstacle = '0 0 0';
646 if(self.waterlevel>WATERLEVEL_SWIMMING)
649 self.aistatus |= AI_STATUS_OUT_WATER;
653 if(self.velocity_z >= 0 && !(self.watertype == CONTENT_WATER && self.goalcurrent.origin_z < self.origin_z) &&
654 ( !(self.waterlevel == WATERLEVEL_WETFEET && self.watertype == CONTENT_WATER) || self.aistatus & AI_STATUS_OUT_WATER))
655 self.BUTTON_JUMP = TRUE;
657 self.BUTTON_JUMP = FALSE;
659 dir = normalize(flatdir);
660 makevectors(self.v_angle_y * '0 1 0');
664 if(self.aistatus & AI_STATUS_OUT_WATER)
665 self.aistatus &~= AI_STATUS_OUT_WATER;
667 // jump if going toward an obstacle that doesn't look like stairs we
668 // can walk up directly
669 tracebox(self.origin, self.mins, self.maxs, self.origin + self.velocity * 0.2, FALSE, self);
670 if (trace_fraction < 1)
671 if (trace_plane_normal_z < 0.7)
674 tracebox(self.origin + '0 0 16', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 16', FALSE, self);
675 if (trace_fraction < s + 0.01)
676 if (trace_plane_normal_z < 0.7)
679 tracebox(self.origin + '0 0 48', self.mins, self.maxs, self.origin + self.velocity * 0.2 + '0 0 48', FALSE, self);
680 if (trace_fraction > s)
681 self.BUTTON_JUMP = 1;
685 // avoiding dangers and obstacles
686 local vector dst_ahead, dst_down;
687 makevectors(self.v_angle_y * '0 1 0');
688 dst_ahead = self.origin + self.view_ofs + (self.velocity * 0.4) + (v_forward * 32 * 3);
689 dst_down = dst_ahead + '0 0 -1500';
692 traceline(self.origin + self.view_ofs , dst_ahead, TRUE, world);
694 // Check head-banging against walls
695 if(vlen(self.origin + self.view_ofs - trace_endpos) < 25 && !(self.aistatus & AI_STATUS_OUT_WATER))
697 self.BUTTON_JUMP = TRUE;
698 if(self.facingwalltime && time > self.facingwalltime)
700 self.ignoregoal = self.goalcurrent;
701 self.ignoregoaltime = time + cvar("bot_ai_ignoregoal_timeout");
702 self.bot_strategytime = 0;
707 self.facingwalltime = time + 0.05;
712 self.facingwalltime = 0;
714 if(self.ignoregoal != world && time > self.ignoregoaltime)
716 self.ignoregoal = world;
717 self.ignoregoaltime = 0;
721 // Check for water/slime/lava and dangerous edges
722 // (only when the bot is on the ground or jumping intentionally)
723 self.aistatus &~= AI_STATUS_DANGER_AHEAD;
725 if(trace_fraction == 1)
726 if(self.flags & FL_ONGROUND || self.aistatus & AI_STATUS_RUNNING || self.BUTTON_JUMP == TRUE)
729 traceline(dst_ahead , dst_down, TRUE, world);
730 // te_lightning2(world, self.origin, dst_ahead); // Draw "ahead" look
731 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
732 if(trace_endpos_z < self.origin_z + self.mins_z)
734 s = pointcontents(trace_endpos + '0 0 1');
735 if (s != CONTENT_SOLID)
736 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
737 evadelava = normalize(self.velocity) * -1;
738 else if (s == CONTENT_SKY)
739 evadeobstacle = normalize(self.velocity) * -1;
740 else if (!boxesoverlap(dst_ahead - self.view_ofs + self.mins, dst_ahead - self.view_ofs + self.maxs,
741 self.goalcurrent.absmin, self.goalcurrent.absmax))
743 // if ain't a safe goal with "holes" (like the jumpad on soylent)
744 // and there is a trigger_hurt below
745 if(tracebox_hits_trigger_hurt(dst_ahead, self.mins, self.maxs, trace_endpos))
747 // Remove dangerous dynamic goals from stack
748 if (self.goalcurrent.classname == "player" || self.goalcurrent.classname == "droppedweapon")
749 navigation_poproute();
752 evadeobstacle = normalize(self.velocity) * -1;
761 makevectors(self.v_angle_y * '0 1 0');
763 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
764 self.aistatus |= AI_STATUS_DANGER_AHEAD;
767 dodge = havocbot_dodge();
768 dodge = dodge * bound(0,3+skill*0.1,1);
769 evadelava = evadelava * bound(1,3-skill,3); //Noobs fear lava a lot and take more distance from it
770 traceline(self.origin, self.enemy.origin, TRUE, world);
771 if(trace_ent.classname == "player")
772 dir = dir * bound(0,skill/7,1);
774 dir = normalize(dir + dodge + evadeobstacle + evadelava);
775 // self.bot_dodgevector = dir;
776 // self.bot_dodgevector_jumpbutton = self.BUTTON_JUMP;
779 if(time < self.ladder_time)
781 if(self.goalcurrent.origin_z + self.goalcurrent.mins_z > self.origin_z + self.mins_z)
783 if(self.origin_z + self.mins_z < self.ladder_entity.origin_z + self.ladder_entity.maxs_z)
788 if(self.origin_z + self.mins_z > self.ladder_entity.origin_z + self.ladder_entity.mins_z)
793 //dir = self.bot_dodgevector;
794 //if (self.bot_dodgevector_jumpbutton)
795 // self.BUTTON_JUMP = 1;
796 self.movement_x = dir * v_forward * maxspeed;
797 self.movement_y = dir * v_right * maxspeed;
798 self.movement_z = dir * v_up * maxspeed;
800 // Emulate keyboard interface
802 havocbot_keyboard_movement(destorg);
805 // if(self.aistatus & AI_STATUS_ROAMING)
806 if(skill >= cvar("bot_ai_bunnyhop_skilloffset"))
807 havocbot_bunnyhop(dir);
809 if ((dir * v_up) >= cvar("sv_jumpvelocity")*0.5 && (self.flags & FL_ONGROUND)) self.BUTTON_JUMP=1;
810 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill)*0.1,1)) self.BUTTON_JUMP=TRUE;
811 if (((dodge * v_up) < 0) && random()*frametime >= 0.5*bound(0,(10-skill)*0.1,1)) self.havocbot_ducktime=time+0.3/bound(0.1,skill,10);
814 void havocbot_chooseenemy()
816 local entity head, best, head2;
817 local float rating, bestrating, i, f;
819 if (cvar("bot_nofire") || IS_INDEPENDENT_PLAYER(self))
826 if (!bot_shouldattack(self.enemy))
828 // enemy died or something, find a new target
830 self.havocbot_chooseenemy_finished = time;
832 else if (self.havocbot_stickenemy)
834 // tracking last chosen enemy
835 // if enemy is visible
836 // and not really really far away
837 // and we're not severely injured
838 // then keep tracking for a half second into the future
839 traceline(self.origin+self.view_ofs, self.enemy.origin+self.enemy.view_ofs*0.5,FALSE,world);
840 if (trace_ent == self.enemy || trace_fraction == 1)
841 if (vlen(self.enemy.origin - self.origin) < 1000)
842 if (self.health > 30)
844 // remain tracking him for a shot while (case he went after a small corner or pilar
845 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
848 // enemy isn't visible, or is far away, or we're injured severely
849 // so stop preferring this enemy
850 // (it will still take a half second until a new one is chosen)
851 self.havocbot_stickenemy = 0;
854 if (time < self.havocbot_chooseenemy_finished)
856 self.havocbot_chooseenemy_finished = time + cvar("bot_ai_enemydetectioninterval");
857 eye = self.origin + self.view_ofs;
859 bestrating = 100000000;
860 head = head2 = findchainfloat(bot_attack, TRUE);
862 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
867 v = (head.absmin + head.absmax) * 0.5;
868 rating = vlen(v - eye);
869 if (rating<cvar("bot_ai_enemydetectionradius"))
870 if (bestrating > rating)
871 if (bot_shouldattack(head))
873 traceline(eye, v, TRUE, self);
874 if (trace_ent == head || trace_fraction >= 1)
883 // I want to do a second scan if no enemy was found or I don't have weapons
884 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
885 if(best || self.weapons) // || self.weapon == WEP_CAMPINGRIFLE
890 // Set flags to see through transparent objects
891 f = self.dphitcontentsmask;
892 self.dphitcontentsmask = DPCONTENTS_OPAQUE;
898 // Restore hit flags if needed
900 self.dphitcontentsmask = f;
903 self.havocbot_stickenemy = TRUE;
906 void havocbot_chooseweapon()
911 if(g_weaponarena == WEPBIT_TUBA)
913 self.switchweapon = WEP_TUBA;
917 // TODO: clean this up by moving it to weapon code
918 if(self.enemy==world)
920 // If no weapon was chosen get the first available weapon
922 for(i=WEP_LASER + 1; i < WEP_COUNT ; ++i)
924 if(client_hasweapon(self, i, TRUE, FALSE))
926 self.switchweapon = i;
933 // Do not change weapon during the next second after a combo
934 i = time - self.lastcombotime;
938 // Workaround for rifle reloading (..)
939 if(self.weapon == WEP_CAMPINGRIFLE)
940 if(i < cvar("g_balance_campingrifle_reloadtime") + 1)
944 local float rocket ; rocket =-1000;
945 local float nex ; nex =-1000;
946 local float hagar ; hagar =-1000;
947 local float grenade ; grenade =-1000;
948 local float electro ; electro =-1000;
949 local float crylink ; crylink =-1000;
950 local float uzi ; uzi =-1000;
951 local float shotgun ; shotgun =-1000;
952 local float campingrifle ; campingrifle =-1000;
953 local float laser ; laser =-1000;
954 local float minstanex ; minstanex =-1000;
955 local float bestscore; bestscore = 0;
956 local float bestweapon; bestweapon=self.switchweapon;
957 local float distance; distance=bound(10,vlen(self.origin-self.enemy.origin)-200,10000);
958 local float maxdelaytime=0.5;
959 local float spreadpenalty=10;
961 // Should it do a weapon combo?
962 local float af, ct, combo_time, combo;
964 af = ATTACK_FINISHED(self);
965 ct = cvar("bot_ai_weapon_combo_threshold");
967 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
968 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
969 combo_time = time + ct + (ct * ((-0.3*skill)+3));
973 if(cvar("bot_ai_weapon_combo"))
974 if(self.weapon == self.lastfiredweapon)
978 self.lastcombotime = time;
981 // Custom weapon list based on distance to the enemy
982 if(bot_custom_weapon){
984 // Choose weapons for far distance
985 if ( distance > bot_distance_far ) {
986 for(i=0; i < WEP_COUNT && bot_weapons_far[i] != -1 ; ++i){
987 w = bot_weapons_far[i];
988 if ( client_hasweapon(self, w, TRUE, FALSE) ){
989 if ( self.weapon == w && combo)
991 self.switchweapon = w;
997 // Choose weapons for mid distance
998 if ( distance > bot_distance_close ) {
999 for(i=0; i < WEP_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1000 w = bot_weapons_mid[i];
1001 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1002 if ( self.weapon == w && combo)
1004 self.switchweapon = w;
1010 // Choose weapons for close distance
1011 for(i=0; i < WEP_COUNT && bot_weapons_close[i] != -1 ; ++i){
1012 w = bot_weapons_close[i];
1013 if ( client_hasweapon(self, w, TRUE, FALSE) ){
1014 if ( self.weapon == w && combo)
1016 self.switchweapon = w;
1023 // TODO: This disabled code is not working well and got replaced by custom weapon priorities.
1024 // However, this logic should be refactored and moved to weapons code so each new weapon can be
1025 // evaluated dynamically by bots without updating the "ai" or config files. --mand1nga
1026 float s, distancefromfloor, currentscore;
1030 // (Damage/Sec * Weapon spefic change to get that damage)
1031 // *(Time to get to target * weapon specfic hitchange bonus) / (in a time of maxdelaytime)
1032 // *(Spread change of hit) // if it applies
1033 // *(Penality for target beeing in air)
1036 traceline(self.enemy.origin,self.enemy.origin-'0 0 1000',TRUE,world);
1037 distancefromfloor = self.enemy.origin_z - trace_endpos_z;
1039 if (client_hasweapon(self, WEP_MINSTANEX, TRUE, FALSE))
1040 minstanex = (1000/cvar("g_balance_minstanex_refire")*1.0)
1043 if (client_hasweapon(self, WEP_ROCKET_LAUNCHER, TRUE, FALSE) &&
1044 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ROCKET_LAUNCHER &&
1048 rocket = (cvar("g_balance_rocketlauncher_damage")/cvar("g_balance_rocketlauncher_refire")*0.75)
1049 * bound(0,(cvar("g_balance_rocketlauncher_speed")/distance*maxdelaytime),1)*1.5;
1051 if (client_hasweapon(self, WEP_NEX, TRUE, FALSE) &&
1052 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_NEX &&
1056 nex = (cvar("g_balance_nex_damage")/cvar("g_balance_nex_refire")*1.0)
1059 if (client_hasweapon(self, WEP_HAGAR, TRUE, FALSE) ) // &&
1060 // !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_HAGAR && time < self.bot_lastshot + cvar("g_balance_hagar_primary_refire") ))
1061 hagar = (cvar("g_balance_hagar_primary_damage")/cvar("g_balance_hagar_primary_refire")*1.0)
1062 * bound(0,(cvar("g_balance_hagar_primary_speed")/distance*maxdelaytime),1)*0.2;
1064 if (client_hasweapon(self, WEP_GRENADE_LAUNCHER, TRUE, FALSE) &&
1066 cvar("bot_ai_weapon_combo") && self.weapon == WEP_GRENADE_LAUNCHER &&
1070 grenade = (cvar("g_balance_grenadelauncher_primary_damage")/cvar("g_balance_grenadelauncher_primary_refire")*1.0)
1071 * bound(0,(cvar("g_balance_grenadelauncher_primary_speed")/distance*maxdelaytime),1)*1.1;
1073 if (client_hasweapon(self, WEP_ELECTRO, TRUE, FALSE) &&
1074 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_ELECTRO &&
1078 electro = (cvar("g_balance_electro_primary_damage")/cvar("g_balance_electro_primary_refire")*0.75)
1079 * bound(0,(cvar("g_balance_electro_primary_speed")/distance*maxdelaytime),1)*1.0;
1081 if (client_hasweapon(self, WEP_CRYLINK, TRUE, FALSE) ) // &&
1082 // !( self.weapon == WEP_CRYLINK && time < self.bot_lastshot + cvar("g_balance_crylink_primary_refire") ))
1083 crylink = (cvar("g_balance_crylink_primary_damage")/cvar("g_balance_crylink_primary_refire")*1.0)
1084 * bound(0,(cvar("g_balance_crylink_primary_speed")/distance*maxdelaytime),1)*(64/(32+cvar("g_balance_crylink_primary_spread")*distance))*1.0;
1086 if (client_hasweapon(self, WEP_UZI, TRUE, FALSE) ) // &&
1087 // !( self.weapon == WEP_UZI && time < self.bot_lastshot + cvar("g_balance_uzi_sustained_refire") ))
1088 uzi = (cvar("g_balance_uzi_sustained_damage")/cvar("g_balance_uzi_sustained_refire")*1.0)
1089 * bound(0,32/(32+cvar("g_balance_uzi_sustained_spread")*distance),1);
1091 if (client_hasweapon(self, WEP_SHOTGUN, TRUE, FALSE) &&
1092 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_SHOTGUN &&
1096 shotgun = (cvar("g_balance_shotgun_primary_damage")*cvar("g_balance_shotgun_primary_bullets")/cvar("g_balance_shotgun_primary_refire")*1.0)
1097 * bound(0,32/(32+cvar("g_balance_shotgun_primary_spread")*distance),1);
1099 if (client_hasweapon(self, WEP_LASER, FALSE, FALSE) &&
1100 !( cvar("bot_ai_weapon_combo") && self.weapon == WEP_LASER &&
1104 laser = (cvar("g_balance_laser_primary_damage")/cvar("g_balance_laser_primary_refire")*1.0)
1105 * bound(0,cvar("g_balance_laser_primary_speed")/distance*maxdelaytime,1);
1107 if((self.enemy.flags & FL_ONGROUND)==FALSE){
1108 rocket = rocket * (1.5-bound(0, distancefromfloor/cvar("g_balance_rocketlauncher_radius" ),0.9)); //slight bigger change
1109 grenade = grenade * (1.5-bound(0,distancefromfloor/cvar("g_balance_grenadelauncher_primary_radius"),0.95));
1110 electro = electro * (1.5-bound(0,distancefromfloor/cvar("g_balance_electro_primary_radius" ),0.95));
1111 laser = laser * (1.5-bound(0,distancefromfloor/cvar("g_balance_laser_primary_radius" ),0.95));
1114 dprint("Floor distance: ",ftos(distancefromfloor),"\n");
1115 dprint("Rocket: " , ftos(rocket ), "\n");
1116 dprint("Nex: " , ftos(nex ), "\n");
1117 dprint("Hagar: " , ftos(hagar ), "\n");
1118 dprint("Grenade: ", ftos(grenade ), "\n");
1119 dprint("Electro: ", ftos(electro ), "\n");
1120 dprint("Crylink: ", ftos(crylink ), "\n");
1121 dprint("Uzi: " , ftos(uzi ), "\n");
1122 dprint("Shotgun :", ftos(shotgun ), "\n");
1123 dprint("Laser :", ftos(laser ), "\n\n");
1126 w = WEP_MINSTANEX ;s = minstanex;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1127 w = WEP_ROCKET_LAUNCHER ;s = rocket ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1128 w = WEP_NEX ;s = nex ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1129 w = WEP_HAGAR ;s = hagar ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1130 w = WEP_GRENADE_LAUNCHER ;s = grenade ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1131 w = WEP_ELECTRO ;s = electro ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1132 w = WEP_CRYLINK ;s = crylink ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1133 w = WEP_UZI ;s = uzi ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1134 w = WEP_SHOTGUN ;s = shotgun ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1135 w = WEP_LASER ;s = laser ;if (s > bestscore){bestscore = s;bestweapon = w;} if (self.switchweapon == w) currentscore = s;
1137 // switch if the best weapon would provide a significant damage increase
1138 if (bestscore > currentscore*1.5){
1139 self.switchweapon = bestweapon;
1141 // buys time for detonating the rocket. not tested yet
1142 if ( cvar("bot_ai_weapon_combo") && bestweapon == WEP_ROCKET_LAUNCHER )
1143 self.bot_chooseweapontime += (distance / cvar("g_balance_rocketlauncher_speed"));
1150 local vector selfvel, enemyvel;
1151 // if(self.flags & FL_INWATER)
1153 if (time < self.nextaim)
1155 self.nextaim = time + 0.1;
1156 selfvel = self.velocity;
1157 if (!self.waterlevel)
1161 enemyvel = self.enemy.velocity;
1162 if (!self.enemy.waterlevel)
1164 lag_additem(time + self.ping, 0, 0, self.enemy, self.origin, selfvel, self.enemy.origin, enemyvel);
1167 lag_additem(time + self.ping, 0, 0, world, self.origin, selfvel, self.goalcurrent.origin, '0 0 0');
1170 float havocbot_moveto_refresh_route()
1172 // Refresh path to goal if necessary
1174 wp = self.havocbot_personal_waypoint;
1175 navigation_goalrating_start();
1176 navigation_routerating(wp, 10000, 10000);
1177 navigation_goalrating_end();
1178 return self.navigation_hasgoals;
1181 float havocbot_moveto(vector pos)
1185 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1187 // Step 4: Move to waypoint
1188 if(self.havocbot_personal_waypoint==world)
1190 dprint("Error: ", self.netname, " trying to walk to a non existent personal waypoint\n");
1191 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1192 return CMD_STATUS_ERROR;
1195 if (!bot_strategytoken_taken)
1196 if(self.havocbot_personal_waypoint_searchtime<time)
1198 bot_strategytoken_taken = TRUE;
1199 if(havocbot_moveto_refresh_route())
1201 dprint(self.netname, " walking to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1202 self.havocbot_personal_waypoint_searchtime = time + 10;
1203 self.havocbot_personal_waypoint_failcounter = 0;
1207 self.havocbot_personal_waypoint_failcounter += 1;
1208 self.havocbot_personal_waypoint_searchtime = time + 2;
1209 if(self.havocbot_personal_waypoint_failcounter >= 30)
1211 dprint("Warning: can't walk to the personal waypoint located at ", vtos(self.havocbot_personal_waypoint.origin),"\n");
1212 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1213 remove(self.havocbot_personal_waypoint);
1214 return CMD_STATUS_ERROR;
1217 dprint(self.netname, " can't walk to its personal waypoint (after ", ftos(self.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1221 #ifdef DEBUG_BOT_GOALSTACK
1226 local vector dir = self.goalcurrent.origin - (self.origin + self.view_ofs);
1228 bot_aimdir(dir, -1);
1231 havocbot_movetogoal();
1233 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1235 // Step 5: Waypoint reached
1236 dprint(self.netname, "'s personal waypoint reached\n");
1237 remove(self.havocbot_personal_waypoint);
1238 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1239 return CMD_STATUS_FINISHED;
1242 return CMD_STATUS_EXECUTING;
1245 // Step 2: Linking waypoint
1246 if(self.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1248 // Wait until it is linked
1249 if(!self.havocbot_personal_waypoint.wplinked)
1251 dprint(self.netname, " waiting for personal waypoint to be linked\n");
1252 return CMD_STATUS_EXECUTING;
1255 self.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1256 self.aistatus &~= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1257 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1259 // Step 3: Route to waypoint
1260 dprint(self.netname, " walking to its personal waypoint\n");
1262 return CMD_STATUS_EXECUTING;
1265 // Step 1: Spawning waypoint
1266 wp = waypoint_spawnpersonal(pos);
1269 dprint("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1270 return CMD_STATUS_ERROR;
1273 self.havocbot_personal_waypoint = wp;
1274 self.havocbot_personal_waypoint_failcounter = 0;
1275 self.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1277 return CMD_STATUS_EXECUTING;
1280 float havocbot_resetgoal()
1282 navigation_clearroute();
1283 return CMD_STATUS_FINISHED;
1286 void havocbot_setupbot()
1288 self.bot_ai = havocbot_ai;
1289 self.cmd_moveto = havocbot_moveto;
1290 self.cmd_resetgoal = havocbot_resetgoal;
1292 // will be updated by think code
1293 //Generate some random skill levels
1294 self.havocbot_keyboardskill = random() - 0.5;
1295 havocbot_chooserole();
1298 vector havocbot_dodge()
1300 // LordHavoc: disabled because this is too expensive
1304 local vector dodge, v, n;
1305 local float danger, bestdanger, vl, d;
1308 // check for dangerous objects near bot or approaching bot
1309 head = findchainfloat(bot_dodge, TRUE);
1312 if (head.owner != self)
1314 vl = vlen(head.velocity);
1315 if (vl > sv_maxspeed * 0.3)
1317 n = normalize(head.velocity);
1318 v = self.origin - head.origin;
1320 if (d > (0 - head.bot_dodgerating))
1321 if (d < (vl * 0.2 + head.bot_dodgerating))
1323 // calculate direction and distance from the flight path, by removing the forward axis
1324 v = v - (n * (v * n));
1325 danger = head.bot_dodgerating - vlen(v);
1326 if (bestdanger < danger)
1328 bestdanger = danger;
1329 // dodge to the side of the object
1330 dodge = normalize(v);
1336 danger = head.bot_dodgerating - vlen(head.origin - self.origin);
1337 if (bestdanger < danger)
1339 bestdanger = danger;
1340 dodge = normalize(self.origin - head.origin);