scriptname _tckSortingContainerScript extends ObjectReference  

import debug
import utility

Keyword Property ClothingNecklace Auto
Keyword Property ArmorHelmet Auto
Keyword Property ArmorBoots Auto
Keyword Property ArmorGauntlets Auto
Keyword Property VendorItemGem Auto
Keyword Property ClothingCirclet Auto
Keyword Property ClothingRing Auto
Keyword Property ArmorJewelry Auto
Keyword Property WeapTypeDagger Auto
Keyword Property WeapTypeSword Auto
Keyword Property WeapTypeGreatsword Auto
Keyword Property WeapTypeWarAxe Auto
Keyword Property WeapTypeBattleaxe Auto
Keyword Property WeapTypeWarhammer Auto
Keyword Property WeapTypeStaff Auto
Keyword Property WeapTypeBow Auto
Keyword Property WeapTypeMace Auto
Keyword Property ArmorCuirass Auto
Keyword Property ArmorShield Auto
Keyword Property ClothingBody Auto
Keyword Property ClothingFeet Auto
Keyword Property ClothingHands Auto
Keyword Property ClothingHead Auto
Keyword Property VendorItemClutter Auto

Bool Property UseOverflowContainer Auto

;ObjectReference Property DaggerContainer Auto Hidden
;ObjectReference Property SwordContainer Auto Hidden

ObjectReference[] Property ContainerList Auto Hidden

ObjectReference Property OverflowContainer Auto Hidden

ObjectReference Property EmptyRef Auto Hidden
{Null Ref}

int Property NumberOfContainers Auto Hidden

Keyword Property LinkCustom01 Auto
Keyword Property LinkCustom02 Auto
Keyword Property LinkCustom03 Auto
Keyword Property LinkCustom04 Auto
Keyword Property LinkCustom05 Auto
Keyword Property LinkCustom06 Auto
Keyword Property LinkCustom07 Auto
Keyword Property LinkCustom08 Auto
Keyword Property LinkCustom09 Auto
Keyword Property LinkCustom10 Auto
Keyword Property _tckOverflowContainer Auto

Bool Property AlreadyLoaded = FALSE Auto Hidden

bool Property BusyAdding = False Auto Hidden

int Property WaitList = 0 Auto Hidden

EVENT OnCellLoad()
	if AlreadyLoaded == FALSE
		ContainerList = new ObjectReference[10]
		
		ContainerList[0] = GetLinkedRef(LinkCustom01)
		ContainerList[1] = GetLinkedRef(LinkCustom02)
		ContainerList[2] = GetLinkedRef(LinkCustom03)
		ContainerList[3] = GetLinkedRef(LinkCustom04)
		ContainerList[4] = GetLinkedRef(LinkCustom05)
		ContainerList[5] = GetLinkedRef(LinkCustom06)
		ContainerList[6] = GetLinkedRef(LinkCustom07)
		ContainerList[7] = GetLinkedRef(LinkCustom08)
		ContainerList[8] = GetLinkedRef(LinkCustom09)
		ContainerList[9] = GetLinkedRef(LinkCustom10)
		
		OverflowContainer = GetLinkedRef(_tckOverflowContainer)
		
		int index = 0
		bool stop = false
		while !stop && (index < 10)
			if (ContainerList[index] == EmptyRef)
				stop = true
			else
				index += 1
			endif
		endwhile
		NumberOfContainers = index

		AlreadyLoaded = TRUE
	endif
endEVENT

EVENT OnClose(ObjectReference akActionRef)
	while WaitList > 0
		Utility.wait(1.0)
	endwhile
	
	int index = 0
	while (index < NumberOfContainers)
		if ContainerList[index] != EmptyRef
			ContainerList[index].Activate(Self)
		endif
		index += 1
	endWhile
