init.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 groups = extract_groups(item)
  217. if groups then
  218. item = groups_to_item(groups)
  219. end
  220. item_button_fs(fs, base_x + x, base_y + y, item, item, groups)
  221. end
  222. if shapeless or recipe.method == "cooking" then
  223. table.insert(fs, ("image[3.2,0.5;0.5,0.5;craftguide_%s.png]")
  224. :format(shapeless and "shapeless" or "furnace"))
  225. local tooltip = shapeless and S("Shapeless") or
  226. S("Cooking time: @1", minetest.colorize("yellow", cooktime))
  227. table.insert(fs, "tooltip[3.2,0.5;0.5,0.5;"..esc(tooltip).."]")
  228. end
  229. table.insert(fs, "image[3,1;1,1;sfinv_crafting_arrow.png]")
  230. item_button_fs(fs, 4, 1, recipe.output, recipe.output:match("%S*"))
  231. end
  232. local function get_formspec(player)
  233. local name = player:get_player_name()
  234. local data = player_data[name]
  235. data.pagemax = math.max(1, math.ceil(#data.items / 32))
  236. local fs = {}
  237. table.insert(fs,
  238. "style_type[item_image_button;padding=2]"..
  239. "field[0.3,4.2;2.8,1.2;filter;;"..esc(data.filter).."]"..
  240. "label[5.8,4.15;"..minetest.colorize("yellow", data.pagenum).." / "..
  241. data.pagemax.."]"..
  242. "image_button[2.63,4.05;0.8,0.8;craftguide_search_icon.png;search;]"..
  243. "image_button[3.25,4.05;0.8,0.8;craftguide_clear_icon.png;clear;]"..
  244. "image_button[5,4.05;0.8,0.8;craftguide_prev_icon.png;prev;]"..
  245. "image_button[7.25,4.05;0.8,0.8;craftguide_next_icon.png;next;]"..
  246. "tooltip[search;"..esc(S("Search")).."]"..
  247. "tooltip[clear;"..esc(S("Reset")).."]"..
  248. "tooltip[prev;"..esc(S("Previous page")).."]"..
  249. "tooltip[next;"..esc(S("Next page")).."]"..
  250. "field_close_on_enter[filter;false]")
  251. if #data.items == 0 then
  252. table.insert(fs, "label[3,2;"..esc(S("No items to show.")).."]")
  253. else
  254. local first_item = (data.pagenum - 1) * 32
  255. for i = first_item, first_item + 31 do
  256. local item = data.items[i + 1]
  257. if not item then
  258. break
  259. end
  260. local x, y = coords(i % 32, 8)
  261. item_button_fs(fs, x, y, item, item)
  262. end
  263. end
  264. table.insert(fs, "container[0,5.6]")
  265. if data.recipes then
  266. recipe_fs(fs, data)
  267. elseif data.prev_item then
  268. table.insert(fs, ("label[2,1;%s]"):format(esc(data.show_usages
  269. and S("No usages.").."\n"..S("Click again to show recipes.")
  270. or S("No recipes.").."\n"..S("Click again to show usages."))))
  271. end
  272. table.insert(fs, "container_end[]")
  273. return table.concat(fs)
  274. end
  275. local function imatch(str, filter)
  276. return str:lower():find(filter, 1, true) ~= nil
  277. end
  278. local function execute_search(data)
  279. local filter = data.filter
  280. if filter == "" then
  281. data.items = init_items
  282. return
  283. end
  284. data.items = {}
  285. for _, item in ipairs(init_items) do
  286. local def = minetest.registered_items[item]
  287. local desc = def and minetest.get_translated_string(data.lang_code, def.description)
  288. if imatch(item, filter) or desc and imatch(desc, filter) then
  289. table.insert(data.items, item)
  290. end
  291. end
  292. end
  293. local function on_receive_fields(player, fields)
  294. local name = player:get_player_name()
  295. local data = player_data[name]
  296. if fields.clear then
  297. data.filter = ""
  298. data.pagenum = 1
  299. data.prev_item = nil
  300. data.recipes = nil
  301. data.items = init_items
  302. return true
  303. elseif fields.key_enter_field == "filter" or fields.search then
  304. local new = fields.filter:lower()
  305. if data.filter == new then
  306. return
  307. end
  308. data.filter = new
  309. data.pagenum = 1
  310. execute_search(data)
  311. return true
  312. elseif fields.prev or fields.next then
  313. if data.pagemax == 1 then
  314. return
  315. end
  316. data.pagenum = data.pagenum + (fields.next and 1 or -1)
  317. if data.pagenum > data.pagemax then
  318. data.pagenum = 1
  319. elseif data.pagenum == 0 then
  320. data.pagenum = data.pagemax
  321. end
  322. return true
  323. elseif fields.recipe_next or fields.recipe_prev then
  324. data.rnum = data.rnum + (fields.recipe_next and 1 or -1)
  325. if data.rnum > #data.recipes then
  326. data.rnum = 1
  327. elseif data.rnum == 0 then
  328. data.rnum = #data.recipes
  329. end
  330. return true
  331. else
  332. local item
  333. for field in pairs(fields) do
  334. if field:find(":") then
  335. item = field
  336. break
  337. end
  338. end
  339. if not item then
  340. return
  341. end
  342. if item == data.prev_item then
  343. data.show_usages = not data.show_usages
  344. else
  345. data.show_usages = nil
  346. end
  347. if data.show_usages then
  348. data.recipes = usages_cache[item]
  349. else
  350. data.recipes = recipes_cache[item]
  351. end
  352. data.prev_item = item
  353. data.rnum = 1
  354. return true
  355. end
  356. end
  357. minetest.register_on_joinplayer(function(player)
  358. local name = player:get_player_name()
  359. local info = minetest.get_player_information(name)
  360. player_data[name] = {
  361. filter = "",
  362. pagenum = 1,
  363. items = init_items,
  364. lang_code = info.lang_code
  365. }
  366. end)
  367. minetest.register_on_leaveplayer(function(player)
  368. local name = player:get_player_name()
  369. player_data[name] = nil
  370. end)
  371. sfinv.register_page("mtg_craftguide:craftguide", {
  372. title = esc(S("Recipes")),
  373. get = function(self, player, context)
  374. return sfinv.make_formspec(player, context, get_formspec(player))
  375. end,
  376. on_player_receive_fields = function(self, player, context, fields)
  377. if on_receive_fields(player, fields) then
  378. sfinv.set_player_inventory_formspec(player)
  379. end
  380. end
  381. })