scriptname _tckObjectContainerScript extends ObjectReference  

import debug
import utility

Keyword Property _tckObject01 Auto
Keyword Property _tckObject02 Auto
Keyword Property _tckObject03 Auto
Keyword Property _tckObject04 Auto
Keyword Property _tckObject05 Auto
Keyword Property _tckObject06 Auto
Keyword Property _tckObject07 Auto
Keyword Property _tckObject08 Auto
Keyword Property _tckObject09 Auto
Keyword Property _tckObject10 Auto
Keyword Property _tckObject11 Auto
Keyword Property _tckObject12 Auto
Keyword Property _tckObject13 Auto
Keyword Property _tckObject14 Auto
Keyword Property _tckObject15 Auto
Keyword Property _tckObject16 Auto
Keyword Property _tckObject17 Auto
Keyword Property _tckObject18 Auto
Keyword Property _tckObjectContainerTrigger01 Auto
Keyword Property _tckObjectContainerTrigger02 Auto
Keyword Property _tckObjectContainerTrigger03 Auto
Keyword Property _tckObjectContainerTrigger04 Auto
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
{List of required Keywords}

GlobalVariable Property ContainerObjectType Auto
{Type of object being managed}

Bool Property SetBlockActivation Auto

Bool Property SetKeyframedMotion Auto

ObjectReference Property ObjectMarker01 Auto Hidden
ObjectReference Property ObjectMarker02 Auto Hidden
ObjectReference Property ObjectMarker03 Auto Hidden
ObjectReference Property ObjectMarker04 Auto Hidden
ObjectReference Property ObjectMarker05 Auto Hidden
ObjectReference Property ObjectMarker06 Auto Hidden
ObjectReference Property ObjectMarker07 Auto Hidden
ObjectReference Property ObjectMarker08 Auto Hidden
ObjectReference Property ObjectMarker09 Auto Hidden
ObjectReference Property ObjectMarker10 Auto Hidden
ObjectReference Property ObjectMarker11 Auto Hidden
ObjectReference Property ObjectMarker12 Auto Hidden
ObjectReference Property ObjectMarker13 Auto Hidden
ObjectReference Property ObjectMarker14 Auto Hidden
ObjectReference Property ObjectMarker15 Auto Hidden
ObjectReference Property ObjectMarker16 Auto Hidden
ObjectReference Property ObjectMarker17 Auto Hidden
ObjectReference Property ObjectMarker18 Auto Hidden
ObjectReference Property ObjectCaseTrigger01Ref Auto Hidden
ObjectReference Property ObjectCaseTrigger02Ref Auto Hidden
ObjectReference Property ObjectCaseTrigger03Ref Auto Hidden
ObjectReference Property ObjectCaseTrigger04Ref Auto Hidden

int Property ObjectType Auto Hidden

Int Property MaxObjectsAllowed Auto Hidden
{Max objects allowed in this partciular case}

Int Property CurrentObjectAmount Auto Hidden
{The current number of Objects placed in the case}

Form Property EmptyForm Auto Hidden
{Null Form}

ObjectReference Property EmptyRef Auto Hidden
{Null Ref}

Form Property CurrentObjectForm Auto Hidden
{Object Form we are working with at any one time}

ObjectReference Property CurrentObjectRef Auto Hidden
{Object Ref we are working with at any one time}

Form Property PlacedObject01 Auto Hidden
Form Property PlacedObject02 Auto Hidden
Form Property PlacedObject03 Auto Hidden
Form Property PlacedObject04 Auto Hidden
Form Property PlacedObject05 Auto Hidden
Form Property PlacedObject06 Auto Hidden
Form Property PlacedObject07 Auto Hidden
Form Property PlacedObject08 Auto Hidden
Form Property PlacedObject09 Auto Hidden
Form Property PlacedObject10 Auto Hidden
Form Property PlacedObject11 Auto Hidden
Form Property PlacedObject12 Auto Hidden
Form Property PlacedObject13 Auto Hidden
Form Property PlacedObject14 Auto Hidden
Form Property PlacedObject15 Auto Hidden
Form Property PlacedObject17 Auto Hidden
Form Property PlacedObject16 Auto Hidden
Form Property PlacedObject18 Auto Hidden
{List of Placed object Forms}

