PGRData/Script/matrix/xcommon/XMath.lua

59 lines
No EOL
1.2 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local math = math
local mathFloor = math.floor
local mathRandom = math.random
XMath = XMath or {}
function XMath.RandByWeights(weights)
local weightSum = 0
for i = 1, #weights do
weightSum = weightSum + weights[i]
end
local rand = mathRandom(weightSum)
local curWeight = 0
for i = 1, #weights do
local weight = weights[i]
curWeight = curWeight + weight
if rand < curWeight then
return i
end
end
return #weights
end
function XMath.Clamp(value, min, max)
if value < min then
return min
end
if value > max then
return max
end
return value
end
--==============================--
--desc: 转换成整数,浮点数四舍五入
--==============================--
XMath.ToInt = function(val)
if not val then return end
return mathFloor(val + 0.5)
end
--==============================--
--desc: 转换成整数,浮点数向下取整数
--==============================--
XMath.ToMinInt = function(val)
if not val then return end
return mathFloor(val)
end
--==============================--
--desc: 最大整数与C#一致
--==============================--
XMath.IntMax = function()
return 2147483647
end