Module:Chemistry Lookup

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

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

function p.readscalar(frame) return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]]) 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 then out = out .. frame:expandTemplate{ title = "Chembox", args = { prototype = k }} end end return out end

function p.buildrecipes(frame) local chem = frame.args[1] local out = "" for id, recipe in pairs(p.chem[chem].recipes) do		local data = p.react[recipe] local args = {} local i = 0 for k,v in pairs(data.reactants) do			i = i + 1 args["component-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v.amount }} end i = 0 for k,v in pairs(data.products) do			i = i + 1 args["result-" .. i] = frame:expandTemplate{ title = "Chem Recipe Component", args = { reagent = k, amount = v }} end

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

function p.checksatiatesthirst(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) 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) 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" or w.id ~= "SatiateThirst" then return "1" end end end return "" end

function p.geneffects(frame) local chem = frame.args[1] local met = p.chem[chem].metabolisms if met == nil then return "" end local out = "" for k, v in pairs(met) do out = out .. "'''" .. k .. "'''\n" for l, w in pairs(v.effects) do			-- Popup Message is ignored on purpose if w.id == "HealthChange" then out = out .. ":" .. p.genhealthchange(w, v.rate, frame) .. "\n" elseif w.id == "AdjustReagent" then out = out .. ":" .. p.genadjustreagent(w, v.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" end end end return out end

function p.gengenericstatuseffect(r, frame) if r.conditions ~= nil then conds = p.genconds(r.conditions, frame) end return frame:expandTemplate{ title = "GenericStatusEffect", args = { amount = r.Amount, when = conds }} end

function p.genadjusttemperature(r, frame) if r.conditions ~= nil then conds = p.genconds(r.conditions, frame) end return frame:expandTemplate{ title = "AdjustTemperature", args = { amount = r.Amount, when = conds }} end

function p.genflammablereaction(r, frame) if r.conditions ~= nil then conds = p.genconds(r.conditions, frame) end return frame:expandTemplate{ title = "FlammableReaction", args = { multiplier = r.Multiplier, when = conds }} end

function p.genflammablereaction(r, frame) if r.conditions ~= nil then conds = p.genconds(r.conditions, frame) end return frame:expandTemplate{ title = "FlammableReaction", args = { multiplier = r.Multiplier, when = conds }} end

function p.genadjustreagent(r, rate, frame) if r.conditions ~= nil then conds = p.genconds(r.conditions, frame) end return frame:expandTemplate{ title = "AdjustReagent", args = { amount = r.Amount, reagent = r.Reagent, when = conds }} end

function p.genhealthchange(h, rate, frame) local healst = {} local dealst = {} local r = 1.0 / rate if h.damage.types ~= nil then for k, v in pairs(h.damage.types) do		if v < 0 then healst[k] = math.abs(v) * r		else dealst[k] = math.abs(v) * r		end end end if h.damage.groups ~= nil then for k, v in pairs(h.damage.groups) do		if v < 0 then healst[k] = math.abs(v) * r		else dealst[k] = math.abs(v) * r		end end end local heals = hchangelist(healst) local deals = hchangelist(dealst) local conds = nil if h.conditions ~= nil then conds = p.genconds(h.conditions, frame) end return frame:expandTemplate{ title = "HealthChange", args = { heals = heals, deals = deals, when = conds }} end

function hchangelist(l) out = "" local len = tablelength(l) local i = 0 for k, v in pairs(l) do		i = i + 1 if len == i and i ~= 1 then out = out .. ", and " elseif i ~= 1 then out = out .. ", "		end out = out .. hchange(k, v)	end return out end

-- So we can make it fancy later function hchange(ty, amnt) return amnt .. " " .. ty end

function p.genconds(conds, 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) 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 } } end return "" end

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

return p