7 #include "../navigation.qh"
8 #include "../scripting.qh"
9 #include "../waypoints.qh"
11 #include <common/constants.qh>
12 #include <common/physics/player.qh>
13 #include <common/state.qh>
14 #include <common/items/_mod.qh>
15 #include <common/wepent.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 if (bot_strategytoken == this)
33 if (!bot_strategytoken_taken)
35 if(this.havocbot_blockhead)
37 this.havocbot_blockhead = false;
41 if (!this.jumppadcount)
42 this.havocbot_role(this); // little too far down the rabbit hole
45 // TODO: tracewalk() should take care of this job (better path finding under water)
46 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
49 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
51 // Look for the closest waypoint out of water
52 entity newgoal = NULL;
53 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
55 if(it.origin.z < this.origin.z)
58 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
61 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
64 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
66 if(trace_fraction < 1)
69 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
75 // te_wizspike(newgoal.origin);
76 navigation_pushroute(this, newgoal);
80 // token has been used this frame
81 bot_strategytoken_taken = true;
87 havocbot_chooseenemy(this);
89 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
91 .entity weaponentity = weaponentities[slot];
92 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
93 if(this.(weaponentity).bot_chooseweapontime < time)
95 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
96 havocbot_chooseweapon(this, weaponentity);
101 if (this.bot_aimtarg)
103 this.aistatus |= AI_STATUS_ATTACKING;
104 this.aistatus &= ~AI_STATUS_ROAMING;
108 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
110 PHYS_INPUT_BUTTON_ATCK(this) = false;
111 PHYS_INPUT_BUTTON_ATCK2(this) = false;
115 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
117 .entity weaponentity = weaponentities[slot];
118 Weapon w = this.(weaponentity).m_weapon;
119 if(w == WEP_Null && slot != 0)
121 w.wr_aim(w, this, weaponentity);
122 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
123 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
129 if(IS_PLAYER(this.bot_aimtarg))
130 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
133 else if (this.goalcurrent)
135 this.aistatus |= AI_STATUS_ROAMING;
136 this.aistatus &= ~AI_STATUS_ATTACKING;
138 vector now,v,next;//,heading;
139 float aimdistance,skillblend,distanceblend,blend;
140 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
141 aimdistance = vlen(now);
142 //heading = this.velocity;
143 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
145 this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
146 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
148 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
150 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
151 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
152 blend = skillblend * (1-distanceblend);
153 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
154 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
155 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
156 v = now + blend * (next - now);
157 //dprint(etos(this), " ");
158 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
159 //v = now * (distanceblend) + next * (1-distanceblend);
160 if (this.waterlevel < WATERLEVEL_SWIMMING)
162 //dprint("walk at:", vtos(v), "\n");
163 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
164 bot_aimdir(this, v, -1);
166 havocbot_movetogoal(this);
168 // if the bot is not attacking, consider reloading weapons
169 if (!(this.aistatus & AI_STATUS_ATTACKING))
171 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
173 .entity weaponentity = weaponentities[slot];
175 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
178 // we are currently holding a weapon that's not fully loaded, reload it
179 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
180 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
181 this.impulse = 20; // "press" the reload button, not sure if this is done right
183 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
184 // the code above executes next frame, starting the reloading then
185 if(skill >= 5) // bots can only look for unloaded weapons past this skill
186 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
188 FOREACH(Weapons, it != WEP_Null, LAMBDA(
189 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
190 this.(weaponentity).m_switchweapon = it;
197 void havocbot_keyboard_movement(entity this, vector destorg)
200 float blend, maxspeed;
203 sk = skill + this.bot_moveskill;
205 maxspeed = autocvar_sv_maxspeed;
207 if (time < this.havocbot_keyboardtime)
210 this.havocbot_keyboardtime =
212 this.havocbot_keyboardtime
213 + 0.05/max(1, sk+this.havocbot_keyboardskill)
214 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
216 keyboard = this.movement * (1.0 / maxspeed);
218 float trigger, trigger1;
219 blend = bound(0,sk*0.1,1);
220 trigger = autocvar_bot_ai_keyboard_threshold;
221 trigger1 = 0 - trigger;
223 // categorize forward movement
224 // at skill < 1.5 only forward
225 // at skill < 2.5 only individual directions
226 // at skill < 4.5 only individual directions, and forward diagonals
227 // at skill >= 4.5, all cases allowed
228 if (keyboard.x > trigger)
234 else if (keyboard.x < trigger1 && sk > 1.5)
249 if (keyboard.y > trigger)
251 else if (keyboard.y < trigger1)
256 if (keyboard.z > trigger)
258 else if (keyboard.z < trigger1)
263 this.havocbot_keyboard = keyboard * maxspeed;
264 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
266 keyboard = this.havocbot_keyboard;
267 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
268 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
269 this.movement = this.movement + (keyboard - this.movement) * blend;
272 void havocbot_bunnyhop(entity this, vector dir)
274 float bunnyhopdistance;
279 // Don't jump when attacking
280 if(this.aistatus & AI_STATUS_ATTACKING)
283 if(IS_PLAYER(this.goalcurrent))
286 maxspeed = autocvar_sv_maxspeed;
288 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
290 this.aistatus &= ~AI_STATUS_RUNNING;
291 PHYS_INPUT_BUTTON_JUMP(this) = false;
292 this.bot_canruntogoal = 0;
293 this.bot_timelastseengoal = 0;
297 if(this.waterlevel > WATERLEVEL_WETFEET)
299 this.aistatus &= ~AI_STATUS_RUNNING;
303 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
305 this.bot_canruntogoal = 0;
306 this.bot_timelastseengoal = 0;
309 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
310 bunnyhopdistance = vlen(this.origin - gco);
312 // Run only to visible goals
313 if(IS_ONGROUND(this))
314 if(this.speed==maxspeed)
315 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
317 this.bot_lastseengoal = this.goalcurrent;
320 if(this.bot_timelastseengoal)
322 // for a period of time
323 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
326 checkdistance = true;
328 // don't run if it is too close
329 if(this.bot_canruntogoal==0)
331 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
332 this.bot_canruntogoal = 1;
334 this.bot_canruntogoal = -1;
337 if(this.bot_canruntogoal != 1)
340 if(this.aistatus & AI_STATUS_ROAMING)
341 if(this.goalcurrent.classname=="waypoint")
342 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
343 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
344 if(this.goalstack01!=NULL)
346 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
347 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
348 while (deviation.y < -180) deviation.y = deviation.y + 360;
349 while (deviation.y > 180) deviation.y = deviation.y - 360;
351 if(fabs(deviation.y) < 20)
352 if(bunnyhopdistance < vlen(this.origin - gno))
353 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
355 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
356 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
358 checkdistance = false;
365 this.aistatus &= ~AI_STATUS_RUNNING;
366 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
367 PHYS_INPUT_BUTTON_JUMP(this) = true;
371 this.aistatus |= AI_STATUS_RUNNING;
372 PHYS_INPUT_BUTTON_JUMP(this) = true;
378 this.bot_timelastseengoal = time;
383 this.bot_timelastseengoal = 0;
387 // Release jump button
388 if(!cvar("sv_pogostick"))
389 if((IS_ONGROUND(this)) == 0)
391 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
392 PHYS_INPUT_BUTTON_JUMP(this) = false;
395 if(this.aistatus & AI_STATUS_RUNNING)
396 if(vlen(this.velocity)>maxspeed)
398 deviation = vectoangles(dir) - vectoangles(this.velocity);
399 while (deviation.y < -180) deviation.y = deviation.y + 360;
400 while (deviation.y > 180) deviation.y = deviation.y - 360;
402 if(fabs(deviation.y)>10)
406 this.movement_y = maxspeed * -1;
407 else if(deviation.y<10)
408 this.movement_y = maxspeed;
415 void havocbot_movetogoal(entity this)
423 vector evadeobstacle;
430 //if (this.goalentity)
431 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
432 this.movement = '0 0 0';
433 maxspeed = autocvar_sv_maxspeed;
435 // Jetpack navigation
437 if(this.navigation_jetpack_goal)
438 if(this.goalcurrent==this.navigation_jetpack_goal)
441 if(autocvar_bot_debug_goalstack)
443 debuggoalstack(this);
444 te_wizspike(this.navigation_jetpack_point);
448 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
450 // Brake almost completely so it can get a good direction
451 if(vdist(this.velocity, >, 10))
453 this.aistatus |= AI_STATUS_JETPACK_FLYING;
456 makevectors(this.v_angle.y * '0 1 0');
457 dir = normalize(this.navigation_jetpack_point - this.origin);
460 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
462 // Calculate brake distance in xy
466 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
468 v = vlen(this.velocity - this.velocity.z * '0 0 1');
469 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
470 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
471 if(d < db || d < 500)
474 if(fabs(this.velocity.x)>maxspeed*0.3)
476 this.movement_x = dir * v_forward * -maxspeed;
479 // Switch to normal mode
480 this.navigation_jetpack_goal = NULL;
481 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
482 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
486 else if(checkpvs(this.origin,this.goalcurrent))
488 // If I can see the goal switch to landing code
489 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
490 this.aistatus |= AI_STATUS_JETPACK_LANDING;
495 PHYS_INPUT_BUTTON_HOOK(this) = true;
496 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
498 this.movement_x = dir * v_forward * maxspeed;
499 this.movement_y = dir * v_right * maxspeed;
504 // Handling of jump pads
505 if(this.jumppadcount)
507 // If got stuck on the jump pad try to reach the farthest visible waypoint
508 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
510 if(fabs(this.velocity.z)<50)
512 entity newgoal = NULL;
513 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
515 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
517 if(trace_fraction < 1)
520 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
526 this.ignoregoal = this.goalcurrent;
527 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
528 navigation_clearroute(this);
529 navigation_routetogoal(this, newgoal, this.origin);
530 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
538 if(this.velocity.z>0)
541 vector velxy = this.velocity; velxy_z = 0;
542 threshold = maxspeed * 0.2;
543 if(vdist(velxy, <, threshold))
545 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
546 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
551 // Don't chase players while using a jump pad
552 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
556 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
557 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
559 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
561 if (!(IS_ONGROUND(this)))
563 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
564 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
565 if(this.items & IT_JETPACK)
567 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
568 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
570 if(this.velocity.z<0)
572 PHYS_INPUT_BUTTON_HOOK(this) = true;
576 PHYS_INPUT_BUTTON_HOOK(this) = true;
578 // If there is no goal try to move forward
580 if(this.goalcurrent==NULL)
583 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
585 vector xyvelocity = this.velocity; xyvelocity_z = 0;
586 float xyspeed = xyvelocity * dir;
588 if(xyspeed < (maxspeed / 2))
590 makevectors(this.v_angle.y * '0 1 0');
591 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
592 if(trace_fraction==1)
594 this.movement_x = dir * v_forward * maxspeed;
595 this.movement_y = dir * v_right * maxspeed;
597 havocbot_keyboard_movement(this, this.origin + dir * 100);
601 this.havocbot_blockhead = true;
605 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
607 if(this.velocity.z < 0)
609 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
611 .entity weaponentity = weaponentities[slot];
613 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
616 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
618 this.movement_x = maxspeed;
620 if(this.rocketjumptime)
622 if(time > this.rocketjumptime)
624 PHYS_INPUT_BUTTON_ATCK2(this) = true;
625 this.rocketjumptime = 0;
630 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
632 PHYS_INPUT_BUTTON_ATCK(this) = true;
633 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
641 // If there is no goal try to move forward
642 if(this.goalcurrent==NULL)
643 this.movement_x = maxspeed;
647 // If we are under water with no goals, swim up
649 if(this.goalcurrent==NULL)
652 if(this.waterlevel>WATERLEVEL_SWIMMING)
654 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
655 PHYS_INPUT_BUTTON_JUMP(this) = true;
657 PHYS_INPUT_BUTTON_JUMP(this) = false;
658 makevectors(this.v_angle.y * '0 1 0');
659 this.movement_x = dir * v_forward * maxspeed;
660 this.movement_y = dir * v_right * maxspeed;
661 this.movement_z = dir * v_up * maxspeed;
664 // if there is nowhere to go, exit
665 if (this.goalcurrent == NULL)
668 if (this.goalcurrent)
669 navigation_poptouchedgoals(this);
671 // if ran out of goals try to use an alternative goal or get a new strategy asap
672 if(this.goalcurrent == NULL)
674 this.bot_strategytime = 0;
679 if(autocvar_bot_debug_goalstack)
680 debuggoalstack(this);
682 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
683 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
684 destorg = this.origin;
685 destorg.x = bound(m1_x, destorg.x, m2_x);
686 destorg.y = bound(m1_y, destorg.y, m2_y);
687 destorg.z = bound(m1_z, destorg.z, m2_z);
688 diff = destorg - this.origin;
690 dir = normalize(diff);
691 flatdir = diff;flatdir.z = 0;
692 flatdir = normalize(flatdir);
693 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
695 //if (this.bot_dodgevector_time < time)
697 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
698 // this.bot_dodgevector_jumpbutton = 1;
699 evadeobstacle = '0 0 0';
704 if(this.waterlevel>WATERLEVEL_SWIMMING)
707 this.aistatus |= AI_STATUS_OUT_WATER;
711 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
712 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
713 PHYS_INPUT_BUTTON_JUMP(this) = true;
715 PHYS_INPUT_BUTTON_JUMP(this) = false;
717 dir = normalize(flatdir);
718 makevectors(this.v_angle.y * '0 1 0');
722 if(this.aistatus & AI_STATUS_OUT_WATER)
723 this.aistatus &= ~AI_STATUS_OUT_WATER;
725 // jump if going toward an obstacle that doesn't look like stairs we
726 // can walk up directly
727 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
728 if (trace_fraction < 1)
729 if (trace_plane_normal.z < 0.7)
732 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
733 if (trace_fraction < s + 0.01)
734 if (trace_plane_normal.z < 0.7)
737 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
738 if (trace_fraction > s)
739 PHYS_INPUT_BUTTON_JUMP(this) = true;
743 // avoiding dangers and obstacles
744 vector dst_ahead, dst_down;
745 makevectors(this.v_angle.y * '0 1 0');
746 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
747 dst_down = dst_ahead - '0 0 1500';
750 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
752 // Check head-banging against walls
753 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
755 PHYS_INPUT_BUTTON_JUMP(this) = true;
756 if(this.facingwalltime && time > this.facingwalltime)
758 this.ignoregoal = this.goalcurrent;
759 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
760 this.bot_strategytime = 0;
765 this.facingwalltime = time + 0.05;
770 this.facingwalltime = 0;
772 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
774 this.ignoregoal = NULL;
775 this.ignoregoaltime = 0;
779 // Check for water/slime/lava and dangerous edges
780 // (only when the bot is on the ground or jumping intentionally)
781 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
783 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
784 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
787 traceline(dst_ahead , dst_down, true, NULL);
788 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
789 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
790 if(trace_endpos.z < this.origin.z + this.mins.z)
792 s = pointcontents(trace_endpos + '0 0 1');
793 if (s != CONTENT_SOLID)
794 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
795 evadelava = normalize(this.velocity) * -1;
796 else if (s == CONTENT_SKY)
797 evadeobstacle = normalize(this.velocity) * -1;
798 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
799 this.goalcurrent.absmin, this.goalcurrent.absmax))
801 // if ain't a safe goal with "holes" (like the jumpad on soylent)
802 // and there is a trigger_hurt below
803 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
805 // Remove dangerous dynamic goals from stack
806 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
807 navigation_clearroute(this);
817 makevectors(this.v_angle.y * '0 1 0');
819 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
820 this.aistatus |= AI_STATUS_DANGER_AHEAD;
823 dodge = havocbot_dodge(this);
824 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
825 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
826 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
827 if(IS_PLAYER(trace_ent))
828 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
830 dir = normalize(dir + dodge + evadeobstacle + evadelava);
831 // this.bot_dodgevector = dir;
832 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
835 if(time < this.ladder_time)
837 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
839 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
844 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
849 //dir = this.bot_dodgevector;
850 //if (this.bot_dodgevector_jumpbutton)
851 // PHYS_INPUT_BUTTON_JUMP(this) = true;
852 this.movement_x = dir * v_forward * maxspeed;
853 this.movement_y = dir * v_right * maxspeed;
854 this.movement_z = dir * v_up * maxspeed;
856 // Emulate keyboard interface
858 havocbot_keyboard_movement(this, destorg);
861 // if(this.aistatus & AI_STATUS_ROAMING)
863 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
864 havocbot_bunnyhop(this, dir);
866 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
867 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;
868 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);
871 entity havocbot_gettarget(entity this, bool secondary)
874 vector eye = CENTER_OR_VIEWOFS(this);
875 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
877 vector v = CENTER_OR_VIEWOFS(it);
878 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
879 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
880 if(bot_shouldattack(this, it))
882 traceline(eye, v, true, this);
883 if (trace_ent == it || trace_fraction >= 1)
891 void havocbot_chooseenemy(entity this)
893 entity head, best, head2;
894 float rating, bestrating, hf;
896 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
903 if (!bot_shouldattack(this, this.enemy))
905 // enemy died or something, find a new target
907 this.havocbot_chooseenemy_finished = time;
909 else if (this.havocbot_stickenemy)
911 // tracking last chosen enemy
912 // if enemy is visible
913 // and not really really far away
914 // and we're not severely injured
915 // then keep tracking for a half second into the future
916 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
917 if (trace_ent == this.enemy || trace_fraction == 1)
918 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
919 if (this.health > 30)
921 // remain tracking him for a shot while (case he went after a small corner or pilar
922 this.havocbot_chooseenemy_finished = time + 0.5;
925 // enemy isn't visible, or is far away, or we're injured severely
926 // so stop preferring this enemy
927 // (it will still take a half second until a new one is chosen)
928 this.havocbot_stickenemy = 0;
931 if (time < this.havocbot_chooseenemy_finished)
933 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
934 eye = this.origin + this.view_ofs;
936 bestrating = 100000000;
937 head = head2 = findchainfloat(bot_attack, true);
940 hf = this.dphitcontentsmask;
942 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
944 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
946 bool scan_transparent = false;
947 bool scan_secondary_targets = false;
948 bool have_secondary_targets = false;
951 scan_secondary_targets = false;
953 for( ; head; head = head.chain)
955 if(!scan_secondary_targets)
957 if(head.classname == "misc_breakablemodel")
959 have_secondary_targets = true;
965 if(head.classname != "misc_breakablemodel")
969 v = (head.absmin + head.absmax) * 0.5;
970 rating = vlen(v - eye);
971 if (rating<autocvar_bot_ai_enemydetectionradius)
972 if (bestrating > rating)
973 if (bot_shouldattack(this, head))
975 traceline(eye, v, true, this);
976 if (trace_ent == head || trace_fraction >= 1)
984 if(!best && have_secondary_targets && !scan_secondary_targets)
986 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;
1004 scan_transparent = true;
1007 // Restore hit flags
1008 this.dphitcontentsmask = hf;
1011 this.havocbot_stickenemy = true;
1012 if(best && best.classname == "misc_breakablemodel")
1013 this.havocbot_stickenemy = false;
1016 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1018 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1019 // so skip this for them, or they'll never get to reload their weapons at all.
1020 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1024 // if this weapon is scheduled for reloading, don't switch to it during combat
1025 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1027 bool other_weapon_available = false;
1028 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1029 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1030 other_weapon_available = true;
1032 if(other_weapon_available)
1039 void havocbot_chooseweapon(entity this, .entity weaponentity)
1044 if(g_weaponarena_weapons == WEPSET(TUBA))
1046 this.(weaponentity).m_switchweapon = WEP_TUBA;
1050 // TODO: clean this up by moving it to weapon code
1051 if(this.enemy==NULL)
1053 // If no weapon was chosen get the first available weapon
1054 if(this.(weaponentity).m_weapon==WEP_Null)
1055 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1056 if(client_hasweapon(this, it, weaponentity, true, false))
1058 this.(weaponentity).m_switchweapon = it;
1065 // Do not change weapon during the next second after a combo
1066 float f = time - this.lastcombotime;
1071 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1073 // Should it do a weapon combo?
1074 float af, ct, combo_time, combo;
1076 af = ATTACK_FINISHED(this, 0);
1077 ct = autocvar_bot_ai_weapon_combo_threshold;
1079 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1080 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1081 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1085 if(autocvar_bot_ai_weapon_combo)
1086 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1090 this.lastcombotime = time;
1093 distance *= pow(2, this.bot_rangepreference);
1095 // Custom weapon list based on distance to the enemy
1096 if(bot_custom_weapon){
1098 // Choose weapons for far distance
1099 if ( distance > bot_distance_far ) {
1100 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1101 w = bot_weapons_far[i];
1102 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1104 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1106 this.(weaponentity).m_switchweapon = Weapons_from(w);
1112 // Choose weapons for mid distance
1113 if ( distance > bot_distance_close) {
1114 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1115 w = bot_weapons_mid[i];
1116 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1118 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1120 this.(weaponentity).m_switchweapon = Weapons_from(w);
1126 // Choose weapons for close distance
1127 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1128 w = bot_weapons_close[i];
1129 if ( client_hasweapon(this, Weapons_from(w), weaponentity, true, false) )
1131 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1133 this.(weaponentity).m_switchweapon = Weapons_from(w);
1140 void havocbot_aim(entity this)
1142 vector myvel, enemyvel;
1143 // if(this.flags & FL_INWATER)
1145 if (time < this.nextaim)
1147 this.nextaim = time + 0.1;
1148 myvel = this.velocity;
1149 if (!this.waterlevel)
1153 enemyvel = this.enemy.velocity;
1154 if (!this.enemy.waterlevel)
1156 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1159 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1162 bool havocbot_moveto_refresh_route(entity this)
1164 // Refresh path to goal if necessary
1166 wp = this.havocbot_personal_waypoint;
1167 navigation_goalrating_start(this);
1168 navigation_routerating(this, wp, 10000, 10000);
1169 navigation_goalrating_end(this);
1170 return this.navigation_hasgoals;
1173 float havocbot_moveto(entity this, vector pos)
1177 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1179 // Step 4: Move to waypoint
1180 if(this.havocbot_personal_waypoint==NULL)
1182 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1183 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1184 return CMD_STATUS_ERROR;
1187 if (!bot_strategytoken_taken)
1188 if(this.havocbot_personal_waypoint_searchtime<time)
1190 bot_strategytoken_taken = true;
1191 if(havocbot_moveto_refresh_route(this))
1193 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1194 this.havocbot_personal_waypoint_searchtime = time + 10;
1195 this.havocbot_personal_waypoint_failcounter = 0;
1199 this.havocbot_personal_waypoint_failcounter += 1;
1200 this.havocbot_personal_waypoint_searchtime = time + 2;
1201 if(this.havocbot_personal_waypoint_failcounter >= 30)
1203 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1204 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1205 delete(this.havocbot_personal_waypoint);
1206 return CMD_STATUS_ERROR;
1209 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1213 if(autocvar_bot_debug_goalstack)
1214 debuggoalstack(this);
1217 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1219 bot_aimdir(this, dir, -1);
1222 havocbot_movetogoal(this);
1224 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1226 // Step 5: Waypoint reached
1227 LOG_TRACE(this.netname, "'s personal waypoint reached");
1228 delete(this.havocbot_personal_waypoint);
1229 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1230 return CMD_STATUS_FINISHED;
1233 return CMD_STATUS_EXECUTING;
1236 // Step 2: Linking waypoint
1237 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1239 // Wait until it is linked
1240 if(!this.havocbot_personal_waypoint.wplinked)
1242 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1243 return CMD_STATUS_EXECUTING;
1246 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1247 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1248 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1250 // Step 3: Route to waypoint
1251 LOG_TRACE(this.netname, " walking to its personal waypoint");
1253 return CMD_STATUS_EXECUTING;
1256 // Step 1: Spawning waypoint
1257 wp = waypoint_spawnpersonal(this, pos);
1260 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1261 return CMD_STATUS_ERROR;
1264 this.havocbot_personal_waypoint = wp;
1265 this.havocbot_personal_waypoint_failcounter = 0;
1266 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1268 // if pos is inside a teleport, then let's mark it as teleport waypoint
1269 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1271 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1272 this.lastteleporttime = 0;
1276 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1277 print("routing to a teleporter\n");
1279 print("routing to a non-teleporter\n");
1282 return CMD_STATUS_EXECUTING;
1285 float havocbot_resetgoal(entity this)
1287 navigation_clearroute(this);
1288 return CMD_STATUS_FINISHED;
1291 void havocbot_setupbot(entity this)
1293 this.bot_ai = havocbot_ai;
1294 this.cmd_moveto = havocbot_moveto;
1295 this.cmd_resetgoal = havocbot_resetgoal;
1297 havocbot_chooserole(this);
1300 vector havocbot_dodge(entity this)
1302 // LordHavoc: disabled because this is too expensive
1307 float danger, bestdanger, vl, d;
1310 // check for dangerous objects near bot or approaching bot
1311 head = findchainfloat(bot_dodge, true);
1314 if (head.owner != this)
1316 vl = vlen(head.velocity);
1317 if (vl > autocvar_sv_maxspeed * 0.3)
1319 n = normalize(head.velocity);
1320 v = this.origin - head.origin;
1322 if (d > (0 - head.bot_dodgerating))
1323 if (d < (vl * 0.2 + head.bot_dodgerating))
1325 // calculate direction and distance from the flight path, by removing the forward axis
1326 v = v - (n * (v * n));
1327 danger = head.bot_dodgerating - vlen(v);
1328 if (bestdanger < danger)
1330 bestdanger = danger;
1331 // dodge to the side of the object
1332 dodge = normalize(v);
1338 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1339 if (bestdanger < danger)
1341 bestdanger = danger;
1342 dodge = normalize(this.origin - head.origin);