5 #include "../navigation.qh"
6 #include "../scripting.qh"
7 #include "../waypoints.qh"
9 #include <common/constants.qh>
10 #include <common/physics/player.qh>
11 #include <common/state.qh>
12 #include <common/items/all.qh>
14 #include <common/triggers/trigger/jumppads.qh>
16 #include <lib/warpzone/common.qh>
20 void havocbot_ai(entity this)
25 if(bot_execute_commands(this))
28 if (bot_strategytoken == this)
29 if (!bot_strategytoken_taken)
31 if(this.havocbot_blockhead)
33 this.havocbot_blockhead = false;
37 if (!this.jumppadcount)
38 this.havocbot_role(this); // little too far down the rabbit hole
41 // TODO: tracewalk() should take care of this job (better path finding under water)
42 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
45 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
47 // Look for the closest waypoint out of water
48 entity newgoal = NULL;
49 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
51 if(it.origin.z < this.origin.z)
54 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
57 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
60 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
62 if(trace_fraction < 1)
65 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
71 // te_wizspike(newgoal.origin);
72 navigation_pushroute(this, newgoal);
76 // token has been used this frame
77 bot_strategytoken_taken = true;
83 havocbot_chooseenemy(this);
84 if (this.bot_chooseweapontime < time )
86 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
87 havocbot_chooseweapon(this);
93 this.aistatus |= AI_STATUS_ATTACKING;
94 this.aistatus &= ~AI_STATUS_ROAMING;
98 Weapon w = PS(this).m_weapon;
100 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
102 PHYS_INPUT_BUTTON_ATCK(this) = false;
103 PHYS_INPUT_BUTTON_ATCK2(this) = false;
107 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
108 this.lastfiredweapon = PS(this).m_weapon.m_id;
113 if(IS_PLAYER(this.bot_aimtarg))
114 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
117 else if (this.goalcurrent)
119 this.aistatus |= AI_STATUS_ROAMING;
120 this.aistatus &= ~AI_STATUS_ATTACKING;
122 vector now,v,next;//,heading;
123 float aimdistance,skillblend,distanceblend,blend;
124 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
125 aimdistance = vlen(now);
126 //heading = this.velocity;
127 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
129 this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
130 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
132 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
134 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
135 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
136 blend = skillblend * (1-distanceblend);
137 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
138 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
139 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
140 v = now + blend * (next - now);
141 //dprint(etos(this), " ");
142 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
143 //v = now * (distanceblend) + next * (1-distanceblend);
144 if (this.waterlevel < WATERLEVEL_SWIMMING)
146 //dprint("walk at:", vtos(v), "\n");
147 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
148 bot_aimdir(this, v, -1);
150 havocbot_movetogoal(this);
152 // if the bot is not attacking, consider reloading weapons
153 if (!(this.aistatus & AI_STATUS_ATTACKING))
155 // we are currently holding a weapon that's not fully loaded, reload it
156 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
157 if(this.clip_load < this.clip_size)
158 this.impulse = 20; // "press" the reload button, not sure if this is done right
160 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
161 // the code above executes next frame, starting the reloading then
162 if(skill >= 5) // bots can only look for unloaded weapons past this skill
163 if(this.clip_load >= 0) // only if we're not reloading a weapon already
165 FOREACH(Weapons, it != WEP_Null, LAMBDA(
166 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
167 PS(this).m_switchweapon = it;
173 void havocbot_keyboard_movement(entity this, vector destorg)
176 float blend, maxspeed;
179 sk = skill + this.bot_moveskill;
181 maxspeed = autocvar_sv_maxspeed;
183 if (time < this.havocbot_keyboardtime)
186 this.havocbot_keyboardtime =
188 this.havocbot_keyboardtime
189 + 0.05/max(1, sk+this.havocbot_keyboardskill)
190 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
192 keyboard = this.movement * (1.0 / maxspeed);
194 float trigger, trigger1;
195 blend = bound(0,sk*0.1,1);
196 trigger = autocvar_bot_ai_keyboard_threshold;
197 trigger1 = 0 - trigger;
199 // categorize forward movement
200 // at skill < 1.5 only forward
201 // at skill < 2.5 only individual directions
202 // at skill < 4.5 only individual directions, and forward diagonals
203 // at skill >= 4.5, all cases allowed
204 if (keyboard.x > trigger)
210 else if (keyboard.x < trigger1 && sk > 1.5)
225 if (keyboard.y > trigger)
227 else if (keyboard.y < trigger1)
232 if (keyboard.z > trigger)
234 else if (keyboard.z < trigger1)
239 this.havocbot_keyboard = keyboard * maxspeed;
240 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
242 keyboard = this.havocbot_keyboard;
243 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
244 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
245 this.movement = this.movement + (keyboard - this.movement) * blend;
248 void havocbot_bunnyhop(entity this, vector dir)
250 float bunnyhopdistance;
255 // Don't jump when attacking
256 if(this.aistatus & AI_STATUS_ATTACKING)
259 if(IS_PLAYER(this.goalcurrent))
262 maxspeed = autocvar_sv_maxspeed;
264 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
266 this.aistatus &= ~AI_STATUS_RUNNING;
267 PHYS_INPUT_BUTTON_JUMP(this) = false;
268 this.bot_canruntogoal = 0;
269 this.bot_timelastseengoal = 0;
273 if(this.waterlevel > WATERLEVEL_WETFEET)
275 this.aistatus &= ~AI_STATUS_RUNNING;
279 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
281 this.bot_canruntogoal = 0;
282 this.bot_timelastseengoal = 0;
285 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
286 bunnyhopdistance = vlen(this.origin - gco);
288 // Run only to visible goals
289 if(IS_ONGROUND(this))
290 if(this.speed==maxspeed)
291 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
293 this.bot_lastseengoal = this.goalcurrent;
296 if(this.bot_timelastseengoal)
298 // for a period of time
299 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
302 checkdistance = true;
304 // don't run if it is too close
305 if(this.bot_canruntogoal==0)
307 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
308 this.bot_canruntogoal = 1;
310 this.bot_canruntogoal = -1;
313 if(this.bot_canruntogoal != 1)
316 if(this.aistatus & AI_STATUS_ROAMING)
317 if(this.goalcurrent.classname=="waypoint")
318 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
319 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
320 if(this.goalstack01!=NULL)
322 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
323 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
324 while (deviation.y < -180) deviation.y = deviation.y + 360;
325 while (deviation.y > 180) deviation.y = deviation.y - 360;
327 if(fabs(deviation.y) < 20)
328 if(bunnyhopdistance < vlen(this.origin - gno))
329 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
331 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
332 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
334 checkdistance = false;
341 this.aistatus &= ~AI_STATUS_RUNNING;
342 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
343 PHYS_INPUT_BUTTON_JUMP(this) = true;
347 this.aistatus |= AI_STATUS_RUNNING;
348 PHYS_INPUT_BUTTON_JUMP(this) = true;
354 this.bot_timelastseengoal = time;
359 this.bot_timelastseengoal = 0;
363 // Release jump button
364 if(!cvar("sv_pogostick"))
365 if((IS_ONGROUND(this)) == 0)
367 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
368 PHYS_INPUT_BUTTON_JUMP(this) = false;
371 if(this.aistatus & AI_STATUS_RUNNING)
372 if(vlen(this.velocity)>maxspeed)
374 deviation = vectoangles(dir) - vectoangles(this.velocity);
375 while (deviation.y < -180) deviation.y = deviation.y + 360;
376 while (deviation.y > 180) deviation.y = deviation.y - 360;
378 if(fabs(deviation.y)>10)
382 this.movement_y = maxspeed * -1;
383 else if(deviation.y<10)
384 this.movement_y = maxspeed;
391 void havocbot_movetogoal(entity this)
399 vector evadeobstacle;
406 //if (this.goalentity)
407 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
408 this.movement = '0 0 0';
409 maxspeed = autocvar_sv_maxspeed;
411 // Jetpack navigation
413 if(this.navigation_jetpack_goal)
414 if(this.goalcurrent==this.navigation_jetpack_goal)
417 if(autocvar_bot_debug_goalstack)
419 debuggoalstack(this);
420 te_wizspike(this.navigation_jetpack_point);
424 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
426 // Brake almost completely so it can get a good direction
427 if(vdist(this.velocity, >, 10))
429 this.aistatus |= AI_STATUS_JETPACK_FLYING;
432 makevectors(this.v_angle.y * '0 1 0');
433 dir = normalize(this.navigation_jetpack_point - this.origin);
436 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
438 // Calculate brake distance in xy
442 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
444 v = vlen(this.velocity - this.velocity.z * '0 0 1');
445 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
446 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
447 if(d < db || d < 500)
450 if(fabs(this.velocity.x)>maxspeed*0.3)
452 this.movement_x = dir * v_forward * -maxspeed;
455 // Switch to normal mode
456 this.navigation_jetpack_goal = NULL;
457 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
458 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
462 else if(checkpvs(this.origin,this.goalcurrent))
464 // If I can see the goal switch to landing code
465 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
466 this.aistatus |= AI_STATUS_JETPACK_LANDING;
471 PHYS_INPUT_BUTTON_HOOK(this) = true;
472 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
474 this.movement_x = dir * v_forward * maxspeed;
475 this.movement_y = dir * v_right * maxspeed;
480 // Handling of jump pads
481 if(this.jumppadcount)
483 // If got stuck on the jump pad try to reach the farthest visible waypoint
484 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
486 if(fabs(this.velocity.z)<50)
488 entity newgoal = NULL;
489 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
491 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
493 if(trace_fraction < 1)
496 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
502 this.ignoregoal = this.goalcurrent;
503 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
504 navigation_clearroute(this);
505 navigation_routetogoal(this, newgoal, this.origin);
506 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
514 if(this.velocity.z>0)
517 vector velxy = this.velocity; velxy_z = 0;
518 threshold = maxspeed * 0.2;
519 if(vdist(velxy, <, threshold))
521 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now\n");
522 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
527 // Don't chase players while using a jump pad
528 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
532 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
533 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
535 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
537 if (!(IS_ONGROUND(this)))
539 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
540 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
541 if(this.items & IT_JETPACK)
543 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
544 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
546 if(this.velocity.z<0)
548 PHYS_INPUT_BUTTON_HOOK(this) = true;
552 PHYS_INPUT_BUTTON_HOOK(this) = true;
554 // If there is no goal try to move forward
556 if(this.goalcurrent==NULL)
559 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
561 vector xyvelocity = this.velocity; xyvelocity_z = 0;
562 float xyspeed = xyvelocity * dir;
564 if(xyspeed < (maxspeed / 2))
566 makevectors(this.v_angle.y * '0 1 0');
567 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
568 if(trace_fraction==1)
570 this.movement_x = dir * v_forward * maxspeed;
571 this.movement_y = dir * v_right * maxspeed;
573 havocbot_keyboard_movement(this, this.origin + dir * 100);
577 this.havocbot_blockhead = true;
581 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
583 if(this.velocity.z < 0)
584 if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
586 this.movement_x = maxspeed;
588 if(this.rocketjumptime)
590 if(time > this.rocketjumptime)
592 PHYS_INPUT_BUTTON_ATCK2(this) = true;
593 this.rocketjumptime = 0;
598 PS(this).m_switchweapon = WEP_DEVASTATOR;
600 PHYS_INPUT_BUTTON_ATCK(this) = true;
601 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
607 // If there is no goal try to move forward
608 if(this.goalcurrent==NULL)
609 this.movement_x = maxspeed;
613 // If we are under water with no goals, swim up
615 if(this.goalcurrent==NULL)
618 if(this.waterlevel>WATERLEVEL_SWIMMING)
620 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
621 PHYS_INPUT_BUTTON_JUMP(this) = true;
623 PHYS_INPUT_BUTTON_JUMP(this) = false;
624 makevectors(this.v_angle.y * '0 1 0');
625 this.movement_x = dir * v_forward * maxspeed;
626 this.movement_y = dir * v_right * maxspeed;
627 this.movement_z = dir * v_up * maxspeed;
630 // if there is nowhere to go, exit
631 if (this.goalcurrent == NULL)
634 if (this.goalcurrent)
635 navigation_poptouchedgoals(this);
637 // if ran out of goals try to use an alternative goal or get a new strategy asap
638 if(this.goalcurrent == NULL)
640 this.bot_strategytime = 0;
645 if(autocvar_bot_debug_goalstack)
646 debuggoalstack(this);
648 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
649 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
650 destorg = this.origin;
651 destorg.x = bound(m1_x, destorg.x, m2_x);
652 destorg.y = bound(m1_y, destorg.y, m2_y);
653 destorg.z = bound(m1_z, destorg.z, m2_z);
654 diff = destorg - this.origin;
656 dir = normalize(diff);
657 flatdir = diff;flatdir.z = 0;
658 flatdir = normalize(flatdir);
659 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
661 //if (this.bot_dodgevector_time < time)
663 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
664 // this.bot_dodgevector_jumpbutton = 1;
665 evadeobstacle = '0 0 0';
670 if(this.waterlevel>WATERLEVEL_SWIMMING)
673 this.aistatus |= AI_STATUS_OUT_WATER;
677 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
678 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
679 PHYS_INPUT_BUTTON_JUMP(this) = true;
681 PHYS_INPUT_BUTTON_JUMP(this) = false;
683 dir = normalize(flatdir);
684 makevectors(this.v_angle.y * '0 1 0');
688 if(this.aistatus & AI_STATUS_OUT_WATER)
689 this.aistatus &= ~AI_STATUS_OUT_WATER;
691 // jump if going toward an obstacle that doesn't look like stairs we
692 // can walk up directly
693 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
694 if (trace_fraction < 1)
695 if (trace_plane_normal.z < 0.7)
698 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
699 if (trace_fraction < s + 0.01)
700 if (trace_plane_normal.z < 0.7)
703 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
704 if (trace_fraction > s)
705 PHYS_INPUT_BUTTON_JUMP(this) = true;
709 // avoiding dangers and obstacles
710 vector dst_ahead, dst_down;
711 makevectors(this.v_angle.y * '0 1 0');
712 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
713 dst_down = dst_ahead - '0 0 1500';
716 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
718 // Check head-banging against walls
719 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
721 PHYS_INPUT_BUTTON_JUMP(this) = true;
722 if(this.facingwalltime && time > this.facingwalltime)
724 this.ignoregoal = this.goalcurrent;
725 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
726 this.bot_strategytime = 0;
731 this.facingwalltime = time + 0.05;
736 this.facingwalltime = 0;
738 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
740 this.ignoregoal = NULL;
741 this.ignoregoaltime = 0;
745 // Check for water/slime/lava and dangerous edges
746 // (only when the bot is on the ground or jumping intentionally)
747 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
749 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
750 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
753 traceline(dst_ahead , dst_down, true, NULL);
754 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
755 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
756 if(trace_endpos.z < this.origin.z + this.mins.z)
758 s = pointcontents(trace_endpos + '0 0 1');
759 if (s != CONTENT_SOLID)
760 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
761 evadelava = normalize(this.velocity) * -1;
762 else if (s == CONTENT_SKY)
763 evadeobstacle = normalize(this.velocity) * -1;
764 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
765 this.goalcurrent.absmin, this.goalcurrent.absmax))
767 // if ain't a safe goal with "holes" (like the jumpad on soylent)
768 // and there is a trigger_hurt below
769 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
771 // Remove dangerous dynamic goals from stack
772 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared\n");
773 navigation_clearroute(this);
783 makevectors(this.v_angle.y * '0 1 0');
785 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
786 this.aistatus |= AI_STATUS_DANGER_AHEAD;
789 dodge = havocbot_dodge(this);
790 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
791 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
792 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
793 if(IS_PLAYER(trace_ent))
794 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
796 dir = normalize(dir + dodge + evadeobstacle + evadelava);
797 // this.bot_dodgevector = dir;
798 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
801 if(time < this.ladder_time)
803 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
805 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
810 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
815 //dir = this.bot_dodgevector;
816 //if (this.bot_dodgevector_jumpbutton)
817 // PHYS_INPUT_BUTTON_JUMP(this) = true;
818 this.movement_x = dir * v_forward * maxspeed;
819 this.movement_y = dir * v_right * maxspeed;
820 this.movement_z = dir * v_up * maxspeed;
822 // Emulate keyboard interface
824 havocbot_keyboard_movement(this, destorg);
827 // if(this.aistatus & AI_STATUS_ROAMING)
829 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
830 havocbot_bunnyhop(this, dir);
832 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
833 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;
834 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);
837 void havocbot_chooseenemy(entity this)
839 entity head, best, head2;
840 float rating, bestrating, hf;
842 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
849 if (!bot_shouldattack(this, this.enemy))
851 // enemy died or something, find a new target
853 this.havocbot_chooseenemy_finished = time;
855 else if (this.havocbot_stickenemy)
857 // tracking last chosen enemy
858 // if enemy is visible
859 // and not really really far away
860 // and we're not severely injured
861 // then keep tracking for a half second into the future
862 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
863 if (trace_ent == this.enemy || trace_fraction == 1)
864 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
865 if (this.health > 30)
867 // remain tracking him for a shot while (case he went after a small corner or pilar
868 this.havocbot_chooseenemy_finished = time + 0.5;
871 // enemy isn't visible, or is far away, or we're injured severely
872 // so stop preferring this enemy
873 // (it will still take a half second until a new one is chosen)
874 this.havocbot_stickenemy = 0;
877 if (time < this.havocbot_chooseenemy_finished)
879 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
880 eye = this.origin + this.view_ofs;
882 bestrating = 100000000;
883 head = head2 = findchainfloat(bot_attack, true);
886 hf = this.dphitcontentsmask;
888 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
890 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
892 bool scan_transparent = false;
893 bool scan_secondary_targets = false;
894 bool have_secondary_targets = false;
897 scan_secondary_targets = false;
899 for( ; head; head = head.chain)
901 if(!scan_secondary_targets)
903 if(head.classname == "misc_breakablemodel")
905 have_secondary_targets = true;
911 if(head.classname != "misc_breakablemodel")
915 v = (head.absmin + head.absmax) * 0.5;
916 rating = vlen(v - eye);
917 if (rating<autocvar_bot_ai_enemydetectionradius)
918 if (bestrating > rating)
919 if (bot_shouldattack(this, head))
921 traceline(eye, v, true, this);
922 if (trace_ent == head || trace_fraction >= 1)
930 if(!best && have_secondary_targets && !scan_secondary_targets)
932 scan_secondary_targets = true;
935 bestrating = 100000000;
939 // I want to do a second scan if no enemy was found or I don't have weapons
940 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
941 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
946 // Set flags to see through transparent objects
947 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
950 scan_transparent = true;
954 this.dphitcontentsmask = hf;
957 this.havocbot_stickenemy = true;
958 if(best && best.classname == "misc_breakablemodel")
959 this.havocbot_stickenemy = false;
962 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
964 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
965 // so skip this for them, or they'll never get to reload their weapons at all.
966 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
970 // if this weapon is scheduled for reloading, don't switch to it during combat
971 if (this.weapon_load[new_weapon] < 0)
973 bool other_weapon_available = false;
974 FOREACH(Weapons, it != WEP_Null, LAMBDA(
975 if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
976 other_weapon_available = true;
978 if(other_weapon_available)
985 void havocbot_chooseweapon(entity this)
990 if(g_weaponarena_weapons == WEPSET(TUBA))
992 PS(this).m_switchweapon = WEP_TUBA;
996 // TODO: clean this up by moving it to weapon code
999 // If no weapon was chosen get the first available weapon
1000 if(PS(this).m_weapon==WEP_Null)
1001 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1002 if(client_hasweapon(this, it, true, false))
1004 PS(this).m_switchweapon = it;
1011 // Do not change weapon during the next second after a combo
1012 float f = time - this.lastcombotime;
1017 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1019 // Should it do a weapon combo?
1020 float af, ct, combo_time, combo;
1022 af = ATTACK_FINISHED(this, 0);
1023 ct = autocvar_bot_ai_weapon_combo_threshold;
1025 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1026 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1027 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1031 if(autocvar_bot_ai_weapon_combo)
1032 if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1036 this.lastcombotime = time;
1039 distance *= pow(2, this.bot_rangepreference);
1041 // Custom weapon list based on distance to the enemy
1042 if(bot_custom_weapon){
1044 // Choose weapons for far distance
1045 if ( distance > bot_distance_far ) {
1046 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1047 w = bot_weapons_far[i];
1048 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1050 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1052 PS(this).m_switchweapon = Weapons_from(w);
1058 // Choose weapons for mid distance
1059 if ( distance > bot_distance_close) {
1060 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1061 w = bot_weapons_mid[i];
1062 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1064 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1066 PS(this).m_switchweapon = Weapons_from(w);
1072 // Choose weapons for close distance
1073 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1074 w = bot_weapons_close[i];
1075 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1077 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1079 PS(this).m_switchweapon = Weapons_from(w);
1086 void havocbot_aim(entity this)
1088 vector myvel, enemyvel;
1089 // if(this.flags & FL_INWATER)
1091 if (time < this.nextaim)
1093 this.nextaim = time + 0.1;
1094 myvel = this.velocity;
1095 if (!this.waterlevel)
1099 enemyvel = this.enemy.velocity;
1100 if (!this.enemy.waterlevel)
1102 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1105 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1108 bool havocbot_moveto_refresh_route(entity this)
1110 // Refresh path to goal if necessary
1112 wp = this.havocbot_personal_waypoint;
1113 navigation_goalrating_start(this);
1114 navigation_routerating(this, wp, 10000, 10000);
1115 navigation_goalrating_end(this);
1116 return this.navigation_hasgoals;
1119 float havocbot_moveto(entity this, vector pos)
1123 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1125 // Step 4: Move to waypoint
1126 if(this.havocbot_personal_waypoint==NULL)
1128 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint\n");
1129 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1130 return CMD_STATUS_ERROR;
1133 if (!bot_strategytoken_taken)
1134 if(this.havocbot_personal_waypoint_searchtime<time)
1136 bot_strategytoken_taken = true;
1137 if(havocbot_moveto_refresh_route(this))
1139 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1140 this.havocbot_personal_waypoint_searchtime = time + 10;
1141 this.havocbot_personal_waypoint_failcounter = 0;
1145 this.havocbot_personal_waypoint_failcounter += 1;
1146 this.havocbot_personal_waypoint_searchtime = time + 2;
1147 if(this.havocbot_personal_waypoint_failcounter >= 30)
1149 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin),"\n");
1150 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1151 remove(this.havocbot_personal_waypoint);
1152 return CMD_STATUS_ERROR;
1155 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1159 if(autocvar_bot_debug_goalstack)
1160 debuggoalstack(this);
1163 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1165 bot_aimdir(this, dir, -1);
1168 havocbot_movetogoal(this);
1170 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1172 // Step 5: Waypoint reached
1173 LOG_TRACE(this.netname, "'s personal waypoint reached\n");
1174 remove(this.havocbot_personal_waypoint);
1175 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1176 return CMD_STATUS_FINISHED;
1179 return CMD_STATUS_EXECUTING;
1182 // Step 2: Linking waypoint
1183 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1185 // Wait until it is linked
1186 if(!this.havocbot_personal_waypoint.wplinked)
1188 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked\n");
1189 return CMD_STATUS_EXECUTING;
1192 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1193 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1194 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1196 // Step 3: Route to waypoint
1197 LOG_TRACE(this.netname, " walking to its personal waypoint\n");
1199 return CMD_STATUS_EXECUTING;
1202 // Step 1: Spawning waypoint
1203 wp = waypoint_spawnpersonal(this, pos);
1206 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1207 return CMD_STATUS_ERROR;
1210 this.havocbot_personal_waypoint = wp;
1211 this.havocbot_personal_waypoint_failcounter = 0;
1212 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1214 // if pos is inside a teleport, then let's mark it as teleport waypoint
1215 FOREACH_ENTITY_CLASS("trigger_teleport", WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1217 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1218 this.lastteleporttime = 0;
1222 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1223 print("routing to a teleporter\n");
1225 print("routing to a non-teleporter\n");
1228 return CMD_STATUS_EXECUTING;
1231 float havocbot_resetgoal(entity this)
1233 navigation_clearroute(this);
1234 return CMD_STATUS_FINISHED;
1237 void havocbot_setupbot(entity this)
1239 this.bot_ai = havocbot_ai;
1240 this.cmd_moveto = havocbot_moveto;
1241 this.cmd_resetgoal = havocbot_resetgoal;
1243 havocbot_chooserole(this);
1246 vector havocbot_dodge(entity this)
1248 // LordHavoc: disabled because this is too expensive
1253 float danger, bestdanger, vl, d;
1256 // check for dangerous objects near bot or approaching bot
1257 head = findchainfloat(bot_dodge, true);
1260 if (head.owner != this)
1262 vl = vlen(head.velocity);
1263 if (vl > autocvar_sv_maxspeed * 0.3)
1265 n = normalize(head.velocity);
1266 v = this.origin - head.origin;
1268 if (d > (0 - head.bot_dodgerating))
1269 if (d < (vl * 0.2 + head.bot_dodgerating))
1271 // calculate direction and distance from the flight path, by removing the forward axis
1272 v = v - (n * (v * n));
1273 danger = head.bot_dodgerating - vlen(v);
1274 if (bestdanger < danger)
1276 bestdanger = danger;
1277 // dodge to the side of the object
1278 dodge = normalize(v);
1284 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1285 if (bestdanger < danger)
1287 bestdanger = danger;
1288 dodge = normalize(this.origin - head.origin);