Scriptname _00E_Theriantrophist_TransformStorage extends Quest  

Form EquippedItemLeft
Form EquippedItemRight

Armor[] wornArmor

Int whatKindLeft
Int whatKindRight

Float armorRating
Float weaponDamageSpeed

Float Property DualWieldLeftHandMalus = 4.0 Autoreadonly Hidden
{The damage of a the left hand weapon is divided by this to avoid exploitation}

Perk Property _00E_Class_BladeDancer_P01_Mercenary_01 Auto ;8
Perk Property _00E_Class_BladeDancer_P01_Mercenary_02 Auto ;7
Perk Property _00E_Class_Vandal_P01_Barbarian_01 Auto ; 12
Perk Property _00E_Class_Vandal_P01_Barbarian_02 Auto ; 10
Perk Property _00E_Class_Trickster_P01_StrongBowstring_01 Auto ; 12
Perk Property _00E_Class_Trickster_P01_StrongBowstring_02 Auto ; 10

ObjectReference Property _00E_TransformStorageREF Auto

int Function GetWeaponDamage(Weapon weapID)

	int iDamage	
	String scalingAV
	float fPerkMod
	
	Actor akplayer = Game.GetPlayer()
	
	if weapID.IsBattleaxe() || weapID.IsGreatsword()
		scalingAV = "TwoHanded"
	elseif weapID.IsBow()
		scalingAV = "Marksman"
	else
		scalingAV = "OneHanded"
	endif
	
	if scalingAV == "TwoHanded"
	
		if akplayer.HasPerk(_00E_Class_Vandal_P01_Barbarian_01)
			fPerkMod += 12
		endif
		
		if akplayer.HasPerk(_00E_Class_Vandal_P01_Barbarian_02)
			fPerkMod += 10
		endif
		
	elseif scalingAV == "Marksman"
	
		if akplayer.HasPerk(_00E_Class_Trickster_P01_StrongBowstring_01)
			fPerkMod += 12
		endif
		
		if akplayer.HasPerk(_00E_Class_Trickster_P01_StrongBowstring_02)
			fPerkMod += 10
		endif
	
	else
	
		if akplayer.HasPerk(_00E_Class_BladeDancer_P01_Mercenary_01)
			fPerkMod += 8
		endif
		
		if akplayer.HasPerk(_00E_Class_BladeDancer_P01_Mercenary_02)
			fPerkMod += 7
		endif
	
	endif
	
	iDamage = ((weapID.GetBaseDamage()*weapID.getSpeed())*(1+((akplayer.GetActorValue(scalingAV)*0.55)/100))*(1+(fPerkMod/100))) as Int
	return iDamage
	
EndFunction

Function saveEquippedItems()
		
	Actor PlayerRef = Game.GetPlayer()
	
	EquippedItemLeft = PlayerRef.GetEquippedObject(0) 
	EquippedItemRight = PlayerRef.GetEquippedObject(1) 
	weaponDamageSpeed = 0
	
	If !EquippedItemRight 
		whatKindRight = 0
	ElseIf (EquippedItemRight as Weapon)
		weaponDamageSpeed += GetWeaponDamage(EquippedItemRight as Weapon)
		whatKindRight = 1
	ElseIf (EquippedItemRight as Spell)
		whatKindRight = 2
	Else 
		whatKindRight = 1
	EndIf
	
	
	If !EquippedItemLeft 
		whatKindLeft = 0
	ElseIf (EquippedItemLeft as Weapon)
		GetWeaponDamage(EquippedItemLeft as Weapon)
		whatKindLeft = 1
	ElseIf (EquippedItemLeft as Spell)
		whatKindLeft = 2
	Else 
		whatKindLeft = 1
	EndIf

	wornArmor = new Armor[15]
	wornArmor[0] = PlayerRef.GetWornForm(0x00000001) as Armor
	wornArmor[1] = PlayerRef.GetWornForm(0x00000002) as Armor
	wornArmor[2] = PlayerRef.GetWornForm(0x00000004) as Armor
	wornArmor[3] = PlayerRef.GetWornForm(0x00000008) as Armor
	wornArmor[4] = PlayerRef.GetWornForm(0x00000010) as Armor
	wornArmor[5] = PlayerRef.GetWornForm(0x00000020) as Armor
	wornArmor[6] = PlayerRef.GetWornForm(0x00000040) as Armor
	wornArmor[7] = PlayerRef.GetWornForm(0x00000080) as Armor
	wornArmor[8] = PlayerRef.GetWornForm(0x00000100) as Armor
	wornArmor[9] = PlayerRef.GetWornForm(0x00000200) as Armor
	wornArmor[10] = PlayerRef.GetWornForm(0x00000400) as Armor
	wornArmor[11] = PlayerRef.GetWornForm(0x00000800) as Armor
	wornArmor[12] = PlayerRef.GetWornForm(0x00001000) as Armor
	wornArmor[13] = PlayerRef.GetWornForm(0x00002000) as Armor
	wornArmor[13] = PlayerRef.GetWornForm(0x00004000) as Armor
	wornArmor[14] = PlayerRef.GetWornForm(0x00020000) as Armor
	
	armorRating = 0
	Int i = 0
	while (i < wornArmor.length)
		if (wornArmor[i] != None)
			armorRating += wornArmor[i].getArmorRating()
		Endif
		i += 1
	Endwhile
endFunction

Float Function getArmorRating()
	return armorRating
Endfunction

Float Function getWeaponDamageSpeed()
	return weaponDamageSpeed
Endfunction

bool Function isEquippedItemRightSpell()
	return whatKindRight == 2
Endfunction

bool Function isEquippedItemLeftSpell()
	return whatKindLeft == 2
Endfunction

Spell Function getEquippedSpellRight()
	return EquippedItemRight as Spell
EndFunction

Spell Function getEquippedSpellLeft()
	return EquippedItemLeft as Spell
EndFunction

Function equipeItems()
	Actor PlayerRef = Game.GetPlayer()
	
	if whatKindRight == 2
		PlayerRef.EquipSpell(EquippedItemRight as Spell, 1)
	elseif PlayerRef.GetItemCount(EquippedItemRight) > 0
		PlayerRef.EquipItemEx(EquippedItemRight,1 )
	endif

	if whatKindLeft == 2
		PlayerRef.EquipSpell(EquippedItemLeft as Spell, 0)
	elseif PlayerRef.GetItemCount(EquippedItemLeft) > 0
		PlayerRef.EquipItemEx(EquippedItemLeft,2 )
	endif
	
	Int index = wornArmor.length
	while index > 0
		index -= 1
		If wornArmor[index] && PlayerRef.GetItemCount(wornArmor[index]) > 0
			;removing and readding items fixes the issue that set bonuses wouldn't apply properly after re-transforming
			PlayerRef.RemoveItem(wornArmor[index], 1, true, _00E_TransformStorageREF)
			_00E_TransformStorageREF.RemoveItem(wornArmor[index], 1, true, PlayerRef)
			PlayerRef.EquipItem(wornArmor[index],false,true)
		EndIf
	Endwhile
endFunction