ScriptName aaaKimachuuAutoEquipment Extends ActiveMagicEffect

Bool bRunning = False

Event OnEffectStart(Actor akTarget, Actor akCaster)
	If bRunning == True
		Debug.Notification("Running!")
		Return
	EndIf

	bRunning == True

	Actor actPlayer = akCaster
	Form[] containerForms = PO3_SKSEFunctions.AddItemsOfTypeToArray(actPlayer, 26, True, False, True) ;コンテナに対してフォームタイプでフィルタしてから配列にいれる機能で、26が防具
	Int iArmorCount = containerForms.Length

	Int[] iSlot = new Int[6]
	iSlot[0] = 1 ;Slot30
	iSlot[1] = 2 ;Slot31
	iSlot[2] = 4096 ;Slot42
	iSlot[3] = 4 ;Slot32
	iSlot[4] = 8 ;Slot33
	iSlot[5] = 128 ;Slot37
	

	Form[] armArrayH = new Form[8] ;配列の数はあえて少なめにして、全配列が埋まったら処理中断する的な
	Form[] armArray32 = new Form[8]
	Form[] armArray33 = new Form[8]
	Form[] armArray37 = new Form[8]

	Int i = 0

	Int iHead = 0 ;分類箱に防具が入る度に伸ばして、後の乱数の範囲を広げる
	Int i32 = 0
	Int i33 = 0
	Int i37 = 0

	;スロット配列に防具を分類する
	While i < iArmorCount
		Armor armArmor = containerForms[i] as Armor
		If armArmor != None ;所持品にアーマーがあった場合
			Int iSlotmask = armArmor.GetSlotMask()
			;Debug.Notification(armArmor.GetName() + " SlotMask is :" + iSlotmask)

			If iHead >= armArrayH.Length && i32 >= armArray32.Length && i33 >= armArray33.Length && i37 >= armArray37.Length
				i = iArmorCount
				Debug.Notification("Skip.")
			ElseIf iSlotmask == 4096 || iSlotmask == 4097 || iSlotmask == 4098 ;頭装備
				If iHead <= armArrayH.Length
					armArrayH[iHead] = armArmor ;分類箱に入れる
					;Debug.Notification(armArrayH[iHead].GetName() + " to Head")
					iHead += 1 ;後に使う乱数の長さを伸ばす
				EndIf
			ElseIf iSlotmask == 4102 ;フード付きローブを力技で除外
				;Debug.Notification(armArmor.GetName() + " is a hooded robe.")
			ElseIf iSlotmask == 4 ;胴装備
				If i32 <= armArray32.Length
					armArray32[i32] = armArmor
					;Debug.Notification(armArray32[i32].GetName() + " to 32")
					i32 += 1
				EndIf
			ElseIf iSlotmask == 8 ;腕装備
				If i33 <= armArray33.Length
					armArray33[i33] = armArmor
					;Debug.Notification(armArray33[i33].GetName() + " to 33")
					i33 += 1
				EndIf
			ElseIf iSlotmask == 128 ;足装備
				If i37 <= armArray37.Length
					armArray37[i37] = armArmor
					;Debug.Notification(armArray37[i37].GetName() + " to 37")
					i37 += 1
				EndIf
			Else
				;empty
			EndIf
		EndIf
		armArmor = None
		i += 1
	EndWhile

	;Debug.Notification("i Count:" + i) ;繰り返し処理した回数を表示、デバッグ用

	Int x = 0

	;未装備の部位に分類した防具を装着する
	While x < 6
		Form bArmor = actPlayer.GetWornForm(iSlot[x]) ;プレイヤーの防具を取得
		If x == 0
			If actPlayer.GetWornForm(iSlot[0]) == None && actPlayer.GetWornForm(iSlot[1]) == None && actPlayer.GetWornForm(iSlot[2]) == None
				Int iRnd = Utility.RandomInt(0, iHead - 1) ;分類箱の長さ分の乱数を生成する
				actPlayer.EquipItem(armArrayH[iRnd], False, True) ;選ばれた防具を自動装備する
			EndIf
		ElseIf x == 3 && bArmor == None
			Int iRnd = Utility.RandomInt(0, i32 - 1)
			actPlayer.EquipItem(armArray32[iRnd], False, True)
		ElseIf x == 4 && bArmor == None
			Int iRnd = Utility.RandomInt(0, i33 - 1)
			actPlayer.EquipItem(armArray33[iRnd], False, True)
		ElseIf x == 5 && bArmor == None
			Int iRnd = Utility.RandomInt(0, i37 - 1)
			actPlayer.EquipItem(armArray37[iRnd], False, True)
		EndIf
		bArmor = None
		x += 1
	EndWhile

	bRunning == False

	Debug.Notification("Done.")
endEvent