init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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_enter_after_edit[filter;true]"..
  253. "field_close_on_enter[filter;false]")
  254. if #data.items == 0 then
  255. table.insert(fs, "label[3,2;"..esc(S("No items to show.")).."]")
  256. else
  257. local first_item = (data.pagenum - 1) * 32
  258. for i = first_item, first_item + 31 do
  259. local item = data.items[i + 1]
  260. if not item then
  261. break
  262. end
  263. local x, y = coords(i % 32, 8)
  264. item_button_fs(fs, x, y, item, item)
  265. end
  266. end
  267. table.insert(fs, "container[0,5.6]")
  268. if data.recipes then
  269. recipe_fs(fs, data)
  270. elseif data.prev_item then
  271. table.insert(fs, ("label[2,1;%s]"):format(esc(data.show_usages
  272. and S("No usages.").."\n"..S("Click again to show recipes.")
  273. or S("No recipes.").."\n"..S("Click again to show usages."))))
  274. end
  275. table.insert(fs, "container_end[]")
  276. return table.concat(fs)
  277. end
  278. local function imatch(str, filter)
  279. return str:lower():find(filter, 1, true) ~= nil
  280. end
  281. local function execute_search(data)
  282. local filter = data.filter
  283. if filter == "" then
  284. data.items = init_items
  285. return
  286. end
  287. data.items = {}
  288. for _, item in ipairs(init_items) do
  289. local def = minetest.registered_items[item]
  290. local desc = def and minetest.get_translated_string(data.lang_code, def.description)
  291. if imatch(item, filter) or desc and imatch(desc, filter) then
  292. table.insert(data.items, item)
  293. end
  294. end
  295. end
  296. local function on_receive_fields(player, fields)
  297. local name = player:get_player_name()
  298. local data = player_data[name]
  299. if fields.clear then
  300. data.filter = ""
  301. data.pagenum = 1
  302. data.prev_item = nil
  303. data.recipes = nil
  304. data.items = init_items
  305. return true
  306. elseif (fields.key_enter_field == "filter" or fields.search)
  307. and fields.filter then
  308. local new = fields.filter:sub(1, 128) -- truncate to a sane length
  309. :gsub("[%z\1-\8\11-\31\127]", "") -- strip naughty control characters (keeps \t and \n)
  310. :lower() -- search is case insensitive
  311. if data.filter == new then
  312. return
  313. end
  314. data.filter = new
  315. data.pagenum = 1
  316. execute_search(data)
  317. return true
  318. elseif fields.prev or fields.next then
  319. if data.pagemax == 1 then
  320. return
  321. end
  322. data.pagenum = data.pagenum + (fields.next and 1 or -1)
  323. if data.pagenum > data.pagemax then
  324. data.pagenum = 1
  325. elseif data.pagenum == 0 then
  326. data.pagenum = data.pagemax
  327. end
  328. return true
  329. elseif fields.recipe_next or fields.recipe_prev then
  330. data.rnum = data.rnum + (fields.recipe_next and 1 or -1)
  331. if data.rnum > #data.recipes then
  332. data.rnum = 1
  333. elseif data.rnum == 0 then
  334. data.rnum = #data.recipes
  335. end
  336. return true
  337. else
  338. local item
  339. for field in pairs(fields) do
  340. if field:find(":") then
  341. item = field:match("[%w_:]+")
  342. break
  343. end
  344. end
  345. if not item then
  346. return
  347. end
  348. if item == data.prev_item then
  349. data.show_usages = not data.show_usages
  350. else
  351. data.show_usages = nil
  352. end
  353. if data.show_usages then
  354. data.recipes = usages_cache[item]
  355. else
  356. data.recipes = recipes_cache[item]
  357. end
  358. data.prev_item = item
  359. data.rnum = 1
  360. return true
  361. end
  362. end
  363. minetest.register_on_joinplayer(function(player)
  364. local name = player:get_player_name()
  365. local info = minetest.get_player_information(name)
  366. player_data[name] = {
  367. filter = "",
  368. pagenum = 1,
  369. items = init_items,
  370. lang_code = info.lang_code
  371. }
  372. end)
  373. minetest.register_on_leaveplayer(function(player)
  374. local name = player:get_player_name()
  375. player_data[name] = nil
  376. end)
  377. sfinv.register_page("mtg_craftguide:craftguide", {
  378. title = esc(S("Recipes")),
  379. get = function(self, player, context)
  380. return sfinv.make_formspec(player, context, get_formspec(player))
  381. end,
  382. on_player_receive_fields = function(self, player, context, fields)
  383. if on_receive_fields(player, fields) then
  384. sfinv.set_player_inventory_formspec(player)
  385. end
  386. end
  387. })