Scriptname RenContainer:RenContainerRefAliasScript extends ReferenceAlias

Perk Property PowerArmorPerk Auto
MiscObject Property Caps001 Auto
Message Property RenContainerWontFit Auto
ActorValue Property CarryWeight Auto
Formlist Property RenContainerDroppedObjs Auto
ObjectReference Property RenContainerObj Auto
bool showedmessage = false

Event OnAliasInit()
	AddInventoryEventFilter(None)
endEvent

Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
	if ((GetReference() Is Actor) == true && (GetReference() As Actor).HasPerk(PowerArmorPerk) == true || akBaseItem == Caps001)
		return
	endif
	
	if (akItemReference.IsQuestItem() == true)
		return
	endif
	
	if (akItemReference == none)
		akItemReference = akBaseItem As ObjectReference
	endif
	
	int itr = aiItemCount
	
	showedmessage = false
	
	while (akItemReference.GetWidth() * itr > Game.GetPlayer().GetWidth() || akItemReference.GetLength() * itr > Game.GetPlayer().GetLength() || akItemReference.GetHeight() * itr > Game.GetPlayer().GetHeight())
		; the items won't fit!
		if (showedmessage == false)
			RenContainerWontFit.Show()
			showedmessage = true
		endif
		GetReference().DropObject(akBaseItem, 1)
		itr -= 1
	endWhile
endEvent

Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)

	RenContainerObj = akDestContainer As ObjectReference

	debug.messagebox(RenContainerObj + ", " + akDestContainer)
	
	if (akDestContainer != none	&& akBaseItem != Caps001 && Utility.IsInMenuMode() == true)
		ObjectReference placeditem
		if (akItemReference == none && akBaseItem != none)
			while (Utility.IsInMenuMode())
				; spin...
			endWhile		
			placeditem = akDestContainer.PlaceAtMe(akBaseItem, 1, false, false, false)
			while (placeditem.Is3DLoaded() == false && placeditem.IsDisabled() == false)
				; spin...
			endWhile
		else	
			placeditem = akItemReference
		endif
	
		float containervolume = akDestContainer.GetWidth() * akDestContainer.GetLength() * akDestContainer.GetHeight()
		; cm3
		
		; 1 game unit unit = 1.428 cm
		
		; Density = mass/volume
		; so volume = mass / density
		; object volume = (placeditem.GetMass() * 453.592) lbs to grams / 2.0 cm3
	
		int containeritems = akDestContainer.GetItemCount()
	
		debug.messagebox("Items in container: " + containeritems)
	
		int itr = 1
		
		float insidecontainermass = 0.0
		
		while (itr <= containeritems)
			ObjectReference tempref = akDestContainer.DropFirstObject(false)
			RenContainerDroppedObjs.AddForm(tempref)
			tempref = none
			insidecontainermass += (RenContainerDroppedObjs.GetAt(itr) As ObjectReference).GetMass() * 453.592
			itr += 1
		endWhile
	
		itr = 0
		
		while (itr < RenContainerDroppedObjs.GetSize())
			(RenContainerDroppedObjs.GetAt(itr) As ObjectReference).Enable()
			akDestContainer.AddItem(RenContainerDroppedObjs.GetAt(itr))
			itr += 1
		endWhile
		
		RenContainerDroppedObjs.Revert()
	
		debug.messagebox("Container weight: " + akDestContainer.GetMass() + ", carryweight: " + insidecontainermass + ", volume (" + akDestContainer.GetWidth() + " " + akDestContainer.GetLength() + " " + akDestContainer.GetHeight() + ": " + containervolume + ", object volume (" + placeditem.GetMass() + "): " + ((placeditem.GetMass() * 453.592) / 2.0))	
	
		float objectvolume = (placeditem.GetMass() * 453.592)
	
		if ((akDestContainer Is Actor == true) && (akDestContainer As Actor).HasPerk(PowerArmorPerk) == true)
			return
		endif
		
		itr = aiItemCount
		
		showedmessage = false
		
		if (placeditem.GetWidth() <= 0 && placeditem.GetWidth() <= 0 && placeditem.GetWidth() <= 0)
		
		else
			while (placeditem.GetWidth() * itr > akDestContainer.GetWidth() || placeditem.GetLength() * itr > akDestContainer.GetLength() || placeditem.GetHeight() * itr > akDestContainer.GetHeight())
				; the items won't fit!
				if (showedmessage == false)
					RenContainerWontFit.Show()
					showedmessage = true
				endif
				akDestContainer.DropObject(akBaseItem, 1)
				itr -= 1
			endWhile
			placeditem.Disable()
			placeditem.DeleteWhenAble()
			placeditem = none
		endif
	else
		return
	endif
endEvent