User:PROXiCiDE/MathUtil
< User:PROXiCiDE
Jump to navigation
Jump to search
Revision as of 20:46, 27 April 2014 by imported>PROXiCiDE (→Bit Code)
Non SKSE
Math Code
;---------------------------------------------------------------------------------[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
Bit Code
;---------------------------------------------------------------------------------[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 Manipulation Example
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"
Bit_SetBool(aiPerkCTX[PerkCTX_Bits],PerkCTX_Bit_SkillOverrideInit,True)
EndIf
Bit_SetBool(aiPerkCTX[PerkCTX_Bits], PerkCTX_Bit_Ordered, abOrdered)
Bit_SetBool(aiPerkCTX[PerkCTX_Bits], PerkCTX_Bit_PerkReset, abPerkReset)
Bit_SetBool(aiPerkCTX[PerkCTX_Bits], PerkCTX_Bit_SkillOverride, abSkillReqOverride)
aiPerkCTX[PerkCTX_PerkMult] = MakeDWord(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