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>
16 #include <common/wepent.qh>
18 #include <common/triggers/teleporters.qh>
19 #include <common/triggers/trigger/jumppads.qh>
21 #include <lib/warpzone/common.qh>
25 void havocbot_ai(entity this)
30 if(bot_execute_commands(this))
33 while(this.goalcurrent && wasfreed(this.goalcurrent))
35 navigation_poproute(this);
37 this.bot_strategytime = 0;
40 if (bot_strategytoken == this)
41 if (!bot_strategytoken_taken)
43 if(this.havocbot_blockhead)
45 this.havocbot_blockhead = false;
49 if (!this.jumppadcount)
50 this.havocbot_role(this); // little too far down the rabbit hole
53 // TODO: tracewalk() should take care of this job (better path finding under water)
54 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
57 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
59 // Look for the closest waypoint out of water
60 entity newgoal = NULL;
61 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
63 if(it.origin.z < this.origin.z)
66 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
69 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
72 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
74 if(trace_fraction < 1)
77 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
83 // te_wizspike(newgoal.origin);
84 navigation_pushroute(this, newgoal);
88 // token has been used this frame
89 bot_strategytoken_taken = true;
95 havocbot_chooseenemy(this);
97 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
99 .entity weaponentity = weaponentities[slot];
100 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
101 if(this.(weaponentity).bot_chooseweapontime < time)
103 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
104 havocbot_chooseweapon(this, weaponentity);
109 if (this.bot_aimtarg)
111 this.aistatus |= AI_STATUS_ATTACKING;
112 this.aistatus &= ~AI_STATUS_ROAMING;
116 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
118 PHYS_INPUT_BUTTON_ATCK(this) = false;
119 PHYS_INPUT_BUTTON_ATCK2(this) = false;
123 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
125 .entity weaponentity = weaponentities[slot];
126 Weapon w = this.(weaponentity).m_weapon;
127 if(w == WEP_Null && slot != 0)
129 w.wr_aim(w, this, weaponentity);
130 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
131 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
137 if(IS_PLAYER(this.bot_aimtarg))
138 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
141 else if (this.goalcurrent)
143 this.aistatus |= AI_STATUS_ROAMING;
144 this.aistatus &= ~AI_STATUS_ATTACKING;
146 vector now,v,next;//,heading;
147 float aimdistance,skillblend,distanceblend,blend;
148 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
149 aimdistance = vlen(now);
150 //heading = this.velocity;
151 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
153 this.goalstack01 != this && this.goalstack01 && !wasfreed(this.goalstack01) && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
154 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
156 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
158 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
159 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
160 blend = skillblend * (1-distanceblend);
161 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
162 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
163 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
164 v = now + blend * (next - now);
165 //dprint(etos(this), " ");
166 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
167 //v = now * (distanceblend) + next * (1-distanceblend);
168 if (this.waterlevel < WATERLEVEL_SWIMMING)
170 //dprint("walk at:", vtos(v), "\n");
171 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
172 bot_aimdir(this, v, -1);
174 havocbot_movetogoal(this);
176 // if the bot is not attacking, consider reloading weapons
177 if (!(this.aistatus & AI_STATUS_ATTACKING))
179 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
181 .entity weaponentity = weaponentities[slot];
183 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
186 // we are currently holding a weapon that's not fully loaded, reload it
187 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
188 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
189 this.impulse = 20; // "press" the reload button, not sure if this is done right
191 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
192 // the code above executes next frame, starting the reloading then
193 if(skill >= 5) // bots can only look for unloaded weapons past this skill
194 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
196 FOREACH(Weapons, it != WEP_Null, LAMBDA(
197 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
198 this.(weaponentity).m_switchweapon = it;
205 void havocbot_keyboard_movement(entity this, vector destorg)
208 float blend, maxspeed;
211 sk = skill + this.bot_moveskill;
213 maxspeed = autocvar_sv_maxspeed;
215 if (time < this.havocbot_keyboardtime)
218 this.havocbot_keyboardtime =
220 this.havocbot_keyboardtime
221 + 0.05/max(1, sk+this.havocbot_keyboardskill)
222 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
224 keyboard = this.movement * (1.0 / maxspeed);
226 float trigger, trigger1;
227 blend = bound(0,sk*0.1,1);
228 trigger = autocvar_bot_ai_keyboard_threshold;
229 trigger1 = 0 - trigger;
231 // categorize forward movement
232 // at skill < 1.5 only forward
233 // at skill < 2.5 only individual directions
234 // at skill < 4.5 only individual directions, and forward diagonals
235 // at skill >= 4.5, all cases allowed
236 if (keyboard.x > trigger)
242 else if (keyboard.x < trigger1 && sk > 1.5)
257 if (keyboard.y > trigger)
259 else if (keyboard.y < trigger1)
264 if (keyboard.z > trigger)
266 else if (keyboard.z < trigger1)
271 this.havocbot_keyboard = keyboard * maxspeed;
272 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
274 keyboard = this.havocbot_keyboard;
275 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
276 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
277 this.movement = this.movement + (keyboard - this.movement) * blend;
280 void havocbot_bunnyhop(entity this, vector dir)
282 float bunnyhopdistance;
287 // Don't jump when attacking
288 if(this.aistatus & AI_STATUS_ATTACKING)
291 if(IS_PLAYER(this.goalcurrent))
294 maxspeed = autocvar_sv_maxspeed;
296 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
298 this.aistatus &= ~AI_STATUS_RUNNING;
299 PHYS_INPUT_BUTTON_JUMP(this) = false;
300 this.bot_canruntogoal = 0;
301 this.bot_timelastseengoal = 0;
305 if(this.waterlevel > WATERLEVEL_WETFEET)
307 this.aistatus &= ~AI_STATUS_RUNNING;
311 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
313 this.bot_canruntogoal = 0;
314 this.bot_timelastseengoal = 0;
317 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
318 bunnyhopdistance = vlen(this.origin - gco);
320 // Run only to visible goals
321 if(IS_ONGROUND(this))
322 if(this.speed==maxspeed)
323 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
325 this.bot_lastseengoal = this.goalcurrent;
328 if(this.bot_timelastseengoal)
330 // for a period of time
331 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
334 checkdistance = true;
336 // don't run if it is too close
337 if(this.bot_canruntogoal==0)
339 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
340 this.bot_canruntogoal = 1;
342 this.bot_canruntogoal = -1;
345 if(this.bot_canruntogoal != 1)
348 if(this.aistatus & AI_STATUS_ROAMING)
349 if(this.goalcurrent.classname=="waypoint")
350 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
351 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
352 if(this.goalstack01 && !wasfreed(this.goalstack01))
354 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
355 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
356 while (deviation.y < -180) deviation.y = deviation.y + 360;
357 while (deviation.y > 180) deviation.y = deviation.y - 360;
359 if(fabs(deviation.y) < 20)
360 if(bunnyhopdistance < vlen(this.origin - gno))
361 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
363 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
364 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
366 checkdistance = false;
373 this.aistatus &= ~AI_STATUS_RUNNING;
374 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
375 PHYS_INPUT_BUTTON_JUMP(this) = true;
379 this.aistatus |= AI_STATUS_RUNNING;
380 PHYS_INPUT_BUTTON_JUMP(this) = true;
386 this.bot_timelastseengoal = time;
391 this.bot_timelastseengoal = 0;
395 // Release jump button
396 if(!cvar("sv_pogostick"))
397 if((IS_ONGROUND(this)) == 0)
399 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
400 PHYS_INPUT_BUTTON_JUMP(this) = false;
403 if(this.aistatus & AI_STATUS_RUNNING)
404 if(vlen(this.velocity)>maxspeed)
406 deviation = vectoangles(dir) - vectoangles(this.velocity);
407 while (deviation.y < -180) deviation.y = deviation.y + 360;
408 while (deviation.y > 180) deviation.y = deviation.y - 360;
410 if(fabs(deviation.y)>10)
414 this.movement_y = maxspeed * -1;
415 else if(deviation.y<10)
416 this.movement_y = maxspeed;
423 void havocbot_movetogoal(entity this)
431 vector evadeobstacle;
438 //if (this.goalentity)
439 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
440 this.movement = '0 0 0';
441 maxspeed = autocvar_sv_maxspeed;
443 // Jetpack navigation
445 if(this.navigation_jetpack_goal)
446 if(this.goalcurrent==this.navigation_jetpack_goal)
449 if(autocvar_bot_debug_goalstack)
451 debuggoalstack(this);
452 te_wizspike(this.navigation_jetpack_point);
456 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
458 // Brake almost completely so it can get a good direction
459 if(vdist(this.velocity, >, 10))
461 this.aistatus |= AI_STATUS_JETPACK_FLYING;
464 makevectors(this.v_angle.y * '0 1 0');
465 dir = normalize(this.navigation_jetpack_point - this.origin);
468 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
470 // Calculate brake distance in xy
474 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
476 v = vlen(this.velocity - this.velocity.z * '0 0 1');
477 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
478 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
479 if(d < db || d < 500)
482 if(fabs(this.velocity.x)>maxspeed*0.3)
484 this.movement_x = dir * v_forward * -maxspeed;
487 // Switch to normal mode
488 this.navigation_jetpack_goal = NULL;
489 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
490 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
494 else if(checkpvs(this.origin,this.goalcurrent))
496 // If I can see the goal switch to landing code
497 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
498 this.aistatus |= AI_STATUS_JETPACK_LANDING;
503 PHYS_INPUT_BUTTON_HOOK(this) = true;
504 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
506 this.movement_x = dir * v_forward * maxspeed;
507 this.movement_y = dir * v_right * maxspeed;
512 // Handling of jump pads
513 if(this.jumppadcount)
515 // If got stuck on the jump pad try to reach the farthest visible waypoint
516 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
518 if(fabs(this.velocity.z)<50)
520 entity newgoal = NULL;
521 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
523 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
525 if(trace_fraction < 1)
528 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
534 this.ignoregoal = this.goalcurrent;
535 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
536 navigation_clearroute(this);
537 navigation_routetogoal(this, newgoal, this.origin);
538 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
546 if(this.velocity.z>0)
549 vector velxy = this.velocity; velxy_z = 0;
550 threshold = maxspeed * 0.2;
551 if(vdist(velxy, <, threshold))
553 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
554 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
559 // Don't chase players while using a jump pad
560 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
564 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
565 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
567 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
569 if (!(IS_ONGROUND(this)))
571 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
572 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
573 if(this.items & IT_JETPACK)
575 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
576 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
578 if(this.velocity.z<0)
580 PHYS_INPUT_BUTTON_HOOK(this) = true;
584 PHYS_INPUT_BUTTON_HOOK(this) = true;
586 // If there is no goal try to move forward
588 if(this.goalcurrent==NULL)
591 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
593 vector xyvelocity = this.velocity; xyvelocity_z = 0;
594 float xyspeed = xyvelocity * dir;
596 if(xyspeed < (maxspeed / 2))
598 makevectors(this.v_angle.y * '0 1 0');
599 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
600 if(trace_fraction==1)
602 this.movement_x = dir * v_forward * maxspeed;
603 this.movement_y = dir * v_right * maxspeed;
605 havocbot_keyboard_movement(this, this.origin + dir * 100);
609 this.havocbot_blockhead = true;
613 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
615 if(this.velocity.z < 0)
617 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
619 .entity weaponentity = weaponentities[slot];
621 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
624 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
626 this.movement_x = maxspeed;
628 if(this.rocketjumptime)
630 if(time > this.rocketjumptime)
632 PHYS_INPUT_BUTTON_ATCK2(this) = true;
633 this.rocketjumptime = 0;
638 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
640 PHYS_INPUT_BUTTON_ATCK(this) = true;
641 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
649 // If there is no goal try to move forward
650 if(this.goalcurrent==NULL)
651 this.movement_x = maxspeed;
655 // If we are under water with no goals, swim up
657 if(this.goalcurrent==NULL)
660 if(this.waterlevel>WATERLEVEL_SWIMMING)
662 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
663 PHYS_INPUT_BUTTON_JUMP(this) = true;
665 PHYS_INPUT_BUTTON_JUMP(this) = false;
666 makevectors(this.v_angle.y * '0 1 0');
667 this.movement_x = dir * v_forward * maxspeed;
668 this.movement_y = dir * v_right * maxspeed;
669 this.movement_z = dir * v_up * maxspeed;
672 // if there is nowhere to go, exit
673 if (this.goalcurrent == NULL)
676 if (this.goalcurrent)
677 navigation_poptouchedgoals(this);
679 // if ran out of goals try to use an alternative goal or get a new strategy asap
680 if(this.goalcurrent == NULL)
682 this.bot_strategytime = 0;
687 if(autocvar_bot_debug_goalstack)
688 debuggoalstack(this);
690 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
691 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
692 destorg = this.origin;
693 destorg.x = bound(m1_x, destorg.x, m2_x);
694 destorg.y = bound(m1_y, destorg.y, m2_y);
695 destorg.z = bound(m1_z, destorg.z, m2_z);
696 diff = destorg - this.origin;
698 dir = normalize(diff);
699 flatdir = diff;flatdir.z = 0;
700 flatdir = normalize(flatdir);
701 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
703 //if (this.bot_dodgevector_time < time)
705 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
706 // this.bot_dodgevector_jumpbutton = 1;
707 evadeobstacle = '0 0 0';
712 if(this.waterlevel>WATERLEVEL_SWIMMING)
715 this.aistatus |= AI_STATUS_OUT_WATER;
719 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
720 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
721 PHYS_INPUT_BUTTON_JUMP(this) = true;
723 PHYS_INPUT_BUTTON_JUMP(this) = false;
725 dir = normalize(flatdir);
726 makevectors(this.v_angle.y * '0 1 0');
730 if(this.aistatus & AI_STATUS_OUT_WATER)
731 this.aistatus &= ~AI_STATUS_OUT_WATER;
733 // jump if going toward an obstacle that doesn't look like stairs we
734 // can walk up directly
735 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
736 if (trace_fraction < 1)
737 if (trace_plane_normal.z < 0.7)
740 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
741 if (trace_fraction < s + 0.01)
742 if (trace_plane_normal.z < 0.7)
745 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
746 if (trace_fraction > s)
747 PHYS_INPUT_BUTTON_JUMP(this) = true;
751 // avoiding dangers and obstacles
752 vector dst_ahead, dst_down;
753 makevectors(this.v_angle.y * '0 1 0');
754 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
755 dst_down = dst_ahead - '0 0 1500';
758 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
760 // Check head-banging against walls
761 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
763 PHYS_INPUT_BUTTON_JUMP(this) = true;
764 if(this.facingwalltime && time > this.facingwalltime)
766 this.ignoregoal = this.goalcurrent;
767 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
768 this.bot_strategytime = 0;
773 this.facingwalltime = time + 0.05;
778 this.facingwalltime = 0;
780 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
782 this.ignoregoal = NULL;
783 this.ignoregoaltime = 0;
787 // Check for water/slime/lava and dangerous edges
788 // (only when the bot is on the ground or jumping intentionally)
789 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
791 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
792 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
795 traceline(dst_ahead , dst_down, true, NULL);
796 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
797 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
798 if(trace_endpos.z < this.origin.z + this.mins.z)
800 s = pointcontents(trace_endpos + '0 0 1');
801 if (s != CONTENT_SOLID)
802 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
803 evadelava = normalize(this.velocity) * -1;
804 else if (s == CONTENT_SKY)
805 evadeobstacle = normalize(this.velocity) * -1;
806 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
807 this.goalcurrent.absmin, this.goalcurrent.absmax))
809 // if ain't a safe goal with "holes" (like the jumpad on soylent)
810 // and there is a trigger_hurt below
811 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
813 // Remove dangerous dynamic goals from stack
814 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
815 navigation_clearroute(this);
825 makevectors(this.v_angle.y * '0 1 0');
827 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
828 this.aistatus |= AI_STATUS_DANGER_AHEAD;
831 dodge = havocbot_dodge(this);
832 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
833 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
834 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
835 if(IS_PLAYER(trace_ent))
836 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
838 dir = normalize(dir + dodge + evadeobstacle + evadelava);
839 // this.bot_dodgevector = dir;
840 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
843 if(time < this.ladder_time)
845 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
847 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
852 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
857 //dir = this.bot_dodgevector;
858 //if (this.bot_dodgevector_jumpbutton)
859 // PHYS_INPUT_BUTTON_JUMP(this) = true;
860 this.movement_x = dir * v_forward * maxspeed;
861 this.movement_y = dir * v_right * maxspeed;
862 this.movement_z = dir * v_up * maxspeed;
864 // Emulate keyboard interface
866 havocbot_keyboard_movement(this, destorg);
869 // if(this.aistatus & AI_STATUS_ROAMING)
871 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
872 havocbot_bunnyhop(this, dir);
874 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
875 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;
876 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);
879 entity havocbot_gettarget(entity this, bool secondary)
882 vector eye = CENTER_OR_VIEWOFS(this);
883 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
885 vector v = CENTER_OR_VIEWOFS(it);
886 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
887 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
888 if(bot_shouldattack(this, it))
890 traceline(eye, v, true, this);
891 if (trace_ent == it || trace_fraction >= 1)
899 void havocbot_chooseenemy(entity this)
901 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
908 if (!bot_shouldattack(this, this.enemy))
910 // enemy died or something, find a new target
912 this.havocbot_chooseenemy_finished = time;
914 else if (this.havocbot_stickenemy)
916 // tracking last chosen enemy
917 // if enemy is visible
918 // and not really really far away
919 // and we're not severely injured
920 // then keep tracking for a half second into the future
921 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
922 if (trace_ent == this.enemy || trace_fraction == 1)
923 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
924 if (this.health > 30)
926 // remain tracking him for a shot while (case he went after a small corner or pilar
927 this.havocbot_chooseenemy_finished = time + 0.5;
930 // enemy isn't visible, or is far away, or we're injured severely
931 // so stop preferring this enemy
932 // (it will still take a half second until a new one is chosen)
933 this.havocbot_stickenemy = 0;
936 if (time < this.havocbot_chooseenemy_finished)
938 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
939 vector eye = this.origin + this.view_ofs;
941 float bestrating = 100000000;
944 int hf = this.dphitcontentsmask;
946 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
948 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
950 bool scan_transparent = false;
951 bool scan_secondary_targets = false;
952 bool have_secondary_targets = false;
955 scan_secondary_targets = false;
957 IL_EACH(g_bot_targets, it.bot_attack,
959 if(!scan_secondary_targets)
961 if(it.classname == "misc_breakablemodel")
963 have_secondary_targets = true;
967 else if(it.classname != "misc_breakablemodel")
970 vector v = (it.absmin + it.absmax) * 0.5;
971 float rating = vlen2(v - eye);
972 if (vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
973 if (bestrating > rating)
974 if (bot_shouldattack(this, it))
976 traceline(eye, v, true, this);
977 if (trace_ent == it || trace_fraction >= 1)
985 if(!best && have_secondary_targets && !scan_secondary_targets)
987 scan_secondary_targets = true;
989 bestrating = 100000000;
993 // I want to do a second scan if no enemy was found or I don't have weapons
994 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
995 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
1000 // Set flags to see through transparent objects
1001 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1003 scan_transparent = true;
1006 // Restore hit flags
1007 this.dphitcontentsmask = hf;
1010 this.havocbot_stickenemy = true;
1011 if(best && best.classname == "misc_breakablemodel")
1012 this.havocbot_stickenemy = false;
1015 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1017 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1018 // so skip this for them, or they'll never get to reload their weapons at all.
1019 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1023 // if this weapon is scheduled for reloading, don't switch to it during combat
1024 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1026 bool other_weapon_available = false;
1027 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1028 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1029 other_weapon_available = true;
1031 if(other_weapon_available)
1038 void havocbot_chooseweapon(entity this, .entity weaponentity)
1043 if(g_weaponarena_weapons == WEPSET(TUBA))
1045 this.(weaponentity).m_switchweapon = WEP_TUBA;
1049 // TODO: clean this up by moving it to weapon code
1050 if(this.enemy==NULL)
1052 // If no weapon was chosen get the first available weapon
1053 if(this.(weaponentity).m_weapon==WEP_Null)
1054 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1055 if(client_hasweapon(this, it, weaponentity, true, false))
1057 this.(weaponentity).m_switchweapon = it;
1064 // Do not change weapon during the next second after a combo
1065 float f = time - this.lastcombotime;
1070 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1072 // Should it do a weapon combo?
1073 float af, ct, combo_time, combo;
1075 af = ATTACK_FINISHED(this, 0);
1076 ct = autocvar_bot_ai_weapon_combo_threshold;
1078 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1079 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1080 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1084 if(autocvar_bot_ai_weapon_combo)
1085 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1089 this.lastcombotime = time;
1092 distance *= pow(2, this.bot_rangepreference);
1094 // Custom weapon list based on distance to the enemy
1095 if(bot_custom_weapon){
1097 // Choose weapons for far distance
1098 if ( distance > bot_distance_far ) {
1099 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1100 w = bot_weapons_far[i];
1101 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1103 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1105 this.(weaponentity).m_switchweapon = Weapons_from(w);
1111 // Choose weapons for mid distance
1112 if ( distance > bot_distance_close) {
1113 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1114 w = bot_weapons_mid[i];
1115 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1117 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1119 this.(weaponentity).m_switchweapon = Weapons_from(w);
1125 // Choose weapons for close distance
1126 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1127 w = bot_weapons_close[i];
1128 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1130 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1132 this.(weaponentity).m_switchweapon = Weapons_from(w);
1139 void havocbot_aim(entity this)
1141 vector myvel, enemyvel;
1142 // if(this.flags & FL_INWATER)
1144 if (time < this.nextaim)
1146 this.nextaim = time + 0.1;
1147 myvel = this.velocity;
1148 if (!this.waterlevel)
1152 enemyvel = this.enemy.velocity;
1153 if (!this.enemy.waterlevel)
1155 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1158 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1161 bool havocbot_moveto_refresh_route(entity this)
1163 // Refresh path to goal if necessary
1165 wp = this.havocbot_personal_waypoint;
1166 navigation_goalrating_start(this);
1167 navigation_routerating(this, wp, 10000, 10000);
1168 navigation_goalrating_end(this);
1169 return this.navigation_hasgoals;
1172 float havocbot_moveto(entity this, vector pos)
1176 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1178 // Step 4: Move to waypoint
1179 if(this.havocbot_personal_waypoint==NULL)
1181 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1182 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1183 return CMD_STATUS_ERROR;
1186 if (!bot_strategytoken_taken)
1187 if(this.havocbot_personal_waypoint_searchtime<time)
1189 bot_strategytoken_taken = true;
1190 if(havocbot_moveto_refresh_route(this))
1192 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1193 this.havocbot_personal_waypoint_searchtime = time + 10;
1194 this.havocbot_personal_waypoint_failcounter = 0;
1198 this.havocbot_personal_waypoint_failcounter += 1;
1199 this.havocbot_personal_waypoint_searchtime = time + 2;
1200 if(this.havocbot_personal_waypoint_failcounter >= 30)
1202 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1203 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1204 delete(this.havocbot_personal_waypoint);
1205 return CMD_STATUS_ERROR;
1208 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1212 if(autocvar_bot_debug_goalstack)
1213 debuggoalstack(this);
1216 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1218 bot_aimdir(this, dir, -1);
1221 havocbot_movetogoal(this);
1223 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1225 // Step 5: Waypoint reached
1226 LOG_TRACE(this.netname, "'s personal waypoint reached");
1227 delete(this.havocbot_personal_waypoint);
1228 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1229 return CMD_STATUS_FINISHED;
1232 return CMD_STATUS_EXECUTING;
1235 // Step 2: Linking waypoint
1236 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1238 // Wait until it is linked
1239 if(!this.havocbot_personal_waypoint.wplinked)
1241 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1242 return CMD_STATUS_EXECUTING;
1245 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1246 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1247 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1249 // Step 3: Route to waypoint
1250 LOG_TRACE(this.netname, " walking to its personal waypoint");
1252 return CMD_STATUS_EXECUTING;
1255 // Step 1: Spawning waypoint
1256 wp = waypoint_spawnpersonal(this, pos);
1259 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1260 return CMD_STATUS_ERROR;
1263 this.havocbot_personal_waypoint = wp;
1264 this.havocbot_personal_waypoint_failcounter = 0;
1265 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1267 // if pos is inside a teleport, then let's mark it as teleport waypoint
1268 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1270 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1271 this.lastteleporttime = 0;
1275 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1276 print("routing to a teleporter\n");
1278 print("routing to a non-teleporter\n");
1281 return CMD_STATUS_EXECUTING;
1284 float havocbot_resetgoal(entity this)
1286 navigation_clearroute(this);
1287 return CMD_STATUS_FINISHED;
1290 void havocbot_setupbot(entity this)
1292 this.bot_ai = havocbot_ai;
1293 this.cmd_moveto = havocbot_moveto;
1294 this.cmd_resetgoal = havocbot_resetgoal;
1296 havocbot_chooserole(this);
1299 vector havocbot_dodge(entity this)
1301 // LordHavoc: disabled because this is too expensive
1306 float danger, bestdanger, vl, d;
1309 // check for dangerous objects near bot or approaching bot
1310 head = findchainfloat(bot_dodge, true);
1313 if (head.owner != this)
1315 vl = vlen(head.velocity);
1316 if (vl > autocvar_sv_maxspeed * 0.3)
1318 n = normalize(head.velocity);
1319 v = this.origin - head.origin;
1321 if (d > (0 - head.bot_dodgerating))
1322 if (d < (vl * 0.2 + head.bot_dodgerating))
1324 // calculate direction and distance from the flight path, by removing the forward axis
1325 v = v - (n * (v * n));
1326 danger = head.bot_dodgerating - vlen(v);
1327 if (bestdanger < danger)
1329 bestdanger = danger;
1330 // dodge to the side of the object
1331 dodge = normalize(v);
1337 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1338 if (bestdanger < danger)
1340 bestdanger = danger;
1341 dodge = normalize(this.origin - head.origin);