void CAI_Squad::SquadNewEnemy( CBaseEntity *pEnemy ) { if ( !pEnemy ) { DevMsg( "ERROR: SquadNewEnemy() - pEnemy is NULL!\n" ); return; } for (int i = 0; i < m_SquadMembers.Count(); i++) { CAI_BaseNPC *pMember = m_SquadMembers[i]; if (pMember) { // reset members who aren't activly engaged in fighting (only do this if the NPC's using the squad memory, or it'll fail) if ( !pMember->GetEnemy() || ( pMember->GetEnemy() != pEnemy && !pMember->HasCondition( COND_SEE_ENEMY) && gpGlobals->curtime - pMember->GetEnemyLastTimeSeen() > 3.0 ) ) { // give them a new enemy if( !hl2_episodic.GetBool() || pMember->IsValidEnemy(pEnemy) ) { pMember->SetEnemy( pEnemy ); } // pMember->SetLastAttackTime( 0 ); } } } }
bool CAI_MoveAndShootOverlay::CanAimAtEnemy() { CAI_BaseNPC *pOuter = GetOuter(); bool result = false; bool resetConditions = false; CAI_ScheduleBits savedConditions; if ( !GetOuter()->ConditionsGathered() ) { savedConditions = GetOuter()->AccessConditionBits(); GetOuter()->GatherEnemyConditions( GetOuter()->GetEnemy() ); } if ( pOuter->HasCondition( COND_CAN_RANGE_ATTACK1 ) ) { result = true; } else if ( !pOuter->HasCondition( COND_ENEMY_DEAD ) && !pOuter->HasCondition( COND_TOO_FAR_TO_ATTACK ) && !pOuter->HasCondition( COND_ENEMY_TOO_FAR ) && !pOuter->HasCondition( COND_ENEMY_OCCLUDED ) && !pOuter->HasCondition( COND_NO_PRIMARY_AMMO ) ) { result = true; } if ( resetConditions ) { GetOuter()->AccessConditionBits() = savedConditions; } return result; }
bool CAI_MoveAndShootOverlay::CanAimAtEnemy() { CAI_BaseNPC *pOuter = GetOuter(); bool result = false; bool resetConditions = false; CAI_ScheduleBits savedConditions; if ( !GetOuter()->ConditionsGathered() ) { savedConditions = GetOuter()->AccessConditionBits(); GetOuter()->GatherEnemyConditions( GetOuter()->GetEnemy() ); } if ( pOuter->HasCondition( COND_CAN_RANGE_ATTACK1 ) ) { result = true; } else if ( !pOuter->HasCondition( COND_ENEMY_DEAD ) && !pOuter->HasCondition( COND_TOO_FAR_TO_ATTACK ) && !pOuter->HasCondition( COND_ENEMY_TOO_FAR ) && !pOuter->HasCondition( COND_ENEMY_OCCLUDED ) ) { result = true; } // If we don't have a weapon, stop // This catches NPCs who holster their weapons while running if ( !HasAvailableRangeAttack() ) { result = false; } if ( resetConditions ) { GetOuter()->AccessConditionBits() = savedConditions; } return result; }
void CAI_MoveAndShootOverlay::RunShootWhileMove() { if ( m_flNextMoveShootTime == FLT_MAX ) return; CAI_BaseNPC *pOuter = GetOuter(); // keep enemy if dead but try to look for a new one if (!pOuter->GetEnemy() || !pOuter->GetEnemy()->IsAlive()) { CBaseEntity *pNewEnemy = pOuter->BestEnemy(); if( pNewEnemy != NULL ) { //New enemy! Clear the timers and set conditions. pOuter->SetCondition( COND_NEW_ENEMY ); pOuter->SetEnemy( pNewEnemy ); pOuter->SetState( NPC_STATE_COMBAT ); } else { pOuter->ClearAttackConditions(); } // SetEnemy( NULL ); } if ( GetEnemy() == NULL || !pOuter->GetNavigator()->IsGoalActive() ) return; bool bMoveAimAtEnemy = CanAimAtEnemy(); UpdateMoveShootActivity( bMoveAimAtEnemy ); if (bMoveAimAtEnemy) { Assert( pOuter->GetActiveWeapon() ); // This should have been caught at task start // time to fire? if ( pOuter->HasCondition( COND_CAN_RANGE_ATTACK1 ) && gpGlobals->curtime >= m_flNextMoveShootTime ) { if ( m_bMovingAndShooting || GetOuter()->OnBeginMoveAndShoot() ) { m_bMovingAndShooting = true; Activity activity = pOuter->TranslateActivity( ACT_GESTURE_RANGE_ATTACK1 ); Assert( activity != ACT_INVALID ); if (--m_nMoveShots > 0) { pOuter->SetLastAttackTime( gpGlobals->curtime ); pOuter->AddGesture( activity ); // FIXME: this seems a bit wacked pOuter->Weapon_SetActivity( pOuter->Weapon_TranslateActivity( ACT_RANGE_ATTACK1 ), 0 ); m_flNextMoveShootTime = gpGlobals->curtime + pOuter->GetActiveWeapon()->GetFireRate() - 0.1; } else { m_nMoveShots = random->RandomInt( m_minBurst, m_maxBurst ); m_flNextMoveShootTime = gpGlobals->curtime + random->RandomFloat( m_minPause, m_maxPause ); m_bMovingAndShooting = false; GetOuter()->OnEndMoveAndShoot(); } } } // try to keep facing towards the last known position of the enemy Vector vecEnemyLKP = pOuter->GetEnemyLKP(); pOuter->AddFacingTarget( pOuter->GetEnemy(), vecEnemyLKP, 1.0, 0.8 ); } else { if ( m_bMovingAndShooting ) { m_bMovingAndShooting = false; GetOuter()->OnEndMoveAndShoot(); } } }
void CAI_MoveAndShootOverlay::RunShootWhileMove() { if ( m_bNoShootWhileMove ) return; if ( gpGlobals->curtime < m_flSuspendUntilTime ) return; m_flSuspendUntilTime = MOVESHOOT_DO_NOT_SUSPEND; CAI_BaseNPC *pOuter = GetOuter(); // keep enemy if dead but try to look for a new one if (!pOuter->GetEnemy() || !pOuter->GetEnemy()->IsAlive()) { CBaseEntity *pNewEnemy = pOuter->BestEnemy(); if( pNewEnemy != NULL ) { //New enemy! Clear the timers and set conditions. pOuter->SetEnemy( pNewEnemy ); pOuter->SetState( NPC_STATE_COMBAT ); } else { pOuter->ClearAttackConditions(); } // SetEnemy( NULL ); } /*if( !pOuter->GetNavigator()->IsGoalActive() ) return;*/ if ( GetEnemy() == NULL ) { if ( pOuter->GetAlternateMoveShootTarget() ) { // Aim at this other thing if I can't aim at my enemy. pOuter->AddFacingTarget( pOuter->GetAlternateMoveShootTarget(), pOuter->GetAlternateMoveShootTarget()->GetAbsOrigin(), 1.0, 0.2 ); } return; } bool bMoveAimAtEnemy = CanAimAtEnemy(); UpdateMoveShootActivity( bMoveAimAtEnemy ); if ( !bMoveAimAtEnemy ) { EndShootWhileMove(); return; } Assert( HasAvailableRangeAttack() ); // This should have been caught at task start Activity activity; bool bIsReloading = false; if ( ( activity = pOuter->TranslateActivity( ACT_GESTURE_RELOAD ) ) != ACT_INVALID ) { bIsReloading = pOuter->IsPlayingGesture( activity ); } if ( !bIsReloading && HasAvailableRangeAttack() ) { // time to fire? if ( pOuter->HasCondition( COND_CAN_RANGE_ATTACK1, false ) ) { if ( pOuter->GetShotRegulator()->IsInRestInterval() ) { EndShootWhileMove(); } else if ( pOuter->GetShotRegulator()->ShouldShoot() ) { if ( m_bMovingAndShooting || pOuter->OnBeginMoveAndShoot() ) { m_bMovingAndShooting = true; pOuter->OnRangeAttack1(); activity = pOuter->TranslateActivity( ACT_GESTURE_RANGE_ATTACK1 ); Assert( activity != ACT_INVALID ); pOuter->RestartGesture( activity ); // FIXME: this seems a bit wacked pOuter->Weapon_SetActivity( pOuter->Weapon_TranslateActivity( ACT_RANGE_ATTACK1 ), 0 ); } } } } // try to keep facing towards the last known position of the enemy Vector vecEnemyLKP = pOuter->GetEnemyLKP(); pOuter->AddFacingTarget( pOuter->GetEnemy(), vecEnemyLKP, 1.0, 0.8 ); }