User:PROXiCiDE/PerkUtil

From the CreationKit Wiki
Jump to navigation Jump to search

Requires: SKSE 1.70 +

;---------------------------------------------------------------------------------[PerkGetRankInfo]
; Returns array
; 	[0] = The total number of ranks the perk contains, is always >= 1
; 	[1] = The number of ranks that have been learned
;
; int[] iNumRanks = PerkGetRankInfo(Overdraw00)
; 	iNumRanks[5, 0]
Int[] Function PerkGetRankInfo(Perk akPerk)
	Int[] iResults
	
	If akPerk == None
		Return iResults
	EndIf
	
	iResults = New Int[2]
	
	Int iNumRanks = 1
	Int iNumRanksLearned = 0
	
	If akPerk != None
		
		If PlayerRef.HasPerk(akPerk)
			iNumRanksLearned += 1
		EndIf
		
		Perk pNext = akPerk.GetNextPerk()
		While pNext != None
			If PlayerRef.HasPerk(pNext)
				iNumRanksLearned += 1
			EndIf
			
			iNumRanks += 1
			pNext = pNext.GetNextPerk()
		EndWhile
	EndIf
	
	iResults[0] = iNumRanks
	iResults[1] = iNumRanksLearned
	
	Return iResults
EndFunction

;---------------------------------------------------------------------------------[PerkSelectRank]
; Selects a rank from a perk
; Must be used on the initial Perk, Overdraw00 as example
; PerkSelectRank(Overdraw00, 2) == Overdraw20
; PerkSelectRank(Overdraw00, 5) == Overdraw80
Perk Function PerkSelectRank(Perk akPerk,Int aiRank = 1)
	If akPerk != None
		Int[] iNumRanks = PerkGetRankInfo(akPerk)
		
		If aiRank > iNumRanks[0]
			aiRank = iNumRanks[0]
		EndIf
		
		If aiRank < 2
			Return akPerk
		EndIf
		
		Int i = 2
		Perk pNext = akPerk.GetNextPerk()
		While (i <= aiRank) && pNext != None
			If i == aiRank
				Return pNext
			EndIf
			pNext = pNext.GetNextPerk()
			i += 1
		EndWhile
	EndIf
	Return None
EndFunction

;---------------------------------------------------------------------------------[PerkRemoveRanked]
; Removes all perk(s) learned from the specific perk
; Example
; PerkRemoveRanked(Overdraw00) will remove all ranks if they are learned
; PerkRemoveRanked(Overdraw20) will only remove all ranks Overdraw20 >= Learned, Overdraw00 will still be present
; because we skipped that rank
Int Function PerkRemoveRanked(Perk akPerk)
	Int iResults = 0
	If akPerk != None
		If PlayerRef.HasPerk(akPerk)
			PlayerRef.RemovePerk(akPerk)
			iResults += 1
		EndIf
		
		Perk pNext = akPerk.GetNextPerk()
		While pNext != None
			If PlayerRef.HasPerk(pNext)
				PlayerRef.RemovePerk(pNext)
				iResults += 1
			EndIf
			pNext = pNext.GetNextPerk()
		EndWhile
	EndIf
	Return iResults
EndFunction