ObjectReference Property PlacedObject01Ref Auto Hidden
ObjectReference Property PlacedObject02Ref Auto Hidden
ObjectReference Property PlacedObject03Ref Auto Hidden
ObjectReference Property PlacedObject04Ref Auto Hidden
ObjectReference Property PlacedObject05Ref Auto Hidden
ObjectReference Property PlacedObject06Ref Auto Hidden
ObjectReference Property PlacedObject07Ref Auto Hidden
ObjectReference Property PlacedObject08Ref Auto Hidden
ObjectReference Property PlacedObject09Ref Auto Hidden
ObjectReference Property PlacedObject10Ref Auto Hidden
ObjectReference Property PlacedObject11Ref Auto Hidden
ObjectReference Property PlacedObject12Ref Auto Hidden
ObjectReference Property PlacedObject13Ref Auto Hidden
ObjectReference Property PlacedObject14Ref Auto Hidden
ObjectReference Property PlacedObject15Ref Auto Hidden
ObjectReference Property PlacedObject17Ref Auto Hidden
ObjectReference Property PlacedObject16Ref Auto Hidden
ObjectReference Property PlacedObject18Ref Auto Hidden
{List of Placed object Refs}

Bool Property AlreadyLoaded = FALSE Auto Hidden
{Whether this scritp has already went through it's OnCellLoad() Event}

Bool Property BlockObjects = FALSE Auto Hidden
{Used for when you can't place any more objects}

Message Property _tckNoMoreRoomMsg Auto
{Displayed message for when the amount of objects the player is placing excedes the case limit.}

Message Property _tckWrongObjectMsg Auto
{Message displayed when the player places a non target object form in the container.}

Message Property _tckRoomLeftMsg Auto
{Notification that tells the player how much room is left in the case upon first activating it.}

EVENT OnCellLoad()
	if AlreadyLoaded == FALSE
		; Get all the object markers
		ObjectMarker01 = GetLinkedRef(_tckObject01)
		ObjectMarker02 = GetLinkedRef(_tckObject02)
		ObjectMarker03 = GetLinkedRef(_tckObject03)
		ObjectMarker04 = GetLinkedRef(_tckObject04)
		ObjectMarker05 = GetLinkedRef(_tckObject05)
		ObjectMarker06 = GetLinkedRef(_tckObject06)
		ObjectMarker07 = GetLinkedRef(_tckObject07)
		ObjectMarker08 = GetLinkedRef(_tckObject08)
		ObjectMarker09 = GetLinkedRef(_tckObject09)
		ObjectMarker10 = GetLinkedRef(_tckObject10)
		ObjectMarker11 = GetLinkedRef(_tckObject11)
		ObjectMarker12 = GetLinkedRef(_tckObject12)
		ObjectMarker13 = GetLinkedRef(_tckObject13)
		ObjectMarker14 = GetLinkedRef(_tckObject14)
		ObjectMarker15 = GetLinkedRef(_tckObject15)
		ObjectMarker16 = GetLinkedRef(_tckObject16)
		ObjectMarker17 = GetLinkedRef(_tckObject17)
		ObjectMarker18 = GetLinkedRef(_tckObject18)
		if !SetBlockActivation
			ObjectCaseTrigger01Ref = (GetLinkedRef(_tckObjectContainerTrigger01) as _tckObjectContainerTriggerScript)
			ObjectCaseTrigger02Ref = (GetLinkedRef(_tckObjectContainerTrigger02) as _tckObjectContainerTriggerScript)
			ObjectCaseTrigger03Ref = (GetLinkedRef(_tckObjectContainerTrigger03) as _tckObjectContainerTriggerScript)
			ObjectCaseTrigger04Ref = (GetLinkedRef(_tckObjectContainerTrigger04) as _tckObjectContainerTriggerScript)
		endif
		
		; Count how many objects can be placed in this case
		CountMaxObjects()
		
		ObjectType = ContainerObjectType.GetValueInt()

		AlreadyLoaded = TRUE
	endif
endEVENT



EVENT OnActivate(ObjectReference akActionRef)
	if akActionRef == Game.GetPlayer()
		_tckRoomLeftMsg.Show((MaxObjectsAllowed - CurrentObjectAmount))
	endIf
	
	;Block activation
	if !SetBlockActivation
		BlockActivate()
	endif

	if !SetBlockActivation
		if ObjectCaseTrigger01Ref
			ObjectCaseTrigger01Ref.GoToState("IgnoreObjects")
		endif
		if ObjectCaseTrigger02Ref
			ObjectCaseTrigger02Ref.GoToState("IgnoreObjects")
		endif
		if ObjectCaseTrigger03Ref
			ObjectCaseTrigger03Ref.GoToState("IgnoreObjects")
		endif
		if ObjectCaseTrigger04Ref
			ObjectCaseTrigger04Ref.GoToState("IgnoreObjects")
		endif
	endif

	Wait(0.25)
	; The following will fire when the player leaves inventory
	UpdateObjects()

	if !SetBlockActivation
		if ObjectCaseTrigger01Ref
			ObjectCaseTrigger01Ref.GoToState("WaitForObjects")
		endif
		if ObjectCaseTrigger02Ref
			ObjectCaseTrigger02Ref.GoToState("WaitForObjects")
		endif
		if ObjectCaseTrigger03Ref
			ObjectCaseTrigger03Ref.GoToState("WaitForObjects")
		endif
		if ObjectCaseTrigger04Ref
			ObjectCaseTrigger04Ref.GoToState("WaitForObjects")
		endif
	endif

endEVENT


Event OnItemRemoved(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akDestContainer)
	if TestObject(akBaseItem)
		if BlockObjects == FALSE
			; If the item is correct object type find the corresponding reference and remove it.
			RemoveObject(akBaseItem, aiItemCount)
			CurrentObjectAmount = CurrentObjectAmount - aiItemCount
		else
			BlockObjects = FALSE
		endif
	endif
endEvent


Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
	if TestObject(akBaseItem)
		; If the item being added is the correct type check to see if there is room in the case.
		if ((aiItemCount + CurrentObjectAmount) <= MaxObjectsAllowed)
			; There's room on the at the inn, manage the placement
			AddObject(akBaseItem, aiItemCount)
			CurrentObjectAmount = CurrentObjectAmount + aiItemCount
		else
			; There is no room left at the inn.  Tell the player and give the object back.
			utility.waitMenuMode(0)
			_tckNoMoreRoomMsg.Show()
			BlockObjects = TRUE
			self.RemoveItem(akBaseItem, aiItemCount, true, Game.GetPlayer())
		endif
	else
		; The item placed in the container isn't the right type, so give the player it back and tell him the type of object allowed.
		self.RemoveItem(akBaseItem, aiItemCount, true, Game.GetPlayer())
		utility.WaitMenuMode(0)
		_tckWrongObjectMsg.Show()
	endif
	
endEvent

int Function GetSpaceLeft()
	return MaxObjectsAllowed - CurrentObjectAmount
endFunction

int Function GetObjectType()
	return ObjectType
endFunction

Bool Function TestObject(Form akBaseItem)
	; necklaces
	if (ObjectType == 1)
		return (akBaseItem as Armor) && akBaseItem.HasKeyword(ArmorJewelry) && !akBaseItem.HasKeyWord(ClothingCirclet) && !akBaseItem.HasKeyWord(ClothingRing)
	; potions
	elseif (ObjectType == 2)
		return(akBaseItem as Potion)
	; books	
	elseif (ObjectType == 3)
		return (akBaseItem as Book)
	; soul gems	
	elseif (ObjectType == 4)
		return (akBaseItem as SoulGem)
	; helmets	
	elseif (ObjectType == 5)
		return(akBaseItem as Armor) && (akBaseItem.HasKeyword(ArmorHelmet) || akBaseItem.HasKeyword(ClothingHead))
	; boots	
	elseif (ObjectType == 6)
		return(akBaseItem as Armor) && (akBaseItem.HasKeyword(ArmorBoots) || akBaseItem.HasKeyword(ClothingFeet))
	; gauntlets	
	elseif (ObjectType == 7)
		return(akBaseItem as Armor) && (akBaseItem.HasKeyword(ArmorGauntlets) || akBaseItem.HasKeyword(ClothingHands))
	; gems	
	elseif (ObjectType == 8)
		return akBaseItem.HasKeyword(VendorItemGem)
	; circlet	
	elseif (ObjectType == 9)
		return(akBaseItem as Armor) && akBaseItem.HasKeyWord(ClothingCirclet)
	; ingredients	
	elseif (ObjectType == 10)
		return(akBaseItem as Ingredient)
	; dagger	
	elseif (ObjectType == 11)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeDagger)
	; sword	
	elseif (ObjectType == 12)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeSword)
		; war hammer	
	elseif (ObjectType == 13)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeWarhammer)
		; battle axe
	elseif (ObjectType == 14)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeBattleaxe)
		; great sword
	elseif (ObjectType == 15)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeGreatsword)
		; war axe
	elseif (ObjectType == 16)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeWarAxe)
		; mace
	elseif (ObjectType == 17)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeMace)
		; bow
	elseif (ObjectType == 18)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeBow)
		; staff
	elseif (ObjectType == 19)
		return(akBaseItem as Weapon) && akBaseItem.HasKeyword(WeapTypeStaff)
		; cuirass
	elseif (ObjectType == 20)
		return(akBaseItem as Armor) && (akBaseItem.HasKeyword(ArmorCuirass) || akBaseItem.HasKeyword(ClothingBody))
		; shield
	elseif (ObjectType == 21)
		return(akBaseItem as Armor) && akBaseItem.HasKeyword(ArmorShield)
			; clutter (like dragon claws)
	elseif (ObjectType == 22)
		return(akBaseItem as MiscObject) && akBaseItem.HasKeyword(VendorItemClutter)
	else
		return (false)
	endIf
