7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
11 #include <common/constants.qh>
12 #include <common/impulses/all.qh>
13 #include <common/net_linked.qh>
14 #include <common/physics/player.qh>
15 #include <common/state.qh>
16 #include <common/items/_mod.qh>
17 #include <common/wepent.qh>
19 #include <common/triggers/teleporters.qh>
20 #include <common/triggers/trigger/jumppads.qh>
22 #include <lib/warpzone/common.qh>
26 void havocbot_ai(entity this)
31 if(bot_execute_commands(this))
34 if (bot_strategytoken == this)
35 if (!bot_strategytoken_taken)
37 if(this.havocbot_blockhead)
39 this.havocbot_blockhead = false;
43 if (!this.jumppadcount && !STAT(FROZEN, this))
44 this.havocbot_role(this); // little too far down the rabbit hole
47 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
48 if(!(IS_DEAD(this) || STAT(FROZEN, this)))
50 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
52 // Look for the closest waypoint out of water
53 entity newgoal = NULL;
54 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
56 if(it.origin.z < this.origin.z)
59 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
62 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
65 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
67 if(trace_fraction < 1)
70 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
76 // te_wizspike(newgoal.origin);
77 navigation_pushroute(this, newgoal);
81 // token has been used this frame
82 bot_strategytoken_taken = true;
85 if(IS_DEAD(this) || STAT(FROZEN, this))
88 havocbot_chooseenemy(this);
90 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
92 .entity weaponentity = weaponentities[slot];
93 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
94 if(this.(weaponentity).bot_chooseweapontime < time)
96 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
97 havocbot_chooseweapon(this, weaponentity);
102 if (this.bot_aimtarg)
104 this.aistatus |= AI_STATUS_ATTACKING;
105 this.aistatus &= ~AI_STATUS_ROAMING;
109 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
111 PHYS_INPUT_BUTTON_ATCK(this) = false;
112 PHYS_INPUT_BUTTON_ATCK2(this) = false;
116 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
118 .entity weaponentity = weaponentities[slot];
119 Weapon w = this.(weaponentity).m_weapon;
120 if(w == WEP_Null && slot != 0)
122 w.wr_aim(w, this, weaponentity);
123 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
124 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
130 if(IS_PLAYER(this.bot_aimtarg))
131 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
134 else if (this.goalcurrent)
136 this.aistatus |= AI_STATUS_ROAMING;
137 this.aistatus &= ~AI_STATUS_ATTACKING;
140 float aimdistance,skillblend,distanceblend,blend;
142 SET_DESTCOORDS(this.goalcurrent, this.origin, v);
143 if(this.goalcurrent.wpisbox)
145 // avoid a glitch when bot is teleported but teleport waypoint isn't removed yet
146 if(this.goalstack02 && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT
147 && this.lastteleporttime > 0 && time - this.lastteleporttime < 0.15)
148 v = (this.goalstack02.absmin + this.goalstack02.absmax) * 0.5;
149 // aim to teleport origin if bot is inside teleport waypoint but hasn't touched the real teleport yet
150 else if(boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin, this.origin))
151 v = this.goalcurrent.origin;
153 next = now = v - (this.origin + this.view_ofs);
154 aimdistance = vlen(now);
156 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
158 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
159 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
161 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
163 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
164 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
165 blend = skillblend * (1-distanceblend);
166 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
167 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
168 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
169 v = now + blend * (next - now);
170 //dprint(etos(this), " ");
171 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
172 //v = now * (distanceblend) + next * (1-distanceblend);
173 if (this.waterlevel < WATERLEVEL_SWIMMING)
175 //dprint("walk at:", vtos(v), "\n");
176 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
177 bot_aimdir(this, v, -1);
179 havocbot_movetogoal(this);
181 // if the bot is not attacking, consider reloading weapons
182 if (!(this.aistatus & AI_STATUS_ATTACKING))
184 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
186 .entity weaponentity = weaponentities[slot];
188 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
191 // we are currently holding a weapon that's not fully loaded, reload it
192 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
193 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
194 CS(this).impulse = IMP_weapon_reload.impulse; // not sure if this is done right
196 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
197 // the code above executes next frame, starting the reloading then
198 if(skill >= 5) // bots can only look for unloaded weapons past this skill
199 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
201 FOREACH(Weapons, it != WEP_Null, LAMBDA(
202 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
204 this.(weaponentity).m_switchweapon = it;
213 void havocbot_keyboard_movement(entity this, vector destorg)
217 if (time > this.havocbot_keyboardtime)
219 float sk = skill + this.bot_moveskill;
220 this.havocbot_keyboardtime =
222 this.havocbot_keyboardtime
223 + 0.05 / max(1, sk + this.havocbot_keyboardskill)
224 + random() * 0.025 / max(0.00025, skill + this.havocbot_keyboardskill)
226 keyboard = CS(this).movement / autocvar_sv_maxspeed;
228 float trigger = autocvar_bot_ai_keyboard_threshold;
229 float trigger1 = -trigger;
231 // categorize forward movement
232 // at skill < 1.5 only forward
233 // at skill < 2.5 only individual directions
234 // at skill < 4.5 only individual directions, and forward diagonals
235 // at skill >= 4.5, all cases allowed
236 if (keyboard.x > trigger)
242 else if (keyboard.x < trigger1 && sk > 1.5)
257 if (keyboard.y > trigger)
259 else if (keyboard.y < trigger1)
264 if (keyboard.z > trigger)
266 else if (keyboard.z < trigger1)
271 this.havocbot_keyboard = keyboard * autocvar_sv_maxspeed;
272 if (this.havocbot_ducktime > time)
273 PHYS_INPUT_BUTTON_CROUCH(this) = true;
276 keyboard = this.havocbot_keyboard;
277 float blend = bound(0, vlen(destorg - this.origin) / autocvar_bot_ai_keyboard_distance, 1); // When getting close move with 360 degree
278 //dprint("movement ", vtos(CS(this).movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
279 CS(this).movement = CS(this).movement + (keyboard - CS(this).movement) * blend;
282 void havocbot_bunnyhop(entity this, vector dir)
284 float bunnyhopdistance;
289 // Don't jump when attacking
290 if(this.aistatus & AI_STATUS_ATTACKING)
293 if(IS_PLAYER(this.goalcurrent))
296 maxspeed = autocvar_sv_maxspeed;
298 if(this.aistatus & AI_STATUS_RUNNING && vdist(this.velocity, <, autocvar_sv_maxspeed * 0.75)
299 || this.aistatus & AI_STATUS_DANGER_AHEAD)
301 this.aistatus &= ~AI_STATUS_RUNNING;
302 PHYS_INPUT_BUTTON_JUMP(this) = false;
303 this.bot_canruntogoal = 0;
304 this.bot_timelastseengoal = 0;
308 if(this.waterlevel > WATERLEVEL_WETFEET)
310 this.aistatus &= ~AI_STATUS_RUNNING;
314 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
316 this.bot_canruntogoal = 0;
317 this.bot_timelastseengoal = 0;
320 SET_DESTCOORDS(this.goalcurrent, this.origin, gco);
321 bunnyhopdistance = vlen(this.origin - gco);
323 // Run only to visible goals
324 if(IS_ONGROUND(this))
325 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
326 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
328 this.bot_lastseengoal = this.goalcurrent;
331 if(this.bot_timelastseengoal)
333 // for a period of time
334 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
337 checkdistance = true;
339 // don't run if it is too close
340 if(this.bot_canruntogoal==0)
342 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
343 this.bot_canruntogoal = 1;
345 this.bot_canruntogoal = -1;
348 if(this.bot_canruntogoal != 1)
351 if(this.aistatus & AI_STATUS_ROAMING)
352 if(this.goalcurrent.classname=="waypoint")
353 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
354 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
355 if(this.goalstack01 && !wasfreed(this.goalstack01))
357 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
358 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
359 while (deviation.y < -180) deviation.y = deviation.y + 360;
360 while (deviation.y > 180) deviation.y = deviation.y - 360;
362 if(fabs(deviation.y) < 20)
363 if(bunnyhopdistance < vlen(this.origin - gno))
364 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
366 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
367 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
369 checkdistance = false;
376 this.aistatus &= ~AI_STATUS_RUNNING;
377 // increase stop distance in case the goal is on a slope or a lower platform
378 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
379 PHYS_INPUT_BUTTON_JUMP(this) = true;
383 this.aistatus |= AI_STATUS_RUNNING;
384 PHYS_INPUT_BUTTON_JUMP(this) = true;
390 this.bot_timelastseengoal = time;
395 this.bot_timelastseengoal = 0;
399 // Release jump button
400 if(!cvar("sv_pogostick"))
401 if((IS_ONGROUND(this)) == 0)
403 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
404 PHYS_INPUT_BUTTON_JUMP(this) = false;
407 if(this.aistatus & AI_STATUS_RUNNING)
408 if(vlen(this.velocity)>maxspeed)
410 deviation = vectoangles(dir) - vectoangles(this.velocity);
411 while (deviation.y < -180) deviation.y = deviation.y + 360;
412 while (deviation.y > 180) deviation.y = deviation.y - 360;
414 if(fabs(deviation.y)>10)
415 CS(this).movement_x = 0;
418 CS(this).movement_y = maxspeed * -1;
419 else if(deviation.y<10)
420 CS(this).movement_y = maxspeed;
427 // return true when bot isn't getting closer to the current goal
428 bool havocbot_checkgoaldistance(entity this, vector gco)
430 float curr_dist = vlen(this.origin - gco);
431 if(curr_dist > this.goalcurrent_distance)
433 if(!this.goalcurrent_distance_time)
434 this.goalcurrent_distance_time = time;
435 else if (time - this.goalcurrent_distance_time > 0.5)
440 // reduce it a little bit so it works even with very small approaches to the goal
441 this.goalcurrent_distance = max(20, curr_dist - 15);
442 this.goalcurrent_distance_time = 0;
447 void havocbot_movetogoal(entity this)
453 vector evadeobstacle;
459 //if (this.goalentity)
460 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
461 CS(this).movement = '0 0 0';
462 maxspeed = autocvar_sv_maxspeed;
464 // Jetpack navigation
466 if(this.navigation_jetpack_goal)
467 if(this.goalcurrent==this.navigation_jetpack_goal)
470 if(autocvar_bot_debug_goalstack)
472 debuggoalstack(this);
473 te_wizspike(this.navigation_jetpack_point);
477 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
479 // Brake almost completely so it can get a good direction
480 if(vdist(this.velocity, >, 10))
482 this.aistatus |= AI_STATUS_JETPACK_FLYING;
485 makevectors(this.v_angle.y * '0 1 0');
486 dir = normalize(this.navigation_jetpack_point - this.origin);
489 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
491 // Calculate brake distance in xy
495 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
497 v = vlen(this.velocity - this.velocity.z * '0 0 1');
498 db = ((v ** 2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
499 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
500 if(d < db || d < 500)
503 if(fabs(this.velocity.x)>maxspeed*0.3)
505 CS(this).movement_x = dir * v_forward * -maxspeed;
508 // Switch to normal mode
509 this.navigation_jetpack_goal = NULL;
510 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
511 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
515 else if(checkpvs(this.origin,this.goalcurrent))
517 // If I can see the goal switch to landing code
518 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
519 this.aistatus |= AI_STATUS_JETPACK_LANDING;
524 PHYS_INPUT_BUTTON_HOOK(this) = true;
525 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
527 CS(this).movement_x = dir * v_forward * maxspeed;
528 CS(this).movement_y = dir * v_right * maxspeed;
533 // Handling of jump pads
534 if(this.jumppadcount)
536 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
538 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
539 navigation_poptouchedgoals(this);
542 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
544 // If got stuck on the jump pad try to reach the farthest visible waypoint
545 // but with some randomness so it can try out different paths
546 if(!this.goalcurrent)
548 entity newgoal = NULL;
549 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
551 if(it.wpflags & WAYPOINTFLAG_TELEPORT)
552 if(it.origin.z < this.origin.z - 100 && vdist(vec2(it.origin - this.origin), <, 100))
555 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
557 if(trace_fraction < 1)
560 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
566 this.ignoregoal = this.goalcurrent;
567 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
568 navigation_clearroute(this);
569 navigation_routetogoal(this, newgoal, this.origin);
570 if(autocvar_bot_debug_goalstack)
571 debuggoalstack(this);
572 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
577 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
578 if (this.origin.z > gco.z && vdist(vec2(this.velocity), <, autocvar_sv_maxspeed))
579 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
580 else if(havocbot_checkgoaldistance(this, gco))
582 navigation_clearroute(this);
583 this.bot_strategytime = 0;
591 if(time - this.lastteleporttime > 0.2 && this.velocity.z > 0)
593 vector velxy = this.velocity; velxy_z = 0;
594 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
596 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
597 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
602 // Don't chase players while using a jump pad
603 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
607 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
608 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
610 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
612 if (!(IS_ONGROUND(this)))
614 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
615 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
616 if(this.items & IT_JETPACK)
618 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
619 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
621 if(this.velocity.z<0)
623 PHYS_INPUT_BUTTON_HOOK(this) = true;
627 PHYS_INPUT_BUTTON_HOOK(this) = true;
629 // If there is no goal try to move forward
631 if(this.goalcurrent==NULL)
634 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
636 vector xyvelocity = this.velocity; xyvelocity_z = 0;
637 float xyspeed = xyvelocity * dir;
639 if(xyspeed < (maxspeed / 2))
641 makevectors(this.v_angle.y * '0 1 0');
642 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
643 if(trace_fraction==1)
645 CS(this).movement_x = dir * v_forward * maxspeed;
646 CS(this).movement_y = dir * v_right * maxspeed;
648 havocbot_keyboard_movement(this, this.origin + dir * 100);
652 this.havocbot_blockhead = true;
656 else if(this.health > WEP_CVAR(devastator, damage) * 0.5 * ((this.strength_finished < time) ? autocvar_g_balance_powerup_strength_selfdamage : 1))
658 if(this.velocity.z < 0)
660 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
662 .entity weaponentity = weaponentities[slot];
664 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
667 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
669 CS(this).movement_x = maxspeed;
671 if(this.rocketjumptime)
673 if(time > this.rocketjumptime)
675 PHYS_INPUT_BUTTON_ATCK2(this) = true;
676 this.rocketjumptime = 0;
681 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
683 PHYS_INPUT_BUTTON_ATCK(this) = true;
684 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
692 // If there is no goal try to move forward
693 if(this.goalcurrent==NULL)
694 CS(this).movement_x = maxspeed;
698 // If we are under water with no goals, swim up
700 if(this.goalcurrent==NULL)
703 if(this.waterlevel>WATERLEVEL_SWIMMING)
705 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
706 PHYS_INPUT_BUTTON_JUMP(this) = true;
708 PHYS_INPUT_BUTTON_JUMP(this) = false;
709 makevectors(this.v_angle.y * '0 1 0');
710 CS(this).movement_x = dir * v_forward * maxspeed;
711 CS(this).movement_y = dir * v_right * maxspeed;
712 CS(this).movement_z = dir * v_up * maxspeed;
715 // if there is nowhere to go, exit
716 if (this.goalcurrent == NULL)
720 bool locked_goal = false;
721 if(this.goalentity && wasfreed(this.goalentity))
723 navigation_clearroute(this);
724 this.bot_strategytime = 0;
727 else if(this.goalentity.bot_pickup)
729 if(this.goalentity.bot_pickup_respawning)
731 if(this.goalentity.solid) // item respawned
732 this.goalentity.bot_pickup_respawning = false;
733 else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
735 this.goalentity.bot_pickup_respawning = false;
736 navigation_clearroute(this);
737 this.bot_strategytime = 0;
740 else if(this.goalentity == this.goalcurrent)
741 locked_goal = true; // wait for item to respawn
743 else if(!this.goalentity.solid)
745 navigation_clearroute(this);
746 this.bot_strategytime = 0;
751 navigation_poptouchedgoals(this);
753 // if ran out of goals try to use an alternative goal or get a new strategy asap
754 if(this.goalcurrent == NULL)
756 this.bot_strategytime = 0;
761 if(autocvar_bot_debug_goalstack)
762 debuggoalstack(this);
764 bool bunnyhop_forbidden = false;;
765 SET_DESTCOORDS(this.goalcurrent, this.origin, destorg);
767 // in case bot ends up inside the teleport waypoint without touching
768 // the teleport itself, head to the teleport origin
769 if(this.goalcurrent.wpisbox && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin + eZ * this.mins.z, this.origin + eZ * this.maxs.z))
771 bunnyhop_forbidden = true;
772 destorg = this.goalcurrent.origin;
773 if(destorg.z > this.origin.z)
774 PHYS_INPUT_BUTTON_JUMP(this) = true;
777 diff = destorg - this.origin;
779 dir = normalize(diff);
780 flatdir = diff;flatdir.z = 0;
781 flatdir = normalize(flatdir);
782 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
784 //if (this.bot_dodgevector_time < time)
786 //this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
787 //this.bot_dodgevector_jumpbutton = 1;
788 evadeobstacle = '0 0 0';
791 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
792 makevectors(this.v_angle.y * '0 1 0');
795 if(this.waterlevel>WATERLEVEL_SWIMMING)
797 if(!this.goalcurrent)
798 this.aistatus |= AI_STATUS_OUT_WATER;
799 else if(gco.z > this.origin.z)
800 PHYS_INPUT_BUTTON_JUMP(this) = true;
805 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
806 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
807 PHYS_INPUT_BUTTON_JUMP(this) = true;
809 PHYS_INPUT_BUTTON_JUMP(this) = false;
816 if(this.aistatus & AI_STATUS_OUT_WATER)
817 this.aistatus &= ~AI_STATUS_OUT_WATER;
819 // jump if going toward an obstacle that doesn't look like stairs we
820 // can walk up directly
821 offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
822 tracebox(this.origin, this.mins, this.maxs, this.origin + offset, false, this);
823 if (trace_fraction < 1)
824 if (trace_plane_normal.z < 0.7)
827 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + offset + stepheightvec, false, this);
828 if (trace_fraction < s + 0.01)
829 if (trace_plane_normal.z < 0.7)
832 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + offset + jumpstepheightvec, false, this);
833 if (trace_fraction > s)
834 PHYS_INPUT_BUTTON_JUMP(this) = true;
838 // if bot for some reason doesn't get close to the current goal find another one
839 if(!this.jumppadcount && !IS_PLAYER(this.goalcurrent) && !(this.goalcurrent.bot_pickup_respawning && this.goalcurrent_distance < 50))
840 if(havocbot_checkgoaldistance(this, gco))
842 navigation_clearroute(this);
843 this.bot_strategytime = 0;
847 // Check for water/slime/lava and dangerous edges
848 // (only when the bot is on the ground or jumping intentionally)
850 vector dst_ahead = this.origin + this.view_ofs + offset;
851 vector dst_down = dst_ahead - '0 0 3000';
852 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
854 bool unreachable = false;
856 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
857 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
860 traceline(dst_ahead , dst_down, true, NULL);
861 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
862 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
863 if(trace_endpos.z < this.origin.z + this.mins.z)
865 s = pointcontents(trace_endpos + '0 0 1');
866 if (s != CONTENT_SOLID)
867 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
868 evadelava = normalize(this.velocity) * -1;
869 else if (s == CONTENT_SKY)
870 evadeobstacle = normalize(this.velocity) * -1;
871 else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
873 // the traceline check isn't enough but is good as optimization,
874 // when not true (most of the time) this tracebox call is avoided
875 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
876 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
878 if (gco.z > this.origin.z + jumpstepheightvec.z)
880 // the goal is probably on an upper platform, assume bot can't get there
884 evadelava = normalize(this.velocity) * -1;
893 makevectors(this.v_angle.y * '0 1 0');
895 if(evadeobstacle || evadelava || (s == CONTENT_WATER))
897 this.aistatus |= AI_STATUS_DANGER_AHEAD;
898 if(IS_PLAYER(this.goalcurrent))
903 navigation_clearroute(this);
904 this.bot_strategytime = 0;
908 dodge = havocbot_dodge(this);
909 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
910 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
911 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
912 if(IS_PLAYER(trace_ent))
913 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
915 dir = normalize(dir + dodge + evadeobstacle + evadelava);
916 // this.bot_dodgevector = dir;
917 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
920 if(time < this.ladder_time)
922 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
924 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
929 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
934 //dir = this.bot_dodgevector;
935 //if (this.bot_dodgevector_jumpbutton)
936 // PHYS_INPUT_BUTTON_JUMP(this) = true;
937 CS(this).movement_x = dir * v_forward * maxspeed;
938 CS(this).movement_y = dir * v_right * maxspeed;
939 CS(this).movement_z = dir * v_up * maxspeed;
941 // Emulate keyboard interface
943 havocbot_keyboard_movement(this, destorg);
946 //if(this.aistatus & AI_STATUS_ROAMING)
947 if(!bunnyhop_forbidden && this.goalcurrent)
948 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
949 havocbot_bunnyhop(this, dir);
951 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
952 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;
953 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);
956 entity havocbot_gettarget(entity this, bool secondary)
959 vector eye = CENTER_OR_VIEWOFS(this);
960 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
962 vector v = CENTER_OR_VIEWOFS(it);
963 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
964 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
965 if(bot_shouldattack(this, it))
967 traceline(eye, v, true, this);
968 if (trace_ent == it || trace_fraction >= 1)
976 void havocbot_chooseenemy(entity this)
978 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
985 if (!bot_shouldattack(this, this.enemy))
987 // enemy died or something, find a new target
989 this.havocbot_chooseenemy_finished = time;
991 else if (this.havocbot_stickenemy)
993 // tracking last chosen enemy
994 // if enemy is visible
995 // and not really really far away
996 // and we're not severely injured
997 // then keep tracking for a half second into the future
998 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
999 if (trace_ent == this.enemy || trace_fraction == 1)
1000 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
1001 if (this.health > 30)
1003 // remain tracking him for a shot while (case he went after a small corner or pilar
1004 this.havocbot_chooseenemy_finished = time + 0.5;
1007 // enemy isn't visible, or is far away, or we're injured severely
1008 // so stop preferring this enemy
1009 // (it will still take a half second until a new one is chosen)
1010 this.havocbot_stickenemy = 0;
1013 if (time < this.havocbot_chooseenemy_finished)
1015 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
1016 vector eye = this.origin + this.view_ofs;
1018 float bestrating = 100000000;
1021 int hf = this.dphitcontentsmask;
1023 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1025 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1027 bool scan_transparent = false;
1028 bool scan_secondary_targets = false;
1029 bool have_secondary_targets = false;
1032 scan_secondary_targets = false;
1034 IL_EACH(g_bot_targets, it.bot_attack,
1036 if(!scan_secondary_targets)
1038 if(it.classname == "misc_breakablemodel")
1040 have_secondary_targets = true;
1044 else if(it.classname != "misc_breakablemodel")
1047 vector v = (it.absmin + it.absmax) * 0.5;
1048 float rating = vlen2(v - eye);
1049 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1050 if (bestrating > rating)
1051 if (bot_shouldattack(this, it))
1053 traceline(eye, v, true, this);
1054 if (trace_ent == it || trace_fraction >= 1)
1057 bestrating = rating;
1062 if(!best && have_secondary_targets && !scan_secondary_targets)
1064 scan_secondary_targets = true;
1066 bestrating = 100000000;
1070 // I want to do a second scan if no enemy was found or I don't have weapons
1071 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1072 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1074 if(scan_transparent)
1077 // Set flags to see through transparent objects
1078 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1080 scan_transparent = true;
1083 // Restore hit flags
1084 this.dphitcontentsmask = hf;
1087 this.havocbot_stickenemy = true;
1088 if(best && best.classname == "misc_breakablemodel")
1089 this.havocbot_stickenemy = false;
1092 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1094 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1095 // so skip this for them, or they'll never get to reload their weapons at all.
1096 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1100 // if this weapon is scheduled for reloading, don't switch to it during combat
1101 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1103 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1104 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1105 return true; // other weapon available
1112 void havocbot_chooseweapon(entity this, .entity weaponentity)
1117 if(g_weaponarena_weapons == WEPSET(TUBA))
1119 this.(weaponentity).m_switchweapon = WEP_TUBA;
1123 // TODO: clean this up by moving it to weapon code
1124 if(this.enemy==NULL)
1126 // If no weapon was chosen get the first available weapon
1127 if(this.(weaponentity).m_weapon==WEP_Null)
1128 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1129 if(client_hasweapon(this, it, weaponentity, true, false))
1131 this.(weaponentity).m_switchweapon = it;
1138 // Do not change weapon during the next second after a combo
1139 float f = time - this.lastcombotime;
1144 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1146 // Should it do a weapon combo?
1147 float af, ct, combo_time, combo;
1149 af = ATTACK_FINISHED(this, 0);
1150 ct = autocvar_bot_ai_weapon_combo_threshold;
1152 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1153 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1154 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1158 if(autocvar_bot_ai_weapon_combo)
1159 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1163 this.lastcombotime = time;
1166 distance *= (2 ** this.bot_rangepreference);
1168 // Custom weapon list based on distance to the enemy
1169 if(bot_custom_weapon){
1171 // Choose weapons for far distance
1172 if ( distance > bot_distance_far ) {
1173 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1174 w = bot_weapons_far[i];
1175 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1177 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1179 this.(weaponentity).m_switchweapon = Weapons_from(w);
1185 // Choose weapons for mid distance
1186 if ( distance > bot_distance_close) {
1187 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1188 w = bot_weapons_mid[i];
1189 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1191 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1193 this.(weaponentity).m_switchweapon = Weapons_from(w);
1199 // Choose weapons for close distance
1200 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1201 w = bot_weapons_close[i];
1202 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1204 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1206 this.(weaponentity).m_switchweapon = Weapons_from(w);
1213 void havocbot_aim(entity this)
1215 if (time < this.nextaim)
1217 this.nextaim = time + 0.1;
1218 vector myvel = this.velocity;
1219 if (!this.waterlevel)
1221 if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1222 else if (this.enemy)
1224 vector enemyvel = this.enemy.velocity;
1225 if (!this.enemy.waterlevel)
1227 lag_additem(this, time + CS(this).ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1230 lag_additem(this, time + CS(this).ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1233 bool havocbot_moveto_refresh_route(entity this)
1235 // Refresh path to goal if necessary
1237 wp = this.havocbot_personal_waypoint;
1238 navigation_goalrating_start(this);
1239 navigation_routerating(this, wp, 10000, 10000);
1240 navigation_goalrating_end(this);
1241 return (this.goalentity != NULL);
1244 float havocbot_moveto(entity this, vector pos)
1248 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1250 // Step 4: Move to waypoint
1251 if(this.havocbot_personal_waypoint==NULL)
1253 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1254 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1255 return CMD_STATUS_ERROR;
1258 if (!bot_strategytoken_taken)
1259 if(this.havocbot_personal_waypoint_searchtime<time)
1261 bot_strategytoken_taken = true;
1262 if(havocbot_moveto_refresh_route(this))
1264 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1265 this.havocbot_personal_waypoint_searchtime = time + 10;
1266 this.havocbot_personal_waypoint_failcounter = 0;
1270 this.havocbot_personal_waypoint_failcounter += 1;
1271 this.havocbot_personal_waypoint_searchtime = time + 2;
1272 if(this.havocbot_personal_waypoint_failcounter >= 30)
1274 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1275 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1276 delete(this.havocbot_personal_waypoint);
1277 return CMD_STATUS_ERROR;
1280 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1284 if(autocvar_bot_debug_goalstack)
1285 debuggoalstack(this);
1289 SET_DESTCOORDS(this.goalcurrent, this.origin, dir);
1290 dir = dir - (this.origin + this.view_ofs);
1292 bot_aimdir(this, dir, -1);
1295 havocbot_movetogoal(this);
1297 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1299 // Step 5: Waypoint reached
1300 LOG_TRACE(this.netname, "'s personal waypoint reached");
1301 delete(this.havocbot_personal_waypoint);
1302 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1303 return CMD_STATUS_FINISHED;
1306 return CMD_STATUS_EXECUTING;
1309 // Step 2: Linking waypoint
1310 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1312 // Wait until it is linked
1313 if(!this.havocbot_personal_waypoint.wplinked)
1315 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1316 return CMD_STATUS_EXECUTING;
1319 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1320 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1321 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1323 // Step 3: Route to waypoint
1324 LOG_TRACE(this.netname, " walking to its personal waypoint");
1326 return CMD_STATUS_EXECUTING;
1329 // Step 1: Spawning waypoint
1330 wp = waypoint_spawnpersonal(this, pos);
1333 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1334 return CMD_STATUS_ERROR;
1337 this.havocbot_personal_waypoint = wp;
1338 this.havocbot_personal_waypoint_failcounter = 0;
1339 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1341 // if pos is inside a teleport, then let's mark it as teleport waypoint
1342 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1344 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1345 this.lastteleporttime = 0;
1349 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1350 print("routing to a teleporter\n");
1352 print("routing to a non-teleporter\n");
1355 return CMD_STATUS_EXECUTING;
1358 float havocbot_resetgoal(entity this)
1360 navigation_clearroute(this);
1361 return CMD_STATUS_FINISHED;
1364 void havocbot_setupbot(entity this)
1366 this.bot_ai = havocbot_ai;
1367 this.cmd_moveto = havocbot_moveto;
1368 this.cmd_resetgoal = havocbot_resetgoal;
1370 havocbot_chooserole(this);
1373 vector havocbot_dodge(entity this)
1375 // LordHavoc: disabled because this is too expensive
1380 float danger, bestdanger, vl, d;
1383 // check for dangerous objects near bot or approaching bot
1384 head = findchainfloat(bot_dodge, true);
1387 if (head.owner != this)
1389 vl = vlen(head.velocity);
1390 if (vl > autocvar_sv_maxspeed * 0.3)
1392 n = normalize(head.velocity);
1393 v = this.origin - head.origin;
1395 if (d > (0 - head.bot_dodgerating))
1396 if (d < (vl * 0.2 + head.bot_dodgerating))
1398 // calculate direction and distance from the flight path, by removing the forward axis
1399 v = v - (n * (v * n));
1400 danger = head.bot_dodgerating - vlen(v);
1401 if (bestdanger < danger)
1403 bestdanger = danger;
1404 // dodge to the side of the object
1405 dodge = normalize(v);
1411 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1412 if (bestdanger < danger)
1414 bestdanger = danger;
1415 dodge = normalize(this.origin - head.origin);