User:PROXiCiDE/MathUtil
Jump to navigation
Jump to search
Non SKSE[edit | edit source]
Math Code[edit | edit source]
;---------------------------------------------------------------------------------[Math]
; Math Functions
;
; Version History
; 0.1 - Initial Release
; 0.2 - Optimizations by Cipscis
; IsNumberEven ,IsNumberOdd ,InRange
; 0.3 - Bit Set/Xor/SetBool/Get/Clear/ClearFrom
;---------------------------------------------------------------------------------[Min]
; MinInt() / MinFloat()
; Returns the smallest of X and Y
; If X is greater than Y, then Y is returned
; If both are equivalent X is returned
Int Function MinInt(Int x,Int y)
If x < y
Return x
Else
Return y
EndIf
EndFunction
Float Function MinFloat(Float x,Float y)
If x < y
Return x
Else
Return y
EndIf
EndFunction
;---------------------------------------------------------------------------------[Max]
; MaxInt() / MaxFloat()
; Returns the largest of X and Y
; If X is lesser than Y, then Y is returned
; If both are equivalent X is returned
Int Function MaxInt(Int x,Int y)
If x > y
Return x
Else
Return y
EndIf
EndFunction
Float Function MaxFloat(Float x,Float y)
If x > y
Return x
Else
Return y
EndIf
EndFunction
;---------------------------------------------------------------------------------[IsNumberEven]
; Is the Number a Even Number?
; 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
Bool Function IsNumberEven (Int num)
If Math.Floor(num % 2)
Return False
Else
Return True
EndIf
EndFunction
;---------------------------------------------------------------------------------[IsNumberOdd]
; Is the Number a Odd Number?
; 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31
Bool Function IsNumberOdd (Int num)
If Math.Floor(num % 2)
Return True
Else
Return False
EndIf
EndFunction
;---------------------------------------------------------------------------------[Clamp]
; ClampInt() / ClampFloat()
; Clamps a Value with-in a given Min and Max Range
; If X is lesser than Min , Results = Min
; If X is greater than Max , Results = Max
; Otherwise Return X
Float Function ClampFloat(Float x,Float min,Float max)
Return MinFloat(MaxFloat(x,min),max)
EndFunction
Int Function ClampInt(Int x,Int min,Int max)
Return MinInt(MaxInt(x,min),max)
EndFunction
;---------------------------------------------------------------------------------[InRange]
; Checks to see if Value is with-in a Range of Min and Max
; If X is greater than Max, Returns False
; If X is lesser than Min, Returns False
; Otherwise return True
Bool Function InRange(Float x,Float min,Float max)
Return (min <= x && x <= max)
EndFunction
;---------------------------------------------------------------------------------[Round]
; Float : afValue - number to round
; Int : iMult - Multiplier default is 10
; Int : iMode - Mode type for rounding
; 0 - Default
; 1 - Up
; 2 - Down
Int Function Round(Float afValue, Int iMult = 10, Int iMode = 0)
If iMode == 1 ; Round Up
Return Math.Ceiling(afValue / iMult) * iMult
ElseIf iMode == 2; Round Down
Return Math.Floor(afValue / iMult) * iMult
Else
Return Math.Floor((afValue * iMult) + 0.5) / iMult
EndIf
EndFunction
Requires SKSE[edit | edit source]
Bit Code[edit | edit source]
;---------------------------------------------------------------------------------[Bit Manipulation]
; Requires SKSE
; Bits are range of 1, 30
; 1............................30
; 000000000000000000000000000000
;
; i = Bit_Set(0,2) = 010000000000000000000000000000
; i = Bit_Set(i,4) = 010100000000000000000000000000
; i = Bit_Set(i,1) = 110100000000000000000000000000
; i = Bit_Set(i,30) = 110100000000000000000000000001
; i = Bit_Xor(i,30) = 110100000000000000000000000000
; i = Bit_Clear(i,2) = 101111111111111111111111111111
; i = Bit_Clear(i,30) = 101111111111111111111111111110
; i = Bit_ClearFrom(i,15) = 101111111111110000000000000000
; i = Bit_Xor(i,30) = 101111111111110000000000000001
; i = Bit_ClearFrom(i,4) = 101000000000000000000000000000
; i = Bit_Set(i,5) = 101010000000000000000000000000
Int Function Bit_MakeByte(Int aiIndex = 1)
Int iPos = aiIndex % 30
Return Math.LeftShift(1, iPos)
EndFunction
Int Function Bit_Set(Int aiValue, Int aiIndex = 1)
Return Math.LogicalOr(aiValue,Bit_MakeByte(aiIndex))
EndFunction
Int Function Bit_Xor(Int aiValue, Int aiIndex = 1)
Return Math.LogicalXor(aiValue,Bit_MakeByte(aiIndex))
EndFunction
Int Function Bit_SetBool(Int aiValue, Int aiIndex = 1, Bool abValue)
If abValue
Bit_Set(aiValue,aiIndex)
Else
Bit_Clear(aiValue,aiIndex)
EndIf
EndFunction
Bool Function Bit_Get(Int aiValue, Int aiIndex = 1)
Return Math.LogicalAnd(aiValue,Bit_MakeByte(aiIndex)) != 0
EndFunction
Int Function Bit_Clear(Int aiValue, Int aiIndex = 1)
Return Math.LogicalAnd(aiValue,Math.LogicalNot(Bit_MakeByte(aiIndex)))
EndFunction
Int Function Bit_ClearFrom(Int aiValue, Int aiIndex = 1)
aiIndex = ClampInt(aiIndex, 1, 30)
If aiIndex > 1 && aiIndex < 30
Int iPos = aiIndex
While (iPos <= 30)
aiValue = Bit_Clear(aiValue,iPos)
iPos += 1
EndWhile
Return aiValue
Else
Return 0
EndIf
EndFunction
Bit Array[edit | edit source]
Bit Array supports a maximum of 512 Bits
; int[] bits = BitArray_New(256)
; BitArray_Set(bits,128)
; bool bResult = BitArray_Get(bits,128)
;
; BitArray_Xor(bits,200)
; BitArray_Clear(bits,128)
; BitArray_Xor(bits,200)
Int[] Function BitArray_New(Int aiSize)
Int iSize = Math.Ceiling(ClampInt(aiSize,30,512) / 30)
Int[] iResults = Bit_CreateArray(iSize)
iResults[0] = aiSize
Int i = 1
While (i < iResults.Length)
iResults[i] = 0
i += 1
EndWhile
Return iResults
EndFunction
Function BitArray_Set(Int[] aiBitCTX, Int aiIndex)
Int iIndex = Math.Ceiling(aiIndex / 30)
aiBitCTX[iIndex] = Bit_Set(aiBitCTX[iIndex], aiIndex)
EndFunction
Bool Function BitArray_Get(Int[] aiBitCTX, Int aiIndex)
Int iIndex = Math.Ceiling(aiIndex / 30)
Return Bit_Get(aiBitCTX[iIndex], aiIndex)
EndFunction
Function BitArray_Xor(Int[] aiBitCTX, Int aiIndex)
Int iIndex = Math.Ceiling(aiIndex / 30)
aiBitCTX[iIndex] = Bit_Xor(aiBitCTX[iIndex], aiIndex)
EndFunction
Function BitArray_Clear(Int[] aiBitCTX, Int aiIndex)
Int iIndex = Math.Ceiling(aiIndex / 30)
aiBitCTX[iIndex] = Bit_Clear(aiBitCTX[iIndex], aiIndex)
EndFunction
;Helper function
;Array[0] is Reserved for Bit Size
Int[] Function Bit_CreateArray(Int aiSize)
If aiSize == 1
Return New Int[2]
ElseIf aiSize == 2
Return New Int[3]
ElseIf aiSize == 3
Return New Int[4]
ElseIf aiSize == 4
Return New Int[5]
ElseIf aiSize == 5
Return New Int[6]
ElseIf aiSize == 6
Return New Int[7]
ElseIf aiSize == 7
Return New Int[8]
ElseIf aiSize == 8
Return New Int[9]
ElseIf aiSize == 9
Return New Int[10]
ElseIf aiSize == 10
Return New Int[11]
ElseIf aiSize == 11
Return New Int[12]
ElseIf aiSize == 12
Return New Int[13]
ElseIf aiSize == 13
Return New Int[14]
ElseIf aiSize == 14
Return New Int[15]
ElseIf aiSize == 15
Return New Int[16]
ElseIf aiSize == 16
Return New Int[17]
ElseIf aiSize == 17
Return New Int[18]
ElseIf aiSize == 18
Return New Int[19]
EndIf
EndFunction
Bit Manipulation Example[edit | edit source]
Following Example is incomplete code and only serves as a skeletal example for Bit Manipulation methods
Int PerkCTX_Bits = 0
;Perk Bits
Int PerkCTX_Bit_Ordered = 1
Int PerkCTX_Bit_PerkReset = 2
Int PerkCTX_Bit_SkillOverrideInit = 3
Int PerkCTX_Bit_SkillOverride = 4
Function PerkUI_Init(Int[] aiPerkCTX, Bool abOrdered = True, Bool abPerkReset = True, String asActorValue = "", Int aiPerkMult = 20, Int aiPerkRankMult = 20, Bool abSkillReqOverride = False)
If !Bit_Get(aiPerkCTX[PerkCTX_Bits],PerkCTX_Bit_SkillOverrideInit)
_sSkillName = New String[18]
;Combat
_sSkillName[0] = "Marksman"
_sSkillName[1] = "Block"
_sSkillName[2] = "HeavyArmor"
_sSkillName[3] = "OneHanded"
_sSkillName[4] = "Smithing"
_sSkillName[5] = "TwoHanded"
;Magic
_sSkillName[6] = "Alteration"
_sSkillName[7] = "Conjuration"
_sSkillName[8] = "Destruction"
_sSkillName[9] = "Enchanting"
_sSkillName[10] = "Illusion"
_sSkillName[11] = "Restoration"
;Stealth
_sSkillName[12] = "Alchemy"
_sSkillName[13] = "LightArmor"
_sSkillName[14] = "Lockpicking"
_sSkillName[15] = "Pickpocket"
_sSkillName[16] = "Sneak"
_sSkillName[17] = "Speechcraft"
aiPerkCTX[PerkCTX_Bits] = Bit_SetBool(aiPerkCTX[PerkCTX_Bits],PerkCTX_Bit_SkillOverrideInit,True)
EndIf
aiPerkCTX[PerkCTX_Bits] = Bit_SetBool(aiPerkCTX[PerkCTX_Bits], PerkCTX_Bit_Ordered, abOrdered)
aiPerkCTX[PerkCTX_Bits] = Bit_SetBool(aiPerkCTX[PerkCTX_Bits], PerkCTX_Bit_PerkReset, abPerkReset)
aiPerkCTX[PerkCTX_Bits] = Bit_SetBool(aiPerkCTX[PerkCTX_Bits], PerkCTX_Bit_SkillOverride, abSkillReqOverride)
aiPerkCTX[PerkCTX_PerkMult] = MakeWord(aiPerkMult,aiPerkRankMult)
aiPerkCTX[PerkCTX_PerkReserved] = 0 ; Reserved
aiPerkCTX[PerkCTX_PerkSkillAttribute] = PerkUI_SetSkillNameToIndex(asActorValue)
EndFunction
Function PerkUI_ApplyByFormList(FormList akList, Int[] aiPerkCTX, Bool abAddPerks = True, Int[] aiSkillOverride)
If akList == None
Return
EndIf
Bool abOrdered = Bit_Get(aiPerkCTX[PerkCTX_Bits],PerkCTX_Bit_Ordered)
Bool abPerkReset = Bit_Get(aiPerkCTX[PerkCTX_Bits],PerkCTX_Bit_PerkReset)
...
... Rest of Code
...
EndFunction