endEVENT

Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
	
	if (akBaseItem as Potion)
		AddObject(akBaseItem, aiItemCount, 2)
		self.RemoveItem(akBaseItem, aiItemCount, true)
	elseif (akBaseItem as Book)
		AddObject(akBaseItem, aiItemCount, 3)
		self.RemoveItem(akBaseItem, aiItemCount, true)
	elseif (akBaseItem as SoulGem)
		AddObject(akBaseItem, aiItemCount, 4)
		self.RemoveItem(akBaseItem, aiItemCount, true)
	elseif (akBaseItem as Ingredient)
		AddObject(akBaseItem, aiItemCount, 10)
		self.RemoveItem(akBaseItem, aiItemCount, true)
	elseif akBaseItem.HasKeyword(VendorItemGem)
		AddObject(akBaseItem, aiItemCount, 8)
		self.RemoveItem(akBaseItem, aiItemCount, true)
	elseif (akBaseItem as MiscObject) && akBaseItem.HasKeyword(VendorItemClutter)
		AddObject(akBaseItem, aiItemCount, 22)
		self.RemoveItem(akBaseItem, aiItemCount, true)
	elseif (akBaseItem as Weapon)
		if akBaseItem.HasKeyword(WeapTypeDagger)
			AddObject(akBaseItem, aiItemCount, 11)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeSword)
			AddObject(akBaseItem, aiItemCount, 12)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeWarhammer)
			AddObject(akBaseItem, aiItemCount, 13)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeBattleaxe)
			AddObject(akBaseItem, aiItemCount, 14)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeGreatsword)
			AddObject(akBaseItem, aiItemCount, 15)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeWarAxe)
			AddObject(akBaseItem, aiItemCount, 16)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeMace)
			AddObject(akBaseItem, aiItemCount, 17)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeBow)
			AddObject(akBaseItem, aiItemCount, 18)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(WeapTypeStaff)
			AddObject(akBaseItem, aiItemCount, 19)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		else
			self.RemoveItem(akBaseItem, aiItemCount, true, Game.GetPlayer())
		endif
	elseif (akBaseItem as Armor)
		if akBaseItem.HasKeyword(ArmorJewelry) && !akBaseItem.HasKeyWord(ClothingCirclet) && !akBaseItem.HasKeyWord(ClothingRing)
			AddObject(akBaseItem, aiItemCount, 11)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif (akBaseItem.HasKeyword(ArmorHelmet) || akBaseItem.HasKeyword(ClothingHead))
			AddObject(akBaseItem, aiItemCount, 5)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif (akBaseItem.HasKeyword(ArmorBoots) || akBaseItem.HasKeyword(ClothingFeet))
			AddObject(akBaseItem, aiItemCount, 6)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif (akBaseItem.HasKeyword(ArmorGauntlets) || akBaseItem.HasKeyword(ClothingHands))
			AddObject(akBaseItem, aiItemCount, 7)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(ClothingCirclet)
			AddObject(akBaseItem, aiItemCount, 9)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif (akBaseItem.HasKeyword(ArmorCuirass) || akBaseItem.HasKeyword(ClothingBody))
			AddObject(akBaseItem, aiItemCount, 20)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		elseif akBaseItem.HasKeyword(ArmorShield)
			AddObject(akBaseItem, aiItemCount, 21)
			self.RemoveItem(akBaseItem, aiItemCount, true)
		else
			self.RemoveItem(akBaseItem, aiItemCount, true, Game.GetPlayer())
		endif
	else
		self.RemoveItem(akBaseItem, aiItemCount, true, Game.GetPlayer())
	endif
	
endEvent

Function AddObject(Form ObjectBase, Int ObjectAmount, int ObjectType)
	int index
	bool keepgoing = true
	WaitList += 1
	
	while BusyAdding
		Utility.wait(0.5)
	endwhile
	BusyAdding = true
	
	while ObjectAmount > 0 && keepgoing
		index = 0
		while (index < NumberOfContainers)
			if (ContainerList[index] as _tckObjectContainerScript).GetObjectType() == ObjectType && (ContainerList[index] as _tckObjectContainerScript).GetSpaceLeft() > 0
				ContainerList[index].AddItem(ObjectBase, 1, true)
				ObjectAmount -= 1
				index = NumberOfContainers
				Utility.wait(0.05)
			else
				index += 1
				if index == NumberOfContainers
					keepgoing = false
				endif	
			endif
		endwhile
	endwhile
	
	if ObjectAmount > 0
		If UseOverflowContainer
			OverflowContainer.AddItem(ObjectBase, ObjectAmount, true)
		else
			Game.GetPlayer().AddItem(ObjectBase, ObjectAmount, true)
		endif
	endif
	
	BusyAdding = false
	WaitList -= 1

endFunction