//---------------------------------------------------------------
// Repair
//
// Repair the structure for iAmount of health
//---------------------------------------------------------------
eNDErrorCode CNDBaseStructure::Repair( int iHealthToAdd )
{
int iHealthMissing = GetMaxHealth() - GetHealth();
if( iHealthMissing <= 0 )
return ND_ERROR_ALREADY_FULL_HEALTH;
if( iHealthToAdd > iHealthMissing )
iHealthToAdd = iHealthMissing;
// Update health and visuals
SetHealth( GetHealth() + iHealthToAdd );
UpdateVisualState();
bool bReactivate = false;
// If we're in Broken state and no longer critically damaged, try to reactivate
if( IsBroken() && !IsCriticallyDamaged() )
bReactivate = true;
// Done constructing / Repairing?
if( GetHealth() >= GetMaxHealth() )
{
// Make sure we didn't go beyond MaxHealth
SetHealth( GetMaxHealth() );
if( !IsConstructionComplete() )
{
DevMsg("CNDBaseStructure::OnPlayerRepair - Construction complete \n");
m_bConstructionComplete = true;
FireEvent( NDEVENT_ConstructionComplete );
bReactivate = true;
}
else
{
DevMsg("CNDBaseStructure::OnPlayerRepair - Repair complete \n");
FireEvent( NDEVENT_RepairComplete );
}
}
// Update health info
NetworkStateChanged();
// If we need to re/activate the structure
if( bReactivate )
{
eNDErrorCode iRet = ND_FAILED;
// If we were off before turning broken, restore as OFF
if( GetOldState() == NDSTATE_Off )
iRet = ChangeState( NDSTATE_Off );
else
{
// Try to turn ON
iRet = ChangeState( NDSTATE_On );
// If we didn't have enough power to turn ON, go on Power Outage state
if( iRet == ND_ERROR_NOT_ENOUGH_POWER )
iRet = ChangeState( NDSTATE_PowerOut );
// That failed too for some reason, try with State_OFF
if( iRet != ND_SUCCESS )
iRet = ChangeState( NDSTATE_Off );
}
// We successfully changed state
if( iRet == ND_SUCCESS )
{
// Extinguish fire if necessary
if( IsOnFire() )
Extinguish();
}
else // Booo, everything failed - Must never happen
{
Warning("CNDBaseStructure::Repair - Unable to ACTIVATE structure - Last ChangeState returned %d\n", iRet);
return iRet;
}
}
return ND_SUCCESS;
}