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 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
50 if(!(IS_DEAD(this) || STAT(FROZEN, this)))
52 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
54 // Look for the closest waypoint out of water
55 entity newgoal = NULL;
56 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
58 if(it.origin.z < this.origin.z)
61 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
64 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
67 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
69 if(trace_fraction < 1)
72 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
78 // te_wizspike(newgoal.origin);
79 navigation_pushroute(this, newgoal);
83 // token has been used this frame
84 bot_strategytoken_taken = true;
87 if(IS_DEAD(this) || STAT(FROZEN, this))
90 havocbot_chooseenemy(this);
92 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
94 .entity weaponentity = weaponentities[slot];
95 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
96 if(this.(weaponentity).bot_chooseweapontime < time)
98 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
99 havocbot_chooseweapon(this, weaponentity);
104 if (this.bot_aimtarg)
106 this.aistatus |= AI_STATUS_ATTACKING;
107 this.aistatus &= ~AI_STATUS_ROAMING;
111 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
113 PHYS_INPUT_BUTTON_ATCK(this) = false;
114 PHYS_INPUT_BUTTON_ATCK2(this) = false;
118 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
120 .entity weaponentity = weaponentities[slot];
121 Weapon w = this.(weaponentity).m_weapon;
122 if(w == WEP_Null && slot != 0)
124 w.wr_aim(w, this, weaponentity);
125 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
126 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
132 if(IS_PLAYER(this.bot_aimtarg))
133 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
136 else if (this.goalcurrent)
138 this.aistatus |= AI_STATUS_ROAMING;
139 this.aistatus &= ~AI_STATUS_ATTACKING;
141 vector v = '0 0 0', now, next;
142 float aimdistance,skillblend,distanceblend,blend;
144 SET_DESTCOORDS(this.goalcurrent, this.origin, v);
145 if(this.goalcurrent.wpisbox)
147 // avoid a glitch when bot is teleported but teleport waypoint isn't removed yet
148 if(this.goalstack02 && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT
149 && this.lastteleporttime > 0 && time - this.lastteleporttime < 0.15)
150 v = (this.goalstack02.absmin + this.goalstack02.absmax) * 0.5;
151 // aim to teleport origin if bot is inside teleport waypoint but hasn't touched the real teleport yet
152 else if(boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin, this.origin))
153 v = this.goalcurrent.origin;
155 next = now = v - (this.origin + this.view_ofs);
156 aimdistance = vlen(now);
158 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
160 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
161 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
163 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
165 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
166 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
167 blend = skillblend * (1-distanceblend);
168 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
169 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
170 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
171 v = now + blend * (next - now);
172 //dprint(etos(this), " ");
173 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
174 //v = now * (distanceblend) + next * (1-distanceblend);
175 if (this.waterlevel < WATERLEVEL_SWIMMING)
177 //dprint("walk at:", vtos(v), "\n");
178 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
179 bot_aimdir(this, v, -1);
181 havocbot_movetogoal(this);
183 // if the bot is not attacking, consider reloading weapons
184 if (!(this.aistatus & AI_STATUS_ATTACKING))
186 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
188 .entity weaponentity = weaponentities[slot];
190 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
193 // we are currently holding a weapon that's not fully loaded, reload it
194 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
195 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
196 CS(this).impulse = IMP_weapon_reload.impulse; // not sure if this is done right
198 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
199 // the code above executes next frame, starting the reloading then
200 if(skill >= 5) // bots can only look for unloaded weapons past this skill
201 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
203 FOREACH(Weapons, it != WEP_Null, {
204 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
206 this.(weaponentity).m_switchweapon = it;
215 void havocbot_keyboard_movement(entity this, vector destorg)
219 if (time > this.havocbot_keyboardtime)
221 float sk = skill + this.bot_moveskill;
222 this.havocbot_keyboardtime =
224 this.havocbot_keyboardtime
225 + 0.05 / max(1, sk + this.havocbot_keyboardskill)
226 + random() * 0.025 / max(0.00025, skill + this.havocbot_keyboardskill)
228 keyboard = CS(this).movement / autocvar_sv_maxspeed;
230 float trigger = autocvar_bot_ai_keyboard_threshold;
231 float trigger1 = -trigger;
233 // categorize forward movement
234 // at skill < 1.5 only forward
235 // at skill < 2.5 only individual directions
236 // at skill < 4.5 only individual directions, and forward diagonals
237 // at skill >= 4.5, all cases allowed
238 if (keyboard.x > trigger)
244 else if (keyboard.x < trigger1 && sk > 1.5)
259 if (keyboard.y > trigger)
261 else if (keyboard.y < trigger1)
266 if (keyboard.z > trigger)
268 else if (keyboard.z < trigger1)
273 this.havocbot_keyboard = keyboard * autocvar_sv_maxspeed;
274 if (this.havocbot_ducktime > time)
275 PHYS_INPUT_BUTTON_CROUCH(this) = true;
278 keyboard = this.havocbot_keyboard;
279 float blend = bound(0, vlen(destorg - this.origin) / autocvar_bot_ai_keyboard_distance, 1); // When getting close move with 360 degree
280 //dprint("movement ", vtos(CS(this).movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
281 CS(this).movement = CS(this).movement + (keyboard - CS(this).movement) * blend;
284 void havocbot_bunnyhop(entity this, vector dir)
286 float bunnyhopdistance;
289 vector gco = '0 0 0', gno;
291 // Don't jump when attacking
292 if(this.aistatus & AI_STATUS_ATTACKING)
295 if(IS_PLAYER(this.goalcurrent))
298 maxspeed = autocvar_sv_maxspeed;
300 if(this.aistatus & AI_STATUS_RUNNING && vdist(this.velocity, <, autocvar_sv_maxspeed * 0.75)
301 || this.aistatus & AI_STATUS_DANGER_AHEAD)
303 this.aistatus &= ~AI_STATUS_RUNNING;
304 PHYS_INPUT_BUTTON_JUMP(this) = false;
305 this.bot_canruntogoal = 0;
306 this.bot_timelastseengoal = 0;
310 if(this.waterlevel > WATERLEVEL_WETFEET)
312 this.aistatus &= ~AI_STATUS_RUNNING;
316 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
318 this.bot_canruntogoal = 0;
319 this.bot_timelastseengoal = 0;
322 SET_DESTCOORDS(this.goalcurrent, this.origin, gco);
323 bunnyhopdistance = vlen(this.origin - gco);
325 // Run only to visible goals
326 if(IS_ONGROUND(this))
327 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
328 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
330 this.bot_lastseengoal = this.goalcurrent;
333 if(this.bot_timelastseengoal)
335 // for a period of time
336 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
339 checkdistance = true;
341 // don't run if it is too close
342 if(this.bot_canruntogoal==0)
344 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
345 this.bot_canruntogoal = 1;
347 this.bot_canruntogoal = -1;
350 if(this.bot_canruntogoal != 1)
353 if(this.aistatus & AI_STATUS_ROAMING)
354 if(this.goalcurrent.classname=="waypoint")
355 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
356 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
357 if(this.goalstack01 && !wasfreed(this.goalstack01))
359 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
360 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
361 while (deviation.y < -180) deviation.y = deviation.y + 360;
362 while (deviation.y > 180) deviation.y = deviation.y - 360;
364 if(fabs(deviation.y) < 20)
365 if(bunnyhopdistance < vlen(this.origin - gno))
366 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
368 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
369 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
371 checkdistance = false;
378 this.aistatus &= ~AI_STATUS_RUNNING;
379 // increase stop distance in case the goal is on a slope or a lower platform
380 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
381 PHYS_INPUT_BUTTON_JUMP(this) = true;
385 this.aistatus |= AI_STATUS_RUNNING;
386 PHYS_INPUT_BUTTON_JUMP(this) = true;
392 this.bot_timelastseengoal = time;
397 this.bot_timelastseengoal = 0;
401 // Release jump button
402 if(!cvar("sv_pogostick"))
403 if((IS_ONGROUND(this)) == 0)
405 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
406 PHYS_INPUT_BUTTON_JUMP(this) = false;
409 if(this.aistatus & AI_STATUS_RUNNING)
410 if(vlen(this.velocity)>maxspeed)
412 deviation = vectoangles(dir) - vectoangles(this.velocity);
413 while (deviation.y < -180) deviation.y = deviation.y + 360;
414 while (deviation.y > 180) deviation.y = deviation.y - 360;
416 if(fabs(deviation.y)>10)
417 CS(this).movement_x = 0;
420 CS(this).movement_y = maxspeed * -1;
421 else if(deviation.y<10)
422 CS(this).movement_y = maxspeed;
429 // return true when bot isn't getting closer to the current goal
430 bool havocbot_checkgoaldistance(entity this, vector gco)
432 float curr_dist = vlen(this.origin - gco);
433 float curr_dist_2d = vlen(vec2(this.origin - gco));
434 float distance_time = this.goalcurrent_distance_time;
435 if(distance_time < 0)
436 distance_time = -distance_time;
437 if(curr_dist > this.goalcurrent_distance && curr_dist_2d > this.goalcurrent_distance_2d)
440 this.goalcurrent_distance_time = time;
441 else if (time - distance_time > 0.5)
446 // reduce it a little bit so it works even with very small approaches to the goal
447 this.goalcurrent_distance = max(20, curr_dist - 10);
448 this.goalcurrent_distance_2d = max(20, curr_dist_2d - 10);
449 this.goalcurrent_distance_time = 0;
454 void havocbot_movetogoal(entity this)
456 vector destorg = '0 0 0';
460 vector evadeobstacle;
466 //if (this.goalentity)
467 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
468 CS(this).movement = '0 0 0';
469 maxspeed = autocvar_sv_maxspeed;
471 PHYS_INPUT_BUTTON_JETPACK(this) = false;
472 // Jetpack navigation
474 if(this.navigation_jetpack_goal)
475 if(this.goalcurrent==this.navigation_jetpack_goal)
478 if(autocvar_bot_debug_goalstack)
480 debuggoalstack(this);
481 te_wizspike(this.navigation_jetpack_point);
485 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
487 // Brake almost completely so it can get a good direction
488 if(vdist(this.velocity, >, 10))
490 this.aistatus |= AI_STATUS_JETPACK_FLYING;
493 makevectors(this.v_angle.y * '0 1 0');
494 dir = normalize(this.navigation_jetpack_point - this.origin);
497 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
499 // Calculate brake distance in xy
503 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
505 v = vlen(this.velocity - this.velocity.z * '0 0 1');
506 db = ((v ** 2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
507 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
508 if(d < db || d < 500)
511 if(fabs(this.velocity.x)>maxspeed*0.3)
513 CS(this).movement_x = dir * v_forward * -maxspeed;
516 // Switch to normal mode
517 this.navigation_jetpack_goal = NULL;
518 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
519 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
523 else if(checkpvs(this.origin,this.goalcurrent))
525 // If I can see the goal switch to landing code
526 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
527 this.aistatus |= AI_STATUS_JETPACK_LANDING;
532 PHYS_INPUT_BUTTON_JETPACK(this) = true;
533 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
535 CS(this).movement_x = dir * v_forward * maxspeed;
536 CS(this).movement_y = dir * v_right * maxspeed;
541 // Handling of jump pads
542 if(this.jumppadcount)
544 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
546 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
547 navigation_poptouchedgoals(this);
550 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
552 // If got stuck on the jump pad try to reach the farthest visible waypoint
553 // but with some randomness so it can try out different paths
554 if(!this.goalcurrent)
556 entity newgoal = NULL;
557 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
559 if(it.wpflags & WAYPOINTFLAG_TELEPORT)
560 if(it.origin.z < this.origin.z - 100 && vdist(vec2(it.origin - this.origin), <, 100))
563 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
565 if(trace_fraction < 1)
568 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
574 this.ignoregoal = this.goalcurrent;
575 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
576 navigation_clearroute(this);
577 navigation_routetogoal(this, newgoal, this.origin);
578 if(autocvar_bot_debug_goalstack)
579 debuggoalstack(this);
580 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
585 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
586 if (this.origin.z > gco.z && vdist(vec2(this.velocity), <, autocvar_sv_maxspeed))
587 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
588 else if(havocbot_checkgoaldistance(this, gco))
590 navigation_clearroute(this);
591 navigation_goalrating_timeout_force(this);
599 if(time - this.lastteleporttime > 0.2 && this.velocity.z > 0)
601 vector velxy = this.velocity; velxy_z = 0;
602 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
604 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
605 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
610 // Don't chase players while using a jump pad
611 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
615 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
616 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
618 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
619 if (skill > 6 && !(IS_ONGROUND(this)))
621 #define ROCKETJUMP_DAMAGE() WEP_CVAR(devastator, damage) * 0.8 \
622 * ((this.strength_finished < time) ? autocvar_g_balance_powerup_strength_selfdamage : 1) \
623 * ((this.invincible_finished < time) ? autocvar_g_balance_powerup_invincible_takedamage : 1)
625 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
626 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
627 if(this.items & IT_JETPACK)
629 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
630 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
632 if(this.velocity.z<0)
633 PHYS_INPUT_BUTTON_JETPACK(this) = true;
636 PHYS_INPUT_BUTTON_JETPACK(this) = true;
638 // If there is no goal try to move forward
640 if(this.goalcurrent==NULL)
643 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
645 vector xyvelocity = this.velocity; xyvelocity_z = 0;
646 float xyspeed = xyvelocity * dir;
648 if(xyspeed < (maxspeed / 2))
650 makevectors(this.v_angle.y * '0 1 0');
651 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
652 if(trace_fraction==1)
654 CS(this).movement_x = dir * v_forward * maxspeed;
655 CS(this).movement_y = dir * v_right * maxspeed;
657 havocbot_keyboard_movement(this, this.origin + dir * 100);
661 this.havocbot_blockhead = true;
665 else if(this.health + this.armorvalue > ROCKETJUMP_DAMAGE())
667 if(this.velocity.z < 0)
669 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
671 .entity weaponentity = weaponentities[slot];
673 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
676 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
678 CS(this).movement_x = maxspeed;
680 if(this.rocketjumptime)
682 if(time > this.rocketjumptime)
684 PHYS_INPUT_BUTTON_ATCK2(this) = true;
685 this.rocketjumptime = 0;
690 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
692 PHYS_INPUT_BUTTON_ATCK(this) = true;
693 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
701 // If there is no goal try to move forward
702 if(this.goalcurrent==NULL)
703 CS(this).movement_x = maxspeed;
707 // If we are under water with no goals, swim up
709 if(this.goalcurrent==NULL)
712 if(this.waterlevel>WATERLEVEL_SWIMMING)
714 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
715 PHYS_INPUT_BUTTON_JUMP(this) = true;
717 PHYS_INPUT_BUTTON_JUMP(this) = false;
718 makevectors(this.v_angle.y * '0 1 0');
719 CS(this).movement_x = dir * v_forward * maxspeed;
720 CS(this).movement_y = dir * v_right * maxspeed;
721 CS(this).movement_z = dir * v_up * maxspeed;
724 // if there is nowhere to go, exit
725 if (this.goalcurrent == NULL)
729 bool locked_goal = false;
730 if(this.goalentity && wasfreed(this.goalentity))
732 navigation_clearroute(this);
733 navigation_goalrating_timeout_force(this);
736 else if(this.goalentity.bot_pickup)
738 if(this.goalentity.bot_pickup_respawning)
740 if(this.goalentity.solid) // item respawned
741 this.goalentity.bot_pickup_respawning = false;
742 else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
744 if(checkpvs(this.origin, this.goalentity))
746 this.goalentity.bot_pickup_respawning = false;
747 navigation_clearroute(this);
748 navigation_goalrating_timeout_force(this);
752 else if(this.goalentity == this.goalcurrent)
753 locked_goal = true; // wait for item to respawn
755 else if(!this.goalentity.solid && !boxesoverlap(this.goalentity.absmin, this.goalentity.absmax, this.absmin, this.absmax))
757 if(checkpvs(this.origin, this.goalentity))
759 navigation_clearroute(this);
760 navigation_goalrating_timeout_force(this);
767 if(navigation_poptouchedgoals(this) && time > this.bot_strategytime - (IS_MOVABLE(this.goalentity) ? 3 : 2))
768 navigation_goalrating_timeout_force(this);
771 // if ran out of goals try to use an alternative goal or get a new strategy asap
772 if(this.goalcurrent == NULL)
774 navigation_goalrating_timeout_force(this);
779 if(autocvar_bot_debug_goalstack)
780 debuggoalstack(this);
782 bool bunnyhop_forbidden = false;;
783 SET_DESTCOORDS(this.goalcurrent, this.origin, destorg);
785 // in case bot ends up inside the teleport waypoint without touching
786 // the teleport itself, head to the teleport origin
787 if(this.goalcurrent.wpisbox && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin + eZ * this.mins.z, this.origin + eZ * this.maxs.z))
789 bunnyhop_forbidden = true;
790 destorg = this.goalcurrent.origin;
791 if(destorg.z > this.origin.z)
792 PHYS_INPUT_BUTTON_JUMP(this) = true;
795 diff = destorg - this.origin;
797 dir = normalize(diff);
798 flatdir = diff;flatdir.z = 0;
799 flatdir = normalize(flatdir);
800 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
802 //if (this.bot_dodgevector_time < time)
804 //this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
805 //this.bot_dodgevector_jumpbutton = 1;
806 evadeobstacle = '0 0 0';
809 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
810 makevectors(this.v_angle.y * '0 1 0');
813 if(this.waterlevel>WATERLEVEL_SWIMMING)
815 if(!this.goalcurrent)
816 this.aistatus |= AI_STATUS_OUT_WATER;
817 else if(gco.z > this.origin.z)
818 PHYS_INPUT_BUTTON_JUMP(this) = true;
823 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
824 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
825 PHYS_INPUT_BUTTON_JUMP(this) = true;
827 PHYS_INPUT_BUTTON_JUMP(this) = false;
834 if(this.aistatus & AI_STATUS_OUT_WATER)
835 this.aistatus &= ~AI_STATUS_OUT_WATER;
837 // jump if going toward an obstacle that doesn't look like stairs we
838 // can walk up directly
839 offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
840 tracebox(this.origin, this.mins, this.maxs, this.origin + offset, false, this);
841 if (trace_fraction < 1)
842 if (trace_plane_normal.z < 0.7)
845 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + offset + stepheightvec, false, this);
846 if (trace_fraction < s + 0.01)
847 if (trace_plane_normal.z < 0.7)
850 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + offset + jumpstepheightvec, false, this);
851 if (trace_fraction > s)
852 PHYS_INPUT_BUTTON_JUMP(this) = true;
856 // if bot for some reason doesn't get close to the current goal find another one
857 if(!this.jumppadcount && !IS_PLAYER(this.goalcurrent) && !(this.goalcurrent.bot_pickup_respawning && this.goalcurrent_distance < 50))
858 if(havocbot_checkgoaldistance(this, gco))
860 if(this.goalcurrent_distance_time < 0) // can't get close for the second time
862 navigation_clearroute(this);
863 navigation_goalrating_timeout_force(this);
867 vector dest = '0 0 0';
868 float dest_height = 0;
869 SET_TRACEWALK_DESTCOORDS(this.goalcurrent, this.origin, dest, dest_height);
870 if (!tracewalk(this, this.origin, this.mins, this.maxs, dest, dest_height, bot_navigation_movemode))
872 navigation_clearroute(this);
873 navigation_goalrating_timeout_force(this);
877 // give bot only another chance to prevent bot getting stuck
878 // in case it thinks it can walk but actually can't
879 this.goalcurrent_distance = FLOAT_MAX;
880 this.goalcurrent_distance_2d = FLOAT_MAX;
881 this.goalcurrent_distance_time = -time; // mark second try
884 // Check for water/slime/lava and dangerous edges
885 // (only when the bot is on the ground or jumping intentionally)
887 vector dst_ahead = this.origin + this.view_ofs + offset;
888 vector dst_down = dst_ahead - '0 0 3000';
889 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
891 bool unreachable = false;
893 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
894 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
897 traceline(dst_ahead , dst_down, true, NULL);
898 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
899 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
900 if(trace_endpos.z < this.origin.z + this.mins.z)
902 s = pointcontents(trace_endpos + '0 0 1');
903 if (s != CONTENT_SOLID)
904 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
905 evadelava = normalize(this.velocity) * -1;
906 else if (s == CONTENT_SKY)
907 evadeobstacle = normalize(this.velocity) * -1;
908 else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
910 // the traceline check isn't enough but is good as optimization,
911 // when not true (most of the time) this tracebox call is avoided
912 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
913 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
915 if (gco.z > this.origin.z + jumpstepheightvec.z)
917 // the goal is probably on an upper platform, assume bot can't get there
921 evadelava = normalize(this.velocity) * -1;
930 makevectors(this.v_angle.y * '0 1 0');
932 if(evadeobstacle || evadelava || (s == CONTENT_WATER))
934 this.aistatus |= AI_STATUS_DANGER_AHEAD;
935 if(IS_PLAYER(this.goalcurrent))
940 navigation_clearroute(this);
941 navigation_goalrating_timeout_force(this);
945 dodge = havocbot_dodge(this);
946 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
947 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
948 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
949 if(IS_PLAYER(trace_ent))
950 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
952 dir = normalize(dir + dodge + evadeobstacle + evadelava);
953 // this.bot_dodgevector = dir;
954 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
957 if(time < this.ladder_time)
959 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
961 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
966 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
971 //dir = this.bot_dodgevector;
972 //if (this.bot_dodgevector_jumpbutton)
973 // PHYS_INPUT_BUTTON_JUMP(this) = true;
974 CS(this).movement_x = dir * v_forward * maxspeed;
975 CS(this).movement_y = dir * v_right * maxspeed;
976 CS(this).movement_z = dir * v_up * maxspeed;
978 // Emulate keyboard interface
980 havocbot_keyboard_movement(this, destorg);
983 //if(this.aistatus & AI_STATUS_ROAMING)
984 if(!bunnyhop_forbidden && this.goalcurrent)
985 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
986 havocbot_bunnyhop(this, dir);
988 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
989 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;
990 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);
993 entity havocbot_gettarget(entity this, bool secondary)
996 vector eye = CENTER_OR_VIEWOFS(this);
997 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
999 vector v = CENTER_OR_VIEWOFS(it);
1000 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1001 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
1002 if(bot_shouldattack(this, it))
1004 traceline(eye, v, true, this);
1005 if (trace_ent == it || trace_fraction >= 1)
1013 void havocbot_chooseenemy(entity this)
1015 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
1022 if (!bot_shouldattack(this, this.enemy))
1024 // enemy died or something, find a new target
1026 this.havocbot_chooseenemy_finished = time;
1028 else if (this.havocbot_stickenemy)
1030 // tracking last chosen enemy
1031 // if enemy is visible
1032 // and not really really far away
1033 // and we're not severely injured
1034 // then keep tracking for a half second into the future
1035 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
1036 if (trace_ent == this.enemy || trace_fraction == 1)
1037 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
1038 if (this.health > 30)
1040 // remain tracking him for a shot while (case he went after a small corner or pilar
1041 this.havocbot_chooseenemy_finished = time + 0.5;
1044 // enemy isn't visible, or is far away, or we're injured severely
1045 // so stop preferring this enemy
1046 // (it will still take a half second until a new one is chosen)
1047 this.havocbot_stickenemy = 0;
1050 if (time < this.havocbot_chooseenemy_finished)
1052 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
1053 vector eye = this.origin + this.view_ofs;
1055 float bestrating = 100000000;
1058 int hf = this.dphitcontentsmask;
1060 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1062 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1064 bool scan_transparent = false;
1065 bool scan_secondary_targets = false;
1066 bool have_secondary_targets = false;
1069 scan_secondary_targets = false;
1071 IL_EACH(g_bot_targets, it.bot_attack,
1073 if(!scan_secondary_targets)
1075 if(it.classname == "misc_breakablemodel")
1077 have_secondary_targets = true;
1081 else if(it.classname != "misc_breakablemodel")
1084 vector v = (it.absmin + it.absmax) * 0.5;
1085 float rating = vlen2(v - eye);
1086 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1087 if (bestrating > rating)
1088 if (bot_shouldattack(this, it))
1090 traceline(eye, v, true, this);
1091 if (trace_ent == it || trace_fraction >= 1)
1094 bestrating = rating;
1099 if(!best && have_secondary_targets && !scan_secondary_targets)
1101 scan_secondary_targets = true;
1103 bestrating = 100000000;
1107 // I want to do a second scan if no enemy was found or I don't have weapons
1108 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1109 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1111 if(scan_transparent)
1114 // Set flags to see through transparent objects
1115 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1117 scan_transparent = true;
1120 // Restore hit flags
1121 this.dphitcontentsmask = hf;
1124 this.havocbot_stickenemy = true;
1125 if(best && best.classname == "misc_breakablemodel")
1126 this.havocbot_stickenemy = false;
1129 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1131 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1132 // so skip this for them, or they'll never get to reload their weapons at all.
1133 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1137 // if this weapon is scheduled for reloading, don't switch to it during combat
1138 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1140 FOREACH(Weapons, it != WEP_Null, {
1141 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1142 return true; // other weapon available
1149 void havocbot_chooseweapon(entity this, .entity weaponentity)
1154 if(g_weaponarena_weapons == WEPSET(TUBA))
1156 this.(weaponentity).m_switchweapon = WEP_TUBA;
1160 // TODO: clean this up by moving it to weapon code
1161 if(this.enemy==NULL)
1163 // If no weapon was chosen get the first available weapon
1164 if(this.(weaponentity).m_weapon==WEP_Null)
1165 FOREACH(Weapons, it != WEP_Null, {
1166 if(client_hasweapon(this, it, weaponentity, true, false))
1168 this.(weaponentity).m_switchweapon = it;
1175 // Do not change weapon during the next second after a combo
1176 float f = time - this.lastcombotime;
1181 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1183 // Should it do a weapon combo?
1184 float af, ct, combo_time, combo;
1186 af = ATTACK_FINISHED(this, 0);
1187 ct = autocvar_bot_ai_weapon_combo_threshold;
1189 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1190 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1191 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1195 if(autocvar_bot_ai_weapon_combo)
1196 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1200 this.lastcombotime = time;
1203 distance *= (2 ** this.bot_rangepreference);
1205 // Custom weapon list based on distance to the enemy
1206 if(bot_custom_weapon){
1208 // Choose weapons for far distance
1209 if ( distance > bot_distance_far ) {
1210 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1211 w = bot_weapons_far[i];
1212 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1214 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1216 this.(weaponentity).m_switchweapon = Weapons_from(w);
1222 // Choose weapons for mid distance
1223 if ( distance > bot_distance_close) {
1224 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1225 w = bot_weapons_mid[i];
1226 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1228 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1230 this.(weaponentity).m_switchweapon = Weapons_from(w);
1236 // Choose weapons for close distance
1237 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1238 w = bot_weapons_close[i];
1239 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1241 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1243 this.(weaponentity).m_switchweapon = Weapons_from(w);
1250 void havocbot_aim(entity this)
1252 if (time < this.nextaim)
1254 this.nextaim = time + 0.1;
1255 vector myvel = this.velocity;
1256 if (!this.waterlevel)
1258 if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1259 else if (this.enemy)
1261 vector enemyvel = this.enemy.velocity;
1262 if (!this.enemy.waterlevel)
1264 lag_additem(this, time + CS(this).ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1267 lag_additem(this, time + CS(this).ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1270 bool havocbot_moveto_refresh_route(entity this)
1272 // Refresh path to goal if necessary
1274 wp = this.havocbot_personal_waypoint;
1275 navigation_goalrating_start(this);
1276 navigation_routerating(this, wp, 10000, 10000);
1277 navigation_goalrating_end(this);
1278 return (this.goalentity != NULL);
1281 float havocbot_moveto(entity this, vector pos)
1285 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1287 // Step 4: Move to waypoint
1288 if(this.havocbot_personal_waypoint==NULL)
1290 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1291 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1292 return CMD_STATUS_ERROR;
1295 if (!bot_strategytoken_taken)
1296 if(this.havocbot_personal_waypoint_searchtime<time)
1298 bot_strategytoken_taken = true;
1299 if(havocbot_moveto_refresh_route(this))
1301 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1302 this.havocbot_personal_waypoint_searchtime = time + 10;
1303 this.havocbot_personal_waypoint_failcounter = 0;
1307 this.havocbot_personal_waypoint_failcounter += 1;
1308 this.havocbot_personal_waypoint_searchtime = time + 2;
1309 if(this.havocbot_personal_waypoint_failcounter >= 30)
1311 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1312 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1313 delete(this.havocbot_personal_waypoint);
1314 return CMD_STATUS_ERROR;
1317 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1321 if(autocvar_bot_debug_goalstack)
1322 debuggoalstack(this);
1325 vector dir = '0 0 0';
1326 SET_DESTCOORDS(this.goalcurrent, this.origin, dir);
1327 dir = dir - (this.origin + this.view_ofs);
1329 bot_aimdir(this, dir, -1);
1332 havocbot_movetogoal(this);
1334 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1336 // Step 5: Waypoint reached
1337 LOG_TRACE(this.netname, "'s personal waypoint reached");
1338 delete(this.havocbot_personal_waypoint);
1339 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1340 return CMD_STATUS_FINISHED;
1343 return CMD_STATUS_EXECUTING;
1346 // Step 2: Linking waypoint
1347 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1349 // Wait until it is linked
1350 if(!this.havocbot_personal_waypoint.wplinked)
1352 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1353 return CMD_STATUS_EXECUTING;
1356 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1357 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1358 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1360 // Step 3: Route to waypoint
1361 LOG_TRACE(this.netname, " walking to its personal waypoint");
1363 return CMD_STATUS_EXECUTING;
1366 // Step 1: Spawning waypoint
1367 wp = waypoint_spawnpersonal(this, pos);
1370 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1371 return CMD_STATUS_ERROR;
1374 this.havocbot_personal_waypoint = wp;
1375 this.havocbot_personal_waypoint_failcounter = 0;
1376 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1378 // if pos is inside a teleport, then let's mark it as teleport waypoint
1379 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1381 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1382 this.lastteleporttime = 0;
1386 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1387 print("routing to a teleporter\n");
1389 print("routing to a non-teleporter\n");
1392 return CMD_STATUS_EXECUTING;
1395 float havocbot_resetgoal(entity this)
1397 navigation_clearroute(this);
1398 return CMD_STATUS_FINISHED;
1401 void havocbot_setupbot(entity this)
1403 this.bot_ai = havocbot_ai;
1404 this.cmd_moveto = havocbot_moveto;
1405 this.cmd_resetgoal = havocbot_resetgoal;
1407 havocbot_chooserole(this);
1410 vector havocbot_dodge(entity this)
1412 // LordHavoc: disabled because this is too expensive
1417 float danger, bestdanger, vl, d;
1420 // check for dangerous objects near bot or approaching bot
1421 head = findchainfloat(bot_dodge, true);
1424 if (head.owner != this)
1426 vl = vlen(head.velocity);
1427 if (vl > autocvar_sv_maxspeed * 0.3)
1429 n = normalize(head.velocity);
1430 v = this.origin - head.origin;
1432 if (d > (0 - head.bot_dodgerating))
1433 if (d < (vl * 0.2 + head.bot_dodgerating))
1435 // calculate direction and distance from the flight path, by removing the forward axis
1436 v = v - (n * (v * n));
1437 danger = head.bot_dodgerating - vlen(v);
1438 if (bestdanger < danger)
1440 bestdanger = danger;
1441 // dodge to the side of the object
1442 dodge = normalize(v);
1448 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1449 if (bestdanger < danger)
1451 bestdanger = danger;
1452 dodge = normalize(this.origin - head.origin);