Scriptname RenContainer:RenContainerRefColScript extends RefCollectionAlias

Perk Property PowerArmorPerk Auto
MiscObject Property Caps001 Auto
Message Property RenContainerWontFit Auto
ActorValue Property CarryWeight Auto
Formlist Property RenContainerDroppedObjs Auto
Formlist Property RenContainerBaseForms Auto
Container Property RenContainerObj Auto
bool showedmessage = false

Message Property RenContainerSizeMsg Auto

Form[] baseobjects
Float[] baseobjectweights

ObjectReference workingref

float debugpoint = 0.0

ObjectReference Property PlayerRef Auto

Event OnAliasInit()
	AddInventoryEventFilter(None)
endEvent

Event OnItemAdded(ObjectReference akSenderRef, Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
	ObjectReference akDestContainer = akSenderRef
	
	debugpoint = 0.1	
	
	if (PlayerRef != Game.GetPlayer())
		PlayerRef = Game.GetPlayer()
	endif
	
	if (akSenderRef == workingref || akSenderRef.IsCreated() == true || akSourceContainer.IsCreated() == true || akSenderRef Is Actor == true || akSourceContainer == none)
		debugpoint = 0.11
		return
	endif
	
	if (akItemReference != none && akItemReference.IsQuestItem() == true)
		return
	endif
	
	workingref = akSenderRef
	akSenderRef.BlockActivation(true)
	
	ObjectReference tempcontainertwo	
	
	debugpoint = 0.2	
	
	float insidecontainervolume = 0.0	
	float containervolume = 0.0
	
	if (akDestContainer != none	&& akBaseItem != Caps001)
		;while (Utility.IsInMenuMode())
			; spin...
		;endWhile		
	
		;Utility.Wait(0.1)
	
		tempcontainertwo = PlayerRef.PlaceAtMe(RenContainerObj, 1, true, false, false)		
		
		while (tempcontainertwo == none)
			tempcontainertwo = PlayerRef.PlaceAtMe(RenContainerObj, 1, true, false, false)				
		endWhile
		
		while (tempcontainertwo.IsBoundGameObjectAvailable() == false)
			; Spin...
		endWhile
	
		tempcontainertwo.SetPosition(PlayerRef.GetPositionX(), PlayerRef.GetPositionY(), (PlayerRef.GetPositionZ() - 500))
		
		;baseobjects = new Form[128]
		;baseobjectweights = new Float[128]	
	
		containervolume = ((akDestContainer.GetWidth() * akDestContainer.GetLength() * akDestContainer.GetHeight()) * 0.75)
		; cm3
		
		float containerwidth = akDestContainer.GetWidth()
		float containerlength = akDestContainer.GetLength()
		float containerheight = akDestContainer.GetHeight()
		
		; 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()
	
		int itr = 1
		
		float baseitemweight = 0.0
		
		akDestContainer.RemoveAllItems(tempcontainertwo, true)
		
		debugpoint = 1.0		
		
		bool continuewhile = true
		
		while (insidecontainervolume <= containervolume && continuewhile == true && tempcontainertwo.GetItemCount() > 0)
			debugpoint = 1.1
			ObjectReference tempref = tempcontainertwo.DropFirstObject(false)
			while (tempref.IsBoundGameObjectAvailable() == false || tempref.Is3DLoaded() == false)
				; spin...
			endWhile
			float mass = tempref.GetMass() * 453.592
			
			Form curbaseobj = tempref.GetBaseObject()
			int itemcount = tempcontainertwo.GetItemCount(curbaseobj)
			
			;/
			debug.trace("Got Havok mass: " + mass, 0)
			debug.trace("Container volume: " + containervolume)
			debug.trace("Object length, width, height: " + tempref.GetLength() + " " + tempref.GetWidth() + " " + tempref.GetHeight())
			debug.notification("Mass: " + mass + ", " + "Object length, width, height: " + tempref.GetLength() + " " + tempref.GetWidth() + " " + tempref.GetHeight())
			/;
			
			float objlength = tempref.GetLength()
			float objwidth = tempref.GetWidth()
			float objheight = tempref.GetHeight()
			
			float objvolume = (objlength * objwidth * objheight) * 4
			
			float totalmass = (itemcount + 1) * mass
			float totalvolume = (itemcount + 1) * objvolume						
			
			int itemstoadd = 0
			
			if (tempref.IsQuestItem() == true)
				akDestContainer.AddItem(tempref)
			elseif (objvolume > containervolume || mass > containervolume)
				Utility.WaitMenuMode(0.1)				
				PlayerRef.AddItem(tempref)
				RenContainerWontFit.Show()				
				continuewhile = false
			else
				if (objlength > 0.0 && objwidth > 0.0 && objheight > 0.0)
					;if (objlength < containerlength && objwidth < containerwidth && objheight < containerheight)
						while ((insidecontainervolume + objvolume) < containervolume && tempcontainertwo.GetItemCount(curbaseobj) > itemstoadd)
							if (tempref != none)
								akDestContainer.AddItem(tempref, 1)		
								tempref = none
							else
								itemstoadd += 1							
							endif
							insidecontainervolume += objvolume	
						endWhile
						tempcontainertwo.RemoveItem(curbaseobj, itemstoadd, false, none)	
						akDestContainer.AddItem(curbaseobj, itemstoadd)
					;endif
				elseif (tempref.GetMass() > 0.0)
					if (mass <= 0.01)
						mass = 0.01
					endif
					
					while ((insidecontainervolume + mass) < containervolume && tempcontainertwo.GetItemCount(curbaseobj) > itemstoadd)
						if (tempref != none)
							akDestContainer.AddItem(tempref, 1)		
							tempref = none
						else
							itemstoadd += 1
						endif
						insidecontainervolume += mass							
					endWhile		
					tempcontainertwo.RemoveItem(curbaseobj, itemstoadd, false, none)	
					akDestContainer.AddItem(curbaseobj, itemstoadd)
				else
					; bad!
					PlayerRef.AddItem(tempref)
				endif
			endif			
			
			if (tempcontainertwo.GetItemCount(curbaseobj) > 0)
				; we filled up the container.
				; and there is no more room
				continuewhile = false
			endif
			
			tempref = none
		endWhile
		
		if (tempcontainertwo.GetItemCount() > 0)
			RenContainerWontFit.Show()		
		endif
	
		tempcontainertwo.RemoveAllItems(PlayerRef)	
		
		debugpoint = 2.0
	
		itr = 0	
	endif
	
	debugpoint = 5.0
	
	RenContainerSizeMsg.Show(((insidecontainervolume / containervolume) * 100))
	
	akSenderRef.BlockActivation(false)
	workingref = none	
	if (tempcontainertwo != none)
		tempcontainertwo.Disable()
		tempcontainertwo.DeleteWhenAble()			
	endif		
endEvent