New crafting system is in place and tests work; now have to hook up craft recipes

This commit is contained in:
KaadmY 2017-05-13 15:15:43 -07:00
parent 509827186f
commit 3cef23e65b
9 changed files with 124 additions and 109 deletions

View File

@ -53,41 +53,37 @@ function crafting.register_craft(output, def)
minetest.log("info", "Registered recipe for " .. itemn .. ": " .. dump(crafting.registered_crafts[itemn]))
end
function crafting.craft(output, items)
function crafting.craft(wanted, output, items)
-- `output` can be any ItemStack value
-- Duplicate items in `items` should work correctly
print(dump(output))
local wanted_itemstack = ItemStack(output)
print(dump(wanted_itemstack:to_string()))
local craftdef = crafting.registered_crafts[wanted_itemstack:get_name()]
local craftdef = crafting.registered_crafts[wanted:get_name()]
if craftdef == nil then
minetest.log("warning",
"Tried to craft an unregistered item " .. wanted_itemstack:get_name())
"Tried to craft an unregistered item " .. wanted:get_name())
return nil
end
--print("Craftdef items: " .. dump(craftdef.items))
print("Input before: " .. dump(items))
-- Check for validity
local craft_count = wanted_itemstack:get_count()
local craft_count = wanted:get_count()
for i = 1, crafting.max_inputs do
local required_itemstack = ItemStack(craftdef.items[i])
local itemc = 0
local group = string.match(required_itemstack:get_name(), "group:(.*)")
if required_itemstack ~= nil and required_itemstack:get_count() ~= 0 then
for j = 1, crafting.max_inputs do
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()
end
end
@ -96,28 +92,42 @@ function crafting.craft(output, items)
if craft_count < 1 then
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
--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
local function remove_used_item(itemn, count)
local items_required = count
local group = string.match(itemn, "group:(.*)")
for i = 1, crafting.max_inputs do
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()
print("Taking " .. items_required .. " items from " .. itemn)
input_itemstack:take_item(items_required)
if items_left > 0 then
@ -140,26 +150,30 @@ function crafting.craft(output, items)
end
end
print("Input after: " .. dump(items))
return items
return {items = items, output = output}
end
local form = 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)
local form_crafting = default.ui.get_page("default:2part")
form = form .. "list[current_player;craft;2.25,0.75;3,3;]"
form = form .. "listring[current_player;craft]"
form_crafting = form_crafting .. "list[current_player;main;0.25,4.75;8,4;]"
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 = 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)
default.ui.register_page("crafting:crafting", form)
form_crafting = form_crafting .. "list[current_player;craft_out;7.25,0.25;1,1;]"
form_crafting = form_crafting .. default.ui.get_itemslot_bg(0.25, 0.25, 1, 4)
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)
local form = default.ui.get_page("crafting:crafting")
@ -167,6 +181,52 @@ function crafting.get_formspec(name)
return form
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(
"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")

View File

@ -38,7 +38,9 @@ function default.ui.button(x, y, w, h, name, label, noclip)
nc = "true"
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]"
else
return "image_button["..x..","..y..";"..w..","..h..";ui_button_3w_inactive.png;"..name..";"..label..";"..nc..";false;ui_button_3w_active.png]"

View File

@ -3,7 +3,7 @@
--
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 .. "listring[current_player;main]"
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
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 .. "listring[current_player;main]"
form_furnace = form_furnace .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
@ -142,7 +142,7 @@ minetest.register_abm(
local fuel_time = meta:get_float("fuel_time") or 0
local src_time = meta:get_float("src_time") or 0
local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
--
-- Initialize inventory
--
@ -160,24 +160,24 @@ minetest.register_abm(
local srclist = inv:get_list("src")
local fuellist = inv:get_list("fuel")
local dstlist = inv:get_list("dst")
--
-- Cooking
--
-- Check if we have cookable content
local cooked, aftercooked = minetest.get_craft_result({method = "cooking", width = 1, items = srclist})
local cookable = true
if cooked.time == 0 then
cookable = false
end
-- Check if we have enough fuel to burn
if fuel_time < fuel_totaltime then
-- The furnace is currently active and has enough fuel
fuel_time = fuel_time + 1
-- If there is a cookable item then check if it is ready yet
if cookable then
src_time = src_time + 1
@ -195,7 +195,7 @@ minetest.register_abm(
if cookable then
-- We need to get new fuel
local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
if fuel.time == 0 then
-- No valid fuel in fuel list
fuel_totaltime = 0
@ -204,10 +204,10 @@ minetest.register_abm(
else
-- Take fuel from fuel list
inv:set_stack("fuel", 1, afterfuel.items[1])
fuel_totaltime = fuel.time
fuel_time = 0
end
else
-- We don't need to get new fuel since there is no cookable item
@ -216,7 +216,7 @@ minetest.register_abm(
src_time = 0
end
end
--
-- Update formspec, infotext and node
--
@ -233,7 +233,7 @@ minetest.register_abm(
item_state = "Not cookable"
end
end
local fuel_state = "Empty"
local active = "inactive "
if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
@ -248,9 +248,9 @@ minetest.register_abm(
end
swap_node(pos, "default:furnace")
end
local infotext = "Furnace " .. active .. "(Item: " .. item_state .. "; Fuel: " .. fuel_state .. ")"
--
-- Set meta values
--
@ -262,4 +262,4 @@ minetest.register_abm(
end,
})
default.log("furnace", "loaded")
default.log("furnace", "loaded")

View File

@ -662,7 +662,7 @@ minetest.register_node(
sounds = default.node_sound_wood_defaults(),
on_construct = function(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")
local inv = meta:get_inventory()
inv:set_size("main", 4*2)
@ -1226,7 +1226,7 @@ minetest.register_node(
on_construct = function(pos)
--local n = minetest.get_node(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("text", "")
end,
@ -1483,7 +1483,7 @@ minetest.register_node(
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 .. "listring[current_name;main]"
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 .. 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)
default.ui.register_page("default_chest", form_chest)
default.ui.register_page("default:chest", form_chest)
default.log("nodes", "loaded")

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 B

Binary file not shown.

View File

@ -4,7 +4,7 @@ nav.map_radius = 256
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)
local open_formspecs = {}
@ -29,7 +29,7 @@ function nav.get_waypoints_in_square(pos, radius)
for name, data in pairs(nav.waypoints) do
local wp = data.pos
if wp.x > pos.x-radius and wp.x < pos.x+radius and wp.z > pos.z-radius and wp.z < pos.z+radius then
table.insert(wpts, name)
end
@ -45,7 +45,7 @@ local function get_formspec_waypoint(x, y, name, label, isinfo)
end
local form = ""
form = form .. "image_button["..(x-0.72)..","..(y-0.53)..";0.5,0.5;"..img..";"..name..";;false;false;"..img.."]"
form = form .. "tooltip["..name..";"..minetest.formspec_escape(label).."]"
@ -131,9 +131,9 @@ local function step(dtime)
for _, player in pairs(minetest.get_connected_players()) do
if player ~= nil then
local name = player:get_player_name()
players[name] = player
nav.show_map(player)
end
end
@ -188,4 +188,4 @@ achievements.register_achievement(
description = "Craft a map",
times = 1,
craftitem = "nav:map",
})
})