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>
16 #include <common/triggers/teleporters.qh>
17 #include <common/triggers/trigger/jumppads.qh>
19 #include <lib/warpzone/common.qh>
23 void havocbot_ai(entity this)
28 if(bot_execute_commands(this))
31 if (bot_strategytoken == this)
32 if (!bot_strategytoken_taken)
34 if(this.havocbot_blockhead)
36 this.havocbot_blockhead = false;
40 if (!this.jumppadcount)
41 this.havocbot_role(this); // little too far down the rabbit hole
44 // TODO: tracewalk() should take care of this job (better path finding under water)
45 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
48 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
50 // Look for the closest waypoint out of water
51 entity newgoal = NULL;
52 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
54 if(it.origin.z < this.origin.z)
57 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
60 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
63 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
65 if(trace_fraction < 1)
68 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
74 // te_wizspike(newgoal.origin);
75 navigation_pushroute(this, newgoal);
79 // token has been used this frame
80 bot_strategytoken_taken = true;
86 havocbot_chooseenemy(this);
87 if (this.bot_chooseweapontime < time )
89 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
90 havocbot_chooseweapon(this);
96 this.aistatus |= AI_STATUS_ATTACKING;
97 this.aistatus &= ~AI_STATUS_ROAMING;
101 Weapon w = PS(this).m_weapon;
103 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
105 PHYS_INPUT_BUTTON_ATCK(this) = false;
106 PHYS_INPUT_BUTTON_ATCK2(this) = false;
110 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
111 this.lastfiredweapon = PS(this).m_weapon.m_id;
116 if(IS_PLAYER(this.bot_aimtarg))
117 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
120 else if (this.goalcurrent)
122 this.aistatus |= AI_STATUS_ROAMING;
123 this.aistatus &= ~AI_STATUS_ATTACKING;
125 vector now,v,next;//,heading;
126 float aimdistance,skillblend,distanceblend,blend;
127 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
128 aimdistance = vlen(now);
129 //heading = this.velocity;
130 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
132 this.goalstack01 != this && this.goalstack01 != NULL && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
133 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
135 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
137 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
138 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
139 blend = skillblend * (1-distanceblend);
140 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
141 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
142 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
143 v = now + blend * (next - now);
144 //dprint(etos(this), " ");
145 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
146 //v = now * (distanceblend) + next * (1-distanceblend);
147 if (this.waterlevel < WATERLEVEL_SWIMMING)
149 //dprint("walk at:", vtos(v), "\n");
150 //te_lightning2(NULL, this.origin, this.goalcurrent.origin);
151 bot_aimdir(this, v, -1);
153 havocbot_movetogoal(this);
155 // if the bot is not attacking, consider reloading weapons
156 if (!(this.aistatus & AI_STATUS_ATTACKING))
158 // we are currently holding a weapon that's not fully loaded, reload it
159 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
160 if(this.clip_load < this.clip_size)
161 this.impulse = 20; // "press" the reload button, not sure if this is done right
163 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
164 // the code above executes next frame, starting the reloading then
165 if(skill >= 5) // bots can only look for unloaded weapons past this skill
166 if(this.clip_load >= 0) // only if we're not reloading a weapon already
168 FOREACH(Weapons, it != WEP_Null, LAMBDA(
169 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
170 PS(this).m_switchweapon = it;
176 void havocbot_keyboard_movement(entity this, vector destorg)
179 float blend, maxspeed;
182 sk = skill + this.bot_moveskill;
184 maxspeed = autocvar_sv_maxspeed;
186 if (time < this.havocbot_keyboardtime)
189 this.havocbot_keyboardtime =
191 this.havocbot_keyboardtime
192 + 0.05/max(1, sk+this.havocbot_keyboardskill)
193 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
195 keyboard = this.movement * (1.0 / maxspeed);
197 float trigger, trigger1;
198 blend = bound(0,sk*0.1,1);
199 trigger = autocvar_bot_ai_keyboard_threshold;
200 trigger1 = 0 - trigger;
202 // categorize forward movement
203 // at skill < 1.5 only forward
204 // at skill < 2.5 only individual directions
205 // at skill < 4.5 only individual directions, and forward diagonals
206 // at skill >= 4.5, all cases allowed
207 if (keyboard.x > trigger)
213 else if (keyboard.x < trigger1 && sk > 1.5)
228 if (keyboard.y > trigger)
230 else if (keyboard.y < trigger1)
235 if (keyboard.z > trigger)
237 else if (keyboard.z < trigger1)
242 this.havocbot_keyboard = keyboard * maxspeed;
243 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
245 keyboard = this.havocbot_keyboard;
246 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
247 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
248 this.movement = this.movement + (keyboard - this.movement) * blend;
251 void havocbot_bunnyhop(entity this, vector dir)
253 float bunnyhopdistance;
258 // Don't jump when attacking
259 if(this.aistatus & AI_STATUS_ATTACKING)
262 if(IS_PLAYER(this.goalcurrent))
265 maxspeed = autocvar_sv_maxspeed;
267 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
269 this.aistatus &= ~AI_STATUS_RUNNING;
270 PHYS_INPUT_BUTTON_JUMP(this) = false;
271 this.bot_canruntogoal = 0;
272 this.bot_timelastseengoal = 0;
276 if(this.waterlevel > WATERLEVEL_WETFEET)
278 this.aistatus &= ~AI_STATUS_RUNNING;
282 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
284 this.bot_canruntogoal = 0;
285 this.bot_timelastseengoal = 0;
288 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
289 bunnyhopdistance = vlen(this.origin - gco);
291 // Run only to visible goals
292 if(IS_ONGROUND(this))
293 if(this.speed==maxspeed)
294 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
296 this.bot_lastseengoal = this.goalcurrent;
299 if(this.bot_timelastseengoal)
301 // for a period of time
302 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
305 checkdistance = true;
307 // don't run if it is too close
308 if(this.bot_canruntogoal==0)
310 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
311 this.bot_canruntogoal = 1;
313 this.bot_canruntogoal = -1;
316 if(this.bot_canruntogoal != 1)
319 if(this.aistatus & AI_STATUS_ROAMING)
320 if(this.goalcurrent.classname=="waypoint")
321 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
322 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
323 if(this.goalstack01!=NULL)
325 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
326 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
327 while (deviation.y < -180) deviation.y = deviation.y + 360;
328 while (deviation.y > 180) deviation.y = deviation.y - 360;
330 if(fabs(deviation.y) < 20)
331 if(bunnyhopdistance < vlen(this.origin - gno))
332 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
334 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
335 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
337 checkdistance = false;
344 this.aistatus &= ~AI_STATUS_RUNNING;
345 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
346 PHYS_INPUT_BUTTON_JUMP(this) = true;
350 this.aistatus |= AI_STATUS_RUNNING;
351 PHYS_INPUT_BUTTON_JUMP(this) = true;
357 this.bot_timelastseengoal = time;
362 this.bot_timelastseengoal = 0;
366 // Release jump button
367 if(!cvar("sv_pogostick"))
368 if((IS_ONGROUND(this)) == 0)
370 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
371 PHYS_INPUT_BUTTON_JUMP(this) = false;
374 if(this.aistatus & AI_STATUS_RUNNING)
375 if(vlen(this.velocity)>maxspeed)
377 deviation = vectoangles(dir) - vectoangles(this.velocity);
378 while (deviation.y < -180) deviation.y = deviation.y + 360;
379 while (deviation.y > 180) deviation.y = deviation.y - 360;
381 if(fabs(deviation.y)>10)
385 this.movement_y = maxspeed * -1;
386 else if(deviation.y<10)
387 this.movement_y = maxspeed;
394 void havocbot_movetogoal(entity this)
402 vector evadeobstacle;
409 //if (this.goalentity)
410 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
411 this.movement = '0 0 0';
412 maxspeed = autocvar_sv_maxspeed;
414 // Jetpack navigation
416 if(this.navigation_jetpack_goal)
417 if(this.goalcurrent==this.navigation_jetpack_goal)
420 if(autocvar_bot_debug_goalstack)
422 debuggoalstack(this);
423 te_wizspike(this.navigation_jetpack_point);
427 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
429 // Brake almost completely so it can get a good direction
430 if(vdist(this.velocity, >, 10))
432 this.aistatus |= AI_STATUS_JETPACK_FLYING;
435 makevectors(this.v_angle.y * '0 1 0');
436 dir = normalize(this.navigation_jetpack_point - this.origin);
439 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
441 // Calculate brake distance in xy
445 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
447 v = vlen(this.velocity - this.velocity.z * '0 0 1');
448 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
449 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
450 if(d < db || d < 500)
453 if(fabs(this.velocity.x)>maxspeed*0.3)
455 this.movement_x = dir * v_forward * -maxspeed;
458 // Switch to normal mode
459 this.navigation_jetpack_goal = NULL;
460 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
461 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
465 else if(checkpvs(this.origin,this.goalcurrent))
467 // If I can see the goal switch to landing code
468 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
469 this.aistatus |= AI_STATUS_JETPACK_LANDING;
474 PHYS_INPUT_BUTTON_HOOK(this) = true;
475 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
477 this.movement_x = dir * v_forward * maxspeed;
478 this.movement_y = dir * v_right * maxspeed;
483 // Handling of jump pads
484 if(this.jumppadcount)
486 // If got stuck on the jump pad try to reach the farthest visible waypoint
487 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
489 if(fabs(this.velocity.z)<50)
491 entity newgoal = NULL;
492 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
494 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
496 if(trace_fraction < 1)
499 if(!newgoal || vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin))
505 this.ignoregoal = this.goalcurrent;
506 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
507 navigation_clearroute(this);
508 navigation_routetogoal(this, newgoal, this.origin);
509 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
517 if(this.velocity.z>0)
520 vector velxy = this.velocity; velxy_z = 0;
521 threshold = maxspeed * 0.2;
522 if(vdist(velxy, <, threshold))
524 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
525 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
530 // Don't chase players while using a jump pad
531 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
535 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
536 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
538 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
540 if (!(IS_ONGROUND(this)))
542 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
543 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
544 if(this.items & IT_JETPACK)
546 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
547 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
549 if(this.velocity.z<0)
551 PHYS_INPUT_BUTTON_HOOK(this) = true;
555 PHYS_INPUT_BUTTON_HOOK(this) = true;
557 // If there is no goal try to move forward
559 if(this.goalcurrent==NULL)
562 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
564 vector xyvelocity = this.velocity; xyvelocity_z = 0;
565 float xyspeed = xyvelocity * dir;
567 if(xyspeed < (maxspeed / 2))
569 makevectors(this.v_angle.y * '0 1 0');
570 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
571 if(trace_fraction==1)
573 this.movement_x = dir * v_forward * maxspeed;
574 this.movement_y = dir * v_right * maxspeed;
576 havocbot_keyboard_movement(this, this.origin + dir * 100);
580 this.havocbot_blockhead = true;
584 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
586 if(this.velocity.z < 0)
587 if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
589 this.movement_x = maxspeed;
591 if(this.rocketjumptime)
593 if(time > this.rocketjumptime)
595 PHYS_INPUT_BUTTON_ATCK2(this) = true;
596 this.rocketjumptime = 0;
601 PS(this).m_switchweapon = WEP_DEVASTATOR;
603 PHYS_INPUT_BUTTON_ATCK(this) = true;
604 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
610 // If there is no goal try to move forward
611 if(this.goalcurrent==NULL)
612 this.movement_x = maxspeed;
616 // If we are under water with no goals, swim up
618 if(this.goalcurrent==NULL)
621 if(this.waterlevel>WATERLEVEL_SWIMMING)
623 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
624 PHYS_INPUT_BUTTON_JUMP(this) = true;
626 PHYS_INPUT_BUTTON_JUMP(this) = false;
627 makevectors(this.v_angle.y * '0 1 0');
628 this.movement_x = dir * v_forward * maxspeed;
629 this.movement_y = dir * v_right * maxspeed;
630 this.movement_z = dir * v_up * maxspeed;
633 // if there is nowhere to go, exit
634 if (this.goalcurrent == NULL)
637 if (this.goalcurrent)
638 navigation_poptouchedgoals(this);
640 // if ran out of goals try to use an alternative goal or get a new strategy asap
641 if(this.goalcurrent == NULL)
643 this.bot_strategytime = 0;
648 if(autocvar_bot_debug_goalstack)
649 debuggoalstack(this);
651 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
652 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
653 destorg = this.origin;
654 destorg.x = bound(m1_x, destorg.x, m2_x);
655 destorg.y = bound(m1_y, destorg.y, m2_y);
656 destorg.z = bound(m1_z, destorg.z, m2_z);
657 diff = destorg - this.origin;
659 dir = normalize(diff);
660 flatdir = diff;flatdir.z = 0;
661 flatdir = normalize(flatdir);
662 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
664 //if (this.bot_dodgevector_time < time)
666 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
667 // this.bot_dodgevector_jumpbutton = 1;
668 evadeobstacle = '0 0 0';
673 if(this.waterlevel>WATERLEVEL_SWIMMING)
676 this.aistatus |= AI_STATUS_OUT_WATER;
680 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
681 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
682 PHYS_INPUT_BUTTON_JUMP(this) = true;
684 PHYS_INPUT_BUTTON_JUMP(this) = false;
686 dir = normalize(flatdir);
687 makevectors(this.v_angle.y * '0 1 0');
691 if(this.aistatus & AI_STATUS_OUT_WATER)
692 this.aistatus &= ~AI_STATUS_OUT_WATER;
694 // jump if going toward an obstacle that doesn't look like stairs we
695 // can walk up directly
696 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
697 if (trace_fraction < 1)
698 if (trace_plane_normal.z < 0.7)
701 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
702 if (trace_fraction < s + 0.01)
703 if (trace_plane_normal.z < 0.7)
706 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
707 if (trace_fraction > s)
708 PHYS_INPUT_BUTTON_JUMP(this) = true;
712 // avoiding dangers and obstacles
713 vector dst_ahead, dst_down;
714 makevectors(this.v_angle.y * '0 1 0');
715 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
716 dst_down = dst_ahead - '0 0 1500';
719 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
721 // Check head-banging against walls
722 if(vdist(this.origin + this.view_ofs - trace_endpos, <, 25) && !(this.aistatus & AI_STATUS_OUT_WATER))
724 PHYS_INPUT_BUTTON_JUMP(this) = true;
725 if(this.facingwalltime && time > this.facingwalltime)
727 this.ignoregoal = this.goalcurrent;
728 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
729 this.bot_strategytime = 0;
734 this.facingwalltime = time + 0.05;
739 this.facingwalltime = 0;
741 if(this.ignoregoal != NULL && time > this.ignoregoaltime)
743 this.ignoregoal = NULL;
744 this.ignoregoaltime = 0;
748 // Check for water/slime/lava and dangerous edges
749 // (only when the bot is on the ground or jumping intentionally)
750 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
752 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
753 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
756 traceline(dst_ahead , dst_down, true, NULL);
757 // te_lightning2(NULL, this.origin, dst_ahead); // Draw "ahead" look
758 // te_lightning2(NULL, dst_ahead, dst_down); // Draw "downwards" look
759 if(trace_endpos.z < this.origin.z + this.mins.z)
761 s = pointcontents(trace_endpos + '0 0 1');
762 if (s != CONTENT_SOLID)
763 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
764 evadelava = normalize(this.velocity) * -1;
765 else if (s == CONTENT_SKY)
766 evadeobstacle = normalize(this.velocity) * -1;
767 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
768 this.goalcurrent.absmin, this.goalcurrent.absmax))
770 // if ain't a safe goal with "holes" (like the jumpad on soylent)
771 // and there is a trigger_hurt below
772 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
774 // Remove dangerous dynamic goals from stack
775 LOG_TRACE("bot ", this.netname, " avoided the goal ", this.goalcurrent.classname, " ", etos(this.goalcurrent), " because it led to a dangerous path; goal stack cleared");
776 navigation_clearroute(this);
786 makevectors(this.v_angle.y * '0 1 0');
788 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
789 this.aistatus |= AI_STATUS_DANGER_AHEAD;
792 dodge = havocbot_dodge(this);
793 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
794 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
795 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, NULL);
796 if(IS_PLAYER(trace_ent))
797 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
799 dir = normalize(dir + dodge + evadeobstacle + evadelava);
800 // this.bot_dodgevector = dir;
801 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
804 if(time < this.ladder_time)
806 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
808 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
813 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
818 //dir = this.bot_dodgevector;
819 //if (this.bot_dodgevector_jumpbutton)
820 // PHYS_INPUT_BUTTON_JUMP(this) = true;
821 this.movement_x = dir * v_forward * maxspeed;
822 this.movement_y = dir * v_right * maxspeed;
823 this.movement_z = dir * v_up * maxspeed;
825 // Emulate keyboard interface
827 havocbot_keyboard_movement(this, destorg);
830 // if(this.aistatus & AI_STATUS_ROAMING)
832 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
833 havocbot_bunnyhop(this, dir);
835 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
836 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;
837 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);
840 entity havocbot_gettarget(entity this, bool secondary)
843 vector eye = CENTER_OR_VIEWOFS(this);
844 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
846 vector v = CENTER_OR_VIEWOFS(it);
847 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
848 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
849 if(bot_shouldattack(this, it))
851 traceline(eye, v, true, this);
852 if (trace_ent == it || trace_fraction >= 1)
860 void havocbot_chooseenemy(entity this)
862 entity head, best, head2;
863 float rating, bestrating, hf;
865 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
872 if (!bot_shouldattack(this, this.enemy))
874 // enemy died or something, find a new target
876 this.havocbot_chooseenemy_finished = time;
878 else if (this.havocbot_stickenemy)
880 // tracking last chosen enemy
881 // if enemy is visible
882 // and not really really far away
883 // and we're not severely injured
884 // then keep tracking for a half second into the future
885 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,NULL);
886 if (trace_ent == this.enemy || trace_fraction == 1)
887 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
888 if (this.health > 30)
890 // remain tracking him for a shot while (case he went after a small corner or pilar
891 this.havocbot_chooseenemy_finished = time + 0.5;
894 // enemy isn't visible, or is far away, or we're injured severely
895 // so stop preferring this enemy
896 // (it will still take a half second until a new one is chosen)
897 this.havocbot_stickenemy = 0;
900 if (time < this.havocbot_chooseenemy_finished)
902 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
903 eye = this.origin + this.view_ofs;
905 bestrating = 100000000;
906 head = head2 = findchainfloat(bot_attack, true);
909 hf = this.dphitcontentsmask;
911 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
913 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
915 bool scan_transparent = false;
916 bool scan_secondary_targets = false;
917 bool have_secondary_targets = false;
920 scan_secondary_targets = false;
922 for( ; head; head = head.chain)
924 if(!scan_secondary_targets)
926 if(head.classname == "misc_breakablemodel")
928 have_secondary_targets = true;
934 if(head.classname != "misc_breakablemodel")
938 v = (head.absmin + head.absmax) * 0.5;
939 rating = vlen(v - eye);
940 if (rating<autocvar_bot_ai_enemydetectionradius)
941 if (bestrating > rating)
942 if (bot_shouldattack(this, head))
944 traceline(eye, v, true, this);
945 if (trace_ent == head || trace_fraction >= 1)
953 if(!best && have_secondary_targets && !scan_secondary_targets)
955 scan_secondary_targets = true;
958 bestrating = 100000000;
962 // I want to do a second scan if no enemy was found or I don't have weapons
963 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
964 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
969 // Set flags to see through transparent objects
970 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
973 scan_transparent = true;
977 this.dphitcontentsmask = hf;
980 this.havocbot_stickenemy = true;
981 if(best && best.classname == "misc_breakablemodel")
982 this.havocbot_stickenemy = false;
985 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
987 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
988 // so skip this for them, or they'll never get to reload their weapons at all.
989 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
993 // if this weapon is scheduled for reloading, don't switch to it during combat
994 if (this.weapon_load[new_weapon] < 0)
996 bool other_weapon_available = false;
997 FOREACH(Weapons, it != WEP_Null, LAMBDA(
998 if(it.wr_checkammo1(it, this) + it.wr_checkammo2(it, this))
999 other_weapon_available = true;
1001 if(other_weapon_available)
1008 void havocbot_chooseweapon(entity this)
1013 if(g_weaponarena_weapons == WEPSET(TUBA))
1015 PS(this).m_switchweapon = WEP_TUBA;
1019 // TODO: clean this up by moving it to weapon code
1020 if(this.enemy==NULL)
1022 // If no weapon was chosen get the first available weapon
1023 if(PS(this).m_weapon==WEP_Null)
1024 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1025 if(client_hasweapon(this, it, true, false))
1027 PS(this).m_switchweapon = it;
1034 // Do not change weapon during the next second after a combo
1035 float f = time - this.lastcombotime;
1040 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1042 // Should it do a weapon combo?
1043 float af, ct, combo_time, combo;
1045 af = ATTACK_FINISHED(this, 0);
1046 ct = autocvar_bot_ai_weapon_combo_threshold;
1048 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1049 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1050 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1054 if(autocvar_bot_ai_weapon_combo)
1055 if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1059 this.lastcombotime = time;
1062 distance *= pow(2, this.bot_rangepreference);
1064 // Custom weapon list based on distance to the enemy
1065 if(bot_custom_weapon){
1067 // Choose weapons for far distance
1068 if ( distance > bot_distance_far ) {
1069 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1070 w = bot_weapons_far[i];
1071 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1073 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1075 PS(this).m_switchweapon = Weapons_from(w);
1081 // Choose weapons for mid distance
1082 if ( distance > bot_distance_close) {
1083 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1084 w = bot_weapons_mid[i];
1085 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1087 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1089 PS(this).m_switchweapon = Weapons_from(w);
1095 // Choose weapons for close distance
1096 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1097 w = bot_weapons_close[i];
1098 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1100 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1102 PS(this).m_switchweapon = Weapons_from(w);
1109 void havocbot_aim(entity this)
1111 vector myvel, enemyvel;
1112 // if(this.flags & FL_INWATER)
1114 if (time < this.nextaim)
1116 this.nextaim = time + 0.1;
1117 myvel = this.velocity;
1118 if (!this.waterlevel)
1122 enemyvel = this.enemy.velocity;
1123 if (!this.enemy.waterlevel)
1125 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1128 lag_additem(this, time + this.ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1131 bool havocbot_moveto_refresh_route(entity this)
1133 // Refresh path to goal if necessary
1135 wp = this.havocbot_personal_waypoint;
1136 navigation_goalrating_start(this);
1137 navigation_routerating(this, wp, 10000, 10000);
1138 navigation_goalrating_end(this);
1139 return this.navigation_hasgoals;
1142 float havocbot_moveto(entity this, vector pos)
1146 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1148 // Step 4: Move to waypoint
1149 if(this.havocbot_personal_waypoint==NULL)
1151 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1152 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1153 return CMD_STATUS_ERROR;
1156 if (!bot_strategytoken_taken)
1157 if(this.havocbot_personal_waypoint_searchtime<time)
1159 bot_strategytoken_taken = true;
1160 if(havocbot_moveto_refresh_route(this))
1162 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1163 this.havocbot_personal_waypoint_searchtime = time + 10;
1164 this.havocbot_personal_waypoint_failcounter = 0;
1168 this.havocbot_personal_waypoint_failcounter += 1;
1169 this.havocbot_personal_waypoint_searchtime = time + 2;
1170 if(this.havocbot_personal_waypoint_failcounter >= 30)
1172 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1173 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1174 delete(this.havocbot_personal_waypoint);
1175 return CMD_STATUS_ERROR;
1178 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1182 if(autocvar_bot_debug_goalstack)
1183 debuggoalstack(this);
1186 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1188 bot_aimdir(this, dir, -1);
1191 havocbot_movetogoal(this);
1193 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1195 // Step 5: Waypoint reached
1196 LOG_TRACE(this.netname, "'s personal waypoint reached");
1197 delete(this.havocbot_personal_waypoint);
1198 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1199 return CMD_STATUS_FINISHED;
1202 return CMD_STATUS_EXECUTING;
1205 // Step 2: Linking waypoint
1206 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1208 // Wait until it is linked
1209 if(!this.havocbot_personal_waypoint.wplinked)
1211 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1212 return CMD_STATUS_EXECUTING;
1215 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1216 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1217 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1219 // Step 3: Route to waypoint
1220 LOG_TRACE(this.netname, " walking to its personal waypoint");
1222 return CMD_STATUS_EXECUTING;
1225 // Step 1: Spawning waypoint
1226 wp = waypoint_spawnpersonal(this, pos);
1229 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1230 return CMD_STATUS_ERROR;
1233 this.havocbot_personal_waypoint = wp;
1234 this.havocbot_personal_waypoint_failcounter = 0;
1235 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1237 // if pos is inside a teleport, then let's mark it as teleport waypoint
1238 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1240 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1241 this.lastteleporttime = 0;
1245 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1246 print("routing to a teleporter\n");
1248 print("routing to a non-teleporter\n");
1251 return CMD_STATUS_EXECUTING;
1254 float havocbot_resetgoal(entity this)
1256 navigation_clearroute(this);
1257 return CMD_STATUS_FINISHED;
1260 void havocbot_setupbot(entity this)
1262 this.bot_ai = havocbot_ai;
1263 this.cmd_moveto = havocbot_moveto;
1264 this.cmd_resetgoal = havocbot_resetgoal;
1266 havocbot_chooserole(this);
1269 vector havocbot_dodge(entity this)
1271 // LordHavoc: disabled because this is too expensive
1276 float danger, bestdanger, vl, d;
1279 // check for dangerous objects near bot or approaching bot
1280 head = findchainfloat(bot_dodge, true);
1283 if (head.owner != this)
1285 vl = vlen(head.velocity);
1286 if (vl > autocvar_sv_maxspeed * 0.3)
1288 n = normalize(head.velocity);
1289 v = this.origin - head.origin;
1291 if (d > (0 - head.bot_dodgerating))
1292 if (d < (vl * 0.2 + head.bot_dodgerating))
1294 // calculate direction and distance from the flight path, by removing the forward axis
1295 v = v - (n * (v * n));
1296 danger = head.bot_dodgerating - vlen(v);
1297 if (bestdanger < danger)
1299 bestdanger = danger;
1300 // dodge to the side of the object
1301 dodge = normalize(v);
1307 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1308 if (bestdanger < danger)
1310 bestdanger = danger;
1311 dodge = normalize(this.origin - head.origin);