endFunction

Function BlockActivate()
	if PlacedObject01Ref
		PlacedObject01Ref.BlockActivation(TRUE)
	endif
	if PlacedObject02Ref
		PlacedObject02Ref.BlockActivation(TRUE)
	endif
	if PlacedObject03Ref
		PlacedObject03Ref.BlockActivation(TRUE)
	endif
	if PlacedObject04Ref
		PlacedObject04Ref.BlockActivation(TRUE)
	endif
	if PlacedObject05Ref
		PlacedObject05Ref.BlockActivation(TRUE)
	endif
	if PlacedObject06Ref
		PlacedObject06Ref.BlockActivation(TRUE)
	endif
	if PlacedObject07Ref
		PlacedObject07Ref.BlockActivation(TRUE)
	endif
	if PlacedObject08Ref
		PlacedObject08Ref.BlockActivation(TRUE)
	endif
	if PlacedObject09Ref
		PlacedObject09Ref.BlockActivation(TRUE)
	endif
	if PlacedObject10Ref
		PlacedObject10Ref.BlockActivation(TRUE)
	endif
	if PlacedObject11Ref
		PlacedObject11Ref.BlockActivation(TRUE)
	endif
	if PlacedObject12Ref
		PlacedObject12Ref.BlockActivation(TRUE)
	endif
	if PlacedObject13Ref
		PlacedObject13Ref.BlockActivation(TRUE)
	endif
	if PlacedObject14Ref
		PlacedObject14Ref.BlockActivation(TRUE)
	endif
	if PlacedObject15Ref
		PlacedObject15Ref.BlockActivation(TRUE)
	endif
	if PlacedObject16Ref
		PlacedObject16Ref.BlockActivation(TRUE)
	endif
	if PlacedObject17Ref
		PlacedObject17Ref.BlockActivation(TRUE)
	endif
	if PlacedObject18Ref
		PlacedObject18Ref.BlockActivation(TRUE)
	endif
