Little TakeInventory refactoring.

Introduce AActor::TakeInventory, which unifies DoTakeInv from ACS and DoTakeInventory from Decorate, and AInventory::DepleteOrDestroy, which is extracted from the DoTakeInv core function, and use both where they're needed.
I don't know if the differences between DoTakeInv and DoTakeInventory were intentional, so I kept both behaviors.
This commit is contained in:
Edoardo Prezioso 2015-04-28 15:34:13 +02:00
commit b51fac344d
10 changed files with 92 additions and 107 deletions

View file

@ -591,6 +591,57 @@ void AActor::RemoveInventory(AInventory *item)
}
}
//============================================================================
//
// AActor :: TakeInventory
//
//============================================================================
bool AActor::TakeInventory(const PClass *itemclass, int amount, bool fromdecorate, bool notakeinfinite)
{
AInventory *item = FindInventory(itemclass);
if (item == NULL)
return false;
if (!fromdecorate)
{
item->Amount -= amount;
if (item->Amount <= 0)
{
item->DepleteOrDestroy();
}
// It won't be used in non-decorate context, so return false here
return false;
}
bool result = false;
if (item->Amount > 0)
{
result = true;
}
if (item->IsKindOf(RUNTIME_CLASS(AHexenArmor)))
return false;
// Do not take ammo if the "no take infinite/take as ammo depletion" flag is set
// and infinite ammo is on
if (notakeinfinite &&
((dmflags & DF_INFINITE_AMMO) || (player && player->cheats & CF_INFINITEAMMO)) &&
item->IsKindOf(RUNTIME_CLASS(AAmmo)))
{
// Nothing to do here, except maybe res = false;? Would it make sense?
}
else if (!amount || amount>=item->Amount)
{
item->DepleteOrDestroy();
}
else item->Amount-=amount;
return result;
}
//============================================================================
//
// AActor :: DestroyAllInventory
@ -658,9 +709,9 @@ bool AActor::UseInventory (AInventory *item)
if (dmflags2 & DF2_INFINITE_INVENTORY)
return true;
if (--item->Amount <= 0 && !(item->ItemFlags & IF_KEEPDEPLETED))
if (--item->Amount <= 0)
{
item->Destroy ();
item->DepleteOrDestroy ();
}
return true;
}