VoxeLibre/mods/ITEMS/mcl_banners/patterncraft.lua

418 lines
9.1 KiB
Lua
Raw Normal View History

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;
-- normal 08/15 crafting won't work here.
2017-07-29 00:22:58 +02:00
-- Maximum number of layers which can be put on a banner by crafting.
local max_layers_crafting = 6
2017-07-28 17:23:13 +02:00
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"] = {
name = "%s Bordure",
2017-07-28 17:23:13 +02:00
{ d, d, d },
{ d, e, d },
{ d, d, d },
},
["bricks"] = {
name = "%s 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"] = {
2017-07-29 00:12:21 +02:00
name = "%s Roundel",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, d, e },
{ e, e, e },
},
["creeper"] = {
name = "%s 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"] = {
name = "%s Saltire",
2017-07-28 17:23:13 +02:00
{ d, e, d },
{ e, d, e },
{ d, e, d },
},
["curly_border"] = {
name = "%s Intented Bordure",
2017-07-28 17:41:04 +02:00
type = "shapeless",
{ e, "mcl_core:vine", d },
2017-07-28 17:23:13 +02:00
},
["diagonal_left"] = {
name = "%s Inverted Per Bend",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ d, e, e },
{ d, d, e },
},
["diagonal_right"] = {
name = "%s Inverted Per Bend Sinister",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, d },
{ e, d, d },
},
["diagonal_up_left"] = {
name = "%s Per Bend",
2017-07-28 17:23:13 +02:00
{ e, d, d },
{ e, e, d },
{ e, e, e },
},
["diagonal_up_right"] = {
name = "%s Per Bend Sinister",
2017-07-28 17:23:13 +02:00
{ d, d, e },
{ d, e, e },
{ e, e, e },
},
["flower"] = {
name = "%s 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"] = {
name = "%s Gradient",
2017-07-28 17:23:13 +02:00
{ d, e, d },
{ e, d, e },
{ e, d, e },
},
["gradient_up"] = {
name = "%s Base Gradient",
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ e, d, e },
{ d, e, d },
},
["half_horizontal_bottom"] = {
name = "%s Inverted Per Fess",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ d, d, d },
{ d, d, d },
},
["half_horizontal"] = {
name = "%s Per Fess",
2017-07-28 17:23:13 +02:00
{ d, d, d },
{ d, d, d },
{ e, e, e },
},
["half_vertical"] = {
name = "%s Per Pale",
2017-07-28 17:23:13 +02:00
{ d, d, e },
{ d, d, e },
{ d, d, e },
},
["half_vertical_right"] = {
name = "%s Inverted Per Pale",
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 🙝
name = "%s Thing",
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"] = {
name = "%s Lozenge",
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ d, e, d },
{ e, d, e },
},
["skull"] = {
name = "%s 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"] = {
name = "%s Paly",
2017-07-28 17:23:13 +02:00
{ d, e, d },
{ d, e, d },
{ e, e, e },
},
["square_bottom_left"] = {
name = "%s Base Dexter Canton",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, e },
{ d, e, e },
},
["square_bottom_right"] = {
name = "%s Base Sinister Canton",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, e },
{ e, e, d },
},
["square_top_left"] = {
name = "%s Chief Dexter Canton",
2017-07-28 17:23:13 +02:00
{ d, e, e },
{ e, e, e },
{ e, e, e },
},
["square_top_right"] = {
name = "%s Chief Sinister Canton",
2017-07-28 17:23:13 +02:00
{ e, e, d },
{ e, e, e },
{ e, e, e },
},
["straight_cross"] = {
name = "%s Cross",
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ d, d, d },
{ e, d, e },
},
["stripe_bottom"] = {
name = "%s Base",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, e, e },
{ d, d, d },
},
["stripe_center"] = {
name = "%s Fess",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ d, d, d },
{ e, e, e },
},
["stripe_downleft"] = {
name = "%s Bend Sinister",
2017-07-28 17:23:13 +02:00
{ e, e, d },
{ e, d, e },
{ d, e, e },
},
["stripe_downright"] = {
name = "%s Bend",
2017-07-28 17:23:13 +02:00
{ d, e, e },
{ e, d, e },
{ e, e, d },
},
["stripe_left"] = {
name = "%s Pale Dexter",
2017-07-28 17:23:13 +02:00
{ d, e, e },
{ d, e, e },
{ d, e, e },
},
["stripe_middle"] = {
name = "%s Pale",
2017-07-28 17:23:13 +02:00
{ e, d, e },
{ e, d, e },
{ e, d, e },
},
["stripe_right"] = {
name = "%s Pale Sinister",
2017-07-28 17:23:13 +02:00
{ e, e, d },
{ e, e, d },
{ e, e, d },
},
["stripe_top"] = {
name = "%s Chief",
2017-07-28 17:23:13 +02:00
{ d, d, d },
{ e, e, e },
{ e, e, e },
},
["triangle_bottom"] = {
name = "%s Chevron",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ e, d, e },
{ d, e, d },
},
["triangle_top"] = {
name = "%s Inverted Chevron",
{ d, e, d },
{ e, d, e },
{ e, e, e },
},
2017-07-28 17:23:13 +02:00
["triangles_bottom"] = {
name = "%s Base Indented",
2017-07-28 17:23:13 +02:00
{ e, e, e },
{ d, e, d },
{ e, d, e },
},
["triangles_top"] = {
name = "%s 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
-- This is for handling all those complex pattern crafting recipes
local banner_pattern_craft = function(itemstack, player, old_craft_grid, craft_inv)
if minetest.get_item_group(itemstack:get_name(), "banner") ~= 1 then
return
end
2017-07-28 23:24:02 +02:00
local banner -- banner item
local dye -- itemstring of the dye being used
local banner_index -- crafting inventory index of the banner
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
banner = old_craft_grid[i]
banner_index = i
-- 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
return ItemStack("")
end
-- 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
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]
if inv_i > max_i then
break
end
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
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))
local odesc = itemstack:get_definition().description
local description = mcl_banners.make_advanced_banner_description(odesc, layers)
imeta:set_string("description", description)
return itemstack
end
minetest.register_craft_predict(banner_pattern_craft)
minetest.register_on_craft(banner_pattern_craft)
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