From d5f198e5e130004e6a34f998d2d4feb7296cae3b Mon Sep 17 00:00:00 2001 From: Signal Date: Thu, 6 Nov 2025 22:17:25 -0500 Subject: [PATCH] Add recipe indexing to towns --- mods/rgt_towns/rgt_towns_core/init.lua | 9 +++++++++ mods/rgt_towns/rgt_towns_core/items.lua | 23 +++++++++++++++++------ mods/rgt_towns/rgt_towns_core/plots.lua | 4 ++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/mods/rgt_towns/rgt_towns_core/init.lua b/mods/rgt_towns/rgt_towns_core/init.lua index e0a70f7..5c650bf 100644 --- a/mods/rgt_towns/rgt_towns_core/init.lua +++ b/mods/rgt_towns/rgt_towns_core/init.lua @@ -1,5 +1,6 @@ rgt_towns = { plots = {}, + recipes = {}, spawners = {} } local ns = rgt_towns @@ -98,5 +99,13 @@ function ns.register_plot(def) end def.constructors = out end + if def.recipe then + local idx = {} + for i, x in ipairs(def.recipe) do + def.recipe[i] = ItemStack(x) + idx[#idx +1] = def.recipe[i]:get_name().." "..def.recipe[i]:get_count() + end + ns.recipes[table.concat(idx, "|")] = def.name + end ns.plots[def.name] = def end \ No newline at end of file diff --git a/mods/rgt_towns/rgt_towns_core/items.lua b/mods/rgt_towns/rgt_towns_core/items.lua index 67779a8..fe0d7bd 100644 --- a/mods/rgt_towns/rgt_towns_core/items.lua +++ b/mods/rgt_towns/rgt_towns_core/items.lua @@ -59,16 +59,26 @@ minetest.register_craftitem(":rgt_towns:spawner", { -Z ]] -function ns.check_recipe(inv, plot) +function ns.inv_to_string(inv) + local out = {} + for _, x in ipairs(inv:get_list("main")) do + -- Only check against name and count; metadata doesn't really matter. + out[#out +1] = x:get_name().." "..x:get_count() + end + return table.concat(out, "|") +end + +function ns.check_recipe(inv) + local plot = ns.recipes[ns.inv_to_string(inv)] local recipe = ns.plots[plot].recipe print(dump(recipe)) for i = 1, 9 do - print("Evaluating: "..inv:get_stack("main", i):to_string().." vs. "..ItemStack(recipe[i]):to_string()) - if inv:get_stack("main", i) ~= ItemStack(recipe[i]) then + print("Evaluating: "..inv:get_stack("main", i):to_string().." vs. "..recipe[i]:to_string()) + if inv:get_stack("main", i) ~= recipe[i] then return false end end - return true + return plot end minetest.register_node(":rgt_towns:constructor", { @@ -84,11 +94,12 @@ minetest.register_node(":rgt_towns:constructor", { local m = minetest.get_meta(pos) local grid = m:get_string("grid") local gpos = vector.from_string(m:get_string("build_pos")) - local plot = m:get("plot") or "house" local inv = m:get_inventory() - if not ns.check_recipe(inv, plot) then + local plot = ns.check_recipe(inv) + + if not plot then return end diff --git a/mods/rgt_towns/rgt_towns_core/plots.lua b/mods/rgt_towns/rgt_towns_core/plots.lua index b93fe50..4ac138e 100644 --- a/mods/rgt_towns/rgt_towns_core/plots.lua +++ b/mods/rgt_towns/rgt_towns_core/plots.lua @@ -245,6 +245,7 @@ function ns.place_plot(grid, pos, plot, owner, rot) m = minetest.get_meta(p) m:set_string("grid", grid) m:set_string("build_pos", gp:to_string()) + m:set_string("plot", plot) end gp = pos:offset(x, 0, 1) @@ -256,6 +257,7 @@ function ns.place_plot(grid, pos, plot, owner, rot) m = minetest.get_meta(p) m:set_string("grid", grid) m:set_string("build_pos", gp:to_string()) + m:set_string("plot", plot) end end @@ -270,6 +272,7 @@ function ns.place_plot(grid, pos, plot, owner, rot) m = minetest.get_meta(p) m:set_string("grid", grid) m:set_string("build_pos", pos:offset(-1, 0, z):to_string()) + m:set_string("plot", plot) end gp = pos:offset(1, 0, z) @@ -281,6 +284,7 @@ function ns.place_plot(grid, pos, plot, owner, rot) m = minetest.get_meta(p) m:set_string("grid", grid) m:set_string("build_pos", pos:offset(1, 0, z):to_string()) + m:set_string("plot", plot) end end