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/all.qh>
16 #include <common/triggers/trigger/jumppads.qh>
18 #include <lib/warpzone/common.qh>
22 void havocbot_ai(entity this)
27 if(bot_execute_commands(this))
30 if (bot_strategytoken == this)
31 if (!bot_strategytoken_taken)
33 if(this.havocbot_blockhead)
35 this.havocbot_blockhead = false;
39 if (!this.jumppadcount)
40 this.havocbot_role(this); // little too far down the rabbit hole
43 // TODO: tracewalk() should take care of this job (better path finding under water)
44 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
47 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
49 // Look for the closest waypoint out of water
50 entity newgoal = NULL;
51 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
53 if(it.origin.z < this.origin.z)
56 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
59 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
62 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
64 if(trace_fraction < 1)
67 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
73 // te_wizspike(newgoal.origin);
74 navigation_pushroute(this, newgoal);
78 // token has been used this frame
79 bot_strategytoken_taken = true;
85 havocbot_chooseenemy(this);
86 if (this.bot_chooseweapontime < time )
88 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
89 havocbot_chooseweapon(this);
95 this.aistatus |= AI_STATUS_ATTACKING;
96 this.aistatus &= ~AI_STATUS_ROAMING;
100 Weapon w = PS(this).m_weapon;
102 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
104 PHYS_INPUT_BUTTON_ATCK(this) = false;
105 PHYS_INPUT_BUTTON_ATCK2(this) = false;
109 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
110 this.lastfiredweapon = PS(this).m_weapon.m_id;
115 if(IS_PLAYER(this.bot_aimtarg))
116 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
119 else if (this.goalcurrent)
121 this.aistatus |= AI_STATUS_ROAMING;
122 this.aistatus &= ~AI_STATUS_ATTACKING;
124 vector now,v,next;//,heading;
125 float aimdistance,skillblend,distanceblend,blend;
126 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
127 aimdistance = vlen(now);
128 //heading = this.velocity;
129 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
131 this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
132 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
134 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
136 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
137 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
138 blend = skillblend * (1-distanceblend);
139 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
140 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
141 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
142 v = now + blend * (next - now);
143 //dprint(etos(this), " ");
144 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
145 //v = now * (distanceblend) + next * (1-distanceblend);
146 if (this.waterlevel < WATERLEVEL_SWIMMING)
148 //dprint("walk at:", vtos(v), "\n");
149 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
150 bot_aimdir(this, v, -1);
152 havocbot_movetogoal(this);
154 // if the bot is not attacking, consider reloading weapons
155 if (!(this.aistatus & AI_STATUS_ATTACKING))
157 // we are currently holding a weapon that's not fully loaded, reload it
158 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
159 if(this.clip_load < this.clip_size)
160 this.impulse = 20; // "press" the reload button, not sure if this is done right
162 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
163 // the code above executes next frame, starting the reloading then
164 if(skill >= 5) // bots can only look for unloaded weapons past this skill
165 if(this.clip_load >= 0) // only if we're not reloading a weapon already
167 FOREACH(Weapons, it != WEP_Null, LAMBDA(
168 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
169 PS(this).m_switchweapon = it;
175 void havocbot_keyboard_movement(entity this, vector destorg)
178 float blend, maxspeed;
181 sk = skill + this.bot_moveskill;
183 maxspeed = autocvar_sv_maxspeed;
185 if (time < this.havocbot_keyboardtime)
188 this.havocbot_keyboardtime =
190 this.havocbot_keyboardtime
191 + 0.05/max(1, sk+this.havocbot_keyboardskill)
192 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
194 keyboard = this.movement * (1.0 / maxspeed);
196 float trigger, trigger1;
197 blend = bound(0,sk*0.1,1);
198 trigger = autocvar_bot_ai_keyboard_threshold;
199 trigger1 = 0 - trigger;
201 // categorize forward movement
202 // at skill < 1.5 only forward
203 // at skill < 2.5 only individual directions
204 // at skill < 4.5 only individual directions, and forward diagonals
205 // at skill >= 4.5, all cases allowed
206 if (keyboard.x > trigger)
212 else if (keyboard.x < trigger1 && sk > 1.5)
227 if (keyboard.y > trigger)
229 else if (keyboard.y < trigger1)
234 if (keyboard.z > trigger)
236 else if (keyboard.z < trigger1)
241 this.havocbot_keyboard = keyboard * maxspeed;
242 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
244 keyboard = this.havocbot_keyboard;
245 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
246 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
247 this.movement = this.movement + (keyboard - this.movement) * blend;
250 void havocbot_bunnyhop(entity this, vector dir)
252 float bunnyhopdistance;
257 // Don't jump when attacking
258 if(this.aistatus & AI_STATUS_ATTACKING)
261 if(IS_PLAYER(this.goalcurrent))
264 maxspeed = autocvar_sv_maxspeed;
266 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
268 this.aistatus &= ~AI_STATUS_RUNNING;
269 PHYS_INPUT_BUTTON_JUMP(this) = false;
270 this.bot_canruntogoal = 0;
271 this.bot_timelastseengoal = 0;
275 if(this.waterlevel > WATERLEVEL_WETFEET)
277 this.aistatus &= ~AI_STATUS_RUNNING;
281 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
283 this.bot_canruntogoal = 0;
284 this.bot_timelastseengoal = 0;
287 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
288 bunnyhopdistance = vlen(this.origin - gco);
290 // Run only to visible goals
291 if(IS_ONGROUND(this))
292 if(this.speed==maxspeed)
293 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
295 this.bot_lastseengoal = this.goalcurrent;
298 if(this.bot_timelastseengoal)
300 // for a period of time
301 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
304 checkdistance = true;
306 // don't run if it is too close
307 if(this.bot_canruntogoal==0)
309 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
310 this.bot_canruntogoal = 1;
312 this.bot_canruntogoal = -1;
315 if(this.bot_canruntogoal != 1)
318 if(this.aistatus & AI_STATUS_ROAMING)
319 if(this.goalcurrent.classname=="waypoint")
320 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
321 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
322 if(this.goalstack01!=NULL)
324 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
325 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
326 while (deviation.y < -180) deviation.y = deviation.y + 360;
327 while (deviation.y > 180) deviation.y = deviation.y - 360;
329 if(fabs(deviation.y) < 20)
330 if(bunnyhopdistance < vlen(this.origin - gno))
331 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
333 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
334 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
336 checkdistance = false;
343 this.aistatus &= ~AI_STATUS_RUNNING;
344 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
345 PHYS_INPUT_BUTTON_JUMP(this) = true;
349 this.aistatus |= AI_STATUS_RUNNING;
350 PHYS_INPUT_BUTTON_JUMP(this) = true;
356 this.bot_timelastseengoal = time;
361 this.bot_timelastseengoal = 0;
365 // Release jump button
366 if(!cvar("sv_pogostick"))
367 if((IS_ONGROUND(this)) == 0)
369 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
370 PHYS_INPUT_BUTTON_JUMP(this) = false;
373 if(this.aistatus & AI_STATUS_RUNNING)
374 if(vlen(this.velocity)>maxspeed)
376 deviation = vectoangles(dir) - vectoangles(this.velocity);
377 while (deviation.y < -180) deviation.y = deviation.y + 360;
378 while (deviation.y > 180) deviation.y = deviation.y - 360;
380 if(fabs(deviation.y)>10)
384 this.movement_y = maxspeed * -1;
385 else if(deviation.y<10)
386 this.movement_y = maxspeed;
393 void havocbot_movetogoal(entity this)
401 vector evadeobstacle;
408 //if (this.goalentity)
409 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
410 this.movement = '0 0 0';
411 maxspeed = autocvar_sv_maxspeed;
413 // Jetpack navigation
415 if(this.navigation_jetpack_goal)
416 if(this.goalcurrent==this.navigation_jetpack_goal)
419 if(autocvar_bot_debug_goalstack)
421 debuggoalstack(this);
422 te_wizspike(this.navigation_jetpack_point);
426 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
428 // Brake almost completely so it can get a good direction
429 if(vdist(this.velocity, >, 10))
431 this.aistatus |= AI_STATUS_JETPACK_FLYING;
434 makevectors(this.v_angle.y * '0 1 0');
435 dir = normalize(this.navigation_jetpack_point - this.origin);
438 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
440 // Calculate brake distance in xy
444 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
446 v = vlen(this.velocity - this.velocity.z * '0 0 1');
447 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
448 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
449 if(d < db || d < 500)
452 if(fabs(this.velocity.x)>maxspeed*0.3)
454 this.movement_x = dir * v_forward * -maxspeed;
457 // Switch to normal mode
458 this.navigation_jetpack_goal = NULL;
459 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
460 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
464 else if(checkpvs(this.origin,this.goalcurrent))
466 // If I can see the goal switch to landing code
467 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
468 this.aistatus |= AI_STATUS_JETPACK_LANDING;
473 PHYS_INPUT_BUTTON_HOOK(this) = true;
474 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
476 this.movement_x = dir * v_forward * maxspeed;
477 this.movement_y = dir * v_right * maxspeed;
482 // Handling of jump pads
483 if(this.jumppadcount)
485 // If got stuck on the jump pad try to reach the farthest visible waypoint
486 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
488 if(fabs(this.velocity.z)<50)
490 entity newgoal = NULL;
491 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
493 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
495 if(trace_fraction < 1)
498 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
504 this.ignoregoal = this.goalcurrent;
505 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
506 navigation_clearroute(this);
507 navigation_routetogoal(this, newgoal, this.origin);
508 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
516 if(this.velocity.z>0)
519 vector velxy = this.velocity; velxy_z = 0;
520 threshold = maxspeed * 0.2;
521 if(vdist(velxy, <, threshold))
523 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
524 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
529 // Don't chase players while using a jump pad
530 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
534 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
535 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
537 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
539 if (!(IS_ONGROUND(this)))
541 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
542 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
543 if(this.items & IT_JETPACK)
545 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
546 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
548 if(this.velocity.z<0)
550 PHYS_INPUT_BUTTON_HOOK(this) = true;
554 PHYS_INPUT_BUTTON_HOOK(this) = true;
556 // If there is no goal try to move forward
558 if(this.goalcurrent==NULL)
561 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
563 vector xyvelocity = this.velocity; xyvelocity_z = 0;
564 float xyspeed = xyvelocity * dir;
566 if(xyspeed < (maxspeed / 2))
568 makevectors(this.v_angle.y * '0 1 0');
569 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
570 if(trace_fraction==1)
572 this.movement_x = dir * v_forward * maxspeed;
573 this.movement_y = dir * v_right * maxspeed;
575 havocbot_keyboard_movement(this, this.origin + dir * 100);
579 this.havocbot_blockhead = true;
583 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
585 if(this.velocity.z < 0)
586 if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
588 this.movement_x = maxspeed;
590 if(this.rocketjumptime)
592 if(time > this.rocketjumptime)
594 PHYS_INPUT_BUTTON_ATCK2(this) = true;
595 this.rocketjumptime = 0;
600 PS(this).m_switchweapon = WEP_DEVASTATOR;
602 PHYS_INPUT_BUTTON_ATCK(this) = true;
603 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
609 // If there is no goal try to move forward
610 if(this.goalcurrent==NULL)
611 this.movement_x = maxspeed;
615 // If we are under water with no goals, swim up
617 if(this.goalcurrent==NULL)
620 if(this.waterlevel>WATERLEVEL_SWIMMING)
622 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
623 PHYS_INPUT_BUTTON_JUMP(this) = true;
625 PHYS_INPUT_BUTTON_JUMP(this) = false;
626 makevectors(this.v_angle.y * '0 1 0');
627 this.movement_x = dir * v_forward * maxspeed;
628 this.movement_y = dir * v_right * maxspeed;
629 this.movement_z = dir * v_up * maxspeed;
632 // if there is nowhere to go, exit
633 if (this.goalcurrent == NULL)
636 if (this.goalcurrent)
637 navigation_poptouchedgoals(this);
639 // if ran out of goals try to use an alternative goal or get a new strategy asap
640 if(this.goalcurrent == NULL)
642 this.bot_strategytime = 0;
647 if(autocvar_bot_debug_goalstack)
648 debuggoalstack(this);
650 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
651 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
652 destorg = this.origin;
653 destorg.x = bound(m1_x, destorg.x, m2_x);
654 destorg.y = bound(m1_y, destorg.y, m2_y);
655 destorg.z = bound(m1_z, destorg.z, m2_z);
656 diff = destorg - this.origin;
658 dir = normalize(diff);
659 flatdir = diff;flatdir.z = 0;
660 flatdir = normalize(flatdir);
661 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
663 //if (this.bot_dodgevector_time < time)
665 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
666 // this.bot_dodgevector_jumpbutton = 1;
667 evadeobstacle = '0 0 0';
672 if(this.waterlevel>WATERLEVEL_SWIMMING)
675 this.aistatus |= AI_STATUS_OUT_WATER;
679 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
680 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
681 PHYS_INPUT_BUTTON_JUMP(this) = true;
683 PHYS_INPUT_BUTTON_JUMP(this) = false;
685 dir = normalize(flatdir);
686 makevectors(this.v_angle.y * '0 1 0');
690 if(this.aistatus & AI_STATUS_OUT_WATER)
691 this.aistatus &= ~AI_STATUS_OUT_WATER;
693 // jump if going toward an obstacle that doesn't look like stairs we
694 // can walk up directly
695 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
696 if (trace_fraction < 1)
697 if (trace_plane_normal.z < 0.7)
700 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
701 if (trace_fraction < s + 0.01)
702 if (trace_plane_normal.z < 0.7)
705 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
706 if (trace_fraction > s)
707 PHYS_INPUT_BUTTON_JUMP(this) = true;
711 // avoiding dangers and obstacles
712 vector dst_ahead, dst_down;
713 makevectors(this.v_angle.y * '0 1 0');
714 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
715 dst_down = dst_ahead - '0 0 1500';
718 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
720 // Check head-banging against walls
721 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
723 PHYS_INPUT_BUTTON_JUMP(this) = true;
724 if(this.facingwalltime && time > this.facingwalltime)
726 this.ignoregoal = this.goalcurrent;
727 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
728 this.bot_strategytime = 0;
733 this.facingwalltime = time + 0.05;
738 this.facingwalltime = 0;
740 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
742 this.ignoregoal = NULL;
743 this.ignoregoaltime = 0;
747 // Check for water/slime/lava and dangerous edges
748 // (only when the bot is on the ground or jumping intentionally)
749 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
751 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
752 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
755 traceline(dst_ahead , dst_down, true, NULL);
756 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
757 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
758 if(trace_endpos.z < this.origin.z + this.mins.z)
760 s = pointcontents(trace_endpos + '0 0 1');
761 if (s != CONTENT_SOLID)
762 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
763 evadelava = normalize(this.velocity) * -1;
764 else if (s == CONTENT_SKY)
765 evadeobstacle = normalize(this.velocity) * -1;
766 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
767 this.goalcurrent.absmin, this.goalcurrent.absmax))
769 // if ain't a safe goal with "holes" (like the jumpad on soylent)
770 // and there is a trigger_hurt below
771 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
773 // Remove dangerous dynamic goals from stack
774 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
775 navigation_clearroute(this);
785 makevectors(this.v_angle.y * '0 1 0');
787 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
788 this.aistatus |= AI_STATUS_DANGER_AHEAD;
791 dodge = havocbot_dodge(this);
792 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
793 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
794 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
795 if(IS_PLAYER(trace_ent))
796 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
798 dir = normalize(dir + dodge + evadeobstacle + evadelava);
799 // this.bot_dodgevector = dir;
800 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
803 if(time < this.ladder_time)
805 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
807 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
812 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
817 //dir = this.bot_dodgevector;
818 //if (this.bot_dodgevector_jumpbutton)
819 // PHYS_INPUT_BUTTON_JUMP(this) = true;
820 this.movement_x = dir * v_forward * maxspeed;
821 this.movement_y = dir * v_right * maxspeed;
822 this.movement_z = dir * v_up * maxspeed;
824 // Emulate keyboard interface
826 havocbot_keyboard_movement(this, destorg);
829 // if(this.aistatus & AI_STATUS_ROAMING)
831 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
832 havocbot_bunnyhop(this, dir);
834 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
835 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;
836 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);
839 void havocbot_chooseenemy(entity this)
841 entity head, best, head2;
842 float rating, bestrating, hf;
844 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
851 if (!bot_shouldattack(this, this.enemy))
853 // enemy died or something, find a new target
855 this.havocbot_chooseenemy_finished = time;
857 else if (this.havocbot_stickenemy)
859 // tracking last chosen enemy
860 // if enemy is visible
861 // and not really really far away
862 // and we're not severely injured
863 // then keep tracking for a half second into the future
864 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
865 if (trace_ent == this.enemy || trace_fraction == 1)
866 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
867 if (this.health > 30)
869 // remain tracking him for a shot while (case he went after a small corner or pilar
870 this.havocbot_chooseenemy_finished = time + 0.5;
873 // enemy isn't visible, or is far away, or we're injured severely
874 // so stop preferring this enemy
875 // (it will still take a half second until a new one is chosen)
876 this.havocbot_stickenemy = 0;
879 if (time < this.havocbot_chooseenemy_finished)
881 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
882 eye = this.origin + this.view_ofs;
884 bestrating = 100000000;
885 head = head2 = findchainfloat(bot_attack, true);
888 hf = this.dphitcontentsmask;
890 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
892 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
894 bool scan_transparent = false;
895 bool scan_secondary_targets = false;
896 bool have_secondary_targets = false;
899 scan_secondary_targets = false;
901 for( ; head; head = head.chain)
903 if(!scan_secondary_targets)
905 if(head.classname == "misc_breakablemodel")
907 have_secondary_targets = true;
913 if(head.classname != "misc_breakablemodel")
917 v = (head.absmin + head.absmax) * 0.5;
918 rating = vlen(v - eye);
919 if (rating<autocvar_bot_ai_enemydetectionradius)
920 if (bestrating > rating)
921 if (bot_shouldattack(this, head))
923 traceline(eye, v, true, this);
924 if (trace_ent == head || trace_fraction >= 1)
932 if(!best && have_secondary_targets && !scan_secondary_targets)
934 scan_secondary_targets = true;
937 bestrating = 100000000;
941 // I want to do a second scan if no enemy was found or I don't have weapons
942 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
943 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
948 // Set flags to see through transparent objects
949 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
952 scan_transparent = true;
956 this.dphitcontentsmask = hf;
959 this.havocbot_stickenemy = true;
960 if(best && best.classname == "misc_breakablemodel")
961 this.havocbot_stickenemy = false;
964 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
966 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
967 // so skip this for them, or they'll never get to reload their weapons at all.
968 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
972 // if this weapon is scheduled for reloading, don't switch to it during combat
973 if (this.weapon_load[new_weapon] < 0)
975 bool other_weapon_available = false;
976 FOREACH(Weapons, it != WEP_Null, LAMBDA(
977 if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
978 other_weapon_available = true;
980 if(other_weapon_available)
987 void havocbot_chooseweapon(entity this)
992 if(g_weaponarena_weapons == WEPSET(TUBA))
994 PS(this).m_switchweapon = WEP_TUBA;
998 // TODO: clean this up by moving it to weapon code
1001 // If no weapon was chosen get the first available weapon
1002 if(PS(this).m_weapon==WEP_Null)
1003 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1004 if(client_hasweapon(this, it, true, false))
1006 PS(this).m_switchweapon = it;
1013 // Do not change weapon during the next second after a combo
1014 float f = time - this.lastcombotime;
1019 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1021 // Should it do a weapon combo?
1022 float af, ct, combo_time, combo;
1024 af = ATTACK_FINISHED(this, 0);
1025 ct = autocvar_bot_ai_weapon_combo_threshold;
1027 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1028 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1029 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1033 if(autocvar_bot_ai_weapon_combo)
1034 if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1038 this.lastcombotime = time;
1041 distance *= pow(2, this.bot_rangepreference);
1043 // Custom weapon list based on distance to the enemy
1044 if(bot_custom_weapon){
1046 // Choose weapons for far distance
1047 if ( distance > bot_distance_far ) {
1048 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1049 w = bot_weapons_far[i];
1050 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1052 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1054 PS(this).m_switchweapon = Weapons_from(w);
1060 // Choose weapons for mid distance
1061 if ( distance > bot_distance_close) {
1062 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1063 w = bot_weapons_mid[i];
1064 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1066 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1068 PS(this).m_switchweapon = Weapons_from(w);
1074 // Choose weapons for close distance
1075 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1076 w = bot_weapons_close[i];
1077 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1079 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1081 PS(this).m_switchweapon = Weapons_from(w);
1088 void havocbot_aim(entity this)
1090 vector myvel, enemyvel;
1091 // if(this.flags & FL_INWATER)
1093 if (time < this.nextaim)
1095 this.nextaim = time + 0.1;
1096 myvel = this.velocity;
1097 if (!this.waterlevel)
1101 enemyvel = this.enemy.velocity;
1102 if (!this.enemy.waterlevel)
1104 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1107 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1110 bool havocbot_moveto_refresh_route(entity this)
1112 // Refresh path to goal if necessary
1114 wp = this.havocbot_personal_waypoint;
1115 navigation_goalrating_start(this);
1116 navigation_routerating(this, wp, 10000, 10000);
1117 navigation_goalrating_end(this);
1118 return this.navigation_hasgoals;
1121 float havocbot_moveto(entity this, vector pos)
1125 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1127 // Step 4: Move to waypoint
1128 if(this.havocbot_personal_waypoint==NULL)
1130 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1131 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1132 return CMD_STATUS_ERROR;
1135 if (!bot_strategytoken_taken)
1136 if(this.havocbot_personal_waypoint_searchtime<time)
1138 bot_strategytoken_taken = true;
1139 if(havocbot_moveto_refresh_route(this))
1141 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1142 this.havocbot_personal_waypoint_searchtime = time + 10;
1143 this.havocbot_personal_waypoint_failcounter = 0;
1147 this.havocbot_personal_waypoint_failcounter += 1;
1148 this.havocbot_personal_waypoint_searchtime = time + 2;
1149 if(this.havocbot_personal_waypoint_failcounter >= 30)
1151 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1152 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1153 delete(this.havocbot_personal_waypoint);
1154 return CMD_STATUS_ERROR;
1157 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1161 if(autocvar_bot_debug_goalstack)
1162 debuggoalstack(this);
1165 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1167 bot_aimdir(this, dir, -1);
1170 havocbot_movetogoal(this);
1172 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1174 // Step 5: Waypoint reached
1175 LOG_TRACE(this.netname, "'s personal waypoint reached");
1176 delete(this.havocbot_personal_waypoint);
1177 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1178 return CMD_STATUS_FINISHED;
1181 return CMD_STATUS_EXECUTING;
1184 // Step 2: Linking waypoint
1185 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1187 // Wait until it is linked
1188 if(!this.havocbot_personal_waypoint.wplinked)
1190 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1191 return CMD_STATUS_EXECUTING;
1194 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1195 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1196 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1198 // Step 3: Route to waypoint
1199 LOG_TRACE(this.netname, " walking to its personal waypoint");
1201 return CMD_STATUS_EXECUTING;
1204 // Step 1: Spawning waypoint
1205 wp = waypoint_spawnpersonal(this, pos);
1208 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1209 return CMD_STATUS_ERROR;
1212 this.havocbot_personal_waypoint = wp;
1213 this.havocbot_personal_waypoint_failcounter = 0;
1214 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1216 // if pos is inside a teleport, then let's mark it as teleport waypoint
1217 FOREACH_ENTITY_CLASS("trigger_teleport", WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1219 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1220 this.lastteleporttime = 0;
1224 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1225 print("routing to a teleporter\n");
1227 print("routing to a non-teleporter\n");
1230 return CMD_STATUS_EXECUTING;
1233 float havocbot_resetgoal(entity this)
1235 navigation_clearroute(this);
1236 return CMD_STATUS_FINISHED;
1239 void havocbot_setupbot(entity this)
1241 this.bot_ai = havocbot_ai;
1242 this.cmd_moveto = havocbot_moveto;
1243 this.cmd_resetgoal = havocbot_resetgoal;
1245 havocbot_chooserole(this);
1248 vector havocbot_dodge(entity this)
1250 // LordHavoc: disabled because this is too expensive
1255 float danger, bestdanger, vl, d;
1258 // check for dangerous objects near bot or approaching bot
1259 head = findchainfloat(bot_dodge, true);
1262 if (head.owner != this)
1264 vl = vlen(head.velocity);
1265 if (vl > autocvar_sv_maxspeed * 0.3)
1267 n = normalize(head.velocity);
1268 v = this.origin - head.origin;
1270 if (d > (0 - head.bot_dodgerating))
1271 if (d < (vl * 0.2 + head.bot_dodgerating))
1273 // calculate direction and distance from the flight path, by removing the forward axis
1274 v = v - (n * (v * n));
1275 danger = head.bot_dodgerating - vlen(v);
1276 if (bestdanger < danger)
1278 bestdanger = danger;
1279 // dodge to the side of the object
1280 dodge = normalize(v);
1286 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1287 if (bestdanger < danger)
1289 bestdanger = danger;
1290 dodge = normalize(this.origin - head.origin);