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

This commit is contained in:
Evert Prants 2018-04-04 20:19:33 +03:00
parent b9cf10bd3d
commit 7b75a2e9d8
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
9 changed files with 153 additions and 103 deletions

View File

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

View File

@ -118,24 +118,3 @@ function fluidity.register_molten_metal(metal)
"Molten "..description.." Bucket" "Molten "..description.." Bucket"
) )
end 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
View 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"}
})

View File

@ -184,11 +184,3 @@ function fluidity.register_fluid_tank(data)
end end
end 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"}
})

View File

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

View File

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

46
metal_melter/meltable.lua Normal file
View 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")

View File

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

View File

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