VoxeLibre/mods/ITEMS/mcl_banners/patterncraft.lua

562 lines
14 KiB
Lua
Raw Normal View History

local S = minetest.get_translator("mcl_banners")
2019-03-14 04:47:56 +01:00
local N = function(s) return s end
2017-07-28 23:24:02 +02:00
-- Pattern crafting. This file contains the code for crafting all the
-- emblazonings you can put on the banners. It's quite complicated;
2019-03-01 17:53:21 +01:00
-- run-of-the-mill crafting won't work here.
2017-07-28 23:24:02 +02:00
2017-07-29 00:22:58 +02:00
-- Maximum number of layers which can be put on a banner by crafting.
2019-03-01 17:53:21 +01:00
local max_layers_crafting = 12
2017-07-28 17:23:13 +02:00
-- Maximum number of layers when banner includes a gradient (workaround, see below).
local max_layers_gradient = 3
-- Max. number lines in the descriptions for the banner layers.
-- This is done to avoid huge tooltips.
local max_layer_lines = 6
2017-07-28 23:24:02 +02:00
-- List of patterns with crafting rules
2017-07-28 17:41:04 +02:00
local d = "group:dye" -- dye
2017-07-28 17:23:13 +02:00
local e = "" -- empty slot (one of them must contain the banner)
local patterns = {
2017-07-28 17:23:13 +02:00
["border"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Bordure"),
2017-07-28 17:23:13 +02:00
{ d, d, d },
{ d, e, d },
{ d, d, d },
},
["bricks"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Bricks"),
2017-07-28 17:23:13 +02:00
type = "shapeless",
2017-07-28 17:41:04 +02:00
{ e, "mcl_core:brick_block", d },
2017-07-28 17:23:13 +02:00
},
["circle"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Roundel"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, d, e },
{ e, e, e },
},
["creeper"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Creeper Charge"),
2017-07-28 17:23:13 +02:00
type = "shapeless",
2017-07-28 17:41:04 +02:00
{ e, "mcl_heads:creeper", d },
2017-07-28 17:23:13 +02:00
},
["cross"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Saltire"),
2017-07-28 17:23:13 +02:00
{ d, e, d },
{ e, d, e },
{ d, e, d },
},
["curly_border"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Bordure Indented"),
2017-07-28 17:41:04 +02:00
type = "shapeless",
{ e, "mcl_core:vine", d },
2017-07-28 17:23:13 +02:00
},
["diagonal_up_left"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Bend Inverted"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ d, e, e },
{ d, d, e },
},
["diagonal_up_right"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Bend Sinister Inverted"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, d },
{ e, d, d },
},
["diagonal_right"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Bend"),
2017-07-28 17:23:13 +02:00
{ e, d, d },
{ e, e, d },
{ e, e, e },
},
["diagonal_left"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Bend Sinister"),
2017-07-28 17:23:13 +02:00
{ d, d, e },
{ d, e, e },
{ e, e, e },
},
["flower"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Flower Charge"),
2017-07-28 17:23:13 +02:00
type = "shapeless",
2017-07-28 17:41:04 +02:00
{ e, "mcl_flowers:oxeye_daisy", d },
2017-07-28 17:23:13 +02:00
},
["gradient"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Gradient"),
2017-07-28 17:23:13 +02:00
{ d, e, d },
{ e, d, e },
{ e, d, e },
},
["gradient_up"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Base Gradient"),
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ e, d, e },
{ d, e, d },
},
["half_horizontal_bottom"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Fess Inverted"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ d, d, d },
{ d, d, d },
},
["half_horizontal"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Fess"),
2017-07-28 17:23:13 +02:00
{ d, d, d },
{ d, d, d },
{ e, e, e },
},
["half_vertical"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Pale"),
2017-07-28 17:23:13 +02:00
{ d, d, e },
{ d, d, e },
{ d, d, e },
},
["half_vertical_right"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Per Pale Inverted"),
2017-07-28 17:23:13 +02:00
{ e, d, d },
{ e, d, d },
{ e, d, d },
},
["thing"] = {
2017-07-28 23:15:03 +02:00
-- Symbol used for the “Thing”: U+1F65D 🙝
2019-03-14 04:47:56 +01:00
name = N("@1 Thing Charge"),
2017-07-28 17:23:13 +02:00
type = "shapeless",
2017-07-28 17:41:04 +02:00
-- TODO: Replace with enchanted golden apple
{ e, "mcl_core:apple_gold", d },
2017-07-28 17:23:13 +02:00
},
["rhombus"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Lozenge"),
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ d, e, d },
{ e, d, e },
},
["skull"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Skull Charge"),
2017-07-28 17:23:13 +02:00
type = "shapeless",
2017-07-28 17:41:04 +02:00
{ e, "mcl_heads:wither_skeleton", d },
2017-07-28 17:23:13 +02:00
},
["small_stripes"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Paly"),
2017-07-28 17:23:13 +02:00
{ d, e, d },
{ d, e, d },
{ e, e, e },
},
["square_bottom_left"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Base Dexter Canton"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, e },
{ d, e, e },
},
["square_bottom_right"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Base Sinister Canton"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, e },
{ e, e, d },
},
["square_top_left"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Chief Dexter Canton"),
2017-07-28 17:23:13 +02:00
{ d, e, e },
{ e, e, e },
{ e, e, e },
},
["square_top_right"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Chief Sinister Canton"),
2017-07-28 17:23:13 +02:00
{ e, e, d },
{ e, e, e },
{ e, e, e },
},
["straight_cross"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Cross"),
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ d, d, d },
{ e, d, e },
},
["stripe_bottom"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Base"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, e },
{ d, d, d },
},
["stripe_center"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Pale"),
{ e, d, e },
{ e, d, e },
{ e, d, e },
2017-07-28 17:23:13 +02:00
},
["stripe_downleft"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Bend Sinister"),
2017-07-28 17:23:13 +02:00
{ e, e, d },
{ e, d, e },
{ d, e, e },
},
["stripe_downright"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Bend"),
2017-07-28 17:23:13 +02:00
{ d, e, e },
{ e, d, e },
{ e, e, d },
},
["stripe_left"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Pale Dexter"),
2017-07-28 17:23:13 +02:00
{ d, e, e },
{ d, e, e },
{ d, e, e },
},
["stripe_middle"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Fess"),
{ e, e, e },
{ d, d, d },
{ e, e, e },
2017-07-28 17:23:13 +02:00
},
["stripe_right"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Pale Sinister"),
2017-07-28 17:23:13 +02:00
{ e, e, d },
{ e, e, d },
{ e, e, d },
},
["stripe_top"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Chief"),
2017-07-28 17:23:13 +02:00
{ d, d, d },
{ e, e, e },
{ e, e, e },
},
["triangle_bottom"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Chevron"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, d, e },
{ d, e, d },
},
["triangle_top"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Chevron Inverted"),
{ d, e, d },
{ e, d, e },
{ e, e, e },
},
2017-07-28 17:23:13 +02:00
["triangles_bottom"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Base Indented"),
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ d, e, d },
{ e, d, e },
},
["triangles_top"] = {
2019-03-14 04:47:56 +01:00
name = N("@1 Chief Indented"),
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ d, e, d },
{ e, e, e },
},
}
2017-07-28 23:24:02 +02:00
-- Just a simple reverse-lookup table from dye itemstring to banner color ID
-- to avoid some pointless future iterations.
local dye_to_colorid_mapping = {}
for colorid, colortab in pairs(mcl_banners.colors) do
dye_to_colorid_mapping[colortab[5]] = colorid
end
-- Create a banner description containing all the layer names
mcl_banners.make_advanced_banner_description = function(description, layers)
if layers == nil or #layers == 0 then
-- No layers, revert to default
return ""
else
local layerstrings = {}
for l=1, #layers do
-- Prevent excess length description
if l > max_layer_lines then
break
end
-- Layer text line.
local color = mcl_banners.colors[layers[l].color][6]
local pattern_name = patterns[layers[l].pattern].name
2019-03-14 04:47:56 +01:00
-- The pattern name is a format string
-- (e.g. “@1 Base” → “Yellow Base”)
table.insert(layerstrings, S(pattern_name, S(color)))
end
-- Warn about missing information
if #layers == max_layer_lines + 1 then
2019-03-14 04:47:56 +01:00
table.insert(layerstrings, S("And one additional layer"))
elseif #layers > max_layer_lines + 1 then
2019-03-14 04:47:56 +01:00
table.insert(layerstrings, S("And @1 additional layers", #layers - max_layer_lines))
end
-- Final string concatenations: Just a list of strings
local append = table.concat(layerstrings, "\n")
description = description .. "\n" .. minetest.colorize(mcl_colors.GRAY, append)
return description
end
end
--[[ This is for handling all those complex pattern crafting recipes.
Parameters same as for minetest.register_craft_predict.
craft_predict is set true when called from minetest.craft_preview, in this case, this function
MUST NOT change the crafting grid.
]]
local banner_pattern_craft = function(itemstack, player, old_craft_grid, craft_inv, craft_predict)
if minetest.get_item_group(itemstack:get_name(), "banner") ~= 1 then
return
end
--[[ Basic item checks: Banners and dyes ]]
2017-07-28 23:24:02 +02:00
local banner -- banner item
2017-07-29 02:44:51 +02:00
local banner2 -- second banner item (used when copying)
2017-07-28 23:24:02 +02:00
local dye -- itemstring of the dye being used
local banner_index -- crafting inventory index of the banner
local banner2_index
for i = 1, player:get_inventory():get_size("craft") do
local itemname = old_craft_grid[i]:get_name()
if minetest.get_item_group(itemname, "banner") == 1 then
2017-07-29 02:44:51 +02:00
if not banner then
banner = old_craft_grid[i]
banner_index = i
elseif not banner2 then
banner2 = old_craft_grid[i]
banner2_index = i
2017-07-29 02:44:51 +02:00
else
return
end
-- Check if all dyes are equal
elseif minetest.get_item_group(itemname, "dye") == 1 then
if dye == nil then
dye = itemname
elseif itemname ~= dye then
return ItemStack("")
end
end
end
if not banner then
2017-07-29 02:44:51 +02:00
return
end
--[[ Check copy ]]
2017-07-29 02:44:51 +02:00
if banner2 then
-- Two banners found: This means copying!
local b1meta = banner:get_meta()
local b2meta = banner2:get_meta()
local b1layers_raw = b1meta:get_string("layers")
local b2layers_raw = b2meta:get_string("layers")
local b1layers = minetest.deserialize(b1layers_raw)
local b2layers = minetest.deserialize(b2layers_raw)
if type(b1layers) ~= "table" then
b1layers = {}
end
if type(b2layers) ~= "table" then
b2layers = {}
end
2017-07-29 02:44:51 +02:00
-- For copying to be allowed, one banner has to have no layers while the other one has at least 1 layer.
-- The banner with layers will be used as a source.
2018-02-04 06:11:08 +01:00
local src_banner, src_layers, src_layers_raw, src_desc, src_index
if #b1layers == 0 and #b2layers > 0 then
src_banner = banner2
2018-02-02 05:29:35 +01:00
src_layers = b2layers
2017-07-29 02:44:51 +02:00
src_layers_raw = b2layers_raw
2018-02-02 05:29:35 +01:00
src_desc = minetest.registered_items[src_banner:get_name()].description
src_index = banner2_index
2018-02-04 06:11:08 +01:00
elseif #b2layers == 0 and #b1layers > 0 then
src_banner = banner
2018-02-02 05:29:35 +01:00
src_layers = b1layers
2017-07-29 02:44:51 +02:00
src_layers_raw = b1layers_raw
2018-02-02 05:29:35 +01:00
src_desc = minetest.registered_items[src_banner:get_name()].description
src_index = banner_index
2017-07-29 02:44:51 +02:00
else
return ItemStack("")
end
-- Set output metadata
local imeta = itemstack:get_meta()
imeta:set_string("layers", src_layers_raw)
2018-02-02 05:29:35 +01:00
-- Generate new description. This clears any (anvil) name from the original banners.
2018-02-04 06:11:08 +01:00
imeta:set_string("description", mcl_banners.make_advanced_banner_description(src_desc, src_layers))
2017-07-29 02:44:51 +02:00
if not craft_predict then
-- Don't destroy source banner so this recipe is a true copy
craft_inv:set_stack("craft", src_index, src_banner)
end
2017-07-29 02:44:51 +02:00
return itemstack
end
-- No two banners found
-- From here on we check which banner pattern should be added
--[[ Check patterns ]]
2017-07-29 02:44:51 +02:00
-- Get old layers
local ometa = banner:get_meta()
local layers_raw = ometa:get_string("layers")
local layers = minetest.deserialize(layers_raw)
if type(layers) ~= "table" then
layers = {}
end
2017-07-29 00:22:58 +02:00
-- Disallow crafting when a certain number of layers is reached or exceeded
if #layers >= max_layers_crafting then
return ItemStack("")
end
-- Lower layer limit when banner includes any gradient.
2019-03-01 18:32:10 +01:00
-- Workaround to circumvent Minetest bug (https://github.com/minetest/minetest/issues/6210)
-- TODO: Remove this restriction when bug #6210 is fixed.
if #layers >= max_layers_gradient then
for l=1, #layers do
if layers[l].pattern == "gradient" or layers[l].pattern == "gradient_up" then
return ItemStack("")
end
end
end
local matching_pattern
local max_i = player:get_inventory():get_size("craft")
-- Find the matching pattern
for pattern_name, pattern in pairs(patterns) do
-- Shaped / fixed
if pattern.type == nil then
local pattern_ok = true
local inv_i = 1
-- This complex code just iterates through the pattern slots one-by-one and compares them with the pattern
for p=1, #pattern do
local row = pattern[p]
for r=1, #row do
local itemname = old_craft_grid[inv_i]:get_name()
local pitem = row[r]
if (pitem == d and minetest.get_item_group(itemname, "dye") == 0) or (pitem == e and itemname ~= e and inv_i ~= banner_index) then
pattern_ok = false
break
else
end
inv_i = inv_i + 1
if inv_i > max_i then
break
end
end
if inv_i > max_i then
break
end
end
-- Everything matched! We found our pattern!
if pattern_ok then
matching_pattern = pattern_name
break
end
elseif pattern.type == "shapeless" then
2017-07-29 00:12:21 +02:00
local orig = pattern[1]
local no_mismatches_so_far = true
-- This code compares the craft grid with the required items
for o=1, #orig do
local item_ok = false
for i=1, max_i do
local itemname = old_craft_grid[i]:get_name()
if (orig[o] == e) or -- Empty slot: Always wins
(orig[o] ~= e and orig[o] == itemname) or -- non-empty slot: Exact item match required
(orig[o] == d and minetest.get_item_group(itemname, "dye") == 1) then -- Dye slot
item_ok = true
break
end
end
-- Sorry, item not found. :-(
if not item_ok then
no_mismatches_so_far = false
break
end
end
-- Ladies and Gentlemen, we have a winner!
if no_mismatches_so_far then
matching_pattern = pattern_name
break
end
end
if matching_pattern then
break
end
end
if not matching_pattern then
return ItemStack("")
end
-- Add the new layer and update other metadata
2017-07-28 23:24:02 +02:00
local color = dye_to_colorid_mapping[dye]
table.insert(layers, {pattern=matching_pattern, color=color})
local imeta = itemstack:get_meta()
imeta:set_string("layers", minetest.serialize(layers))
2018-02-02 05:07:58 +01:00
local mname = ometa:get_string("name")
-- Only change description if banner does not have a name
if mname == "" then
local odesc = itemstack:get_definition().description
local description = mcl_banners.make_advanced_banner_description(odesc, layers)
imeta:set_string("description", description)
else
2018-02-04 06:36:44 +01:00
imeta:set_string("description", ometa:get_string("description"))
imeta:set_string("name", mname)
2018-02-02 05:07:58 +01:00
end
return itemstack
end
minetest.register_craft_predict(function(itemstack, player, old_craft_grid, craft_inv)
return banner_pattern_craft(itemstack, player, old_craft_grid, craft_inv, true)
end)
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
return banner_pattern_craft(itemstack, player, old_craft_grid, craft_inv, false)
end)
2017-07-28 17:23:13 +02:00
-- Register crafting recipes for all the patterns
for pattern_name, pattern in pairs(patterns) do
2017-07-28 17:41:04 +02:00
-- Shaped and fixed recipes
2017-07-28 17:23:13 +02:00
if pattern.type == nil then
for colorid, colortab in pairs(mcl_banners.colors) do
local banner = "mcl_banners:banner_item_"..colortab[1]
local bannered = false
local recipe = {}
for row_id=1, #pattern do
local row = pattern[row_id]
local newrow = {}
for r=1, #row do
2017-07-28 17:41:04 +02:00
if row[r] == e and not bannered then
2017-07-28 17:23:13 +02:00
newrow[r] = banner
bannered = true
else
newrow[r] = row[r]
end
end
table.insert(recipe, newrow)
end
minetest.register_craft({
2017-07-28 17:41:04 +02:00
output = banner,
2017-07-28 17:23:13 +02:00
recipe = recipe,
})
end
2017-07-28 17:41:04 +02:00
-- Shapeless recipes
2017-07-28 17:23:13 +02:00
elseif pattern.type == "shapeless" then
2017-07-28 17:41:04 +02:00
for colorid, colortab in pairs(mcl_banners.colors) do
local banner = "mcl_banners:banner_item_"..colortab[1]
local orig = pattern[1]
local recipe = {}
for r=1, #orig do
if orig[r] == e then
recipe[r] = banner
else
recipe[r] = orig[r]
end
end
minetest.register_craft({
type = "shapeless",
output = banner,
recipe = recipe,
})
end
2017-07-28 17:23:13 +02:00
end
end
2017-07-29 02:44:51 +02:00
-- Register crafting recipe for copying the banner pattern
for colorid, colortab in pairs(mcl_banners.colors) do
local banner = "mcl_banners:banner_item_"..colortab[1]
minetest.register_craft({
type = "shapeless",
output = banner,
recipe = { banner, banner },
})
end