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 // TODO: tracewalk() should take care of this job (better path finding under water)
48 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
49 if(!(IS_DEAD(this) || STAT(FROZEN, this)))
51 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
53 // Look for the closest waypoint out of water
54 entity newgoal = NULL;
55 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
57 if(it.origin.z < this.origin.z)
60 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
63 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
66 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
68 if(trace_fraction < 1)
71 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
77 // te_wizspike(newgoal.origin);
78 navigation_pushroute(this, newgoal);
82 // token has been used this frame
83 bot_strategytoken_taken = true;
86 if(IS_DEAD(this) || STAT(FROZEN, this))
89 havocbot_chooseenemy(this);
91 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
93 .entity weaponentity = weaponentities[slot];
94 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
95 if(this.(weaponentity).bot_chooseweapontime < time)
97 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
98 havocbot_chooseweapon(this, weaponentity);
103 if (this.bot_aimtarg)
105 this.aistatus |= AI_STATUS_ATTACKING;
106 this.aistatus &= ~AI_STATUS_ROAMING;
110 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
112 PHYS_INPUT_BUTTON_ATCK(this) = false;
113 PHYS_INPUT_BUTTON_ATCK2(this) = false;
117 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
119 .entity weaponentity = weaponentities[slot];
120 Weapon w = this.(weaponentity).m_weapon;
121 if(w == WEP_Null && slot != 0)
123 w.wr_aim(w, this, weaponentity);
124 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
125 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
131 if(IS_PLAYER(this.bot_aimtarg))
132 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
135 else if (this.goalcurrent)
137 this.aistatus |= AI_STATUS_ROAMING;
138 this.aistatus &= ~AI_STATUS_ATTACKING;
140 vector now,v,next;//,heading;
141 float aimdistance,skillblend,distanceblend,blend;
142 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
143 aimdistance = vlen(now);
144 //heading = this.velocity;
145 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
147 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
148 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
150 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
152 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
153 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
154 blend = skillblend * (1-distanceblend);
155 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
156 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
157 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
158 v = now + blend * (next - now);
159 //dprint(etos(this), " ");
160 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
161 //v = now * (distanceblend) + next * (1-distanceblend);
162 if (this.waterlevel < WATERLEVEL_SWIMMING)
164 //dprint("walk at:", vtos(v), "\n");
165 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
166 bot_aimdir(this, v, -1);
168 havocbot_movetogoal(this);
170 // if the bot is not attacking, consider reloading weapons
171 if (!(this.aistatus & AI_STATUS_ATTACKING))
173 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
175 .entity weaponentity = weaponentities[slot];
177 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
180 // we are currently holding a weapon that's not fully loaded, reload it
181 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
182 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
183 this.impulse = IMP_weapon_reload.impulse; // not sure if this is done right
185 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
186 // the code above executes next frame, starting the reloading then
187 if(skill >= 5) // bots can only look for unloaded weapons past this skill
188 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
190 FOREACH(Weapons, it != WEP_Null, LAMBDA(
191 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
193 this.(weaponentity).m_switchweapon = it;
202 void havocbot_keyboard_movement(entity this, vector destorg)
206 if (time > this.havocbot_keyboardtime)
208 float sk = skill + this.bot_moveskill;
209 this.havocbot_keyboardtime =
211 this.havocbot_keyboardtime
212 + 0.05 / max(1, sk + this.havocbot_keyboardskill)
213 + random() * 0.025 / max(0.00025, skill + this.havocbot_keyboardskill)
215 keyboard = this.movement / autocvar_sv_maxspeed;
217 float trigger = autocvar_bot_ai_keyboard_threshold;
218 float trigger1 = -trigger;
220 // categorize forward movement
221 // at skill < 1.5 only forward
222 // at skill < 2.5 only individual directions
223 // at skill < 4.5 only individual directions, and forward diagonals
224 // at skill >= 4.5, all cases allowed
225 if (keyboard.x > trigger)
231 else if (keyboard.x < trigger1 && sk > 1.5)
246 if (keyboard.y > trigger)
248 else if (keyboard.y < trigger1)
253 if (keyboard.z > trigger)
255 else if (keyboard.z < trigger1)
260 this.havocbot_keyboard = keyboard * autocvar_sv_maxspeed;
261 if (this.havocbot_ducktime > time)
262 PHYS_INPUT_BUTTON_CROUCH(this) = true;
265 keyboard = this.havocbot_keyboard;
266 float blend = bound(0, vlen(destorg - this.origin) / autocvar_bot_ai_keyboard_distance, 1); // When getting close move with 360 degree
267 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
268 this.movement = this.movement + (keyboard - this.movement) * blend;
271 void havocbot_bunnyhop(entity this, vector dir)
273 float bunnyhopdistance;
278 // Don't jump when attacking
279 if(this.aistatus & AI_STATUS_ATTACKING)
282 if(IS_PLAYER(this.goalcurrent))
285 maxspeed = autocvar_sv_maxspeed;
287 if(this.aistatus & AI_STATUS_RUNNING && vdist(this.velocity, <, autocvar_sv_maxspeed * 0.75)
288 || this.aistatus & AI_STATUS_DANGER_AHEAD)
290 this.aistatus &= ~AI_STATUS_RUNNING;
291 PHYS_INPUT_BUTTON_JUMP(this) = false;
292 this.bot_canruntogoal = 0;
293 this.bot_timelastseengoal = 0;
297 if(this.waterlevel > WATERLEVEL_WETFEET)
299 this.aistatus &= ~AI_STATUS_RUNNING;
303 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
305 this.bot_canruntogoal = 0;
306 this.bot_timelastseengoal = 0;
309 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
310 bunnyhopdistance = vlen(this.origin - gco);
312 // Run only to visible goals
313 if(IS_ONGROUND(this))
314 if(vlen(this.velocity - eZ * this.velocity.z) >= autocvar_sv_maxspeed) // if -really- running
315 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
317 this.bot_lastseengoal = this.goalcurrent;
320 if(this.bot_timelastseengoal)
322 // for a period of time
323 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
326 checkdistance = true;
328 // don't run if it is too close
329 if(this.bot_canruntogoal==0)
331 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
332 this.bot_canruntogoal = 1;
334 this.bot_canruntogoal = -1;
337 if(this.bot_canruntogoal != 1)
340 if(this.aistatus & AI_STATUS_ROAMING)
341 if(this.goalcurrent.classname=="waypoint")
342 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
343 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
344 if(this.goalstack01 && !wasfreed(this.goalstack01))
346 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
347 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
348 while (deviation.y < -180) deviation.y = deviation.y + 360;
349 while (deviation.y > 180) deviation.y = deviation.y - 360;
351 if(fabs(deviation.y) < 20)
352 if(bunnyhopdistance < vlen(this.origin - gno))
353 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
355 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
356 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
358 checkdistance = false;
365 this.aistatus &= ~AI_STATUS_RUNNING;
366 // increase stop distance in case the goal is on a slope or a lower platform
367 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
368 PHYS_INPUT_BUTTON_JUMP(this) = true;
372 this.aistatus |= AI_STATUS_RUNNING;
373 PHYS_INPUT_BUTTON_JUMP(this) = true;
379 this.bot_timelastseengoal = time;
384 this.bot_timelastseengoal = 0;
388 // Release jump button
389 if(!cvar("sv_pogostick"))
390 if((IS_ONGROUND(this)) == 0)
392 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
393 PHYS_INPUT_BUTTON_JUMP(this) = false;
396 if(this.aistatus & AI_STATUS_RUNNING)
397 if(vlen(this.velocity)>maxspeed)
399 deviation = vectoangles(dir) - vectoangles(this.velocity);
400 while (deviation.y < -180) deviation.y = deviation.y + 360;
401 while (deviation.y > 180) deviation.y = deviation.y - 360;
403 if(fabs(deviation.y)>10)
407 this.movement_y = maxspeed * -1;
408 else if(deviation.y<10)
409 this.movement_y = maxspeed;
416 .entity goalcurrent_prev;
417 .float goalcurrent_distance;
418 .float goalcurrent_distance_time;
419 void havocbot_movetogoal(entity this)
427 vector evadeobstacle;
433 //if (this.goalentity)
434 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
435 this.movement = '0 0 0';
436 maxspeed = autocvar_sv_maxspeed;
438 // Jetpack navigation
440 if(this.navigation_jetpack_goal)
441 if(this.goalcurrent==this.navigation_jetpack_goal)
444 if(autocvar_bot_debug_goalstack)
446 debuggoalstack(this);
447 te_wizspike(this.navigation_jetpack_point);
451 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
453 // Brake almost completely so it can get a good direction
454 if(vdist(this.velocity, >, 10))
456 this.aistatus |= AI_STATUS_JETPACK_FLYING;
459 makevectors(this.v_angle.y * '0 1 0');
460 dir = normalize(this.navigation_jetpack_point - this.origin);
463 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
465 // Calculate brake distance in xy
469 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
471 v = vlen(this.velocity - this.velocity.z * '0 0 1');
472 db = ((v ** 2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
473 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
474 if(d < db || d < 500)
477 if(fabs(this.velocity.x)>maxspeed*0.3)
479 this.movement_x = dir * v_forward * -maxspeed;
482 // Switch to normal mode
483 this.navigation_jetpack_goal = NULL;
484 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
485 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
489 else if(checkpvs(this.origin,this.goalcurrent))
491 // If I can see the goal switch to landing code
492 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
493 this.aistatus |= AI_STATUS_JETPACK_LANDING;
498 PHYS_INPUT_BUTTON_HOOK(this) = true;
499 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
501 this.movement_x = dir * v_forward * maxspeed;
502 this.movement_y = dir * v_right * maxspeed;
507 // Handling of jump pads
508 if(this.jumppadcount)
510 // If got stuck on the jump pad try to reach the farthest visible waypoint
511 // but with some randomness so it can try out different paths
512 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
514 if(fabs(this.velocity.z)<50)
516 entity newgoal = NULL;
517 if (vdist(this.origin - this.goalcurrent.origin, <, 150))
518 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
519 else IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
521 if(it.wpflags & WAYPOINTFLAG_TELEPORT)
522 if(it.origin.z < this.origin.z - 100 && vdist(vec2(it.origin - this.origin), <, 100))
525 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
527 if(trace_fraction < 1)
530 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
536 this.ignoregoal = this.goalcurrent;
537 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
538 navigation_clearroute(this);
539 navigation_routetogoal(this, newgoal, this.origin);
540 if(autocvar_bot_debug_goalstack)
541 debuggoalstack(this);
542 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
550 if(time - this.lastteleporttime > 0.3 && this.velocity.z > 0)
552 vector velxy = this.velocity; velxy_z = 0;
553 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
555 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
556 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
561 // Don't chase players while using a jump pad
562 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
566 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
567 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
569 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
571 if (!(IS_ONGROUND(this)))
573 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
574 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
575 if(this.items & IT_JETPACK)
577 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
578 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
580 if(this.velocity.z<0)
582 PHYS_INPUT_BUTTON_HOOK(this) = true;
586 PHYS_INPUT_BUTTON_HOOK(this) = true;
588 // If there is no goal try to move forward
590 if(this.goalcurrent==NULL)
593 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
595 vector xyvelocity = this.velocity; xyvelocity_z = 0;
596 float xyspeed = xyvelocity * dir;
598 if(xyspeed < (maxspeed / 2))
600 makevectors(this.v_angle.y * '0 1 0');
601 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
602 if(trace_fraction==1)
604 this.movement_x = dir * v_forward * maxspeed;
605 this.movement_y = dir * v_right * maxspeed;
607 havocbot_keyboard_movement(this, this.origin + dir * 100);
611 this.havocbot_blockhead = true;
615 else if(this.health > WEP_CVAR(devastator, damage) * 0.5 * ((this.strength_finished < time) ? autocvar_g_balance_powerup_strength_selfdamage : 1))
617 if(this.velocity.z < 0)
619 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
621 .entity weaponentity = weaponentities[slot];
623 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
626 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
628 this.movement_x = maxspeed;
630 if(this.rocketjumptime)
632 if(time > this.rocketjumptime)
634 PHYS_INPUT_BUTTON_ATCK2(this) = true;
635 this.rocketjumptime = 0;
640 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
642 PHYS_INPUT_BUTTON_ATCK(this) = true;
643 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
651 // If there is no goal try to move forward
652 if(this.goalcurrent==NULL)
653 this.movement_x = maxspeed;
657 // If we are under water with no goals, swim up
659 if(this.goalcurrent==NULL)
662 if(this.waterlevel>WATERLEVEL_SWIMMING)
664 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
665 PHYS_INPUT_BUTTON_JUMP(this) = true;
667 PHYS_INPUT_BUTTON_JUMP(this) = false;
668 makevectors(this.v_angle.y * '0 1 0');
669 this.movement_x = dir * v_forward * maxspeed;
670 this.movement_y = dir * v_right * maxspeed;
671 this.movement_z = dir * v_up * maxspeed;
674 // if there is nowhere to go, exit
675 if (this.goalcurrent == NULL)
679 bool locked_goal = false;
680 if(this.goalentity && wasfreed(this.goalentity))
682 navigation_clearroute(this);
683 this.bot_strategytime = 0;
686 else if(this.goalentity.bot_pickup)
688 if(this.goalentity.bot_pickup_respawning)
690 if(this.goalentity.solid) // item respawned
691 this.goalentity.bot_pickup_respawning = false;
692 else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
694 this.goalentity.bot_pickup_respawning = false;
695 navigation_clearroute(this);
696 this.bot_strategytime = 0;
699 else if(this.goalentity == this.goalcurrent)
700 locked_goal = true; // wait for item to respawn
702 else if(!this.goalentity.solid)
704 navigation_clearroute(this);
705 this.bot_strategytime = 0;
710 navigation_poptouchedgoals(this);
712 // if ran out of goals try to use an alternative goal or get a new strategy asap
713 if(this.goalcurrent == NULL)
715 this.bot_strategytime = 0;
720 if(autocvar_bot_debug_goalstack)
721 debuggoalstack(this);
723 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
724 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
725 destorg = this.origin;
726 destorg.x = bound(m1_x, destorg.x, m2_x);
727 destorg.y = bound(m1_y, destorg.y, m2_y);
728 destorg.z = bound(m1_z, destorg.z, m2_z);
730 // in case bot ends up inside the teleport waypoint without touching
731 // the teleport itself, head to the teleport origin
732 if(destorg == this.origin)
733 destorg = this.goalcurrent.origin;
735 diff = destorg - this.origin;
737 dir = normalize(diff);
738 flatdir = diff;flatdir.z = 0;
739 flatdir = normalize(flatdir);
740 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
742 //if (this.bot_dodgevector_time < time)
744 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
745 // this.bot_dodgevector_jumpbutton = 1;
746 evadeobstacle = '0 0 0';
749 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
750 makevectors(this.v_angle.y * '0 1 0');
753 if(this.waterlevel>WATERLEVEL_SWIMMING)
756 this.aistatus |= AI_STATUS_OUT_WATER;
760 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
761 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
762 PHYS_INPUT_BUTTON_JUMP(this) = true;
764 PHYS_INPUT_BUTTON_JUMP(this) = false;
766 dir = normalize(flatdir);
772 if(this.aistatus & AI_STATUS_OUT_WATER)
773 this.aistatus &= ~AI_STATUS_OUT_WATER;
775 // jump if going toward an obstacle that doesn't look like stairs we
776 // can walk up directly
777 offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : v_forward * 32);
778 tracebox(this.origin, this.mins, this.maxs, this.origin + offset, false, this);
779 if (trace_fraction < 1)
780 if (trace_plane_normal.z < 0.7)
783 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + offset + stepheightvec, false, this);
784 if (trace_fraction < s + 0.01)
785 if (trace_plane_normal.z < 0.7)
788 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + offset + jumpstepheightvec, false, this);
789 if (trace_fraction > s)
790 PHYS_INPUT_BUTTON_JUMP(this) = true;
794 // if bot for some reason doesn't get close to the current goal find another one
795 if(!IS_PLAYER(this.goalcurrent) && !(this.goalcurrent.bot_pickup_respawning && this.goalcurrent_distance < 50))
797 float curr_dist = vlen(this.origin - this.goalcurrent.origin);
798 if(this.goalcurrent != this.goalcurrent_prev)
800 this.goalcurrent_prev = this.goalcurrent;
801 this.goalcurrent_distance = curr_dist;
802 this.goalcurrent_distance_time = 0;
804 else if(curr_dist > this.goalcurrent_distance)
806 if(!this.goalcurrent_distance_time)
807 this.goalcurrent_distance_time = time;
808 else if (time - this.goalcurrent_distance_time > 0.5)
810 this.goalcurrent_prev = NULL;
811 navigation_clearroute(this);
812 this.bot_strategytime = 0;
818 // reduce it a little bit so it works even with very small approaches to the goal
819 this.goalcurrent_distance = max(20, curr_dist - 15);
820 this.goalcurrent_distance_time = 0;
824 // Check for water/slime/lava and dangerous edges
825 // (only when the bot is on the ground or jumping intentionally)
827 vector dst_ahead = this.origin + this.view_ofs + offset;
828 vector dst_down = dst_ahead - '0 0 3000';
829 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
831 bool unreachable = false;
832 bool ignorehazards = false;
834 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
835 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
838 traceline(dst_ahead , dst_down, true, NULL);
839 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
840 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
841 if(trace_endpos.z < this.origin.z + this.mins.z)
843 s = pointcontents(trace_endpos + '0 0 1');
844 if (s != CONTENT_SOLID)
845 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
847 evadelava = normalize(this.velocity) * -1;
848 if(this.waterlevel >= WATERLEVEL_WETFEET && (this.watertype == CONTENT_LAVA || this.watertype == CONTENT_SLIME))
849 ignorehazards = true;
851 else if (s == CONTENT_WATER)
853 if(this.waterlevel >= WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER)
854 ignorehazards = true;
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))
885 this.aistatus |= AI_STATUS_DANGER_AHEAD;
886 if(IS_PLAYER(this.goalcurrent))
891 navigation_clearroute(this);
892 this.bot_strategytime = 0;
896 dodge = havocbot_dodge(this);
897 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
898 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
899 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
900 if(IS_PLAYER(trace_ent))
901 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
903 dir = normalize(dir + dodge + evadeobstacle + evadelava);
904 // this.bot_dodgevector = dir;
905 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
908 if(time < this.ladder_time)
910 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
912 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
917 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
922 //dir = this.bot_dodgevector;
923 //if (this.bot_dodgevector_jumpbutton)
924 // PHYS_INPUT_BUTTON_JUMP(this) = true;
925 this.movement_x = dir * v_forward * maxspeed;
926 this.movement_y = dir * v_right * maxspeed;
927 this.movement_z = dir * v_up * maxspeed;
929 // Emulate keyboard interface
931 havocbot_keyboard_movement(this, destorg);
934 // if(this.aistatus & AI_STATUS_ROAMING)
936 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
937 havocbot_bunnyhop(this, dir);
939 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
940 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;
941 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);
944 entity havocbot_gettarget(entity this, bool secondary)
947 vector eye = CENTER_OR_VIEWOFS(this);
948 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
950 vector v = CENTER_OR_VIEWOFS(it);
951 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
952 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
953 if(bot_shouldattack(this, it))
955 traceline(eye, v, true, this);
956 if (trace_ent == it || trace_fraction >= 1)
964 void havocbot_chooseenemy(entity this)
966 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
973 if (!bot_shouldattack(this, this.enemy))
975 // enemy died or something, find a new target
977 this.havocbot_chooseenemy_finished = time;
979 else if (this.havocbot_stickenemy)
981 // tracking last chosen enemy
982 // if enemy is visible
983 // and not really really far away
984 // and we're not severely injured
985 // then keep tracking for a half second into the future
986 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
987 if (trace_ent == this.enemy || trace_fraction == 1)
988 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
989 if (this.health > 30)
991 // remain tracking him for a shot while (case he went after a small corner or pilar
992 this.havocbot_chooseenemy_finished = time + 0.5;
995 // enemy isn't visible, or is far away, or we're injured severely
996 // so stop preferring this enemy
997 // (it will still take a half second until a new one is chosen)
998 this.havocbot_stickenemy = 0;
1001 if (time < this.havocbot_chooseenemy_finished)
1003 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
1004 vector eye = this.origin + this.view_ofs;
1006 float bestrating = 100000000;
1009 int hf = this.dphitcontentsmask;
1011 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1013 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1015 bool scan_transparent = false;
1016 bool scan_secondary_targets = false;
1017 bool have_secondary_targets = false;
1020 scan_secondary_targets = false;
1022 IL_EACH(g_bot_targets, it.bot_attack,
1024 if(!scan_secondary_targets)
1026 if(it.classname == "misc_breakablemodel")
1028 have_secondary_targets = true;
1032 else if(it.classname != "misc_breakablemodel")
1035 vector v = (it.absmin + it.absmax) * 0.5;
1036 float rating = vlen2(v - eye);
1037 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1038 if (bestrating > rating)
1039 if (bot_shouldattack(this, it))
1041 traceline(eye, v, true, this);
1042 if (trace_ent == it || trace_fraction >= 1)
1045 bestrating = rating;
1050 if(!best && have_secondary_targets && !scan_secondary_targets)
1052 scan_secondary_targets = true;
1054 bestrating = 100000000;
1058 // I want to do a second scan if no enemy was found or I don't have weapons
1059 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1060 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1062 if(scan_transparent)
1065 // Set flags to see through transparent objects
1066 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1068 scan_transparent = true;
1071 // Restore hit flags
1072 this.dphitcontentsmask = hf;
1075 this.havocbot_stickenemy = true;
1076 if(best && best.classname == "misc_breakablemodel")
1077 this.havocbot_stickenemy = false;
1080 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1082 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1083 // so skip this for them, or they'll never get to reload their weapons at all.
1084 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1088 // if this weapon is scheduled for reloading, don't switch to it during combat
1089 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1091 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1092 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1093 return true; // other weapon available
1100 void havocbot_chooseweapon(entity this, .entity weaponentity)
1105 if(g_weaponarena_weapons == WEPSET(TUBA))
1107 this.(weaponentity).m_switchweapon = WEP_TUBA;
1111 // TODO: clean this up by moving it to weapon code
1112 if(this.enemy==NULL)
1114 // If no weapon was chosen get the first available weapon
1115 if(this.(weaponentity).m_weapon==WEP_Null)
1116 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1117 if(client_hasweapon(this, it, weaponentity, true, false))
1119 this.(weaponentity).m_switchweapon = it;
1126 // Do not change weapon during the next second after a combo
1127 float f = time - this.lastcombotime;
1132 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1134 // Should it do a weapon combo?
1135 float af, ct, combo_time, combo;
1137 af = ATTACK_FINISHED(this, 0);
1138 ct = autocvar_bot_ai_weapon_combo_threshold;
1140 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1141 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1142 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1146 if(autocvar_bot_ai_weapon_combo)
1147 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1151 this.lastcombotime = time;
1154 distance *= (2 ** this.bot_rangepreference);
1156 // Custom weapon list based on distance to the enemy
1157 if(bot_custom_weapon){
1159 // Choose weapons for far distance
1160 if ( distance > bot_distance_far ) {
1161 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1162 w = bot_weapons_far[i];
1163 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1165 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1167 this.(weaponentity).m_switchweapon = Weapons_from(w);
1173 // Choose weapons for mid distance
1174 if ( distance > bot_distance_close) {
1175 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1176 w = bot_weapons_mid[i];
1177 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1179 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1181 this.(weaponentity).m_switchweapon = Weapons_from(w);
1187 // Choose weapons for close distance
1188 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1189 w = bot_weapons_close[i];
1190 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1192 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1194 this.(weaponentity).m_switchweapon = Weapons_from(w);
1201 void havocbot_aim(entity this)
1203 if (time < this.nextaim)
1205 this.nextaim = time + 0.1;
1206 vector myvel = this.velocity;
1207 if (!this.waterlevel)
1209 if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1210 else if (this.enemy)
1212 vector enemyvel = this.enemy.velocity;
1213 if (!this.enemy.waterlevel)
1215 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1218 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1221 bool havocbot_moveto_refresh_route(entity this)
1223 // Refresh path to goal if necessary
1225 wp = this.havocbot_personal_waypoint;
1226 navigation_goalrating_start(this);
1227 navigation_routerating(this, wp, 10000, 10000);
1228 navigation_goalrating_end(this);
1229 return (this.goalentity != NULL);
1232 float havocbot_moveto(entity this, vector pos)
1236 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1238 // Step 4: Move to waypoint
1239 if(this.havocbot_personal_waypoint==NULL)
1241 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1242 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1243 return CMD_STATUS_ERROR;
1246 if (!bot_strategytoken_taken)
1247 if(this.havocbot_personal_waypoint_searchtime<time)
1249 bot_strategytoken_taken = true;
1250 if(havocbot_moveto_refresh_route(this))
1252 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1253 this.havocbot_personal_waypoint_searchtime = time + 10;
1254 this.havocbot_personal_waypoint_failcounter = 0;
1258 this.havocbot_personal_waypoint_failcounter += 1;
1259 this.havocbot_personal_waypoint_searchtime = time + 2;
1260 if(this.havocbot_personal_waypoint_failcounter >= 30)
1262 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1263 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1264 delete(this.havocbot_personal_waypoint);
1265 return CMD_STATUS_ERROR;
1268 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1272 if(autocvar_bot_debug_goalstack)
1273 debuggoalstack(this);
1276 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1278 bot_aimdir(this, dir, -1);
1281 havocbot_movetogoal(this);
1283 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1285 // Step 5: Waypoint reached
1286 LOG_TRACE(this.netname, "'s personal waypoint reached");
1287 delete(this.havocbot_personal_waypoint);
1288 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1289 return CMD_STATUS_FINISHED;
1292 return CMD_STATUS_EXECUTING;
1295 // Step 2: Linking waypoint
1296 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1298 // Wait until it is linked
1299 if(!this.havocbot_personal_waypoint.wplinked)
1301 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1302 return CMD_STATUS_EXECUTING;
1305 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1306 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1307 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1309 // Step 3: Route to waypoint
1310 LOG_TRACE(this.netname, " walking to its personal waypoint");
1312 return CMD_STATUS_EXECUTING;
1315 // Step 1: Spawning waypoint
1316 wp = waypoint_spawnpersonal(this, pos);
1319 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1320 return CMD_STATUS_ERROR;
1323 this.havocbot_personal_waypoint = wp;
1324 this.havocbot_personal_waypoint_failcounter = 0;
1325 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1327 // if pos is inside a teleport, then let's mark it as teleport waypoint
1328 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1330 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1331 this.lastteleporttime = 0;
1335 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1336 print("routing to a teleporter\n");
1338 print("routing to a non-teleporter\n");
1341 return CMD_STATUS_EXECUTING;
1344 float havocbot_resetgoal(entity this)
1346 navigation_clearroute(this);
1347 return CMD_STATUS_FINISHED;
1350 void havocbot_setupbot(entity this)
1352 this.bot_ai = havocbot_ai;
1353 this.cmd_moveto = havocbot_moveto;
1354 this.cmd_resetgoal = havocbot_resetgoal;
1356 havocbot_chooserole(this);
1359 vector havocbot_dodge(entity this)
1361 // LordHavoc: disabled because this is too expensive
1366 float danger, bestdanger, vl, d;
1369 // check for dangerous objects near bot or approaching bot
1370 head = findchainfloat(bot_dodge, true);
1373 if (head.owner != this)
1375 vl = vlen(head.velocity);
1376 if (vl > autocvar_sv_maxspeed * 0.3)
1378 n = normalize(head.velocity);
1379 v = this.origin - head.origin;
1381 if (d > (0 - head.bot_dodgerating))
1382 if (d < (vl * 0.2 + head.bot_dodgerating))
1384 // calculate direction and distance from the flight path, by removing the forward axis
1385 v = v - (n * (v * n));
1386 danger = head.bot_dodgerating - vlen(v);
1387 if (bestdanger < danger)
1389 bestdanger = danger;
1390 // dodge to the side of the object
1391 dodge = normalize(v);
1397 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1398 if (bestdanger < danger)
1400 bestdanger = danger;
1401 dodge = normalize(this.origin - head.origin);