Scriptname ProperWeaponScaleAlias extends ReferenceAlias  

Actor Property PlayerRef Auto

;Alternative shield scale, scale the shield in hand instead on the back, may cause problems with drawn shields
bool Property SHIELD_SOLUTION_ALT = false AutoReadOnly

string Property NINODE_RIGHT_HAND = "NPC R Hand [RHnd]" AutoReadOnly
string Property NINODE_LEFT_HAND = "NPC L Hand [LHnd]" AutoReadOnly

string Property NINODE_AXE = "WeaponAxe" AutoReadOnly
string Property NINODE_MACE = "WeaponMace" AutoReadOnly
string Property NINODE_SWORD = "WeaponSword" AutoReadOnly
string Property NINODE_DAGGER = "WeaponDagger" AutoReadOnly
string Property NINODE_WEAPON_BACK = "WeaponBack" AutoReadOnly
string Property NINODE_WEAPON = "WEAPON" AutoReadOnly
string Property NINODE_SHIELD = "SHIELD" AutoReadOnly

;Dual Sheath Nodes
string Property NINODE_AXE_LEFT = "WeaponAxeLeft" AutoReadOnly
string Property NINODE_MACE_LEFT = "WeaponMaceLeft" AutoReadOnly
string Property NINODE_SWORD_LEFT = "WeaponSwordLeft" AutoReadOnly
string Property NINODE_DAGGER_LEFT = "WeaponDaggerLeft" AutoReadOnly
string Property NINODE_SHIELD_BACK = "ShieldBack" AutoReadOnly
string Property NINODE_STAFF = "WeaponStaff" AutoReadOnly
string Property NINODE_STAFF_LEFT = "WeaponStaffLeft" AutoReadOnly

;Magic Nodes (unused) (May needed in later version)
string Property NINODE_RIGHT_MAGIC = "NPC R MagicNode [RMag]" AutoReadOnly
string Property NINODE_LEFT_MAGIC = "NPC L MagicNode [LMag]" AutoReadOnly

;RaceMenuPluginWeapon Key
string Property RM_PLUGIN = "RSMPlugin" AutoReadOnly
string Property RMW_PLUGIN = "RMWPlugin" AutoReadOnly

;Mod Keys
string Property PWS_PLUGIN = "PWS_PLUGIN" AutoReadOnly
string Property PWS_RMPLUGIN = "PWS_RMPLUGIN" AutoReadOnly
string Property PWS_RMWEAPON = "PWS_RMWEAPON" AutoReadOnly

actor MySelf
int iSetDebug = 0

int Gender
bool Female
int leftHand = 0
int rightHand = 1

int rightHandType = 0
int leftHandType = 0

;Default so no Division through Zero
float defaultLeftHandScale = 0.8517
float defaultRightHandScale = 0.8517

float defaultWEAPONScale = 1.0
float defaultSHIELDScale = 1.0

float correctWEAPONScale = 1.0
float correctSHIELDScale = 1.0

Event OnInit()
	if ( iSetDebug == 1 )
		Debug.Notification( "Proper Weapon Scale has been initialized." )
	endif
    SetActorGender()
EndEvent

Event OnPlayerLoadGame()
	if ( iSetDebug == 1 )
		Debug.Notification( "Proper Weapon Scale has been loaded." )
	endif
    SetActorGender()
EndEvent

Function SetActorGender()
	MySelf = PlayerRef
	Gender = MySelf.GetActorBase().GetSex()
	if ( Gender == 1 )
		Female = Gender as bool
		defaultLeftHandScale = 0.8517
		defaultRightHandScale = 0.8517
	elseif ( Gender == 0 )
		Female = Gender as bool
		defaultLeftHandScale = 1.0
		defaultRightHandScale = 1.0
	endif
	if ( iSetDebug == 1 )
		Debug.Notification( "Getting gender for " + MySelf.GetActorBase().GetName() + "." )
		if ( Gender == 1 )
			Debug.Notification( MySelf.GetActorBase().GetName() + " is Female." )
		elseif ( Gender == 0 )
			Debug.Notification( MySelf.GetActorBase().GetName() + " is Male." )
		else
			Debug.Notification( MySelf.GetActorBase().GetName() + " is an unknown gender." )
		endif
	endif
EndFunction

