init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. local S = minetest.get_translator("mtg_craftguide")
  2. local esc = minetest.formspec_escape
  3. local player_data = {}
  4. local init_items = {}
  5. local recipes_cache = {}
  6. local usages_cache = {}
  7. local group_stereotypes = {
  8. dye = "dye:white",
  9. wool = "wool:white",
  10. coal = "default:coal_lump",
  11. vessel = "vessels:glass_bottle",
  12. flower = "flowers:dandelion_yellow"
  13. }
  14. local group_names = {
  15. coal = S("Any coal"),
  16. sand = S("Any sand"),
  17. wool = S("Any wool"),
  18. stick = S("Any stick"),
  19. vessel = S("Any vessel"),
  20. wood = S("Any wood planks"),
  21. stone = S("Any kind of stone block"),
  22. ["color_red,flower"] = S("Any red flower"),
  23. ["color_blue,flower"] = S("Any blue flower"),
  24. ["color_black,flower"] = S("Any black flower"),
  25. ["color_green,flower"] = S("Any green flower"),
  26. ["color_white,flower"] = S("Any white flower"),
  27. ["color_orange,flower"] = S("Any orange flower"),
  28. ["color_violet,flower"] = S("Any violet flower"),
  29. ["color_yellow,flower"] = S("Any yellow flower"),
  30. ["color_red,dye"] = S("Any red dye"),
  31. ["color_blue,dye"] = S("Any blue dye"),
  32. ["color_cyan,dye"] = S("Any cyan dye"),
  33. ["color_grey,dye"] = S("Any grey dye"),
  34. ["color_pink,dye"] = S("Any pink dye"),
  35. ["color_black,dye"] = S("Any black dye"),
  36. ["color_brown,dye"] = S("Any brown dye"),
  37. ["color_green,dye"] = S("Any green dye"),
  38. ["color_white,dye"] = S("Any white dye"),
  39. ["color_orange,dye"] = S("Any orange dye"),
  40. ["color_violet,dye"] = S("Any violet dye"),
  41. ["color_yellow,dye"] = S("Any yellow dye"),
  42. ["color_magenta,dye"] = S("Any magenta dye"),
  43. ["color_dark_grey,dye"] = S("Any dark grey dye"),
  44. ["color_dark_green,dye"] = S("Any dark green dye")
  45. }
  46. local function table_replace(t, val, new)
  47. for k, v in pairs(t) do
  48. if v == val then
  49. t[k] = new
  50. end
  51. end
  52. end
  53. local function extract_groups(str)
  54. if str:sub(1, 6) == "group:" then
  55. return str:sub(7):split()
  56. end
  57. return nil
  58. end
  59. local function item_has_groups(item_groups, groups)
  60. for _, group in ipairs(groups) do
  61. if not item_groups[group] then
  62. return false
  63. end
  64. end
  65. return true
  66. end
  67. local function groups_to_item(groups)
  68. if #groups == 1 then
  69. local group = groups[1]
  70. if group_stereotypes[group] then
  71. return group_stereotypes[group]
  72. elseif minetest.registered_items["default:"..group] then
  73. return "default:"..group
  74. end
  75. end
  76. for name, def in pairs(minetest.registered_items) do
  77. if item_has_groups(def.groups, groups) then
  78. return name
  79. end
  80. end
  81. return ":unknown"
  82. end
  83. local function get_craftable_recipes(output)
  84. local recipes = minetest.get_all_craft_recipes(output)
  85. if not recipes then
  86. return nil
  87. end
  88. for i = #recipes, 1, -1 do
  89. for _, item in pairs(recipes[i].items) do
  90. local groups = extract_groups(item)
  91. if groups then
  92. item = groups_to_item(groups)
  93. end
  94. if not minetest.registered_items[item] then
  95. table.remove(recipes, i)
  96. break
  97. end
  98. end
  99. end
  100. if #recipes > 0 then
  101. return recipes
  102. end
  103. end
  104. local function show_item(def)
  105. return def.groups.not_in_craft_guide ~= 1 and def.description ~= ""
  106. end
  107. local function cache_usages(recipe)
  108. local added = {}
  109. for _, item in pairs(recipe.items) do
  110. if not added[item] then
  111. local groups = extract_groups(item)
  112. if groups then
  113. for name, def in pairs(minetest.registered_items) do
  114. if not added[name] and show_item(def)
  115. and item_has_groups(def.groups, groups) then
  116. local usage = table.copy(recipe)
  117. table_replace(usage.items, item, name)
  118. usages_cache[name] = usages_cache[name] or {}
  119. table.insert(usages_cache[name], usage)
  120. added[name] = true
  121. end
  122. end
  123. elseif show_item(minetest.registered_items[item]) then
  124. usages_cache[item] = usages_cache[item] or {}
  125. table.insert(usages_cache[item], recipe)
  126. end
  127. added[item] = true
  128. end
  129. end
  130. end
  131. minetest.register_on_mods_loaded(function()
  132. for name, def in pairs(minetest.registered_items) do
  133. if show_item(def) then
  134. local recipes = get_craftable_recipes(name)
  135. if recipes then
  136. recipes_cache[name] = recipes
  137. for _, recipe in ipairs(recipes) do
  138. cache_usages(recipe)
  139. end
  140. end
  141. end
  142. end
  143. for name, def in pairs(minetest.registered_items) do
  144. if recipes_cache[name] or usages_cache[name] then
  145. table.insert(init_items, name)
  146. end
  147. end
  148. table.sort(init_items)
  149. end)
  150. local function coords(i, cols)
  151. return i % cols, math.floor(i / cols)
  152. end
  153. local function is_fuel(item)
  154. return minetest.get_craft_result({method="fuel", items={item}}).time > 0
  155. end
  156. local function item_button_fs(fs, x, y, item, element_name, groups)
  157. table.insert(fs, ("item_image_button[%s,%s;1.05,1.05;%s;%s;%s]")
  158. :format(x, y, item, element_name, groups and "\n"..esc(S("G")) or ""))
  159. local tooltip
  160. if groups then
  161. table.sort(groups)
  162. tooltip = group_names[table.concat(groups, ",")]
  163. if not tooltip then
  164. local groupstr = {}
  165. for _, group in ipairs(groups) do
  166. table.insert(groupstr, minetest.colorize("yellow", group))
  167. end
  168. groupstr = table.concat(groupstr, ", ")
  169. tooltip = S("Any item belonging to the group(s): @1", groupstr)
  170. end
  171. elseif is_fuel(item) then
  172. local itemdef = minetest.registered_items[item:match("%S*")]
  173. local desc = itemdef and itemdef.description or S("Unknown Item")
  174. tooltip = desc.."\n"..minetest.colorize("orange", S("Fuel"))
  175. end
  176. if tooltip then
  177. table.insert(fs, ("tooltip[%s;%s]"):format(element_name, esc(tooltip)))
  178. end
  179. end
  180. local function recipe_fs(fs, data)
  181. local recipe = data.recipes[data.rnum]
  182. local width = recipe.width
  183. local cooktime, shapeless
  184. if recipe.method == "cooking" then
  185. cooktime, width = width, 1
  186. elseif width == 0 then
  187. shapeless = true
  188. if #recipe.items == 1 then
  189. width = 1
  190. elseif #recipe.items <= 4 then
  191. width = 2
  192. else
  193. width = 3
  194. end
  195. end
  196. table.insert(fs, ("label[5.5,1;%s]"):format(esc(data.show_usages
  197. and S("Usage @1 of @2", data.rnum, #data.recipes)
  198. or S("Recipe @1 of @2", data.rnum, #data.recipes))))
  199. if #data.recipes > 1 then
  200. table.insert(fs,
  201. "image_button[5.5,1.6;0.8,0.8;craftguide_prev_icon.png;recipe_prev;]"..
  202. "image_button[6.2,1.6;0.8,0.8;craftguide_next_icon.png;recipe_next;]"..
  203. "tooltip[recipe_prev;"..esc(S("Previous recipe")).."]"..
  204. "tooltip[recipe_next;"..esc(S("Next recipe")).."]")
  205. end
  206. local rows = math.ceil(table.maxn(recipe.items) / width)
  207. if width > 3 or rows > 3 then
  208. table.insert(fs, ("label[0,1;%s]")
  209. :format(esc(S("Recipe is too big to be displayed."))))
  210. return
  211. end
  212. local base_x = 3 - width
  213. local base_y = rows == 1 and 1 or 0
  214. for i, item in pairs(recipe.items) do
  215. local x, y = coords(i - 1, width)
  216. local elem_name = item
  217. local groups = extract_groups(item)
  218. if groups then
  219. item = groups_to_item(groups)
  220. elem_name = esc(item.."."..table.concat(groups, "+"))
  221. end
  222. item_button_fs(fs, base_x + x, base_y + y, item, elem_name, groups)
  223. end
  224. if shapeless or recipe.method == "cooking" then
  225. table.insert(fs, ("image[3.2,0.5;0.5,0.5;craftguide_%s.png]")
  226. :format(shapeless and "shapeless" or "furnace"))
  227. local tooltip = shapeless and S("Shapeless") or
  228. S("Cooking time: @1", minetest.colorize("yellow", cooktime))
  229. table.insert(fs, "tooltip[3.2,0.5;0.5,0.5;"..esc(tooltip).."]")
  230. end
  231. table.insert(fs, "image[3,1;1,1;sfinv_crafting_arrow.png]")
  232. item_button_fs(fs, 4, 1, recipe.output, recipe.output:match("%S*"))
  233. end
  234. local function get_formspec(player)
  235. local name = player:get_player_name()
  236. local data = player_data[name]
  237. data.pagemax = math.max(1, math.ceil(#data.items / 32))
  238. local fs = {}
  239. table.insert(fs,
  240. "style_type[item_image_button;padding=2]"..
  241. "field[0.3,4.2;2.8,1.2;filter;;"..esc(data.filter).."]"..
  242. "label[5.8,4.15;"..minetest.colorize("yellow", data.pagenum).." / "..
  243. data.pagemax.."]"..
  244. "image_button[2.63,4.05;0.8,0.8;craftguide_search_icon.png;search;]"..
  245. "image_button[3.25,4.05;0.8,0.8;craftguide_clear_icon.png;clear;]"..
  246. "image_button[5,4.05;0.8,0.8;craftguide_prev_icon.png;prev;]"..
  247. "image_button[7.25,4.05;0.8,0.8;craftguide_next_icon.png;next;]"..
  248. "tooltip[search;"..esc(S("Search")).."]"..
  249. "tooltip[clear;"..esc(S("Reset")).."]"..
  250. "tooltip[prev;"..esc(S("Previous page")).."]"..
  251. "tooltip[next;"..esc(S("Next page")).."]"..
  252. "field_close_on_enter[filter;false]")
  253. if #data.items == 0 then
  254. table.insert(fs, "label[3,2;"..esc(S("No items to show.")).."]")
  255. else
  256. local first_item = (data.pagenum - 1) * 32
  257. for i = first_item, first_item + 31 do
  258. local item = data.items[i + 1]
  259. if not item then
  260. break
  261. end
  262. local x, y = coords(i % 32, 8)
  263. item_button_fs(fs, x, y, item, item)
  264. end
  265. end
  266. table.insert(fs, "container[0,5.6]")
  267. if data.recipes then
  268. recipe_fs(fs, data)
  269. elseif data.prev_item then
  270. table.insert(fs, ("label[2,1;%s]"):format(esc(data.show_usages
  271. and S("No usages.").."\n"..S("Click again to show recipes.")
  272. or S("No recipes.").."\n"..S("Click again to show usages."))))
  273. end
  274. table.insert(fs, "container_end[]")
  275. return table.concat(fs)
  276. end
  277. local function imatch(str, filter)
  278. return str:lower():find(filter, 1, true) ~= nil
  279. end
  280. local function execute_search(data)
  281. local filter = data.filter
  282. if filter == "" then
  283. data.items = init_items
  284. return
  285. end
  286. data.items = {}
  287. for _, item in ipairs(init_items) do
  288. local def = minetest.registered_items[item]
  289. local desc = def and minetest.get_translated_string(data.lang_code, def.description)
  290. if imatch(item, filter) or desc and imatch(desc, filter) then
  291. table.insert(data.items, item)
  292. end
  293. end
  294. end
  295. local function on_receive_fields(player, fields)
  296. local name = player:get_player_name()
  297. local data = player_data[name]
  298. if fields.clear then
  299. data.filter = ""
  300. data.pagenum = 1
  301. data.prev_item = nil
  302. data.recipes = nil
  303. data.items = init_items
  304. return true
  305. elseif (fields.key_enter_field == "filter" or fields.search)
  306. and fields.filter then
  307. local new = fields.filter:sub(1, 128) -- truncate to a sane length
  308. :gsub("[%z\1-\8\11-\31\127]", "") -- strip naughty control characters (keeps \t and \n)
  309. :lower() -- search is case insensitive
  310. if data.filter == new then
  311. return
  312. end
  313. data.filter = new
  314. data.pagenum = 1
  315. execute_search(data)
  316. return true
  317. elseif fields.prev or fields.next then
  318. if data.pagemax == 1 then
  319. return
  320. end
  321. data.pagenum = data.pagenum + (fields.next and 1 or -1)
  322. if data.pagenum > data.pagemax then
  323. data.pagenum = 1
  324. elseif data.pagenum == 0 then
  325. data.pagenum = data.pagemax
  326. end
  327. return true
  328. elseif fields.recipe_next or fields.recipe_prev then
  329. data.rnum = data.rnum + (fields.recipe_next and 1 or -1)
  330. if data.rnum > #data.recipes then
  331. data.rnum = 1
  332. elseif data.rnum == 0 then
  333. data.rnum = #data.recipes
  334. end
  335. return true
  336. else
  337. local item
  338. for field in pairs(fields) do
  339. if field:find(":") then
  340. item = field:match("[%w_:]+")
  341. break
  342. end
  343. end
  344. if not item then
  345. return
  346. end
  347. if item == data.prev_item then
  348. data.show_usages = not data.show_usages
  349. else
  350. data.show_usages = nil
  351. end
  352. if data.show_usages then
  353. data.recipes = usages_cache[item]
  354. else
  355. data.recipes = recipes_cache[item]
  356. end
  357. data.prev_item = item
  358. data.rnum = 1
  359. return true
  360. end
  361. end
  362. minetest.register_on_joinplayer(function(player)
  363. local name = player:get_player_name()
  364. local info = minetest.get_player_information(name)
  365. player_data[name] = {
  366. filter = "",
  367. pagenum = 1,
  368. items = init_items,
  369. lang_code = info.lang_code
  370. }
  371. end)
  372. minetest.register_on_leaveplayer(function(player)
  373. local name = player:get_player_name()
  374. player_data[name] = nil
  375. end)
  376. sfinv.register_page("mtg_craftguide:craftguide", {
  377. title = esc(S("Recipes")),
  378. get = function(self, player, context)
  379. return sfinv.make_formspec(player, context, get_formspec(player))
  380. end,
  381. on_player_receive_fields = function(self, player, context, fields)
  382. if on_receive_fields(player, fields) then
  383. sfinv.set_player_inventory_formspec(player)
  384. end
  385. end
  386. })