5 #include <server/bot/api.qh>
6 #include <server/client.qh>
7 #include <common/weapons/_all.qh>
8 #include <common/stats.qh>
9 #include <server/damage.qh>
10 #include <server/items/items.qh>
11 #include <server/mutators/_mod.qh>
12 #include <server/weapons/selection.qh>
13 #include <server/weapons/weaponsystem.qh>
14 #include <server/world.qh>
15 #include "../cvars.qh"
19 #include "../navigation.qh"
20 #include "../scripting.qh"
21 #include "../waypoints.qh"
23 #include <common/constants.qh>
24 #include <common/impulses/all.qh>
25 #include <common/net_linked.qh>
26 #include <common/physics/player.qh>
27 #include <common/state.qh>
28 #include <common/items/_mod.qh>
29 #include <common/wepent.qh>
31 #include <common/mapobjects/func/ladder.qh>
32 #include <common/mapobjects/teleporters.qh>
33 #include <common/mapobjects/trigger/hurt.qh>
34 #include <common/mapobjects/trigger/jumppads.qh>
36 #include <lib/warpzone/common.qh>
38 void havocbot_ai(entity this)
43 this.bot_aimdir_executed = false;
44 // lock aim if teleported or passing through a warpzone
45 if (this.lastteleporttime && !this.jumppadcount)
46 this.bot_aimdir_executed = true;
48 if(bot_execute_commands(this))
51 if (bot_strategytoken == this && !bot_strategytoken_taken)
53 if(this.havocbot_blockhead)
55 this.havocbot_blockhead = false;
59 if (!this.jumppadcount && !STAT(FROZEN, this)
60 && !(this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP) && !IS_ONGROUND(this)))
63 this.havocbot_role(this); // little too far down the rabbit hole
67 // if we don't have a goal and we're under water look for a waypoint near the "shore" and push it
68 if(!(IS_DEAD(this) || STAT(FROZEN, this)))
70 if(this.waterlevel == WATERLEVEL_SWIMMING || (this.aistatus & AI_STATUS_OUT_WATER))
72 // Look for the closest waypoint out of water
73 entity newgoal = NULL;
74 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 10000),
76 if(it.origin.z < this.origin.z)
79 if(it.origin.z - this.origin.z - this.view_ofs.z > 100)
82 if (pointcontents(it.origin + it.maxs + '0 0 1') != CONTENT_EMPTY)
85 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
87 if(trace_fraction < 1)
90 if(!newgoal || vlen2(it.origin - this.origin) < vlen2(newgoal.origin - this.origin))
96 // te_wizspike(newgoal.origin);
97 navigation_pushroute(this, newgoal);
101 // token has been used this frame
102 bot_strategytoken_taken = true;
105 if (this.goalcurrent && wasfreed(this.goalcurrent))
107 navigation_clearroute(this);
108 navigation_goalrating_timeout_force(this);
112 if(IS_DEAD(this) || STAT(FROZEN, this))
114 if (this.goalcurrent)
115 navigation_clearroute(this);
117 this.bot_aimtarg = NULL;
121 havocbot_chooseenemy(this);
123 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
125 .entity weaponentity = weaponentities[slot];
126 if(this.(weaponentity).m_weapon != WEP_Null || slot == 0)
127 if(this.(weaponentity).bot_chooseweapontime < time)
129 this.(weaponentity).bot_chooseweapontime = time + autocvar_bot_ai_chooseweaponinterval;
130 havocbot_chooseweapon(this, weaponentity);
136 if (this.bot_aimtarg)
138 this.aistatus |= AI_STATUS_ATTACKING;
139 this.aistatus &= ~AI_STATUS_ROAMING;
141 if(STAT(WEAPONS, this))
143 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
145 PHYS_INPUT_BUTTON_ATCK(this) = false;
146 PHYS_INPUT_BUTTON_ATCK2(this) = false;
150 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
152 .entity weaponentity = weaponentities[slot];
153 Weapon w = this.(weaponentity).m_weapon;
154 if(w == WEP_Null && slot != 0)
156 w.wr_aim(w, this, weaponentity);
157 if(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)) // TODO: what if we didn't fire this weapon, but the previous?
158 this.(weaponentity).lastfiredweapon = this.(weaponentity).m_weapon.m_id;
164 if(IS_PLAYER(this.bot_aimtarg))
165 bot_aimdir(this, this.bot_aimtarg.origin + this.bot_aimtarg.view_ofs - this.origin - this.view_ofs, 0);
168 else if (this.goalcurrent)
170 this.aistatus |= AI_STATUS_ROAMING;
171 this.aistatus &= ~AI_STATUS_ATTACKING;
174 havocbot_movetogoal(this);
175 if (!this.bot_aimdir_executed && this.goalcurrent)
178 vector dir = get_closer_dest(this.goalcurrent, this.origin);
179 dir -= this.origin + this.view_ofs;
181 bot_aimdir(this, dir, 0);
184 // if the bot is not attacking, consider reloading weapons
185 if (!(this.aistatus & AI_STATUS_ATTACKING))
187 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
189 .entity weaponentity = weaponentities[slot];
191 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
194 // we are currently holding a weapon that's not fully loaded, reload it
195 if(skill >= 2) // bots can only reload the held weapon on purpose past this skill
196 if(this.(weaponentity).clip_load < this.(weaponentity).clip_size)
197 CS(this).impulse = IMP_weapon_reload.impulse; // not sure if this is done right
199 // if we're not reloading a weapon, switch to any weapon in our invnetory that's not fully loaded to reload it next
200 // the code above executes next frame, starting the reloading then
201 if(skill >= 5) // bots can only look for unloaded weapons past this skill
202 if(this.(weaponentity).clip_load >= 0) // only if we're not reloading a weapon already
204 FOREACH(Weapons, it != WEP_Null, {
205 if((STAT(WEAPONS, this) & (it.m_wepset)) && (it.spawnflags & WEP_FLAG_RELOADABLE) && (this.(weaponentity).weapon_load[it.m_id] < it.reloading_ammo))
207 this.(weaponentity).m_switchweapon = it;
216 void havocbot_bunnyhop(entity this, vector dir)
218 bool can_run = false;
219 if (!(this.aistatus & AI_STATUS_ATTACKING) && this.goalcurrent && !IS_PLAYER(this.goalcurrent)
220 && vdist(vec2(this.velocity), >=, autocvar_sv_maxspeed) && !(this.aistatus & AI_STATUS_DANGER_AHEAD)
221 && this.waterlevel <= WATERLEVEL_WETFEET && !IS_DUCKED(this)
222 && IS_ONGROUND(this) && !(this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP)))
224 vector vel_angles = vectoangles(this.velocity);
225 vector deviation = vel_angles - vectoangles(dir);
226 while (deviation.y < -180) deviation.y = deviation.y + 360;
227 while (deviation.y > 180) deviation.y = deviation.y - 360;
228 if (fabs(deviation.y) < autocvar_bot_ai_bunnyhop_dir_deviation_max)
230 vector gco = get_closer_dest(this.goalcurrent, this.origin);
231 float vel = vlen(vec2(this.velocity));
233 // with the current physics, jump distance grows linearly with the speed
234 float jump_distance = 52.661 + 0.606 * vel;
235 jump_distance += this.origin.z - gco.z; // roughly take into account vertical distance too
236 if (vdist(vec2(gco - this.origin), >, max(0, jump_distance)))
238 else if (!(this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP)
239 && !(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
240 && this.goalstack01 && !wasfreed(this.goalstack01) && !(this.goalstack01.wpflags & WAYPOINTFLAG_JUMP)
241 && vdist(vec2(gco - this.goalstack01.origin), >, 70))
243 vector gno = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
244 vector ang = vectoangles(gco - this.origin);
245 deviation = vectoangles(gno - gco) - vel_angles;
246 while (deviation.y < -180) deviation.y = deviation.y + 360;
247 while (deviation.y > 180) deviation.y = deviation.y - 360;
249 float max_turn_angle = autocvar_bot_ai_bunnyhop_turn_angle_max;
250 max_turn_angle -= autocvar_bot_ai_bunnyhop_turn_angle_reduction * ((vel - autocvar_sv_maxspeed) / autocvar_sv_maxspeed);
251 if ((ang.x < 90 || ang.x > 360 - autocvar_bot_ai_bunnyhop_downward_pitch_max)
252 && fabs(deviation.y) < max(autocvar_bot_ai_bunnyhop_turn_angle_min, max_turn_angle))
262 PHYS_INPUT_BUTTON_JUMP(this) = true;
263 this.bot_jump_time = time;
264 this.aistatus |= AI_STATUS_RUNNING;
268 if (IS_ONGROUND(this) || this.waterlevel > WATERLEVEL_WETFEET)
269 this.aistatus &= ~AI_STATUS_RUNNING;
273 void havocbot_keyboard_movement(entity this, vector destorg)
275 if(time <= this.havocbot_keyboardtime)
278 float sk = skill + this.bot_moveskill;
279 this.havocbot_keyboardtime =
281 this.havocbot_keyboardtime
282 + 0.05 / max(1, sk + this.havocbot_keyboardskill)
283 + random() * 0.025 / max(0.00025, skill + this.havocbot_keyboardskill)
285 vector keyboard = CS(this).movement / autocvar_sv_maxspeed;
287 float trigger = autocvar_bot_ai_keyboard_threshold;
289 // categorize forward movement
290 // at skill < 1.5 only forward
291 // at skill < 2.5 only individual directions
292 // at skill < 4.5 only individual directions, and forward diagonals
293 // at skill >= 4.5, all cases allowed
294 if (keyboard.x > trigger)
300 else if (keyboard.x < -trigger && sk > 1.5)
315 if (keyboard.y > trigger)
317 else if (keyboard.y < -trigger)
322 if (keyboard.z > trigger)
324 else if (keyboard.z < -trigger)
329 // make sure bots don't get stuck if havocbot_keyboardtime is very high
330 if (keyboard == '0 0 0')
331 this.havocbot_keyboardtime = min(this.havocbot_keyboardtime, time + 0.2);
333 this.havocbot_keyboard = keyboard * autocvar_sv_maxspeed;
334 if (this.havocbot_ducktime > time)
335 PHYS_INPUT_BUTTON_CROUCH(this) = true;
337 keyboard = this.havocbot_keyboard;
338 float blend = bound(0, vlen(destorg - this.origin) / autocvar_bot_ai_keyboard_distance, 1); // When getting close move with 360 degree
339 //dprint("movement ", vtos(CS(this).movement), " keyboard ", vtos(keyboard), " blend ", ftos(blend), "\n");
340 CS(this).movement = CS(this).movement + (keyboard - CS(this).movement) * blend;
343 // return true when bot isn't getting closer to the current goal
344 bool havocbot_checkgoaldistance(entity this, vector gco)
346 if (this.bot_stop_moving_timeout > time)
348 float curr_dist_z = max(20, fabs(this.origin.z - gco.z));
349 float curr_dist_2d = max(20, vlen(vec2(this.origin - gco)));
350 float distance_time = this.goalcurrent_distance_time;
351 if(distance_time < 0)
352 distance_time = -distance_time;
353 if(curr_dist_z >= this.goalcurrent_distance_z && curr_dist_2d >= this.goalcurrent_distance_2d)
356 this.goalcurrent_distance_time = time;
357 else if (time - distance_time > 0.5)
362 // reduce it a little bit so it works even with very small approaches to the goal
363 this.goalcurrent_distance_z = max(20, curr_dist_z - 10);
364 this.goalcurrent_distance_2d = max(20, curr_dist_2d - 10);
365 this.goalcurrent_distance_time = 0;
370 entity havocbot_select_an_item_of_group(entity this, int gr)
372 entity selected = NULL;
373 float selected_dist2 = 0;
374 // select farthest item of this group from bot's position
375 IL_EACH(g_items, it.item_group == gr && it.solid,
377 float dist2 = vlen2(this.origin - it.origin);
378 if (dist2 < 600 ** 2 && dist2 > selected_dist2)
381 selected_dist2 = vlen2(this.origin - selected.origin);
388 set_tracewalk_dest(selected, this.origin, false);
389 if (!tracewalk(this, this.origin, STAT(PL_MIN, this), STAT(PL_MAX, this),
390 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
398 void havocbot_movetogoal(entity this)
403 float dodge_enemy_factor = 1;
407 //if (this.goalentity)
408 // te_lightning2(this, this.origin, (this.goalentity.absmin + this.goalentity.absmax) * 0.5);
409 CS(this).movement = '0 0 0';
410 maxspeed = autocvar_sv_maxspeed;
412 PHYS_INPUT_BUTTON_CROUCH(this) = boolean(this.goalcurrent.wpflags & WAYPOINTFLAG_CROUCH);
414 PHYS_INPUT_BUTTON_JETPACK(this) = false;
415 // Jetpack navigation
416 if(this.navigation_jetpack_goal)
417 if(this.goalcurrent==this.navigation_jetpack_goal)
418 if(GetResource(this, RES_FUEL))
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
442 float d = vlen(vec2(this.origin - (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5));
443 float vel2 = vlen2(vec2(this.velocity));
444 float db = (vel2 / (autocvar_g_jetpack_acceleration_side * 2)) + 100;
445 //LOG_INFOF("distance %d, velocity %d, brake at %d ", ceil(d), ceil(v), ceil(db));
446 if(d < db || d < 500)
449 if (vel2 > (maxspeed * 0.3) ** 2)
451 CS(this).movement_x = dir * v_forward * -maxspeed;
454 // Switch to normal mode
455 this.navigation_jetpack_goal = NULL;
456 this.aistatus &= ~AI_STATUS_JETPACK_LANDING;
457 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
461 else if(checkpvs(this.origin,this.goalcurrent))
463 // If I can see the goal switch to landing code
464 this.aistatus &= ~AI_STATUS_JETPACK_FLYING;
465 this.aistatus |= AI_STATUS_JETPACK_LANDING;
470 PHYS_INPUT_BUTTON_JETPACK(this) = true;
471 if(this.navigation_jetpack_point.z - STAT(PL_MAX, this).z + STAT(PL_MIN, this).z < this.origin.z)
473 CS(this).movement_x = dir * v_forward * maxspeed;
474 CS(this).movement_y = dir * v_right * maxspeed;
479 // Handling of jump pads
480 if(this.jumppadcount)
482 if(this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
484 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
485 if(navigation_poptouchedgoals(this))
488 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
490 // If got stuck on the jump pad try to reach the farthest visible waypoint
491 // but with some randomness so it can try out different paths
492 if(!this.goalcurrent)
494 entity newgoal = NULL;
495 IL_EACH(g_waypoints, vdist(it.origin - this.origin, <=, 1000),
497 if(it.wpflags & WAYPOINTFLAG_TELEPORT)
498 if(it.origin.z < this.origin.z - 100 && vdist(vec2(it.origin - this.origin), <, 100))
501 traceline(this.origin + this.view_ofs, ((it.absmin + it.absmax) * 0.5), true, this);
503 if(trace_fraction < 1)
506 if(!newgoal || ((random() < 0.8) && vlen2(it.origin - this.origin) > vlen2(newgoal.origin - this.origin)))
512 this.ignoregoal = this.goalcurrent;
513 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
514 navigation_clearroute(this);
515 navigation_routetogoal(this, newgoal, this.origin);
516 if(autocvar_bot_debug_goalstack)
517 debuggoalstack(this);
518 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
521 else //if (this.goalcurrent)
523 if (this.goalcurrent.bot_pickup)
525 entity jumppad_wp = this.goalcurrent_prev;
526 navigation_poptouchedgoals(this);
527 if(!this.goalcurrent && jumppad_wp.wp00)
529 // head to the jumppad destination once bot reaches the goal item
530 navigation_pushroute(this, jumppad_wp.wp00);
533 vector gco = (this.goalcurrent.absmin + this.goalcurrent.absmax) * 0.5;
534 if (this.origin.z > gco.z && vdist(vec2(this.velocity), <, autocvar_sv_maxspeed))
536 if (this.velocity.z < 0)
537 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
539 else if(havocbot_checkgoaldistance(this, gco))
541 navigation_clearroute(this);
542 navigation_goalrating_timeout_force(this);
548 else //if (!(this.aistatus & AI_STATUS_OUT_JUMPPAD))
550 if(this.velocity.z > 0 && this.origin.z - this.lastteleport_origin.z > (this.maxs.z - this.mins.z) * 0.5)
552 vector velxy = this.velocity; velxy_z = 0;
553 if(vdist(velxy, <, autocvar_sv_maxspeed * 0.2))
555 LOG_TRACE("Warning: ", this.netname, " got stuck on a jumppad (velocity in xy is ", vtos(velxy), "), trying to get out of it now");
556 this.aistatus |= AI_STATUS_OUT_JUMPPAD;
561 // Don't chase players while using a jump pad
562 if(IS_PLAYER(this.goalcurrent) || IS_PLAYER(this.goalstack01))
566 else if(this.aistatus & AI_STATUS_OUT_JUMPPAD)
567 this.aistatus &= ~AI_STATUS_OUT_JUMPPAD;
569 // If there is a trigger_hurt right below try to use the jetpack or make a rocketjump
570 if (skill > 6 && !(IS_ONGROUND(this)))
572 #define ROCKETJUMP_DAMAGE() WEP_CVAR(devastator, damage) * 0.8 \
573 * ((STAT(STRENGTH_FINISHED, this) > time) ? autocvar_g_balance_powerup_strength_selfdamage : 1) \
574 * ((STAT(INVINCIBLE_FINISHED, this) > time) ? autocvar_g_balance_powerup_invincible_takedamage : 1)
576 // save some CPU cycles by checking trigger_hurt after checking
577 // that something can be done to evade it (cheaper checks)
578 int action_for_trigger_hurt = 0;
579 if (this.items & IT_JETPACK)
580 action_for_trigger_hurt = 1;
581 else if (!this.jumppadcount && !waypoint_is_hardwiredlink(this.goalcurrent_prev, this.goalcurrent)
582 && !(this.goalcurrent_prev && this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP)
583 && GetResource(this, RES_HEALTH) + GetResource(this, RES_ARMOR) > ROCKETJUMP_DAMAGE())
585 action_for_trigger_hurt = 2;
587 else if (!this.goalcurrent)
588 action_for_trigger_hurt = 3;
590 if (action_for_trigger_hurt)
592 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 -65536', MOVE_NOMONSTERS, this);
593 if(!tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos))
594 action_for_trigger_hurt = 0;
597 if(action_for_trigger_hurt == 1) // jetpack
599 tracebox(this.origin, this.mins, this.maxs, this.origin + '0 0 65536', MOVE_NOMONSTERS, this);
600 if(tracebox_hits_trigger_hurt(this.origin, this.mins, this.maxs, trace_endpos + '0 0 1' ))
602 if(this.velocity.z<0)
603 PHYS_INPUT_BUTTON_JETPACK(this) = true;
606 PHYS_INPUT_BUTTON_JETPACK(this) = true;
608 // If there is no goal try to move forward
610 if(this.goalcurrent==NULL)
613 dir = normalize(( ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5 ) - this.origin);
615 vector xyvelocity = this.velocity; xyvelocity_z = 0;
616 float xyspeed = xyvelocity * dir;
618 if(xyspeed < (maxspeed / 2))
620 makevectors(this.v_angle.y * '0 1 0');
621 tracebox(this.origin, this.mins, this.maxs, this.origin + (dir * maxspeed * 3), MOVE_NOMONSTERS, this);
622 if(trace_fraction==1)
624 CS(this).movement_x = dir * v_forward * maxspeed;
625 CS(this).movement_y = dir * v_right * maxspeed;
627 havocbot_keyboard_movement(this, this.origin + dir * 100);
631 this.havocbot_blockhead = true;
635 else if(action_for_trigger_hurt == 2) // rocketjump
637 if(this.velocity.z < 0)
639 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
641 .entity weaponentity = weaponentities[slot];
643 if(this.(weaponentity).m_weapon == WEP_Null && slot != 0)
646 if(client_hasweapon(this, WEP_DEVASTATOR, weaponentity, true, false))
648 CS(this).movement_x = maxspeed;
650 if(this.rocketjumptime)
652 if(time > this.rocketjumptime)
654 PHYS_INPUT_BUTTON_ATCK2(this) = true;
655 this.rocketjumptime = 0;
660 this.(weaponentity).m_switchweapon = WEP_DEVASTATOR;
662 PHYS_INPUT_BUTTON_ATCK(this) = true;
663 this.rocketjumptime = time + WEP_CVAR(devastator, detonatedelay);
669 else if(action_for_trigger_hurt == 3) // no goal
671 // If there is no goal try to move forward
672 CS(this).movement_x = maxspeed;
676 // If we are under water with no goals, swim up
677 if(this.waterlevel && !this.goalcurrent)
680 if(this.waterlevel>WATERLEVEL_SWIMMING)
682 else if(this.velocity.z >= 0 && !(this.waterlevel == WATERLEVEL_WETFEET && this.watertype == CONTENT_WATER))
683 PHYS_INPUT_BUTTON_JUMP(this) = true;
684 makevectors(this.v_angle.y * '0 1 0');
685 vector v = dir * maxspeed;
686 CS(this).movement.x = v * v_forward;
687 CS(this).movement.y = v * v_right;
688 CS(this).movement.z = v * v_up;
691 // if there is nowhere to go, exit
692 if (this.goalcurrent == NULL)
696 bool locked_goal = false;
697 if((this.goalentity && wasfreed(this.goalentity))
698 || (this.goalcurrent == this.goalentity && this.goalentity.tag_entity))
700 navigation_clearroute(this);
701 navigation_goalrating_timeout_force(this);
704 else if(this.goalentity.tag_entity)
706 navigation_goalrating_timeout_expire(this, 2);
708 else if(this.goalentity.bot_pickup)
710 if(this.goalentity.bot_pickup_respawning)
712 if(this.goalentity.solid) // item respawned
713 this.goalentity.bot_pickup_respawning = false;
714 else if(time < this.goalentity.scheduledrespawntime - 10) // item already taken (by someone else)
716 if(checkpvs(this.origin, this.goalentity))
718 this.goalentity.bot_pickup_respawning = false;
719 navigation_goalrating_timeout_expire(this, random());
721 locked_goal = true; // wait for item to respawn
723 else if(this.goalentity == this.goalcurrent)
724 locked_goal = true; // wait for item to respawn
726 else if(!this.goalentity.solid && !boxesoverlap(this.goalentity.absmin, this.goalentity.absmax, this.absmin, this.absmax))
728 if(checkpvs(this.origin, this.goalentity))
730 navigation_goalrating_timeout_expire(this, random());
734 if (this.goalcurrent == this.goalentity && this.goalentity_lock_timeout > time)
737 if (navigation_shortenpath(this))
739 if (vdist(this.origin - this.goalcurrent_prev.origin, <, 50)
740 && navigation_goalrating_timeout_can_be_anticipated(this))
742 navigation_goalrating_timeout_force(this);
746 bool goalcurrent_can_be_removed = false;
747 if (IS_PLAYER(this.goalcurrent) || IS_MONSTER(this.goalcurrent))
749 bool freeze_state_changed = (boolean(STAT(FROZEN, this.goalentity)) != this.goalentity_shouldbefrozen);
750 if (IS_DEAD(this.goalcurrent) || (this.goalentity == this.goalcurrent && freeze_state_changed))
752 goalcurrent_can_be_removed = true;
753 // don't remove if not visible
754 if (checkpvs(this.origin + this.view_ofs, this.goalcurrent))
756 if (IS_DEAD(this.goalcurrent))
758 IL_EACH(g_items, it.enemy == this.goalcurrent && Item_IsLoot(it),
760 if (vdist(it.origin - this.goalcurrent.death_origin, <, 50))
762 navigation_clearroute(this);
763 navigation_pushroute(this, it);
764 // loot can't be immediately rated since it isn't on ground yet
765 // it will be rated after a second when on ground, meanwhile head to it
766 navigation_goalrating_timeout_expire(this, 1);
771 if (!Item_IsLoot(this.goalcurrent))
773 navigation_goalrating_timeout_force(this);
778 else if (!(STAT(FROZEN, this.goalentity)) && this.bot_tracewalk_time < time)
780 set_tracewalk_dest(this.goalcurrent, this.origin, true);
781 if (!(trace_ent == this || tracewalk(this, this.origin, this.mins, this.maxs,
782 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode)))
784 navigation_goalrating_timeout_force(this);
787 this.bot_tracewalk_time = max(time, this.bot_tracewalk_time) + 0.25;
793 // optimize path finding by anticipating goalrating when bot is near a waypoint;
794 // in this case path finding can start directly from a waypoint instead of
795 // looking for all the reachable waypoints up to a certain distance
796 if (navigation_poptouchedgoals(this))
798 if (this.goalcurrent)
800 if (goalcurrent_can_be_removed)
802 // remove even if not visible
803 navigation_goalrating_timeout_force(this);
806 else if (navigation_goalrating_timeout_can_be_anticipated(this))
807 navigation_goalrating_timeout_force(this);
811 entity old_goal = this.goalcurrent_prev;
812 if (old_goal.item_group && this.item_group != old_goal.item_group)
814 // Avoid multiple costly calls of path finding code that selects one of the closest
815 // item of the group by telling the bot to head directly to the farthest item.
816 // Next time we let the bot select a goal as usual which can be another item
817 // of this group (the closest one) and so on
818 this.item_group = old_goal.item_group;
819 entity new_goal = havocbot_select_an_item_of_group(this, old_goal.item_group);
821 navigation_pushroute(this, new_goal);
827 // if ran out of goals try to use an alternative goal or get a new strategy asap
828 if(this.goalcurrent == NULL)
830 navigation_goalrating_timeout_force(this);
835 if(autocvar_bot_debug_goalstack)
836 debuggoalstack(this);
838 bool bunnyhop_forbidden = false;
839 vector destorg = get_closer_dest(this.goalcurrent, this.origin);
840 if (this.jumppadcount && this.goalcurrent.wpflags & WAYPOINTFLAG_TELEPORT)
842 // if bot used the jumppad, push towards jumppad origin until jumppad waypoint gets removed
843 destorg = this.goalcurrent.origin;
845 else if (this.goalcurrent.wpisbox)
847 // if bot is inside the teleport waypoint, head to teleport origin until teleport gets used
848 // do it even if bot is on a ledge above a teleport/jumppad so it doesn't get stuck
849 if (boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin + eZ * this.mins.z, this.origin + eZ * this.maxs.z)
850 || (this.absmin.z > destorg.z && destorg.x == this.origin.x && destorg.y == this.origin.y))
852 bunnyhop_forbidden = true;
853 destorg = this.goalcurrent.origin;
854 if(destorg.z > this.origin.z)
855 PHYS_INPUT_BUTTON_JUMP(this) = true;
859 diff = destorg - this.origin;
861 if (time < this.bot_stop_moving_timeout
862 || (this.goalcurrent == this.goalentity && time < this.goalentity_lock_timeout && vdist(diff, <, 10)))
864 // stop if the locked goal has been reached
865 destorg = this.origin;
866 diff = dir = '0 0 0';
868 else if (IS_PLAYER(this.goalcurrent) || IS_MONSTER(this.goalcurrent))
870 if (vdist(diff, <, 80))
872 // stop if too close to target player (even if frozen)
873 destorg = this.origin;
874 diff = dir = '0 0 0';
878 // move destorg out of target players, otherwise bot will consider them
879 // an obstacle that needs to be jumped (especially if frozen)
880 dir = normalize(diff);
881 destorg -= dir * PL_MAX_CONST.x * M_SQRT2;
882 diff = destorg - this.origin;
886 dir = normalize(diff);
887 flatdir = (diff.z == 0) ? dir : normalize(vec2(diff));
889 bool danger_detected = false;
890 vector do_break = '0 0 0';
892 //if (this.bot_dodgevector_time < time)
894 //this.bot_dodgevector_time = time + cvar("bot_ai_dodgeupdateinterval");
895 //this.bot_dodgevector_jumpbutton = 1;
897 this.aistatus &= ~AI_STATUS_DANGER_AHEAD;
898 makevectors(this.v_angle.y * '0 1 0');
899 if (this.waterlevel > WATERLEVEL_WETFEET)
901 if (this.waterlevel > WATERLEVEL_SWIMMING)
903 if(!this.goalcurrent)
904 this.aistatus |= AI_STATUS_OUT_WATER;
905 else if(destorg.z > this.origin.z)
906 PHYS_INPUT_BUTTON_JUMP(this) = true;
910 if(this.velocity.z >= 0 && !(this.watertype == CONTENT_WATER && destorg.z < this.origin.z) &&
911 (this.aistatus & AI_STATUS_OUT_WATER))
913 PHYS_INPUT_BUTTON_JUMP(this) = true;
918 if (destorg.z > this.origin.z)
927 if(this.aistatus & AI_STATUS_OUT_WATER)
928 this.aistatus &= ~AI_STATUS_OUT_WATER;
930 // jump if going toward an obstacle that doesn't look like stairs we
931 // can walk up directly
932 vector deviation = '0 0 0';
933 float current_speed = vlen(vec2(this.velocity));
934 if (current_speed < maxspeed * 0.2)
935 current_speed = maxspeed * 0.2;
938 deviation = vectoangles(diff) - vectoangles(this.velocity);
939 while (deviation.y < -180) deviation.y += 360;
940 while (deviation.y > 180) deviation.y -= 360;
942 float turning = false;
943 vector flat_diff = vec2(diff);
944 offset = max(32, current_speed * cos(deviation.y * DEG2RAD) * 0.3) * flatdir;
945 vector actual_destorg = this.origin + offset;
946 if (this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP))
948 if (time > this.bot_stop_moving_timeout
949 && fabs(deviation.y) > 20 && current_speed > maxspeed * 0.4
950 && vdist(vec2(this.origin - this.goalcurrent_prev.origin), <, 50))
952 this.bot_stop_moving_timeout = time + 0.1;
954 if (current_speed > autocvar_sv_maxspeed * 0.9
955 && vlen2(flat_diff) < vlen2(vec2(this.goalcurrent_prev.origin - destorg))
956 && vdist(vec2(this.origin - this.goalcurrent_prev.origin), >, 50)
957 && vdist(vec2(this.origin - this.goalcurrent_prev.origin), <, 150)
960 PHYS_INPUT_BUTTON_JUMP(this) = true;
961 this.bot_jump_time = time;
964 else if (!this.goalstack01 || (this.goalcurrent.wpflags & (WAYPOINTFLAG_TELEPORT | WAYPOINTFLAG_LADDER)))
966 if (vlen2(flat_diff) < vlen2(offset))
968 if ((this.goalcurrent.wpflags & WAYPOINTFLAG_JUMP) && this.goalstack01)
970 // oblique warpzones need a jump otherwise bots gets stuck
971 PHYS_INPUT_BUTTON_JUMP(this) = true;
975 actual_destorg.x = destorg.x;
976 actual_destorg.y = destorg.y;
980 else if (vdist(flat_diff, <, 32) && diff.z < -16) // destination is under the bot
982 actual_destorg.x = destorg.x;
983 actual_destorg.y = destorg.y;
985 else if (vlen2(flat_diff) < vlen2(offset))
987 vector next_goal_org = (this.goalstack01.absmin + this.goalstack01.absmax) * 0.5;
988 vector next_dir = normalize(vec2(next_goal_org - destorg));
989 float dist = vlen(vec2(this.origin + offset - destorg));
990 // if current and next goal are close to each other make sure
991 // actual_destorg isn't set beyond next_goal_org
992 if (dist ** 2 > vlen2(vec2(next_goal_org - destorg)))
993 actual_destorg = next_goal_org;
995 actual_destorg = vec2(destorg) + dist * next_dir;
996 actual_destorg.z = this.origin.z;
1000 LABEL(jumpobstacle_check);
1001 dir = flatdir = normalize(actual_destorg - this.origin);
1003 bool jump_forbidden = false;
1004 if (!turning && fabs(deviation.y) > 50)
1005 jump_forbidden = true;
1006 else if (IS_DUCKED(this))
1008 tracebox(this.origin, PL_MIN_CONST, PL_MAX_CONST, this.origin, false, this);
1009 if (trace_startsolid)
1010 jump_forbidden = true;
1013 if (!jump_forbidden)
1015 tracebox(this.origin, this.mins, this.maxs, actual_destorg, false, this);
1016 if (trace_fraction < 1 && trace_plane_normal.z < 0.7)
1019 tracebox(this.origin + stepheightvec, this.mins, this.maxs, actual_destorg + stepheightvec, false, this);
1020 if (trace_fraction < s + 0.01 && trace_plane_normal.z < 0.7)
1022 // found an obstacle
1023 if (turning && fabs(deviation.y) > 5)
1025 // check if the obstacle is still there without turning
1026 actual_destorg = destorg;
1028 this.bot_tracewalk_time = time + 0.25;
1029 goto jumpobstacle_check;
1032 // don't artificially reduce max jump height in real-time
1033 // (jumpstepheightvec is reduced a bit to make the jumps easy in tracewalk)
1034 vector jump_height = (IS_ONGROUND(this)) ? stepheightvec + jumpheight_vec : jumpstepheightvec;
1035 tracebox(this.origin + jump_height, this.mins, this.maxs, actual_destorg + jump_height, false, this);
1036 if (trace_fraction > s)
1038 PHYS_INPUT_BUTTON_JUMP(this) = true;
1039 this.bot_jump_time = time;
1043 jump_height = stepheightvec + jumpheight_vec / 2;
1044 tracebox(this.origin + jump_height, this.mins, this.maxs, actual_destorg + jump_height, false, this);
1045 if (trace_fraction > s)
1047 PHYS_INPUT_BUTTON_JUMP(this) = true;
1048 this.bot_jump_time = time;
1055 // if bot for some reason doesn't get close to the current goal find another one
1056 if(!this.jumppadcount && !IS_PLAYER(this.goalcurrent))
1057 if(!(locked_goal && this.goalcurrent_distance_z < 50 && this.goalcurrent_distance_2d < 50))
1058 if(havocbot_checkgoaldistance(this, destorg))
1060 if(this.goalcurrent_distance_time < 0) // can't get close for the second time
1062 navigation_clearroute(this);
1063 navigation_goalrating_timeout_force(this);
1067 set_tracewalk_dest(this.goalcurrent, this.origin, false);
1068 if (!tracewalk(this, this.origin, this.mins, this.maxs,
1069 tracewalk_dest, tracewalk_dest_height, bot_navigation_movemode))
1071 navigation_clearroute(this);
1072 navigation_goalrating_timeout_force(this);
1076 // give bot only another chance to prevent bot getting stuck
1077 // in case it thinks it can walk but actually can't
1078 this.goalcurrent_distance_z = FLOAT_MAX;
1079 this.goalcurrent_distance_2d = FLOAT_MAX;
1080 this.goalcurrent_distance_time = -time; // mark second try
1083 if (skill + this.bot_moveskill <= 3 && time > this.bot_stop_moving_timeout
1084 && current_speed > maxspeed * 0.9 && fabs(deviation.y) > 70)
1086 this.bot_stop_moving_timeout = time + 0.4 + random() * 0.2;
1089 // Check for water/slime/lava and dangerous edges
1090 // (only when the bot is on the ground or jumping intentionally)
1092 offset = (vdist(this.velocity, >, 32) ? this.velocity * 0.2 : flatdir * 32);
1093 vector dst_ahead = this.origin + this.view_ofs + offset;
1094 vector dst_down = dst_ahead - '0 0 3000';
1095 traceline(this.origin + this.view_ofs, dst_ahead, true, NULL);
1097 bool unreachable = false;
1099 if (trace_fraction == 1 && !this.jumppadcount
1100 && !waypoint_is_hardwiredlink(this.goalcurrent_prev, this.goalcurrent)
1101 && !(this.goalcurrent_prev && (this.goalcurrent_prev.wpflags & WAYPOINTFLAG_JUMP)))
1102 if((IS_ONGROUND(this)) || (this.aistatus & AI_STATUS_RUNNING) || (this.aistatus & AI_STATUS_ROAMING) || PHYS_INPUT_BUTTON_JUMP(this))
1105 traceline(dst_ahead , dst_down, true, NULL);
1106 //te_lightning2(NULL, this.origin + this.view_ofs, dst_ahead); // Draw "ahead" look
1107 //te_lightning2(NULL, dst_ahead, trace_endpos); // Draw "downwards" look
1108 if(trace_endpos.z < this.origin.z + this.mins.z)
1110 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY)
1111 danger_detected = true;
1112 else if (trace_endpos.z < min(this.origin.z + this.mins.z, this.goalcurrent.origin.z) - 100)
1113 danger_detected = true;
1116 s = pointcontents(trace_endpos + '0 0 1');
1117 if (s != CONTENT_SOLID)
1119 if (s == CONTENT_LAVA || s == CONTENT_SLIME)
1120 danger_detected = true;
1121 else if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
1123 // the traceline check isn't enough but is good as optimization,
1124 // when not true (most of the time) this tracebox call is avoided
1125 tracebox(dst_ahead, this.mins, this.maxs, dst_down, true, this);
1126 if (tracebox_hits_trigger_hurt(dst_ahead, this.mins, this.maxs, trace_endpos))
1128 if (destorg.z > this.origin.z + jumpstepheightvec.z)
1130 // the goal is probably on an upper platform, assume bot can't get there
1134 danger_detected = true;
1143 makevectors(this.v_angle.y * '0 1 0');
1145 if (danger_detected || (s == CONTENT_WATER))
1147 this.aistatus |= AI_STATUS_DANGER_AHEAD;
1148 if(IS_PLAYER(this.goalcurrent))
1152 // slow down if bot is in the air and goal is under it
1153 if (!waypoint_is_hardwiredlink(this.goalcurrent_prev, this.goalcurrent)
1154 && vdist(flat_diff, <, 250) && this.origin.z - destorg.z > 120
1155 && (!IS_ONGROUND(this) || vdist(vec2(this.velocity), >, maxspeed * 0.3)))
1157 // tracebox wouldn't work when bot is still on the ledge
1158 traceline(this.origin, this.origin - '0 0 200', true, this);
1159 if (this.origin.z - trace_endpos.z > 120)
1160 do_break = normalize(this.velocity) * -1;
1165 navigation_clearroute(this);
1166 navigation_goalrating_timeout_force(this);
1167 this.ignoregoal = this.goalcurrent;
1168 this.ignoregoaltime = time + autocvar_bot_ai_ignoregoal_timeout;
1172 dodge = havocbot_dodge(this);
1174 dodge *= bound(0, 0.5 + (skill + this.bot_dodgeskill) * 0.1, 1);
1177 traceline(this.origin, (this.enemy.absmin + this.enemy.absmax) * 0.5, true, NULL);
1178 if (IS_PLAYER(trace_ent))
1179 dodge_enemy_factor = bound(0, (skill + this.bot_dodgeskill) / 7, 1);
1181 // this.bot_dodgevector = dir;
1182 // this.bot_dodgevector_jumpbutton = PHYS_INPUT_BUTTON_JUMP(this);
1185 float ladder_zdir = 0;
1186 if(this.ladder_entity)
1188 if(this.goalcurrent.origin.z + this.goalcurrent.mins.z > this.origin.z + this.mins.z)
1190 if(this.origin.z + this.mins.z < this.ladder_entity.origin.z + this.ladder_entity.maxs.z)
1195 if(this.origin.z + this.mins.z > this.ladder_entity.origin.z + this.ladder_entity.mins.z)
1200 if (vdist(vec2(diff), <, 40))
1201 dir.z = ladder_zdir * 4;
1203 dir.z = ladder_zdir * 2;
1204 dir = normalize(dir);
1208 if (this.goalcurrent.wpisbox
1209 && boxesoverlap(this.goalcurrent.absmin, this.goalcurrent.absmax, this.origin, this.origin))
1211 // bot is inside teleport waypoint but hasn't touched the real teleport yet
1212 // head to teleport origin
1213 dir = (this.goalcurrent.origin - this.origin);
1215 dir = normalize(dir);
1218 // already executed when bot targets an enemy
1219 if (!this.bot_aimdir_executed)
1221 if (time < this.bot_stop_moving_timeout)
1222 bot_aimdir(this, normalize(this.goalcurrent.origin - this.origin), 0);
1224 bot_aimdir(this, dir, 0);
1227 vector evadedanger = '0 0 0';
1230 dir *= dodge_enemy_factor;
1231 if (danger_detected && vdist(this.velocity, >, maxspeed * 0.8) && this.goalcurrent_prev
1232 && this.goalcurrent.classname == "waypoint")
1234 vector p = this.origin + this.velocity * 0.2;
1235 vector evadedanger = point_line_vec(p, vec2(this.goalcurrent_prev.origin) + eZ * p.z,
1236 vec2(destorg - this.goalcurrent_prev.origin));
1237 if (vdist(evadedanger, >, 20))
1239 if (vdist(evadedanger, >, 40))
1240 do_break = normalize(this.velocity) * -1;
1241 evadedanger = normalize(evadedanger);
1242 evadedanger *= bound(1, 3 - (skill + this.bot_dodgeskill), 3); // Noobs fear dangers a lot and take more distance from them
1245 evadedanger = '0 0 0';
1247 dir = normalize(dir + dodge + do_break + evadedanger);
1250 makevectors(this.v_angle);
1251 //dir = this.bot_dodgevector;
1252 //if (this.bot_dodgevector_jumpbutton)
1253 // PHYS_INPUT_BUTTON_JUMP(this) = true;
1254 CS(this).movement_x = dir * v_forward * maxspeed;
1255 CS(this).movement_y = dir * v_right * maxspeed;
1256 CS(this).movement_z = dir * v_up * maxspeed;
1258 // Emulate keyboard interface
1260 havocbot_keyboard_movement(this, destorg);
1263 if (!bunnyhop_forbidden && !evadedanger && !do_break && skill + this.bot_moveskill >= autocvar_bot_ai_bunnyhop_skilloffset)
1264 havocbot_bunnyhop(this, dir);
1266 if (dir * v_up >= autocvar_sv_jumpvelocity * 0.5 && IS_ONGROUND(this))
1267 PHYS_INPUT_BUTTON_JUMP(this) = true;
1270 if (dodge * v_up > 0 && random() * frametime >= 0.2 * bound(0, (10 - skill - this.bot_dodgeskill) * 0.1, 1))
1271 PHYS_INPUT_BUTTON_JUMP(this) = true;
1272 if (dodge * v_up < 0 && random() * frametime >= 0.5 * bound(0, (10 - skill - this.bot_dodgeskill) * 0.1, 1))
1273 this.havocbot_ducktime = time + 0.3 / bound(0.1, skill + this.bot_dodgeskill, 10);
1277 entity havocbot_gettarget(entity this, bool secondary)
1280 vector eye = CENTER_OR_VIEWOFS(this);
1281 IL_EACH(g_bot_targets, boolean((secondary) ? it.classname == "misc_breakablemodel" : it.classname != "misc_breakablemodel"),
1283 vector v = CENTER_OR_VIEWOFS(it);
1284 if(vdist(v - eye, <, autocvar_bot_ai_enemydetectionradius))
1285 if(!best || vlen2(CENTER_OR_VIEWOFS(best) - eye) > vlen2(v - eye))
1286 if(bot_shouldattack(this, it))
1288 traceline(eye, v, true, this);
1289 if (trace_ent == it || trace_fraction >= 1)
1297 void havocbot_chooseenemy(entity this)
1299 if (autocvar_bot_nofire || IS_INDEPENDENT_PLAYER(this))
1307 if (!bot_shouldattack(this, this.enemy))
1309 // enemy died or something, find a new target
1311 this.havocbot_chooseenemy_finished = time;
1313 else if (this.havocbot_stickenemy_time && time < this.havocbot_stickenemy_time)
1315 // tracking last chosen enemy
1316 vector targ_pos = (this.enemy.absmin + this.enemy.absmax) * 0.5;
1317 traceline(this.origin + this.view_ofs, targ_pos, false, NULL);
1318 if (trace_ent == this.enemy || trace_fraction == 1)
1319 if (vdist(targ_pos - this.origin, <, 1000))
1321 // remain tracking him for a shot while (case he went after a small corner or pilar
1322 this.havocbot_chooseenemy_finished = time + 0.5;
1326 // stop preferring this enemy
1327 this.havocbot_stickenemy_time = 0;
1330 if (time < this.havocbot_chooseenemy_finished)
1332 this.havocbot_chooseenemy_finished = time + autocvar_bot_ai_enemydetectioninterval;
1333 vector eye = this.origin + this.view_ofs;
1335 float bestrating = autocvar_bot_ai_enemydetectionradius ** 2;
1338 int hf = this.dphitcontentsmask;
1340 // Search for enemies, if no enemy can be seen directly try to look through transparent objects
1342 this.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
1344 bool scan_transparent = false;
1345 bool scan_secondary_targets = false;
1346 bool have_secondary_targets = false;
1349 scan_secondary_targets = false;
1351 IL_EACH(g_bot_targets, it.bot_attack,
1353 if(!scan_secondary_targets)
1355 if(it.classname == "misc_breakablemodel")
1357 have_secondary_targets = true;
1361 else if(it.classname != "misc_breakablemodel")
1364 vector v = (it.absmin + it.absmax) * 0.5;
1365 float rating = vlen2(v - eye);
1366 if (rating < bestrating && bot_shouldattack(this, it))
1368 traceline(eye, v, true, this);
1369 if (trace_ent == it || trace_fraction >= 1)
1372 bestrating = rating;
1377 if(!best && have_secondary_targets && !scan_secondary_targets)
1379 scan_secondary_targets = true;
1381 bestrating = autocvar_bot_ai_enemydetectionradius ** 2;
1385 // I want to do a second scan if no enemy was found or I don't have weapons
1386 // TODO: Perform the scan when using the rifle (requires changes on the rifle code)
1387 if(best || STAT(WEAPONS, this)) // || this.weapon == WEP_RIFLE.m_id
1389 if(scan_transparent)
1392 // Set flags to see through transparent objects
1393 this.dphitcontentsmask |= DPCONTENTS_OPAQUE;
1395 scan_transparent = true;
1398 // Restore hit flags
1399 this.dphitcontentsmask = hf;
1402 this.havocbot_stickenemy_time = time + autocvar_bot_ai_enemydetectioninterval_stickingtoenemy;
1403 if(best && best.classname == "misc_breakablemodel")
1404 this.havocbot_stickenemy_time = 0;
1407 float havocbot_chooseweapon_checkreload(entity this, .entity weaponentity, int new_weapon)
1409 // bots under this skill cannot find unloaded weapons to reload idly when not in combat,
1410 // so skip this for them, or they'll never get to reload their weapons at all.
1411 // this also allows bots under this skill to be more stupid, and reload more often during combat :)
1415 // if this weapon is scheduled for reloading, don't switch to it during combat
1416 if (this.(weaponentity).weapon_load[new_weapon] < 0)
1418 FOREACH(Weapons, it != WEP_Null, {
1419 if(it.wr_checkammo1(it, this, weaponentity) + it.wr_checkammo2(it, this, weaponentity))
1420 return true; // other weapon available
1427 void havocbot_chooseweapon(entity this, .entity weaponentity)
1432 if(g_weaponarena_weapons == WEPSET(TUBA))
1434 this.(weaponentity).m_switchweapon = WEP_TUBA;
1438 // TODO: clean this up by moving it to weapon code
1439 if(this.enemy==NULL)
1441 // If no weapon was chosen get the first available weapon
1442 if(this.(weaponentity).m_weapon==WEP_Null)
1443 FOREACH(Weapons, it != WEP_Null, {
1444 if(client_hasweapon(this, it, weaponentity, true, false))
1446 this.(weaponentity).m_switchweapon = it;
1453 // Do not change weapon during the next second after a combo
1454 float f = time - this.lastcombotime;
1459 float distance; distance=bound(10,vlen(this.origin-this.enemy.origin)-200,10000);
1461 // Should it do a weapon combo?
1462 float af, ct, combo_time, combo;
1464 af = ATTACK_FINISHED(this, weaponentity);
1465 ct = autocvar_bot_ai_weapon_combo_threshold;
1467 // Bots with no skill will be 4 times more slower than "godlike" bots when doing weapon combos
1468 // Ideally this 4 should be calculated as longest_weapon_refire / bot_ai_weapon_combo_threshold
1469 combo_time = time + ct + (ct * ((-0.3*(skill+this.bot_weaponskill))+3));
1473 if(autocvar_bot_ai_weapon_combo)
1474 if(this.(weaponentity).m_weapon.m_id == this.(weaponentity).lastfiredweapon)
1478 this.lastcombotime = time;
1481 distance *= (2 ** this.bot_rangepreference);
1483 // Custom weapon list based on distance to the enemy
1484 if(bot_custom_weapon){
1486 // Choose weapons for far distance
1487 if ( distance > bot_distance_far ) {
1488 for(i=0; i < REGISTRY_COUNT(Weapons) && bot_weapons_far[i] != -1 ; ++i){
1489 w = bot_weapons_far[i];
1490 if ( client_hasweapon(this, REGISTRY_GET(Weapons, w), weaponentity, true, false) )
1492 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1494 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, w);
1500 // Choose weapons for mid distance
1501 if ( distance > bot_distance_close) {
1502 for(i=0; i < REGISTRY_COUNT(Weapons) && bot_weapons_mid[i] != -1 ; ++i){
1503 w = bot_weapons_mid[i];
1504 if ( client_hasweapon(this, REGISTRY_GET(Weapons, w), weaponentity, true, false) )
1506 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1508 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, w);
1514 // Choose weapons for close distance
1515 for(i=0; i < REGISTRY_COUNT(Weapons) && bot_weapons_close[i] != -1 ; ++i){
1516 w = bot_weapons_close[i];
1517 if ( client_hasweapon(this, REGISTRY_GET(Weapons, w), weaponentity, true, false) )
1519 if ((this.(weaponentity).m_weapon.m_id == w && combo) || havocbot_chooseweapon_checkreload(this, weaponentity, w))
1521 this.(weaponentity).m_switchweapon = REGISTRY_GET(Weapons, w);
1528 void havocbot_aim(entity this)
1530 if (time < this.nextaim)
1532 this.nextaim = time + 0.1;
1533 vector myvel = this.velocity;
1534 if (!this.waterlevel)
1536 if(MUTATOR_CALLHOOK(HavocBot_Aim, this)) { /* do nothing */ }
1537 else if (this.enemy)
1539 vector enemyvel = this.enemy.velocity;
1540 if (!this.enemy.waterlevel)
1542 lag_additem(this, time + CS(this).ping, 0, 0, this.enemy, this.origin, myvel, (this.enemy.absmin + this.enemy.absmax) * 0.5, enemyvel);
1545 lag_additem(this, time + CS(this).ping, 0, 0, NULL, this.origin, myvel, ( this.goalcurrent.absmin + this.goalcurrent.absmax ) * 0.5, '0 0 0');
1548 bool havocbot_moveto_refresh_route(entity this)
1550 // Refresh path to goal if necessary
1552 wp = this.havocbot_personal_waypoint;
1553 navigation_goalrating_start(this);
1554 navigation_routerating(this, wp, 10000, 10000);
1555 navigation_goalrating_end(this);
1556 return (this.goalentity != NULL);
1559 float havocbot_moveto(entity this, vector pos)
1563 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_GOING)
1565 // Step 4: Move to waypoint
1566 if(this.havocbot_personal_waypoint==NULL)
1568 LOG_TRACE("Error: ", this.netname, " trying to walk to a non existent personal waypoint");
1569 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_GOING;
1570 return CMD_STATUS_ERROR;
1573 if (!bot_strategytoken_taken)
1574 if(this.havocbot_personal_waypoint_searchtime<time)
1576 bot_strategytoken_taken = true;
1577 if(havocbot_moveto_refresh_route(this))
1579 LOG_TRACE(this.netname, " walking to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts)");
1580 this.havocbot_personal_waypoint_searchtime = time + 10;
1581 this.havocbot_personal_waypoint_failcounter = 0;
1585 this.havocbot_personal_waypoint_failcounter += 1;
1586 this.havocbot_personal_waypoint_searchtime = time + 2;
1587 if(this.havocbot_personal_waypoint_failcounter >= 30)
1589 LOG_TRACE("Warning: can't walk to the personal waypoint located at ", vtos(this.havocbot_personal_waypoint.origin));
1590 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1591 delete(this.havocbot_personal_waypoint);
1592 return CMD_STATUS_ERROR;
1595 LOG_TRACE(this.netname, " can't walk to its personal waypoint (after ", ftos(this.havocbot_personal_waypoint_failcounter), " failed attempts), trying later");
1599 if(autocvar_bot_debug_goalstack)
1600 debuggoalstack(this);
1604 havocbot_movetogoal(this);
1606 if (!this.bot_aimdir_executed && this.goalcurrent)
1609 vector dir = get_closer_dest(this.goalcurrent, this.origin);
1610 dir -= this.origin + this.view_ofs;
1612 bot_aimdir(this, dir, 0);
1615 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_REACHED)
1617 // Step 5: Waypoint reached
1618 LOG_TRACE(this.netname, "'s personal waypoint reached");
1619 waypoint_remove(this.havocbot_personal_waypoint);
1620 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_REACHED;
1621 return CMD_STATUS_FINISHED;
1624 return CMD_STATUS_EXECUTING;
1627 // Step 2: Linking waypoint
1628 if(this.aistatus & AI_STATUS_WAYPOINT_PERSONAL_LINKING)
1630 // Wait until it is linked
1631 if(!this.havocbot_personal_waypoint.wplinked)
1633 LOG_TRACE(this.netname, " waiting for personal waypoint to be linked");
1634 return CMD_STATUS_EXECUTING;
1637 this.havocbot_personal_waypoint_searchtime = time; // so we set the route next frame
1638 this.aistatus &= ~AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1639 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_GOING;
1641 // Step 3: Route to waypoint
1642 LOG_TRACE(this.netname, " walking to its personal waypoint");
1644 return CMD_STATUS_EXECUTING;
1647 // Step 1: Spawning waypoint
1648 wp = waypoint_spawnpersonal(this, pos);
1651 LOG_TRACE("Error: Can't spawn personal waypoint at ",vtos(pos));
1652 return CMD_STATUS_ERROR;
1655 this.havocbot_personal_waypoint = wp;
1656 this.havocbot_personal_waypoint_failcounter = 0;
1657 this.aistatus |= AI_STATUS_WAYPOINT_PERSONAL_LINKING;
1659 // if pos is inside a teleport, then let's mark it as teleport waypoint
1660 IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(pos, pos, it, NULL),
1662 wp.wpflags |= WAYPOINTFLAG_TELEPORT;
1663 this.lastteleporttime = 0;
1667 if(wp.wpflags & WAYPOINTFLAG_TELEPORT)
1668 print("routing to a teleporter\n");
1670 print("routing to a non-teleporter\n");
1673 return CMD_STATUS_EXECUTING;
1676 float havocbot_resetgoal(entity this)
1678 navigation_clearroute(this);
1679 return CMD_STATUS_FINISHED;
1682 void havocbot_setupbot(entity this)
1684 this.bot_ai = havocbot_ai;
1685 this.cmd_moveto = havocbot_moveto;
1686 this.cmd_resetgoal = havocbot_resetgoal;
1688 // NOTE: bot is not player yet
1689 havocbot_chooserole(this);
1692 vector havocbot_dodge(entity this)
1694 // LordHavoc: disabled because this is too expensive
1699 float danger, bestdanger, vl, d;
1702 // check for dangerous objects near bot or approaching bot
1703 head = findchainfloat(bot_dodge, true);
1706 if (head.owner != this)
1708 vl = vlen(head.velocity);
1709 if (vl > autocvar_sv_maxspeed * 0.3)
1711 n = normalize(head.velocity);
1712 v = this.origin - head.origin;
1714 if (d > (0 - head.bot_dodgerating))
1715 if (d < (vl * 0.2 + head.bot_dodgerating))
1717 // calculate direction and distance from the flight path, by removing the forward axis
1718 v = v - (n * (v * n));
1719 danger = head.bot_dodgerating - vlen(v);
1720 if (bestdanger < danger)
1722 bestdanger = danger;
1723 // dodge to the side of the object
1724 dodge = normalize(v);
1730 danger = head.bot_dodgerating - vlen(head.origin - this.origin);
1731 if (bestdanger < danger)
1733 bestdanger = danger;
1734 dodge = normalize(this.origin - head.origin);