Scriptname SSLRTMain extends Quest

Event OnInit()
	RegisterForModEvent("HookAnimationStart", "AddRapeTrauma")
EndEvent

Event AddRapeTrauma(int tid, bool hasPlayer)
	GoToState("Busy")
	_AddRapeTraumaSub(tid, hasPlayer)
	GoToState("")
EndEvent

state Busy
	Event AddRapeTrauma(int tid, bool hasPlayer)

	EndEvent
endState

Function _AddRapeTraumaSub(int tid, bool hasPlayer)
	Actor Player = Game.GetPlayer()
	if (!hasPlayer || Player.GetActorBase().GetSex() == 0)
		return
	endif
	
	sslThreadController controller = SexLab.GetController(tid)
	if (controller.IsAggressive)
		if !(controller.IsVictim(Player))
			return
		endif
	else
		return
	endif
	
	Actor act = controller.Positions[1]
	if (act.GetActorBase().GetSex() != 0) ; not male
		return
	endif
	
	Perk[] perks = self._getPerks(act)
	Spell[] spells = self._getSpells(act)
	int numAggressors = controller.Positions.Length - 1
	
	self._addRapeTrauma(Player, perks, spells, numAggressors)
EndFunction

Function RemoveRapeTrauma(Actor victim)
	GoToState("Busy")
	float r = Utility.RandomFloat(0.0, 100.0)
	float v = SSLRTRemovePercent.GetValue()
	Debug.Trace("SLRT: RemoveRapeTrauma victim=" + victim + ": r=" + r + ": v=" + v)
	if (r <= v)
		Actor Player = Game.GetPlayer()
		Perk[] perks = self._getPerks(victim)
		Spell[] spells = self._getSpells(victim)
		
		if (player.HasPerk(perks[4]))
			self._downRapeTrauma(player, perks, spells, 4)
		elseif (player.HasPerk(perks[3]))
			self._downRapeTrauma(player, perks, spells, 3)
		elseif (player.HasPerk(perks[2]))
			self._downRapeTrauma(player, perks, spells, 2)
		elseif (player.HasPerk(perks[1]))
			self._downRapeTrauma(player, perks, spells, 1)
		elseif (player.HasPerk(perks[0]))
			self._downRapeTrauma(player, perks, spells, 0, true)
		endif
	endif
	GoToState("")
EndFunction

state Busy
	Function RemoveRapeTrauma(Actor victim)

	EndFunction
endState

Function _downRapeTrauma(Actor act, Perk[] perks, Spell[] spells, int level, bool allremove = false)
	if (allremove)
		debug.notification("$SLRTAllRemoveTrauma")
	else
		debug.notification("$SLRTRemoveTrauma")
	endif

	act.RemovePerk(perks[level])
	act.RemoveSpell(spells[level])
	if (level != 0)
		int x = level - 1
		act.AddSpell(spells[x], false)
	endif
	
	act.AddSpell(SSLRTRemoveTrauma, false)
	Utility.Wait(1.0)
	act.RemoveSpell(SSLRTRemoveTrauma)
EndFunction

Function _addRapeTrauma(Actor player, Perk[] perks, Spell[] spells, int numAggressors)
	Debug.Trace("SLRT: _addRapeTrauma numAggressors=" + numAggressors)
	bool newtrauma = false
	int currentLevel = -1
	
	if (player.HasPerk(perks[4]))
		return
	elseif (player.HasPerk(perks[3]))
		currentLevel = 3
	elseif (player.HasPerk(perks[2]))
		currentLevel = 2
	elseif (player.HasPerk(perks[1]))
		currentLevel = 1
	elseif (player.HasPerk(perks[0]))
		currentLevel = 0
	else ; no trauma
		currentLevel = -1
		newtrauma = true
	endif

	int perklevel = currentLevel + numAggressors
	if perklevel > 4
		perklevel = 4
	endif

	int i = currentLevel + 1
	while i <= perklevel
		player.AddPerk(perks[i])
		i += 1
	endWhile
	
	self._addSpells(player, perklevel, spells, newtrauma)
EndFunction

Function _addSpells(Actor player, int perklevel, Spell[] spells, bool newtrauma)
	if (newtrauma)
		debug.notification("$SLRTGetTrauma")
	else
		debug.notification("$SLRTAddTrauma")
	endif

	if (!newtrauma)
		int len = spells.Length
		while len > 0
			len -= 1
			player.RemoveSpell(spells[len])
		endwhile
	endif
	
	debug.trace("SLRT: " + perklevel)
	player.AddSpell(spells[perklevel], false)
EndFunction

Perk[] Function _getPerks(Actor act)
	if (act.HasKeyword(ActorTypeNPC))
		return MalePerks
	elseif (act.HasSpell(abDraugr) || act.HasSpell(abDraugrMagic))
		return DraugrPerks
	elseif (act.HasSpell(abFalmer))
		return FalmerPerks
	elseif (act.HasSpell(DLC2abRiekling))
		return RieklingPerks
	elseif (act.HasSpell(abTroll) || act.HasSpell(abTrollFrost))
		return TrollPerks
	elseif (act.HasKeyword(ActorTypeDwarven))
		return DwarvenPerks
	elseif (act.IsInFaction(PredatorFaction))
		return PredatorPerks
	else
		return CreaturePerks
	endif
EndFunction

Spell[] Function _getSpells(Actor act)
	if (act.HasKeyword(ActorTypeNPC))
		return MaleSpells
	elseif (act.HasSpell(abDraugr) || act.HasSpell(abDraugrMagic))
		return DraugrSpells
	elseif (act.HasSpell(abFalmer))
		return FalmerSpells
	elseif (act.HasSpell(DLC2abRiekling))
		return RieklingSpells
	elseif (act.HasSpell(abTroll) || act.HasSpell(abTrollFrost))
		return TrollSpells
	elseif (act.HasKeyword(ActorTypeDwarven))
		return DwarvenSpells
	elseif (act.IsInFaction(PredatorFaction))
		return PredatorSpells
	else
		return CreatureSpells
	endif
EndFunction


SexLabFramework Property SexLab Auto

Keyword Property ActorTypeNPC  Auto
Keyword Property ActorTypeDwarven  Auto
Faction Property PredatorFaction  Auto
Faction Property CreatureFaction  Auto  ; not use
Spell Property abDraugr Auto
Spell Property abDraugrMagic Auto
Spell Property abFalmer Auto
Spell Property DLC2abRiekling Auto
Spell Property abTroll Auto
Spell Property abTrollFrost Auto

Perk[] Property MalePerks  Auto
Perk[] Property DraugrPerks Auto
Perk[] Property FalmerPerks Auto
Perk[] Property RieklingPerks Auto
Perk[] Property TrollPerks Auto
Perk[] Property DwarvenPerks  Auto
Perk[] Property PredatorPerks  Auto
Perk[] Property CreaturePerks  Auto

SPELL[] Property MaleSpells  Auto
SPELL[] Property DraugrSpells Auto
SPELL[] Property FalmerSpells Auto
SPELL[] Property RieklingSpells Auto
SPELL[] Property TrollSpells Auto
SPELL[] Property DwarvenSpells  Auto
SPELL[] Property PredatorSpells  Auto
SPELL[] Property CreatureSpells  Auto
; SPELL[] Property PredetorSpells  Auto  ; fuck, miss spell
SPELL Property SSLRTRemoveTrauma  Auto

GlobalVariable Property SSLRTRemovePercent  Auto

