Difference between revisions of "User:PROXiCiDE/MathUtil"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>PROXiCiDE
imported>PROXiCiDE
(Bit Manipulation requires SKSE)
Line 114: Line 114:


;---------------------------------------------------------------------------------[Bit Manipulation]
;---------------------------------------------------------------------------------[Bit Manipulation]
; Requires SKSE
; Bits are range of 1, 30
; Bits are range of 1, 30
;                      1............................30
;                      1............................30

Revision as of 00:01, 27 April 2014

;---------------------------------------------------------------------------------[Math]
; Math Functions
; 
; Version History
; 0.1 - Initial Release
; 0.2 - Optimizations by Cipscis
;	IsNumberEven ,IsNumberOdd ,InRange
; 0.3 - Bit Set/Get/Clear

;---------------------------------------------------------------------------------[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

;---------------------------------------------------------------------------------[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)
	aiIndex = ClampInt(aiIndex, 1, 30)
	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