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
44 if(this.goalcurrent==world)
45 if(this.waterlevel==WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
47 // Look for the closest waypoint out of water
49 float bestdistance, distance;
53 for (head = findchain(classname, "waypoint"); head; head = head.chain)
55 distance = vlen(head.origin - this.origin);
59 if(head.origin.z < this.origin.z)
62 if(head.origin.z - this.origin.z - this.view_ofs.z > 100)
65 if (pointcontents(head.origin + head.maxs + '0 0 1') != CONTENT_EMPTY)
68 traceline(this.origin + this.view_ofs , head.origin, true, head);
73 if(distance<bestdistance)
76 bestdistance = distance;
82 // te_wizspike(newgoal.origin);
83 navigation_pushroute(this, newgoal);
87 // token has been used this frame
88 bot_strategytoken_taken = true;
94 havocbot_chooseenemy(this);
95 if (this.bot_chooseweapontime < time )
97 this.bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
98 havocbot_chooseweapon(this);
102 if (this.bot_aimtarg)
104 this.aistatus |= AI_STATUS_ATTACKING;
105 this.aistatus &= ~AI_STATUS_ROAMING;
109 Weapon w = PS(this).m_weapon;
111 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
113 PHYS_INPUT_BUTTON_ATCK(this) = false;
114 PHYS_INPUT_BUTTON_ATCK2(this) = false;
118 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this))
119 this.lastfiredweapon = PS(this).m_weapon.m_id;
124 if(IS_PLAYER(this.bot_aimtarg))
125 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs , -1);
128 else if (this.goalcurrent)
130 this.aistatus |= AI_STATUS_ROAMING;
131 this.aistatus &= ~AI_STATUS_ATTACKING;
133 vector now,v,next;//,heading;
134 float aimdistance,skillblend,distanceblend,blend;
135 next = now = ( (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5) - (this.origin + this.view_ofs);
136 aimdistance = vlen(now);
137 //heading = this.velocity;
138 //dprint(this.goalstack01.classname,etos(this.goalstack01),"\n");
140 this.goalstack01 != this && this.goalstack01 != world && ((this.aistatus & AI_STATUS_RUNNING) == 0) &&
141 !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
143 next = ((this.goalstack01.absmin + this.goalstack01.absmax) * 0.5) - (this.origin + this.view_ofs);
145 skillblend=bound(0,(skill+this.bot_moveskill-2.5)*0.5,1); //lower skill player can't preturn
146 distanceblend=bound(0,aimdistance/autocvar_bot_ai_keyboard_distance,1);
147 blend = skillblend * (1-distanceblend);
148 //v = (now * (distanceblend) + next * (1-distanceblend)) * (skillblend) + now * (1-skillblend);
149 //v = now * (distanceblend) * (skillblend) + next * (1-distanceblend) * (skillblend) + now * (1-skillblend);
150 //v = now * ((1-skillblend) + (distanceblend) * (skillblend)) + next * (1-distanceblend) * (skillblend);
151 v = now + blend * (next - now);
152 //dprint(etos(this), " ");
153 //dprint(vtos(now), ":", vtos(next), "=", vtos(v), " (blend ", ftos(blend), ")\n");
154 //v = now * (distanceblend) + next * (1-distanceblend);
155 if (this.waterlevel < WATERLEVEL_SWIMMING)
157 //dprint("walk at:", vtos(v), "\n");
158 //te_lightning2(world, this.origin, this.goalcurrent.origin);
159 bot_aimdir(this, v, -1);
161 havocbot_movetogoal(this);
163 // if the bot is not attacking, consider reloading weapons
164 if (!(this.aistatus & AI_STATUS_ATTACKING))
166 // we are currently holding a weapon that's not fully loaded, reload it
167 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
168 if(this.clip_load < this.clip_size)
169 this.impulse = 20; // "press" the reload button, not sure if this is done right
171 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
172 // the code above executes next frame, starting the reloading then
173 if(skill >= 5) // bots can only look for unloaded weapons past this skill
174 if(this.clip_load >= 0) // only if we're not reloading a weapon already
176 FOREACH(Weapons, it != WEP_Null, LAMBDA(
177 if((this.weapons & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.weapon_load[it.m_id] < it.reloading_ammo))
178 PS(this).m_switchweapon = it;
184 void havocbot_keyboard_movement(entity this, vector destorg)
187 float blend, maxspeed;
190 sk = skill + this.bot_moveskill;
192 maxspeed = autocvar_sv_maxspeed;
194 if (time < this.havocbot_keyboardtime)
197 this.havocbot_keyboardtime =
199 this.havocbot_keyboardtime
200 + 0.05/max(1, sk+this.havocbot_keyboardskill)
201 + random()*0.025/max(0.00025, skill+this.havocbot_keyboardskill)
203 keyboard = this.movement * (1.0 / maxspeed);
205 float trigger, trigger1;
206 blend = bound(0,sk*0.1,1);
207 trigger = autocvar_bot_ai_keyboard_threshold;
208 trigger1 = 0 - trigger;
210 // categorize forward movement
211 // at skill < 1.5 only forward
212 // at skill < 2.5 only individual directions
213 // at skill < 4.5 only individual directions, and forward diagonals
214 // at skill >= 4.5, all cases allowed
215 if (keyboard.x > trigger)
221 else if (keyboard.x < trigger1 && sk > 1.5)
236 if (keyboard.y > trigger)
238 else if (keyboard.y < trigger1)
243 if (keyboard.z > trigger)
245 else if (keyboard.z < trigger1)
250 this.havocbot_keyboard = keyboard * maxspeed;
251 if (this.havocbot_ducktime>time) PHYS_INPUT_BUTTON_CROUCH(this) = true;
253 keyboard = this.havocbot_keyboard;
254 blend = bound(0,vlen(destorg-this.origin)/autocvar_bot_ai_keyboard_distance,1); // When getting close move with 360 degree
255 //dprint("movement ", vtos(this.movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
256 this.movement = this.movement + (keyboard - this.movement) * blend;
259 void havocbot_bunnyhop(entity this, vector dir)
261 float bunnyhopdistance;
266 // Don't jump when attacking
267 if(this.aistatus & AI_STATUS_ATTACKING)
270 if(IS_PLAYER(this.goalcurrent))
273 maxspeed = autocvar_sv_maxspeed;
275 if(this.aistatus & AI_STATUS_DANGER_AHEAD)
277 this.aistatus &= ~AI_STATUS_RUNNING;
278 PHYS_INPUT_BUTTON_JUMP(this) = false;
279 this.bot_canruntogoal = 0;
280 this.bot_timelastseengoal = 0;
284 if(this.waterlevel > WATERLEVEL_WETFEET)
286 this.aistatus &= ~AI_STATUS_RUNNING;
290 if(this.bot_lastseengoal != this.goalcurrent && !(this.aistatus & AI_STATUS_RUNNING))
292 this.bot_canruntogoal = 0;
293 this.bot_timelastseengoal = 0;
296 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
297 bunnyhopdistance = vlen(this.origin - gco);
299 // Run only to visible goals
300 if(IS_ONGROUND(this))
301 if(this.speed==maxspeed)
302 if(checkpvs(this.origin + this.view_ofs, this.goalcurrent))
304 this.bot_lastseengoal = this.goalcurrent;
307 if(this.bot_timelastseengoal)
309 // for a period of time
310 if(time - this.bot_timelastseengoal > autocvar_bot_ai_bunnyhop_firstjumpdelay)
313 checkdistance = true;
315 // don't run if it is too close
316 if(this.bot_canruntogoal==0)
318 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_startdistance)
319 this.bot_canruntogoal = 1;
321 this.bot_canruntogoal = -1;
324 if(this.bot_canruntogoal != 1)
327 if(this.aistatus & AI_STATUS_ROAMING)
328 if(this.goalcurrent.classname=="waypoint")
329 if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_PERSONAL))
330 if(fabs(gco.z - this.origin.z) < this.maxs.z - this.mins.z)
331 if(this.goalstack01!=world)
333 gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
334 deviation = vectoangles(gno - this.origin) - vectoangles(gco - this.origin);
335 while (deviation.y < -180) deviation.y = deviation.y + 360;
336 while (deviation.y > 180) deviation.y = deviation.y - 360;
338 if(fabs(deviation.y) < 20)
339 if(bunnyhopdistance < vlen(this.origin - gno))
340 if(fabs(gno.z - gco.z) < this.maxs.z - this.mins.z)
342 if(vdist(gco - gno, >, autocvar_bot_ai_bunnyhop_startdistance))
343 if(checkpvs(this.origin + this.view_ofs, this.goalstack01))
345 checkdistance = false;
352 this.aistatus &= ~AI_STATUS_RUNNING;
353 if(bunnyhopdistance > autocvar_bot_ai_bunnyhop_stopdistance)
354 PHYS_INPUT_BUTTON_JUMP(this) = true;
358 this.aistatus |= AI_STATUS_RUNNING;
359 PHYS_INPUT_BUTTON_JUMP(this) = true;
365 this.bot_timelastseengoal = time;
370 this.bot_timelastseengoal = 0;
374 // Release jump button
375 if(!cvar("sv_pogostick"))
376 if((IS_ONGROUND(this)) == 0)
378 if(this.velocity.z < 0 || vlen(this.velocity)<maxspeed)
379 PHYS_INPUT_BUTTON_JUMP(this) = false;
382 if(this.aistatus & AI_STATUS_RUNNING)
383 if(vlen(this.velocity)>maxspeed)
385 deviation = vectoangles(dir) - vectoangles(this.velocity);
386 while (deviation.y < -180) deviation.y = deviation.y + 360;
387 while (deviation.y > 180) deviation.y = deviation.y - 360;
389 if(fabs(deviation.y)>10)
393 this.movement_y = maxspeed * -1;
394 else if(deviation.y<10)
395 this.movement_y = maxspeed;
402 void havocbot_movetogoal(entity this)
410 vector evadeobstacle;
417 //if (this.goalentity)
418 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
419 this.movement = '0 0 0';
420 maxspeed = autocvar_sv_maxspeed;
422 // Jetpack navigation
424 if(this.navigation_jetpack_goal)
425 if(this.goalcurrent==this.navigation_jetpack_goal)
428 if(autocvar_bot_debug_goalstack)
430 debuggoalstack(this);
431 te_wizspike(this.navigation_jetpack_point);
435 if (!(this.aistatus & AI_STATUS_JETPACK_FLYING))
437 // Brake almost completely so it can get a good direction
438 if(vdist(this.velocity, >, 10))
440 this.aistatus |= AI_STATUS_JETPACK_FLYING;
443 makevectors(this.v_angle.y * '0 1 0');
444 dir = normalize(this.navigation_jetpack_point - this.origin);
447 if(this.aistatus & AI_STATUS_JETPACK_LANDING)
449 // Calculate brake distance in xy
453 dxy = this.origin - ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ); dxy.z = 0;
455 v = vlen(this.velocity - this.velocity.z * '0 0 1');
456 db = (pow(v,2) / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
457 // dprint("distance ", ftos(ceil(d)), " velocity ", ftos(ceil(v)), " brake at ", ftos(ceil(db)), "\n");
458 if(d < db || d < 500)
461 if(fabs(this.velocity.x)>maxspeed*0.3)
463 this.movement_x = dir * v_forward * -maxspeed;
466 // Switch to normal mode
467 this.navigation_jetpack_goal = world;
468 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
469 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
473 else if(checkpvs(this.origin,this.goalcurrent))
475 // If I can see the goal switch to landing code
476 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
477 this.aistatus |= AI_STATUS_JETPACK_LANDING;
482 PHYS_INPUT_BUTTON_HOOK(this) = true;
483 if(this.navigation_jetpack_point.z - STAT(PL_MAX, NULL).z + STAT(PL_MIN, NULL).z < this.origin.z)
485 this.movement_x = dir * v_forward * maxspeed;
486 this.movement_y = dir * v_right * maxspeed;
491 // Handling of jump pads
492 if(this.jumppadcount)
494 // If got stuck on the jump pad try to reach the farthest visible waypoint
495 if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
497 if(fabs(this.velocity.z)<50)
499 entity head, newgoal = world;
500 float distance, bestdistance = 0;
502 for (head = findchain(classname, "waypoint"); head; head = head.chain)
505 distance = vlen(head.origin - this.origin);
509 traceline(this.origin + this.view_ofs , ( ( head.absmin + head.absmax ) * 0.5 ), true, world);
514 if(distance>bestdistance)
517 bestdistance = distance;
523 this.ignoregoal = this.goalcurrent;
524 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
525 navigation_clearroute(this);
526 navigation_routetogoal(this, newgoal, this.origin);
527 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
535 if(this.velocity.z>0)
538 vector velxy = this.velocity; velxy_z = 0;
539 threshold = maxspeed * 0.2;
540 if(vdist(velxy, <, threshold))
542 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now\n");
543 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
548 // Don't chase players while using a jump pad
549 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
553 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
554 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
556 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
558 if (!(IS_ONGROUND(this)))
560 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
561 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos ))
562 if(this.items & IT_JETPACK)
564 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
565 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
567 if(this.velocity.z<0)
569 PHYS_INPUT_BUTTON_HOOK(this) = true;
573 PHYS_INPUT_BUTTON_HOOK(this) = true;
575 // If there is no goal try to move forward
577 if(this.goalcurrent==world)
580 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
582 vector xyvelocity = this.velocity; xyvelocity_z = 0;
583 float xyspeed = xyvelocity * dir;
585 if(xyspeed < (maxspeed / 2))
587 makevectors(this.v_angle.y * '0 1 0');
588 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
589 if(trace_fraction==1)
591 this.movement_x = dir * v_forward * maxspeed;
592 this.movement_y = dir * v_right * maxspeed;
594 havocbot_keyboard_movement(this, this.origin + dir * 100);
598 this.havocbot_blockhead = true;
602 else if(this.health>WEP_CVAR(devastator, damage)*0.5)
604 if(this.velocity.z < 0)
605 if(client_hasweapon(this, WEP_DEVASTATOR, true, false))
607 this.movement_x = maxspeed;
609 if(this.rocketjumptime)
611 if(time > this.rocketjumptime)
613 PHYS_INPUT_BUTTON_ATCK2(this) = true;
614 this.rocketjumptime = 0;
619 PS(this).m_switchweapon = WEP_DEVASTATOR;
621 PHYS_INPUT_BUTTON_ATCK(this) = true;
622 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
628 // If there is no goal try to move forward
629 if(this.goalcurrent==world)
630 this.movement_x = maxspeed;
634 // If we are under water with no goals, swim up
636 if(this.goalcurrent==world)
639 if(this.waterlevel>WATERLEVEL_SWIMMING)
641 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
642 PHYS_INPUT_BUTTON_JUMP(this) = true;
644 PHYS_INPUT_BUTTON_JUMP(this) = false;
645 makevectors(this.v_angle.y * '0 1 0');
646 this.movement_x = dir * v_forward * maxspeed;
647 this.movement_y = dir * v_right * maxspeed;
648 this.movement_z = dir * v_up * maxspeed;
651 // if there is nowhere to go, exit
652 if (this.goalcurrent == world)
655 if (this.goalcurrent)
656 navigation_poptouchedgoals(this);
658 // if ran out of goals try to use an alternative goal or get a new strategy asap
659 if(this.goalcurrent == world)
661 this.bot_strategytime = 0;
666 if(autocvar_bot_debug_goalstack)
667 debuggoalstack(this);
669 m1 = this.goalcurrent.origin + this.goalcurrent.mins;
670 m2 = this.goalcurrent.origin + this.goalcurrent.maxs;
671 destorg = this.origin;
672 destorg.x = bound(m1_x, destorg.x, m2_x);
673 destorg.y = bound(m1_y, destorg.y, m2_y);
674 destorg.z = bound(m1_z, destorg.z, m2_z);
675 diff = destorg - this.origin;
677 dir = normalize(diff);
678 flatdir = diff;flatdir.z = 0;
679 flatdir = normalize(flatdir);
680 gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
682 //if (this.bot_dodgevector_time < time)
684 // this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
685 // this.bot_dodgevector_jumpbutton = 1;
686 evadeobstacle = '0 0 0';
691 if(this.waterlevel>WATERLEVEL_SWIMMING)
694 this.aistatus |= AI_STATUS_OUT_WATER;
698 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && gco.z < this.origin.z) &&
699 ( !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER) || this.aistatus & AI_STATUS_OUT_WATER))
700 PHYS_INPUT_BUTTON_JUMP(this) = true;
702 PHYS_INPUT_BUTTON_JUMP(this) = false;
704 dir = normalize(flatdir);
705 makevectors(this.v_angle.y * '0 1 0');
709 if(this.aistatus & AI_STATUS_OUT_WATER)
710 this.aistatus &= ~AI_STATUS_OUT_WATER;
712 // jump if going toward an obstacle that doesn't look like stairs we
713 // can walk up directly
714 tracebox(this.origin, this.mins, this.maxs, this.origin + this.velocity * 0.2, false, this);
715 if (trace_fraction < 1)
716 if (trace_plane_normal.z < 0.7)
719 tracebox(this.origin + stepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + stepheightvec, false, this);
720 if (trace_fraction < s + 0.01)
721 if (trace_plane_normal.z < 0.7)
724 tracebox(this.origin + jumpstepheightvec, this.mins, this.maxs, this.origin + this.velocity * 0.2 + jumpstepheightvec, false, this);
725 if (trace_fraction > s)
726 PHYS_INPUT_BUTTON_JUMP(this) = true;
730 // avoiding dangers and obstacles
731 vector dst_ahead, dst_down;
732 makevectors(this.v_angle.y * '0 1 0');
733 dst_ahead = this.origin + this.view_ofs + (this.velocity * 0.4) + (v_forward * 32 * 3);
734 dst_down = dst_ahead - '0 0 1500';
737 traceline(this.origin + this.view_ofs, dst_ahead, true, world);
739 // Check head-banging against walls
740 if(vlen(this.origin + this.view_ofs - trace_endpos) < 25 && !(this.aistatus & AI_STATUS_OUT_WATER))
742 PHYS_INPUT_BUTTON_JUMP(this) = true;
743 if(this.facingwalltime && time > this.facingwalltime)
745 this.ignoregoal = this.goalcurrent;
746 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
747 this.bot_strategytime = 0;
752 this.facingwalltime = time + 0.05;
757 this.facingwalltime = 0;
759 if(this.ignoregoal != world && time > this.ignoregoaltime)
761 this.ignoregoal = world;
762 this.ignoregoaltime = 0;
766 // Check for water/slime/lava and dangerous edges
767 // (only when the bot is on the ground or jumping intentionally)
768 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
770 if(trace_fraction == 1 && this.jumppadcount == 0 && !this.goalcurrent.wphardwired )
771 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || PHYS_INPUT_BUTTON_JUMP(this))
774 traceline(dst_ahead , dst_down, true, world);
775 // te_lightning2(world, this.origin, dst_ahead); // Draw "ahead" look
776 // te_lightning2(world, dst_ahead, dst_down); // Draw "downwards" look
777 if(trace_endpos.z < this.origin.z + this.mins.z)
779 s = pointcontents(trace_endpos + '0 0 1');
780 if (s != CONTENT_SOLID)
781 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
782 evadelava = normalize(this.velocity) * -1;
783 else if (s == CONTENT_SKY)
784 evadeobstacle = normalize(this.velocity) * -1;
785 else if (!boxesoverlap(dst_ahead - this.view_ofs + this.mins, dst_ahead - this.view_ofs + this.maxs,
786 this.goalcurrent.absmin, this.goalcurrent.absmax))
788 // if ain't a safe goal with "holes" (like the jumpad on soylent)
789 // and there is a trigger_hurt below
790 if(tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
792 // Remove dangerous dynamic goals from stack
793 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");
794 navigation_clearroute(this);
804 makevectors(this.v_angle.y * '0 1 0');
806 if(evadeobstacle!='0 0 0'||evadelava!='0 0 0')
807 this.aistatus |= AI_STATUS_DANGER_AHEAD;
810 dodge = havocbot_dodge();
811 dodge = dodge * bound(0,0.5+(skill+this.bot_dodgeskill)*0.1,1);
812 evadelava = evadelava * bound(1,3-(skill+this.bot_dodgeskill),3); //Noobs fear lava a lot and take more distance from it
813 traceline(this.origin, ( ( this.enemy.absmin + this.enemy.absmax ) * 0.5 ), true, world);
814 if(IS_PLAYER(trace_ent))
815 dir = dir * bound(0,(skill+this.bot_dodgeskill)/7,1);
817 dir = normalize(dir + dodge + evadeobstacle + evadelava);
818 // this.bot_dodgevector = dir;
819 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
822 if(time < this.ladder_time)
824 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
826 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
831 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
836 //dir = this.bot_dodgevector;
837 //if (this.bot_dodgevector_jumpbutton)
838 // PHYS_INPUT_BUTTON_JUMP(this) = true;
839 this.movement_x = dir * v_forward * maxspeed;
840 this.movement_y = dir * v_right * maxspeed;
841 this.movement_z = dir * v_up * maxspeed;
843 // Emulate keyboard interface
845 havocbot_keyboard_movement(this, destorg);
848 // if(this.aistatus & AI_STATUS_ROAMING)
850 if(skill+this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
851 havocbot_bunnyhop(this, dir);
853 if ((dir * v_up) >= autocvar_sv_jumpvelocity*0.5 && (IS_ONGROUND(this))) PHYS_INPUT_BUTTON_JUMP(this) = true;
854 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;
855 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);
858 void havocbot_chooseenemy(entity this)
860 entity head, best, head2;
861 float rating, bestrating, hf;
863 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
870 if (!bot_shouldattack(this, this.enemy))
872 // enemy died or something, find a new target
874 this.havocbot_chooseenemy_finished = time;
876 else if (this.havocbot_stickenemy)
878 // tracking last chosen enemy
879 // if enemy is visible
880 // and not really really far away
881 // and we're not severely injured
882 // then keep tracking for a half second into the future
883 traceline(this.origin+this.view_ofs, ( this.enemy.absmin + this.enemy.absmax ) * 0.5,false,world);
884 if (trace_ent == this.enemy || trace_fraction == 1)
885 if (vdist(((this.enemy.absmin + this.enemy.absmax) * 0.5) - this.origin, <, 1000))
886 if (this.health > 30)
888 // remain tracking him for a shot while (case he went after a small corner or pilar
889 this.havocbot_chooseenemy_finished = time + 0.5;
892 // enemy isn't visible, or is far away, or we're injured severely
893 // so stop preferring this enemy
894 // (it will still take a half second until a new one is chosen)
895 this.havocbot_stickenemy = 0;
898 if (time < this.havocbot_chooseenemy_finished)
900 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
901 eye = this.origin + this.view_ofs;
903 bestrating = 100000000;
904 head = head2 = findchainfloat(bot_attack, true);
907 hf = this.dphitcontentsmask;
909 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
911 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
913 bool scan_transparent = false;
914 bool scan_secondary_targets = false;
915 bool have_secondary_targets = false;
918 scan_secondary_targets = false;
920 for( ; head; head = head.chain)
922 if(!scan_secondary_targets)
924 if(head.classname == "misc_breakablemodel")
926 have_secondary_targets = true;
932 if(head.classname != "misc_breakablemodel")
936 v = (head.absmin + head.absmax) * 0.5;
937 rating = vlen(v - eye);
938 if (rating<autocvar_bot_ai_enemydetectionradius)
939 if (bestrating > rating)
940 if (bot_shouldattack(this, head))
942 traceline(eye, v, true, this);
943 if (trace_ent == head || trace_fraction >= 1)
951 if(!best && have_secondary_targets && !scan_secondary_targets)
953 scan_secondary_targets = true;
956 bestrating = 100000000;
960 // I want to do a second scan if no enemy was found or I don't have weapons
961 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
962 if(best || this.weapons) // || this.weapon == WEP_RIFLE.m_id
967 // Set flags to see through transparent objects
968 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
971 scan_transparent = true;
975 this.dphitcontentsmask = hf;
978 this.havocbot_stickenemy = true;
979 if(best && best.classname == "misc_breakablemodel")
980 this.havocbot_stickenemy = false;
983 float havocbot_chooseweapon_checkreload(entity this, int new_weapon)
985 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
986 // so skip this for them, or they'll never get to reload their weapons at all.
987 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
991 // if this weapon is scheduled for reloading, don't switch to it during combat
992 if (this.weapon_load[new_weapon] < 0)
994 bool other_weapon_available = false;
995 FOREACH(Weapons, it != WEP_Null, LAMBDA(
996 if(it.wr_checkammo1(it) + it.wr_checkammo2(it))
997 other_weapon_available = true;
999 if(other_weapon_available)
1006 void havocbot_chooseweapon(entity this)
1011 if(g_weaponarena_weapons == WEPSET(TUBA))
1013 PS(this).m_switchweapon = WEP_TUBA;
1017 // TODO: clean this up by moving it to weapon code
1018 if(this.enemy==world)
1020 // If no weapon was chosen get the first available weapon
1021 if(PS(this).m_weapon==WEP_Null)
1022 FOREACH(Weapons, it != WEP_Null, LAMBDA(
1023 if(client_hasweapon(this, it, true, false))
1025 PS(this).m_switchweapon = it;
1032 // Do not change weapon during the next second after a combo
1033 float f = time - this.lastcombotime;
1038 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1040 // Should it do a weapon combo?
1041 float af, ct, combo_time, combo;
1043 af = ATTACK_FINISHED(this, 0);
1044 ct = autocvar_bot_ai_weapon_combo_threshold;
1046 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1047 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1048 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1052 if(autocvar_bot_ai_weapon_combo)
1053 if(PS(this).m_weapon.m_id == this.lastfiredweapon)
1057 this.lastcombotime = time;
1060 distance *= pow(2, this.bot_rangepreference);
1062 // Custom weapon list based on distance to the enemy
1063 if(bot_custom_weapon){
1065 // Choose weapons for far distance
1066 if ( distance > bot_distance_far ) {
1067 for(i=0; i < Weapons_COUNT && bot_weapons_far[i] != -1 ; ++i){
1068 w = bot_weapons_far[i];
1069 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1071 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1073 PS(this).m_switchweapon = Weapons_from(w);
1079 // Choose weapons for mid distance
1080 if ( distance > bot_distance_close) {
1081 for(i=0; i < Weapons_COUNT && bot_weapons_mid[i] != -1 ; ++i){
1082 w = bot_weapons_mid[i];
1083 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1085 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1087 PS(this).m_switchweapon = Weapons_from(w);
1093 // Choose weapons for close distance
1094 for(i=0; i < Weapons_COUNT && bot_weapons_close[i] != -1 ; ++i){
1095 w = bot_weapons_close[i];
1096 if ( client_hasweapon(this, Weapons_from(w), true, false) )
1098 if ((PS(this).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, w))
1100 PS(this).m_switchweapon = Weapons_from(w);
1107 void havocbot_aim(entity this)
1109 vector myvel, enemyvel;
1110 // if(this.flags & FL_INWATER)
1112 if (time < this.nextaim)
1114 this.nextaim = time + 0.1;
1115 myvel = this.velocity;
1116 if (!this.waterlevel)
1120 enemyvel = this.enemy.velocity;
1121 if (!this.enemy.waterlevel)
1123 lag_additem(this, time + this.ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1126 lag_additem(this, time + this.ping, 0, 0, world, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1129 bool havocbot_moveto_refresh_route(entity this)
1131 // Refresh path to goal if necessary
1133 wp = this.havocbot_personal_waypoint;
1134 navigation_goalrating_start(this);
1135 navigation_routerating(this, wp, 10000, 10000);
1136 navigation_goalrating_end(this);
1137 return this.navigation_hasgoals;
1140 float havocbot_moveto(entity this, vector pos)
1144 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1146 // Step 4: Move to waypoint
1147 if(this.havocbot_personal_waypoint==world)
1149 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint\n");
1150 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1151 return CMD_STATUS_ERROR;
1154 if (!bot_strategytoken_taken)
1155 if(this.havocbot_personal_waypoint_searchtime<time)
1157 bot_strategytoken_taken = true;
1158 if(havocbot_moveto_refresh_route(this))
1160 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)\n");
1161 this.havocbot_personal_waypoint_searchtime = time + 10;
1162 this.havocbot_personal_waypoint_failcounter = 0;
1166 this.havocbot_personal_waypoint_failcounter += 1;
1167 this.havocbot_personal_waypoint_searchtime = time + 2;
1168 if(this.havocbot_personal_waypoint_failcounter >= 30)
1170 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin),"\n");
1171 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1172 remove(this.havocbot_personal_waypoint);
1173 return CMD_STATUS_ERROR;
1176 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later\n");
1180 if(autocvar_bot_debug_goalstack)
1181 debuggoalstack(this);
1184 vector dir = ( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - (this.origin + this.view_ofs);
1186 bot_aimdir(this, dir, -1);
1189 havocbot_movetogoal(this);
1191 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1193 // Step 5: Waypoint reached
1194 LOG_TRACE(this.netname, "'s personal waypoint reached\n");
1195 remove(this.havocbot_personal_waypoint);
1196 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1197 return CMD_STATUS_FINISHED;
1200 return CMD_STATUS_EXECUTING;
1203 // Step 2: Linking waypoint
1204 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1206 // Wait until it is linked
1207 if(!this.havocbot_personal_waypoint.wplinked)
1209 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked\n");
1210 return CMD_STATUS_EXECUTING;
1213 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1214 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1215 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1217 // Step 3: Route to waypoint
1218 LOG_TRACE(this.netname, " walking to its personal waypoint\n");
1220 return CMD_STATUS_EXECUTING;
1223 // Step 1: Spawning waypoint
1224 wp = waypoint_spawnpersonal(this, pos);
1227 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos),"\n");
1228 return CMD_STATUS_ERROR;
1231 this.havocbot_personal_waypoint = wp;
1232 this.havocbot_personal_waypoint_failcounter = 0;
1233 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1235 // if pos is inside a teleport, then let's mark it as teleport waypoint
1237 for(head = world; (head = find(head, classname, "trigger_teleport")); )
1239 if(WarpZoneLib_BoxTouchesBrush(pos, pos, head, world))
1241 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1242 this.lastteleporttime = 0;
1247 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1248 print("routing to a teleporter\n");
1250 print("routing to a non-teleporter\n");
1253 return CMD_STATUS_EXECUTING;
1256 float havocbot_resetgoal(entity this)
1258 navigation_clearroute(this);
1259 return CMD_STATUS_FINISHED;
1262 void havocbot_setupbot(entity this)
1264 this.bot_ai = havocbot_ai;
1265 this.cmd_moveto = havocbot_moveto;
1266 this.cmd_resetgoal = havocbot_resetgoal;
1268 havocbot_chooserole(this);
1271 vector havocbot_dodge()
1273 // LordHavoc: disabled because this is too expensive
1279 float danger, bestdanger, vl, d;
1282 // check for dangerous objects near bot or approaching bot
1283 head = findchainfloat(bot_dodge, true);
1286 if (head.owner != this)
1288 vl = vlen(head.velocity);
1289 if (vl > autocvar_sv_maxspeed * 0.3)
1291 n = normalize(head.velocity);
1292 v = this.origin - head.origin;
1294 if (d > (0 - head.bot_dodgerating))
1295 if (d < (vl * 0.2 + head.bot_dodgerating))
1297 // calculate direction and distance from the flight path, by removing the forward axis
1298 v = v - (n * (v * n));
1299 danger = head.bot_dodgerating - vlen(v);
1300 if (bestdanger < danger)
1302 bestdanger = danger;
1303 // dodge to the side of the object
1304 dodge = normalize(v);
1310 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1311 if (bestdanger < danger)
1313 bestdanger = danger;
1314 dodge = normalize(this.origin - head.origin);