Skip to content

Commit

Permalink
Improve bot attack state
Browse files Browse the repository at this point in the history
This should stop bot aiming forever behind walls and getting stuck to a wall
  • Loading branch information
smallmodel committed Dec 14, 2024
1 parent f2f9ba9 commit 0765537
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
32 changes: 23 additions & 9 deletions code/fgame/playerbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ bool BotController::CheckCondition_Attack(void)

if (controlledEnt->CanSee(sent, 80, maxDistance, false)) {
m_pEnemy = sent;
m_vLastEnemyPos = m_pEnemy->centroid;
m_iAttackTime = level.inttime + 1000;
return true;
}
Expand All @@ -674,6 +675,7 @@ void BotController::State_EndAttack(void)
void BotController::State_Attack(void)
{
bool bMelee = false;
bool bCanSee = false;
float fMinDistance = 128;
float fMinDistanceSquared = fMinDistance * fMinDistance;
Weapon *pWeap = controlledEnt->GetActiveWeapon(WEAPON_MAIN);
Expand All @@ -686,9 +688,10 @@ void BotController::State_Attack(void)
}
float fDistanceSquared = (m_pEnemy->origin - controlledEnt->origin).lengthSquared();

if (controlledEnt->CanSee(
m_pEnemy, 20, Q_min(world->m_fAIVisionDistance, world->farplane_distance * 0.828), false
)) {
m_vOldEnemyPos = m_vLastEnemyPos;

bCanSee = controlledEnt->CanSee(m_pEnemy, 20, Q_min(world->m_fAIVisionDistance, world->farplane_distance * 0.828), false);
if (bCanSee) {
if (!pWeap) {
return;
}
Expand Down Expand Up @@ -761,30 +764,41 @@ void BotController::State_Attack(void)
controlledEnt->ZoomOff();
}

m_iAttackTime = level.inttime + 1000;
m_vOldEnemyPos = m_vLastEnemyPos;
m_vLastEnemyPos = m_pEnemy->centroid;
m_iAttackTime = level.inttime + 1000;
m_iAttackStopAimTime = level.inttime + 3000;
m_vLastEnemyPos = m_pEnemy->centroid;
} else {
m_botCmd.buttons &= ~(BUTTON_ATTACKLEFT | BUTTON_ATTACKRIGHT);
fMinDistanceSquared = 0;
}

rotation.AimAt(m_pEnemy->centroid + Vector(G_CRandom(8), G_CRandom(8), G_CRandom(8)));
if (bCanSee || level.inttime < m_iAttackStopAimTime) {
rotation.AimAt(m_vLastEnemyPos + Vector(G_CRandom(8), G_CRandom(8), G_CRandom(8)));
} else {
AimAtAimNode();
}

if (bNoMove) {
return;
}

if ((!movement.MoveToBestAttractivePoint(5) && !movement.IsMoving())
|| (m_vOldEnemyPos != m_vLastEnemyPos && !movement.MoveDone())) {
if (!bMelee) {
if (!bMelee || !bCanSee) {
if ((controlledEnt->origin - m_vLastEnemyPos).lengthSquared() < fMinDistanceSquared) {
Vector vDir = controlledEnt->origin - m_vLastEnemyPos;
VectorNormalizeFast(vDir);

movement.AvoidPath(m_vLastEnemyPos, fMinDistance, Vector(controlledEnt->orientation[1]) * 512);
} else {
movement.MoveNear(m_vLastEnemyPos, fMinDistance);
movement.MoveTo(m_vLastEnemyPos);
}

if (!bCanSee && movement.MoveDone()) {
// Lost track of the enemy
m_pEnemy = NULL;
m_iAttackTime = 0;
return;
}
} else {
movement.MoveTo(m_vLastEnemyPos);
Expand Down
1 change: 1 addition & 0 deletions code/fgame/playerbot.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class BotController : public Listener
// States
int m_iCuriousTime;
int m_iAttackTime;
int m_iAttackStopAimTime;
Vector m_vLastCuriousPos;
Vector m_vNewCuriousPos;
Vector m_vOldEnemyPos;
Expand Down

0 comments on commit 0765537

Please sign in to comment.