🌟 | 現在、 鉄壁ヘッドショットには対応済みです。 鉄壁HSは通常HSと同じダメージになります。LMG及びDMR、チャージライフル、ハンマーポイント弾を除き、すべてのダメージ値が一致していることを確認しています。 |
「モジュール:Utility/Scale」の版間の差分
ナビゲーションに移動
検索に移動
(関数名を table.mapValue から table.MapValues に変更による改変) |
(ticksを取得するための関数を追加) |
||
2行目: | 2行目: | ||
local table = require('Module:TableExtensions') | local table = require('Module:TableExtensions') | ||
local ninf = -math.huge | |||
local pinf = math.huge | |||
function math.isInf(v) | |||
return v == pinf or v == ninf | |||
end | |||
function math.isNaN(v) | function math.isNaN(v) | ||
--return tostring(v) == '-nan' | --return tostring(v) == '-nan' | ||
124行目: | 129行目: | ||
return -(10 ^ -power) / nice | return -(10 ^ -power) / nice | ||
end | end | ||
end | |||
-- function: ticks | |||
local function ticks(start, stop, count) | |||
if start == stop and count > 0 then | |||
return { start } | |||
end | |||
local reverse = start > stop | |||
if reverse then | |||
start, stop = stop, start | |||
end | |||
local step = tickIncrement(start, stop, count) | |||
if step == 0 or math.isInf(step) then | |||
return {} | |||
end | |||
local ticks = {} | |||
if step > 0 then | |||
local r0 = math.floor(start / step) | |||
local r1 = math.ceil (stop / step) | |||
if r0 * step <= start then | |||
r0 = r0 + 1 | |||
end | |||
if r1 * step >= stop then | |||
r1 = r1 - 1 | |||
end | |||
for i = 0, r1 - r0 do | |||
ticks[i + 1] = (r0 + i) * step | |||
end | |||
else | |||
step = -step | |||
local r0 = math.ceil (start * step) | |||
local r1 = math.floor(stop * step) | |||
if r0 / step <= start then | |||
r0 = r0 + 1 | |||
end | |||
if r1 / step >= stop then | |||
r1 = r1 - 1 | |||
end | |||
for i = 0, r1 - r0 do | |||
ticks[i + 1] = (r0 + i) / step | |||
end | |||
end | |||
if reverse then | |||
table.reverse(ticks) | |||
end | |||
return ticks | |||
end | end | ||
247行目: | 305行目: | ||
local obj = __Scale__Continuous.__new(identity, identity) | local obj = __Scale__Continuous.__new(identity, identity) | ||
return setmetatable(obj, { __index = __Scale__Linear }) | return setmetatable(obj, { __index = __Scale__Linear }) | ||
end | |||
-- [Function] Get ticks | |||
function __Scale__Linear:ticks(count) | |||
libraryUtil.checkType('nice', 1, count, 'number', true) | |||
count = count or 10 | |||
return ticks(self.domain[1], self.domain[#self.domain], count) | |||
end | end | ||
2021年8月21日 (土) 07:10時点における版
このモジュールについての説明文ページを モジュール:Utility/Scale/doc に作成できます
local libraryUtil = require('libraryUtil') local table = require('Module:TableExtensions') local ninf = -math.huge local pinf = math.huge function math.isInf(v) return v == pinf or v == ninf end 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 -- function: ticks local function ticks(start, stop, count) if start == stop and count > 0 then return { start } end local reverse = start > stop if reverse then start, stop = stop, start end local step = tickIncrement(start, stop, count) if step == 0 or math.isInf(step) then return {} end local ticks = {} if step > 0 then local r0 = math.floor(start / step) local r1 = math.ceil (stop / step) if r0 * step <= start then r0 = r0 + 1 end if r1 * step >= stop then r1 = r1 - 1 end for i = 0, r1 - r0 do ticks[i + 1] = (r0 + i) * step end else step = -step local r0 = math.ceil (start * step) local r1 = math.floor(stop * step) if r0 / step <= start then r0 = r0 + 1 end if r1 / step >= stop then r1 = r1 - 1 end for i = 0, r1 - r0 do ticks[i + 1] = (r0 + i) / step end end if reverse then table.reverse(ticks) end return ticks 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.mapValues(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.mapValues(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.mapValues(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] Get ticks function __Scale__Linear:ticks(count) libraryUtil.checkType('nice', 1, count, 'number', true) count = count or 10 return ticks(self.domain[1], self.domain[#self.domain], count) 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