Scriptname SHESON_DynDOLOD_Firstborn extends ObjectReference
;speedrunning is bad mmmkay

ObjectReference MyMaster = None
ObjectReference MyBrother = None
ObjectReference MyBrotherMinion = None
String MyWorld = "None"
String MyImportFileName = "None"
String MyModName = "DynDOLOD.esp"
String MyFormID = "None"
String MyFirstbornList = "None"
String MyMinionList = "None"
Bool MyMasterEnable = FALSE
Bool MyObjectEnable = FALSE


Event onTriggerEnter(objectReference triggerRef)
	if (triggerRef == Game.GetPLayer())
		if (Game.GetPlayer().GetActorValue("SpeedMult") <= 500)
			if (SKSE.GetVersion() > 0)
				if (PapyrusUtil.GetVersion() >= 26)
					MyWorld = StringUtil.Substring(self.GetWorldspace() as string, 13, StringUtil.Find(self.GetWorldspace() as string, "(") - 14)
					MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
					MyFormID = Math.LogicalAnd(0x00FFFFFF, self.GetFormID()) as string
					if (JsonUtil.StringListGet(MyImportFileName, MyFormID, 0) as bool)
						MyFirstbornList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 0)
						MyMinionList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 1)
						MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
						if (MyMaster != None)
							if (!MyMasterEnable) && (StorageUtil.GetIntValue(MyMaster, "DynDOLOD_Active", 1) == 1)
								StorageUtil.SetFormValue(MyMaster, "DynDOLOD_MyCurrentFirstborn", self)
								(MyMaster as SHESON_DynDOLOD_Master).IBowToThee(self, MyFirstbornList, MyMinionList, TRUE)
								MyMasterEnable = TRUE
							endIf
						else
							Debug.Notification("DynDOLOD can not read data from " + MyImportFileName + ".json")
						endIf
					else
						Debug.Notification("DynDOLOD can not read data from DynDOLOD_Worlds.json")
					endIf
				else
					Debug.MessageBox("DynDOLOD requires PapyrusUtil")
				endIf
			else
				Debug.MessageBox("DynDOLOD requires SKSE")
			endIf
		endIf
	endIf
endEvent

Event onTriggerLeave(objectReference triggerRef)
	if (triggerRef == Game.GetPLayer()) && (MyMasterEnable)
		MyMasterEnable = FALSE
		MyWorld = StringUtil.Substring(self.GetWorldspace() as string, 13, StringUtil.Find(self.GetWorldspace() as string, "(") - 14)
		MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
		MyFormID = Math.LogicalAnd(0x00FFFFFF, self.GetFormID()) as string
		MyFirstbornList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 0)
		MyMinionList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 1)
		MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
		if (MyMaster != None)
			if (StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyActiveFirstborn", None) == self)
				Utility.Wait(3)
			endIf
			if (StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyActiveFirstborn", None) == self)
				; cell attach can happen because of wait
				if (self.GetParentCell())
					if (!self.GetParentCell().IsAttached())
						(MyMaster as SHESON_DynDOLOD_Master).IBowToThee(self, MyFirstbornList, MyMinionList, FALSE)
					endIf
				else
					(MyMaster as SHESON_DynDOLOD_Master).IBowToThee(self, MyFirstbornList, MyMinionList, FALSE)
				endIf
			endIf
		endIf
	endIf
endEvent

Event OnCellAttach()
	StorageUtil.SetIntValue(self, "CellState", 1)
	if (Game.GetPlayer().GetActorValue("SpeedMult") <= 500)
		MyWorld = StringUtil.Substring(self.GetWorldspace() as string, 13, StringUtil.Find(self.GetWorldspace() as string, "(") - 14)
		MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
		MyFormID = Math.LogicalAnd(0x00FFFFFF, self.GetFormID()) as string
		MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
		if (MyMaster != None)
			MyBrother = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, MyFormID, 0), MyModName) As ObjectReference
			MyBrotherMinion = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, MyFormID, 1), MyModName) As ObjectReference
			if (StorageUtil.GetIntValue(MyMaster, "DynDOLOD_Active", 1) == 1)
				if (MyBrother == None)
					Activate(self)
				else
					StorageUtil.SetIntValue(MyBrother, "CellState", 1)
					MyBrother.Activate(self)
				endIf
				if (MyBrotherMinion != None)
					StorageUtil.SetIntValue(MyBrotherMinion, "CellState", 1)
					MyBrotherMinion.Activate(self)
				endIf
				MyObjectEnable = TRUE
			endIf
		endIf
	endIf
endEvent

Event OnCellDetach()
	StorageUtil.SetIntValue(self, "CellState", 0)
	if (MyObjectEnable)
		MyWorld = StringUtil.Substring(self.GetWorldspace() as string, 13, StringUtil.Find(self.GetWorldspace() as string, "(") - 14)
		MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
		MyFormID = Math.LogicalAnd(0x00FFFFFF, self.GetFormID()) as string
		MyBrother = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, MyFormID, 0), MyModName) As ObjectReference
		MyBrotherMinion = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, MyFormID, 1), MyModName) As ObjectReference
		if (MyBrother == None)
			Activate(self)
		else
			StorageUtil.SetIntValue(MyBrother, "CellState", 0)
			MyBrother.Activate(self)
		endIf
		if (MyBrotherMinion != None)
			StorageUtil.SetIntValue(MyBrotherMinion, "CellState", 0)
			MyBrotherMinion.Activate(self)
		endIf
		MyObjectEnable = FALSE
	endIf
	if (MyMasterEnable)
		MyMasterEnable = FALSE
		MyWorld = StringUtil.Substring(self.GetWorldspace() as string, 13, StringUtil.Find(self.GetWorldspace() as string, "(") - 14)
		MyImportFileName = "DynDOLOD_" + JsonUtil.GetStringValue("DynDOLOD_Worlds", MyWorld, MyWorld)
		MyFormID = Math.LogicalAnd(0x00FFFFFF, self.GetFormID()) as string
		MyFirstbornList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 0)
		MyMinionList = JsonUtil.StringListGet(MyImportFileName, MyFormID, 1)
		MyMaster = Game.GetFormFromFile(JsonUtil.IntListGet(MyImportFileName, "master", 0), MyModName) As ObjectReference
		if (MyMaster != None)
			if (StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyActiveFirstborn", None) == self)
				Utility.Wait(3)
			endIf
			if (StorageUtil.GetFormValue(MyMaster, "DynDOLOD_MyActiveFirstborn", None) == self)
				; cell attach can happen because of wait
				if (self.GetParentCell())
					if (!self.GetParentCell().IsAttached())
						(MyMaster as SHESON_DynDOLOD_Master).IBowToThee(self, MyFirstbornList, MyMinionList, FALSE)
					endIf
				else
					(MyMaster as SHESON_DynDOLOD_Master).IBowToThee(self, MyFirstbornList, MyMinionList, FALSE)
				endIf
			endIf
		endIf
	endIf
endEvent
