Scriptname _00E_Theriantrophist_WolfAttributes extends Quest 
; script that manages the werewolf attributes, shows the current stats
; in the active effect window and resets them to the human values when the
; transformation ends

Actor Property PlayerREF Auto
MiscObject Property _00E_UpdateInventoryItem Auto
Weapon Property _00E_Theriantrophist_Claws Auto
Spell Property _00E_Theriantrophist_DisplayDamageSpell Auto
Spell Property _00E_Theriantrophist_DisplayHealthSpell Auto
Spell Property _00E_Theriantrophist_DisplayStaminaSpell Auto
Spell Property _00E_Theriantrophist_DisplayArmorSpell Auto
{this spell is not a pure display spell anymore, since it contains the shield effect that makes the wolf armor}
Spell Property _00E_Theriantrophist_DisplaySpeedMultSpell Auto

Float Property WerewolfBaseCarryCapacity = 200 AutoReadOnly Hidden
Float carryCapacityMod

; the health/stamina bonus (or malus) the wolf has in comparison to human form
Float healthMod
Float staminaMod

Float temporarySpeedMultMod
Float speedMult
Float humanSpeedMult

; the absolute damage resist/unarmed damage values the player has while in wolf form
Float damageResist
Float unarmedDamage
Float temporaryUnarmedDamageMod

Function SetInWolfForm(bool wolfForm)
	if wolfForm
		gotoState("WolfForm")
	else
		gotoState("")
	Endif
EndFunction

Function RemoveDisplaySpell(Spell displaySpell)
	PlayerREF.removeSpell(displaySpell)
EndFunction

Function UpdateDisplaySpell(Spell displaySpell, Float value)
	RemoveDisplaySpell(displaySpell)
Endfunction

Function ModWolfHealth(Float mod)
	healthMod += mod
	PlayerREF.ModActorValue("health", mod)
	UpdateDisplaySpell(_00E_Theriantrophist_DisplayHealthSpell, healthMod)
Endfunction

Function ModWolfStamina(Float mod)
	staminaMod += mod
	PlayerREF.ModActorValue("stamina", mod)
	UpdateDisplaySpell(_00E_Theriantrophist_DisplayStaminaSpell, staminaMod)
EndFunction

Float Function GetWolfUnarmedDamage()
	return unarmedDamage
EndFunction

Function ForceWolfUnarmedDmg(Float value)
	unarmedDamage = value
	
	_00E_Theriantrophist_Claws.setBaseDamage((unarmedDamage + temporaryUnarmedDamageMod) as int)
	UpdateDisplaySpell(_00E_Theriantrophist_DisplayDamageSpell, unarmedDamage)
Endfunction

Function SetWolfTempWolfUnarmedDamageMod(Float newTempMod)
	temporaryUnarmedDamageMod = newTempMod
	ForceWolfUnarmedDmg(unarmedDamage)
EndFunction

Function ModWolfUnarmedDmg(Float mod)
	ForceWolfUnarmedDmg(unarmedDamage + mod)
Endfunction

Function ForceWolfDamageResist(Float value)
	damageResist = value
	; the display spell now contains a shield effect
	UpdateDisplaySpell(_00E_Theriantrophist_DisplayArmorSpell, value)
Endfunction

Function ModWolfDamageResist(Float mod)
	ForceWolfDamageResist(damageResist + mod)
EndFunction

Function ForceWolfSpeedMult(Float value)
	speedMult = value
	PlayerREF.SetActorValue("speedMult", speedMult + temporarySpeedMultMod)
	updateSpeedMult()
	UpdateDisplaySpell(_00E_Theriantrophist_DisplaySpeedMultSpell, speedMult - 100)
EndFunction

Function SetTemporarilyWolfSpeedMultMod(Float value)
	temporarySpeedMultMod = value
	ForceWolfSpeedMult(speedMult)
EndFunction

Function ModWolfAV(String attribute, Float mod)
	if (attribute == "health")
		ModWolfHealth(mod)
	elseif (attribute == "stamina")
		ModWolfStamina(mod)
	elseif (attribute == "unarmedDamage")
		ModWolfUnarmedDmg(mod)
	elseif (attribute == "damageResist")
		ModWolfDamageResist(mod)
	elseif (attribute == "speedMult")
		ForceWolfSpeedMult(PlayerREF.GetActorValue("speedMult") + mod)
	EndIf
EndFunction

Function updateSpeedMult()
	PlayerREF.additem(_00E_UpdateInventoryItem, 1, true)
	PlayerREF.removeItem(_00E_UpdateInventoryItem, 1, true)
Endfunction
	
Function renewNotPersistentStats()
EndFunction

Function updateAfterRaceSwitch(bool toWolf)
	ForceWolfSpeedMult(speedMult)
	if (toWolf)
		PlayerREF.ModActorValue("CarryWeight", carryCapacityMod)
	else
		PlayerREF.ModActorValue("CarryWeight", -carryCapacityMod)
	Endif
EndFunction

State WolfForm

	Event OnBeginState()
		temporaryUnarmedDamageMod = 0
		unarmedDamage = 0
		damageResist = 0
		healthMod = 0
		staminaMod = 0
		carryCapacityMod = PlayerREF.GetActorValue("CarryWeight") - WerewolfBaseCarryCapacity
		
		humanSpeedMult = PlayerREF.GetActorValue("speedMult")
		; If the player is under the influence of a slowdown magic effect, take the player's normal speedMult from Fame AV. See FrostSlowFix.psc
		Float fFame = PlayerREF.GetBaseActorValue("Fame")
		If fFame > humanSpeedMult
			humanSpeedMult = fFame
		EndIf
		
		speedMult = humanSpeedMult
		temporarySpeedMultMod = 0
		renewNotPersistentStats()
	Endevent
	
	Event OnEndState()
		if (healthMod > PlayerREF.GetActorValue("health"))
			PlayerREF.RestoreActorValue("health", healthMod - PlayerREF.GetActorValue("health") + 1)
		Endif
		PlayerREF.ModActorValue("health", -healthMod)
		PlayerREF.ModActorValue("stamina", -staminaMod)
		PlayerREF.SetActorValue("speedMult", humanSpeedMult)
		temporarySpeedMultMod = 0
		updateSpeedMult()
		
		RemoveDisplaySpell(_00E_Theriantrophist_DisplayDamageSpell)
		RemoveDisplaySpell(_00E_Theriantrophist_DisplayHealthSpell)
		RemoveDisplaySpell(_00E_Theriantrophist_DisplayStaminaSpell)
		RemoveDisplaySpell(_00E_Theriantrophist_DisplayArmorSpell)
		RemoveDisplaySpell(_00E_Theriantrophist_DisplaySpeedMultSpell)
	Endevent
	
	Function renewNotPersistentStats()
		Utility.wait(0.5)
		PlayerREF.SetActorValue("speedMult", speedMult + temporarySpeedMultMod)
		updateSpeedMult()
		ForceWolfDamageResist(damageResist)
		ForceWolfUnarmedDmg(unarmedDamage)
		UpdateDisplaySpell(_00E_Theriantrophist_DisplaySpeedMultSpell, speedMult)
		UpdateDisplaySpell(_00E_Theriantrophist_DisplayHealthSpell, healthMod)
		UpdateDisplaySpell(_00E_Theriantrophist_DisplayStaminaSpell, staminaMod)
	EndFunction

	Function UpdateDisplaySpell(Spell displaySpell, Float value)
		displaySpell.setNthEffectMagnitude(0, value)
		PlayerREF.removeSpell(displaySpell)
		PlayerREF.addSpell(displaySpell, false)
	Endfunction
EndState