| 🌟 | 現在、 鉄壁HSは通常HSと同じダメージになります。LMG及びDMR、チャージライフル、ハンマーポイント弾を除き、すべてのダメージ値が一致していることを確認しています。 |
「モジュール:Utility/Scale」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「local libraryUtil = require('libraryUtil') local table = require('Module:TableExtensions') function math.isNaN(v) --return tostring(v) == '-nan' return v ~= v en…」) |
(clampの設定に対応) |
||
| 176行目: | 176行目: | ||
self.range = table.map(newRange, asNumber) | self.range = table.map(newRange, asNumber) | ||
self:_rescale() | |||
return self | |||
end | |||
-- [Property] Set clamp | |||
function __Scale__Continuous:setClamp(newClamp) | |||
libraryUtil.checkType('setClamp', 1, newClamp, 'boolean') | |||
self._clamp = newClamp or identity | |||
self:_rescale() | self:_rescale() | ||
return self | return self | ||
| 184行目: | 193行目: | ||
local n = math.min(#self.domain, #self.range) | local n = math.min(#self.domain, #self.range) | ||
if self._clamp ~= identity then | if self._clamp ~= identity then | ||
self._clamp = clamper(self.domain[ | self._clamp = clamper(self.domain[1], self.domain[n]) | ||
end | end | ||
if n > 2 then | if n > 2 then | ||
| 261行目: | 270行目: | ||
self.domain[i0] = start | self.domain[i0] = start | ||
self.domain[i1] = stop | self.domain[i1] = stop | ||
self:_rescale() | |||
break | break | ||
elseif step > 0 then | elseif step > 0 then | ||
2021年8月20日 (金) 18:20時点における版
このモジュールについての説明文ページを モジュール:Utility/Scale/doc に作成できます
local libraryUtil = require('libraryUtil')
local table = require('Module:TableExtensions')
function math.isNaN(v)
--return tostring(v) == '-nan'
return v ~= v
end
local log10e = 1 / math.log(10)
function math.log10(x)
return math.log(x) * log10e
end
-- ============
-- Define utils
-- ============
local unit = { 0, 1 }
local function identity(num)
return num
end
-- function: asNumber - as number if possible
local function asNumber(value)
local typename = type(value)
if typename == 'number' then
return value
elseif typename == 'string' then
return tonumber(value)
else
error('The "value" type must be "number" or "string".')
end
end
-- function: constant
local function constant(num)
return function()
return num
end
end
-- function: normalize
local function normalize(a, b)
b = b - a
if math.isNaN(b) then
return constant(b)
elseif b == 0 then
return constant(0.5)
else
return function(x)
return (x - a) / b
end
end
end
-- function: interpolateValue
local function interpolateValue(a, b)
return function(t)
return a + (b - a) * t
end
end
-- function: clamper - generate clamp function
local function clamper(a, b)
if a > b then
a, b = b, a
end
return function(x)
return math.max(a, math.min(b, x))
end
end
-- function: bimap
local function bimap(domain, range, interpolate)
local d1 = domain[1]
local d2 = domain[2]
local r1 = range[1]
local r2 = range[2]
if d2 < d1 then
d1 = normalize(d2, d1)
r1 = interpolate(r2, r1)
else
d1 = normalize(d1, d2)
r1 = interpolate(r1, r2)
end
return function(x)
return r1(d1(x))
end
end
-- function: tickIncrement
local e10 = math.sqrt(50)
local e5 = math.sqrt(10)
local e2h = math.sqrt(5)
local e2 = math.sqrt(2)
local function tickIncrement(start, stop, count)
local step = (stop - start) / math.max(0, count)
local power = math.floor(math.log10(step))
local error = step / (10 ^ power)
local nice
if power >= 0 then
if error >= e10 then
nice = 10
elseif error >= e5 then
nice = 5
elseif error >= e2h then
nice = 2.5
elseif error >= e2 then
nice = 2
else
nice = 1
end
return nice * (10 ^ power)
else
if error >= e10 then
nice = 10
elseif error >= e5 then
nice = 5
elseif error >= e2h then
nice = 2.5
elseif error >= e2 then
nice = 2
else
nice = 1
end
return -(10 ^ -power) / nice
end
end
-- ================
-- namespace: scale
-- ================
local scale = {}
-- ================
-- type: Continuous
-- ================
local __Scale__ContinuousType = 'continuous'
local __Scale__Continuous = {
__typename = __Scale__ContinuousType,
}
-- [Constructor]
function __Scale__Continuous.__new(transform, untransform)
local obj = {
typename = __Scale__ContinuousType,
domain = unit,
range = { 0, 100 },
_clamp = identity,
_piecewise = bimap,
_interpolate = interpolateValue,
_transform = transform,
_untransform = untransform,
_input = nil,
_output = nil,
}
return setmetatable(obj, { __index = __Scale__Continuous })
end
-- [Property] Set domain
function __Scale__Continuous:setDomain(newDomain)
libraryUtil.checkType('setDomain', 1, newDomain, 'table')
if #newDomain < 2 then
error('The "domain" table size must be at least 2.')
end
self.domain = table.mapValue(newDomain, asNumber)
self:_rescale()
return self
end
-- [Property] Set range
function __Scale__Continuous:setRange(newRange)
libraryUtil.checkType('setRange', 1, newRange, 'table')
if #newRange < 2 then
error('The "range" table size must be at least 2.')
end
self.range = table.map(newRange, asNumber)
self:_rescale()
return self
end
-- [Property] Set clamp
function __Scale__Continuous:setClamp(newClamp)
libraryUtil.checkType('setClamp', 1, newClamp, 'boolean')
self._clamp = newClamp or identity
self:_rescale()
return self
end
-- [Function] Rescale
function __Scale__Continuous:_rescale()
local n = math.min(#self.domain, #self.range)
if self._clamp ~= identity then
self._clamp = clamper(self.domain[1], self.domain[n])
end
if n > 2 then
error('The multiple values of the domain aren\'t currently supported.')
else
self._piecewise = bimap
end
self._input = nil
self._output = nil
end
-- [Function] Scale
function __Scale__Continuous:scale(x)
libraryUtil.checkType('scale', 1, x, 'number')
if math.isNaN(x) then
return nil
else
if not self._output then
self._output = self._piecewise(
table.mapValue(self.domain, self._transform),
self.range,
self._interpolate)
end
return self._output(self._transform(self._clamp(x)))
end
end
-- [Function] Invert
function __Scale__Continuous:invert(y)
libraryUtil.checkType('invert', 1, y, 'number')
if not self._input then
self._input = self._piecewise(
self.range,
table.mapValue(self.domain, self._transform),
interpolateValue)
end
return self._clamp(self._untransform(self._input(y)))
end
-- =============================
-- type: LinearScale: Continuous
-- =============================
local __Scale__LinearType = 'linear'
local __Scale__Linear = {
__typename = __Scale__LinearType,
}
setmetatable(__Scale__Linear, { __index = __Scale__Continuous })
-- [Constructor]
function __Scale__Linear.new()
local obj = __Scale__Continuous.__new(identity, identity)
return setmetatable(obj, { __index = __Scale__Linear })
end
-- [Function] Nice
function __Scale__Linear:nice(count)
libraryUtil.checkType('nice', 1, count, 'number', true)
count = count or 10
local i0 = 1
local i1 = #self.domain
local start = self.domain[i0]
local stop = self.domain[i1]
if start > stop then
start, stop = stop, start
i0, i1 = i1, i0
end
local prestep, step
local maxIter = 5
while maxIter > 0 do
step = tickIncrement(start, stop, count)
if step == prestep then
self.domain[i0] = start
self.domain[i1] = stop
self:_rescale()
break
elseif step > 0 then
start = math.floor(start / step) * step
stop = math.ceil (stop / step) * step
elseif step < 0 then
start = math.ceil (start * step) / step
stop = math.floor(stop * step) / step
else
break
end
prestep = step
maxIter = maxIter - 1
end
return self
end
-- [Export]
scale.LinearScale = __Scale__Linear
return scale