endFunction

Function UnBlockActivate()
	if PlacedObject01Ref
		PlacedObject01Ref.BlockActivation(FALSE)
	endif
	if PlacedObject02Ref
		PlacedObject02Ref.BlockActivation(FALSE)
	endif
	if PlacedObject03Ref
		PlacedObject03Ref.BlockActivation(FALSE)
	endif
	if PlacedObject04Ref
		PlacedObject04Ref.BlockActivation(FALSE)
	endif
	if PlacedObject05Ref
		PlacedObject05Ref.BlockActivation(FALSE)
	endif
	if PlacedObject06Ref
		PlacedObject06Ref.BlockActivation(FALSE)
	endif
	if PlacedObject07Ref
		PlacedObject07Ref.BlockActivation(FALSE)
	endif
	if PlacedObject08Ref
		PlacedObject08Ref.BlockActivation(FALSE)
	endif
	if PlacedObject09Ref
		PlacedObject09Ref.BlockActivation(FALSE)
	endif
	if PlacedObject10Ref
		PlacedObject10Ref.BlockActivation(FALSE)
	endif
	if PlacedObject11Ref
		PlacedObject11Ref.BlockActivation(FALSE)
	endif
	if PlacedObject12Ref
		PlacedObject12Ref.BlockActivation(FALSE)
	endif
	if PlacedObject13Ref
		PlacedObject13Ref.BlockActivation(FALSE)
	endif
	if PlacedObject14Ref
		PlacedObject14Ref.BlockActivation(FALSE)
	endif
	if PlacedObject15Ref
		PlacedObject15Ref.BlockActivation(FALSE)
	endif
	if PlacedObject16Ref
		PlacedObject16Ref.BlockActivation(FALSE)
	endif
	if PlacedObject17Ref
		PlacedObject17Ref.BlockActivation(FALSE)
	endif
	if PlacedObject18Ref
		PlacedObject18Ref.BlockActivation(FALSE)
	endif
