Модуль:Chemistry Lookup: различия между версиями

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
Строка 1: Строка 1:
local prototypes = mw.loadData("Module:Chemistry Lookup/data")
local prototypes = mw.loadData("Module:Chemistry Lookup/data")
local p = {}
local p = {}
p.chem = prototypes.chem
p.chem = prototypes.chem
Строка 5: Строка 6:
p.groupDirection = prototypes.groupDirection
p.groupDirection = prototypes.groupDirection


local function tablelength(t)
-- Обработка ошибок на nil для frame
  local count = 0
local function check_frame(frame)
  for _ in pairs(t) do count = count + 1 end
    if frame == nil then
  return count
        error("frame is nil!")
end
     end
 
local function strsplit(inputstr, separator)
  separator = separator or ","
  local t = {}
  for str in string.gmatch(inputstr, "([^"..separator.."]+)") do
    table.insert(t, str)
  end
  return t
end
 
local function buildConditionString(conds, frame)
  if not conds then return "" end
  local out = {}
  for _, cond in pairs(conds) do
    table.insert(out, p.gencond(cond, frame))
  end
  return table.concat(out, ", and ")
end
 
local function buildEffectString(effects, frame)
  local out = {}
  for _, effect in pairs(effects) do
     table.insert(out, p.geneffect(effect, frame))
  end
  return table.concat(out, "\n")
end
 
-- Общая функция для шаблонов
local function buildTemplate(title, args)
  return frame:expandTemplate{ title = title, args = args }
end
end


function p.readscalar(frame)
function p.readscalar(frame)
  return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
    check_frame(frame)
    return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
end
end


function p.readscalarreact(frame)
function p.readscalarreact(frame)
  local react = p.react[frame.args[1]][frame.args[2]]
    check_frame(frame)
  return react and mw.text.nowiki(react) or ""
    if p.react[frame.args[1]][frame.args[2]] ~= nil then
        return mw.text.nowiki(p.react[frame.args[1]][frame.args[2]])
    else
        return ""
    end
end
end


function p.getcolor(frame)
function p.getcolor(frame)
  return mw.text.nowiki(p.chem[frame.args[1]].color:sub(1, 7))
    check_frame(frame)
    return mw.text.nowiki(p.chem[frame.args[1]].color:sub(1, 7))
end
end


function p.gettextcolor(frame)
function p.gettextcolor(frame)
  local basecol = p.chem[frame.args[1]].color
    check_frame(frame)
  local red, grn, blu = tonumber(basecol:sub(2, 3), 16), tonumber(basecol:sub(4, 5), 16), tonumber(basecol:sub(6, 7), 16)
    local basecol = p.chem[frame.args[1]].color
  local luminance = math.sqrt(0.241 * red^2 + 0.691 * grn^2 + 0.068 * blu^2)
    local red = tonumber(basecol:sub(2, 3), 16)
  return mw.text.nowiki(luminance > 100 and "#000" or "#FFF")
    local grn = tonumber(basecol:sub(4, 5), 16)
    local blu = tonumber(basecol:sub(6, 7), 16)
    local luminance = math.sqrt(0.241 * red * red + 0.691 * grn * grn + 0.068 * blu * blu)
    if luminance > 100 then
        return mw.text.nowiki("#000")
    else
        return mw.text.nowiki("#FFF")
    end
end
end


function p.hasrecipe(frame)
function p.hasrecipe(frame)
  return p.chem[frame.args[1]].recipes[1] ~= nil
    check_frame(frame)
    return p.chem[frame.args[1]]["recipes"][1] ~= nil
end
end


function p.buildboxes(frame)
function p.buildboxes(frame)
  local out = {}
    check_frame(frame)
  local group = frame.args[1]
    local out = ""
  for k in pairs(p.chem) do
    local group = frame.args[1]
    if p.chem[k].group == group or not group then
    for k in pairs(p.chem) do
      table.insert(out, buildTemplate("Chembox", { prototype = k }))
        if p.chem[k].group == group then
            out = out .. frame:expandTemplate{ title = "Chembox", args = { prototype = k }}
        elseif group == nil then
            out = out .. frame:expandTemplate{ title = "Chembox", args = { prototype = k }}
        end
     end
     end
  end
    return out
  return table.concat(out)
end
end


