Compare commits

...

9 Commits

324
init.lua
View File

@ -24,6 +24,7 @@ craftguide.intllib = S
local remove, maxn, sort, concat = table.remove, table.maxn, table.sort, table.concat local remove, maxn, sort, concat = table.remove, table.maxn, table.sort, table.concat
local vector_add, vector_mul = vector.add, vector.multiply local vector_add, vector_mul = vector.add, vector.multiply
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
local fmt = string.format
local DEFAULT_SIZE = 10 local DEFAULT_SIZE = 10
local MIN_LIMIT, MAX_LIMIT = 10, 12 local MIN_LIMIT, MAX_LIMIT = 10, 12
@ -43,11 +44,23 @@ local group_stereotypes = {
} }
local function extract_groups(str) local function extract_groups(str)
if str:sub(1,6) ~= "group:" then return end if str:sub(1,6) ~= "group:" then
return
end
return str:sub(7):split(",") return str:sub(7):split(",")
end end
local function __func()
return debug.getinfo(2, "n").name
end
function craftguide.register_craft_type(name, def) function craftguide.register_craft_type(name, def)
local func = "craftguide." .. __func() .. "(): "
assert(name, func .. "'name' field missing")
assert(def.description, func .. "'description' field missing")
assert(def.icon, func .. "'icon' field missing")
if not craftguide.craft_types[name] then if not craftguide.craft_types[name] then
craftguide.craft_types[name] = def craftguide.craft_types[name] = def
end end
@ -59,6 +72,12 @@ craftguide.register_craft_type("digging", {
}) })
function craftguide.register_craft(def) function craftguide.register_craft(def)
local func = "craftguide." .. __func() .. "(): "
assert(def.type, func .. "'type' field missing")
assert(def.width, func .. "'width' field missing")
assert(def.output, func .. "'output' field missing")
assert(def.items, func .. "'items' field missing")
craftguide.custom_crafts[#craftguide.custom_crafts + 1] = def craftguide.custom_crafts[#craftguide.custom_crafts + 1] = def
end end
@ -69,8 +88,14 @@ craftguide.register_craft({
items = {"default:stone"}, items = {"default:stone"},
}) })
local function colorize(str) local color_codes = {
return mt.colorize("#FFFF00", str) red = "#FF0000",
yellow = "#FFFF00",
}
local function colorize(str, color)
color = color or "yellow"
return mt.colorize(color_codes[color], str)
end end
local function get_fueltime(item) local function get_fueltime(item)
@ -80,10 +105,10 @@ end
local function reset_datas(data) local function reset_datas(data)
data.show_usage = nil data.show_usage = nil
data.filter = "" data.filter = ""
data.item = nil data.input = nil
data.pagenum = 1 data.pagenum = 1
data.rnum = 1 data.rnum = 1
data.fuel = nil data.items = progressive_mode and data.init_filter_items or datas.init_items
end end
local function in_table(T) local function in_table(T)
@ -166,15 +191,24 @@ local function get_tooltip(item, recipe_type, cooktime, groups)
return has_extras and tooltip .. "]" or "" return has_extras and tooltip .. "]" or ""
end end
local function _get_recipe(iX, iY, xoffset, recipe_num, recipes, show_usage) local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
local fs, recipes_total = {}, #recipes if not recipes[1] then
if recipes_total > 1 then return ""
fs[#fs + 1] = "button[" .. (iX - (sfinv_only and 2.2 or 2.6)) .. "," ..
(iY + (sfinv_only and 3.9 or 3.3)) .. ";2.2,1;alternate;" ..
(show_usage and S("Usage") or S("Recipe")) .. " " ..
S("@1 of @2", recipe_num, recipes_total) .. "]"
end end
local fs = {}
fs[#fs + 1] = fmt("button[%f,%f;%f,%f;%s;%s %u %s %u]",
iX - (sfinv_only and 2.2 or 2.6),
iY + (sfinv_only and 3.9 or 3.3),
2.2,
1,
"alternate",
show_usage and S("Usage") or S("Recipe"),
recipe_num,
S("of"),
#recipes)
local recipe_type = recipes[recipe_num].type local recipe_type = recipes[recipe_num].type
local items = recipes[recipe_num].items local items = recipes[recipe_num].items
local width = recipes[recipe_num].width local width = recipes[recipe_num].width
@ -190,8 +224,10 @@ local function _get_recipe(iX, iY, xoffset, recipe_num, recipes, show_usage)
local rightest, s_btn_size = 0 local rightest, s_btn_size = 0
if recipe_type ~= "cooking" and (width > GRID_LIMIT or rows > GRID_LIMIT) then if recipe_type ~= "cooking" and (width > GRID_LIMIT or rows > GRID_LIMIT) then
fs[#fs + 1] = "label[" .. ((iX / 2) - 2) .. "," .. (iY + 2.2) .. ";" .. fs[#fs + 1] = fmt("label[%f,%f;%s]",
S("Recipe is too big to be displayed (@1x@2)", width, rows) .. "]" (iX / 2) - 2,
iY + 2.2,
S("Recipe is too big to be displayed (@1x@2)", width, rows))
return concat(fs) return concat(fs)
else else
@ -214,60 +250,93 @@ local function _get_recipe(iX, iY, xoffset, recipe_num, recipes, show_usage)
local groups = extract_groups(v) local groups = extract_groups(v)
local label = groups and "\nG" or "" local label = groups and "\nG" or ""
local item_r = group_to_item(v) local item_r = group_to_item(v)
local tltip = get_tooltip(item_r, recipe_type, cooktime, groups) local tltp = get_tooltip(item_r, recipe_type, cooktime, groups)
fs[#fs + 1] = "item_image_button[" .. X .. "," .. fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
(Y + (sfinv_only and 0.7 or 0.2)) .. ";" .. X,
BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" .. item_r .. Y + (sfinv_only and 0.7 or 0.2),
";" .. item_r:match("%S*") .. ";" .. label .. "]" .. tltip BUTTON_SIZE,
BUTTON_SIZE,
item_r,
item_r:match("%S*"),
label)
fs[#fs + 1] = tltp
end end
BUTTON_SIZE = 1.1 BUTTON_SIZE = 1.1
end end
local custom_recipe = craftguide.craft_types[recipe_type] local custom_recipe = craftguide.craft_types[recipe_type]
if recipe_type == "cooking" or (recipe_type == "normal" and width == 0) or if recipe_type == "cooking" or custom_recipe or
custom_recipe then (recipe_type == "normal" and width == 0) then
local icon = recipe_type == "cooking" and "furnace" or "shapeless" local icon = recipe_type == "cooking" and "furnace" or "shapeless"
local coords = (rightest + 1.2) .. "," ..
(iY + (sfinv_only and 2.2 or 1.7)) ..
";0.5,0.5;"
fs[#fs + 1] = "image[" .. coords .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(custom_recipe and custom_recipe.icon or rightest + 1.2,
"craftguide_" .. icon .. ".png^[resize:16x16") .. "]" iY + (sfinv_only and 2.2 or 1.7),
0.5,
0.5,
custom_recipe and custom_recipe.icon or
"craftguide_" .. icon .. ".png^[resize:16x16")
fs[#fs + 1] = "tooltip[" .. coords .. fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
(custom_recipe and custom_recipe.description or rightest + 1.2,
recipe_type:gsub("^%l", string.upper)) .. "]" iY + (sfinv_only and 2.2 or 1.7),
0.5,
0.5,
custom_recipe and custom_recipe.description or
recipe_type:gsub("^%l", string.upper))
end end
local output = recipes[recipe_num].output local output = recipes[recipe_num].output
local output_s = output:match("%S+") local output_s = output:match("%S+")
local output_is_fuel = get_fueltime(output) > 0
local arrow_X = rightest + (s_btn_size or BUTTON_SIZE) local arrow_X = rightest + (s_btn_size or BUTTON_SIZE)
local output_X = arrow_X + 0.9 local output_X = arrow_X + 0.9
fs[#fs + 1] = "image[" .. arrow_X .. "," .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(iY + (sfinv_only and 2.85 or 2.35)) .. arrow_X,
";0.9,0.7;craftguide_arrow.png]" iY + (sfinv_only and 2.85 or 2.35),
0.9,
0.7,
"craftguide_arrow.png")
fs[#fs + 1] = "item_image_button[" .. output_X .. "," .. if output == "BURN" then
(iY + (sfinv_only and 2.7 or 2.2)) .. ";" .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" .. output_X,
output .. ";" .. output_s .. ";]" iY + (sfinv_only and 2.68 or 2.18),
1.1,
1.1,
"craftguide_fire.png")
else
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]",
output_X,
iY + (sfinv_only and 2.7 or 2.2),
BUTTON_SIZE,
BUTTON_SIZE,
output,
output_s)
end
fs[#fs + 1] = get_tooltip(output_s) fs[#fs + 1] = get_tooltip(output_s)
local output_is_fuel = get_fueltime(output) > 0
if output_is_fuel then if output_is_fuel then
fs[#fs + 1] = "image[" .. (output_X + 1) .. "," .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(iY + (sfinv_only and 2.83 or 2.33)) .. output_X + 1,
";0.6,0.4;craftguide_arrow.png]" iY + (sfinv_only and 2.83 or 2.33),
0.6,
0.4,
"craftguide_arrow.png")
fs[#fs + 1] = "image[" .. (output_X + 1.6) .. "," .. fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
(iY + (sfinv_only and 2.68 or 2.18)) .. output_X + 1.6,
";0.6,0.6;craftguide_fire.png]" iY + (sfinv_only and 2.68 or 2.18),
0.6,
0.6,
"craftguide_fire.png")
end end
return concat(fs) return concat(fs)
@ -287,7 +356,6 @@ local function get_formspec(player_name)
local fs = {} local fs = {}
if not sfinv_only then if not sfinv_only then
fs[#fs + 1] = "size[" .. (data.iX - 0.35) .. "," .. (iY + 4) .. ";]" fs[#fs + 1] = "size[" .. (data.iX - 0.35) .. "," .. (iY + 4) .. ";]"
fs[#fs + 1] = "background[1,1;1,1;craftguide_bg.png;true]"
fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]" fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]"
fs[#fs + 1] = "tooltip[size_dec;" .. S("Decrease window size") .. "]" fs[#fs + 1] = "tooltip[size_dec;" .. S("Decrease window size") .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX * 0.47) .. fs[#fs + 1] = "image_button[" .. (data.iX * 0.47) ..
@ -297,6 +365,7 @@ local function get_formspec(player_name)
end end
fs[#fs + 1] = [[ fs[#fs + 1] = [[
"background[1,1;1,1;craftguide_bg.png;true]"
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;] image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;] image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
field_close_on_enter[filter;false] field_close_on_enter[filter;false]
@ -308,8 +377,8 @@ local function get_formspec(player_name)
fs[#fs + 1] = "tooltip[next;" .. S("Next page") .. "]" fs[#fs + 1] = "tooltip[next;" .. S("Next page") .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) .. fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) ..
",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]" ",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]"
fs[#fs + 1] = "label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) .. ",0.22;" .. fs[#fs + 1] = "label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) ..
colorize(data.pagenum) .. " / " .. data.pagemax .. "]" ",0.22;" .. colorize(data.pagenum) .. " / " .. data.pagemax .. "]"
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) - fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) -
(data.iX >= 11 and 0.08 or 0)) .. (data.iX >= 11 and 0.08 or 0)) ..
",0.12;0.8,0.8;craftguide_next_icon.png;next;]" ",0.12;0.8,0.8;craftguide_next_icon.png;next;]"
@ -318,51 +387,40 @@ local function get_formspec(player_name)
local xoffset = data.iX / 2.15 local xoffset = data.iX / 2.15
if not next(data.items) then if not next(data.items) then
fs[#fs + 1] = "label[" .. ((data.iX / 2) - 1) .. fs[#fs + 1] = fmt("label[%f,%f;%s]",
",2;" .. S("No item to show") .. "]" (data.iX / 2) - 1,
2,
S("No item to show"))
end end
local first_item = (data.pagenum - 1) * ipp local first_item = (data.pagenum - 1) * ipp
for i = first_item, first_item + ipp - 1 do for i = first_item, first_item + ipp - 1 do
local name = data.items[i + 1] local name = data.items[i + 1]
if not name then break end if not name then
break
end
local X = i % data.iX local X = i % data.iX
local Y = (i % ipp - X) / data.iX + 1 local Y = (i % ipp - X) / data.iX + 1
fs[#fs + 1] = "item_image_button[" .. fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]",
(X - (sfinv_only and 0 or (X * 0.05))) .. "," .. X - (sfinv_only and 0 or (X * 0.05)),
Y .. ";" .. BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" .. Y,
name .. ";" .. name .. "_inv;]" BUTTON_SIZE,
BUTTON_SIZE,
name,
name)
end end
if data.item and reg_items[data.item] then if data.input and reg_items[data.input] then
if not data.recipes_item or (data.fuel and not get_recipe(data.item).items) then
local X = floor(xoffset) - (sfinv_only and 0 or 0.2)
fs[#fs + 1] = "item_image_button[" .. X .. "," ..
(iY + (sfinv_only and 2.7 or 2.2)) ..
";" .. BUTTON_SIZE .. "," .. BUTTON_SIZE ..
";" .. data.item .. ";" .. data.item .. ";]"
fs[#fs + 1] = "image[" .. (X + 1.1) .. "," ..
(iY + (sfinv_only and 2.85 or 2.35)) ..
";0.9,0.7;craftguide_arrow.png]"
fs[#fs + 1] = get_tooltip(data.item)
fs[#fs + 1] = "image[" .. (X + 2.1) .. "," ..
(iY + (sfinv_only and 2.68 or 2.18)) ..
";1.1,1.1;craftguide_fire.png]"
else
local usage = data.show_usage local usage = data.show_usage
fs[#fs + 1] = _get_recipe(data.iX, fs[#fs + 1] = get_recipe_fs(data.iX,
iY, iY,
xoffset, xoffset,
data.rnum, data.rnum,
(usage and data.usages or data.recipes_item), (usage and data.usages or data.recipes_item),
usage) usage)
end end
end
fs = concat(fs) fs = concat(fs)
data.formspec = fs data.formspec = fs
@ -399,6 +457,7 @@ local function recipe_in_inv(inv, item_name, recipes_f)
end end
end end
end end
if not group_in_inv and not item_in_inv(inv, item) then if not group_in_inv and not item_in_inv(inv, item) then
show_item_recipes[i] = false show_item_recipes[i] = false
end end
@ -562,24 +621,30 @@ local function get_fields(player, ...)
formname, fields = args[1], args[2] formname, fields = args[1], args[2]
end end
if not sfinv_only and formname ~= "craftguide" then return end if not sfinv_only and formname ~= "craftguide" then
return
end
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = datas[player_name]
if fields.clear then if fields.clear then
reset_datas(data) reset_datas(data)
data.items = progressive_mode and data.init_filter_items or datas.init_items
show_fs(player, player_name) show_fs(player, player_name)
elseif fields.alternate then elseif fields.alternate then
local num if #(data.show_usage and data.usages or data.recipes_item) == 1 then
if data.show_usage then return
num = data.usages[data.rnum + 1]
else
num = data.recipes_item[data.rnum + 1]
end end
data.rnum = num and data.rnum + 1 or 1 local next_i
if data.show_usage then
next_i = data.usages[data.rnum + 1]
else
next_i = data.recipes_item[data.rnum + 1]
end
data.rnum = next_i and data.rnum + 1 or 1
show_fs(player, player_name) show_fs(player, player_name)
elseif (fields.key_enter_field == "filter" or fields.search) and elseif (fields.key_enter_field == "filter" or fields.search) and
@ -591,7 +656,6 @@ local function get_fields(player, ...)
elseif fields.prev or fields.next then elseif fields.prev or fields.next then
data.pagenum = data.pagenum - (fields.prev and 1 or -1) data.pagenum = data.pagenum - (fields.prev and 1 or -1)
if data.pagenum > data.pagemax then if data.pagenum > data.pagemax then
data.pagenum = 1 data.pagenum = 1
elseif data.pagenum == 0 then elseif data.pagenum == 0 then
@ -608,49 +672,69 @@ local function get_fields(player, ...)
else for item in pairs(fields) do else for item in pairs(fields) do
if item:find(":") then if item:find(":") then
if item:sub(-4) == "_inv" then if item:find("%s") then
item = item:sub(1,-5)
elseif item:find("%s") then
item = item:match("%S*") item = item:match("%S*")
end end
local is_fuel = get_fueltime(item) > 0 local is_fuel = get_fueltime(item) > 0
local recipes = get_recipes(item) or {} local recipes = get_recipes(item) or {}
recipes = add_custom_recipes(item, recipes) recipes = add_custom_recipes(item, recipes)
if not next(recipes) and not is_fuel then return end
if not data.show_usage and item == data.item and not progressive_mode then local no_recipes = not next(recipes)
data.usages = get_item_usages(item) if no_recipes and not is_fuel then
if next(data.usages) then return
data.show_usage = true
data.rnum = 1
end end
show_fs(player, player_name) if item ~= data.input then
data.show_usage = nil
else else
if progressive_mode then data.show_usage = not data.show_usage
end
if not progressive_mode and is_fuel and no_recipes then
data.show_usage = true
end
if data.show_usage then
data.usages = get_item_usages(item)
if is_fuel then
data.usages[#data.usages + 1] = {
width = 1,
type = "normal",
items = {item},
output = "BURN",
}
end
if not next(data.usages) then
data.show_usage = nil
end
elseif progressive_mode then
local inv = player:get_inventory() local inv = player:get_inventory()
local has_item local has_item
recipes, has_item = recipe_in_inv(inv, item, recipes) recipes, has_item = recipe_in_inv(inv, item, recipes)
if not has_item then return end
if not has_item then
return
end
end end
data.item = item data.input = item
data.recipes_item = recipes data.recipes_item = recipes
data.rnum = 1 data.rnum = 1
data.show_usage = nil
data.fuel = is_fuel
show_fs(player, player_name) show_fs(player, player_name)
end end
end end
end end
end end
end
if sfinv_only then if sfinv_only then
sfinv.register_page("craftguide:craftguide", { sfinv.register_page("craftguide:craftguide", {
title = "Craft Guide", title = "Craft Guide",
get = function(self, player, context) get = function(self, player, context)
local player_name = player:get_player_name() local player_name = player:get_player_name()
return sfinv.make_formspec( return sfinv.make_formspec(
@ -659,6 +743,7 @@ if sfinv_only then
get_formspec(player_name) get_formspec(player_name)
) )
end, end,
on_enter = function(self, player, context) on_enter = function(self, player, context)
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = datas[player_name]
@ -667,6 +752,7 @@ if sfinv_only then
init_datas(player, player_name) init_datas(player, player_name)
end end
end, end,
on_player_receive_fields = function(self, player, context, fields) on_player_receive_fields = function(self, player, context, fields)
get_fields(player, fields) get_fields(player, fields)
end, end,
@ -713,10 +799,12 @@ else
wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125}, wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125},
wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375} wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375}
}, },
on_construct = function(pos) on_construct = function(pos)
local meta = mt.get_meta(pos) local meta = mt.get_meta(pos)
meta:set_string("infotext", S("Crafting Guide Sign")) meta:set_string("infotext", S("Crafting Guide Sign"))
end, end,
on_rightclick = function(pos, node, user, itemstack) on_rightclick = function(pos, node, user, itemstack)
on_use(itemstack, user) on_use(itemstack, user)
end end
@ -750,10 +838,10 @@ else
sfinv_buttons.register_button("craftguide", { sfinv_buttons.register_button("craftguide", {
title = S("Crafting Guide"), title = S("Crafting Guide"),
tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"), tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"),
image = "craftguide_book.png",
action = function(player) action = function(player)
on_use(nil, player) on_use(nil, player)
end, end,
image = "craftguide_book.png",
}) })
end end
end end
@ -779,30 +867,44 @@ if not progressive_mode then
end end
if not node_name then if not node_name then
return false, colorize("[craftguide] ") .. S("No node pointed") return false, colorize("[craftguide] ", "red") ..
S("No node pointed")
elseif not datas[name] then elseif not datas[name] then
init_datas(player, name) init_datas(player, name)
end end
local data = datas[name] local data = datas[name]
reset_datas(data)
local is_fuel = get_fueltime(node_name) > 0 local is_fuel = get_fueltime(node_name) > 0
local recipes = get_recipes(node_name) or {} local recipes = get_recipes(node_name) or {}
recipes = add_custom_recipes(node_name, recipes) recipes = add_custom_recipes(node_name, recipes)
local no_recipes = not next(recipes)
if not next(recipes) and not is_fuel then if no_recipes and not is_fuel then
return false, colorize("[craftguide] ") .. return false, colorize("[craftguide] ", "red") ..
S("No recipe for this node:") .. " " .. S("No recipe for this node:") .. " " ..
colorize(node_name) colorize(node_name)
end end
data.show_usage = nil if is_fuel and no_recipes then
data.filter = "" data.usages = get_item_usages(node_name)
data.item = node_name if is_fuel then
data.pagenum = 1 data.usages[#data.usages + 1] = {
data.rnum = 1 width = 1,
type = "normal",
items = {node_name},
output = "BURN",
}
end
if next(data.usages) then
data.show_usage = true
end
end
data.input = node_name
data.recipes_item = recipes data.recipes_item = recipes
data.items = datas.init_items
data.fuel = is_fuel
return true, show_fs(player, name) return true, show_fs(player, name)
end, end,