3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
9 #include "../navigation.qh"
10 #include "../scripting.qh"
11 #include "../waypoints.qh"
13 #include <common/constants.qh>
14 #include <common/impulses/all.qh>
15 #include <common/net_linked.qh>
16 #include <common/physics/player.qh>
17 #include <common/state.qh>
18 #include <common/items/_mod.qh>
19 #include <common/wepent.qh>
21 #include <common/triggers/teleporters.qh>
22 #include <common/triggers/trigger/jumppads.qh>
24 #include <lib/warpzone/common.qh>
28 void havocbot_ai(entity this)
33 if(bot_execute_commands(this))
36 if (bot_strategytoken == this)
37 if (!bot_strategytoken_taken)
39 if(this.havocbot_blockhead)
41 this.havocbot_blockhead = false;
45 if (!this.jumppadcount && !STAT(FROZEN, this))
46 this.havocbot_role(this); // little too far down the rabbit hole
49 // TODO: tracewalk() should take care of this job (better path finding under water)
50 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
51 if(!(IS_DEAD(this) || STAT(FROZEN, this)))
53 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
55 // Look for the closest waypoint out of water
56 entity newgoal = NULL;
57 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
59 if(it.origin.z < this.origin.z)
62 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
65 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
68 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
70 if(trace_fraction < 1)
73 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
79 // te_wizspike(newgoal.origin);
80 navigation_pushroute(this, newgoal);
84 // token has been used this frame
85 bot_strategytoken_taken = true;
88 if(IS_DEAD(this) || STAT(FROZEN, this))
91 havocbot_chooseenemy(this);
93 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
95 .entity weaponentity = weaponentities[slot];
96 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
97 if(this.(weaponentity).bot_chooseweapontime < time)
99 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
100 havocbot_chooseweapon(this, weaponentity);
105 if (this.bot_aimtarg)
107 this.aistatus |= AI_STATUS_ATTACKING;
108 this.aistatus &= ~AI_STATUS_ROAMING;
112 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
114 PHYS_INPUT_BUTTON_ATCK(this) = false;
115 PHYS_INPUT_BUTTON_ATCK2(this) = false;
119 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
121 .entity weaponentity = weaponentities[slot];
122 Weapon w = this.(weaponentity).m_weapon;
123 if(w == WEP_Null && slot != 0)
125 w.wr_aim(w, this, weaponentity);
126 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
127 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
133 if(IS_PLAYER(this.bot_aimtarg))
134 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
137 else if (this.goalcurrent)
139 this.aistatus |= AI_STATUS_ROAMING;
140 this.aistatus &= ~AI_STATUS_ATTACKING;
142 vector now,v,next;//,heading;
143 float aimdistance,skillblend,distanceblend,blend;
144 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
145 aimdistance = vlen(now);
146 //heading = this.velocity;
147 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
149 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
150 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
152 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
154 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
155 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
156 blend = skillblend * (1-distanceblend);
157 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
158 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
159 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
160 v = now + blend * (next - now);
161 //dprint(etos(this), " ");
162 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
163 //v = now * (distanceblend) + next * (1-distanceblend);
164 if (this.waterlevel < WATERLEVEL_SWIMMING)
166 //dprint("walk at:", vtos(v), "\n");
167 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
168 bot_aimdir(this, v, -1);
170 havocbot_movetogoal(this);
172 // if the bot is not attacking, consider reloading weapons
173 if (!(this.aistatus & AI_STATUS_ATTACKING))
175 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
177 .entity weaponentity = weaponentities[slot];
179 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
182 // we are currently holding a weapon that's not fully loaded, reload it
183 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
184 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
185 CS(this).impulse = IMP_weapon_reload.impulse; // not sure if this is done right
187 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
188 // the code above executes next frame, starting the reloading then
189 if(skill >= 5) // bots can only look for unloaded weapons past this skill
190 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
192 FOREACH(Weapons, it != WEP_Null, {
193 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;
204 void havocbot_keyboard_movement(entity this, vector destorg)
208 if (time > this.havocbot_keyboardtime)
210 float sk = skill + this.bot_moveskill;
211 this.havocbot_keyboardtime =
213 this.havocbot_keyboardtime
214 + 0.05 / max(1, sk + this.havocbot_keyboardskill)
215 + random() * 0.025 / max(0.00025, skill + this.havocbot_keyboardskill)
217 keyboard = CS(this).movement / autocvar_sv_maxspeed;
219 float trigger = autocvar_bot_ai_keyboard_threshold;
220 float trigger1 = -trigger;
222 // categorize forward movement
223 // at skill < 1.5 only forward
224 // at skill < 2.5 only individual directions
225 // at skill < 4.5 only individual directions, and forward diagonals
226 // at skill >= 4.5, all cases allowed
227 if (keyboard.x > trigger)
233 else if (keyboard.x < trigger1 && sk > 1.5)
248 if (keyboard.y > trigger)
250 else if (keyboard.y < trigger1)
255 if (keyboard.z > trigger)
257 else if (keyboard.z < trigger1)
262 this.havocbot_keyboard = keyboard * autocvar_sv_maxspeed;
263 if (this.havocbot_ducktime > time)
264 PHYS_INPUT_BUTTON_CROUCH(this) = true;
267 keyboard = this.havocbot_keyboard;
268 float blend = bound(0, vlen(destorg - this.origin) / autocvar_bot_ai_keyboard_distance, 1); // When getting close move with 360 degree
269 //dprint("movement ", vtos(CS(this).movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
270 CS(this).movement = CS(this).movement + (keyboard - CS(this).movement) * blend;
273 void havocbot_bunnyhop(entity this, vector dir)
275 float bunnyhopdistance;
280 // Don't jump when attacking
281 if(this.aistatus & AI_STATUS_ATTACKING)
284 if(IS_PLAYER(this.goalcurrent))
287 maxspeed = autocvar_sv_maxspeed;
289 if(this.aistatus & AI_STATUS_RUNNING && vdist(this.velocity, <, autocvar_sv_maxspeed * 0.75)
290 || this.aistatus & AI_STATUS_DANGER_AHEAD)
292 this.aistatus &= ~AI_STATUS_RUNNING;
293 PHYS_INPUT_BUTTON_JUMP(this) = false;
294 this.bot_canruntogoal = 0;
295 this.bot_timelastseengoal = 0;
299 if(this.waterlevel > WATERLEVEL_WETFEET)
301 this.aistatus &= ~AI_STATUS_RUNNING;
305 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
307 this.bot_canruntogoal = 0;
308 this.bot_timelastseengoal = 0;
311 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
312 bunnyhopdistance = vlen(this.origin - gco);
314 // Run only to visible goals
315 if(IS_ONGROUND(this))
316 if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
317 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
319 this.bot_lastseengoal = this.goalcurrent;
322 if(this.bot_timelastseengoal)
324 // for a period of time
325 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
328 checkdistance = true;
330 // don't run if it is too close
331 if(this.bot_canruntogoal==0)
333 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
334 this.bot_canruntogoal = 1;
336 this.bot_canruntogoal = -1;
339 if(this.bot_canruntogoal != 1)
342 if(this.aistatus & AI_STATUS_ROAMING)
343 if(this.goalcurrent.classname=="waypoint")
344 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
345 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
346 if(this.goalstack01 && !wasfreed(this.goalstack01))
348 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
349 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
350 while (deviation.y < -180) deviation.y = deviation.y + 360;
351 while (deviation.y > 180) deviation.y = deviation.y - 360;
353 if(fabs(deviation.y) < 20)
354 if(bunnyhopdistance < vlen(this.origin - gno))
355 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
357 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
358 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
360 checkdistance = false;
367 this.aistatus &= ~AI_STATUS_RUNNING;
368 // increase stop distance in case the goal is on a slope or a lower platform
369 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
370 PHYS_INPUT_BUTTON_JUMP(this) = true;
374 this.aistatus |= AI_STATUS_RUNNING;
375 PHYS_INPUT_BUTTON_JUMP(this) = true;
381 this.bot_timelastseengoal = time;
386 this.bot_timelastseengoal = 0;
390 // Release jump button
391 if(!cvar("sv_pogostick"))
392 if((IS_ONGROUND(this)) == 0)
394 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
395 PHYS_INPUT_BUTTON_JUMP(this) = false;
398 if(this.aistatus & AI_STATUS_RUNNING)
399 if(vlen(this.velocity)>maxspeed)
401 deviation = vectoangles(dir) - vectoangles(this.velocity);
402 while (deviation.y < -180) deviation.y = deviation.y + 360;
403 while (deviation.y > 180) deviation.y = deviation.y - 360;
405 if(fabs(deviation.y)>10)
406 CS(this).movement_x = 0;
409 CS(this).movement_y = maxspeed * -1;
410 else if(deviation.y<10)
411 CS(this).movement_y = maxspeed;
418 .entity goalcurrent_prev;
419 .float goalcurrent_distance;
420 .float goalcurrent_distance_time;
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 CS(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 = ((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 CS(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 CS(this).movement_x = dir * v_forward * maxspeed;
504 CS(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 if (vdist(this.origin - this.goalcurrent.origin, <, 150))
520 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
521 else IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
523 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
525 if(trace_fraction < 1)
528 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
534 this.ignoregoal = this.goalcurrent;
535 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
536 navigation_clearroute(this);
537 navigation_routetogoal(this, newgoal, this.origin);
538 if(autocvar_bot_debug_goalstack)
539 debuggoalstack(this);
540 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
548 if(time - this.lastteleporttime > 0.3 && this.velocity.z > 0)
550 vector velxy = this.velocity; velxy_z = 0;
551 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
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 CS(this).movement_x = dir * v_forward * maxspeed;
603 CS(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 * ((this.strength_finished < time) ? autocvar_g_balance_powerup_strength_selfdamage : 1))
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 CS(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 CS(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 CS(this).movement_x = dir * v_forward * maxspeed;
668 CS(this).movement_y = dir * v_right * maxspeed;
669 CS(this).movement_z = dir * v_up * maxspeed;
672 // if there is nowhere to go, exit
673 if (this.goalcurrent == NULL)
677 bool locked_goal = false;
678 if(this.goalentity && wasfreed(this.goalentity))
680 navigation_clearroute(this);
681 this.bot_strategytime = 0;
684 else if(this.goalentity.bot_pickup)
686 if(this.goalentity.bot_pickup_respawning)
688 if(this.goalentity.solid) // item respawned
689 this.goalentity.bot_pickup_respawning = false;
690 else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
692 this.goalentity.bot_pickup_respawning = false;
693 navigation_clearroute(this);
694 this.bot_strategytime = 0;
697 else if(this.goalentity == this.goalcurrent)
698 locked_goal = true; // wait for item to respawn
700 else if(!this.goalentity.solid)
702 navigation_clearroute(this);
703 this.bot_strategytime = 0;
708 navigation_poptouchedgoals(this);
710 // if ran out of goals try to use an alternative goal or get a new strategy asap
711 if(this.goalcurrent == NULL)
713 this.bot_strategytime = 0;
718 if(autocvar_bot_debug_goalstack)
719 debuggoalstack(this);
721 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
722 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
723 destorg = this.origin;
724 destorg.x = bound(m1_x, destorg.x, m2_x);
725 destorg.y = bound(m1_y, destorg.y, m2_y);
726 destorg.z = bound(m1_z, destorg.z, m2_z);
727 diff = destorg - this.origin;
729 dir = normalize(diff);
730 flatdir = diff;flatdir.z = 0;
731 flatdir = normalize(flatdir);
732 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
734 //if (this.bot_dodgevector_time < time)
736 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
737 // this.bot_dodgevector_jumpbutton = 1;
738 evadeobstacle = '0 0 0';
741 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
742 makevectors(this.v_angle.y * '0 1 0');
745 if(this.waterlevel>WATERLEVEL_SWIMMING)
748 this.aistatus |= AI_STATUS_OUT_WATER;
752 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
753 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
754 PHYS_INPUT_BUTTON_JUMP(this) = true;
756 PHYS_INPUT_BUTTON_JUMP(this) = false;
758 dir = normalize(flatdir);
764 if(this.aistatus & AI_STATUS_OUT_WATER)
765 this.aistatus &= ~AI_STATUS_OUT_WATER;
767 // jump if going toward an obstacle that doesn't look like stairs we
768 // can walk up directly
769 offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
770 tracebox(this.origin, this.mins, this.maxs, this.origin + offset, false, this);
771 if (trace_fraction < 1)
772 if (trace_plane_normal.z < 0.7)
775 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + offset + stepheightvec, false, this);
776 if (trace_fraction < s + 0.01)
777 if (trace_plane_normal.z < 0.7)
780 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + offset + jumpstepheightvec, false, this);
781 if (trace_fraction > s)
782 PHYS_INPUT_BUTTON_JUMP(this) = true;
786 // if bot for some reason doesn't get close to the current goal find another one
787 if(!IS_PLAYER(this.goalcurrent) && !(this.goalcurrent.bot_pickup_respawning && this.goalcurrent_distance < 50))
789 float curr_dist = vlen(this.origin - this.goalcurrent.origin);
790 if(this.goalcurrent != this.goalcurrent_prev)
792 this.goalcurrent_prev = this.goalcurrent;
793 this.goalcurrent_distance = curr_dist;
794 this.goalcurrent_distance_time = 0;
796 else if(curr_dist > this.goalcurrent_distance)
798 if(!this.goalcurrent_distance_time)
799 this.goalcurrent_distance_time = time;
800 else if (time - this.goalcurrent_distance_time > 0.5)
802 this.goalcurrent_prev = NULL;
803 navigation_clearroute(this);
804 this.bot_strategytime = 0;
810 // reduce it a little bit so it works even with very small approaches to the goal
811 this.goalcurrent_distance = max(20, curr_dist - 15);
812 this.goalcurrent_distance_time = 0;
816 // Check for water/slime/lava and dangerous edges
817 // (only when the bot is on the ground or jumping intentionally)
819 vector dst_ahead = this.origin + this.view_ofs + offset;
820 vector dst_down = dst_ahead - '0 0 3000';
821 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
823 bool unreachable = false;
824 bool ignorehazards = false;
826 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
827 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
830 traceline(dst_ahead , dst_down, true, NULL);
831 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
832 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
833 if(trace_endpos.z < this.origin.z + this.mins.z)
835 s = pointcontents(trace_endpos + '0 0 1');
836 if (s != CONTENT_SOLID)
837 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
839 evadelava = normalize(this.velocity) * -1;
840 if(this.waterlevel >= WATERLEVEL_WETFEET && (this.watertype == CONTENT_LAVA || this.watertype == CONTENT_SLIME))
841 ignorehazards = true;
843 else if (s == CONTENT_WATER)
845 if(this.waterlevel >= WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER)
846 ignorehazards = true;
848 else if (s == CONTENT_SKY)
849 evadeobstacle = normalize(this.velocity) * -1;
850 else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
852 // the traceline check isn't enough but is good as optimization,
853 // when not true (most of the time) this tracebox call is avoided
854 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
855 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
857 if (gco.z > this.origin.z + jumpstepheightvec.z)
859 // the goal is probably on an upper platform, assume bot can't get there
863 evadelava = normalize(this.velocity) * -1;
872 makevectors(this.v_angle.y * '0 1 0');
874 if(evadeobstacle || evadelava || (s == CONTENT_WATER))
877 this.aistatus |= AI_STATUS_DANGER_AHEAD;
878 if(IS_PLAYER(this.goalcurrent))
883 navigation_clearroute(this);
884 this.bot_strategytime = 0;
888 dodge = havocbot_dodge(this);
889 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
890 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
891 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
892 if(IS_PLAYER(trace_ent))
893 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
895 dir = normalize(dir + dodge + evadeobstacle + evadelava);
896 // this.bot_dodgevector = dir;
897 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
900 if(time < this.ladder_time)
902 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
904 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
909 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
914 //dir = this.bot_dodgevector;
915 //if (this.bot_dodgevector_jumpbutton)
916 // PHYS_INPUT_BUTTON_JUMP(this) = true;
917 CS(this).movement_x = dir * v_forward * maxspeed;
918 CS(this).movement_y = dir * v_right * maxspeed;
919 CS(this).movement_z = dir * v_up * maxspeed;
921 // Emulate keyboard interface
923 havocbot_keyboard_movement(this, destorg);
926 // if(this.aistatus & AI_STATUS_ROAMING)
928 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
929 havocbot_bunnyhop(this, dir);
931 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
932 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;
933 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);
936 entity havocbot_gettarget(entity this, bool secondary)
939 vector eye = CENTER_OR_VIEWOFS(this);
940 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
942 vector v = CENTER_OR_VIEWOFS(it);
943 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
944 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
945 if(bot_shouldattack(this, it))
947 traceline(eye, v, true, this);
948 if (trace_ent == it || trace_fraction >= 1)
956 void havocbot_chooseenemy(entity this)
958 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
965 if (!bot_shouldattack(this, this.enemy))
967 // enemy died or something, find a new target
969 this.havocbot_chooseenemy_finished = time;
971 else if (this.havocbot_stickenemy)
973 // tracking last chosen enemy
974 // if enemy is visible
975 // and not really really far away
976 // and we're not severely injured
977 // then keep tracking for a half second into the future
978 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
979 if (trace_ent == this.enemy || trace_fraction == 1)
980 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
981 if (this.health > 30)
983 // remain tracking him for a shot while (case he went after a small corner or pilar
984 this.havocbot_chooseenemy_finished = time + 0.5;
987 // enemy isn't visible, or is far away, or we're injured severely
988 // so stop preferring this enemy
989 // (it will still take a half second until a new one is chosen)
990 this.havocbot_stickenemy = 0;
993 if (time < this.havocbot_chooseenemy_finished)
995 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
996 vector eye = this.origin + this.view_ofs;
998 float bestrating = 100000000;
1001 int hf = this.dphitcontentsmask;
1003 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1005 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1007 bool scan_transparent = false;
1008 bool scan_secondary_targets = false;
1009 bool have_secondary_targets = false;
1012 scan_secondary_targets = false;
1014 IL_EACH(g_bot_targets, it.bot_attack,
1016 if(!scan_secondary_targets)
1018 if(it.classname == "misc_breakablemodel")
1020 have_secondary_targets = true;
1024 else if(it.classname != "misc_breakablemodel")
1027 vector v = (it.absmin + it.absmax) * 0.5;
1028 float rating = vlen2(v - eye);
1029 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1030 if (bestrating > rating)
1031 if (bot_shouldattack(this, it))
1033 traceline(eye, v, true, this);
1034 if (trace_ent == it || trace_fraction >= 1)
1037 bestrating = rating;
1042 if(!best && have_secondary_targets && !scan_secondary_targets)
1044 scan_secondary_targets = true;
1046 bestrating = 100000000;
1050 // I want to do a second scan if no enemy was found or I don't have weapons
1051 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1052 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1054 if(scan_transparent)
1057 // Set flags to see through transparent objects
1058 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1060 scan_transparent = true;
1063 // Restore hit flags
1064 this.dphitcontentsmask = hf;
1067 this.havocbot_stickenemy = true;
1068 if(best && best.classname == "misc_breakablemodel")
1069 this.havocbot_stickenemy = false;
1072 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1074 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1075 // so skip this for them, or they'll never get to reload their weapons at all.
1076 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1080 // if this weapon is scheduled for reloading, don't switch to it during combat
1081 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1083 FOREACH(Weapons, it != WEP_Null, {
1084 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1085 return true; // other weapon available
1092 void havocbot_chooseweapon(entity this, .entity weaponentity)
1097 if(g_weaponarena_weapons == WEPSET(TUBA))
1099 this.(weaponentity).m_switchweapon = WEP_TUBA;
1103 // TODO: clean this up by moving it to weapon code
1104 if(this.enemy==NULL)
1106 // If no weapon was chosen get the first available weapon
1107 if(this.(weaponentity).m_weapon==WEP_Null)
1108 FOREACH(Weapons, it != WEP_Null, {
1109 if(client_hasweapon(this, it, weaponentity, true, false))
1111 this.(weaponentity).m_switchweapon = it;
1118 // Do not change weapon during the next second after a combo
1119 float f = time - this.lastcombotime;
1124 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1126 // Should it do a weapon combo?
1127 float af, ct, combo_time, combo;
1129 af = ATTACK_FINISHED(this, 0);
1130 ct = autocvar_bot_ai_weapon_combo_threshold;
1132 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1133 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1134 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1138 if(autocvar_bot_ai_weapon_combo)
1139 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1143 this.lastcombotime = time;
1146 distance *= (2 ** this.bot_rangepreference);
1148 // Custom weapon list based on distance to the enemy
1149 if(bot_custom_weapon){
1151 // Choose weapons for far distance
1152 if ( distance > bot_distance_far ) {
1153 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1154 w = bot_weapons_far[i];
1155 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1157 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1159 this.(weaponentity).m_switchweapon = Weapons_from(w);
1165 // Choose weapons for mid distance
1166 if ( distance > bot_distance_close) {
1167 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1168 w = bot_weapons_mid[i];
1169 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1171 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1173 this.(weaponentity).m_switchweapon = Weapons_from(w);
1179 // Choose weapons for close distance
1180 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1181 w = bot_weapons_close[i];
1182 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1184 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1186 this.(weaponentity).m_switchweapon = Weapons_from(w);
1193 void havocbot_aim(entity this)
1195 if (time < this.nextaim)
1197 this.nextaim = time + 0.1;
1198 vector myvel = this.velocity;
1199 if (!this.waterlevel)
1201 if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1202 else if (this.enemy)
1204 vector enemyvel = this.enemy.velocity;
1205 if (!this.enemy.waterlevel)
1207 lag_additem(this, time + CS(this).ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1210 lag_additem(this, time + CS(this).ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1213 bool havocbot_moveto_refresh_route(entity this)
1215 // Refresh path to goal if necessary
1217 wp = this.havocbot_personal_waypoint;
1218 navigation_goalrating_start(this);
1219 navigation_routerating(this, wp, 10000, 10000);
1220 navigation_goalrating_end(this);
1221 return (this.goalentity != NULL);
1224 float havocbot_moveto(entity this, vector pos)
1228 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1230 // Step 4: Move to waypoint
1231 if(this.havocbot_personal_waypoint==NULL)
1233 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1234 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1235 return CMD_STATUS_ERROR;
1238 if (!bot_strategytoken_taken)
1239 if(this.havocbot_personal_waypoint_searchtime<time)
1241 bot_strategytoken_taken = true;
1242 if(havocbot_moveto_refresh_route(this))
1244 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1245 this.havocbot_personal_waypoint_searchtime = time + 10;
1246 this.havocbot_personal_waypoint_failcounter = 0;
1250 this.havocbot_personal_waypoint_failcounter += 1;
1251 this.havocbot_personal_waypoint_searchtime = time + 2;
1252 if(this.havocbot_personal_waypoint_failcounter >= 30)
1254 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1255 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1256 delete(this.havocbot_personal_waypoint);
1257 return CMD_STATUS_ERROR;
1260 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1264 if(autocvar_bot_debug_goalstack)
1265 debuggoalstack(this);
1268 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1270 bot_aimdir(this, dir, -1);
1273 havocbot_movetogoal(this);
1275 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1277 // Step 5: Waypoint reached
1278 LOG_TRACE(this.netname, "'s personal waypoint reached");
1279 delete(this.havocbot_personal_waypoint);
1280 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1281 return CMD_STATUS_FINISHED;
1284 return CMD_STATUS_EXECUTING;
1287 // Step 2: Linking waypoint
1288 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1290 // Wait until it is linked
1291 if(!this.havocbot_personal_waypoint.wplinked)
1293 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1294 return CMD_STATUS_EXECUTING;
1297 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1298 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1299 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1301 // Step 3: Route to waypoint
1302 LOG_TRACE(this.netname, " walking to its personal waypoint");
1304 return CMD_STATUS_EXECUTING;
1307 // Step 1: Spawning waypoint
1308 wp = waypoint_spawnpersonal(this, pos);
1311 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1312 return CMD_STATUS_ERROR;
1315 this.havocbot_personal_waypoint = wp;
1316 this.havocbot_personal_waypoint_failcounter = 0;
1317 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1319 // if pos is inside a teleport, then let's mark it as teleport waypoint
1320 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1322 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1323 this.lastteleporttime = 0;
1327 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1328 print("routing to a teleporter\n");
1330 print("routing to a non-teleporter\n");
1333 return CMD_STATUS_EXECUTING;
1336 float havocbot_resetgoal(entity this)
1338 navigation_clearroute(this);
1339 return CMD_STATUS_FINISHED;
1342 void havocbot_setupbot(entity this)
1344 this.bot_ai = havocbot_ai;
1345 this.cmd_moveto = havocbot_moveto;
1346 this.cmd_resetgoal = havocbot_resetgoal;
1348 havocbot_chooserole(this);
1351 vector havocbot_dodge(entity this)
1353 // LordHavoc: disabled because this is too expensive
1358 float danger, bestdanger, vl, d;
1361 // check for dangerous objects near bot or approaching bot
1362 head = findchainfloat(bot_dodge, true);
1365 if (head.owner != this)
1367 vl = vlen(head.velocity);
1368 if (vl > autocvar_sv_maxspeed * 0.3)
1370 n = normalize(head.velocity);
1371 v = this.origin - head.origin;
1373 if (d > (0 - head.bot_dodgerating))
1374 if (d < (vl * 0.2 + head.bot_dodgerating))
1376 // calculate direction and distance from the flight path, by removing the forward axis
1377 v = v - (n * (v * n));
1378 danger = head.bot_dodgerating - vlen(v);
1379 if (bestdanger < danger)
1381 bestdanger = danger;
1382 // dodge to the side of the object
1383 dodge = normalize(v);
1389 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1390 if (bestdanger < danger)
1392 bestdanger = danger;
1393 dodge = normalize(this.origin - head.origin);