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 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
354 PHYS_INPUT_BUTTON_JUMP(this) = true;
358 this.aistatus |= AI_STATUS_RUNNING;
359 PHYS_INPUT_BUTTON_JUMP(this) = true;
365 this.bot_timelastseengoal = time;
370 this.bot_timelastseengoal = 0;
374 // Release jump button
375 if(!cvar("sv_pogostick"))
376 if((IS_ONGROUND(this)) == 0)
378 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
379 PHYS_INPUT_BUTTON_JUMP(this) = false;
382 if(this.aistatus & AI_STATUS_RUNNING)
383 if(vlen(this.velocity)>maxspeed)
385 deviation = vectoangles(dir) - vectoangles(this.velocity);
386 while (deviation.y < -180) deviation.y = deviation.y + 360;
387 while (deviation.y > 180) deviation.y = deviation.y - 360;
389 if(fabs(deviation.y)>10)
393 this.movement_y = maxspeed * -1;
394 else if(deviation.y<10)
395 this.movement_y = maxspeed;
402 void havocbot_movetogoal(entity this)
410 vector evadeobstacle;
417 //if (this.goalentity)
418 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
419 this.movement = '0 0 0';
420 maxspeed = autocvar_sv_maxspeed;
422 // Jetpack navigation
424 if(this.navigation_jetpack_goal)
425 if(this.goalcurrent==this.navigation_jetpack_goal)
428 if(autocvar_bot_debug_goalstack)
430 debuggoalstack(this);
431 te_wizspike(this.navigation_jetpack_point);
435 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
437 // Brake almost completely so it can get a good direction
438 if(vdist(this.velocity, >, 10))
440 this.aistatus |= AI_STATUS_JETPACK_FLYING;
443 makevectors(this.v_angle.y * '0 1 0');
444 dir = normalize(this.navigation_jetpack_point - this.origin);
447 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
449 // Calculate brake distance in xy
453 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
455 v = vlen(this.velocity - this.velocity.z * '0 0 1');
456 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
457 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
458 if(d < db || d < 500)
461 if(fabs(this.velocity.x)>maxspeed*0.3)
463 this.movement_x = dir * v_forward * -maxspeed;
466 // Switch to normal mode
467 this.navigation_jetpack_goal = NULL;
468 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
469 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
473 else if(checkpvs(this.origin,this.goalcurrent))
475 // If I can see the goal switch to landing code
476 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
477 this.aistatus |= AI_STATUS_JETPACK_LANDING;
482 PHYS_INPUT_BUTTON_HOOK(this) = true;
483 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
485 this.movement_x = dir * v_forward * maxspeed;
486 this.movement_y = dir * v_right * maxspeed;
491 // Handling of jump pads
492 if(this.jumppadcount)
494 // If got stuck on the jump pad try to reach the farthest visible waypoint
495 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
497 if(fabs(this.velocity.z)<50)
499 entity newgoal = NULL;
500 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
502 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
504 if(trace_fraction < 1)
507 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
513 this.ignoregoal = this.goalcurrent;
514 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
515 navigation_clearroute(this);
516 navigation_routetogoal(this, newgoal, this.origin);
517 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
525 if(this.velocity.z>0)
528 vector velxy = this.velocity; velxy_z = 0;
529 threshold = maxspeed * 0.2;
530 if(vdist(velxy, <, threshold))
532 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
533 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
538 // Don't chase players while using a jump pad
539 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
543 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
544 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
546 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
548 if (!(IS_ONGROUND(this)))
550 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
551 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
552 if(this.items & IT_JETPACK)
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 + '0 0 1' ))
557 if(this.velocity.z<0)
559 PHYS_INPUT_BUTTON_HOOK(this) = true;
563 PHYS_INPUT_BUTTON_HOOK(this) = true;
565 // If there is no goal try to move forward
567 if(this.goalcurrent==NULL)
570 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
572 vector xyvelocity = this.velocity; xyvelocity_z = 0;
573 float xyspeed = xyvelocity * dir;
575 if(xyspeed < (maxspeed / 2))
577 makevectors(this.v_angle.y * '0 1 0');
578 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
579 if(trace_fraction==1)
581 this.movement_x = dir * v_forward * maxspeed;
582 this.movement_y = dir * v_right * maxspeed;
584 havocbot_keyboard_movement(this, this.origin + dir * 100);
588 this.havocbot_blockhead = true;
592 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
594 if(this.velocity.z < 0)
595 if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
597 this.movement_x = maxspeed;
599 if(this.rocketjumptime)
601 if(time > this.rocketjumptime)
603 PHYS_INPUT_BUTTON_ATCK2(this) = true;
604 this.rocketjumptime = 0;
609 PS(this).m_switchweapon = WEP_DEVASTATOR;
611 PHYS_INPUT_BUTTON_ATCK(this) = true;
612 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
618 // If there is no goal try to move forward
619 if(this.goalcurrent==NULL)
620 this.movement_x = maxspeed;
624 // If we are under water with no goals, swim up
626 if(this.goalcurrent==NULL)
629 if(this.waterlevel>WATERLEVEL_SWIMMING)
631 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
632 PHYS_INPUT_BUTTON_JUMP(this) = true;
634 PHYS_INPUT_BUTTON_JUMP(this) = false;
635 makevectors(this.v_angle.y * '0 1 0');
636 this.movement_x = dir * v_forward * maxspeed;
637 this.movement_y = dir * v_right * maxspeed;
638 this.movement_z = dir * v_up * maxspeed;
641 // if there is nowhere to go, exit
642 if (this.goalcurrent == NULL)
645 if (this.goalcurrent)
646 navigation_poptouchedgoals(this);
648 // if ran out of goals try to use an alternative goal or get a new strategy asap
649 if(this.goalcurrent == NULL)
651 this.bot_strategytime = 0;
656 if(autocvar_bot_debug_goalstack)
657 debuggoalstack(this);
659 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
660 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
661 destorg = this.origin;
662 destorg.x = bound(m1_x, destorg.x, m2_x);
663 destorg.y = bound(m1_y, destorg.y, m2_y);
664 destorg.z = bound(m1_z, destorg.z, m2_z);
665 diff = destorg - this.origin;
667 dir = normalize(diff);
668 flatdir = diff;flatdir.z = 0;
669 flatdir = normalize(flatdir);
670 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
672 //if (this.bot_dodgevector_time < time)
674 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
675 // this.bot_dodgevector_jumpbutton = 1;
676 evadeobstacle = '0 0 0';
681 if(this.waterlevel>WATERLEVEL_SWIMMING)
684 this.aistatus |= AI_STATUS_OUT_WATER;
688 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
689 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
690 PHYS_INPUT_BUTTON_JUMP(this) = true;
692 PHYS_INPUT_BUTTON_JUMP(this) = false;
694 dir = normalize(flatdir);
695 makevectors(this.v_angle.y * '0 1 0');
699 if(this.aistatus & AI_STATUS_OUT_WATER)
700 this.aistatus &= ~AI_STATUS_OUT_WATER;
702 // jump if going toward an obstacle that doesn't look like stairs we
703 // can walk up directly
704 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
705 if (trace_fraction < 1)
706 if (trace_plane_normal.z < 0.7)
709 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
710 if (trace_fraction < s + 0.01)
711 if (trace_plane_normal.z < 0.7)
714 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
715 if (trace_fraction > s)
716 PHYS_INPUT_BUTTON_JUMP(this) = true;
720 // avoiding dangers and obstacles
721 vector dst_ahead, dst_down;
722 makevectors(this.v_angle.y * '0 1 0');
723 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
724 dst_down = dst_ahead - '0 0 1500';
727 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
729 // Check head-banging against walls
730 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
732 PHYS_INPUT_BUTTON_JUMP(this) = true;
733 if(this.facingwalltime && time > this.facingwalltime)
735 this.ignoregoal = this.goalcurrent;
736 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
737 this.bot_strategytime = 0;
742 this.facingwalltime = time + 0.05;
747 this.facingwalltime = 0;
749 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
751 this.ignoregoal = NULL;
752 this.ignoregoaltime = 0;
756 // Check for water/slime/lava and dangerous edges
757 // (only when the bot is on the ground or jumping intentionally)
758 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
760 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
761 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
764 traceline(dst_ahead , dst_down, true, NULL);
765 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
766 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
767 if(trace_endpos.z < this.origin.z + this.mins.z)
769 s = pointcontents(trace_endpos + '0 0 1');
770 if (s != CONTENT_SOLID)
771 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
772 evadelava = normalize(this.velocity) * -1;
773 else if (s == CONTENT_SKY)
774 evadeobstacle = normalize(this.velocity) * -1;
775 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
776 this.goalcurrent.absmin, this.goalcurrent.absmax))
778 // if ain't a safe goal with "holes" (like the jumpad on soylent)
779 // and there is a trigger_hurt below
780 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
782 // Remove dangerous dynamic goals from stack
783 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
784 navigation_clearroute(this);
794 makevectors(this.v_angle.y * '0 1 0');
796 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
797 this.aistatus |= AI_STATUS_DANGER_AHEAD;
800 dodge = havocbot_dodge(this);
801 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
802 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
803 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
804 if(IS_PLAYER(trace_ent))
805 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
807 dir = normalize(dir + dodge + evadeobstacle + evadelava);
808 // this.bot_dodgevector = dir;
809 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
812 if(time < this.ladder_time)
814 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
816 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
821 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
826 //dir = this.bot_dodgevector;
827 //if (this.bot_dodgevector_jumpbutton)
828 // PHYS_INPUT_BUTTON_JUMP(this) = true;
829 this.movement_x = dir * v_forward * maxspeed;
830 this.movement_y = dir * v_right * maxspeed;
831 this.movement_z = dir * v_up * maxspeed;
833 // Emulate keyboard interface
835 havocbot_keyboard_movement(this, destorg);
838 // if(this.aistatus & AI_STATUS_ROAMING)
840 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
841 havocbot_bunnyhop(this, dir);
843 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
844 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;
845 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);
848 entity havocbot_gettarget(entity this, bool secondary)
851 vector eye = CENTER_OR_VIEWOFS(this);
852 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
854 vector v = CENTER_OR_VIEWOFS(it);
855 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
856 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
857 if(bot_shouldattack(this, it))
859 traceline(eye, v, true, this);
860 if (trace_ent == it || trace_fraction >= 1)
868 void havocbot_chooseenemy(entity this)
870 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
877 if (!bot_shouldattack(this, this.enemy))
879 // enemy died or something, find a new target
881 this.havocbot_chooseenemy_finished = time;
883 else if (this.havocbot_stickenemy)
885 // tracking last chosen enemy
886 // if enemy is visible
887 // and not really really far away
888 // and we're not severely injured
889 // then keep tracking for a half second into the future
890 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
891 if (trace_ent == this.enemy || trace_fraction == 1)
892 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
893 if (this.health > 30)
895 // remain tracking him for a shot while (case he went after a small corner or pilar
896 this.havocbot_chooseenemy_finished = time + 0.5;
899 // enemy isn't visible, or is far away, or we're injured severely
900 // so stop preferring this enemy
901 // (it will still take a half second until a new one is chosen)
902 this.havocbot_stickenemy = 0;
905 if (time < this.havocbot_chooseenemy_finished)
907 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
908 vector eye = this.origin + this.view_ofs;
910 float bestrating = 100000000;
913 int hf = this.dphitcontentsmask;
915 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
917 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
919 bool scan_transparent = false;
920 bool scan_secondary_targets = false;
921 bool have_secondary_targets = false;
924 scan_secondary_targets = false;
926 IL_EACH(g_bot_targets, it.bot_attack,
928 if(!scan_secondary_targets)
930 if(it.classname == "misc_breakablemodel")
932 have_secondary_targets = true;
936 else if(it.classname != "misc_breakablemodel")
939 vector v = (it.absmin + it.absmax) * 0.5;
940 float rating = vlen2(v - eye);
941 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
942 if (bestrating > rating)
943 if (bot_shouldattack(this, it))
945 traceline(eye, v, true, this);
946 if (trace_ent == it || trace_fraction >= 1)
954 if(!best && have_secondary_targets && !scan_secondary_targets)
956 scan_secondary_targets = true;
958 bestrating = 100000000;
962 // I want to do a second scan if no enemy was found or I don't have weapons
963 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
964 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
969 // Set flags to see through transparent objects
970 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
972 scan_transparent = true;
976 this.dphitcontentsmask = hf;
979 this.havocbot_stickenemy = true;
980 if(best && best.classname == "misc_breakablemodel")
981 this.havocbot_stickenemy = false;
984 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
986 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
987 // so skip this for them, or they'll never get to reload their weapons at all.
988 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
992 // if this weapon is scheduled for reloading, don't switch to it during combat
993 if (this.weapon_load[new_weapon] < 0)
995 bool other_weapon_available = false;
996 FOREACH(Weapons, it != WEP_Null, LAMBDA(
997 if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
998 other_weapon_available = true;
1000 if(other_weapon_available)
1007 void havocbot_chooseweapon(entity this)
1012 if(g_weaponarena_weapons == WEPSET(TUBA))
1014 PS(this).m_switchweapon = WEP_TUBA;
1018 // TODO: clean this up by moving it to weapon code
1019 if(this.enemy==NULL)
1021 // If no weapon was chosen get the first available weapon
1022 if(PS(this).m_weapon==WEP_Null)
1023 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1024 if(client_hasweapon(this, it, true, false))
1026 PS(this).m_switchweapon = it;
1033 // Do not change weapon during the next second after a combo
1034 float f = time - this.lastcombotime;
1039 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1041 // Should it do a weapon combo?
1042 float af, ct, combo_time, combo;
1044 af = ATTACK_FINISHED(this, 0);
1045 ct = autocvar_bot_ai_weapon_combo_threshold;
1047 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1048 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1049 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1053 if(autocvar_bot_ai_weapon_combo)
1054 if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1058 this.lastcombotime = time;
1061 distance *= pow(2, this.bot_rangepreference);
1063 // Custom weapon list based on distance to the enemy
1064 if(bot_custom_weapon){
1066 // Choose weapons for far distance
1067 if ( distance > bot_distance_far ) {
1068 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1069 w = bot_weapons_far[i];
1070 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1072 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1074 PS(this).m_switchweapon = Weapons_from(w);
1080 // Choose weapons for mid distance
1081 if ( distance > bot_distance_close) {
1082 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1083 w = bot_weapons_mid[i];
1084 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1086 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1088 PS(this).m_switchweapon = Weapons_from(w);
1094 // Choose weapons for close distance
1095 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1096 w = bot_weapons_close[i];
1097 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1099 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1101 PS(this).m_switchweapon = Weapons_from(w);
1108 void havocbot_aim(entity this)
1110 vector myvel, enemyvel;
1111 // if(this.flags & FL_INWATER)
1113 if (time < this.nextaim)
1115 this.nextaim = time + 0.1;
1116 myvel = this.velocity;
1117 if (!this.waterlevel)
1121 enemyvel = this.enemy.velocity;
1122 if (!this.enemy.waterlevel)
1124 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1127 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1130 bool havocbot_moveto_refresh_route(entity this)
1132 // Refresh path to goal if necessary
1134 wp = this.havocbot_personal_waypoint;
1135 navigation_goalrating_start(this);
1136 navigation_routerating(this, wp, 10000, 10000);
1137 navigation_goalrating_end(this);
1138 return this.navigation_hasgoals;
1141 float havocbot_moveto(entity this, vector pos)
1145 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1147 // Step 4: Move to waypoint
1148 if(this.havocbot_personal_waypoint==NULL)
1150 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1151 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1152 return CMD_STATUS_ERROR;
1155 if (!bot_strategytoken_taken)
1156 if(this.havocbot_personal_waypoint_searchtime<time)
1158 bot_strategytoken_taken = true;
1159 if(havocbot_moveto_refresh_route(this))
1161 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1162 this.havocbot_personal_waypoint_searchtime = time + 10;
1163 this.havocbot_personal_waypoint_failcounter = 0;
1167 this.havocbot_personal_waypoint_failcounter += 1;
1168 this.havocbot_personal_waypoint_searchtime = time + 2;
1169 if(this.havocbot_personal_waypoint_failcounter >= 30)
1171 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1172 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1173 delete(this.havocbot_personal_waypoint);
1174 return CMD_STATUS_ERROR;
1177 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1181 if(autocvar_bot_debug_goalstack)
1182 debuggoalstack(this);
1185 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1187 bot_aimdir(this, dir, -1);
1190 havocbot_movetogoal(this);
1192 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1194 // Step 5: Waypoint reached
1195 LOG_TRACE(this.netname, "'s personal waypoint reached");
1196 delete(this.havocbot_personal_waypoint);
1197 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1198 return CMD_STATUS_FINISHED;
1201 return CMD_STATUS_EXECUTING;
1204 // Step 2: Linking waypoint
1205 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1207 // Wait until it is linked
1208 if(!this.havocbot_personal_waypoint.wplinked)
1210 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1211 return CMD_STATUS_EXECUTING;
1214 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1215 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1216 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1218 // Step 3: Route to waypoint
1219 LOG_TRACE(this.netname, " walking to its personal waypoint");
1221 return CMD_STATUS_EXECUTING;
1224 // Step 1: Spawning waypoint
1225 wp = waypoint_spawnpersonal(this, pos);
1228 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1229 return CMD_STATUS_ERROR;
1232 this.havocbot_personal_waypoint = wp;
1233 this.havocbot_personal_waypoint_failcounter = 0;
1234 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1236 // if pos is inside a teleport, then let's mark it as teleport waypoint
1237 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1239 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1240 this.lastteleporttime = 0;
1244 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1245 print("routing to a teleporter\n");
1247 print("routing to a non-teleporter\n");
1250 return CMD_STATUS_EXECUTING;
1253 float havocbot_resetgoal(entity this)
1255 navigation_clearroute(this);
1256 return CMD_STATUS_FINISHED;
1259 void havocbot_setupbot(entity this)
1261 this.bot_ai = havocbot_ai;
1262 this.cmd_moveto = havocbot_moveto;
1263 this.cmd_resetgoal = havocbot_resetgoal;
1265 havocbot_chooserole(this);
1268 vector havocbot_dodge(entity this)
1270 // LordHavoc: disabled because this is too expensive
1275 float danger, bestdanger, vl, d;
1278 // check for dangerous objects near bot or approaching bot
1279 head = findchainfloat(bot_dodge, true);
1282 if (head.owner != this)
1284 vl = vlen(head.velocity);
1285 if (vl > autocvar_sv_maxspeed * 0.3)
1287 n = normalize(head.velocity);
1288 v = this.origin - head.origin;
1290 if (d > (0 - head.bot_dodgerating))
1291 if (d < (vl * 0.2 + head.bot_dodgerating))
1293 // calculate direction and distance from the flight path, by removing the forward axis
1294 v = v - (n * (v * n));
1295 danger = head.bot_dodgerating - vlen(v);
1296 if (bestdanger < danger)
1298 bestdanger = danger;
1299 // dodge to the side of the object
1300 dodge = normalize(v);
1306 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1307 if (bestdanger < danger)
1309 bestdanger = danger;
1310 dodge = normalize(this.origin - head.origin);