Do not find strings in item names, use a table of items instead

This commit is contained in:
Evert 2018-04-04 20:19:33 +03:00
parent b9cf10bd3d
commit 7b75a2e9d8
No known key found for this signature in database
GPG Key ID: 1688DA83D222D0B5
9 changed files with 153 additions and 103 deletions

@ -6,8 +6,11 @@ fluidity.modpath = mpath
-- Functions
dofile(mpath.."/functions.lua")
-- Register molten metals
-- Molten metals
dofile(mpath.."/molten.lua")
-- Register tanks for each fluid
-- Tanks
dofile(mpath.."/tanks.lua")
-- Register everything
dofile(mpath.."/register.lua")

@ -118,24 +118,3 @@ function fluidity.register_molten_metal(metal)
"Molten "..description.." Bucket"
)
end
-- Default metals
fluidity.register_molten_metal("steel")
fluidity.register_molten_metal("copper")
fluidity.register_molten_metal("tin")
fluidity.register_molten_metal("gold")
fluidity.register_molten_metal("mese")
fluidity.register_molten_metal("obsidian")
-- Technic metals
if minetest.get_modpath("technic") ~= nil then
fluidity.register_molten_metal("lead")
fluidity.register_molten_metal("chromium")
fluidity.register_molten_metal("zinc")
end
-- moreores metals
if minetest.get_modpath("moreores") ~= nil then
fluidity.register_molten_metal("silver")
fluidity.register_molten_metal("mithril")
end

17
fluidity/register.lua Normal file

@ -0,0 +1,17 @@
-- Register molten metals
-- Default metals
local metals = {"steel", "copper", "tin", "gold", "mese", "obsidian", "lead", "chromium", "zinc", "silver", "mithril"}
for _,v in pairs(metals) do
fluidity.register_molten_metal(v)
end
-- Register tanks for all metals
fluidity.register_fluid_tank({
mod_name = "fluidity",
tank_name = "fluid_tank",
tank_description = "Fluid Tank",
capacity = 64,
tiles = {"default_glass.png", "default_glass_detail.png"}
})

@ -184,11 +184,3 @@ function fluidity.register_fluid_tank(data)
end
end
end
fluidity.register_fluid_tank({
mod_name = "fluidity",
tank_name = "fluid_tank",
tank_description = "Fluid Tank",
capacity = 64,
tiles = {"default_glass.png", "default_glass_detail.png"}
})

@ -12,9 +12,9 @@ metal_caster.spec = metal_melter.spec
metal_caster.spec.cast = 288
metal_caster.casts = {
ingot_cast = {name = "Ingot Cast", result = "%s:%s_ingot", cost = metal_caster.spec.ingot, typenames = {"ingot"}},
lump_cast = {name = "Lump Cast", result = "%s:%s_lump", cost = metal_caster.spec.lump, typenames = {"lump"}},
gem_cast = {name = "Gem Cast", result = "%s:%s_crystal", cost = metal_caster.spec.crystal, typenames = {"crystal", "gem"}}
ingot_cast = {name = "Ingot Cast", result = "ingot", cost = metal_caster.spec.ingot, typenames = {"ingot"}},
lump_cast = {name = "Lump Cast", result = "lump", cost = metal_caster.spec.lump, typenames = {"lump"}},
gem_cast = {name = "Gem Cast", result = "crystal", cost = metal_caster.spec.crystal, typenames = {"crystal", "gem"}}
}
local metal_cache = {}
@ -98,37 +98,36 @@ function metal_caster.get_metal_caster_formspec(data)
default.get_hotbar_bg(0, 4.25)
end
-- Find the name of the mod that adds this particular metal into the game.
function metal_caster.get_modname_for_metal(metal_type)
if not metal_cache[metal_type] then
for i, v in pairs(minetest.registered_items) do
if i:find(metal_type) and (i:find("ingot") or i:find("block") or i:find("crystal")) and not metal_cache[metal_type] then
local modname, metalname = i:match("(%a+):(%a+)")
metal_cache[metalname] = modname
end
local function find_castable(metal_name, cast_name)
local cast = metal_caster.casts[cast_name]
if not cast then return nil end
local types = metal_melter.melts[metal_name]
if not types then return nil end
local typeres = types[cast.result]
if not typeres then return nil end
if #typeres > 0 then
return typeres[1]
end
return nil
end
function metal_caster.get_cast_for_name(name)
for index, value in pairs(metal_caster.casts) do
local mod = value.mod or "metal_melter"
if name == mod..":"..index then
return index
end
end
return metal_cache[metal_type]
return nil
end
-- Check to see if this cast is able to cast this metal type
local function can_cast(metal_name, cast_name)
local cast = metal_caster.casts[cast_name]
if cast.mod then
return cast.mod
end
local mod = metal_caster.get_modname_for_metal(metal_name)
local item_name = cast.result:format(mod, metal_name)
if minetest.registered_items[item_name] ~= nil then
return mod
else
return nil
end
end
local function can_dig(pos, player)
local meta = minetest.get_meta(pos)
@ -203,21 +202,26 @@ end
-- Get the corresponding cast for an item
local function get_cast_for(item)
local typename, castname = item:match("(%a+)_([%a_]+)$")
if not typename or not castname then
return nil
end
local cast = nil
for i, v in pairs(metal_caster.casts) do
for _,k in pairs(v.typenames) do
if castname == k then
cast = i
local typename = nil
for metal, types in pairs(metal_melter.melts) do
if typename ~= nil then break end
for t, items in pairs(types) do
if items[item] then
typename = t
break
end
end
end
if not cast then return nil end
for cname,v in pairs(metal_caster.casts) do
if v.result == typename then
cast = cname
break
end
end
return typename, cast
end
@ -290,14 +294,13 @@ local function caster_node_timer(pos, elapsed)
if metal ~= "" then
metal_type = fluidity.get_metal_for_fluid(metal)
local castname = inv:get_stack("cast", 1):get_name()
castname = castname:gsub("[a-zA-Z0-9_]+:", "")
if metal_caster.casts[castname] then
local caststack = inv:get_stack("cast", 1):get_name()
local castname = metal_caster.get_cast_for_name(caststack)
if castname ~= nil then
-- Cast metal using a cast
local cast = metal_caster.casts[castname]
local modname = can_cast(metal_type, castname)
if modname ~= nil then
local result_name = cast.result:format(modname, metal_type)
local result_name = find_castable(metal_type, castname)
if result_name ~= nil then
local result_cost = cast.cost
local coolant_cost = result_cost / 4
@ -317,7 +320,7 @@ local function caster_node_timer(pos, elapsed)
local result_cost = metal_caster.spec.cast
local coolant_cost = result_cost / 4
if metal_count >= result_cost and coolant_count >= coolant_cost then
local mtype, ctype = get_cast_for(castname)
local mtype, ctype = get_cast_for(caststack)
if mtype then
local cmod = metal_caster.casts[ctype].mod or "metal_melter"
local stack = ItemStack(cmod..":"..ctype)

