Add recipe indexing to towns

This commit is contained in:
Signal 2025-11-06 22:17:25 -05:00
parent 00e1fd985d
commit d5f198e5e1
3 changed files with 30 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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