Hide repair recipes if disable_repair=1 is set
[MineClone/MineClone2.git] / mods / HELP / mcl_craftguide / init.lua
blob6b3d4132831d423463376cc333646664157fcbee
1 mcl_craftguide = {}
3 local craftguide, datas, mt = {}, {}, minetest
4 -- Progressive Mode:
5 -- true: Only show recipes which include at least one of the items the player posesses
6 -- false: Show all crafting recipes
7 local progressive_mode = false
8 if mt.settings:get_bool("craftguide_progressive_mode") == true then
9 progressive_mode = true
10 end
11 local get_recipe = mt.get_craft_recipe
12 local get_result, show_formspec = mt.get_craft_result, mt.show_formspec
13 local reg_items = mt.registered_items
15 local get_recipes = function(query_item)
16 local recipes = mt.get_all_craft_recipes(query_item)
18 -- Manually add repairing recipes (workaround, because get_all_craft_recipes
19 -- doesn't return repairing recipes)
20 if minetest.get_modpath("mcl_core") then
21 local def = minetest.registered_items[query_item]
22 if not def then
23 return
24 end
25 if def.type == "tool" then
26 if recipes == nil then
27 recipes = {}
28 end
29 if minetest.get_item_group(query_item, "disable_repair") ~= 1 then
30 table.insert(recipes, {
31 type = "normal",
32 width = 0,
33 items = { [1] = query_item, [2] = query_item },
34 output = query_item,
35 -- Special marker for repairing recipes
36 _is_toolrepair = true,
38 end
39 end
40 end
41 return recipes
42 end
44 -- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
45 -- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
46 local remove, maxn, sort = table.remove, table.maxn, table.sort
47 local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
49 local group_stereotypes = {
50 wool = "mcl_wool:white",
51 carpet = "mcl_wool:white_carpet",
52 dye = "mcl_dye:red",
53 water_bucket = "mcl_buckets:bucket_water",
54 flower = "mcl_flowers:dandelion",
55 mushroom = "mcl_mushrooms:mushroom_brown",
56 wood_slab = "mcl_stairs:slab_wood",
57 wood_stairs = "mcl_stairs:stairs_wood",
58 coal = "mcl_core:coal_lump",
59 shulker_box = "mcl_chests:violet_shulker_box",
60 quartz_block = "mcl_nether:quartz_block",
61 banner = "mcl_banners:banner_item_white",
62 mesecon_conductor_craftable = "mesecons:wire_00000000_off",
63 purpur_block = "mcl_end:purpur_block",
64 compass = mcl_compass.stereotype,
65 clock = mcl_clock.sterotype,
68 local group_names = {
69 shulker_box = "Any shulker box",
70 wool = "Any wool",
71 wood = "Any wood planks",
72 tree = "Any wood",
73 sand = "Any sand",
74 sandstone = "Any sandstone (yellow)",
75 redsandstone = "Any red sandstone",
76 carpet = "Any carpet",
77 dye = "Any dye",
78 water_bucket = "Any water bucket",
79 flower = "Any flower",
80 mushroom = "Any mushroom",
81 wood_slab = "Any wooden slab",
82 wood_stairs = "Any wooden stairs",
83 coal = "Any coal",
84 quartz_block = "Any kind of quartz block",
85 purpur_block = "Any kind of purpur block",
86 stonebrick = "Any stone bricks",
89 function craftguide:group_to_item(item)
90 if item:sub(1,6) == "group:" then
91 local itemsub = item:sub(7)
92 if group_stereotypes[itemsub] then
93 item = group_stereotypes[itemsub]
94 elseif reg_items["mcl_core:"..itemsub] then
95 item = item:gsub("group:", "mcl_core:")
96 else
97 for name, def in pairs(reg_items) do
98 if def.groups[item:match("[^,:]+$")] then
99 item = name
104 return item:sub(1,6) == "group:" and "" or item
107 local function extract_groups(str)
108 if str:sub(1,6) ~= "group:" then return end
109 return str:sub(7):split(",")
112 local function colorize(str)
113 -- If client <= 0.4.14, don't colorize for compatibility.
114 return mt.colorize and mt.colorize("#FFFF00", str) or str
117 local function get_fueltime(item)
118 return get_result({method="fuel", width=1, items={item}}).time
121 function craftguide:get_tooltip(item, recipe_type, cooktime, groups)
122 local raw = self:get_tooltip_raw(item, recipe_type, cooktime, groups)
123 if raw == "" then
124 return raw
125 else
126 local tooltip = "tooltip["..item..";"
127 tooltip = tooltip .. raw
128 tooltip = tooltip .. "]"
129 return tooltip
133 function craftguide:get_tooltip_raw(item, recipe_type, cooktime, groups)
134 local tooltip, item_desc = "", ""
135 local fueltime = get_fueltime(item)
136 local has_extras = groups or recipe_type == "cooking" or fueltime > 0
138 if reg_items[item] then
139 if not groups then
140 item_desc = reg_items[item].description
142 else
143 return tooltip.."Unknown Item ("..item..")]"
145 if groups then
146 local gcol = "#FFAAFF"
147 local groupstr
148 if #groups == 1 then
149 local g = group_names[groups[1]]
150 -- Treat the groups “compass” and “clock” as fake groups
151 -- and just print the normal group name without special formatting
152 if groups[1] == "compass" or groups[1] == "clock" then
153 gcol = ""
154 groupstr = reg_items[item].description
155 elseif group_names[groups[1]] then
156 -- Use the special group name string
157 groupstr = group_names[groups[1]]
158 else
159 --[[ Fallback: Generic group explanation: This always
160 works, but the internally used group name (which
161 looks ugly) is exposed to the user. ]]
162 groupstr = "Any item belonging to the " .. groups[1] .. " group"
164 else
165 groupstr = "Any item belonging to the following groups: "
166 for i=1, #groups do
167 groupstr = groupstr .. groups[i]..
168 (groups[i+1] and " and " or "")
171 tooltip = tooltip..core.colorize(gcol, groupstr)
173 tooltip = tooltip .. item_desc
174 if recipe_type == "cooking" then
175 tooltip = tooltip.."\nCooking time: "..
176 colorize(cooktime)
178 if fueltime > 0 and not groups then
179 tooltip = tooltip.."\nBurning time: "..
180 colorize(fueltime)
183 return tooltip
186 function craftguide:get_recipe(iY, xoffset, tooltip_raw, item, recipe_num, recipes)
187 local formspec, recipes_total = "", #recipes
188 if recipes_total > 1 then
189 formspec = formspec..
190 "button[0,"..(iY+3)..";2,1;alternate;Alternate]"..
191 "label[0,"..(iY+2)..".5;Recipe "..
192 recipe_num.." of "..recipes_total.."]"
194 local recipe_type = recipes[recipe_num].type
196 local items = recipes[recipe_num].items
197 local width = recipes[recipe_num].width
198 local output = recipes[recipe_num].output
199 local cooking_time = 10
200 local is_shapeless = false
201 if recipe_type == "normal" and width == 0 then
202 is_shapeless = true
203 if #items <= 4 then
204 width = 2
205 else
206 width = min(3, #items)
210 --[[ Recipe type symbols ]]
212 -- Cooking (furnace)
213 if recipe_type == "cooking" then
214 cooking_time = width
215 width = 1
216 formspec = formspec..
217 "image["..(xoffset-0.8)..","..(iY+1)..
218 ".5;0.5,0.5;default_furnace_front_active.png]"
219 -- Shapeless recipes (intertwined arrows)
220 elseif is_shapeless then
221 formspec = formspec..
222 "image["..(xoffset-0.8)..","..(iY+1)..
223 ".5;0.5,0.5;craftguide_shapeless.png]"
226 -- Recipe only available in v6 (“v6” icon)
227 -- TODO/FIXME: This only works for the unique red sand recipe.
228 -- Remove this when red sand becomes regularily available.
229 local v6_only_recipe = false
230 if output == "mcl_core:redsand 8" and
231 width == 3 and
232 items[1] == "mcl_core:sand" and
233 items[2] == "mcl_core:sand" and
234 items[3] == "mcl_core:sand" and
235 items[4] == "mcl_core:sand" and
236 items[5] == "mcl_nether:nether_wart_item" and
237 items[6] == "mcl_core:sand" and
238 items[7] == "mcl_core:sand" and
239 items[8] == "mcl_core:sand" and
240 items[9] == "mcl_core:sand" then
241 v6_only_recipe = true
244 if v6_only_recipe then
245 formspec = formspec..
246 "image["..(xoffset-0.8)..","..(iY+2.75)..
247 ".5;0.5,0.5;mcl_craftguide_v6.png]"
250 -- Render slots
252 local rows = ceil(maxn(items) / width)
253 local btn_size, craftgrid_limit = 1, 5
255 if recipe_type == "normal" and
256 width > craftgrid_limit or rows > craftgrid_limit then
257 formspec = formspec..
258 "label["..xoffset..","..(iY+2)..
259 ";Recipe is too big to\nbe displayed ("..
260 width.."x"..rows..")]"
261 else
262 for i, v in pairs(items) do
263 local X = (i-1) % width + xoffset - 4 + (3 - max(1, width))
264 local Y = ceil(i / width + iY+2 - min(2, rows))
266 if recipe_type == "normal" and
267 width > 3 or rows > 3 then
268 btn_size = width > 3 and 3 / width or 3 / rows
269 X = btn_size * (i % width) + xoffset - 4 + (3 - max(1, width))
271 Y = btn_size * floor((i-1) / width) + iY+3 -
272 min(2, rows)
275 local groups = extract_groups(v)
276 local label = ""
277 -- Add the “G” symbols for group item slots
278 if groups then
279 --[[ Exception: Groups “compass” and “clock” since the items in these groups should
280 be treated as a single item from the user perspective. ]]
281 if not (#groups == 1 and (groups[1] == "compass" or groups[1] == "clock")) then
282 label = "\nG" or ""
285 local item_r = self:group_to_item(v)
286 local tltip = self:get_tooltip(
287 item_r, recipe_type, cooking_time, groups)
289 formspec = formspec..
290 "item_image_button["..X..","..Y..";"..
291 btn_size..","..btn_size..";"..item_r..
292 ";"..item_r..";"..label.."]"..tltip
295 local label = ""
296 if recipes[recipe_num]._is_toolrepair then
297 tooltip_raw = tooltip_raw .. "\n" .. core.colorize("#00FF00", string.format("Repaired by %.0f%%", (mcl_core.repair*100)))
298 label = "\nR"
300 return formspec..
301 "image["..(xoffset-1)..","..(iY+2)..
302 ".12;0.9,0.7;craftguide_arrow.png]"..
303 "item_image_button["..(xoffset)..","..(iY+2)..";1,1;"..
304 output..";"..item.."_out"..";"..label.."]".."tooltip["..item.."_out"..";"..minetest.formspec_escape(tooltip_raw).."]"
307 function craftguide:get_formspec(player_name, is_fuel)
308 local data = datas[player_name]
309 local iY = data.iX - 5
310 local ipp = data.iX * iY
312 if not data.items then
313 data.items = datas.init_items
315 data.pagemax = max(1, ceil(#data.items / ipp))
317 local formspec = "size["..data.iX..","..(iY+3)..".6;]"..
318 mcl_vars.gui_slots ..
319 mcl_vars.gui_bg ..
320 [=[background[1,1;1,1;craftguide_bg.png;true]
321 button[2.4,0.21;0.8,0.5;search;?]
322 button[3.05,0.21;0.8,0.5;clear;X]
323 tooltip[search;Search]
324 tooltip[clear;Reset]
325 tooltip[size_inc;Increase window size]
326 tooltip[size_dec;Decrease window size]
327 field_close_on_enter[filter;false]]=]..
328 "button["..(data.iX/2)..",-0.02;0.7,1;size_inc;+]"..
329 "button["..((data.iX/2) + 0.5)..
330 ",-0.02;0.7,1;size_dec;-]"..
331 "button["..(data.iX-3)..".4,0;0.8,0.95;prev;<]"..
332 "label["..(data.iX-2)..".1,0.18;"..
333 colorize(data.pagenum).." / "..data.pagemax.."]"..
334 "button["..(data.iX-1)..".2,0;0.8,0.95;next;>]"..
335 "field[0.3,0.32;2.5,1;filter;;"..
336 mt.formspec_escape(data.filter).."]"
338 local even_num = data.iX % 2 == 0
339 local xoffset = data.iX / 2 + (even_num and 0.5 or 0) + 2
341 if not next(data.items) then
342 local msg = ""
343 if data.filter == "" then
344 msg = "You don't know any crafting recipes yet.\nCollect some items and open the recipe book again."
345 else
346 msg = "No crafting recipes found.\nReset the search and try again."
348 formspec = formspec.."label[0,2;"..mt.formspec_escape(msg).."]"
351 local first_item = (data.pagenum - 1) * ipp
352 for i = first_item, first_item + ipp - 1 do
353 local name = data.items[i+1]
354 if not name then break end
355 local X = i % data.iX
356 local Y = (i % ipp - X) / data.iX + 1
358 formspec = formspec..
359 "item_image_button["..X..","..Y..";1,1;"..
360 name..";"..name.."_inv;]"
363 if data.item and reg_items[data.item] then
364 local tooltip_raw = self:get_tooltip_raw(data.item)
365 local tooltip = ""
366 if tooltip_raw ~= "" then
367 tooltip = "tooltip["..data.item..";"..minetest.formspec_escape(tooltip_raw).."]"
369 if not data.recipes_item or (is_fuel and not
370 get_recipe(data.item).items) then
371 formspec = formspec..
372 "image["..(xoffset-1)..","..(iY+2)..
373 ".12;0.9,0.7;craftguide_arrow.png]"..
374 "item_image_button["..(xoffset-2)..","..(iY+2)..
375 ";1,1;"..data.item..";"..data.item..";]"..
376 tooltip..
377 "image["..(xoffset)..","..
378 -- TODO: Remove fire icon, find better way to represent fuel
379 (iY+1.98)..";1,1;mcl_craftguide_fuel.png]"
380 else
381 formspec = formspec..self:get_recipe(
382 iY, xoffset, tooltip_raw, data.item,
383 data.recipe_num, data.recipes_item)
387 data.formspec = formspec
388 show_formspec(player_name, "craftguide", formspec)
391 local function player_has_item(T)
392 for i=1, #T do
393 if T[i] then return true end
397 local function group_to_items(group)
398 local items_with_group, counter = {}, 0
399 for name, def in pairs(reg_items) do
400 if def.groups[group:sub(7)] then
401 counter = counter + 1
402 items_with_group[counter] = name
405 return items_with_group
408 local function item_in_inv(inv, item)
409 return inv:contains_item("main", item)
412 -- Returns true if player knows the item. Used for progressive mode (EXPERIMENTAL).
413 local function knows_item(playername, item)
414 local has_item = doc.entry_exists("nodes", item) and doc.entry_revealed(playername, "nodes", item)
415 if not has_item then
416 has_item = doc.entry_exists("tools", item) and doc.entry_revealed(playername, "tools", item)
418 if not has_item then
419 has_item = doc.entry_exists("craftitems", item) and doc.entry_revealed(playername, "craftitems", item)
421 return has_item
424 function craftguide:recipe_in_inv(inv, item_name, recipes_f, playername)
425 local recipes = recipes_f or get_recipes(item_name) or {}
426 local show_item_recipes = {}
428 for i=1, #recipes do
429 show_item_recipes[i] = false
430 for _, item in pairs(recipes[i].items) do
431 local group_in_inv = false
432 if item:sub(1,6) == "group:" then
433 local groups = group_to_items(item)
434 for j=1, #groups do
435 if item_in_inv(inv, groups[j]) then
436 group_in_inv = true
440 if group_in_inv or item_in_inv(inv, item) or knows_item(playername, item) then
441 show_item_recipes[i] = true
445 for i=#show_item_recipes, 1, -1 do
446 if not show_item_recipes[i] then
447 remove(recipes, i)
451 return recipes, player_has_item(show_item_recipes)
454 function craftguide:get_init_items()
455 local items_list, counter = {}, 0
456 for name, def in pairs(reg_items) do
457 local is_fuel = get_fueltime(name) > 0
458 local is_tool = def.type == "tool"
459 if (not def.groups.not_in_craft_guide or def.groups.not_in_craft_guide == 0)
460 and (get_recipe(name).items or is_fuel or is_tool)
461 and def.description and def.description ~= "" then
462 counter = counter + 1
463 items_list[counter] = name
467 sort(items_list)
468 datas.init_items = items_list
471 function craftguide:get_filter_items(data, player)
472 local filter = data.filter
473 local items_list = progressive_mode and data.init_filter_items or
474 datas.init_items
475 local inv = player:get_inventory()
476 local filtered_list, counter = {}, 0
478 for i=1, #items_list do
479 local item = items_list[i]
480 local item_desc = reg_items[item].description:lower()
482 if filter ~= "" then
483 if item:find(filter, 1, true) or
484 item_desc:find(filter, 1, true) then
485 counter = counter + 1
486 filtered_list[counter] = item
488 elseif progressive_mode then
489 local _, has_item = self:recipe_in_inv(inv, item, nil, player:get_player_name())
490 if has_item then
491 counter = counter + 1
492 filtered_list[counter] = item
497 if progressive_mode and not data.items then
498 data.init_filter_items = filtered_list
500 data.items = filtered_list
503 mt.register_on_player_receive_fields(function(player, formname, fields)
504 if formname ~= "craftguide" then return end
505 local player_name = player:get_player_name()
506 local data = datas[player_name]
508 if fields.clear then
509 data.filter, data.item, data.pagenum, data.recipe_num =
510 "", nil, 1, 1
511 data.items = progressive_mode and data.init_filter_items or
512 datas.init_items
513 craftguide:get_formspec(player_name)
514 elseif fields.alternate then
515 local recipe = data.recipes_item[data.recipe_num + 1]
516 data.recipe_num = recipe and data.recipe_num + 1 or 1
517 craftguide:get_formspec(player_name)
518 elseif (fields.key_enter_field == "filter" or fields.search) and
519 fields.filter ~= "" then
520 data.filter = fields.filter:lower()
521 data.pagenum = 1
522 craftguide:get_filter_items(data, player)
523 craftguide:get_formspec(player_name)
524 elseif fields.prev or fields.next then
525 data.pagenum = data.pagenum - (fields.prev and 1 or -1)
526 if data.pagenum > data.pagemax then
527 data.pagenum = 1
528 elseif data.pagenum == 0 then
529 data.pagenum = data.pagemax
531 craftguide:get_formspec(player_name)
532 elseif (fields.size_inc and data.iX < 12) or
533 (fields.size_dec and data.iX > 8) then
534 data.pagenum = 1
535 data.iX = data.iX - (fields.size_dec and 1 or -1)
536 craftguide:get_formspec(player_name)
537 elseif (fields.quit) then
538 datas[player_name] = nil
539 else
540 for item in pairs(fields) do
541 if item:find(":") then
542 if item:sub(-4) == "_inv" or item:sub(-4) == "_out" then
543 item = item:sub(1,-5)
546 local is_fuel = get_fueltime(item) > 0
547 local recipes = get_recipes(item)
548 if not recipes and not is_fuel then return end
550 if item == data.item then
551 if data.recipes_item and #data.recipes_item >= 2 then
552 local recipe = data.recipes_item[data.recipe_num + 1]
553 data.recipe_num = recipe and data.recipe_num + 1 or 1
554 craftguide:get_formspec(player_name)
556 else
558 if progressive_mode then
559 local inv = player:get_inventory()
560 local _, has_item = craftguide:recipe_in_inv(inv, item, nil, player:get_player_name())
562 if not has_item then return end
563 recipes = craftguide:recipe_in_inv(inv, item, recipes, player_name)
566 data.item = item
567 data.recipe_num = 1
568 data.recipes_item = recipes
570 craftguide:get_formspec(player_name, is_fuel)
575 end)
577 function craftguide:on_use(user)
578 if not datas.init_items then
579 craftguide:get_init_items()
582 local player_name = user:get_player_name()
583 local data = datas[player_name]
585 if progressive_mode or not data then
586 datas[player_name] = {filter="", pagenum=1, iX=9}
587 if progressive_mode then
588 craftguide:get_filter_items(
589 datas[player_name], user)
591 craftguide:get_formspec(player_name)
592 else
593 show_formspec(player_name, "craftguide", data.formspec)
597 mcl_craftguide.show_craftguide = function(player)
598 craftguide:on_use(player)
601 mt.register_on_player_receive_fields(function(player, formname, fields)
602 if fields.__mcl_craftguide then
603 craftguide:on_use(player)
605 end)
607 mt.register_on_leaveplayer(function(player)
608 datas[player:get_player_name()] = nil
609 end)