7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
11 #include <common/constants.qh>
12 #include <common/net_linked.qh>
13 #include <common/physics/player.qh>
14 #include <common/state.qh>
15 #include <common/items/_mod.qh>
17 #include <common/triggers/teleporters.qh>
18 #include <common/triggers/trigger/jumppads.qh>
20 #include <lib/warpzone/common.qh>
24 void havocbot_ai(entity this)
29 if(bot_execute_commands(this))
32 while(this.goalcurrent && wasfreed(this.goalcurrent))
34 navigation_poproute(this);
36 this.bot_strategytime = 0;
39 if (bot_strategytoken == this)
40 if (!bot_strategytoken_taken)
42 if(this.havocbot_blockhead)
44 this.havocbot_blockhead = false;
48 if (!this.jumppadcount)
49 this.havocbot_role(this); // little too far down the rabbit hole
52 // TODO: tracewalk() should take care of this job (better path finding under water)
53 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
56 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
58 // Look for the closest waypoint out of water
59 entity newgoal = NULL;
60 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
62 if(it.origin.z < this.origin.z)
65 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
68 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
71 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
73 if(trace_fraction < 1)
76 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
82 // te_wizspike(newgoal.origin);
83 navigation_pushroute(this, newgoal);
87 // token has been used this frame
88 bot_strategytoken_taken = true;
94 havocbot_chooseenemy(this);
95 if (this.bot_chooseweapontime < time )
97 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
98 havocbot_chooseweapon(this);
102 if (this.bot_aimtarg)
104 this.aistatus |= AI_STATUS_ATTACKING;
105 this.aistatus &= ~AI_STATUS_ROAMING;
109 Weapon w = PS(this).m_weapon;
111 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
113 PHYS_INPUT_BUTTON_ATCK(this) = false;
114 PHYS_INPUT_BUTTON_ATCK2(this) = false;
118 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
119 this.lastfiredweapon = PS(this).m_weapon.m_id;
124 if(IS_PLAYER(this.bot_aimtarg))
125 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
128 else if (this.goalcurrent)
130 this.aistatus |= AI_STATUS_ROAMING;
131 this.aistatus &= ~AI_STATUS_ATTACKING;
133 vector now,v,next;//,heading;
134 float aimdistance,skillblend,distanceblend,blend;
135 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
136 aimdistance = vlen(now);
137 //heading = this.velocity;
138 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
140 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
141 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
143 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
145 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
146 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
147 blend = skillblend * (1-distanceblend);
148 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
149 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
150 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
151 v = now + blend * (next - now);
152 //dprint(etos(this), " ");
153 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
154 //v = now * (distanceblend) + next * (1-distanceblend);
155 if (this.waterlevel < WATERLEVEL_SWIMMING)
157 //dprint("walk at:", vtos(v), "\n");
158 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
159 bot_aimdir(this, v, -1);
161 havocbot_movetogoal(this);
163 // if the bot is not attacking, consider reloading weapons
164 if (!(this.aistatus & AI_STATUS_ATTACKING))
166 // we are currently holding a weapon that's not fully loaded, reload it
167 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
168 if(this.clip_load < this.clip_size)
169 this.impulse = 20; // "press" the reload button, not sure if this is done right
171 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
172 // the code above executes next frame, starting the reloading then
173 if(skill >= 5) // bots can only look for unloaded weapons past this skill
174 if(this.clip_load >= 0) // only if we're not reloading a weapon already
176 FOREACH(Weapons, it != WEP_Null, LAMBDA(
177 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
178 PS(this).m_switchweapon = it;
184 void havocbot_keyboard_movement(entity this, vector destorg)
187 float blend, maxspeed;
190 sk = skill + this.bot_moveskill;
192 maxspeed = autocvar_sv_maxspeed;
194 if (time < this.havocbot_keyboardtime)
197 this.havocbot_keyboardtime =
199 this.havocbot_keyboardtime
200 + 0.05/max(1, sk+this.havocbot_keyboardskill)
201 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
203 keyboard = this.movement * (1.0 / maxspeed);
205 float trigger, trigger1;
206 blend = bound(0,sk*0.1,1);
207 trigger = autocvar_bot_ai_keyboard_threshold;
208 trigger1 = 0 - trigger;
210 // categorize forward movement
211 // at skill < 1.5 only forward
212 // at skill < 2.5 only individual directions
213 // at skill < 4.5 only individual directions, and forward diagonals
214 // at skill >= 4.5, all cases allowed
215 if (keyboard.x > trigger)
221 else if (keyboard.x < trigger1 && sk > 1.5)
236 if (keyboard.y > trigger)
238 else if (keyboard.y < trigger1)
243 if (keyboard.z > trigger)
245 else if (keyboard.z < trigger1)
250 this.havocbot_keyboard = keyboard * maxspeed;
251 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
253 keyboard = this.havocbot_keyboard;
254 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
255 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
256 this.movement = this.movement + (keyboard - this.movement) * blend;
259 void havocbot_bunnyhop(entity this, vector dir)
261 float bunnyhopdistance;
266 // Don't jump when attacking
267 if(this.aistatus & AI_STATUS_ATTACKING)
270 if(IS_PLAYER(this.goalcurrent))
273 maxspeed = autocvar_sv_maxspeed;
275 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
277 this.aistatus &= ~AI_STATUS_RUNNING;
278 PHYS_INPUT_BUTTON_JUMP(this) = false;
279 this.bot_canruntogoal = 0;
280 this.bot_timelastseengoal = 0;
284 if(this.waterlevel > WATERLEVEL_WETFEET)
286 this.aistatus &= ~AI_STATUS_RUNNING;
290 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
292 this.bot_canruntogoal = 0;
293 this.bot_timelastseengoal = 0;
296 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
297 bunnyhopdistance = vlen(this.origin - gco);
299 // Run only to visible goals
300 if(IS_ONGROUND(this))
301 if(this.speed==maxspeed)
302 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
304 this.bot_lastseengoal = this.goalcurrent;
307 if(this.bot_timelastseengoal)
309 // for a period of time
310 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
313 checkdistance = true;
315 // don't run if it is too close
316 if(this.bot_canruntogoal==0)
318 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
319 this.bot_canruntogoal = 1;
321 this.bot_canruntogoal = -1;
324 if(this.bot_canruntogoal != 1)
327 if(this.aistatus & AI_STATUS_ROAMING)
328 if(this.goalcurrent.classname=="waypoint")
329 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
330 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
331 if(this.goalstack01 && !wasfreed(this.goalstack01))
333 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
334 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
335 while (deviation.y < -180) deviation.y = deviation.y + 360;
336 while (deviation.y > 180) deviation.y = deviation.y - 360;
338 if(fabs(deviation.y) < 20)
339 if(bunnyhopdistance < vlen(this.origin - gno))
340 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
342 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
343 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
345 checkdistance = false;
352 this.aistatus &= ~AI_STATUS_RUNNING;
353 // increase stop distance in case the goal is on a slope or a lower platform
354 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance + (this.origin.z - gco.z))
355 PHYS_INPUT_BUTTON_JUMP(this) = true;
359 this.aistatus |= AI_STATUS_RUNNING;
360 PHYS_INPUT_BUTTON_JUMP(this) = true;
366 this.bot_timelastseengoal = time;
371 this.bot_timelastseengoal = 0;
375 // Release jump button
376 if(!cvar("sv_pogostick"))
377 if((IS_ONGROUND(this)) == 0)
379 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
380 PHYS_INPUT_BUTTON_JUMP(this) = false;
383 if(this.aistatus & AI_STATUS_RUNNING)
384 if(vlen(this.velocity)>maxspeed)
386 deviation = vectoangles(dir) - vectoangles(this.velocity);
387 while (deviation.y < -180) deviation.y = deviation.y + 360;
388 while (deviation.y > 180) deviation.y = deviation.y - 360;
390 if(fabs(deviation.y)>10)
394 this.movement_y = maxspeed * -1;
395 else if(deviation.y<10)
396 this.movement_y = maxspeed;
403 void havocbot_movetogoal(entity this)
411 vector evadeobstacle;
418 //if (this.goalentity)
419 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
420 this.movement = '0 0 0';
421 maxspeed = autocvar_sv_maxspeed;
423 // Jetpack navigation
425 if(this.navigation_jetpack_goal)
426 if(this.goalcurrent==this.navigation_jetpack_goal)
429 if(autocvar_bot_debug_goalstack)
431 debuggoalstack(this);
432 te_wizspike(this.navigation_jetpack_point);
436 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
438 // Brake almost completely so it can get a good direction
439 if(vdist(this.velocity, >, 10))
441 this.aistatus |= AI_STATUS_JETPACK_FLYING;
444 makevectors(this.v_angle.y * '0 1 0');
445 dir = normalize(this.navigation_jetpack_point - this.origin);
448 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
450 // Calculate brake distance in xy
454 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
456 v = vlen(this.velocity - this.velocity.z * '0 0 1');
457 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
458 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
459 if(d < db || d < 500)
462 if(fabs(this.velocity.x)>maxspeed*0.3)
464 this.movement_x = dir * v_forward * -maxspeed;
467 // Switch to normal mode
468 this.navigation_jetpack_goal = NULL;
469 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
470 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
474 else if(checkpvs(this.origin,this.goalcurrent))
476 // If I can see the goal switch to landing code
477 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
478 this.aistatus |= AI_STATUS_JETPACK_LANDING;
483 PHYS_INPUT_BUTTON_HOOK(this) = true;
484 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
486 this.movement_x = dir * v_forward * maxspeed;
487 this.movement_y = dir * v_right * maxspeed;
492 // Handling of jump pads
493 if(this.jumppadcount)
495 // If got stuck on the jump pad try to reach the farthest visible waypoint
496 // but with some randomness so it can try out different paths
497 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
499 if(fabs(this.velocity.z)<50)
501 entity newgoal = NULL;
502 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
504 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
506 if(trace_fraction < 1)
509 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
515 this.ignoregoal = this.goalcurrent;
516 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
517 navigation_clearroute(this);
518 navigation_routetogoal(this, newgoal, this.origin);
519 if(autocvar_bot_debug_goalstack)
520 debuggoalstack(this);
521 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
529 if(this.velocity.z>0)
532 vector velxy = this.velocity; velxy_z = 0;
533 threshold = maxspeed * 0.2;
534 if(vdist(velxy, <, threshold))
536 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
537 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
542 // Don't chase players while using a jump pad
543 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
547 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
548 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
550 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
552 if (!(IS_ONGROUND(this)))
554 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
555 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
556 if(this.items & IT_JETPACK)
558 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
559 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
561 if(this.velocity.z<0)
563 PHYS_INPUT_BUTTON_HOOK(this) = true;
567 PHYS_INPUT_BUTTON_HOOK(this) = true;
569 // If there is no goal try to move forward
571 if(this.goalcurrent==NULL)
574 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
576 vector xyvelocity = this.velocity; xyvelocity_z = 0;
577 float xyspeed = xyvelocity * dir;
579 if(xyspeed < (maxspeed / 2))
581 makevectors(this.v_angle.y * '0 1 0');
582 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
583 if(trace_fraction==1)
585 this.movement_x = dir * v_forward * maxspeed;
586 this.movement_y = dir * v_right * maxspeed;
588 havocbot_keyboard_movement(this, this.origin + dir * 100);
592 this.havocbot_blockhead = true;
596 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
598 if(this.velocity.z < 0)
599 if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
601 this.movement_x = maxspeed;
603 if(this.rocketjumptime)
605 if(time > this.rocketjumptime)
607 PHYS_INPUT_BUTTON_ATCK2(this) = true;
608 this.rocketjumptime = 0;
613 PS(this).m_switchweapon = WEP_DEVASTATOR;
615 PHYS_INPUT_BUTTON_ATCK(this) = true;
616 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
622 // If there is no goal try to move forward
623 if(this.goalcurrent==NULL)
624 this.movement_x = maxspeed;
628 // If we are under water with no goals, swim up
630 if(this.goalcurrent==NULL)
633 if(this.waterlevel>WATERLEVEL_SWIMMING)
635 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
636 PHYS_INPUT_BUTTON_JUMP(this) = true;
638 PHYS_INPUT_BUTTON_JUMP(this) = false;
639 makevectors(this.v_angle.y * '0 1 0');
640 this.movement_x = dir * v_forward * maxspeed;
641 this.movement_y = dir * v_right * maxspeed;
642 this.movement_z = dir * v_up * maxspeed;
645 // if there is nowhere to go, exit
646 if (this.goalcurrent == NULL)
649 if (this.goalcurrent)
650 navigation_poptouchedgoals(this);
652 // if ran out of goals try to use an alternative goal or get a new strategy asap
653 if(this.goalcurrent == NULL)
655 this.bot_strategytime = 0;
660 if(autocvar_bot_debug_goalstack)
661 debuggoalstack(this);
663 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
664 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
665 destorg = this.origin;
666 destorg.x = bound(m1_x, destorg.x, m2_x);
667 destorg.y = bound(m1_y, destorg.y, m2_y);
668 destorg.z = bound(m1_z, destorg.z, m2_z);
669 diff = destorg - this.origin;
671 dir = normalize(diff);
672 flatdir = diff;flatdir.z = 0;
673 flatdir = normalize(flatdir);
674 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
676 //if (this.bot_dodgevector_time < time)
678 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
679 // this.bot_dodgevector_jumpbutton = 1;
680 evadeobstacle = '0 0 0';
685 if(this.waterlevel>WATERLEVEL_SWIMMING)
688 this.aistatus |= AI_STATUS_OUT_WATER;
692 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
693 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
694 PHYS_INPUT_BUTTON_JUMP(this) = true;
696 PHYS_INPUT_BUTTON_JUMP(this) = false;
698 dir = normalize(flatdir);
699 makevectors(this.v_angle.y * '0 1 0');
703 if(this.aistatus & AI_STATUS_OUT_WATER)
704 this.aistatus &= ~AI_STATUS_OUT_WATER;
706 // jump if going toward an obstacle that doesn't look like stairs we
707 // can walk up directly
708 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
709 if (trace_fraction < 1)
710 if (trace_plane_normal.z < 0.7)
713 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
714 if (trace_fraction < s + 0.01)
715 if (trace_plane_normal.z < 0.7)
718 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
719 if (trace_fraction > s)
720 PHYS_INPUT_BUTTON_JUMP(this) = true;
724 // avoiding dangers and obstacles
725 vector dst_ahead = this.origin + this.view_ofs + this.velocity * 0.5;
726 vector dst_down = dst_ahead - '0 0 3000';
729 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
731 // Check head-banging against walls
732 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
734 PHYS_INPUT_BUTTON_JUMP(this) = true;
735 if(this.facingwalltime && time > this.facingwalltime)
737 this.ignoregoal = this.goalcurrent;
738 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
739 this.bot_strategytime = 0;
744 this.facingwalltime = time + 0.05;
749 this.facingwalltime = 0;
751 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
753 this.ignoregoal = NULL;
754 this.ignoregoaltime = 0;
758 // Check for water/slime/lava and dangerous edges
759 // (only when the bot is on the ground or jumping intentionally)
760 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
762 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
763 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
766 traceline(dst_ahead , dst_down, true, NULL);
767 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
768 //te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
769 if(trace_endpos.z < this.origin.z + this.mins.z)
771 s = pointcontents(trace_endpos + '0 0 1');
772 if (s != CONTENT_SOLID)
773 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
774 evadelava = normalize(this.velocity) * -1;
775 else if (s == CONTENT_SKY)
776 evadeobstacle = normalize(this.velocity) * -1;
779 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
780 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
782 if (gco.z > this.origin.z + jumpstepheightvec.z)
784 // the goal is probably on an upper platform, assume bot can't get there
785 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
786 navigation_clearroute(this);
787 this.bot_strategytime = 0;
790 evadelava = normalize(this.velocity) * -1;
799 makevectors(this.v_angle.y * '0 1 0');
801 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
802 this.aistatus |= AI_STATUS_DANGER_AHEAD;
805 dodge = havocbot_dodge(this);
806 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
807 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
808 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
809 if(IS_PLAYER(trace_ent))
810 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
812 dir = normalize(dir + dodge + evadeobstacle + evadelava);
813 // this.bot_dodgevector = dir;
814 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
817 if(time < this.ladder_time)
819 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
821 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
826 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
831 //dir = this.bot_dodgevector;
832 //if (this.bot_dodgevector_jumpbutton)
833 // PHYS_INPUT_BUTTON_JUMP(this) = true;
834 this.movement_x = dir * v_forward * maxspeed;
835 this.movement_y = dir * v_right * maxspeed;
836 this.movement_z = dir * v_up * maxspeed;
838 // Emulate keyboard interface
840 havocbot_keyboard_movement(this, destorg);
843 // if(this.aistatus & AI_STATUS_ROAMING)
845 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
846 havocbot_bunnyhop(this, dir);
848 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
849 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;
850 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);
853 entity havocbot_gettarget(entity this, bool secondary)
856 vector eye = CENTER_OR_VIEWOFS(this);
857 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
859 vector v = CENTER_OR_VIEWOFS(it);
860 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
861 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
862 if(bot_shouldattack(this, it))
864 traceline(eye, v, true, this);
865 if (trace_ent == it || trace_fraction >= 1)
873 void havocbot_chooseenemy(entity this)
875 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
882 if (!bot_shouldattack(this, this.enemy))
884 // enemy died or something, find a new target
886 this.havocbot_chooseenemy_finished = time;
888 else if (this.havocbot_stickenemy)
890 // tracking last chosen enemy
891 // if enemy is visible
892 // and not really really far away
893 // and we're not severely injured
894 // then keep tracking for a half second into the future
895 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
896 if (trace_ent == this.enemy || trace_fraction == 1)
897 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
898 if (this.health > 30)
900 // remain tracking him for a shot while (case he went after a small corner or pilar
901 this.havocbot_chooseenemy_finished = time + 0.5;
904 // enemy isn't visible, or is far away, or we're injured severely
905 // so stop preferring this enemy
906 // (it will still take a half second until a new one is chosen)
907 this.havocbot_stickenemy = 0;
910 if (time < this.havocbot_chooseenemy_finished)
912 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
913 vector eye = this.origin + this.view_ofs;
915 float bestrating = 100000000;
918 int hf = this.dphitcontentsmask;
920 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
922 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
924 bool scan_transparent = false;
925 bool scan_secondary_targets = false;
926 bool have_secondary_targets = false;
929 scan_secondary_targets = false;
931 IL_EACH(g_bot_targets, it.bot_attack,
933 if(!scan_secondary_targets)
935 if(it.classname == "misc_breakablemodel")
937 have_secondary_targets = true;
941 else if(it.classname != "misc_breakablemodel")
944 vector v = (it.absmin + it.absmax) * 0.5;
945 float rating = vlen2(v - eye);
946 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
947 if (bestrating > rating)
948 if (bot_shouldattack(this, it))
950 traceline(eye, v, true, this);
951 if (trace_ent == it || trace_fraction >= 1)
959 if(!best && have_secondary_targets && !scan_secondary_targets)
961 scan_secondary_targets = true;
963 bestrating = 100000000;
967 // I want to do a second scan if no enemy was found or I don't have weapons
968 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
969 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
974 // Set flags to see through transparent objects
975 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
977 scan_transparent = true;
981 this.dphitcontentsmask = hf;
984 this.havocbot_stickenemy = true;
985 if(best && best.classname == "misc_breakablemodel")
986 this.havocbot_stickenemy = false;
989 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
991 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
992 // so skip this for them, or they'll never get to reload their weapons at all.
993 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
997 // if this weapon is scheduled for reloading, don't switch to it during combat
998 if (this.weapon_load[new_weapon] < 0)
1000 bool other_weapon_available = false;
1001 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1002 if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
1003 other_weapon_available = true;
1005 if(other_weapon_available)
1012 void havocbot_chooseweapon(entity this)
1017 if(g_weaponarena_weapons == WEPSET(TUBA))
1019 PS(this).m_switchweapon = WEP_TUBA;
1023 // TODO: clean this up by moving it to weapon code
1024 if(this.enemy==NULL)
1026 // If no weapon was chosen get the first available weapon
1027 if(PS(this).m_weapon==WEP_Null)
1028 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1029 if(client_hasweapon(this, it, true, false))
1031 PS(this).m_switchweapon = it;
1038 // Do not change weapon during the next second after a combo
1039 float f = time - this.lastcombotime;
1044 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1046 // Should it do a weapon combo?
1047 float af, ct, combo_time, combo;
1049 af = ATTACK_FINISHED(this, 0);
1050 ct = autocvar_bot_ai_weapon_combo_threshold;
1052 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1053 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1054 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1058 if(autocvar_bot_ai_weapon_combo)
1059 if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1063 this.lastcombotime = time;
1066 distance *= pow(2, this.bot_rangepreference);
1068 // Custom weapon list based on distance to the enemy
1069 if(bot_custom_weapon){
1071 // Choose weapons for far distance
1072 if ( distance > bot_distance_far ) {
1073 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1074 w = bot_weapons_far[i];
1075 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1077 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1079 PS(this).m_switchweapon = Weapons_from(w);
1085 // Choose weapons for mid distance
1086 if ( distance > bot_distance_close) {
1087 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1088 w = bot_weapons_mid[i];
1089 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1091 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1093 PS(this).m_switchweapon = Weapons_from(w);
1099 // Choose weapons for close distance
1100 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1101 w = bot_weapons_close[i];
1102 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1104 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1106 PS(this).m_switchweapon = Weapons_from(w);
1113 void havocbot_aim(entity this)
1115 vector myvel, enemyvel;
1116 // if(this.flags & FL_INWATER)
1118 if (time < this.nextaim)
1120 this.nextaim = time + 0.1;
1121 myvel = this.velocity;
1122 if (!this.waterlevel)
1126 enemyvel = this.enemy.velocity;
1127 if (!this.enemy.waterlevel)
1129 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1132 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1135 bool havocbot_moveto_refresh_route(entity this)
1137 // Refresh path to goal if necessary
1139 wp = this.havocbot_personal_waypoint;
1140 navigation_goalrating_start(this);
1141 navigation_routerating(this, wp, 10000, 10000);
1142 navigation_goalrating_end(this);
1143 return this.navigation_hasgoals;
1146 float havocbot_moveto(entity this, vector pos)
1150 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1152 // Step 4: Move to waypoint
1153 if(this.havocbot_personal_waypoint==NULL)
1155 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1156 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1157 return CMD_STATUS_ERROR;
1160 if (!bot_strategytoken_taken)
1161 if(this.havocbot_personal_waypoint_searchtime<time)
1163 bot_strategytoken_taken = true;
1164 if(havocbot_moveto_refresh_route(this))
1166 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1167 this.havocbot_personal_waypoint_searchtime = time + 10;
1168 this.havocbot_personal_waypoint_failcounter = 0;
1172 this.havocbot_personal_waypoint_failcounter += 1;
1173 this.havocbot_personal_waypoint_searchtime = time + 2;
1174 if(this.havocbot_personal_waypoint_failcounter >= 30)
1176 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1177 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1178 delete(this.havocbot_personal_waypoint);
1179 return CMD_STATUS_ERROR;
1182 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1186 if(autocvar_bot_debug_goalstack)
1187 debuggoalstack(this);
1190 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1192 bot_aimdir(this, dir, -1);
1195 havocbot_movetogoal(this);
1197 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1199 // Step 5: Waypoint reached
1200 LOG_TRACE(this.netname, "'s personal waypoint reached");
1201 delete(this.havocbot_personal_waypoint);
1202 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1203 return CMD_STATUS_FINISHED;
1206 return CMD_STATUS_EXECUTING;
1209 // Step 2: Linking waypoint
1210 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1212 // Wait until it is linked
1213 if(!this.havocbot_personal_waypoint.wplinked)
1215 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1216 return CMD_STATUS_EXECUTING;
1219 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1220 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1221 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1223 // Step 3: Route to waypoint
1224 LOG_TRACE(this.netname, " walking to its personal waypoint");
1226 return CMD_STATUS_EXECUTING;
1229 // Step 1: Spawning waypoint
1230 wp = waypoint_spawnpersonal(this, pos);
1233 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1234 return CMD_STATUS_ERROR;
1237 this.havocbot_personal_waypoint = wp;
1238 this.havocbot_personal_waypoint_failcounter = 0;
1239 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1241 // if pos is inside a teleport, then let's mark it as teleport waypoint
1242 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1244 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1245 this.lastteleporttime = 0;
1249 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1250 print("routing to a teleporter\n");
1252 print("routing to a non-teleporter\n");
1255 return CMD_STATUS_EXECUTING;
1258 float havocbot_resetgoal(entity this)
1260 navigation_clearroute(this);
1261 return CMD_STATUS_FINISHED;
1264 void havocbot_setupbot(entity this)
1266 this.bot_ai = havocbot_ai;
1267 this.cmd_moveto = havocbot_moveto;
1268 this.cmd_resetgoal = havocbot_resetgoal;
1270 havocbot_chooserole(this);
1273 vector havocbot_dodge(entity this)
1275 // LordHavoc: disabled because this is too expensive
1280 float danger, bestdanger, vl, d;
1283 // check for dangerous objects near bot or approaching bot
1284 head = findchainfloat(bot_dodge, true);
1287 if (head.owner != this)
1289 vl = vlen(head.velocity);
1290 if (vl > autocvar_sv_maxspeed * 0.3)
1292 n = normalize(head.velocity);
1293 v = this.origin - head.origin;
1295 if (d > (0 - head.bot_dodgerating))
1296 if (d < (vl * 0.2 + head.bot_dodgerating))
1298 // calculate direction and distance from the flight path, by removing the forward axis
1299 v = v - (n * (v * n));
1300 danger = head.bot_dodgerating - vlen(v);
1301 if (bestdanger < danger)
1303 bestdanger = danger;
1304 // dodge to the side of the object
1305 dodge = normalize(v);
1311 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1312 if (bestdanger < danger)
1314 bestdanger = danger;
1315 dodge = normalize(this.origin - head.origin);