endFunction


Function RemoveObject(Form ObjectBase, Int ObjectAmount)
	; Find a specific object form and remove it
	While ObjectAmount > 0
		if PlacedObject01 == ObjectBase
			PlacedObject01 = EmptyForm
		elseif PlacedObject02 == ObjectBase
			PlacedObject02 = EmptyForm
		elseif PlacedObject03 == ObjectBase
			PlacedObject03 = EmptyForm
		elseif PlacedObject04 == ObjectBase
			PlacedObject04 = EmptyForm
		elseif PlacedObject05 == ObjectBase
			PlacedObject05 = EmptyForm
		elseif PlacedObject06 == ObjectBase
			PlacedObject06 = EmptyForm
		elseif PlacedObject07 == ObjectBase
			PlacedObject07 = EmptyForm
		elseif PlacedObject08 == ObjectBase
			PlacedObject08 = EmptyForm
		elseif PlacedObject09 == ObjectBase
			PlacedObject09 = EmptyForm
		elseif PlacedObject10 == ObjectBase
			PlacedObject10 = EmptyForm
		elseif PlacedObject11 == ObjectBase
			PlacedObject11 = EmptyForm
		elseif PlacedObject12 == ObjectBase
			PlacedObject12 = EmptyForm
		elseif PlacedObject13 == ObjectBase
			PlacedObject13 = EmptyForm
		elseif PlacedObject14 == ObjectBase
			PlacedObject14 = EmptyForm
		elseif PlacedObject15 == ObjectBase
			PlacedObject15 = EmptyForm
		elseif PlacedObject16 == ObjectBase
			PlacedObject16 = EmptyForm
		elseif PlacedObject17 == ObjectBase
			PlacedObject17 = EmptyForm
		elseif PlacedObject18 == ObjectBase
			PlacedObject18 = EmptyForm
		endif
			ObjectAmount = ObjectAmount - 1
	endWhile