@ -1,6 +1,10 @@
-- Melterns mod
local modpath = minetest.get_modpath("metal_melter")
metal_melter = {}
-- Melting database
dofile(modpath.."/meltable.lua")
-- Crafting components
dofile(modpath.."/components.lua")

46
metal_melter/meltable.lua Normal file

@ -0,0 +1,46 @@
metal_melter.melts = {}
-- fluidity.molten_metals - metals
function metal_melter.register_melt(item, metal, type)
if not fluidity.molten_metals[metal] then
return nil
end
if not metal_melter.melts[metal] then
metal_melter.melts[metal] = {}
end
if not metal_melter.melts[metal][type] then
metal_melter.melts[metal][type] = {}
end
table.insert(metal_melter.melts[metal][type], item)
end
-- Autofind meltable
local autofind = {"ingot", "lump", "crystal", "ore", "block"}
local modfind = {"default", "technic", "moreores"}
for metal,_ in pairs(fluidity.molten_metals) do
for i,v in pairs(modfind) do
for j,k in pairs(autofind) do
local name = v..":"..metal.."_"..k
if minetest.registered_items[name] then
metal_melter.register_melt(name, metal, k)
end
end
end
end
-- Manually register default blocks, for now
metal_melter.register_melt("default:mese", "mese", "block")
metal_melter.register_melt("default:obsidian", "obsidian", "block")
metal_melter.register_melt("default:goldblock", "gold", "block")
metal_melter.register_melt("default:steelblock", "steel", "block")
metal_melter.register_melt("default:copperblock", "copper", "block")
metal_melter.register_melt("default:tinblock", "tin", "block")
-- Special snowflake
metal_melter.register_melt("default:iron_lump", "steel", "lump")

@ -1,7 +1,5 @@
-- Melts metals using lava as a heat source
metal_melter = {}
metal_melter.max_fuel = 8000
metal_melter.max_metal = 16000
metal_melter.spec = {
@ -12,38 +10,40 @@ metal_melter.spec = {
ore = 288
}
function metal_melter.get_metal_from_stack(stack)
local metal = nil
local metal_type = nil
-- Find a metal that is in the name
for v,_ in pairs(fluidity.molten_metals) do
if stack:find(v) then
metal = v
local function in_table(t, n)
local found = nil
for _, v in pairs(t) do
if v == n then
found = v
end
end
if not metal then
return nil
end
return found
end
-- Find type
if stack:find("lump") then
metal_type = "lump"
elseif stack:find("ingot") then
metal_type = "ingot"
elseif stack:find("ore") then
metal_type = "ore"
elseif stack:find("crystal") then
metal_type = "crystal"
else
-- Assume block
metal_type = "block"
function metal_melter.get_metal_from_stack(stack)
local metal = nil
local metal_type = nil
for mt, types in pairs(metal_melter.melts) do
if metal then break end
for tp,items in pairs(types) do
if in_table(items, stack) then
metal = mt
metal_type = tp
break
end
end
end
return metal, metal_type
end
function metal_melter.set_spec(specname, value)
metal_melter.spec[specname] = value
end
function metal_melter.get_metal_melter_formspec_default()
return "size[8,8.5]"..
default.gui_bg..

@ -344,11 +344,16 @@ local num_tools = 0
for m, s in pairs(tinkering.materials) do
for i, v in pairs(components) do
if v.materials == 1 then
local component = m.."_"..i
tinkering.register_component({
name = m.."_"..i,
name = component,
description = v.description:format(s.name),
image = tinkering.color_filter(v.image, s.color)
})
-- Make all components meltable
metal_melter.register_melt("tinkering:"..component, m, i)
num_components = num_components + 1
end
end
@ -361,10 +366,11 @@ end
-- Add casts to metal_melter
for i,v in pairs(components) do
metal_melter.set_spec(i, metal_caster.spec.cast)
metal_caster.register_cast(i.."_cast", {
name = v.description:sub(4).." Cast",
mod = "tinkering",
result = "%s:%s_"..i,
result = i,
cost = metal_caster.spec.cast,
typenames = {i}
})