Event OnObjectEquipped(Form akBaseObject, ObjectReference akReference)
	; Run Gender check.
	SetActorGender()
	if (Gender == 0 || Gender == 1)
		;Non Vanilla based skeleton and scaling tools compatibility
		
		float actualRightHandScale = NetImmerse.GetNodeScale(MySelf, NINODE_RIGHT_HAND, false)
		float actualLeftHandScale = NetImmerse.GetNodeScale(MySelf, NINODE_LEFT_HAND, false)
		
		;Division through Zero prevention
		if (actualLeftHandScale != 0.0 && actualRightHandScale != 0.0)
			defaultRightHandScale = actualRightHandScale
			defaultLeftHandScale = actualLeftHandScale
		endif
		
		correctWEAPONScale = 1/defaultRightHandScale
		correctSHIELDScale = 1/defaultLeftHandScale
		
		rightHandType = MySelf.GetEquippedItemType(rightHand)
		leftHandType = MySelf.GetEquippedItemType(leftHand)

		if (rightHandType == 0)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMWEAPON)
		elseif (rightHandType == 1)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRM(MySelf, Female, NINODE_SWORD), PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_SWORD), PWS_RMWEAPON)
		elseif (rightHandType == 2)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_DAGGER), PWS_RMWEAPON)
		elseif (rightHandType == 3)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRM(MySelf, Female, NINODE_AXE), PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_AXE), PWS_RMWEAPON)
		elseif (rightHandType == 4)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRM(MySelf, Female, NINODE_MACE), PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_MACE), PWS_RMWEAPON)
		elseif (rightHandType == 5)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRM(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMWEAPON)
		elseif (rightHandType == 6)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRM(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMWEAPON)
		elseif (rightHandType == 7)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMWEAPON)
		elseif (rightHandType == 8)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_STAFF), PWS_RMWEAPON)
		elseif (rightHandType == 9)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMWEAPON)
		elseif (rightHandType == 10)
			if (!SHIELD_SOLUTION_ALT)
				SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD_BACK, defaultLeftHandScale, PWS_PLUGIN)
				SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_PLUGIN)
			else
				SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			endif
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, GetNodeScaleRMW(MySelf, Female, NINODE_SHIELD_BACK), PWS_RMWEAPON)
		elseif (rightHandType == 11)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMWEAPON)
		elseif (rightHandType == 12)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMWEAPON)
		else
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, defaultWEAPONScale, PWS_RMWEAPON)
		endif

		if (leftHandType == 0)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMWEAPON)
		elseif (leftHandType == 1)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_SWORD_LEFT), PWS_RMWEAPON)
		elseif (leftHandType == 2)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_DAGGER_LEFT), PWS_RMWEAPON)
		elseif (leftHandType == 3)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_AXE_LEFT), PWS_RMWEAPON)
		elseif (leftHandType == 4)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_MACE_LEFT), PWS_RMWEAPON)
		elseif (leftHandType == 5)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRM(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMWEAPON)
		elseif (leftHandType == 6)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRM(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_WEAPON_BACK), PWS_RMWEAPON)
		elseif (leftHandType == 7)
			SetNodeScaleEXD(MySelf, Female, NINODE_WEAPON, correctWEAPONScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMWEAPON)
		elseif (leftHandType == 8)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_STAFF_LEFT), PWS_RMWEAPON)
		elseif (leftHandType == 9)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMWEAPON)
		elseif (leftHandType == 10)
			if (!SHIELD_SOLUTION_ALT)
				SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD_BACK, defaultLeftHandScale, PWS_PLUGIN)
				SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_PLUGIN)
			else
				SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, correctSHIELDScale, PWS_PLUGIN)
			endif
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, GetNodeScaleRMW(MySelf, Female, NINODE_SHIELD_BACK), PWS_RMWEAPON)
		elseif (leftHandType == 11)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMWEAPON)
		elseif (leftHandType == 12)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMWEAPON)
		else
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_PLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMPLUGIN)
			SetNodeScaleEXD(MySelf, Female, NINODE_SHIELD, defaultSHIELDScale, PWS_RMWEAPON)
		endif
	endif
EndEvent

Function SetNodeScaleEXD(Actor akActor, bool isFemale, string nodeName, float value, string modkey)
	If value != 1.0
		NiOverride.AddNodeTransformScale(akActor, false, isFemale, nodeName, modkey, value)
		if ( iSetDebug == 1 )
			if ( iSetDebug == 1 )
				Debug.Notification( MySelf.GetActorBase().GetName() + " has " + modkey + " Transformation (Scale: " + value + ") added.")
			endif
		endif
	Else
		NiOverride.RemoveNodeTransformScale(akActor, false, isFemale, nodeName, modkey)
		if ( iSetDebug == 1 )
			Debug.Notification( MySelf.GetActorBase().GetName() + " has " + modkey + " Transformation removed on" + " nodeName" + ".")
		endif
	Endif
	NiOverride.UpdateNodeTransform(akActor, false, isFemale, nodeName)
EndFunction

float Function GetNodeScaleRM(Actor akActor, bool isFemale, string nodeName)
	return NiOverride.GetNodeTransformScale(akActor, false, isFemale, nodeName, RMW_PLUGIN)
EndFunction

float Function GetNodeScaleRMW(Actor akActor, bool isFemale, string nodeName)
	return NiOverride.GetNodeTransformScale(akActor, false, isFemale, nodeName, RM_PLUGIN)
EndFunction