7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
11 #include <common/constants.qh>
12 #include <common/net_linked.qh>
13 #include <common/physics/player.qh>
14 #include <common/state.qh>
15 #include <common/items/_mod.qh>
16 #include <common/wepent.qh>
18 #include <common/triggers/teleporters.qh>
19 #include <common/triggers/trigger/jumppads.qh>
21 #include <lib/warpzone/common.qh>
25 void havocbot_ai(entity this)
30 if(bot_execute_commands(this))
34 if(wasfreed(this.goalcurrent))
35 navigation_poproute(this);
37 if (bot_strategytoken == this)
38 if (!bot_strategytoken_taken)
40 if(this.havocbot_blockhead)
42 this.havocbot_blockhead = false;
46 if (!this.jumppadcount)
47 this.havocbot_role(this); // little too far down the rabbit hole
50 // TODO: tracewalk() should take care of this job (better path finding under water)
51 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
54 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
56 // Look for the closest waypoint out of water
57 entity newgoal = NULL;
58 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
60 if(it.origin.z < this.origin.z)
63 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
66 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
69 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
71 if(trace_fraction < 1)
74 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
80 // te_wizspike(newgoal.origin);
81 navigation_pushroute(this, newgoal);
85 // token has been used this frame
86 bot_strategytoken_taken = true;
92 havocbot_chooseenemy(this);
94 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
96 .entity weaponentity = weaponentities[slot];
97 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
98 if(this.(weaponentity).bot_chooseweapontime < time)
100 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
101 havocbot_chooseweapon(this, weaponentity);
106 if (this.bot_aimtarg)
108 this.aistatus |= AI_STATUS_ATTACKING;
109 this.aistatus &= ~AI_STATUS_ROAMING;
113 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
115 PHYS_INPUT_BUTTON_ATCK(this) = false;
116 PHYS_INPUT_BUTTON_ATCK2(this) = false;
120 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
122 .entity weaponentity = weaponentities[slot];
123 Weapon w = this.(weaponentity).m_weapon;
124 if(w == WEP_Null && slot != 0)
126 w.wr_aim(w, this, weaponentity);
127 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
128 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
134 if(IS_PLAYER(this.bot_aimtarg))
135 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
138 else if (this.goalcurrent)
140 this.aistatus |= AI_STATUS_ROAMING;
141 this.aistatus &= ~AI_STATUS_ATTACKING;
143 vector now,v,next;//,heading;
144 float aimdistance,skillblend,distanceblend,blend;
145 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
146 aimdistance = vlen(now);
147 //heading = this.velocity;
148 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
150 this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
151 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
153 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
155 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
156 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
157 blend = skillblend * (1-distanceblend);
158 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
159 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
160 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
161 v = now + blend * (next - now);
162 //dprint(etos(this), " ");
163 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
164 //v = now * (distanceblend) + next * (1-distanceblend);
165 if (this.waterlevel < WATERLEVEL_SWIMMING)
167 //dprint("walk at:", vtos(v), "\n");
168 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
169 bot_aimdir(this, v, -1);
171 havocbot_movetogoal(this);
173 // if the bot is not attacking, consider reloading weapons
174 if (!(this.aistatus & AI_STATUS_ATTACKING))
176 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
178 .entity weaponentity = weaponentities[slot];
180 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
183 // we are currently holding a weapon that's not fully loaded, reload it
184 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
185 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
186 this.impulse = 20; // "press" the reload button, not sure if this is done right
188 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
189 // the code above executes next frame, starting the reloading then
190 if(skill >= 5) // bots can only look for unloaded weapons past this skill
191 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
193 FOREACH(Weapons, it != WEP_Null, LAMBDA(
194 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
195 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(this.speed==maxspeed)
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!=NULL)
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 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
372 PHYS_INPUT_BUTTON_JUMP(this) = true;
376 this.aistatus |= AI_STATUS_RUNNING;
377 PHYS_INPUT_BUTTON_JUMP(this) = true;
383 this.bot_timelastseengoal = time;
388 this.bot_timelastseengoal = 0;
392 // Release jump button
393 if(!cvar("sv_pogostick"))
394 if((IS_ONGROUND(this)) == 0)
396 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
397 PHYS_INPUT_BUTTON_JUMP(this) = false;
400 if(this.aistatus & AI_STATUS_RUNNING)
401 if(vlen(this.velocity)>maxspeed)
403 deviation = vectoangles(dir) - vectoangles(this.velocity);
404 while (deviation.y < -180) deviation.y = deviation.y + 360;
405 while (deviation.y > 180) deviation.y = deviation.y - 360;
407 if(fabs(deviation.y)>10)
411 this.movement_y = maxspeed * -1;
412 else if(deviation.y<10)
413 this.movement_y = maxspeed;
420 void havocbot_movetogoal(entity this)
428 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, NULL).z + STAT(PL_MIN, NULL).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 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
515 if(fabs(this.velocity.z)<50)
517 entity newgoal = NULL;
518 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
520 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
522 if(trace_fraction < 1)
525 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
531 this.ignoregoal = this.goalcurrent;
532 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
533 navigation_clearroute(this);
534 navigation_routetogoal(this, newgoal, this.origin);
535 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
543 if(this.velocity.z>0)
546 vector velxy = this.velocity; velxy_z = 0;
547 threshold = maxspeed * 0.2;
548 if(vdist(velxy, <, threshold))
550 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
551 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
556 // Don't chase players while using a jump pad
557 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
561 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
562 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
564 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
566 if (!(IS_ONGROUND(this)))
568 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
569 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
570 if(this.items & IT_JETPACK)
572 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
573 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
575 if(this.velocity.z<0)
577 PHYS_INPUT_BUTTON_HOOK(this) = true;
581 PHYS_INPUT_BUTTON_HOOK(this) = true;
583 // If there is no goal try to move forward
585 if(this.goalcurrent==NULL)
588 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
590 vector xyvelocity = this.velocity; xyvelocity_z = 0;
591 float xyspeed = xyvelocity * dir;
593 if(xyspeed < (maxspeed / 2))
595 makevectors(this.v_angle.y * '0 1 0');
596 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
597 if(trace_fraction==1)
599 this.movement_x = dir * v_forward * maxspeed;
600 this.movement_y = dir * v_right * maxspeed;
602 havocbot_keyboard_movement(this, this.origin + dir * 100);
606 this.havocbot_blockhead = true;
610 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
612 if(this.velocity.z < 0)
614 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
616 .entity weaponentity = weaponentities[slot];
618 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
621 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
623 this.movement_x = maxspeed;
625 if(this.rocketjumptime)
627 if(time > this.rocketjumptime)
629 PHYS_INPUT_BUTTON_ATCK2(this) = true;
630 this.rocketjumptime = 0;
635 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
637 PHYS_INPUT_BUTTON_ATCK(this) = true;
638 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
646 // If there is no goal try to move forward
647 if(this.goalcurrent==NULL)
648 this.movement_x = maxspeed;
652 // If we are under water with no goals, swim up
654 if(this.goalcurrent==NULL)
657 if(this.waterlevel>WATERLEVEL_SWIMMING)
659 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
660 PHYS_INPUT_BUTTON_JUMP(this) = true;
662 PHYS_INPUT_BUTTON_JUMP(this) = false;
663 makevectors(this.v_angle.y * '0 1 0');
664 this.movement_x = dir * v_forward * maxspeed;
665 this.movement_y = dir * v_right * maxspeed;
666 this.movement_z = dir * v_up * maxspeed;
669 // if there is nowhere to go, exit
670 if (this.goalcurrent == NULL)
673 if (this.goalcurrent)
674 navigation_poptouchedgoals(this);
676 // if ran out of goals try to use an alternative goal or get a new strategy asap
677 if(this.goalcurrent == NULL)
679 this.bot_strategytime = 0;
684 if(autocvar_bot_debug_goalstack)
685 debuggoalstack(this);
687 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
688 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
689 destorg = this.origin;
690 destorg.x = bound(m1_x, destorg.x, m2_x);
691 destorg.y = bound(m1_y, destorg.y, m2_y);
692 destorg.z = bound(m1_z, destorg.z, m2_z);
693 diff = destorg - this.origin;
695 dir = normalize(diff);
696 flatdir = diff;flatdir.z = 0;
697 flatdir = normalize(flatdir);
698 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
700 //if (this.bot_dodgevector_time < time)
702 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
703 // this.bot_dodgevector_jumpbutton = 1;
704 evadeobstacle = '0 0 0';
709 if(this.waterlevel>WATERLEVEL_SWIMMING)
712 this.aistatus |= AI_STATUS_OUT_WATER;
716 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
717 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
718 PHYS_INPUT_BUTTON_JUMP(this) = true;
720 PHYS_INPUT_BUTTON_JUMP(this) = false;
722 dir = normalize(flatdir);
723 makevectors(this.v_angle.y * '0 1 0');
727 if(this.aistatus & AI_STATUS_OUT_WATER)
728 this.aistatus &= ~AI_STATUS_OUT_WATER;
730 // jump if going toward an obstacle that doesn't look like stairs we
731 // can walk up directly
732 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
733 if (trace_fraction < 1)
734 if (trace_plane_normal.z < 0.7)
737 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
738 if (trace_fraction < s + 0.01)
739 if (trace_plane_normal.z < 0.7)
742 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
743 if (trace_fraction > s)
744 PHYS_INPUT_BUTTON_JUMP(this) = true;
748 // avoiding dangers and obstacles
749 vector dst_ahead, dst_down;
750 makevectors(this.v_angle.y * '0 1 0');
751 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
752 dst_down = dst_ahead - '0 0 1500';
755 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
757 // Check head-banging against walls
758 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
760 PHYS_INPUT_BUTTON_JUMP(this) = true;
761 if(this.facingwalltime && time > this.facingwalltime)
763 this.ignoregoal = this.goalcurrent;
764 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
765 this.bot_strategytime = 0;
770 this.facingwalltime = time + 0.05;
775 this.facingwalltime = 0;
777 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
779 this.ignoregoal = NULL;
780 this.ignoregoaltime = 0;
784 // Check for water/slime/lava and dangerous edges
785 // (only when the bot is on the ground or jumping intentionally)
786 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
788 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
789 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
792 traceline(dst_ahead , dst_down, true, NULL);
793 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
794 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
795 if(trace_endpos.z < this.origin.z + this.mins.z)
797 s = pointcontents(trace_endpos + '0 0 1');
798 if (s != CONTENT_SOLID)
799 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
800 evadelava = normalize(this.velocity) * -1;
801 else if (s == CONTENT_SKY)
802 evadeobstacle = normalize(this.velocity) * -1;
803 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
804 this.goalcurrent.absmin, this.goalcurrent.absmax))
806 // if ain't a safe goal with "holes" (like the jumpad on soylent)
807 // and there is a trigger_hurt below
808 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
810 // Remove dangerous dynamic goals from stack
811 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
812 navigation_clearroute(this);
822 makevectors(this.v_angle.y * '0 1 0');
824 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
825 this.aistatus |= AI_STATUS_DANGER_AHEAD;
828 dodge = havocbot_dodge(this);
829 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
830 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
831 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
832 if(IS_PLAYER(trace_ent))
833 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
835 dir = normalize(dir + dodge + evadeobstacle + evadelava);
836 // this.bot_dodgevector = dir;
837 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
840 if(time < this.ladder_time)
842 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
844 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
849 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
854 //dir = this.bot_dodgevector;
855 //if (this.bot_dodgevector_jumpbutton)
856 // PHYS_INPUT_BUTTON_JUMP(this) = true;
857 this.movement_x = dir * v_forward * maxspeed;
858 this.movement_y = dir * v_right * maxspeed;
859 this.movement_z = dir * v_up * maxspeed;
861 // Emulate keyboard interface
863 havocbot_keyboard_movement(this, destorg);
866 // if(this.aistatus & AI_STATUS_ROAMING)
868 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
869 havocbot_bunnyhop(this, dir);
871 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
872 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;
873 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);
876 entity havocbot_gettarget(entity this, bool secondary)
879 vector eye = CENTER_OR_VIEWOFS(this);
880 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
882 vector v = CENTER_OR_VIEWOFS(it);
883 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
884 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
885 if(bot_shouldattack(this, it))
887 traceline(eye, v, true, this);
888 if (trace_ent == it || trace_fraction >= 1)
896 void havocbot_chooseenemy(entity this)
898 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
905 if (!bot_shouldattack(this, this.enemy))
907 // enemy died or something, find a new target
909 this.havocbot_chooseenemy_finished = time;
911 else if (this.havocbot_stickenemy)
913 // tracking last chosen enemy
914 // if enemy is visible
915 // and not really really far away
916 // and we're not severely injured
917 // then keep tracking for a half second into the future
918 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
919 if (trace_ent == this.enemy || trace_fraction == 1)
920 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
921 if (this.health > 30)
923 // remain tracking him for a shot while (case he went after a small corner or pilar
924 this.havocbot_chooseenemy_finished = time + 0.5;
927 // enemy isn't visible, or is far away, or we're injured severely
928 // so stop preferring this enemy
929 // (it will still take a half second until a new one is chosen)
930 this.havocbot_stickenemy = 0;
933 if (time < this.havocbot_chooseenemy_finished)
935 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
936 vector eye = this.origin + this.view_ofs;
938 float bestrating = 100000000;
941 int hf = this.dphitcontentsmask;
943 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
945 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
947 bool scan_transparent = false;
948 bool scan_secondary_targets = false;
949 bool have_secondary_targets = false;
952 scan_secondary_targets = false;
954 IL_EACH(g_bot_targets, it.bot_attack,
956 if(!scan_secondary_targets)
958 if(it.classname == "misc_breakablemodel")
960 have_secondary_targets = true;
964 else if(it.classname != "misc_breakablemodel")
967 vector v = (it.absmin + it.absmax) * 0.5;
968 float rating = vlen2(v - eye);
969 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
970 if (bestrating > rating)
971 if (bot_shouldattack(this, it))
973 traceline(eye, v, true, this);
974 if (trace_ent == it || trace_fraction >= 1)
982 if(!best && have_secondary_targets && !scan_secondary_targets)
984 scan_secondary_targets = true;
986 bestrating = 100000000;
990 // I want to do a second scan if no enemy was found or I don't have weapons
991 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
992 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
997 // Set flags to see through transparent objects
998 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1000 scan_transparent = true;
1003 // Restore hit flags
1004 this.dphitcontentsmask = hf;
1007 this.havocbot_stickenemy = true;
1008 if(best && best.classname == "misc_breakablemodel")
1009 this.havocbot_stickenemy = false;
1012 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1014 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1015 // so skip this for them, or they'll never get to reload their weapons at all.
1016 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1020 // if this weapon is scheduled for reloading, don't switch to it during combat
1021 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1023 bool other_weapon_available = false;
1024 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1025 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1026 other_weapon_available = true;
1028 if(other_weapon_available)
1035 void havocbot_chooseweapon(entity this, .entity weaponentity)
1040 if(g_weaponarena_weapons == WEPSET(TUBA))
1042 this.(weaponentity).m_switchweapon = WEP_TUBA;
1046 // TODO: clean this up by moving it to weapon code
1047 if(this.enemy==NULL)
1049 // If no weapon was chosen get the first available weapon
1050 if(this.(weaponentity).m_weapon==WEP_Null)
1051 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1052 if(client_hasweapon(this, it, weaponentity, true, false))
1054 this.(weaponentity).m_switchweapon = it;
1061 // Do not change weapon during the next second after a combo
1062 float f = time - this.lastcombotime;
1067 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1069 // Should it do a weapon combo?
1070 float af, ct, combo_time, combo;
1072 af = ATTACK_FINISHED(this, 0);
1073 ct = autocvar_bot_ai_weapon_combo_threshold;
1075 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1076 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1077 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1081 if(autocvar_bot_ai_weapon_combo)
1082 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1086 this.lastcombotime = time;
1089 distance *= pow(2, this.bot_rangepreference);
1091 // Custom weapon list based on distance to the enemy
1092 if(bot_custom_weapon){
1094 // Choose weapons for far distance
1095 if ( distance > bot_distance_far ) {
1096 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1097 w = bot_weapons_far[i];
1098 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1100 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1102 this.(weaponentity).m_switchweapon = Weapons_from(w);
1108 // Choose weapons for mid distance
1109 if ( distance > bot_distance_close) {
1110 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1111 w = bot_weapons_mid[i];
1112 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1114 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1116 this.(weaponentity).m_switchweapon = Weapons_from(w);
1122 // Choose weapons for close distance
1123 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1124 w = bot_weapons_close[i];
1125 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1127 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1129 this.(weaponentity).m_switchweapon = Weapons_from(w);
1136 void havocbot_aim(entity this)
1138 vector myvel, enemyvel;
1139 // if(this.flags & FL_INWATER)
1141 if (time < this.nextaim)
1143 this.nextaim = time + 0.1;
1144 myvel = this.velocity;
1145 if (!this.waterlevel)
1149 enemyvel = this.enemy.velocity;
1150 if (!this.enemy.waterlevel)
1152 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1155 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1158 bool havocbot_moveto_refresh_route(entity this)
1160 // Refresh path to goal if necessary
1162 wp = this.havocbot_personal_waypoint;
1163 navigation_goalrating_start(this);
1164 navigation_routerating(this, wp, 10000, 10000);
1165 navigation_goalrating_end(this);
1166 return this.navigation_hasgoals;
1169 float havocbot_moveto(entity this, vector pos)
1173 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1175 // Step 4: Move to waypoint
1176 if(this.havocbot_personal_waypoint==NULL)
1178 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1179 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1180 return CMD_STATUS_ERROR;
1183 if (!bot_strategytoken_taken)
1184 if(this.havocbot_personal_waypoint_searchtime<time)
1186 bot_strategytoken_taken = true;
1187 if(havocbot_moveto_refresh_route(this))
1189 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1190 this.havocbot_personal_waypoint_searchtime = time + 10;
1191 this.havocbot_personal_waypoint_failcounter = 0;
1195 this.havocbot_personal_waypoint_failcounter += 1;
1196 this.havocbot_personal_waypoint_searchtime = time + 2;
1197 if(this.havocbot_personal_waypoint_failcounter >= 30)
1199 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1200 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1201 delete(this.havocbot_personal_waypoint);
1202 return CMD_STATUS_ERROR;
1205 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1209 if(autocvar_bot_debug_goalstack)
1210 debuggoalstack(this);
1213 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1215 bot_aimdir(this, dir, -1);
1218 havocbot_movetogoal(this);
1220 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1222 // Step 5: Waypoint reached
1223 LOG_TRACE(this.netname, "'s personal waypoint reached");
1224 delete(this.havocbot_personal_waypoint);
1225 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1226 return CMD_STATUS_FINISHED;
1229 return CMD_STATUS_EXECUTING;
1232 // Step 2: Linking waypoint
1233 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1235 // Wait until it is linked
1236 if(!this.havocbot_personal_waypoint.wplinked)
1238 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1239 return CMD_STATUS_EXECUTING;
1242 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1243 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1244 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1246 // Step 3: Route to waypoint
1247 LOG_TRACE(this.netname, " walking to its personal waypoint");
1249 return CMD_STATUS_EXECUTING;
1252 // Step 1: Spawning waypoint
1253 wp = waypoint_spawnpersonal(this, pos);
1256 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1257 return CMD_STATUS_ERROR;
1260 this.havocbot_personal_waypoint = wp;
1261 this.havocbot_personal_waypoint_failcounter = 0;
1262 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1264 // if pos is inside a teleport, then let's mark it as teleport waypoint
1265 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1267 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1268 this.lastteleporttime = 0;
1272 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1273 print("routing to a teleporter\n");
1275 print("routing to a non-teleporter\n");
1278 return CMD_STATUS_EXECUTING;
1281 float havocbot_resetgoal(entity this)
1283 navigation_clearroute(this);
1284 return CMD_STATUS_FINISHED;
1287 void havocbot_setupbot(entity this)
1289 this.bot_ai = havocbot_ai;
1290 this.cmd_moveto = havocbot_moveto;
1291 this.cmd_resetgoal = havocbot_resetgoal;
1293 havocbot_chooserole(this);
1296 vector havocbot_dodge(entity this)
1298 // LordHavoc: disabled because this is too expensive
1303 float danger, bestdanger, vl, d;
1306 // check for dangerous objects near bot or approaching bot
1307 head = findchainfloat(bot_dodge, true);
1310 if (head.owner != this)
1312 vl = vlen(head.velocity);
1313 if (vl > autocvar_sv_maxspeed * 0.3)
1315 n = normalize(head.velocity);
1316 v = this.origin - head.origin;
1318 if (d > (0 - head.bot_dodgerating))
1319 if (d < (vl * 0.2 + head.bot_dodgerating))
1321 // calculate direction and distance from the flight path, by removing the forward axis
1322 v = v - (n * (v * n));
1323 danger = head.bot_dodgerating - vlen(v);
1324 if (bestdanger < danger)
1326 bestdanger = danger;
1327 // dodge to the side of the object
1328 dodge = normalize(v);
1334 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1335 if (bestdanger < danger)
1337 bestdanger = danger;
1338 dodge = normalize(this.origin - head.origin);