7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
11 #include <common/constants.qh>
12 #include <common/impulses/all.qh>
13 #include <common/net_linked.qh>
14 #include <common/physics/player.qh>
15 #include <common/state.qh>
16 #include <common/items/_mod.qh>
17 #include <common/wepent.qh>
19 #include <common/triggers/teleporters.qh>
20 #include <common/triggers/trigger/jumppads.qh>
22 #include <lib/warpzone/common.qh>
26 void havocbot_ai(entity this)
31 if(bot_execute_commands(this))
34 if (bot_strategytoken == this)
35 if (!bot_strategytoken_taken)
37 if(this.havocbot_blockhead)
39 this.havocbot_blockhead = false;
43 if (!this.jumppadcount)
44 this.havocbot_role(this); // little too far down the rabbit hole
47 // TODO: tracewalk() should take care of this job (better path finding under water)
48 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
51 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
53 // Look for the closest waypoint out of water
54 entity newgoal = NULL;
55 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
57 if(it.origin.z < this.origin.z)
60 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
63 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
66 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
68 if(trace_fraction < 1)
71 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
77 // te_wizspike(newgoal.origin);
78 navigation_pushroute(this, newgoal);
82 // token has been used this frame
83 bot_strategytoken_taken = true;
89 havocbot_chooseenemy(this);
91 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
93 .entity weaponentity = weaponentities[slot];
94 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
95 if(this.(weaponentity).bot_chooseweapontime < time)
97 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
98 havocbot_chooseweapon(this, weaponentity);
103 if (this.bot_aimtarg)
105 this.aistatus |= AI_STATUS_ATTACKING;
106 this.aistatus &= ~AI_STATUS_ROAMING;
110 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
112 PHYS_INPUT_BUTTON_ATCK(this) = false;
113 PHYS_INPUT_BUTTON_ATCK2(this) = false;
117 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
119 .entity weaponentity = weaponentities[slot];
120 Weapon w = this.(weaponentity).m_weapon;
121 if(w == WEP_Null && slot != 0)
123 w.wr_aim(w, this, weaponentity);
124 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
125 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
131 if(IS_PLAYER(this.bot_aimtarg))
132 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
135 else if (this.goalcurrent)
137 this.aistatus |= AI_STATUS_ROAMING;
138 this.aistatus &= ~AI_STATUS_ATTACKING;
140 vector now,v,next;//,heading;
141 float aimdistance,skillblend,distanceblend,blend;
142 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
143 aimdistance = vlen(now);
144 //heading = this.velocity;
145 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
147 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
148 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
150 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
152 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
153 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
154 blend = skillblend * (1-distanceblend);
155 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
156 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
157 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
158 v = now + blend * (next - now);
159 //dprint(etos(this), " ");
160 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
161 //v = now * (distanceblend) + next * (1-distanceblend);
162 if (this.waterlevel < WATERLEVEL_SWIMMING)
164 //dprint("walk at:", vtos(v), "\n");
165 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
166 bot_aimdir(this, v, -1);
168 havocbot_movetogoal(this);
170 // if the bot is not attacking, consider reloading weapons
171 if (!(this.aistatus & AI_STATUS_ATTACKING))
173 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
175 .entity weaponentity = weaponentities[slot];
177 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
180 // we are currently holding a weapon that's not fully loaded, reload it
181 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
182 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
183 this.impulse = IMP_weapon_reload.impulse; // not sure if this is done right
185 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
186 // the code above executes next frame, starting the reloading then
187 if(skill >= 5) // bots can only look for unloaded weapons past this skill
188 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
190 FOREACH(Weapons, it != WEP_Null, LAMBDA(
191 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
193 this.(weaponentity).m_switchweapon = it;
202 void havocbot_keyboard_movement(entity this, vector destorg)
205 float blend, maxspeed;
208 sk = skill + this.bot_moveskill;
210 maxspeed = autocvar_sv_maxspeed;
212 if (time < this.havocbot_keyboardtime)
215 this.havocbot_keyboardtime =
217 this.havocbot_keyboardtime
218 + 0.05/max(1, sk+this.havocbot_keyboardskill)
219 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
221 keyboard = this.movement * (1.0 / maxspeed);
223 float trigger, trigger1;
224 blend = bound(0,sk*0.1,1);
225 trigger = autocvar_bot_ai_keyboard_threshold;
226 trigger1 = 0 - trigger;
228 // categorize forward movement
229 // at skill < 1.5 only forward
230 // at skill < 2.5 only individual directions
231 // at skill < 4.5 only individual directions, and forward diagonals
232 // at skill >= 4.5, all cases allowed
233 if (keyboard.x > trigger)
239 else if (keyboard.x < trigger1 && sk > 1.5)
254 if (keyboard.y > trigger)
256 else if (keyboard.y < trigger1)
261 if (keyboard.z > trigger)
263 else if (keyboard.z < trigger1)
268 this.havocbot_keyboard = keyboard * maxspeed;
269 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
271 keyboard = this.havocbot_keyboard;
272 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
273 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
274 this.movement = this.movement + (keyboard - this.movement) * blend;
277 void havocbot_bunnyhop(entity this, vector dir)
279 float bunnyhopdistance;
284 // Don't jump when attacking
285 if(this.aistatus & AI_STATUS_ATTACKING)
288 if(IS_PLAYER(this.goalcurrent))
291 maxspeed = autocvar_sv_maxspeed;
293 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
295 this.aistatus &= ~AI_STATUS_RUNNING;
296 PHYS_INPUT_BUTTON_JUMP(this) = false;
297 this.bot_canruntogoal = 0;
298 this.bot_timelastseengoal = 0;
302 if(this.waterlevel > WATERLEVEL_WETFEET)
304 this.aistatus &= ~AI_STATUS_RUNNING;
308 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
310 this.bot_canruntogoal = 0;
311 this.bot_timelastseengoal = 0;
314 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
315 bunnyhopdistance = vlen(this.origin - gco);
317 // Run only to visible goals
318 if(IS_ONGROUND(this))
319 if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
320 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
322 this.bot_lastseengoal = this.goalcurrent;
325 if(this.bot_timelastseengoal)
327 // for a period of time
328 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
331 checkdistance = true;
333 // don't run if it is too close
334 if(this.bot_canruntogoal==0)
336 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
337 this.bot_canruntogoal = 1;
339 this.bot_canruntogoal = -1;
342 if(this.bot_canruntogoal != 1)
345 if(this.aistatus & AI_STATUS_ROAMING)
346 if(this.goalcurrent.classname=="waypoint")
347 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
348 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
349 if(this.goalstack01 && !wasfreed(this.goalstack01))
351 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
352 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
353 while (deviation.y < -180) deviation.y = deviation.y + 360;
354 while (deviation.y > 180) deviation.y = deviation.y - 360;
356 if(fabs(deviation.y) < 20)
357 if(bunnyhopdistance < vlen(this.origin - gno))
358 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
360 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
361 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
363 checkdistance = false;
370 this.aistatus &= ~AI_STATUS_RUNNING;
371 // increase stop distance in case the goal is on a slope or a lower platform
372 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
373 PHYS_INPUT_BUTTON_JUMP(this) = true;
377 this.aistatus |= AI_STATUS_RUNNING;
378 PHYS_INPUT_BUTTON_JUMP(this) = true;
384 this.bot_timelastseengoal = time;
389 this.bot_timelastseengoal = 0;
393 // Release jump button
394 if(!cvar("sv_pogostick"))
395 if((IS_ONGROUND(this)) == 0)
397 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
398 PHYS_INPUT_BUTTON_JUMP(this) = false;
401 if(this.aistatus & AI_STATUS_RUNNING)
402 if(vlen(this.velocity)>maxspeed)
404 deviation = vectoangles(dir) - vectoangles(this.velocity);
405 while (deviation.y < -180) deviation.y = deviation.y + 360;
406 while (deviation.y > 180) deviation.y = deviation.y - 360;
408 if(fabs(deviation.y)>10)
412 this.movement_y = maxspeed * -1;
413 else if(deviation.y<10)
414 this.movement_y = maxspeed;
421 void havocbot_movetogoal(entity this)
429 vector evadeobstacle;
435 //if (this.goalentity)
436 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
437 this.movement = '0 0 0';
438 maxspeed = autocvar_sv_maxspeed;
440 // Jetpack navigation
442 if(this.navigation_jetpack_goal)
443 if(this.goalcurrent==this.navigation_jetpack_goal)
446 if(autocvar_bot_debug_goalstack)
448 debuggoalstack(this);
449 te_wizspike(this.navigation_jetpack_point);
453 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
455 // Brake almost completely so it can get a good direction
456 if(vdist(this.velocity, >, 10))
458 this.aistatus |= AI_STATUS_JETPACK_FLYING;
461 makevectors(this.v_angle.y * '0 1 0');
462 dir = normalize(this.navigation_jetpack_point - this.origin);
465 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
467 // Calculate brake distance in xy
471 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
473 v = vlen(this.velocity - this.velocity.z * '0 0 1');
474 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
475 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
476 if(d < db || d < 500)
479 if(fabs(this.velocity.x)>maxspeed*0.3)
481 this.movement_x = dir * v_forward * -maxspeed;
484 // Switch to normal mode
485 this.navigation_jetpack_goal = NULL;
486 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
487 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
491 else if(checkpvs(this.origin,this.goalcurrent))
493 // If I can see the goal switch to landing code
494 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
495 this.aistatus |= AI_STATUS_JETPACK_LANDING;
500 PHYS_INPUT_BUTTON_HOOK(this) = true;
501 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
503 this.movement_x = dir * v_forward * maxspeed;
504 this.movement_y = dir * v_right * maxspeed;
509 // Handling of jump pads
510 if(this.jumppadcount)
512 // If got stuck on the jump pad try to reach the farthest visible waypoint
513 // but with some randomness so it can try out different paths
514 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
516 if(fabs(this.velocity.z)<50)
518 entity newgoal = NULL;
519 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
521 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
523 if(trace_fraction < 1)
526 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
532 this.ignoregoal = this.goalcurrent;
533 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
534 navigation_clearroute(this);
535 navigation_routetogoal(this, newgoal, this.origin);
536 if(autocvar_bot_debug_goalstack)
537 debuggoalstack(this);
538 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
546 if(this.velocity.z>0)
549 vector velxy = this.velocity; velxy_z = 0;
550 threshold = maxspeed * 0.2;
551 if(vdist(velxy, <, threshold))
553 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
554 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
559 // Don't chase players while using a jump pad
560 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
564 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
565 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
567 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
569 if (!(IS_ONGROUND(this)))
571 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
572 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
573 if(this.items & IT_JETPACK)
575 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
576 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
578 if(this.velocity.z<0)
580 PHYS_INPUT_BUTTON_HOOK(this) = true;
584 PHYS_INPUT_BUTTON_HOOK(this) = true;
586 // If there is no goal try to move forward
588 if(this.goalcurrent==NULL)
591 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
593 vector xyvelocity = this.velocity; xyvelocity_z = 0;
594 float xyspeed = xyvelocity * dir;
596 if(xyspeed < (maxspeed / 2))
598 makevectors(this.v_angle.y * '0 1 0');
599 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
600 if(trace_fraction==1)
602 this.movement_x = dir * v_forward * maxspeed;
603 this.movement_y = dir * v_right * maxspeed;
605 havocbot_keyboard_movement(this, this.origin + dir * 100);
609 this.havocbot_blockhead = true;
613 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
615 if(this.velocity.z < 0)
617 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
619 .entity weaponentity = weaponentities[slot];
621 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
624 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
626 this.movement_x = maxspeed;
628 if(this.rocketjumptime)
630 if(time > this.rocketjumptime)
632 PHYS_INPUT_BUTTON_ATCK2(this) = true;
633 this.rocketjumptime = 0;
638 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
640 PHYS_INPUT_BUTTON_ATCK(this) = true;
641 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
649 // If there is no goal try to move forward
650 if(this.goalcurrent==NULL)
651 this.movement_x = maxspeed;
655 // If we are under water with no goals, swim up
657 if(this.goalcurrent==NULL)
660 if(this.waterlevel>WATERLEVEL_SWIMMING)
662 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
663 PHYS_INPUT_BUTTON_JUMP(this) = true;
665 PHYS_INPUT_BUTTON_JUMP(this) = false;
666 makevectors(this.v_angle.y * '0 1 0');
667 this.movement_x = dir * v_forward * maxspeed;
668 this.movement_y = dir * v_right * maxspeed;
669 this.movement_z = dir * v_up * maxspeed;
672 // if there is nowhere to go, exit
673 if (this.goalcurrent == NULL)
676 navigation_poptouchedgoals(this);
678 // if ran out of goals try to use an alternative goal or get a new strategy asap
679 if(this.goalcurrent == NULL)
681 this.bot_strategytime = 0;
686 if(autocvar_bot_debug_goalstack)
687 debuggoalstack(this);
689 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
690 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
691 destorg = this.origin;
692 destorg.x = bound(m1_x, destorg.x, m2_x);
693 destorg.y = bound(m1_y, destorg.y, m2_y);
694 destorg.z = bound(m1_z, destorg.z, m2_z);
695 diff = destorg - this.origin;
697 dir = normalize(diff);
698 flatdir = diff;flatdir.z = 0;
699 flatdir = normalize(flatdir);
700 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
702 //if (this.bot_dodgevector_time < time)
704 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
705 // this.bot_dodgevector_jumpbutton = 1;
706 evadeobstacle = '0 0 0';
711 if(this.waterlevel>WATERLEVEL_SWIMMING)
714 this.aistatus |= AI_STATUS_OUT_WATER;
718 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
719 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
720 PHYS_INPUT_BUTTON_JUMP(this) = true;
722 PHYS_INPUT_BUTTON_JUMP(this) = false;
724 dir = normalize(flatdir);
725 makevectors(this.v_angle.y * '0 1 0');
730 if(this.aistatus & AI_STATUS_OUT_WATER)
731 this.aistatus &= ~AI_STATUS_OUT_WATER;
733 // jump if going toward an obstacle that doesn't look like stairs we
734 // can walk up directly
735 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
736 if (trace_fraction < 1)
737 if (trace_plane_normal.z < 0.7)
740 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
741 if (trace_fraction < s + 0.01)
742 if (trace_plane_normal.z < 0.7)
745 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
746 if (trace_fraction > s)
747 PHYS_INPUT_BUTTON_JUMP(this) = true;
751 // avoiding dangers and obstacles
752 vector dst_ahead = this.origin + this.view_ofs + this.velocity * 0.5;
753 vector dst_down = dst_ahead - '0 0 3000';
756 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
758 // Check head-banging against walls
759 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
761 PHYS_INPUT_BUTTON_JUMP(this) = true;
762 if(this.facingwalltime && time > this.facingwalltime)
764 this.ignoregoal = this.goalcurrent;
765 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
766 this.bot_strategytime = 0;
771 this.facingwalltime = time + 0.05;
776 this.facingwalltime = 0;
778 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
780 this.ignoregoal = NULL;
781 this.ignoregoaltime = 0;
785 // Check for water/slime/lava and dangerous edges
786 // (only when the bot is on the ground or jumping intentionally)
787 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
789 bool unreachable = false;
791 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
792 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
795 traceline(dst_ahead , dst_down, true, NULL);
796 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
797 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
798 if(trace_endpos.z < this.origin.z + this.mins.z)
800 s = pointcontents(trace_endpos + '0 0 1');
801 if (s != CONTENT_SOLID)
802 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
803 evadelava = normalize(this.velocity) * -1;
804 else if (s == CONTENT_SKY)
805 evadeobstacle = normalize(this.velocity) * -1;
806 else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
808 // the traceline check isn't enough but is good as optimization,
809 // when not true (most of the time) this tracebox call is avoided
810 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
811 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
813 if (gco.z > this.origin.z + jumpstepheightvec.z)
815 // the goal is probably on an upper platform, assume bot can't get there
819 evadelava = normalize(this.velocity) * -1;
828 makevectors(this.v_angle.y * '0 1 0');
830 if(evadeobstacle || evadelava || (s == CONTENT_WATER))
832 this.aistatus |= AI_STATUS_DANGER_AHEAD;
833 if(IS_PLAYER(this.goalcurrent))
838 navigation_clearroute(this);
839 this.bot_strategytime = 0;
843 dodge = havocbot_dodge(this);
844 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
845 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
846 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
847 if(IS_PLAYER(trace_ent))
848 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
850 dir = normalize(dir + dodge + evadeobstacle + evadelava);
851 // this.bot_dodgevector = dir;
852 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
855 if(time < this.ladder_time)
857 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
859 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
864 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
869 //dir = this.bot_dodgevector;
870 //if (this.bot_dodgevector_jumpbutton)
871 // PHYS_INPUT_BUTTON_JUMP(this) = true;
872 this.movement_x = dir * v_forward * maxspeed;
873 this.movement_y = dir * v_right * maxspeed;
874 this.movement_z = dir * v_up * maxspeed;
876 // Emulate keyboard interface
878 havocbot_keyboard_movement(this, destorg);
881 // if(this.aistatus & AI_STATUS_ROAMING)
883 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
884 havocbot_bunnyhop(this, dir);
886 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
887 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;
888 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);
891 entity havocbot_gettarget(entity this, bool secondary)
894 vector eye = CENTER_OR_VIEWOFS(this);
895 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
897 vector v = CENTER_OR_VIEWOFS(it);
898 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
899 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
900 if(bot_shouldattack(this, it))
902 traceline(eye, v, true, this);
903 if (trace_ent == it || trace_fraction >= 1)
911 void havocbot_chooseenemy(entity this)
913 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
920 if (!bot_shouldattack(this, this.enemy))
922 // enemy died or something, find a new target
924 this.havocbot_chooseenemy_finished = time;
926 else if (this.havocbot_stickenemy)
928 // tracking last chosen enemy
929 // if enemy is visible
930 // and not really really far away
931 // and we're not severely injured
932 // then keep tracking for a half second into the future
933 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
934 if (trace_ent == this.enemy || trace_fraction == 1)
935 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
936 if (this.health > 30)
938 // remain tracking him for a shot while (case he went after a small corner or pilar
939 this.havocbot_chooseenemy_finished = time + 0.5;
942 // enemy isn't visible, or is far away, or we're injured severely
943 // so stop preferring this enemy
944 // (it will still take a half second until a new one is chosen)
945 this.havocbot_stickenemy = 0;
948 if (time < this.havocbot_chooseenemy_finished)
950 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
951 vector eye = this.origin + this.view_ofs;
953 float bestrating = 100000000;
956 int hf = this.dphitcontentsmask;
958 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
960 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
962 bool scan_transparent = false;
963 bool scan_secondary_targets = false;
964 bool have_secondary_targets = false;
967 scan_secondary_targets = false;
969 IL_EACH(g_bot_targets, it.bot_attack,
971 if(!scan_secondary_targets)
973 if(it.classname == "misc_breakablemodel")
975 have_secondary_targets = true;
979 else if(it.classname != "misc_breakablemodel")
982 vector v = (it.absmin + it.absmax) * 0.5;
983 float rating = vlen2(v - eye);
984 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
985 if (bestrating > rating)
986 if (bot_shouldattack(this, it))
988 traceline(eye, v, true, this);
989 if (trace_ent == it || trace_fraction >= 1)
997 if(!best && have_secondary_targets && !scan_secondary_targets)
999 scan_secondary_targets = true;
1001 bestrating = 100000000;
1005 // I want to do a second scan if no enemy was found or I don't have weapons
1006 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1007 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1009 if(scan_transparent)
1012 // Set flags to see through transparent objects
1013 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1015 scan_transparent = true;
1018 // Restore hit flags
1019 this.dphitcontentsmask = hf;
1022 this.havocbot_stickenemy = true;
1023 if(best && best.classname == "misc_breakablemodel")
1024 this.havocbot_stickenemy = false;
1027 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1029 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1030 // so skip this for them, or they'll never get to reload their weapons at all.
1031 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1035 // if this weapon is scheduled for reloading, don't switch to it during combat
1036 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1038 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1039 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1040 return true; // other weapon available
1047 void havocbot_chooseweapon(entity this, .entity weaponentity)
1052 if(g_weaponarena_weapons == WEPSET(TUBA))
1054 this.(weaponentity).m_switchweapon = WEP_TUBA;
1058 // TODO: clean this up by moving it to weapon code
1059 if(this.enemy==NULL)
1061 // If no weapon was chosen get the first available weapon
1062 if(this.(weaponentity).m_weapon==WEP_Null)
1063 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1064 if(client_hasweapon(this, it, weaponentity, true, false))
1066 this.(weaponentity).m_switchweapon = it;
1073 // Do not change weapon during the next second after a combo
1074 float f = time - this.lastcombotime;
1079 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1081 // Should it do a weapon combo?
1082 float af, ct, combo_time, combo;
1084 af = ATTACK_FINISHED(this, 0);
1085 ct = autocvar_bot_ai_weapon_combo_threshold;
1087 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1088 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1089 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1093 if(autocvar_bot_ai_weapon_combo)
1094 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1098 this.lastcombotime = time;
1101 distance *= pow(2, this.bot_rangepreference);
1103 // Custom weapon list based on distance to the enemy
1104 if(bot_custom_weapon){
1106 // Choose weapons for far distance
1107 if ( distance > bot_distance_far ) {
1108 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1109 w = bot_weapons_far[i];
1110 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1112 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1114 this.(weaponentity).m_switchweapon = Weapons_from(w);
1120 // Choose weapons for mid distance
1121 if ( distance > bot_distance_close) {
1122 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1123 w = bot_weapons_mid[i];
1124 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1126 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1128 this.(weaponentity).m_switchweapon = Weapons_from(w);
1134 // Choose weapons for close distance
1135 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1136 w = bot_weapons_close[i];
1137 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1139 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1141 this.(weaponentity).m_switchweapon = Weapons_from(w);
1148 void havocbot_aim(entity this)
1150 vector myvel, enemyvel;
1151 // if(this.flags & FL_INWATER)
1153 if (time < this.nextaim)
1155 this.nextaim = time + 0.1;
1156 myvel = this.velocity;
1157 if (!this.waterlevel)
1161 enemyvel = this.enemy.velocity;
1162 if (!this.enemy.waterlevel)
1164 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1167 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1170 bool havocbot_moveto_refresh_route(entity this)
1172 // Refresh path to goal if necessary
1174 wp = this.havocbot_personal_waypoint;
1175 navigation_goalrating_start(this);
1176 navigation_routerating(this, wp, 10000, 10000);
1177 navigation_goalrating_end(this);
1178 return this.navigation_hasgoals;
1181 float havocbot_moveto(entity this, vector pos)
1185 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1187 // Step 4: Move to waypoint
1188 if(this.havocbot_personal_waypoint==NULL)
1190 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1191 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1192 return CMD_STATUS_ERROR;
1195 if (!bot_strategytoken_taken)
1196 if(this.havocbot_personal_waypoint_searchtime<time)
1198 bot_strategytoken_taken = true;
1199 if(havocbot_moveto_refresh_route(this))
1201 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1202 this.havocbot_personal_waypoint_searchtime = time + 10;
1203 this.havocbot_personal_waypoint_failcounter = 0;
1207 this.havocbot_personal_waypoint_failcounter += 1;
1208 this.havocbot_personal_waypoint_searchtime = time + 2;
1209 if(this.havocbot_personal_waypoint_failcounter >= 30)
1211 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1212 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1213 delete(this.havocbot_personal_waypoint);
1214 return CMD_STATUS_ERROR;
1217 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1221 if(autocvar_bot_debug_goalstack)
1222 debuggoalstack(this);
1225 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1227 bot_aimdir(this, dir, -1);
1230 havocbot_movetogoal(this);
1232 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1234 // Step 5: Waypoint reached
1235 LOG_TRACE(this.netname, "'s personal waypoint reached");
1236 delete(this.havocbot_personal_waypoint);
1237 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1238 return CMD_STATUS_FINISHED;
1241 return CMD_STATUS_EXECUTING;
1244 // Step 2: Linking waypoint
1245 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1247 // Wait until it is linked
1248 if(!this.havocbot_personal_waypoint.wplinked)
1250 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1251 return CMD_STATUS_EXECUTING;
1254 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1255 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1256 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1258 // Step 3: Route to waypoint
1259 LOG_TRACE(this.netname, " walking to its personal waypoint");
1261 return CMD_STATUS_EXECUTING;
1264 // Step 1: Spawning waypoint
1265 wp = waypoint_spawnpersonal(this, pos);
1268 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1269 return CMD_STATUS_ERROR;
1272 this.havocbot_personal_waypoint = wp;
1273 this.havocbot_personal_waypoint_failcounter = 0;
1274 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1276 // if pos is inside a teleport, then let's mark it as teleport waypoint
1277 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1279 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1280 this.lastteleporttime = 0;
1284 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1285 print("routing to a teleporter\n");
1287 print("routing to a non-teleporter\n");
1290 return CMD_STATUS_EXECUTING;
1293 float havocbot_resetgoal(entity this)
1295 navigation_clearroute(this);
1296 return CMD_STATUS_FINISHED;
1299 void havocbot_setupbot(entity this)
1301 this.bot_ai = havocbot_ai;
1302 this.cmd_moveto = havocbot_moveto;
1303 this.cmd_resetgoal = havocbot_resetgoal;
1305 havocbot_chooserole(this);
1308 vector havocbot_dodge(entity this)
1310 // LordHavoc: disabled because this is too expensive
1315 float danger, bestdanger, vl, d;
1318 // check for dangerous objects near bot or approaching bot
1319 head = findchainfloat(bot_dodge, true);
1322 if (head.owner != this)
1324 vl = vlen(head.velocity);
1325 if (vl > autocvar_sv_maxspeed * 0.3)
1327 n = normalize(head.velocity);
1328 v = this.origin - head.origin;
1330 if (d > (0 - head.bot_dodgerating))
1331 if (d < (vl * 0.2 + head.bot_dodgerating))
1333 // calculate direction and distance from the flight path, by removing the forward axis
1334 v = v - (n * (v * n));
1335 danger = head.bot_dodgerating - vlen(v);
1336 if (bestdanger < danger)
1338 bestdanger = danger;
1339 // dodge to the side of the object
1340 dodge = normalize(v);
1346 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1347 if (bestdanger < danger)
1349 bestdanger = danger;
1350 dodge = normalize(this.origin - head.origin);