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 if(this.goalcurrent.wpisbox && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin, this.origin))
143 v = this.goalcurrent.origin;
145 SET_DESTCOORDS(this.goalcurrent, this.origin, v);
146 next = now = v - (this.origin + this.view_ofs);
147 aimdistance = vlen(now);
149 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
151 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
152 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
154 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
156 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
157 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
158 blend = skillblend * (1-distanceblend);
159 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
160 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
161 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
162 v = now + blend * (next - now);
163 //dprint(etos(this), " ");
164 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
165 //v = now * (distanceblend) + next * (1-distanceblend);
166 if (this.waterlevel < WATERLEVEL_SWIMMING)
168 //dprint("walk at:", vtos(v), "\n");
169 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
170 bot_aimdir(this, v, -1);
172 havocbot_movetogoal(this);
174 // if the bot is not attacking, consider reloading weapons
175 if (!(this.aistatus & AI_STATUS_ATTACKING))
177 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
179 .entity weaponentity = weaponentities[slot];
181 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
184 // we are currently holding a weapon that's not fully loaded, reload it
185 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
186 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
187 CS(this).impulse = IMP_weapon_reload.impulse; // not sure if this is done right
189 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
190 // the code above executes next frame, starting the reloading then
191 if(skill >= 5) // bots can only look for unloaded weapons past this skill
192 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
194 FOREACH(Weapons, it != WEP_Null, LAMBDA(
195 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
197 this.(weaponentity).m_switchweapon = it;
206 void havocbot_keyboard_movement(entity this, vector destorg)
210 if (time > this.havocbot_keyboardtime)
212 float sk = skill + this.bot_moveskill;
213 this.havocbot_keyboardtime =
215 this.havocbot_keyboardtime
216 + 0.05 / max(1, sk + this.havocbot_keyboardskill)
217 + random() * 0.025 / max(0.00025, skill + this.havocbot_keyboardskill)
219 keyboard = CS(this).movement / autocvar_sv_maxspeed;
221 float trigger = autocvar_bot_ai_keyboard_threshold;
222 float trigger1 = -trigger;
224 // categorize forward movement
225 // at skill < 1.5 only forward
226 // at skill < 2.5 only individual directions
227 // at skill < 4.5 only individual directions, and forward diagonals
228 // at skill >= 4.5, all cases allowed
229 if (keyboard.x > trigger)
235 else if (keyboard.x < trigger1 && sk > 1.5)
250 if (keyboard.y > trigger)
252 else if (keyboard.y < trigger1)
257 if (keyboard.z > trigger)
259 else if (keyboard.z < trigger1)
264 this.havocbot_keyboard = keyboard * autocvar_sv_maxspeed;
265 if (this.havocbot_ducktime > time)
266 PHYS_INPUT_BUTTON_CROUCH(this) = true;
269 keyboard = this.havocbot_keyboard;
270 float blend = bound(0, vlen(destorg - this.origin) / autocvar_bot_ai_keyboard_distance, 1); // When getting close move with 360 degree
271 //dprint("movement ", vtos(CS(this).movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
272 CS(this).movement = CS(this).movement + (keyboard - CS(this).movement) * blend;
275 void havocbot_bunnyhop(entity this, vector dir)
277 float bunnyhopdistance;
282 // Don't jump when attacking
283 if(this.aistatus & AI_STATUS_ATTACKING)
286 if(IS_PLAYER(this.goalcurrent))
289 maxspeed = autocvar_sv_maxspeed;
291 if(this.aistatus & AI_STATUS_RUNNING && vdist(this.velocity, <, autocvar_sv_maxspeed * 0.75)
292 || this.aistatus & AI_STATUS_DANGER_AHEAD)
294 this.aistatus &= ~AI_STATUS_RUNNING;
295 PHYS_INPUT_BUTTON_JUMP(this) = false;
296 this.bot_canruntogoal = 0;
297 this.bot_timelastseengoal = 0;
301 if(this.waterlevel > WATERLEVEL_WETFEET)
303 this.aistatus &= ~AI_STATUS_RUNNING;
307 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
309 this.bot_canruntogoal = 0;
310 this.bot_timelastseengoal = 0;
313 SET_DESTCOORDS(this.goalcurrent, this.origin, gco);
314 bunnyhopdistance = vlen(this.origin - gco);
316 // Run only to visible goals
317 if(IS_ONGROUND(this))
318 if(vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed)) // if -really- running
319 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
321 this.bot_lastseengoal = this.goalcurrent;
324 if(this.bot_timelastseengoal)
326 // for a period of time
327 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
330 checkdistance = true;
332 // don't run if it is too close
333 if(this.bot_canruntogoal==0)
335 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
336 this.bot_canruntogoal = 1;
338 this.bot_canruntogoal = -1;
341 if(this.bot_canruntogoal != 1)
344 if(this.aistatus & AI_STATUS_ROAMING)
345 if(this.goalcurrent.classname=="waypoint")
346 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
347 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
348 if(this.goalstack01 && !wasfreed(this.goalstack01))
350 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
351 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
352 while (deviation.y < -180) deviation.y = deviation.y + 360;
353 while (deviation.y > 180) deviation.y = deviation.y - 360;
355 if(fabs(deviation.y) < 20)
356 if(bunnyhopdistance < vlen(this.origin - gno))
357 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
359 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
360 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
362 checkdistance = false;
369 this.aistatus &= ~AI_STATUS_RUNNING;
370 // increase stop distance in case the goal is on a slope or a lower platform
371 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
372 PHYS_INPUT_BUTTON_JUMP(this) = true;
376 this.aistatus |= AI_STATUS_RUNNING;
377 PHYS_INPUT_BUTTON_JUMP(this) = true;
383 this.bot_timelastseengoal = time;
388 this.bot_timelastseengoal = 0;
392 // Release jump button
393 if(!cvar("sv_pogostick"))
394 if((IS_ONGROUND(this)) == 0)
396 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
397 PHYS_INPUT_BUTTON_JUMP(this) = false;
400 if(this.aistatus & AI_STATUS_RUNNING)
401 if(vlen(this.velocity)>maxspeed)
403 deviation = vectoangles(dir) - vectoangles(this.velocity);
404 while (deviation.y < -180) deviation.y = deviation.y + 360;
405 while (deviation.y > 180) deviation.y = deviation.y - 360;
407 if(fabs(deviation.y)>10)
408 CS(this).movement_x = 0;
411 CS(this).movement_y = maxspeed * -1;
412 else if(deviation.y<10)
413 CS(this).movement_y = maxspeed;
420 // return true when bot isn't getting closer to the current goal
421 bool havocbot_checkgoaldistance(entity this, vector gco)
423 float curr_dist = vlen(this.origin - gco);
424 if(curr_dist > this.goalcurrent_distance)
426 if(!this.goalcurrent_distance_time)
427 this.goalcurrent_distance_time = time;
428 else if (time - this.goalcurrent_distance_time > 0.5)
433 // reduce it a little bit so it works even with very small approaches to the goal
434 this.goalcurrent_distance = max(20, curr_dist - 15);
435 this.goalcurrent_distance_time = 0;
440 void havocbot_movetogoal(entity this)
446 vector evadeobstacle;
452 //if (this.goalentity)
453 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
454 CS(this).movement = '0 0 0';
455 maxspeed = autocvar_sv_maxspeed;
457 // Jetpack navigation
459 if(this.navigation_jetpack_goal)
460 if(this.goalcurrent==this.navigation_jetpack_goal)
463 if(autocvar_bot_debug_goalstack)
465 debuggoalstack(this);
466 te_wizspike(this.navigation_jetpack_point);
470 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
472 // Brake almost completely so it can get a good direction
473 if(vdist(this.velocity, >, 10))
475 this.aistatus |= AI_STATUS_JETPACK_FLYING;
478 makevectors(this.v_angle.y * '0 1 0');
479 dir = normalize(this.navigation_jetpack_point - this.origin);
482 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
484 // Calculate brake distance in xy
488 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
490 v = vlen(this.velocity - this.velocity.z * '0 0 1');
491 db = ((v ** 2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
492 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
493 if(d < db || d < 500)
496 if(fabs(this.velocity.x)>maxspeed*0.3)
498 CS(this).movement_x = dir * v_forward * -maxspeed;
501 // Switch to normal mode
502 this.navigation_jetpack_goal = NULL;
503 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
504 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
508 else if(checkpvs(this.origin,this.goalcurrent))
510 // If I can see the goal switch to landing code
511 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
512 this.aistatus |= AI_STATUS_JETPACK_LANDING;
517 PHYS_INPUT_BUTTON_HOOK(this) = true;
518 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
520 CS(this).movement_x = dir * v_forward * maxspeed;
521 CS(this).movement_y = dir * v_right * maxspeed;
526 // Handling of jump pads
527 if(this.jumppadcount)
529 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
531 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
532 navigation_poptouchedgoals(this);
535 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
537 // If got stuck on the jump pad try to reach the farthest visible waypoint
538 // but with some randomness so it can try out different paths
539 if(!this.goalcurrent)
541 entity newgoal = NULL;
542 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
544 if(it.wpflags & WAYPOINTFLAG_TELEPORT)
545 if(it.origin.z < this.origin.z - 100 && vdist(vec2(it.origin - this.origin), <, 100))
548 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
550 if(trace_fraction < 1)
553 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
559 this.ignoregoal = this.goalcurrent;
560 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
561 navigation_clearroute(this);
562 navigation_routetogoal(this, newgoal, this.origin);
563 if(autocvar_bot_debug_goalstack)
564 debuggoalstack(this);
565 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
570 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
571 if (this.origin.z > gco.z && vdist(vec2(this.velocity), <, autocvar_sv_maxspeed))
572 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
573 else if(havocbot_checkgoaldistance(this, gco))
575 navigation_clearroute(this);
576 this.bot_strategytime = 0;
584 if(time - this.lastteleporttime > 0.2 && this.velocity.z > 0)
586 vector velxy = this.velocity; velxy_z = 0;
587 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
589 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
590 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
595 // Don't chase players while using a jump pad
596 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
600 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
601 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
603 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
605 if (!(IS_ONGROUND(this)))
607 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
608 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
609 if(this.items & IT_JETPACK)
611 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
612 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
614 if(this.velocity.z<0)
616 PHYS_INPUT_BUTTON_HOOK(this) = true;
620 PHYS_INPUT_BUTTON_HOOK(this) = true;
622 // If there is no goal try to move forward
624 if(this.goalcurrent==NULL)
627 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
629 vector xyvelocity = this.velocity; xyvelocity_z = 0;
630 float xyspeed = xyvelocity * dir;
632 if(xyspeed < (maxspeed / 2))
634 makevectors(this.v_angle.y * '0 1 0');
635 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
636 if(trace_fraction==1)
638 CS(this).movement_x = dir * v_forward * maxspeed;
639 CS(this).movement_y = dir * v_right * maxspeed;
641 havocbot_keyboard_movement(this, this.origin + dir * 100);
645 this.havocbot_blockhead = true;
649 else if(this.health > WEP_CVAR(devastator, damage) * 0.5 * ((this.strength_finished < time) ? autocvar_g_balance_powerup_strength_selfdamage : 1))
651 if(this.velocity.z < 0)
653 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
655 .entity weaponentity = weaponentities[slot];
657 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
660 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
662 CS(this).movement_x = maxspeed;
664 if(this.rocketjumptime)
666 if(time > this.rocketjumptime)
668 PHYS_INPUT_BUTTON_ATCK2(this) = true;
669 this.rocketjumptime = 0;
674 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
676 PHYS_INPUT_BUTTON_ATCK(this) = true;
677 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
685 // If there is no goal try to move forward
686 if(this.goalcurrent==NULL)
687 CS(this).movement_x = maxspeed;
691 // If we are under water with no goals, swim up
693 if(this.goalcurrent==NULL)
696 if(this.waterlevel>WATERLEVEL_SWIMMING)
698 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
699 PHYS_INPUT_BUTTON_JUMP(this) = true;
701 PHYS_INPUT_BUTTON_JUMP(this) = false;
702 makevectors(this.v_angle.y * '0 1 0');
703 CS(this).movement_x = dir * v_forward * maxspeed;
704 CS(this).movement_y = dir * v_right * maxspeed;
705 CS(this).movement_z = dir * v_up * maxspeed;
708 // if there is nowhere to go, exit
709 if (this.goalcurrent == NULL)
713 bool locked_goal = false;
714 if(this.goalentity && wasfreed(this.goalentity))
716 navigation_clearroute(this);
717 this.bot_strategytime = 0;
720 else if(this.goalentity.bot_pickup)
722 if(this.goalentity.bot_pickup_respawning)
724 if(this.goalentity.solid) // item respawned
725 this.goalentity.bot_pickup_respawning = false;
726 else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
728 this.goalentity.bot_pickup_respawning = false;
729 navigation_clearroute(this);
730 this.bot_strategytime = 0;
733 else if(this.goalentity == this.goalcurrent)
734 locked_goal = true; // wait for item to respawn
736 else if(!this.goalentity.solid)
738 navigation_clearroute(this);
739 this.bot_strategytime = 0;
744 navigation_poptouchedgoals(this);
746 // if ran out of goals try to use an alternative goal or get a new strategy asap
747 if(this.goalcurrent == NULL)
749 this.bot_strategytime = 0;
754 if(autocvar_bot_debug_goalstack)
755 debuggoalstack(this);
757 SET_DESTCOORDS(this.goalcurrent, this.origin, destorg);
759 // in case bot ends up inside the teleport waypoint without touching
760 // the teleport itself, head to the teleport origin
761 if(this.goalcurrent.wpisbox && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin, this.origin))
762 destorg = this.goalcurrent.origin;
764 diff = destorg - this.origin;
766 dir = normalize(diff);
767 flatdir = diff;flatdir.z = 0;
768 flatdir = normalize(flatdir);
769 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
771 //if (this.bot_dodgevector_time < time)
773 //this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
774 //this.bot_dodgevector_jumpbutton = 1;
775 evadeobstacle = '0 0 0';
778 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
779 makevectors(this.v_angle.y * '0 1 0');
782 if(this.waterlevel>WATERLEVEL_SWIMMING)
784 if(!this.goalcurrent)
785 this.aistatus |= AI_STATUS_OUT_WATER;
786 else if(gco.z > this.origin.z)
787 PHYS_INPUT_BUTTON_JUMP(this) = true;
792 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
793 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
794 PHYS_INPUT_BUTTON_JUMP(this) = true;
796 PHYS_INPUT_BUTTON_JUMP(this) = false;
803 if(this.aistatus & AI_STATUS_OUT_WATER)
804 this.aistatus &= ~AI_STATUS_OUT_WATER;
806 // jump if going toward an obstacle that doesn't look like stairs we
807 // can walk up directly
808 offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
809 tracebox(this.origin, this.mins, this.maxs, this.origin + offset, false, this);
810 if (trace_fraction < 1)
811 if (trace_plane_normal.z < 0.7)
814 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + offset + stepheightvec, false, this);
815 if (trace_fraction < s + 0.01)
816 if (trace_plane_normal.z < 0.7)
819 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + offset + jumpstepheightvec, false, this);
820 if (trace_fraction > s)
821 PHYS_INPUT_BUTTON_JUMP(this) = true;
825 // if bot for some reason doesn't get close to the current goal find another one
826 if(!this.jumppadcount && !IS_PLAYER(this.goalcurrent) && !(this.goalcurrent.bot_pickup_respawning && this.goalcurrent_distance < 50))
827 if(havocbot_checkgoaldistance(this, gco))
829 navigation_clearroute(this);
830 this.bot_strategytime = 0;
834 // Check for water/slime/lava and dangerous edges
835 // (only when the bot is on the ground or jumping intentionally)
837 vector dst_ahead = this.origin + this.view_ofs + offset;
838 vector dst_down = dst_ahead - '0 0 3000';
839 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
841 bool unreachable = false;
843 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
844 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
847 traceline(dst_ahead , dst_down, true, NULL);
848 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
849 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
850 if(trace_endpos.z < this.origin.z + this.mins.z)
852 s = pointcontents(trace_endpos + '0 0 1');
853 if (s != CONTENT_SOLID)
854 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
855 evadelava = normalize(this.velocity) * -1;
856 else if (s == CONTENT_SKY)
857 evadeobstacle = normalize(this.velocity) * -1;
858 else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
860 // the traceline check isn't enough but is good as optimization,
861 // when not true (most of the time) this tracebox call is avoided
862 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
863 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
865 if (gco.z > this.origin.z + jumpstepheightvec.z)
867 // the goal is probably on an upper platform, assume bot can't get there
871 evadelava = normalize(this.velocity) * -1;
880 makevectors(this.v_angle.y * '0 1 0');
882 if(evadeobstacle || evadelava || (s == CONTENT_WATER))
884 this.aistatus |= AI_STATUS_DANGER_AHEAD;
885 if(IS_PLAYER(this.goalcurrent))
890 navigation_clearroute(this);
891 this.bot_strategytime = 0;
895 dodge = havocbot_dodge(this);
896 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
897 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
898 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
899 if(IS_PLAYER(trace_ent))
900 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
902 dir = normalize(dir + dodge + evadeobstacle + evadelava);
903 // this.bot_dodgevector = dir;
904 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
907 if(time < this.ladder_time)
909 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
911 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
916 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
921 //dir = this.bot_dodgevector;
922 //if (this.bot_dodgevector_jumpbutton)
923 // PHYS_INPUT_BUTTON_JUMP(this) = true;
924 CS(this).movement_x = dir * v_forward * maxspeed;
925 CS(this).movement_y = dir * v_right * maxspeed;
926 CS(this).movement_z = dir * v_up * maxspeed;
928 // Emulate keyboard interface
930 havocbot_keyboard_movement(this, destorg);
933 // if(this.aistatus & AI_STATUS_ROAMING)
935 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
936 havocbot_bunnyhop(this, dir);
938 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
939 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;
940 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);
943 entity havocbot_gettarget(entity this, bool secondary)
946 vector eye = CENTER_OR_VIEWOFS(this);
947 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
949 vector v = CENTER_OR_VIEWOFS(it);
950 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
951 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
952 if(bot_shouldattack(this, it))
954 traceline(eye, v, true, this);
955 if (trace_ent == it || trace_fraction >= 1)
963 void havocbot_chooseenemy(entity this)
965 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
972 if (!bot_shouldattack(this, this.enemy))
974 // enemy died or something, find a new target
976 this.havocbot_chooseenemy_finished = time;
978 else if (this.havocbot_stickenemy)
980 // tracking last chosen enemy
981 // if enemy is visible
982 // and not really really far away
983 // and we're not severely injured
984 // then keep tracking for a half second into the future
985 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
986 if (trace_ent == this.enemy || trace_fraction == 1)
987 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
988 if (this.health > 30)
990 // remain tracking him for a shot while (case he went after a small corner or pilar
991 this.havocbot_chooseenemy_finished = time + 0.5;
994 // enemy isn't visible, or is far away, or we're injured severely
995 // so stop preferring this enemy
996 // (it will still take a half second until a new one is chosen)
997 this.havocbot_stickenemy = 0;
1000 if (time < this.havocbot_chooseenemy_finished)
1002 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
1003 vector eye = this.origin + this.view_ofs;
1005 float bestrating = 100000000;
1008 int hf = this.dphitcontentsmask;
1010 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1012 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1014 bool scan_transparent = false;
1015 bool scan_secondary_targets = false;
1016 bool have_secondary_targets = false;
1019 scan_secondary_targets = false;
1021 IL_EACH(g_bot_targets, it.bot_attack,
1023 if(!scan_secondary_targets)
1025 if(it.classname == "misc_breakablemodel")
1027 have_secondary_targets = true;
1031 else if(it.classname != "misc_breakablemodel")
1034 vector v = (it.absmin + it.absmax) * 0.5;
1035 float rating = vlen2(v - eye);
1036 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1037 if (bestrating > rating)
1038 if (bot_shouldattack(this, it))
1040 traceline(eye, v, true, this);
1041 if (trace_ent == it || trace_fraction >= 1)
1044 bestrating = rating;
1049 if(!best && have_secondary_targets && !scan_secondary_targets)
1051 scan_secondary_targets = true;
1053 bestrating = 100000000;
1057 // I want to do a second scan if no enemy was found or I don't have weapons
1058 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1059 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1061 if(scan_transparent)
1064 // Set flags to see through transparent objects
1065 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1067 scan_transparent = true;
1070 // Restore hit flags
1071 this.dphitcontentsmask = hf;
1074 this.havocbot_stickenemy = true;
1075 if(best && best.classname == "misc_breakablemodel")
1076 this.havocbot_stickenemy = false;
1079 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1081 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1082 // so skip this for them, or they'll never get to reload their weapons at all.
1083 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1087 // if this weapon is scheduled for reloading, don't switch to it during combat
1088 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1090 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1091 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1092 return true; // other weapon available
1099 void havocbot_chooseweapon(entity this, .entity weaponentity)
1104 if(g_weaponarena_weapons == WEPSET(TUBA))
1106 this.(weaponentity).m_switchweapon = WEP_TUBA;
1110 // TODO: clean this up by moving it to weapon code
1111 if(this.enemy==NULL)
1113 // If no weapon was chosen get the first available weapon
1114 if(this.(weaponentity).m_weapon==WEP_Null)
1115 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1116 if(client_hasweapon(this, it, weaponentity, true, false))
1118 this.(weaponentity).m_switchweapon = it;
1125 // Do not change weapon during the next second after a combo
1126 float f = time - this.lastcombotime;
1131 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1133 // Should it do a weapon combo?
1134 float af, ct, combo_time, combo;
1136 af = ATTACK_FINISHED(this, 0);
1137 ct = autocvar_bot_ai_weapon_combo_threshold;
1139 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1140 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1141 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1145 if(autocvar_bot_ai_weapon_combo)
1146 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1150 this.lastcombotime = time;
1153 distance *= (2 ** this.bot_rangepreference);
1155 // Custom weapon list based on distance to the enemy
1156 if(bot_custom_weapon){
1158 // Choose weapons for far distance
1159 if ( distance > bot_distance_far ) {
1160 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1161 w = bot_weapons_far[i];
1162 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1164 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1166 this.(weaponentity).m_switchweapon = Weapons_from(w);
1172 // Choose weapons for mid distance
1173 if ( distance > bot_distance_close) {
1174 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1175 w = bot_weapons_mid[i];
1176 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1178 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1180 this.(weaponentity).m_switchweapon = Weapons_from(w);
1186 // Choose weapons for close distance
1187 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1188 w = bot_weapons_close[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);
1200 void havocbot_aim(entity this)
1202 if (time < this.nextaim)
1204 this.nextaim = time + 0.1;
1205 vector myvel = this.velocity;
1206 if (!this.waterlevel)
1208 if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1209 else if (this.enemy)
1211 vector enemyvel = this.enemy.velocity;
1212 if (!this.enemy.waterlevel)
1214 lag_additem(this, time + CS(this).ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1217 lag_additem(this, time + CS(this).ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1220 bool havocbot_moveto_refresh_route(entity this)
1222 // Refresh path to goal if necessary
1224 wp = this.havocbot_personal_waypoint;
1225 navigation_goalrating_start(this);
1226 navigation_routerating(this, wp, 10000, 10000);
1227 navigation_goalrating_end(this);
1228 return (this.goalentity != NULL);
1231 float havocbot_moveto(entity this, vector pos)
1235 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1237 // Step 4: Move to waypoint
1238 if(this.havocbot_personal_waypoint==NULL)
1240 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1241 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1242 return CMD_STATUS_ERROR;
1245 if (!bot_strategytoken_taken)
1246 if(this.havocbot_personal_waypoint_searchtime<time)
1248 bot_strategytoken_taken = true;
1249 if(havocbot_moveto_refresh_route(this))
1251 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1252 this.havocbot_personal_waypoint_searchtime = time + 10;
1253 this.havocbot_personal_waypoint_failcounter = 0;
1257 this.havocbot_personal_waypoint_failcounter += 1;
1258 this.havocbot_personal_waypoint_searchtime = time + 2;
1259 if(this.havocbot_personal_waypoint_failcounter >= 30)
1261 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1262 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1263 delete(this.havocbot_personal_waypoint);
1264 return CMD_STATUS_ERROR;
1267 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1271 if(autocvar_bot_debug_goalstack)
1272 debuggoalstack(this);
1276 SET_DESTCOORDS(this.goalcurrent, this.origin, dir);
1277 dir = dir - (this.origin + this.view_ofs);
1279 bot_aimdir(this, dir, -1);
1282 havocbot_movetogoal(this);
1284 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1286 // Step 5: Waypoint reached
1287 LOG_TRACE(this.netname, "'s personal waypoint reached");
1288 delete(this.havocbot_personal_waypoint);
1289 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1290 return CMD_STATUS_FINISHED;
1293 return CMD_STATUS_EXECUTING;
1296 // Step 2: Linking waypoint
1297 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1299 // Wait until it is linked
1300 if(!this.havocbot_personal_waypoint.wplinked)
1302 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1303 return CMD_STATUS_EXECUTING;
1306 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1307 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1308 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1310 // Step 3: Route to waypoint
1311 LOG_TRACE(this.netname, " walking to its personal waypoint");
1313 return CMD_STATUS_EXECUTING;
1316 // Step 1: Spawning waypoint
1317 wp = waypoint_spawnpersonal(this, pos);
1320 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1321 return CMD_STATUS_ERROR;
1324 this.havocbot_personal_waypoint = wp;
1325 this.havocbot_personal_waypoint_failcounter = 0;
1326 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1328 // if pos is inside a teleport, then let's mark it as teleport waypoint
1329 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1331 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1332 this.lastteleporttime = 0;
1336 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1337 print("routing to a teleporter\n");
1339 print("routing to a non-teleporter\n");
1342 return CMD_STATUS_EXECUTING;
1345 float havocbot_resetgoal(entity this)
1347 navigation_clearroute(this);
1348 return CMD_STATUS_FINISHED;
1351 void havocbot_setupbot(entity this)
1353 this.bot_ai = havocbot_ai;
1354 this.cmd_moveto = havocbot_moveto;
1355 this.cmd_resetgoal = havocbot_resetgoal;
1357 havocbot_chooserole(this);
1360 vector havocbot_dodge(entity this)
1362 // LordHavoc: disabled because this is too expensive
1367 float danger, bestdanger, vl, d;
1370 // check for dangerous objects near bot or approaching bot
1371 head = findchainfloat(bot_dodge, true);
1374 if (head.owner != this)
1376 vl = vlen(head.velocity);
1377 if (vl > autocvar_sv_maxspeed * 0.3)
1379 n = normalize(head.velocity);
1380 v = this.origin - head.origin;
1382 if (d > (0 - head.bot_dodgerating))
1383 if (d < (vl * 0.2 + head.bot_dodgerating))
1385 // calculate direction and distance from the flight path, by removing the forward axis
1386 v = v - (n * (v * n));
1387 danger = head.bot_dodgerating - vlen(v);
1388 if (bestdanger < danger)
1390 bestdanger = danger;
1391 // dodge to the side of the object
1392 dodge = normalize(v);
1398 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1399 if (bestdanger < danger)
1401 bestdanger = danger;
1402 dodge = normalize(this.origin - head.origin);