endFunction

Function AddObject(Form ObjectBase, Int ObjectAmount)
	; Find an empty object form and place the new object there
	While ObjectAmount > 0
		if PlacedObject01 == EmptyForm
			PlacedObject01 = ObjectBase
		elseif PlacedObject02 == EmptyForm
			PlacedObject02 = ObjectBase
		elseif PlacedObject03 == EmptyForm
			PlacedObject03 = ObjectBase
		elseif PlacedObject04 == EmptyForm
			PlacedObject04 = ObjectBase
		elseif PlacedObject05 == EmptyForm
			PlacedObject05 = ObjectBase
		elseif PlacedObject06 == EmptyForm
			PlacedObject06 = ObjectBase
		elseif PlacedObject07 == EmptyForm
			PlacedObject07 = ObjectBase
		elseif PlacedObject08 == EmptyForm
			PlacedObject08 = ObjectBase
		elseif PlacedObject09 == EmptyForm
			PlacedObject09 = ObjectBase
		elseif PlacedObject10 == EmptyForm
			PlacedObject10 = ObjectBase
		elseif PlacedObject11 == EmptyForm
			PlacedObject11 = ObjectBase
		elseif PlacedObject12 == EmptyForm
			PlacedObject12 = ObjectBase
		elseif PlacedObject13 == EmptyForm
			PlacedObject13 = ObjectBase
		elseif PlacedObject14 == EmptyForm
			PlacedObject14 = ObjectBase
		elseif PlacedObject15 == EmptyForm
			PlacedObject15 = ObjectBase
		elseif PlacedObject16 == EmptyForm
			PlacedObject16 = ObjectBase
		elseif PlacedObject17 == EmptyForm
			PlacedObject17 = ObjectBase
		elseif PlacedObject18 == EmptyForm
			PlacedObject18 = ObjectBase
		endif
			ObjectAmount = ObjectAmount - 1
	endWhile
endFunction


Function CountMaxObjects()
	; Checks how many Objects can be placed on this shelf
	if ObjectMarker01 == EmptyRef
		MaxObjectsAllowed = 0
	elseif ObjectMarker02 == EmptyRef
		MaxObjectsAllowed = 1
	elseif ObjectMarker03 == EmptyRef
		MaxObjectsAllowed = 2
	elseif ObjectMarker04 == EmptyRef
		MaxObjectsAllowed = 3
	elseif ObjectMarker05 == EmptyRef
		MaxObjectsAllowed = 4
	elseif ObjectMarker06 == EmptyRef
		MaxObjectsAllowed = 5
	elseif ObjectMarker07 == EmptyRef
		MaxObjectsAllowed = 6
	elseif ObjectMarker08 == EmptyRef
		MaxObjectsAllowed = 7
	elseif ObjectMarker09 == EmptyRef
		MaxObjectsAllowed = 8
	elseif ObjectMarker10 == EmptyRef
		MaxObjectsAllowed = 9
	elseif ObjectMarker11 == EmptyRef
		MaxObjectsAllowed = 10
	elseif ObjectMarker12 == EmptyRef
		MaxObjectsAllowed = 11
	elseif ObjectMarker13 == EmptyRef
		MaxObjectsAllowed = 12
	elseif ObjectMarker14 == EmptyRef
		MaxObjectsAllowed = 13
	elseif ObjectMarker15 == EmptyRef
		MaxObjectsAllowed = 14
	elseif ObjectMarker16 == EmptyRef
		MaxObjectsAllowed = 15
	elseif ObjectMarker17 == EmptyRef
		MaxObjectsAllowed = 16
	elseif ObjectMarker18 == EmptyRef
		MaxObjectsAllowed = 17
	elseif ObjectMarker18
		MaxObjectsAllowed = 18
	endif
