7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
11 #include <common/constants.qh>
12 #include <common/physics/player.qh>
13 #include <common/state.qh>
14 #include <common/items/_mod.qh>
16 #include <common/triggers/teleporters.qh>
17 #include <common/triggers/trigger/jumppads.qh>
19 #include <lib/warpzone/common.qh>
23 void havocbot_ai(entity this)
28 if(bot_execute_commands(this))
31 if (bot_strategytoken == this)
32 if (!bot_strategytoken_taken)
34 if(this.havocbot_blockhead)
36 this.havocbot_blockhead = false;
40 if (!this.jumppadcount)
41 this.havocbot_role(this); // little too far down the rabbit hole
44 // TODO: tracewalk() should take care of this job (better path finding under water)
45 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
48 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
50 // Look for the closest waypoint out of water
51 entity newgoal = NULL;
52 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
54 if(it.origin.z < this.origin.z)
57 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
60 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
63 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
65 if(trace_fraction < 1)
68 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
74 // te_wizspike(newgoal.origin);
75 navigation_pushroute(this, newgoal);
79 // token has been used this frame
80 bot_strategytoken_taken = true;
86 havocbot_chooseenemy(this);
87 if (this.bot_chooseweapontime < time )
89 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
90 havocbot_chooseweapon(this);
96 this.aistatus |= AI_STATUS_ATTACKING;
97 this.aistatus &= ~AI_STATUS_ROAMING;
101 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
102 Weapon w = this.(weaponentity).m_weapon;
104 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
106 PHYS_INPUT_BUTTON_ATCK(this) = false;
107 PHYS_INPUT_BUTTON_ATCK2(this) = false;
111 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
112 this.lastfiredweapon = this.(weaponentity).m_weapon.m_id;
117 if(IS_PLAYER(this.bot_aimtarg))
118 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
121 else if (this.goalcurrent)
123 this.aistatus |= AI_STATUS_ROAMING;
124 this.aistatus &= ~AI_STATUS_ATTACKING;
126 vector now,v,next;//,heading;
127 float aimdistance,skillblend,distanceblend,blend;
128 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
129 aimdistance = vlen(now);
130 //heading = this.velocity;
131 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
133 this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
134 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
136 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
138 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
139 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
140 blend = skillblend * (1-distanceblend);
141 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
142 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
143 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
144 v = now + blend * (next - now);
145 //dprint(etos(this), " ");
146 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
147 //v = now * (distanceblend) + next * (1-distanceblend);
148 if (this.waterlevel < WATERLEVEL_SWIMMING)
150 //dprint("walk at:", vtos(v), "\n");
151 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
152 bot_aimdir(this, v, -1);
154 havocbot_movetogoal(this);
156 // if the bot is not attacking, consider reloading weapons
157 if (!(this.aistatus & AI_STATUS_ATTACKING))
159 // we are currently holding a weapon that's not fully loaded, reload it
160 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
161 if(this.clip_load < this.clip_size)
162 this.impulse = 20; // "press" the reload button, not sure if this is done right
164 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
165 // the code above executes next frame, starting the reloading then
166 if(skill >= 5) // bots can only look for unloaded weapons past this skill
167 if(this.clip_load >= 0) // only if we're not reloading a weapon already
169 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
170 FOREACH(Weapons, it != WEP_Null, LAMBDA(
171 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
172 this.(weaponentity).m_switchweapon = it;
178 void havocbot_keyboard_movement(entity this, vector destorg)
181 float blend, maxspeed;
184 sk = skill + this.bot_moveskill;
186 maxspeed = autocvar_sv_maxspeed;
188 if (time < this.havocbot_keyboardtime)
191 this.havocbot_keyboardtime =
193 this.havocbot_keyboardtime
194 + 0.05/max(1, sk+this.havocbot_keyboardskill)
195 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
197 keyboard = this.movement * (1.0 / maxspeed);
199 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 this.havocbot_keyboard = keyboard * maxspeed;
245 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
247 keyboard = this.havocbot_keyboard;
248 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
249 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
250 this.movement = this.movement + (keyboard - this.movement) * blend;
253 void havocbot_bunnyhop(entity this, vector dir)
255 float bunnyhopdistance;
260 // Don't jump when attacking
261 if(this.aistatus & AI_STATUS_ATTACKING)
264 if(IS_PLAYER(this.goalcurrent))
267 maxspeed = autocvar_sv_maxspeed;
269 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
271 this.aistatus &= ~AI_STATUS_RUNNING;
272 PHYS_INPUT_BUTTON_JUMP(this) = false;
273 this.bot_canruntogoal = 0;
274 this.bot_timelastseengoal = 0;
278 if(this.waterlevel > WATERLEVEL_WETFEET)
280 this.aistatus &= ~AI_STATUS_RUNNING;
284 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
286 this.bot_canruntogoal = 0;
287 this.bot_timelastseengoal = 0;
290 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
291 bunnyhopdistance = vlen(this.origin - gco);
293 // Run only to visible goals
294 if(IS_ONGROUND(this))
295 if(this.speed==maxspeed)
296 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
298 this.bot_lastseengoal = this.goalcurrent;
301 if(this.bot_timelastseengoal)
303 // for a period of time
304 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
307 checkdistance = true;
309 // don't run if it is too close
310 if(this.bot_canruntogoal==0)
312 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
313 this.bot_canruntogoal = 1;
315 this.bot_canruntogoal = -1;
318 if(this.bot_canruntogoal != 1)
321 if(this.aistatus & AI_STATUS_ROAMING)
322 if(this.goalcurrent.classname=="waypoint")
323 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
324 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
325 if(this.goalstack01!=NULL)
327 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
328 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
329 while (deviation.y < -180) deviation.y = deviation.y + 360;
330 while (deviation.y > 180) deviation.y = deviation.y - 360;
332 if(fabs(deviation.y) < 20)
333 if(bunnyhopdistance < vlen(this.origin - gno))
334 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
336 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
337 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
339 checkdistance = false;
346 this.aistatus &= ~AI_STATUS_RUNNING;
347 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
348 PHYS_INPUT_BUTTON_JUMP(this) = true;
352 this.aistatus |= AI_STATUS_RUNNING;
353 PHYS_INPUT_BUTTON_JUMP(this) = true;
359 this.bot_timelastseengoal = time;
364 this.bot_timelastseengoal = 0;
368 // Release jump button
369 if(!cvar("sv_pogostick"))
370 if((IS_ONGROUND(this)) == 0)
372 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
373 PHYS_INPUT_BUTTON_JUMP(this) = false;
376 if(this.aistatus & AI_STATUS_RUNNING)
377 if(vlen(this.velocity)>maxspeed)
379 deviation = vectoangles(dir) - vectoangles(this.velocity);
380 while (deviation.y < -180) deviation.y = deviation.y + 360;
381 while (deviation.y > 180) deviation.y = deviation.y - 360;
383 if(fabs(deviation.y)>10)
387 this.movement_y = maxspeed * -1;
388 else if(deviation.y<10)
389 this.movement_y = maxspeed;
396 void havocbot_movetogoal(entity this)
404 vector evadeobstacle;
411 //if (this.goalentity)
412 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
413 this.movement = '0 0 0';
414 maxspeed = autocvar_sv_maxspeed;
416 // Jetpack navigation
418 if(this.navigation_jetpack_goal)
419 if(this.goalcurrent==this.navigation_jetpack_goal)
422 if(autocvar_bot_debug_goalstack)
424 debuggoalstack(this);
425 te_wizspike(this.navigation_jetpack_point);
429 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
431 // Brake almost completely so it can get a good direction
432 if(vdist(this.velocity, >, 10))
434 this.aistatus |= AI_STATUS_JETPACK_FLYING;
437 makevectors(this.v_angle.y * '0 1 0');
438 dir = normalize(this.navigation_jetpack_point - this.origin);
441 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
443 // Calculate brake distance in xy
447 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
449 v = vlen(this.velocity - this.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(this.velocity.x)>maxspeed*0.3)
457 this.movement_x = dir * v_forward * -maxspeed;
460 // Switch to normal mode
461 this.navigation_jetpack_goal = NULL;
462 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
463 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
467 else if(checkpvs(this.origin,this.goalcurrent))
469 // If I can see the goal switch to landing code
470 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
471 this.aistatus |= AI_STATUS_JETPACK_LANDING;
476 PHYS_INPUT_BUTTON_HOOK(this) = true;
477 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
479 this.movement_x = dir * v_forward * maxspeed;
480 this.movement_y = dir * v_right * maxspeed;
485 // Handling of jump pads
486 if(this.jumppadcount)
488 // If got stuck on the jump pad try to reach the farthest visible waypoint
489 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
491 if(fabs(this.velocity.z)<50)
493 entity newgoal = NULL;
494 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
496 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
498 if(trace_fraction < 1)
501 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
507 this.ignoregoal = this.goalcurrent;
508 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
509 navigation_clearroute(this);
510 navigation_routetogoal(this, newgoal, this.origin);
511 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
519 if(this.velocity.z>0)
522 vector velxy = this.velocity; velxy_z = 0;
523 threshold = maxspeed * 0.2;
524 if(vdist(velxy, <, threshold))
526 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
527 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
532 // Don't chase players while using a jump pad
533 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
537 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
538 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
540 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
542 if (!(IS_ONGROUND(this)))
544 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
545 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
546 if(this.items & IT_JETPACK)
548 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
549 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
551 if(this.velocity.z<0)
553 PHYS_INPUT_BUTTON_HOOK(this) = true;
557 PHYS_INPUT_BUTTON_HOOK(this) = true;
559 // If there is no goal try to move forward
561 if(this.goalcurrent==NULL)
564 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
566 vector xyvelocity = this.velocity; xyvelocity_z = 0;
567 float xyspeed = xyvelocity * dir;
569 if(xyspeed < (maxspeed / 2))
571 makevectors(this.v_angle.y * '0 1 0');
572 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
573 if(trace_fraction==1)
575 this.movement_x = dir * v_forward * maxspeed;
576 this.movement_y = dir * v_right * maxspeed;
578 havocbot_keyboard_movement(this, this.origin + dir * 100);
582 this.havocbot_blockhead = true;
586 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
588 if(this.velocity.z < 0)
589 if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
591 this.movement_x = maxspeed;
593 if(this.rocketjumptime)
595 if(time > this.rocketjumptime)
597 PHYS_INPUT_BUTTON_ATCK2(this) = true;
598 this.rocketjumptime = 0;
603 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
604 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
606 PHYS_INPUT_BUTTON_ATCK(this) = true;
607 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
613 // If there is no goal try to move forward
614 if(this.goalcurrent==NULL)
615 this.movement_x = maxspeed;
619 // If we are under water with no goals, swim up
621 if(this.goalcurrent==NULL)
624 if(this.waterlevel>WATERLEVEL_SWIMMING)
626 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
627 PHYS_INPUT_BUTTON_JUMP(this) = true;
629 PHYS_INPUT_BUTTON_JUMP(this) = false;
630 makevectors(this.v_angle.y * '0 1 0');
631 this.movement_x = dir * v_forward * maxspeed;
632 this.movement_y = dir * v_right * maxspeed;
633 this.movement_z = dir * v_up * maxspeed;
636 // if there is nowhere to go, exit
637 if (this.goalcurrent == NULL)
640 if (this.goalcurrent)
641 navigation_poptouchedgoals(this);
643 // if ran out of goals try to use an alternative goal or get a new strategy asap
644 if(this.goalcurrent == NULL)
646 this.bot_strategytime = 0;
651 if(autocvar_bot_debug_goalstack)
652 debuggoalstack(this);
654 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
655 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
656 destorg = this.origin;
657 destorg.x = bound(m1_x, destorg.x, m2_x);
658 destorg.y = bound(m1_y, destorg.y, m2_y);
659 destorg.z = bound(m1_z, destorg.z, m2_z);
660 diff = destorg - this.origin;
662 dir = normalize(diff);
663 flatdir = diff;flatdir.z = 0;
664 flatdir = normalize(flatdir);
665 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
667 //if (this.bot_dodgevector_time < time)
669 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
670 // this.bot_dodgevector_jumpbutton = 1;
671 evadeobstacle = '0 0 0';
676 if(this.waterlevel>WATERLEVEL_SWIMMING)
679 this.aistatus |= AI_STATUS_OUT_WATER;
683 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
684 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
685 PHYS_INPUT_BUTTON_JUMP(this) = true;
687 PHYS_INPUT_BUTTON_JUMP(this) = false;
689 dir = normalize(flatdir);
690 makevectors(this.v_angle.y * '0 1 0');
694 if(this.aistatus & AI_STATUS_OUT_WATER)
695 this.aistatus &= ~AI_STATUS_OUT_WATER;
697 // jump if going toward an obstacle that doesn't look like stairs we
698 // can walk up directly
699 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
700 if (trace_fraction < 1)
701 if (trace_plane_normal.z < 0.7)
704 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
705 if (trace_fraction < s + 0.01)
706 if (trace_plane_normal.z < 0.7)
709 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
710 if (trace_fraction > s)
711 PHYS_INPUT_BUTTON_JUMP(this) = true;
715 // avoiding dangers and obstacles
716 vector dst_ahead, dst_down;
717 makevectors(this.v_angle.y * '0 1 0');
718 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
719 dst_down = dst_ahead - '0 0 1500';
722 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
724 // Check head-banging against walls
725 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
727 PHYS_INPUT_BUTTON_JUMP(this) = true;
728 if(this.facingwalltime && time > this.facingwalltime)
730 this.ignoregoal = this.goalcurrent;
731 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
732 this.bot_strategytime = 0;
737 this.facingwalltime = time + 0.05;
742 this.facingwalltime = 0;
744 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
746 this.ignoregoal = NULL;
747 this.ignoregoaltime = 0;
751 // Check for water/slime/lava and dangerous edges
752 // (only when the bot is on the ground or jumping intentionally)
753 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
755 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
756 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
759 traceline(dst_ahead , dst_down, true, NULL);
760 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
761 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
762 if(trace_endpos.z < this.origin.z + this.mins.z)
764 s = pointcontents(trace_endpos + '0 0 1');
765 if (s != CONTENT_SOLID)
766 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
767 evadelava = normalize(this.velocity) * -1;
768 else if (s == CONTENT_SKY)
769 evadeobstacle = normalize(this.velocity) * -1;
770 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
771 this.goalcurrent.absmin, this.goalcurrent.absmax))
773 // if ain't a safe goal with "holes" (like the jumpad on soylent)
774 // and there is a trigger_hurt below
775 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
777 // Remove dangerous dynamic goals from stack
778 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
779 navigation_clearroute(this);
789 makevectors(this.v_angle.y * '0 1 0');
791 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
792 this.aistatus |= AI_STATUS_DANGER_AHEAD;
795 dodge = havocbot_dodge(this);
796 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
797 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
798 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
799 if(IS_PLAYER(trace_ent))
800 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
802 dir = normalize(dir + dodge + evadeobstacle + evadelava);
803 // this.bot_dodgevector = dir;
804 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
807 if(time < this.ladder_time)
809 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
811 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
816 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
821 //dir = this.bot_dodgevector;
822 //if (this.bot_dodgevector_jumpbutton)
823 // PHYS_INPUT_BUTTON_JUMP(this) = true;
824 this.movement_x = dir * v_forward * maxspeed;
825 this.movement_y = dir * v_right * maxspeed;
826 this.movement_z = dir * v_up * maxspeed;
828 // Emulate keyboard interface
830 havocbot_keyboard_movement(this, destorg);
833 // if(this.aistatus & AI_STATUS_ROAMING)
835 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
836 havocbot_bunnyhop(this, dir);
838 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
839 if (((dodge * v_up) > 0) && random()*frametime >= 0.2*bound(0,(10-skill-this.bot_dodgeskill)*0.1,1)) PHYS_INPUT_BUTTON_JUMP(this) = true;
840 if (((dodge * v_up) < 0) && random()*frametime >= 0.5*bound(0,(10-skill-this.bot_dodgeskill)*0.1,1)) this.havocbot_ducktime=time+0.3/bound(0.1,skill+this.bot_dodgeskill,10);
843 entity havocbot_gettarget(entity this, bool secondary)
846 vector eye = CENTER_OR_VIEWOFS(this);
847 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
849 vector v = CENTER_OR_VIEWOFS(it);
850 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
851 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
852 if(bot_shouldattack(this, it))
854 traceline(eye, v, true, this);
855 if (trace_ent == it || trace_fraction >= 1)
863 void havocbot_chooseenemy(entity this)
865 entity head, best, head2;
866 float rating, bestrating, hf;
868 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
875 if (!bot_shouldattack(this, this.enemy))
877 // enemy died or something, find a new target
879 this.havocbot_chooseenemy_finished = time;
881 else if (this.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(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
889 if (trace_ent == this.enemy || trace_fraction == 1)
890 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
891 if (this.health > 30)
893 // remain tracking him for a shot while (case he went after a small corner or pilar
894 this.havocbot_chooseenemy_finished = time + 0.5;
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 this.havocbot_stickenemy = 0;
903 if (time < this.havocbot_chooseenemy_finished)
905 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
906 eye = this.origin + this.view_ofs;
908 bestrating = 100000000;
909 head = head2 = findchainfloat(bot_attack, true);
912 hf = this.dphitcontentsmask;
914 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
916 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
918 bool scan_transparent = false;
919 bool scan_secondary_targets = false;
920 bool have_secondary_targets = false;
923 scan_secondary_targets = false;
925 for( ; head; head = head.chain)
927 if(!scan_secondary_targets)
929 if(head.classname == "misc_breakablemodel")
931 have_secondary_targets = true;
937 if(head.classname != "misc_breakablemodel")
941 v = (head.absmin + head.absmax) * 0.5;
942 rating = vlen(v - eye);
943 if (rating<autocvar_bot_ai_enemydetectionradius)
944 if (bestrating > rating)
945 if (bot_shouldattack(this, head))
947 traceline(eye, v, true, this);
948 if (trace_ent == head || trace_fraction >= 1)
956 if(!best && have_secondary_targets && !scan_secondary_targets)
958 scan_secondary_targets = true;
961 bestrating = 100000000;
965 // I want to do a second scan if no enemy was found or I don't have weapons
966 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
967 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
972 // Set flags to see through transparent objects
973 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
976 scan_transparent = true;
980 this.dphitcontentsmask = hf;
983 this.havocbot_stickenemy = true;
984 if(best && best.classname == "misc_breakablemodel")
985 this.havocbot_stickenemy = false;
988 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
990 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
991 // so skip this for them, or they'll never get to reload their weapons at all.
992 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
996 // if this weapon is scheduled for reloading, don't switch to it during combat
997 if (this.weapon_load[new_weapon] < 0)
999 bool other_weapon_available = false;
1000 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1001 if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
1002 other_weapon_available = true;
1004 if(other_weapon_available)
1011 void havocbot_chooseweapon(entity this)
1015 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
1018 if(g_weaponarena_weapons == WEPSET(TUBA))
1020 this.(weaponentity).m_switchweapon = WEP_TUBA;
1024 // TODO: clean this up by moving it to weapon code
1025 if(this.enemy==NULL)
1027 // If no weapon was chosen get the first available weapon
1028 if(this.(weaponentity).m_weapon==WEP_Null)
1029 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1030 if(client_hasweapon(this, it, true, false))
1032 this.(weaponentity).m_switchweapon = it;
1039 // Do not change weapon during the next second after a combo
1040 float f = time - this.lastcombotime;
1045 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1047 // Should it do a weapon combo?
1048 float af, ct, combo_time, combo;
1050 af = ATTACK_FINISHED(this, 0);
1051 ct = autocvar_bot_ai_weapon_combo_threshold;
1053 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1054 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1055 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1059 if(autocvar_bot_ai_weapon_combo)
1060 if(this.(weaponentity).m_weapon.m_id == this.lastfiredweapon)
1064 this.lastcombotime = time;
1067 distance *= pow(2, this.bot_rangepreference);
1069 // Custom weapon list based on distance to the enemy
1070 if(bot_custom_weapon){
1072 // Choose weapons for far distance
1073 if ( distance > bot_distance_far ) {
1074 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1075 w = bot_weapons_far[i];
1076 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1078 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1080 this.(weaponentity).m_switchweapon = Weapons_from(w);
1086 // Choose weapons for mid distance
1087 if ( distance > bot_distance_close) {
1088 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1089 w = bot_weapons_mid[i];
1090 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1092 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1094 this.(weaponentity).m_switchweapon = Weapons_from(w);
1100 // Choose weapons for close distance
1101 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1102 w = bot_weapons_close[i];
1103 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1105 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1107 this.(weaponentity).m_switchweapon = Weapons_from(w);
1114 void havocbot_aim(entity this)
1116 vector myvel, enemyvel;
1117 // if(this.flags & FL_INWATER)
1119 if (time < this.nextaim)
1121 this.nextaim = time + 0.1;
1122 myvel = this.velocity;
1123 if (!this.waterlevel)
1127 enemyvel = this.enemy.velocity;
1128 if (!this.enemy.waterlevel)
1130 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1133 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1136 bool havocbot_moveto_refresh_route(entity this)
1138 // Refresh path to goal if necessary
1140 wp = this.havocbot_personal_waypoint;
1141 navigation_goalrating_start(this);
1142 navigation_routerating(this, wp, 10000, 10000);
1143 navigation_goalrating_end(this);
1144 return this.navigation_hasgoals;
1147 float havocbot_moveto(entity this, vector pos)
1151 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1153 // Step 4: Move to waypoint
1154 if(this.havocbot_personal_waypoint==NULL)
1156 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1157 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1158 return CMD_STATUS_ERROR;
1161 if (!bot_strategytoken_taken)
1162 if(this.havocbot_personal_waypoint_searchtime<time)
1164 bot_strategytoken_taken = true;
1165 if(havocbot_moveto_refresh_route(this))
1167 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1168 this.havocbot_personal_waypoint_searchtime = time + 10;
1169 this.havocbot_personal_waypoint_failcounter = 0;
1173 this.havocbot_personal_waypoint_failcounter += 1;
1174 this.havocbot_personal_waypoint_searchtime = time + 2;
1175 if(this.havocbot_personal_waypoint_failcounter >= 30)
1177 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1178 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1179 delete(this.havocbot_personal_waypoint);
1180 return CMD_STATUS_ERROR;
1183 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1187 if(autocvar_bot_debug_goalstack)
1188 debuggoalstack(this);
1191 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1193 bot_aimdir(this, dir, -1);
1196 havocbot_movetogoal(this);
1198 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1200 // Step 5: Waypoint reached
1201 LOG_TRACE(this.netname, "'s personal waypoint reached");
1202 delete(this.havocbot_personal_waypoint);
1203 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1204 return CMD_STATUS_FINISHED;
1207 return CMD_STATUS_EXECUTING;
1210 // Step 2: Linking waypoint
1211 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1213 // Wait until it is linked
1214 if(!this.havocbot_personal_waypoint.wplinked)
1216 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1217 return CMD_STATUS_EXECUTING;
1220 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1221 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1222 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1224 // Step 3: Route to waypoint
1225 LOG_TRACE(this.netname, " walking to its personal waypoint");
1227 return CMD_STATUS_EXECUTING;
1230 // Step 1: Spawning waypoint
1231 wp = waypoint_spawnpersonal(this, pos);
1234 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1235 return CMD_STATUS_ERROR;
1238 this.havocbot_personal_waypoint = wp;
1239 this.havocbot_personal_waypoint_failcounter = 0;
1240 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1242 // if pos is inside a teleport, then let's mark it as teleport waypoint
1243 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1245 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1246 this.lastteleporttime = 0;
1250 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1251 print("routing to a teleporter\n");
1253 print("routing to a non-teleporter\n");
1256 return CMD_STATUS_EXECUTING;
1259 float havocbot_resetgoal(entity this)
1261 navigation_clearroute(this);
1262 return CMD_STATUS_FINISHED;
1265 void havocbot_setupbot(entity this)
1267 this.bot_ai = havocbot_ai;
1268 this.cmd_moveto = havocbot_moveto;
1269 this.cmd_resetgoal = havocbot_resetgoal;
1271 havocbot_chooserole(this);
1274 vector havocbot_dodge(entity this)
1276 // LordHavoc: disabled because this is too expensive
1281 float danger, bestdanger, vl, d;
1284 // check for dangerous objects near bot or approaching bot
1285 head = findchainfloat(bot_dodge, true);
1288 if (head.owner != this)
1290 vl = vlen(head.velocity);
1291 if (vl > autocvar_sv_maxspeed * 0.3)
1293 n = normalize(head.velocity);
1294 v = this.origin - head.origin;
1296 if (d > (0 - head.bot_dodgerating))
1297 if (d < (vl * 0.2 + head.bot_dodgerating))
1299 // calculate direction and distance from the flight path, by removing the forward axis
1300 v = v - (n * (v * n));
1301 danger = head.bot_dodgerating - vlen(v);
1302 if (bestdanger < danger)
1304 bestdanger = danger;
1305 // dodge to the side of the object
1306 dodge = normalize(v);
1312 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1313 if (bestdanger < danger)
1315 bestdanger = danger;
1316 dodge = normalize(this.origin - head.origin);