ScriptName aaaKimachuuETME Extends ActiveMagicEffect

Actor Property PlayerRef Auto ;プレイヤー
Actor Property aaaKimachuuETActor01 Auto ;移植元
Actor Property aaaKimachuuETActor02 Auto ;移植先
Actor Property aaaKimachuuETActor03 Auto ;消去装置
FormList Property aaaKimachuuETList Auto ;キーワードリスト
Message Property aaaKimachuuETMes01 Auto ;選択肢
Message Property aaaKimachuuETMes02 Auto ;移植元エラー
Message Property aaaKimachuuETMes03 Auto ;移植先エラー
Message Property aaaKimachuuETMes04 Auto ;移植エラー

Event OnEffectStart(Actor akTarget, Actor akCaster)
	GoToState("mainState")
EndEvent

State mainState
	Event OnBeginState()
		Int iIndex = aaaKimachuuETMes01.Show()
		If iIndex == 0 ;移植元アイテム選択
			item01Function()
			GoToState("mainState")
		ElseIf iIndex == 1 ;移植先アイテム選択
			item02Function()
			GoToState("mainState")
		ElseIf iIndex == 2 ;移植実行
			transferFunction()
		ElseIf iIndex == 3 ;付呪と強化値を消去
			item03Function()
			GoToState("mainState")
		Else ;キャンセル

		EndIf
	EndEvent
EndState

Function item01Function()
	aaaKimachuuETActor01.RemoveAllItems(PlayerRef)
	aaaKimachuuETActor01.ShowGiftMenu(true, aaaKimachuuETList)
	Int iNumItems = aaaKimachuuETActor01.GetNumItems()
	If iNumItems == 1
		iNumItems -= 1
		Form forArmor = aaaKimachuuETActor01.GetNthForm(iNumItems)
		Armor myArmor = forArmor As Armor
		Int iSM = myArmor.GetSlotMask()
		aaaKimachuuETActor01.EquipItem(myArmor)	
		Enchantment myEnch = WornObject.GetEnchantment(aaaKimachuuETActor01, 0, iSM)

		Int myEffects = myEnch.GetNumEffects()
		If (myArmor) && (myEffects != 0)
			;empty
		Else
			aaaKimachuuETMes02.Show()
			aaaKimachuuETActor01.RemoveAllItems(PlayerRef)
		EndIf
	ElseIf iNumItems == 0
		;empty
	Else
		aaaKimachuuETMes02.Show()
		aaaKimachuuETActor01.RemoveAllItems(PlayerRef)
	EndIf
EndFunction

Function item02Function()
	aaaKimachuuETActor02.RemoveAllItems(PlayerRef)
	aaaKimachuuETActor02.ShowGiftMenu(true, aaaKimachuuETList)
	Int iNumItems = aaaKimachuuETActor02.GetNumItems()
	If iNumItems == 1
		iNumItems -= 1
		Form forArmor = aaaKimachuuETActor02.GetNthForm(iNumItems)
		Armor myArmor = forArmor As Armor
		Int iSM = myArmor.GetSlotMask()
		aaaKimachuuETActor02.EquipItem(myArmor)
		Enchantment myEnch = WornObject.GetEnchantment(aaaKimachuuETActor02, 0, iSM)

		Int myEffects = myEnch.GetNumEffects()
		If (myArmor) && (myEffects == 0)
			;empty
		Else
			aaaKimachuuETMes03.Show()
			aaaKimachuuETActor02.RemoveAllItems(PlayerRef)
		EndIf
	ElseIf iNumItems == 0

	Else
		aaaKimachuuETMes03.Show()
		aaaKimachuuETActor02.RemoveAllItems(PlayerRef)
	EndIf
EndFunction

Function item03Function()
	aaaKimachuuETActor03.ShowGiftMenu(true, aaaKimachuuETList)
	Int iNumItems = aaaKimachuuETActor03.GetNumItems()
	If iNumItems >= 1
		While iNumItems >= 0
			iNumItems -= 1
			Form forArmor = aaaKimachuuETActor03.GetNthForm(iNumItems)
			Armor myArmor = forArmor As Armor
			If myArmor
				PlayerRef.AddItem(myArmor, 1)
			EndIf
		EndWhile
	EndIf
	aaaKimachuuETActor03.RemoveAllItems()
EndFunction

Function transferFunction()
	Form forArmor01 = aaaKimachuuETActor01.GetNthForm(0)
	Armor myArmor01 = forArmor01 As Armor
	Form forArmor02 = aaaKimachuuETActor02.GetNthForm(0)
	Armor myArmor02 = forArmor02 As Armor

	If myArmor01 && myArmor02
		Int iSM01 = myArmor01.GetSlotMask()
		Int iSM02 = myArmor02.GetSlotMask()
		MagicEffect[] meArray = new MagicEffect[3]
		Float[] fMag = new Float[3]
		Int[] iZero = new Int[3]
		Int x = 0

		Enchantment myEnch = WornObject.GetEnchantment(aaaKimachuuETActor01, 0, iSM01)

		While x < 3
			meArray[x] = myEnch.GetNthEffectMagicEffect(x)
			fMag[x] = myEnch.GetNthEffectMagnitude(x)
			iZero[x] = 0
			x += 1
		EndWhile
		WornObject.CreateEnchantment(aaaKimachuuETActor02, 0, iSM02, 0.0, meArray, fMag, iZero, iZero)

		aaaKimachuuETActor01.RemoveAllItems()
		aaaKimachuuETActor02.RemoveAllItems(PlayerRef)
	Else
		aaaKimachuuETMes04.Show()
	EndIf
EndFunction