endFunction


ObjectReference Function UpdateSingleObject(Form TargetObject, ObjectReference PlacedObjectRef, ObjectReference TargetMarker)
	ObjectReference retVal ; We return the object we placed (or None)
	if PlacedObjectRef
		PlacedObjectRef.Disable()
		PlacedObjectRef.Delete()
	endIf
	if TargetObject
		retVal = TargetMarker.PlaceAtMe(TargetObject)
		retVal.BlockActivation()
		if SetKeyframedMotion
			int Count = 0
			While(!retVal.Is3DLoaded()) && (Count < 20)
				; Have to wait to make sure the item is dropped before setting it's motion type, or else I get a "Object has no 3D" error.
				Utility.Wait(0.05)
				Count += 1
			EndWhile
			retVal.SetMotionType(Motion_Keyframed, false)
		endif
	endIf
	return retVal
EndFunction


Function UpdateObjects()
	GoToState("PlacingObjects")
	
	PlacedObject01Ref = UpdateSingleObject(PlacedObject01, PlacedObject01Ref, ObjectMarker01)
	PlacedObject02Ref = UpdateSingleObject(PlacedObject02, PlacedObject02Ref, ObjectMarker02)
	PlacedObject03Ref = UpdateSingleObject(PlacedObject03, PlacedObject03Ref, ObjectMarker03)
	PlacedObject04Ref = UpdateSingleObject(PlacedObject04, PlacedObject04Ref, ObjectMarker04)
	PlacedObject05Ref = UpdateSingleObject(PlacedObject05, PlacedObject05Ref, ObjectMarker05)
	
	PlacedObject06Ref = UpdateSingleObject(PlacedObject06, PlacedObject06Ref, ObjectMarker06)
	PlacedObject07Ref = UpdateSingleObject(PlacedObject07, PlacedObject07Ref, ObjectMarker07)
	PlacedObject08Ref = UpdateSingleObject(PlacedObject08, PlacedObject08Ref, ObjectMarker08)
	PlacedObject09Ref = UpdateSingleObject(PlacedObject09, PlacedObject09Ref, ObjectMarker09)
	PlacedObject10Ref = UpdateSingleObject(PlacedObject10, PlacedObject10Ref, ObjectMarker10)
	
	PlacedObject11Ref = UpdateSingleObject(PlacedObject11, PlacedObject11Ref, ObjectMarker11)
	PlacedObject12Ref = UpdateSingleObject(PlacedObject12, PlacedObject12Ref, ObjectMarker12)
	PlacedObject13Ref = UpdateSingleObject(PlacedObject13, PlacedObject13Ref, ObjectMarker13)
	PlacedObject14Ref = UpdateSingleObject(PlacedObject14, PlacedObject14Ref, ObjectMarker14)
	PlacedObject15Ref = UpdateSingleObject(PlacedObject15, PlacedObject15Ref, ObjectMarker15)
	
	PlacedObject16Ref = UpdateSingleObject(PlacedObject16, PlacedObject16Ref, ObjectMarker16)
	PlacedObject17Ref = UpdateSingleObject(PlacedObject17, PlacedObject17Ref, ObjectMarker17)
	PlacedObject18Ref = UpdateSingleObject(PlacedObject18, PlacedObject18Ref, ObjectMarker18)
	
	if !SetBlockActivation
		UnBlockActivate() ; Allow the player to mess with them
	endIf
	GoToState("") ; Now allow objects to be updated again
EndFunction

State PlacingObjects
	Function UpdateObjects()
		; Already updating objects, so ignore
	EndFunction
EndState
	
