SHEGARE (обсуждение | вклад) (Добавление параметра deleteReagent) |
Dantes (обсуждение | вклад) Нет описания правки |
||
| Строка 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 | ||
p.react = prototypes.react | p.react = prototypes.react | ||
p.groupDirection = prototypes.groupDirection | p.groupDirection = prototypes.groupDirection | ||
local function tablelength(t) | |||
local count = 0 | |||
for _ in pairs(t) do count = count + 1 end | |||
return count | |||
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 | |||
function p.readscalar(frame) | function p.readscalar(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]] | |||
return react and mw.text.nowiki(react) or "" | |||
end | end | ||
function p.getcolor(frame) | function p.getcolor(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 | |||
local red, grn, blu = tonumber(basecol:sub(2, 3), 16), tonumber(basecol:sub(4, 5), 16), tonumber(basecol:sub(6, 7), 16) | |||
local luminance = math.sqrt(0.241 * red^2 + 0.691 * grn^2 + 0.068 * blu^2) | |||
return mw.text.nowiki(luminance > 100 and "#000" or "#FFF") | |||
end | end | ||
function p.hasrecipe(frame) | function p.hasrecipe(frame) | ||
return p.chem[frame.args[1]].recipes[1] ~= nil | |||
end | end | ||
function p.buildboxes(frame) | function p.buildboxes(frame) | ||
local out = {} | |||
local group = frame.args[1] | |||
for k in pairs(p.chem) do | |||
if p.chem[k].group == group or not group then | |||
table.insert(out, buildTemplate("Chembox", { prototype = k })) | |||
end | |||
end | |||
return table.concat(out) | |||
end | end | ||
function p.buildreactboxes(frame) | function p.buildreactboxes(frame) | ||
local out = {} | |||
for k in pairs(p.react) do | |||
if tablelength(p.react[k].effects) > 0 then | |||
table.insert(out, buildTemplate("Reactionbox", { reaction = k })) | |||
end | |||
end | |||
return table.concat(out) | |||
end | end | ||
function p.buildrecipes(frame) | function p.buildrecipes(frame) | ||
local chem = frame.args[1] | |||
local out = {} | |||
for _, recipe in pairs(p.chem[chem].recipes) do | |||
table.insert(out, p.buildreaction(frame, recipe)) | |||
end | |||
return table.concat(out) | |||
end | end | ||
function p.buildreaction(frame, react) | function p.buildreaction(frame, react) | ||
local data = p.react[react] | |||
local args = { prototype = frame.args[1] } | |||
local i = 0 | |||
for k, v in pairs(data.reactants) do | |||
i = i + 1 | |||
local dest = p.groupDirection[p.chem[k].group] or "Chemistry" | |||
args["component-" .. i] = buildTemplate("Chem Recipe Component", { reagent = k, amount = v.amount, dest = dest }) | |||
end | |||
i = 0 | |||
for k, v in pairs(data.products) do | |||
i = i + 1 | |||
local dest = p.groupDirection[p.chem[k].group] or "Chemistry" | |||
args["result-" .. i] = buildTemplate("Chem Recipe Component", { reagent = k, amount = v, dest = dest }) | |||
end | |||
if data.effects then | |||
args.effects = buildEffectString(data.effects, frame) | |||
end | |||
return buildTemplate("Chem Box Recipe", args) | |||
end | end | ||
function p. | function p.geneffect(effect, frame) | ||
local effectMap = { | |||
HealthChange = p.genhealthchange, | |||
AdjustReagent = p.genadjustreagent, | |||
FlammableReaction = p.genflammablereaction, | |||
AdjustTemperature = p.genadjusttemperature, | |||
GenericStatusEffect = p.gengenericstatuseffect, | |||
ExplosionReactionEffect = p.genexplosionreactioneffect, | |||
FoamAreaReactionEffect = p.genfoamareareactioneffect, | |||
SmokeAreaReactionEffect = p.gensmokeareareactioneffect, | |||
ModifyBleedAmount = p.genmodifybleedamount | |||
} | |||
return effectMap[effect.id] and effectMap[effect.id](effect, frame) or "" | |||
end | end | ||
function p.geneffectlist(effects, frame, rate) | function p.geneffectlist(effects, frame, rate) | ||
local out = {} | |||
for _, effect in pairs(effects) do | |||
table.insert(out, p.geneffect(effect, frame)) | |||
end | |||
return table.concat(out, "\n") | |||
end | end | ||
function | function p.genconds(conds, frame) | ||
return buildConditionString(conds, frame) | |||
end | end | ||
function p.genhealthchange(h, rate, frame) | function p.genhealthchange(h, rate, frame) | ||
local healst, dealst = {}, {} | |||
local r = 1.0 / rate | |||
if h.damage.types then | |||
for k, v in pairs(h.damage.types) do | |||
if v < 0 then healst[k] = v * r else dealst[k] = v * r end | |||
end | |||
end | |||
if h.damage.groups then | |||
for k, v in pairs(h.damage.groups) do | |||
if v < 0 then healst[k] = v * r else dealst[k] = v * r end | |||
end | |||
end | |||
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 hchangelist(l, frame) | ||
local out = {} | |||
for k, v in pairs(l) do | |||
table.insert(out, hchange(k, v, frame)) | |||
end | |||
return table.concat(out, ", ") | |||
end | end | ||
function hchange(ty, amnt, frame) | function hchange(ty, amnt, frame) | ||
return buildTemplate("HealthModifier", { adj = amnt, kind = ty }) | |||
end | end | ||
return p | return p | ||
Версия от 12:55, 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
local function tablelength(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
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
function p.readscalar(frame)
return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
end
function p.readscalarreact(frame)
local react = p.react[frame.args[1]][frame.args[2]]
return react and mw.text.nowiki(react) or ""
end
function p.getcolor(frame)
return mw.text.nowiki(p.chem[frame.args[1]].color:sub(1, 7))
end
function p.gettextcolor(frame)
local basecol = p.chem[frame.args[1]].color
local red, grn, blu = tonumber(basecol:sub(2, 3), 16), tonumber(basecol:sub(4, 5), 16), tonumber(basecol:sub(6, 7), 16)
local luminance = math.sqrt(0.241 * red^2 + 0.691 * grn^2 + 0.068 * blu^2)
return mw.text.nowiki(luminance > 100 and "#000" or "#FFF")
end
function p.hasrecipe(frame)
return p.chem[frame.args[1]].recipes[1] ~= nil
end
function p.buildboxes(frame)
local out = {}
local group = frame.args[1]
for k in pairs(p.chem) do
if p.chem[k].group == group or not group then
table.insert(out, buildTemplate("Chembox", { prototype = k }))
end
end
return table.concat(out)
end
function p.buildreactboxes(frame)
local out = {}
for k in pairs(p.react) do
if tablelength(p.react[k].effects) > 0 then
table.insert(out, buildTemplate("Reactionbox", { reaction = k }))
end
end
return table.concat(out)
end
function p.buildrecipes(frame)
local chem = frame.args[1]
local out = {}
for _, recipe in pairs(p.chem[chem].recipes) do
table.insert(out, p.buildreaction(frame, recipe))
end
return table.concat(out)
end
function p.buildreaction(frame, react)
local data = p.react[react]
local args = { prototype = frame.args[1] }
local i = 0
for k, v in pairs(data.reactants) do
i = i + 1
local dest = p.groupDirection[p.chem[k].group] or "Chemistry"
args["component-" .. i] = buildTemplate("Chem Recipe Component", { reagent = k, amount = v.amount, dest = dest })
end
i = 0
for k, v in pairs(data.products) do
i = i + 1
local dest = p.groupDirection[p.chem[k].group] or "Chemistry"
args["result-" .. i] = buildTemplate("Chem Recipe Component", { reagent = k, amount = v, dest = dest })
end
if data.effects then
args.effects = buildEffectString(data.effects, frame)
end
return buildTemplate("Chem Box Recipe", args)
end
function p.geneffect(effect, frame)
local effectMap = {
HealthChange = p.genhealthchange,
AdjustReagent = p.genadjustreagent,
FlammableReaction = p.genflammablereaction,
AdjustTemperature = p.genadjusttemperature,
GenericStatusEffect = p.gengenericstatuseffect,
ExplosionReactionEffect = p.genexplosionreactioneffect,
FoamAreaReactionEffect = p.genfoamareareactioneffect,
SmokeAreaReactionEffect = p.gensmokeareareactioneffect,
ModifyBleedAmount = p.genmodifybleedamount
}
return effectMap[effect.id] and effectMap[effect.id](effect, frame) or ""
end
function p.geneffectlist(effects, frame, rate)
local out = {}
for _, effect in pairs(effects) do
table.insert(out, p.geneffect(effect, frame))
end
return table.concat(out, "\n")
end
function p.genconds(conds, frame)
return buildConditionString(conds, frame)
end
function p.genhealthchange(h, rate, frame)
local healst, dealst = {}, {}
local r = 1.0 / rate
if h.damage.types then
for k, v in pairs(h.damage.types) do
if v < 0 then healst[k] = v * r else dealst[k] = v * r end
end
end
if h.damage.groups then
for k, v in pairs(h.damage.groups) do
if v < 0 then healst[k] = v * r else dealst[k] = v * r end
end
end
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
function hchangelist(l, frame)
local out = {}
for k, v in pairs(l) do
table.insert(out, hchange(k, v, frame))
end
return table.concat(out, ", ")
end
function hchange(ty, amnt, frame)
return buildTemplate("HealthModifier", { adj = amnt, kind = ty })
end
return p