Fix and enhance grid shapes in craft guide

Commit 043f6081452365daaa033c58e0738527ccb64c3d made shaped crafting
recipes display in a grid of the recipe's minimum bounding box,
but broke the use of a fixed 1x1 grid shape for cooking recipes.
This commit generalises the dynamic-grid-shape facility, putting the
craft type registration in charge.  This restores the correct shape for
cooking recipes.

Also change the logic for dynamic grid sizes for regular shaped and
shapeless crafting.  We'd like to always use a grid shape that is
reminiscent of a regular crafting grid, as a visual cue to the crafting
method.  But it's also nice to show smaller recipes in a smaller grid.
In the expectation that crafting grids will always be square, show the
recipe in the smallest square grid that will accommodate it.
This commit is contained in:
Zefram 2014-06-13 12:15:50 +01:00 committed by Diego Martinez
parent a8c8ef0890
commit 4c982dd667
2 changed files with 22 additions and 16 deletions

13
api.lua

@ -134,6 +134,13 @@ unified_inventory.register_craft_type("normal", {
description = "Crafting",
width = 3,
height = 3,
get_shaped_craft_width = function (craft) return craft.width end,
dynamic_display_size = function (craft)
local w = craft.width
local h = math.ceil(table.maxn(craft.items) / craft.width)
local g = w < h and h or w
return { width = g, height = g }
end,
uses_crafting_grid = true,
})
@ -142,6 +149,12 @@ unified_inventory.register_craft_type("shapeless", {
description = "Mixing",
width = 3,
height = 3,
dynamic_display_size = function (craft)
local maxn = table.maxn(craft.items)
local g = 1
while g*g < maxn do g = g + 1 end
return { width = g, height = g }
end,
uses_crafting_grid = true,
})

@ -207,24 +207,18 @@ unified_inventory.register_page("craftguide", {
formspec = formspec.."label[6,3.35;No recipes]"
end
local width = craft and craft.width or 0
if width == 0 then
-- Shapeless recipe
width = craft_type.width
end
local display_size = craft_type.dynamic_display_size and craft_type.dynamic_display_size(craft) or { width = craft_type.width, height = craft_type.height }
local craft_width = craft_type.get_shaped_craft_width and craft_type.get_shaped_craft_width(craft) or display_size.width
local height = craft_type.height
if craft then
height = math.ceil(table.maxn(craft.items) / width)
end
local i = 1
-- This keeps recipes aligned to the right,
-- so that they're close to the arrow.
local xoffset = 1 + (3 - width)
for y = 1, height do
for x = 1, width do
local item = craft and craft.items[i]
local xoffset = 1 + (3 - display_size.width)
for y = 1, display_size.height do
for x = 1, display_size.width do
local item
if craft and x <= craft_width then
item = craft.items[(y-1) * craft_width + x]
end
if item then
formspec = formspec..stack_image_button(
xoffset + x, y, 1.1, 1.1,
@ -235,7 +229,6 @@ unified_inventory.register_page("craftguide", {
..tostring(xoffset + x)..","..tostring(y)
..";1,1;ui_blank_image.png;;]"
end
i = i + 1
end
end