function p.buildreactboxes(frame)
function p.buildreactboxes(frame)
  local out = {}
    check_frame(frame)
  for k in pairs(p.react) do
    local out = ""
    if tablelength(p.react[k].effects) > 0 then
    for k in pairs(p.react) do
      table.insert(out, buildTemplate("Reactionbox", { reaction = k }))
        if tablelength(p.react[k].effects) ~= 0 then
            out = out .. frame:expandTemplate{ title = "Reactionbox", args = { reaction = k }}
        end
     end
     end
  end
    return out
  return table.concat(out)
end
end


function p.buildrecipes(frame)
function p.buildrecipes(frame)
  local chem = frame.args[1]
    check_frame(frame)
  local out = {}
    local chem = frame.args[1]
  for _, recipe in pairs(p.chem[chem].recipes) do
    local out = ""
    table.insert(out, p.buildreaction(frame, recipe))
    for id, recipe in pairs(p.chem[chem].recipes) do
  end
        out = out .. p.buildreaction(frame, recipe)
  return table.concat(out)
    end
    return out
end
 
function p.buildreactionext(frame)
    check_frame(frame)
    local react = frame.args[1]
    return p.buildreaction(frame, react)
end
end


function p.buildreaction(frame, react)
function p.buildreaction(frame, react)
  local data = p.react[react]
    check_frame(frame)
  local args = { prototype = frame.args[1] }
    local data = p.react[react]
  local i = 0
    local dest = "Chemistry"
  for k, v in pairs(data.reactants) do
    local args = {}
    i = i + 1
    local i = 0
    local dest = p.groupDirection[p.chem[k].group] or "Chemistry"
    args["prototype"] = frame.args[1]
    args["component-" .. i] = buildTemplate("Chem Recipe Component", { reagent = k, amount = v.amount, dest = dest })
    for k, v in pairs(data.reactants) do
  end
        i = i + 1
  i = 0
        local dest = "Chemistry"
  for k, v in pairs(data.products) do
        if p.groupDirection[p.chem[k].group] ~= nil then
    i = i + 1
            dest = p.groupDirection[p.chem[k].group]
    local dest = p.groupDirection[p.chem[k].group] or "Chemistry"
        end
    args["result-" .. i] = buildTemplate("Chem Recipe Component", { reagent = k, amount = v, dest = dest })
        args["component-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v.amount, dest = dest }}
  end
    end
  if data.effects then
    i = 0
    args.effects = buildEffectString(data.effects, frame)
    for k, v in pairs(data.products) do
  end
        i = i + 1
  return buildTemplate("Chem Box Recipe", args)
        local dest = "Chemistry"
        if p.groupDirection[p.chem[k].group] ~= nil then
            dest = p.groupDirection[p.chem[k].group]
        end
        args["result-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v, dest = dest }}
    end
   
    if data.effects ~= nil then
        args.effects = p.geneffectlist(data.effects, frame, 1)
    end
 
    return frame:expandTemplate{ title = "Chem Box Recipe", args = args}
end
end


function p.geneffect(effect, frame)
function p.checksatiatesthirst(frame)
  local effectMap = {
    check_frame(frame)
     HealthChange = p.genhealthchange,
    local chem = frame.args[1]
     AdjustReagent = p.genadjustreagent,
     local met = p.chem[chem].metabolisms
     FlammableReaction = p.genflammablereaction,
    if met == nil then
     AdjustTemperature = p.genadjusttemperature,
        return ""
     GenericStatusEffect = p.gengenericstatuseffect,
    end
     ExplosionReactionEffect = p.genexplosionreactioneffect,
    for k, v in pairs(met) do
     FoamAreaReactionEffect = p.genfoamareareactioneffect,
        for l, w in pairs(v.effects) do
     SmokeAreaReactionEffect = p.gensmokeareareactioneffect,
            if w.id == "SatiateThirst" then
     ModifyBleedAmount = p.genmodifybleedamount
                return "1"
  }
            end
  return effectMap[effect.id] and effectMap[effect.id](effect, frame) or ""
        end
    end
    return ""
end
 
function p.checksatiateshunger(frame)
    check_frame(frame)
     local chem = frame.args[1]
     local met = p.chem[chem].metabolisms
    if met == nil then
        return ""
    end
    for k, v in pairs(met) do
        for l, w in pairs(v.effects) do
            if w.id == "SatiateHunger" then
                return "1"
            end
        end
    end
     return ""
end
 
function p.haseffects(frame)
    check_frame(frame)
     local chem = frame.args[1]
     local met = p.chem[chem].metabolisms
    if met == nil then
        return ""
    end
    for k, v in pairs(met) do
        for l, w in pairs(v.effects) do
            if w.id ~= "SatiateHunger" and w.id ~= "SatiateThirst" then
                return "1"
            end
        end
     end
    return ""
end
 
function p.geneffects(frame, chem)
    check_frame(frame)
     if chem == nil then
        chem = frame.args[1]
    end
     local met = p.chem[chem].metabolisms
    if met == nil then
        return ""
    end
    local out = ""
    for k, v in pairs(met) do
        out = out .. "<b>" .. k .. "</b> (" .. v.rate .. " единиц в секунду)\n" .. p.geneffectlist(v.effects, frame, v.rate)
    end
    return out
end
end


function p.geneffectlist(effects, frame, rate)
function p.geneffectlist(effects, frame, rate)
  local out = {}
    check_frame(frame)
  for _, effect in pairs(effects) do
    local out = ""
    table.insert(out, p.geneffect(effect, frame))
    for l, w in pairs(effects) do
  end
        if w.id == "HealthChange" then
  return table.concat(out, "\n")
            out = out .. ":" .. p.genhealthchange(w, rate, frame) .. "\n"
        elseif w.id == "AdjustReagent" then
            out = out .. ":" .. p.genadjustreagent(w, rate, frame) .. "\n"
        elseif w.id == "FlammableReaction" then
            out = out .. ":" .. p.genflammablereaction(w, frame) .. "\n"
        elseif w.id == "AdjustTemperature" then
            out = out .. ":" .. p.genadjusttemperature(w, frame) .. "\n"
        elseif w.id == "GenericStatusEffect" then
            out = out .. ":" .. p.gengenericstatuseffect(w, frame) .. "\n"
        elseif w.id == "ExplosionReactionEffect" then
            out = out .. ":" .. p.genexplosionreactioneffect(w, frame) .. "\n"
        elseif w.id == "FoamAreaReactionEffect" then
            out = out .. ":" .. p.genfoamareareactioneffect(w, frame) .. "\n"
        elseif w.id == "SmokeAreaReactionEffect" then
            out = out .. ":" .. p.gensmokeareareactioneffect(w, frame) .. "\n"
        elseif w.id == "ModifyBleedAmount" then
            out = out .. ":" .. p.genmodifybleedamount(w, frame) .. "\n"
        end
    end
    return out
end
end


function p.genconds(conds, frame)
function p.genconds(conds, frame)
  return buildConditionString(conds, frame)
    check_frame(frame)
    out = ""
    local len = tablelength(conds)
    local i = 0
    for k, v in pairs(conds) do
        i = i + 1
        if len == i and i ~= 1 then
            out = out .. ", and "
        elseif i ~= 1 then
            out = out .. ", "
        end
        out = out .. p.gencond(v, frame)
    end
    return out
end
end


function p.genhealthchange(h, rate, frame)
function p.gencond(c, frame)
  local healst, dealst = {}, {}
    check_frame(frame)
  local r = 1.0 / rate
    if c.id == "TotalDamage" then
  if h.damage.types then
        return frame:expandTemplate{ title = "TotalDamage", args = { min = c.Min, max = c.Max } }
    for k, v in pairs(h.damage.types) do
    elseif c.id == "ReagentThreshold" then
      if v < 0 then healst[k] = v * r else dealst[k] = v * r end
        return frame:expandTemplate{ title = "ReagentThreshold", args = { min = c.Min, max = c.Max, reagent = c.Reagent } }
    elseif c.id == "OrganType" then
        return frame:expandTemplate{ title = "OrganType", args = { shouldhave = c.ShouldHave, type = c.Type } }
    elseif c.id == "Temperature" then
        return frame:expandTemplate{ title = "Temperature", args = { min = c.Min, max = c.Max } }
     end
     end
   end
    return ""
   if h.damage.groups then
end
     for k, v in pairs(h.damage.groups) do
 
      if v < 0 then healst[k] = v * r else dealst[k] = v * r end
function tablelength(T)
  local count = 0
   for _ in pairs(T) do count = count + 1 end
   return count
end
 
function has_value(tab, val)
     for index, value in ipairs(tab) do
        if value[1] == val then
            return true
        end
     end
     end
  end
    return false
  local heals = hchangelist(healst, frame)
  local deals = hchangelist(dealst, frame)
  return buildTemplate("HealthChange", { heals = heals, deals = deals, when = buildConditionString(h.conditions, frame), prob = h.probability })
end
end


function hchangelist(l, frame)
function strsplit(inputstr, seperator)
  local out = {}
        if seperator == nil then
  for k, v in pairs(l) do
                seperator = ","
    table.insert(out, hchange(k, v, frame))
        end
  end
        local t={}
  return table.concat(out, ", ")
        for str in string.gmatch(inputstr, "([^"..seperator.."]+)") do
                table.insert(t, str)
        end
        return t
end
end


function hchange(ty, amnt, frame)
  return buildTemplate("HealthModifier", { adj = amnt, kind = ty })
end


return p
return p

Версия от 12:57, 25 февраля 2025

Для документации этого модуля может быть создана страница Модуль:Chemistry Lookup/doc

local prototypes = mw.loadData("Module:Chemistry Lookup/data")

local p = {}
p.chem = prototypes.chem
p.react = prototypes.react
p.groupDirection = prototypes.groupDirection

-- Обработка ошибок на nil для frame
local function check_frame(frame)
    if frame == nil then
        error("frame is nil!")
    end
end

function p.readscalar(frame)
    check_frame(frame)
    return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
end

function p.readscalarreact(frame)
    check_frame(frame)
    if p.react[frame.args[1]][frame.args[2]] ~= nil then
        return mw.text.nowiki(p.react[frame.args[1]][frame.args[2]])
    else
        return ""
    end
end

function p.getcolor(frame)
    check_frame(frame)
    return mw.text.nowiki(p.chem[frame.args[1]].color:sub(1, 7))
end

function p.gettextcolor(frame)
    check_frame(frame)
    local basecol = p.chem[frame.args[1]].color
    local red = tonumber(basecol:sub(2, 3), 16)
    local grn = tonumber(basecol:sub(4, 5), 16)
    local blu = tonumber(basecol:sub(6, 7), 16)
    local luminance = math.sqrt(0.241 * red * red + 0.691 * grn * grn + 0.068 * blu * blu)
    if luminance > 100 then
        return mw.text.nowiki("#000")
    else
        return mw.text.nowiki("#FFF")
    end
end

function p.hasrecipe(frame)
    check_frame(frame)
    return p.chem[frame.args[1]]["recipes"][1] ~= nil
end

function p.buildboxes(frame)
    check_frame(frame)
    local out = ""
    local group = frame.args[1]
    for k in pairs(p.chem) do
        if p.chem[k].group == group then
            out = out .. frame:expandTemplate{ title = "Chembox", args = { prototype = k }}
        elseif group == nil then
            out = out .. frame:expandTemplate{ title = "Chembox", args = { prototype = k }}
        end
    end
    return out
end

function p.buildreactboxes(frame)
    check_frame(frame)
    local out = ""
    for k in pairs(p.react) do
        if tablelength(p.react[k].effects) ~= 0 then
            out = out .. frame:expandTemplate{ title = "Reactionbox", args = { reaction = k }}
        end
    end
    return out
end

function p.buildrecipes(frame)
    check_frame(frame)
    local chem = frame.args[1]
    local out = ""
    for id, recipe in pairs(p.chem[chem].recipes) do
        out = out .. p.buildreaction(frame, recipe)
    end
    return out
end

function p.buildreactionext(frame)
    check_frame(frame)
    local react = frame.args[1]
    return p.buildreaction(frame, react)
end

function p.buildreaction(frame, react)
    check_frame(frame)
    local data = p.react[react]
    local dest = "Chemistry"
    local args = {}
    local i = 0
    args["prototype"] = frame.args[1]
    for k, v in pairs(data.reactants) do
        i = i + 1
        local dest = "Chemistry"
        if p.groupDirection[p.chem[k].group] ~= nil then
            dest = p.groupDirection[p.chem[k].group]
        end
        args["component-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v.amount, dest = dest }}
    end
    i = 0
    for k, v in pairs(data.products) do
        i = i + 1
        local dest = "Chemistry"
        if p.groupDirection[p.chem[k].group] ~= nil then
            dest = p.groupDirection[p.chem[k].group]
        end
        args["result-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v, dest = dest }}
    end
    
    if data.effects ~= nil then
        args.effects = p.geneffectlist(data.effects, frame, 1)
    end

    return frame:expandTemplate{ title = "Chem Box Recipe", args = args}
end

function p.checksatiatesthirst(frame)
    check_frame(frame)
    local chem = frame.args[1]
    local met = p.chem[chem].metabolisms
    if met == nil then
        return ""
    end
    for k, v in pairs(met) do
        for l, w in pairs(v.effects) do
            if w.id == "SatiateThirst" then
                return "1"
            end
        end
    end
    return ""
end

function p.checksatiateshunger(frame)
    check_frame(frame)
    local chem = frame.args[1]
    local met = p.chem[chem].metabolisms
    if met == nil then
        return ""
    end
    for k, v in pairs(met) do
        for l, w in pairs(v.effects) do
            if w.id == "SatiateHunger" then
                return "1"
            end
        end
    end
    return ""
end

function p.haseffects(frame)
    check_frame(frame)
    local chem = frame.args[1]
    local met = p.chem[chem].metabolisms
    if met == nil then
        return ""
    end
    for k, v in pairs(met) do
        for l, w in pairs(v.effects) do
            if w.id ~= "SatiateHunger" and w.id ~= "SatiateThirst" then
                return "1"
            end
        end
    end
    return ""
end

function p.geneffects(frame, chem)
    check_frame(frame)
    if chem == nil then
        chem = frame.args[1]
    end
    local met = p.chem[chem].metabolisms
    if met == nil then
        return ""
    end
    local out = ""
    for k, v in pairs(met) do
        out = out .. "<b>" .. k .. "</b> (" .. v.rate .. " единиц в секунду)\n" .. p.geneffectlist(v.effects, frame, v.rate)
    end
    return out
end

function p.geneffectlist(effects, frame, rate)
    check_frame(frame)
    local out = ""
    for l, w in pairs(effects) do
        if w.id == "HealthChange" then
            out = out .. ":" .. p.genhealthchange(w, rate, frame) .. "\n"
        elseif w.id == "AdjustReagent" then
            out = out .. ":" .. p.genadjustreagent(w, rate, frame) .. "\n"
        elseif w.id == "FlammableReaction" then
            out = out .. ":" .. p.genflammablereaction(w, frame) .. "\n"
        elseif w.id == "AdjustTemperature" then
            out = out .. ":" .. p.genadjusttemperature(w, frame) .. "\n"
        elseif w.id == "GenericStatusEffect" then
            out = out .. ":" .. p.gengenericstatuseffect(w, frame) .. "\n"
        elseif w.id == "ExplosionReactionEffect" then
            out = out .. ":" .. p.genexplosionreactioneffect(w, frame) .. "\n"
        elseif w.id == "FoamAreaReactionEffect" then
            out = out .. ":" .. p.genfoamareareactioneffect(w, frame) .. "\n"
        elseif w.id == "SmokeAreaReactionEffect" then
            out = out .. ":" .. p.gensmokeareareactioneffect(w, frame) .. "\n"
        elseif w.id == "ModifyBleedAmount" then
            out = out .. ":" .. p.genmodifybleedamount(w, frame) .. "\n"
        end
    end
    return out
end

function p.genconds(conds, frame)
    check_frame(frame)
    out = ""
    local len = tablelength(conds)
    local i = 0
    for k, v in pairs(conds) do
        i = i + 1
        if len == i and i ~= 1 then
            out = out .. ", and "
        elseif i ~= 1 then
            out = out .. ", "
        end
        out = out .. p.gencond(v, frame)
    end
    return out
end

function p.gencond(c, frame)
    check_frame(frame)
    if c.id == "TotalDamage" then
        return frame:expandTemplate{ title = "TotalDamage", args = { min = c.Min, max = c.Max } }
    elseif c.id == "ReagentThreshold" then
        return frame:expandTemplate{ title = "ReagentThreshold", args = { min = c.Min, max = c.Max, reagent = c.Reagent } }
    elseif c.id == "OrganType" then
        return frame:expandTemplate{ title = "OrganType", args = { shouldhave = c.ShouldHave, type = c.Type } }
    elseif c.id == "Temperature" then
        return frame:expandTemplate{ title = "Temperature", args = { min = c.Min, max = c.Max } }
    end
    return ""
end

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

function has_value(tab, val)
    for index, value in ipairs(tab) do
        if value[1] == val then
            return true
        end
    end
    return false
end

function strsplit(inputstr, seperator)
        if seperator == nil then
                seperator = ","
        end
        local t={}
        for str in string.gmatch(inputstr, "([^"..seperator.."]+)") do
                table.insert(t, str)
        end
        return t
end


return p