Scriptname SHESON_DynDOLOD_LODObject extends ObjectReference
;Switching "states" with one activtor parent
;Firstborn may be a Minion

ObjectReference MyMaster = None
ObjectReference MyEnableParent = None
String MyWorld = "None"
String MyImportFileName = "None"
String MyModName = "DynDOLOD.esp"
String MyFormID = "None"
Int SafetyCounter = 0
Bool MyEnabled = FALSE
Bool MyFirstbornState = FALSE
Bool MyAttached = FALSE

Event OnActivate(ObjectReference akActionRef)
	MyWorld = StringUtil.Substring(self.GetWorldspace() as string, 13, StringUtil.Find(self.GetWorldspace() as string, "(") - 14)
	MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
	MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
	if (MyMaster != None)
		MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld) + "_Objects"
		MyFormID = Math.LogicalAnd(0x00FFFFFF, self.GetFormID()) as string

		; is there an enable parent form id and is its mod loaded
		if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 5) as bool) && (Game.GetModByName(JsonUtil.StringListGet(MyImportFileName, MyFormID, 6)) != 255)

		        ; enable parent is not persistant, so do this tricky thing
			MyEnableParent = Game.GetFormFromFile(JsonUtil.StringListGet(MyImportFileName, MyFormID, 5) as int, JsonUtil.StringListGet(MyImportFileName, MyFormID, 6)) As ObjectReference

			; enabled or disabled that is the question
			if (MyEnableParent != None) && (MyEnableParent != self)
				if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 8) as int > -1)
					if (MyEnableParent.GetCurrentDestructionStage())
						; destruction model
						if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 8) as int == 1) && (MyEnableParent.GetCurrentDestructionStage() >= JsonUtil.StringListGet(MyImportFileName, MyFormID, 7) as int)
							MyEnabled = TRUE
						endif
					endIf
				endIf
				if (MyEnableParent.IsDisabled())
					MyEnabled = FALSE
				else
					MyEnabled = TRUE
				endIf
				if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 8) as int > -1)
					if (MyEnableParent.GetCurrentDestructionStage())
						; default model
						if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 8) as int == 0) && (MyEnableParent.GetCurrentDestructionStage() >= JsonUtil.StringListGet(MyImportFileName, MyFormID, 7) as int)
							MyEnabled = FALSE
						endif
					endIf
				endIf
			else
				MyEnabled = JsonUtil.StringListGet(MyImportFileName, MyFormID, 2) as bool
			endIf
			if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 4) as bool)
				MyEnabled = !MyEnabled
			endIf

			if (akActionRef != None)
				if (StorageUtil.GetIntValue(akActionRef, "GridState") == 0)
					if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 0) as bool)
						MyFirstbornState = FALSE
					endIf
				else
					MyFirstbornState = TRUE
				endIf
				if (StorageUtil.GetIntValue(akActionRef, "CellState") == 0)
					MyAttached = FALSE
				else
					if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 3) as bool)
						MyAttached = TRUE
					endIf
				endIf
			endIf

			; change visibility
			if (MyEnabled) && (!MyAttached) && (MyFirstbornState)
				self.EnableNoWait(FALSE)
			else
				if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 1) as bool) && (self != None)
					Utility.Wait(1)
				endIf
				SafetyCounter = 10
				while (self != None ) && (!self.IsDisabled()) && (SafetyCounter)
					if (MyEnabled) && (!MyAttached) && (MyFirstbornState)
						SafetyCounter = 0
					else
						self.DisableNoWait(FALSE)
						SafetyCounter -= 1
						if (self != None ) && (SafetyCounter > 0)
							Utility.Wait(3)
						EndIf
					endIf
				endWhile
			endIf
		else
			if (!self.IsDisabled())
				self.DisableNoWait(FALSE)
			endIf
		endIf
	endIf
EndEvent
