New crafting system is in place and tests work; now have to hook up craft recipes
This commit is contained in:
parent
509827186f
commit
3cef23e65b
@ -53,41 +53,37 @@ function crafting.register_craft(output, def)
|
|||||||
minetest.log("info", "Registered recipe for " .. itemn .. ": " .. dump(crafting.registered_crafts[itemn]))
|
minetest.log("info", "Registered recipe for " .. itemn .. ": " .. dump(crafting.registered_crafts[itemn]))
|
||||||
end
|
end
|
||||||
|
|
||||||
function crafting.craft(output, items)
|
function crafting.craft(wanted, output, items)
|
||||||
-- `output` can be any ItemStack value
|
-- `output` can be any ItemStack value
|
||||||
-- Duplicate items in `items` should work correctly
|
-- Duplicate items in `items` should work correctly
|
||||||
|
|
||||||
print(dump(output))
|
local craftdef = crafting.registered_crafts[wanted:get_name()]
|
||||||
|
|
||||||
local wanted_itemstack = ItemStack(output)
|
|
||||||
|
|
||||||
print(dump(wanted_itemstack:to_string()))
|
|
||||||
|
|
||||||
local craftdef = crafting.registered_crafts[wanted_itemstack:get_name()]
|
|
||||||
|
|
||||||
if craftdef == nil then
|
if craftdef == nil then
|
||||||
minetest.log("warning",
|
minetest.log("warning",
|
||||||
"Tried to craft an unregistered item " .. wanted_itemstack:get_name())
|
"Tried to craft an unregistered item " .. wanted:get_name())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--print("Craftdef items: " .. dump(craftdef.items))
|
|
||||||
print("Input before: " .. dump(items))
|
|
||||||
|
|
||||||
-- Check for validity
|
-- Check for validity
|
||||||
|
|
||||||
local craft_count = wanted_itemstack:get_count()
|
local craft_count = wanted:get_count()
|
||||||
|
|
||||||
for i = 1, crafting.max_inputs do
|
for i = 1, crafting.max_inputs do
|
||||||
local required_itemstack = ItemStack(craftdef.items[i])
|
local required_itemstack = ItemStack(craftdef.items[i])
|
||||||
local itemc = 0
|
local itemc = 0
|
||||||
|
|
||||||
|
local group = string.match(required_itemstack:get_name(), "group:(.*)")
|
||||||
|
|
||||||
if required_itemstack ~= nil and required_itemstack:get_count() ~= 0 then
|
if required_itemstack ~= nil and required_itemstack:get_count() ~= 0 then
|
||||||
for j = 1, crafting.max_inputs do
|
for j = 1, crafting.max_inputs do
|
||||||
local input_itemstack = ItemStack(items[j])
|
local input_itemstack = ItemStack(items[j])
|
||||||
|
|
||||||
if input_itemstack:get_name() == required_itemstack:get_name() then
|
if (group ~= nil
|
||||||
|
and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0)
|
||||||
|
or (input_itemstack ~= nil
|
||||||
|
and input_itemstack:get_name() == required_itemstack:get_name()) then
|
||||||
itemc = itemc + input_itemstack:get_count()
|
itemc = itemc + input_itemstack:get_count()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -96,28 +92,42 @@ function crafting.craft(output, items)
|
|||||||
|
|
||||||
if craft_count < 1 then
|
if craft_count < 1 then
|
||||||
minetest.log("warning",
|
minetest.log("warning",
|
||||||
"Not enough items to craft " .. wanted_itemstack:get_name())
|
"Not enough items to craft " .. wanted:to_string())
|
||||||
|
|
||||||
return nil -- Not enough items
|
return nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--print("Craft count: " .. craft_count .. "/" .. output.count)
|
-- Put stuff in output stack
|
||||||
|
|
||||||
|
craft_count = math.min(craft_count, math.floor(output:get_free_space() / craft_count))
|
||||||
|
|
||||||
|
if craft_count < 1 then
|
||||||
|
minetest.log("warning",
|
||||||
|
"Output stack too full to hold " .. wanted:to_string())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
output:add_item(ItemStack({name = wanted:get_name(), count = wanted:get_count() * craft_count}))
|
||||||
|
|
||||||
-- Iterate through second time to take items used for crafting
|
-- Iterate through second time to take items used for crafting
|
||||||
|
|
||||||
local function remove_used_item(itemn, count)
|
local function remove_used_item(itemn, count)
|
||||||
local items_required = count
|
local items_required = count
|
||||||
|
|
||||||
|
local group = string.match(itemn, "group:(.*)")
|
||||||
|
|
||||||
for i = 1, crafting.max_inputs do
|
for i = 1, crafting.max_inputs do
|
||||||
local input_itemstack = ItemStack(items[i])
|
local input_itemstack = ItemStack(items[i])
|
||||||
|
|
||||||
if items[i] ~= nil and input_itemstack:get_name() == itemn then
|
if (group ~= nil
|
||||||
|
and minetest.get_item_group(input_itemstack:get_name(), group) ~= 0)
|
||||||
|
or (items[i] ~= nil
|
||||||
|
and input_itemstack:get_name() == itemn) then
|
||||||
local items_left = items_required - input_itemstack:get_count()
|
local items_left = items_required - input_itemstack:get_count()
|
||||||
|
|
||||||
print("Taking " .. items_required .. " items from " .. itemn)
|
|
||||||
|
|
||||||
input_itemstack:take_item(items_required)
|
input_itemstack:take_item(items_required)
|
||||||
|
|
||||||
if items_left > 0 then
|
if items_left > 0 then
|
||||||
@ -140,26 +150,30 @@ function crafting.craft(output, items)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Input after: " .. dump(items))
|
return {items = items, output = output}
|
||||||
|
|
||||||
return items
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local form = default.ui.get_page("default:2part")
|
local form_crafting = default.ui.get_page("default:2part")
|
||||||
form = form .. "list[current_player;main;0.25,4.75;8,4;]"
|
|
||||||
form = form .. "listring[current_player;main]"
|
|
||||||
form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
|
||||||
form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
|
|
||||||
|
|
||||||
form = form .. "list[current_player;craft;2.25,0.75;3,3;]"
|
form_crafting = form_crafting .. "list[current_player;main;0.25,4.75;8,4;]"
|
||||||
form = form .. "listring[current_player;craft]"
|
form_crafting = form_crafting .. "listring[current_player;main]"
|
||||||
|
form_crafting = form_crafting .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
||||||
|
form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
|
||||||
|
|
||||||
form = form .. "image[5.25,1.75;1,1;ui_arrow_bg.png^[transformR270]"
|
form_crafting = form_crafting .. "list[current_player;craft_in;0.25,0.25;1,4;]"
|
||||||
|
form_crafting = form_crafting .. "listring[current_player;craft_in]"
|
||||||
|
|
||||||
form = form .. "list[current_player;craftpreview;6.25,1.75;1,1;]"
|
form_crafting = form_crafting .. "list[current_player;craft_out;7.25,0.25;1,1;]"
|
||||||
form = form .. default.ui.get_itemslot_bg(2.25, 0.75, 3, 3)
|
|
||||||
form = form .. default.ui.get_itemslot_bg(6.25, 1.75, 1, 1)
|
form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 0.25, 1, 4)
|
||||||
default.ui.register_page("crafting:crafting", form)
|
form_crafting = form_crafting .. default.ui.get_itemslot_bg(7.25, 0.25, 1, 1)
|
||||||
|
|
||||||
|
form_crafting = form_crafting .. "image[7.25,1.25;1,1;ui_arrow_bg.png]"
|
||||||
|
|
||||||
|
form_crafting = form_crafting .. default.ui.button(7.25, 2.25, 1, 1, "do_craft_1", "1")
|
||||||
|
form_crafting = form_crafting .. default.ui.button(7.25, 3.25, 1, 1, "do_craft_10", "10")
|
||||||
|
|
||||||
|
default.ui.register_page("crafting:crafting", form_crafting)
|
||||||
|
|
||||||
function crafting.get_formspec(name)
|
function crafting.get_formspec(name)
|
||||||
local form = default.ui.get_page("crafting:crafting")
|
local form = default.ui.get_page("crafting:crafting")
|
||||||
@ -167,6 +181,52 @@ function crafting.get_formspec(name)
|
|||||||
return form
|
return form
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function on_joinplayer(player)
|
||||||
|
local inv = player:get_inventory()
|
||||||
|
|
||||||
|
if inv:get_size("craft_in") ~= 4 then
|
||||||
|
inv:set_size("craft_in", 4)
|
||||||
|
end
|
||||||
|
|
||||||
|
if inv:get_size("craft_out") ~= 1 then
|
||||||
|
inv:set_size("craft_out", 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function on_player_receive_fields(player, form_name, fields)
|
||||||
|
if form_name ~= "crafting:crafting" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local inv = player:get_inventory()
|
||||||
|
|
||||||
|
local wanted_itemstack = ItemStack("default:stone 2")
|
||||||
|
local output_itemstack = inv:get_stack("craft_out", 1)
|
||||||
|
|
||||||
|
if output_itemstack:get_name() ~= wanted_itemstack:get_name() and output_itemstack:get_count() ~= 0 then
|
||||||
|
minetest.log("warning", "Trying to craft " .. wanted_itemstack:to_string() .. " but output has too many/different items")
|
||||||
|
end
|
||||||
|
|
||||||
|
if fields.do_craft_1 then
|
||||||
|
wanted_itemstack:set_count(1)
|
||||||
|
elseif fields.do_craft_10 then
|
||||||
|
wanted_itemstack:set_count(2)
|
||||||
|
else
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local crafted = crafting.craft(wanted_itemstack, output_itemstack, inv:get_list("craft_in"))
|
||||||
|
|
||||||
|
if crafted and inv:room_for_item("craft_out", crafted.output) then
|
||||||
|
inv:add_item("craft_out", crafted.output)
|
||||||
|
|
||||||
|
inv:set_list("craft_in", crafted.items)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
minetest.register_on_joinplayer(on_joinplayer)
|
||||||
|
minetest.register_on_player_receive_fields(on_player_receive_fields)
|
||||||
|
|
||||||
crafting.register_craft(
|
crafting.register_craft(
|
||||||
"default:stone 4",
|
"default:stone 4",
|
||||||
{
|
{
|
||||||
@ -177,51 +237,4 @@ crafting.register_craft(
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
crafting.craft(
|
|
||||||
"default:stone 2",
|
|
||||||
{
|
|
||||||
"default:stick 4", -- 0 leftover
|
|
||||||
{name = "default:stick", count = 5}, -- 3 leftover
|
|
||||||
"default:fiber 9", -- 5 leftover
|
|
||||||
{name = "group:stone", count = 4}, -- 0 leftover
|
|
||||||
})
|
|
||||||
|
|
||||||
local function on_player_recieve_fields(player, form_name, fields)
|
|
||||||
if form_name ~= "core:crafting" or fields.cancel then return end
|
|
||||||
|
|
||||||
local inv = player:get_inventory()
|
|
||||||
|
|
||||||
if fields.trade then
|
|
||||||
local item = player:get_wielded_item()
|
|
||||||
|
|
||||||
local trade_wanted1 = inv:get_stack("gold_trade_wanted", 1):to_string()
|
|
||||||
local trade_wanted2 = inv:get_stack("gold_trade_wanted", 2):to_string()
|
|
||||||
|
|
||||||
local trade_in1 = inv:get_stack("gold_trade_in", 1):to_string()
|
|
||||||
local trade_in2 = inv:get_stack("gold_trade_in", 2):to_string()
|
|
||||||
|
|
||||||
local matches = trade_wanted1 == trade_in1 and trade_wanted2 == trade_in2
|
|
||||||
|
|
||||||
local meta = minetest.deserialize(item:get_metadata())
|
|
||||||
|
|
||||||
local trade = {"gold:gold", "gold:gold", "default:stick"}
|
|
||||||
local trade_type = ""
|
|
||||||
|
|
||||||
if meta then
|
|
||||||
trade = meta.trade
|
|
||||||
trade_type = meta.trade_type
|
|
||||||
end
|
|
||||||
|
|
||||||
if matches then
|
|
||||||
if inv:room_for_item("gold_trade_out", trade[3]) then
|
|
||||||
inv:add_item("gold_trade_out", trade[3])
|
|
||||||
inv:set_stack("gold_trade_in", 1, "")
|
|
||||||
inv:set_stack("gold_trade_in", 2, "")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(on_player_recieve_fields)
|
|
||||||
|
|
||||||
default.log("mod:crafting", "loaded")
|
default.log("mod:crafting", "loaded")
|
||||||
|
@ -38,7 +38,9 @@ function default.ui.button(x, y, w, h, name, label, noclip)
|
|||||||
nc = "true"
|
nc = "true"
|
||||||
end
|
end
|
||||||
|
|
||||||
if w == 2 then
|
if w == 1 then
|
||||||
|
return "image_button["..x..","..y..";"..w..","..h..";ui_button_1w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_1w_active.png]"
|
||||||
|
elseif w == 2 then
|
||||||
return "image_button["..x..","..y..";"..w..","..h..";ui_button_2w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_2w_active.png]"
|
return "image_button["..x..","..y..";"..w..","..h..";ui_button_2w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_2w_active.png]"
|
||||||
else
|
else
|
||||||
return "image_button["..x..","..y..";"..w..","..h..";ui_button_3w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_3w_active.png]"
|
return "image_button["..x..","..y..";"..w..","..h..";ui_button_3w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_3w_active.png]"
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
--
|
--
|
||||||
|
|
||||||
function default.furnace_active_formspec(percent, item_percent)
|
function default.furnace_active_formspec(percent, item_percent)
|
||||||
local form = default.ui.get_page("core_2part")
|
local form = default.ui.get_page("default:2part")
|
||||||
form = form .. "list[current_player;main;0.25,4.75;8,4;]"
|
form = form .. "list[current_player;main;0.25,4.75;8,4;]"
|
||||||
form = form .. "listring[current_player;main]"
|
form = form .. "listring[current_player;main]"
|
||||||
form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
||||||
@ -28,7 +28,7 @@ function default.furnace_active_formspec(percent, item_percent)
|
|||||||
return form
|
return form
|
||||||
end
|
end
|
||||||
|
|
||||||
local form_furnace = default.ui.get_page("core_2part")
|
local form_furnace = default.ui.get_page("default:2part")
|
||||||
form_furnace = form_furnace .. "list[current_player;main;0.25,4.75;8,4;]"
|
form_furnace = form_furnace .. "list[current_player;main;0.25,4.75;8,4;]"
|
||||||
form_furnace = form_furnace .. "listring[current_player;main]"
|
form_furnace = form_furnace .. "listring[current_player;main]"
|
||||||
form_furnace = form_furnace .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
form_furnace = form_furnace .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
||||||
|
@ -662,7 +662,7 @@ minetest.register_node(
|
|||||||
sounds = default.node_sound_wood_defaults(),
|
sounds = default.node_sound_wood_defaults(),
|
||||||
on_construct = function(pos)
|
on_construct = function(pos)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
meta:set_string("formspec", default.ui.get_page("core_bookshelf"))
|
meta:set_string("formspec", default.ui.get_page("default:bookshelf"))
|
||||||
meta:set_string("infotext", "Bookshelf")
|
meta:set_string("infotext", "Bookshelf")
|
||||||
local inv = meta:get_inventory()
|
local inv = meta:get_inventory()
|
||||||
inv:set_size("main", 4*2)
|
inv:set_size("main", 4*2)
|
||||||
@ -1226,7 +1226,7 @@ minetest.register_node(
|
|||||||
on_construct = function(pos)
|
on_construct = function(pos)
|
||||||
--local n = minetest.get_node(pos)
|
--local n = minetest.get_node(pos)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
meta:set_string("formspec", default.ui.get_page("core_field"))
|
meta:set_string("formspec", default.ui.get_page("default:field"))
|
||||||
meta:set_string("infotext", '""')
|
meta:set_string("infotext", '""')
|
||||||
meta:set_string("text", "")
|
meta:set_string("text", "")
|
||||||
end,
|
end,
|
||||||
@ -1483,7 +1483,7 @@ minetest.register_node(
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
local form_chest = default.ui.get_page("core_2part")
|
local form_chest = default.ui.get_page("default:2part")
|
||||||
form_chest = form_chest .. "list[current_name;main;0.25,0.25;8,4;]"
|
form_chest = form_chest .. "list[current_name;main;0.25,0.25;8,4;]"
|
||||||
form_chest = form_chest .. "listring[current_name;main]"
|
form_chest = form_chest .. "listring[current_name;main]"
|
||||||
form_chest = form_chest .. default.ui.get_itemslot_bg(0.25, 0.25, 8, 4)
|
form_chest = form_chest .. default.ui.get_itemslot_bg(0.25, 0.25, 8, 4)
|
||||||
@ -1492,6 +1492,6 @@ form_chest = form_chest .. "list[current_player;main;0.25,4.75;8,4;]"
|
|||||||
form_chest = form_chest .. "listring[current_player;main]"
|
form_chest = form_chest .. "listring[current_player;main]"
|
||||||
form_chest = form_chest .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
form_chest = form_chest .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
||||||
form_chest = form_chest .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
|
form_chest = form_chest .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
|
||||||
default.ui.register_page("default_chest", form_chest)
|
default.ui.register_page("default:chest", form_chest)
|
||||||
|
|
||||||
default.log("nodes", "loaded")
|
default.log("nodes", "loaded")
|
||||||
|
BIN
mods/default/textures/ui_button_1w_active.png
Normal file
BIN
mods/default/textures/ui_button_1w_active.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 294 B |
BIN
mods/default/textures/ui_button_1w_active.xcf
Normal file
BIN
mods/default/textures/ui_button_1w_active.xcf
Normal file
Binary file not shown.
BIN
mods/default/textures/ui_button_1w_inactive.png
Normal file
BIN
mods/default/textures/ui_button_1w_inactive.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 325 B |
BIN
mods/default/textures/ui_button_1w_inactive.xcf
Normal file
BIN
mods/default/textures/ui_button_1w_inactive.xcf
Normal file
Binary file not shown.
@ -4,7 +4,7 @@ nav.map_radius = 256
|
|||||||
|
|
||||||
nav.waypoints = {}
|
nav.waypoints = {}
|
||||||
|
|
||||||
local form_nav = default.ui.get_page("core")
|
local form_nav = default.ui.get_page("default:default")
|
||||||
default.ui.register_page("nav:nav", form_nav)
|
default.ui.register_page("nav:nav", form_nav)
|
||||||
|
|
||||||
local open_formspecs = {}
|
local open_formspecs = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user