ScriptName aaaKimachuuCrisisOfLife Extends ReferenceAlias

ActorBase Property aaaKimachuuCOLRaider Auto
FormList Property aaaKimachuuCOLExcludeLocation Auto
GlobalVariable Property RoomCost Auto
Message Property aaaKimachuuCOLMes01 Auto ;完治
Message Property aaaKimachuuCOLMes02 Auto ;襲撃者
Message Property aaaKimachuuCOLMes03 Auto ;症状が和らぐ
MiscObject Property Gold001 Auto
Quest Property aaaKimachuuCOLQuest Auto
Quest Property aaaKimachuuCOLSearchQuest Auto
Spell Property aaaKimachuuCOLDisease01 Auto ;重症
Spell Property aaaKimachuuCOLDisease02 Auto ;中症
Spell Property aaaKimachuuCOLDisease03 Auto ;軽症
Spell Property aaaKimachuuCOLBuffs Auto ;完治バフ
ReferenceAlias Property Marker Auto

Float fSleepStartTime
Float fWellRestedStartTime
Float fWellRestedTime

Float fRecoveryTime = 24.0
Int iSleep = 0

Event OnSleepStart(float afSleepStartTime, float afDesiredSleepEndTime)
    fSleepStartTime = afSleepStartTime
EndEvent

Event OnSleepStop(bool abInterrupted)
	Actor actRef = Self.GetActorReference()
	Float fSleepStopTime = Utility.GetCurrentGameTime()
	fWellRestedTime = 0.0
	fWellRestedTime = ((fSleepStopTime-fSleepStartTime)*24.0)
	raiderFunction(actRef, fWellRestedTime)
	actRef = None
ENDEvent

Event OnEnterBleedout()
	Actor actRef = Self.GetActorReference()
	actRef.StopCombatAlarm()
	addDiseaseFunction(actRef)
	Utility.Wait(1.0)
	moveFunction(actRef)
	actRef = None
EndEvent

Event OnUpdateGameTime()
	Utility.Wait(1.0)
	Actor actRef = Self.GetActorReference()
	recoveryFunction(actRef)
	actRef = None
EndEvent

Function addDiseaseFunction(Actor act01)
	If act01.HasSpell(aaaKimachuuCOLDisease03) || act01.HasSpell(aaaKimachuuCOLDisease02) || act01.HasSpell(aaaKimachuuCOLDisease01)
		aaaKimachuuCOLQuest.Stop()
		Utility.Wait(0.1)
		act01.KillEssential()
		Return
	EndIf

	act01.RemoveSpell(aaaKimachuuCOLDisease01)
	act01.RemoveSpell(aaaKimachuuCOLDisease02)
	act01.RemoveSpell(aaaKimachuuCOLDisease03)
	act01.DispelSpell(aaaKimachuuCOLBuffs)
	act01.AddSpell(aaaKimachuuCOLDisease01)
	UnregisterForUpdateGameTime()
	RegisterForSleep()
	RegisterForSingleUpdateGameTime(fRecoveryTime)
EndFunction

Function recoveryFunction(Actor act01)
	If act01.HasSpell(aaaKimachuuCOLDisease01) ;重症
		act01.RemoveSpell(aaaKimachuuCOLDisease01)
		act01.AddSpell(aaaKimachuuCOLDisease02)
		RegisterForSingleUpdateGameTime(fRecoveryTime - iSleep As Float)
		aaaKimachuuCOLMes03.Show()
	ElseIf act01.HasSpell(aaaKimachuuCOLDisease02) ;中症
		act01.RemoveSpell(aaaKimachuuCOLDisease02)
		act01.AddSpell(aaaKimachuuCOLDisease03)
		RegisterForSingleUpdateGameTime(fRecoveryTime - iSleep As Float)
		aaaKimachuuCOLMes03.Show()
	ElseIf act01.HasSpell(aaaKimachuuCOLDisease03) ;軽症
		act01.RemoveSpell(aaaKimachuuCOLDisease03)
		aaaKimachuuCOLBuffs.Cast(act01, act01)
		iSleep = 0
		aaaKimachuuCOLMes01.Show()
		UnregisterForSleep()
	EndIf
EndFunction

Function moveFunction(Actor act01)
	aaaKimachuuCOLSearchQuest.Stop()
	Utility.Wait(0.1)
	aaaKimachuuCOLSearchQuest.Start()

	Location kCurrentLoc = act01.GetCurrentLocation()
	If aaaKimachuuCOLExcludeLocation.HasForm(kCurrentLoc)
		aaaKimachuuCOLQuest.Stop()
		Utility.Wait(0.1)
		act01.KillEssential()
		Return
	EndIf

	ObjectReference objMarker = Marker.GetReference()
	If objMarker
		act01.SetGhost()
		act01.RestoreActorValue("Health", 9999)
		Utility.Wait(0.1)
		act01.Moveto(objMarker, 0.0, 30.0, 30.0, true)
		Utility.Wait(3.5)
		act01.PushActorAway(act01, 1.0)
		Utility.Wait(1.0)
		act01.SetGhost(false)
		act01.StopCombatAlarm()
	Else
		aaaKimachuuCOLQuest.Stop()
		Utility.Wait(0.1)
		act01.KillEssential()
	EndIf
	If act01.GetItemCount(Gold001) < RoomCost.GetValueInt()
		act01.AddItem(Gold001, RoomCost.GetValueInt())
	EndIf
	kCurrentLoc = None
	objMarker = None
EndFunction

Function raiderFunction(Actor act01, Float f01)
	Int iCount = f01 As Int
	iSleep += iCount
	If iSleep < fRecoveryTime As Int
		Debug.Notification(iSleep + "/" + fRecoveryTime As Int + "hours left.")
	Else
		iSleep -= fRecoveryTime As Int
		If iSleep > 0
			Debug.Notification(iSleep + "/" + fRecoveryTime As Int + "hours left.")
		EndIf
	EndIf
	
	Bool bFlag = False
	While iCount > 0
		Int iRnd = Utility.RandomInt(1, 100)
		If iRnd >= 98
			Actor actRaider = act01.PlaceActorAtMe(aaaKimachuuCOLRaider, 1)
			bFlag = True
		EndIf
		If bFlag == True
			iCount -= 25
		Else
			iCount -= 1
		EndIf
	EndWhile
	If bFlag == True
		aaaKimachuuCOLMes02.Show()
	EndIf
EndFunction