Scriptname wizBarrelMarkerScript extends ObjectReference  

Container Property wizBarrelStaticFood Auto
Container Property wizBarrelStaticIngredientCommon Auto
Container Property wizBarrelStaticIngredientUncommon Auto
Container Property wizBarrelStaticBlackBriarMead Auto
Container Property wizBarrelStaticHonningbrewMead Auto
Container Property wizBarrelStaticMead Auto
Container Property wizBarrelStaticFish Auto
Container Property wizBarrelStaticMeat Auto
FormList Property wizParentIssues Auto
FormList Property wizBarrelList Auto
FormList Property wizAlchemyTables Auto
FormList Property wizBreweryCasks Auto
FormList Property wizCookingPotList Auto
GlobalVariable Property wizDynamicThingsEnabled Auto 
GlobalVariable Property wizOptionBarrelsEnabled Auto 
GlobalVariable Property wizOptionDTResetTime Auto 
Actor Property ArngeirREF Auto
wizDTCompatibility Property Compatibility Auto

ObjectReference ClosestBarrel
ObjectReference BarrelContainer
Bool ParentIssues
Float Respawn

Event OnInit()
	;debug.messagebox(WoodPileContainer)
	if BarrelContainer == None ;Only fire once
		PlaceBarrel()
		Respawn = utility.getCurrentGameTime()
	endIf

endEvent


Event OnCellDetach()
	;Re-enable static wood pile - this is mainly for unistallation puposes
	if ParentIssues != TRUE
		ClosestBarrel.enable()
	endIf

	if BarrelContainer
		;Disable wood pile container
		BarrelContainer.disable()
	else
		self.disable()
		self.delete()
	endIf

	Respawn = utility.getCurrentGameTime()

endEvent

Event OnCellAttach()
	if wizOptionBarrelsEnabled.getvalueint() == 1 && wizDynamicThingsEnabled.getvalueint() == 1

		;Need to check if the container still exists. If not, it has reset manually.

		;Find the curent closest barrel  - may be dufferent if Placeable Statics has been removed!
		ClosestBarrel= Game.FindClosestReferenceOfAnyTypeInListFromRef(wizBarrelList, self, 15)
		if ClosestBarrel == None  || ClosestBarrel.isDeleted() || ClosestBarrel.isDisabled()
			;Barrel moved by a mod that moved a vanilla object
			BarrelContainer.disable()
			BarrelContainer.delete()
			self.disable()
			self.Delete()
		else

			;Might this container be respawning?
			if utility.getCurrentGameTime() >= (Respawn + (wizOptionDTResetTime.getvalue() as float))
				;We are going to loop for 3 seconds to see if BarrelContainer gets deleted!
				int iLoop = 6
				While iLoop
					;Is the container deleted?
					if BarrelContainer.isDeleted()
;						debug.messagebox("BarrelContainer Deleted!")
						PlaceBarrel()
						iLoop = 0
					else
						Utility.wait(0.5)
						iLoop = (iLoop - 1)
					endIf
				endWhile
				
			endIf


			if ParentIssues != TRUE && !wizParentIssues.HasForm(ClosestBarrel)
				ClosestBarrel.disable()

				if !ClosestBarrel.IsDisabled()
					;This catches parent problems and add it to an ignore list to prevent
					debug.trace ("********** wizDYNAMIC THINGS - The Enable State Parent error above has been accounted for and will not occur again for this object **********")
					ParentIssues = TRUE
					;Add to parent issues for logging!
					if !(wizParentIssues.HasForm(ClosestBarrel)) 
						wizParentIssues.AddForm(ClosestBarrel)
					endIf
;					debug.messagebox(ClosestBarrel)
				endIf
			endIf

			if BarrelContainer
				BarrelContainer.enable()
				if  self.getcurrentlocation() && self.getcurrentlocation().haskeyword(Keyword.getkeyword("LocTypeHabitation"))
					BarrelContainer.SetActorOwner(ArngeirREF.GetActorBase())
				endIf
			else
				self.disable()
				self.delete()
			endIf
		endIf

	EndIf
EndEvent

