Module:Failure rate table

From CrawlWiki
Revision as of 03:54, 1 July 2015 by Edsrzf (talk | contribs) (Edsrzf moved page Module:Success rate table to Module:Failure rate table without leaving a redirect)
Jump to: navigation, search

Documentation for this module may be created at Module:Failure rate table/doc

local table = require("Module:Calculated table")

local p = {}

-- translated from spl-cast.cc
local function tetrahedral_number(n)
  return math.floor(n * (n+1) * (n+2) / 6)
end

-- translated from spl-cast.cc
local function true_fail_rate(raw_fail)
  local outcomes = 101 * 101 * 100
  local target = raw_fail * 3
  if target <= 100 then
    return tetrahedral_number(target) / outcomes
  elseif target <= 200 then
    return (tetrahedral_number(target) - 2 * tetrahedral_number(target - 101) - tetrahedral_number(target - 100)) / outcomes
  end
  return (outcomes - tetrahedral_number(300 - target)) / outcomes
end

local function success_func(base_fail, scale)
  return function(env)
    local skill = env.x
    local fail = base_fail - scale*skill
    if fail <= 0 then
      return 0
    end
    return true_fail_rate(base_fail - scale*skill)
  end
end

local function format(n)
  return ("%.2f%%"):format(100*n)
end

function p.table(frame)
  return table._table(frame.args[1], 27, {format = format}, "Failure rate(%)", success_func(frame.args[2], frame.args[3]))
end

return p