class PowerupExtender : Inventory
{
	Array<string> ExtendThese;
	bool WasExtended [99];
	int Tally;

	Default
	{
		Inventory.MaxAmount 0x7FFFFFFF;
	}
	
	// Redefine this virtual in every subclass with the last
	// two of its current components.
	override void AttachToOwner(Actor user)
	{
		// ZScript is dumb and does not zero undeclared variables.
		// Does not need to be called again in subclasses.
		InitializeVariables();
		
		// Push the classname of each powerup you wish to extend
		// to the array. As an example:
		// ExtendThese.Push("PowerInvulnerable");
		
		// Call this after defining the list above.
		Super.AttachToOwner(user);
	}
	
	override void DoEffect()
	{
		// Attempt to extend the current powerups. Powerups
		// don't always exist in inventory on the tic they
		// are given, hence needing a DoEffect to *ensure*
		// they are extended.
		for(int i = 0; i < ExtendThese.Size(); i++)
		{
			if(!WasExtended[i])
			{
				let CurrentPowerup = Powerup(owner.FindInventory(ExtendThese[i]));
				if(CurrentPowerup)
				{
					CurrentPowerup.EffectTics += 15 * 35 * owner.CountInv("AlchemyLab");
					WasExtended[i] = TRUE;
					Tally++;
				}
			}
		}
		
		// Extending cycle complete.
		if(Tally >= ExtendThese.Size())
		{
			let me = owner.FindInventory(self.GetClassName());
			if(me.Amount > 1)
			{
				// If more than one of this item exists, do the cycle of
				// extending all over again.
				for(int i = 0; i < ExtendThese.Size(); i++)
				{
					WasExtended[i] = FALSE;
				}
				Tally = 0;
				owner.SetInventory(me.GetClassName(), me.Amount - 1);
			}
			else
			{
				// When we have extended all powerups and expended all uses,
				// go away and die.
				owner.RemoveInventory(self);
			}
		}
	}
	
	void InitializeVariables()
	{
		Tally = 0;
		for(int i = 0; i < ExtendThese.Size(); i++)
		{
			WasExtended[i] = FALSE;
		}
	}
}