#include "pch.h"
#include "shieldIGC.h"
CshieldIGC::CshieldIGC(void)
:
m_partType(NULL),
m_ship(NULL),
m_fraction(0.0f),
m_mountID(c_mountNA)
{
}
HRESULT CshieldIGC::Initialize(ImissionIGC* pMission, Time now, const void* data, int dataSize)
{
m_pMission = pMission;
ZRetailAssert (data && (dataSize == sizeof(DataPartIGC)));
{
m_partType = ((DataPartIGC*)data)->partType;
assert (m_partType);
m_partType->AddRef();
m_typeData = (const DataShieldTypeIGC*)m_partType->GetData();
}
return S_OK;
}
void CshieldIGC::Terminate(void)
{
AddRef();
SetShip(NULL, NA);
if (m_partType)
{
m_partType->Release();
m_partType = NULL;
}
Release();
}
void CshieldIGC::Update(Time now)
{
assert (m_ship);
assert (m_mountID == 0);
Time lastUpdate = m_ship->GetLastUpdate();
assert (now >= lastUpdate);
float dt = now - lastUpdate;
if (m_mountedFraction < 1.0f)
{
m_mountedFraction += dt * m_pMission->GetFloatConstant(c_fcidMountRate);
if (m_mountedFraction < 1.0f)
return;
IIgcSite* pigc = m_pMission->GetIgcSite();
pigc->PlayNotificationSound(mountedSound, m_ship);
pigc->PostNotificationText(m_ship, false, "%s ready.", GetPartType()->GetName());
m_mountedFraction = 1.0f;
}
float previousFraction = m_fraction;
SetFraction(m_fraction + GetRegeneration() * dt / GetMaxStrength());
if (previousFraction < 0.05 && m_fraction >= 0.05)
m_pMission->GetIgcSite()->PlaySoundEffect(m_typeData->activateSound, m_ship);
}
void CshieldIGC::SetShip(IshipIGC* newVal, Mount mount)
{
AddRef();
if (m_ship)
{
m_ship->DeletePart(this);
m_ship->Release();
}
assert (m_mountID == c_mountNA);
m_ship = newVal;
if (m_ship)
{
m_ship->AddRef();
m_ship->AddPart(this);
SetMountID(mount);
}
Release();
}
void CshieldIGC::SetMountID(Mount newVal)
{
assert (m_ship);
if (newVal != m_mountID)
{
if ((newVal < 0) && (m_mountID == 0))
Deactivate();
m_ship->MountPart(this, newVal, &m_mountID);
if (newVal == 0)
{
m_fraction = 0.0f;
Activate(); }
}
}
float CshieldIGC::ApplyDamage(Time timeCollision,
DamageTypeID type,
float amount,
const Vector& deltaP)
{
float dtm = m_pMission->GetDamageConstant(type, m_typeData->defenseType);
amount *= dtm;
float leakage;
if (amount > 0.0)
{
assert (dtm > 0.0f);
float maxStrength = GetMaxStrength();
float absorbed = amount / maxStrength;
float down = m_pMission->GetFloatConstant(c_fcidDownedShield);
if (m_fraction <= down)
{
leakage = amount / dtm;
}
else
{
float usefulShieldFraction = m_fraction - down;
assert (usefulShieldFraction >= 0.0f);
if ((type & c_dmgidNoFlare) == 0)
m_ship->GetThingSite()->AddFlare(timeCollision, deltaP, 0, m_ship->GetHitTest()->GetEllipseEquation());
if (absorbed <= usefulShieldFraction)
{
leakage = 0.0f;
}
else
{
leakage = (absorbed - usefulShieldFraction) * maxStrength / dtm;
assert (leakage >= 0.0f);
m_pMission->GetIgcSite()->PlaySoundEffect(m_typeData->deactivateSound, m_ship);
m_pMission->GetIgcSite()->PlayNotificationSound(salShieldsOfflineSound, m_ship);
}
}
SetFraction(m_fraction - absorbed);
}
else
leakage = 0.0f;
return leakage;
}