Function PlaceBarrel()
		;Which barrel are we replacing?
		ClosestBarrel = Game.FindClosestReferenceOfAnyTypeInListFromRef(wizBarrelList, self, 15)
		
		if !ClosestBarrel 
			;How do we exist then?
			self.disable()
			self.delete()
			return
		endIf

		if Game.FindClosestReferenceOfAnyTypeInListFromRef(wizBreweryCasks, self, 500) != None
			;Brewery Casks
			int Mead = Utility.RandomInt(1,100)
			if Mead <= 25
				BarrelContainer = self.placeatme(wizBarrelStaticBlackBriarMead)
			elseIf Mead <= 50
				BarrelContainer = self.placeatme(wizBarrelStaticHonningbrewMead)
			Else
				BarrelContainer = self.placeatme(wizBarrelStaticMead)
			EndIf
		elseIf Game.FindClosestReferenceOfAnyTypeInListFromRef(wizCookingPotList, self, 1000) != None
			;Cooking pots
			if Utility.RandomInt(1,100) <= 25 
				BarrelContainer = self.placeatme(wizBarrelStaticFish)
			else
				BarrelContainer = self.placeatme(wizBarrelStaticMeat)
				if Compatibility.isRND
					;RND is installed so use stale food!
					BarrelContainer.RemoveAllItems()
					BarrelContainer.addItem(Game.GetFormFromFile(0x01007F55, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemBarrelFoodStale50
					BarrelContainer.addItem(Game.GetFormFromFile(0x01007F63, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemBarrelFoodRawMeatSame10
					BarrelContainer.addItem(Game.GetFormFromFile(0x01007F65, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemMiscItem50
					BarrelContainer.addItem(Game.GetFormFromFile(0x01007F65, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemMiscItem50
				endIf
			endIf
		elseIf Game.FindClosestReferenceOfAnyTypeInListFromRef(wizAlchemyTables, self, 1000) != None
			;Alchemy Tables
			if Utility.RandomInt(1,100) <= 25 
				BarrelContainer = self.placeatme(wizBarrelStaticIngredientUncommon)
			else
				BarrelContainer = self.placeatme(wizBarrelStaticIngredientCommon)
			endIf
		else
			;Randomize the barrel
			BarrelContainer = self.placeatme(wizBarrelStaticFood)
			if Compatibility.isRND
				;RND is installed so use stale food!
				BarrelContainer.RemoveAllItems()
				BarrelContainer.addItem(Game.GetFormFromFile(0x01007F55, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemBarrelFoodStale50
				BarrelContainer.addItem(Game.GetFormFromFile(0x01007F55, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemBarrelFoodStale50
				BarrelContainer.addItem(Game.GetFormFromFile(0x01007F65, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemMiscItem50
				BarrelContainer.addItem(Game.GetFormFromFile(0x01007F65, "RealisticNeedsandDiseases.esp") AS LeveledItem) ;RND_LItemMiscItem50
			endIf
		endIf

		BarrelContainer.SetAngle(self.GetAngleX(), self.GetAngleY(), self.GetAngleZ())
		;Set the Scale!
		BarrelContainer.SetScale(ClosestBarrel.getscale())

		;Make barrel container owned???
		if  self.getcurrentlocation() && self.getcurrentlocation().haskeyword(Keyword.getkeyword("LocTypeHabitation"))
			BarrelContainer.SetActorOwner(ArngeirREF.GetActorBase())
		endIf

		utility.wait(1) ;This should help reduce flicker
		if ParentIssues != TRUE && !wizParentIssues.HasForm(ClosestBarrel)
			ClosestBarrel.disable()
			if !ClosestBarrel.IsDisabled()
				;This catches parent problems and add it to an ignore list to prevent
				debug.trace ("********** wizDYNAMIC THINGS - The Enable State Parent error above has been accounted for and will not occur again for this object **********")
				ParentIssues = TRUE
				;Add to parent issues for logging!
				if !(wizParentIssues.HasForm(ClosestBarrel)) 
					wizParentIssues.AddForm(ClosestBarrel)
				endIf
;				debug.messagebox(ClosestBarrel)
			endIf
		else
			;Known parent issues!
			ParentIssues = TRUE
		endIf



endFunction