ScriptName aaaKimachuuOnslaughtME Extends ActiveMagicEffect

Actor Property aaaKimachuuOnslaughtDummyRef Auto
ActorBase Property aaaKimachuuOnslaughtPodActor Auto
EffectShader property aaaKimachuuOnslaughtES auto
Explosion Property aaaKimachuuOnslaughtExplosion Auto
FormList Property aaaKimachuuOnslaughtFLN Auto
FormList Property aaaKimachuuOnslaughtFLT Auto
GlobalVariable Property aaaKimachuuOnslaughtGlo Auto
MusicType Property aaaKimachuuOnslaughtSD01 Auto
ObjectReference Property aaaKimachuuOnslaughtMarkerRef Auto
Shout Property aaaKimachuuOnslaughtShout Auto
Spell Property aaaKimachuuOnslaughtSpell Auto
Spell Property aaaKimachuuOnslaughtPodSpell Auto

Actor actCaster
Form forRHands
Form forLHands

Import Math

Event OnEffectStart(Actor akTarget, Actor akCaster)
	actCaster = akTarget

	If aaaKimachuuOnslaughtGlo.GetValueInt() == 1
		finishFunction()
		Return
	EndIf

	aaaKimachuuOnslaughtGlo.SetValueInt(1)
	transformFunction(actCaster)

	Float fltDistance = 50
	Float fltAngZ = actCaster.GetAngleZ()

	Float fltPosX = actCaster.GetPositionX() + Math.Sin(fltAngZ) * fltDistance
	Float fltPosY = actCaster.GetPositionY() + Math.Cos(fltAngZ) * fltDistance
	Float fltPosZ = actCaster.GetPositionZ()

	aaaKimachuuOnslaughtDummyRef.MoveTo(actCaster)
	aaaKimachuuOnslaughtDummyRef.SetPosition(fltPosX, fltPosY, fltPosZ)
	aaaKimachuuOnslaughtDummyRef.SetAngle(0, 0, fltAngZ + 180)
	Utility.Wait(0.1)
	Game.SetCameraTarget(aaaKimachuuOnslaughtDummyRef)
	ObjectReference objExp = actCaster.PlaceAtMe(aaaKimachuuOnslaughtExplosion)

	Int y = 0

	Actor[] actArray = New Actor[3]

	While y < 3
		actArray[y] = actCaster.PlaceActorAtMe(aaaKimachuuOnslaughtPodActor)
		actArray[y].AddSpell(aaaKimachuuOnslaughtPodSpell)
		y += 1
		Utility.Wait(0.1)
	EndWhile

	Utility.Wait(1.0)
	Game.SetCameraTarget(Game.GetPlayer())
	aaaKimachuuOnslaughtDummyRef.MoveTo(aaaKimachuuOnslaughtMarkerRef)
	aaaKimachuuOnslaughtSD01.Add()
	Utility.Wait(1.0)
	aaaKimachuuOnslaughtES.Stop(actCaster)
	objExp = None
	RegisterForSingleUpdate(2.0)
EndEvent

Event OnUpdate()
	If !actCaster.IsInCombat()
		RegisterForSingleUpdate(2.5)
		Return
	EndIf

	Float fHealthMax = actCaster.GetActorValueMax("Health")
	fHealthMax /= 20.0
	actCaster.DamageActorValue("Health", fHealthMax)
	Float fHealth = actCaster.GetActorValue("Health")
	If fHealth <= 0.0
		finishFunction()
	Else
		RegisterForSingleUpdate(1.0)
	EndIf
EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	Utility.Wait(1.0)
	If aaaKimachuuOnslaughtGlo.GetValueInt() == 0
		Return
	EndIf

	finishFunction()
EndEvent

Function finishFunction()
	UnregisterForUpdate()
	actCaster.DispelSpell(aaaKimachuuOnslaughtSpell)
	aaaKimachuuOnslaughtGlo.SetValueInt(0)
	aaaKimachuuOnslaughtSD01.Remove()
	Utility.Wait(1.0)
	releaseFunction(actCaster)
	actCaster = None
EndFunction

Function transformFunction(Actor act01)
	;act01.SetAlpha(0.0)
	aaaKimachuuOnslaughtES.Play(act01)
	forRHands = act01.GetEquippedObject(1)
	forLHands = act01.GetEquippedObject(0)
	aaaKimachuuOnslaughtFLN.Revert()
	Armor armWorn
	Int i = 0

	While i < 31
		armWorn = act01.GetWornForm(LeftShift(1, i)) as Armor
		If armWorn
			aaaKimachuuOnslaughtFLN.AddForm(armWorn)
			act01.UnequipItem(armWorn, false, True)
 		EndIf
 		i += 1
	EndWhile

	Utility.Wait(0.1)

	Int x = aaaKimachuuOnslaughtFLT.GetSize()

	While x > 0
		x -= 1
			armWorn = aaaKimachuuOnslaughtFLT.GetAt(x) as Armor
            If armWorn
            	act01.AddItem(armWorn, 1, True)
				act01.EquipItemEx(armWorn)
			EndIf
	EndWhile

	If forRHands As Spell
		act01.EquipSpell(forRHands As Spell, 1)
	Else
		act01.EquipItemEx(forRHands, 1)
	EndIf
	If forLHands As Spell
		act01.EquipSpell(forLHands As Spell, 0)
	Else
		act01.EquipItemEx(forLHands, 2)
	EndIf

	act01.EquipShout(aaaKimachuuOnslaughtShout)
	;act01.SetAlpha(1.0)
	forRHands = None
	forLHands = None
EndFunction

Function releaseFunction(Actor act01)
	;act01.SetAlpha(0.0)
	aaaKimachuuOnslaughtES.Play(act01)
	forRHands = act01.GetEquippedObject(1)
	forLHands = act01.GetEquippedObject(0)

	Armor armWorn
	Int i = 0

	While i < 31
		armWorn = act01.GetWornForm(LeftShift(1, i)) as Armor
		If armWorn
			act01.RemoveItem(armWorn, 1, true)
 		EndIf
 		i += 1
	EndWhile	

	act01.UnequipAll()
	Utility.Wait(0.1)

	Int x = aaaKimachuuOnslaughtFLN.GetSize()

	While x > 0
		x -= 1
			armWorn = aaaKimachuuOnslaughtFLN.GetAt(x) as Armor
            If armWorn && act01.GetItemCount(armWorn) > 0
				act01.EquipItemEx(armWorn)
			EndIf
	EndWhile

	If forRHands As Spell
		act01.EquipSpell(forRHands As Spell, 1)
	Else
		act01.EquipItemEx(forRHands, 1)
	EndIf
	If forLHands As Spell
		act01.EquipSpell(forLHands As Spell, 0)
	Else
		act01.EquipItemEx(forLHands, 2)
	EndIf

	act01.EquipShout(aaaKimachuuOnslaughtShout)
	;act01.SetAlpha(1.0)
	aaaKimachuuOnslaughtES.Stop(act01)
	forRHands = None
	forLHands = None
EndFunction