init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. -- ZCG mod for minetest
  2. -- See README for more information
  3. -- Released by Zeg9 under WTFPL
  4. zcg = zcg or {}
  5. zcg.modpath = minetest.get_modpath("zcg")
  6. zcg.users = zcg.users or {}
  7. zcg.crafts = zcg.crafts or {}
  8. zcg.itemlist = zcg.itemlist or {}
  9. -- Localize for performance.
  10. local math_floor = math.floor
  11. zcg.items_in_group = function(group)
  12. local items = {}
  13. local ok = true
  14. for name, item in pairs(minetest.registered_items) do
  15. -- the node should be in all groups
  16. ok = true
  17. for _, g in ipairs(group:split(',')) do
  18. if not item.groups[g] then
  19. ok = false
  20. end
  21. end
  22. if ok then table.insert(items,name) end
  23. end
  24. return items
  25. end
  26. local table_copy = function(table)
  27. local out = {}
  28. for k,v in pairs(table) do
  29. out[k] = v
  30. end
  31. return out
  32. end
  33. zcg.add_craft = function(input, realout, output, groups)
  34. if minetest.get_item_group(output, "not_in_craft_guide") > 0 then
  35. return
  36. end
  37. if not groups then groups = {} end
  38. local c = {}
  39. c.width = input.width
  40. c.type = input.type
  41. c.items = input.items
  42. if type(realout) == "string" then
  43. c.result = realout
  44. elseif type(realout) == "table" then
  45. --minetest.log(dump(realout))
  46. if type(realout.output) == "string" then
  47. c.result = realout.output
  48. elseif type(realout.output) == "table" then
  49. -- Recipe output should be two items (separating recipe).
  50. assert(type(realout.output[1]) == "string")
  51. assert(type(realout.output[2]) == "string")
  52. c.result = table.copy(realout.output)
  53. end
  54. end
  55. assert(type(c.result) == "string" or type(c.result) == "table")
  56. if c.items == nil then return end
  57. for i, item in pairs(c.items) do
  58. if item:sub(0,6) == "group:" then
  59. -- The recipe item name may contain a count value.
  60. -- We must extract just the name after the "group:".
  61. local strpart = item:sub(7)
  62. local parts = string.split(strpart, " ")
  63. assert(type(parts[1]) == "string")
  64. local groupname = parts[1]
  65. local groupcount = parts[2] or 1
  66. if groups[groupname] ~= nil then
  67. c.items[i] = groups[groupname] .. " " .. groupcount
  68. else
  69. for _, gi in ipairs(zcg.items_in_group(groupname)) do
  70. local g2 = groups
  71. g2[groupname] = gi
  72. zcg.add_craft({
  73. width = c.width,
  74. type = c.type,
  75. items = table_copy(c.items)
  76. }, realout, output, g2) -- it is needed to copy the table, else groups won't work right
  77. end
  78. return
  79. end
  80. end
  81. end
  82. if c.width == 0 then c.width = 3 end
  83. table.insert(zcg.crafts[output],c)
  84. end
  85. zcg.load_crafts = function(name)
  86. zcg.crafts[name] = {}
  87. local _recipes = minetest.get_all_craft_recipes(name)
  88. if _recipes then
  89. for i, recipe in ipairs(_recipes) do
  90. if (recipe and recipe.items and recipe.type) then
  91. assert(type(recipe.output) ~= "nil")
  92. zcg.add_craft(recipe, recipe.output, name)
  93. end
  94. end
  95. end
  96. if zcg.crafts[name] == nil or #zcg.crafts[name] == 0 then
  97. zcg.crafts[name] = nil
  98. else
  99. table.insert(zcg.itemlist,name)
  100. end
  101. end
  102. zcg.formspec = function(pn)
  103. local page = zcg.users[pn].page
  104. local alt = zcg.users[pn].alt
  105. local current_item = zcg.users[pn].current_item
  106. local formspec = "size[8,8.5]" ..
  107. default.gui_bg ..
  108. default.gui_bg_img ..
  109. default.gui_slots ..
  110. "button[0,0.5;2,.5;main;Back]"
  111. if zcg.users[pn].history.index > 1 then
  112. formspec = formspec .. "image_button[0,1.5;1,1;zcg_previous.png;zcg_previous;;false;false;zcg_previous_press.png]"
  113. else
  114. formspec = formspec .. "image[0,1.5;1,1;zcg_previous_inactive.png]"
  115. end
  116. if zcg.users[pn].history.index < #zcg.users[pn].history.list then
  117. formspec = formspec .. "image_button[1,1.5;1,1;zcg_next.png;zcg_next;;false;false;zcg_next_press.png]"
  118. else
  119. formspec = formspec .. "image[1,1.5;1,1;zcg_next_inactive.png]"
  120. end
  121. -- Show craft recipe
  122. if current_item ~= "" then
  123. if zcg.crafts[current_item] then
  124. if alt > #zcg.crafts[current_item] then
  125. alt = #zcg.crafts[current_item]
  126. end
  127. if alt > 1 then
  128. formspec = formspec .. "button[7,0.5;1,1;zcg_alt:"..(alt-1)..";^]"
  129. end
  130. if alt < #zcg.crafts[current_item] then
  131. formspec = formspec .. "button[7,2.5;1,1;zcg_alt:"..(alt+1)..";v]"
  132. end
  133. local c = zcg.crafts[current_item][alt]
  134. if c then
  135. local x = 3
  136. local y = 0
  137. -- Crafting recipe generated here.
  138. for i, item in pairs(c.items) do
  139. local stack = ItemStack(item)
  140. local itemname = stack:get_name()
  141. formspec = formspec .. "item_image_button["..((i-1)%c.width+x)..","..(math_floor((i-1)/c.width+y)+0.5)..";1,1;"..item..";zcg:"..itemname..";]"
  142. end
  143. if c.type == "normal" or
  144. c.type == "cooking" or
  145. c.type == "grinding" or
  146. c.type == "cutting" or
  147. c.type == "extracting" or
  148. c.type == "alloying" or
  149. c.type == "separating" or
  150. c.type == "compressing" or
  151. c.type == "crushing" then
  152. formspec = formspec .. "image[6,2.5;1,1;zcg_method_"..c.type..".png]"
  153. else -- we don't have an image for other types of crafting
  154. formspec = formspec .. "label[0,2.5;Method: "..c.type.."]"
  155. end
  156. if c.type == "normal" then
  157. formspec = formspec .. "label[0,2.5;Method: Crafting]"
  158. elseif c.type == "cooking" then
  159. formspec = formspec .. "label[0,2.5;Method: Cooking/Smelting]"
  160. elseif c.type == "grinding" then
  161. formspec = formspec .. "label[0,2.5;Method: Grinding]"
  162. elseif c.type == "crushing" then
  163. formspec = formspec .. "label[0,2.5;Method: Crushing]"
  164. elseif c.type == "cutting" then
  165. formspec = formspec .. "label[0,2.5;Method: Cutting]"
  166. elseif c.type == "extracting" then
  167. formspec = formspec .. "label[0,2.5;Method: Extracting]"
  168. elseif c.type == "compressing" then
  169. formspec = formspec .. "label[0,2.5;Method: Compressing]"
  170. elseif c.type == "alloying" then
  171. formspec = formspec .. "label[0,2.5;Method: Alloying]"
  172. elseif c.type == "separating" then
  173. formspec = formspec .. "label[0,2.5;Method: Separating]"
  174. end
  175. if type(c.result) == "string" then
  176. formspec = formspec .. "image[6,1.5;1,1;zcg_craft_arrow.png]"
  177. formspec = formspec .. "item_image_button[7,1.5;1,1;".. c.result ..";;]"
  178. elseif type(c.result) == "table" then
  179. -- Separating recipes have two outputs.
  180. formspec = formspec .. "item_image_button[6,1.5;1,1;".. c.result[1] ..";;]"
  181. formspec = formspec .. "item_image_button[7,1.5;1,1;".. c.result[2] ..";;]"
  182. end
  183. --minetest.chat_send_all(dump(c))
  184. end
  185. end
  186. end
  187. -- Node list
  188. local npp = 8*3 -- nodes per page
  189. local i = 0 -- for positionning buttons
  190. local s = 0 -- for skipping pages
  191. local whichlist = zcg.itemlist
  192. local listname = " total items."
  193. if zcg.users[pn].searchtext ~= "" then
  194. whichlist = zcg.users[pn].searchlist
  195. page = zcg.users[pn].spage
  196. listname = " result(s)."
  197. end
  198. if #whichlist > 0 then
  199. formspec = formspec ..
  200. "label[0,4.0;" .. #whichlist .. " " .. listname .. "]"
  201. for _, name in ipairs(whichlist) do
  202. if s < page*npp then s = s+1 else
  203. if i >= npp then break end
  204. formspec = formspec .. "item_image_button["..(i%8)..","..(math_floor(i/8)+4.5)..";1,1;"..name..";zcg:"..name..";]"
  205. i = i+1
  206. end
  207. end
  208. else
  209. formspec = formspec ..
  210. "label[0,4.0;No results.]"
  211. end
  212. -- Page buttons.
  213. local maxpage = (math.ceil(#whichlist/npp))
  214. if maxpage < 1 then maxpage = 1 end -- In case no results, must have 1 page.
  215. local curpage = page+1
  216. if page > 0 then
  217. formspec = formspec .. "button[0,8;1,.5;zcg_page:"..(page-1)..";<<]"
  218. else
  219. formspec = formspec .. "button[0,8;1,.5;zcg_page:"..(maxpage-1)..";<<]"
  220. end
  221. if curpage < maxpage then
  222. formspec = formspec .. "button[1,8;1,.5;zcg_page:"..(page+1)..";>>]"
  223. elseif curpage >= maxpage then
  224. formspec = formspec .. "button[1,8;1,.5;zcg_page:".. 0 ..";>>]"
  225. end
  226. -- The Y is approximatively the good one to have it centered vertically...
  227. formspec = formspec .. "label[2,7.85;Page " .. curpage .."/".. maxpage .."]"
  228. -- Search field.
  229. formspec = formspec ..
  230. "button[6,8;1,0.5;zcg_search;?]" ..
  231. "button[7,8;1,0.5;zcg_clear;X]"
  232. local text = zcg.users[pn].searchtext or ""
  233. formspec = formspec ..
  234. "field[4,8.1;2.3,1;zcg_sbox;;" .. minetest.formspec_escape(text) .. "]" ..
  235. "field_close_on_enter[zcg_sbox;false]"
  236. return formspec
  237. end
  238. function zcg.update_search(pn, tsearch)
  239. minetest.log("action", "<" .. rename.gpn(pn) .. "> executes craftguide search for \"" .. tsearch .. "\".")
  240. zcg.users[pn].searchlist = {}
  241. if tsearch == "" or tsearch == "<INVAL>" then
  242. return
  243. end
  244. -- Let user search multiple tokens at once.
  245. local texts = string.split(tsearch)
  246. if not texts or #texts == 0 then
  247. return
  248. end
  249. local list = {}
  250. local find = string.find
  251. local ipairs = ipairs
  252. local pairs = pairs
  253. local type = type
  254. local items = minetest.registered_items
  255. -- Returns true only if all tokens in list are found in the search string.
  256. local function find_all(search, combined)
  257. local count = 0
  258. for i=1, #combined do
  259. if find(search, combined[i], 1, true) then
  260. count = count + 1
  261. else
  262. -- Early finish.
  263. return false
  264. end
  265. end
  266. return (count == #combined)
  267. end
  268. for i=1, #texts, 1 do
  269. local text = string.trim(texts[i]):lower()
  270. local combined = string.split(text, " ")
  271. for k=1, #combined do
  272. combined[k] = string.trim(combined[k]):lower()
  273. end
  274. for _, name in ipairs(zcg.itemlist) do
  275. if find_all(name:lower(), combined) then
  276. list[name] = true
  277. else
  278. local ndef = items[name]
  279. if ndef then
  280. -- Search description for a match.
  281. if ndef.description then
  282. if find_all(ndef.description:lower(), combined) then
  283. list[name] = true
  284. end
  285. end
  286. end -- if ndef.
  287. end
  288. end
  289. end -- for all texts.
  290. -- Duplicate results are removed.
  291. local flat = {}
  292. for k, v in pairs(list) do
  293. flat[#flat + 1] = k
  294. end
  295. zcg.users[pn].searchlist = flat
  296. end
  297. -- Sound volumes.
  298. local click_gain = 0.4
  299. local page_gain = 0.7
  300. local sound_range = 16
  301. -- It seems the player's main inventory formspec does not have a formname.
  302. local singleplayer = minetest.is_singleplayer()
  303. zcg.on_receive_fields = function(player, formname, fields)
  304. local played_sound = false
  305. local pn = player:get_player_name()
  306. afk_removal.reset_timeout(pn)
  307. if zcg.users[pn] == nil then
  308. zcg.users[pn] = {
  309. current_item = "",
  310. alt = 1,
  311. page = 0,
  312. history = {index=0, list={}},
  313. searchlist = {},
  314. searchtext = "",
  315. spage = 0, -- Keep search page # seperate from main page #.
  316. }
  317. end
  318. if fields.zcg then
  319. inventory_plus.set_inventory_formspec(player, zcg.formspec(pn))
  320. if not played_sound and not fields.quit then
  321. ambiance.sound_play("button_click", player:get_pos(), click_gain, sound_range)
  322. end
  323. return
  324. elseif fields.zcg_previous then
  325. if zcg.users[pn].history.index > 1 then
  326. zcg.users[pn].history.index = zcg.users[pn].history.index - 1
  327. zcg.users[pn].current_item = zcg.users[pn].history.list[zcg.users[pn].history.index]
  328. zcg.users[pn].searchtext = fields.zcg_sbox or "<INVAL>"
  329. inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
  330. end
  331. elseif fields.zcg_next then
  332. if zcg.users[pn].history.index < #zcg.users[pn].history.list then
  333. zcg.users[pn].history.index = zcg.users[pn].history.index + 1
  334. zcg.users[pn].current_item = zcg.users[pn].history.list[zcg.users[pn].history.index]
  335. zcg.users[pn].searchtext = fields.zcg_sbox or "<INVAL>"
  336. inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
  337. end
  338. end
  339. for k, v in pairs(fields) do
  340. if (k:sub(0,4)=="zcg:") then
  341. local ni = k:sub(5)
  342. zcg.users[pn].searchtext = fields.zcg_sbox or "<INVAL>"
  343. if zcg.crafts[ni] then
  344. local previtem = zcg.users[pn].current_item
  345. zcg.users[pn].current_item = ni
  346. table.insert(zcg.users[pn].history.list, ni)
  347. zcg.users[pn].history.index = #zcg.users[pn].history.list
  348. inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
  349. -- Add item to inventory if creative access is enabled.
  350. if gdac.player_is_admin(pn) or singleplayer then
  351. -- If player clicked twice.
  352. if previtem == ni then
  353. local inv = player:get_inventory()
  354. local stack = ItemStack(ni)
  355. stack:set_count(stack:get_stack_max())
  356. local leftover = inv:add_item("main", stack)
  357. -- Notify if a mapping kit was added.
  358. if map.is_mapping_kit(stack:get_name()) then
  359. map.update_inventory_info(pn)
  360. end
  361. if not leftover or leftover:get_count() == 0 then
  362. local desc = utility.get_short_desc(stack:get_definition().description or "Undescribed Item")
  363. minetest.chat_send_player(pn, "# Server: Added '" .. desc .. "' to inventory!")
  364. else
  365. minetest.chat_send_player(pn, "# Server: Not enough room in inventory!")
  366. end
  367. end
  368. end
  369. end
  370. elseif (k:sub(0,9)=="zcg_page:") then
  371. if zcg.users[pn].searchtext == "" then
  372. zcg.users[pn].page = tonumber(k:sub(10))
  373. else
  374. zcg.users[pn].spage = tonumber(k:sub(10))
  375. end
  376. ambiance.sound_play("pageflip", player:get_pos(), page_gain, sound_range)
  377. played_sound = true
  378. zcg.users[pn].searchtext = fields.zcg_sbox or "<INVAL>"
  379. inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
  380. elseif (k:sub(0,8)=="zcg_alt:") then
  381. zcg.users[pn].alt = tonumber(k:sub(9))
  382. zcg.users[pn].searchtext = fields.zcg_sbox or "<INVAL>"
  383. inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
  384. elseif (k == "zcg_clear") then
  385. zcg.users[pn].searchlist = {}
  386. zcg.users[pn].searchtext = ""
  387. inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
  388. elseif (k == "zcg_search") then
  389. local newtext = fields.zcg_sbox or "<INVAL>"
  390. if newtext ~= zcg.users[pn].searchtext then -- Don't update if same.
  391. zcg.users[pn].searchlist = {}
  392. zcg.users[pn].searchtext = newtext
  393. if string.len(zcg.users[pn].searchtext) > 128 then
  394. zcg.users[pn].searchtext = "<INVAL>"
  395. else
  396. local res, err = pcall(function() zcg.update_search(pn, zcg.users[pn].searchtext) end)
  397. if not res and type(err) == "string" then
  398. minetest.log("error", err)
  399. end
  400. zcg.users[pn].spage = 0 -- Reset user's search page.
  401. end
  402. inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
  403. ambiance.sound_play("pageflip", player:get_pos(), page_gain, sound_range)
  404. played_sound = true
  405. end
  406. end
  407. end
  408. if not played_sound and not fields.quit then
  409. ambiance.sound_play("button_click", player:get_pos(), click_gain, sound_range)
  410. end
  411. if not played_sound and (fields.close or fields.exit or fields.done) then
  412. ambiance.sound_play("button_click", player:get_pos(), click_gain, sound_range)
  413. end
  414. -- This works because the 'quit' field is sent whenever the player tabs or ESC's out of a formspec,
  415. -- but not when changing to show a different formspec.
  416. if fields.quit then
  417. if passport.open_keys[pn] then
  418. passport.open_keys[pn] = nil
  419. ambiance.sound_play("fancy_chime2", player:get_pos(), 1.0, 20, "", false)
  420. end
  421. end
  422. end
  423. if not zcg.registered then
  424. -- Load all crafts directly after server-init time.
  425. -- We can't do this at craft-register time because the logic needs access to
  426. -- the groups of the recipe output items, which may not be known by the engine
  427. -- until after recipes for the items are registered.
  428. minetest.register_on_mods_loaded(function()
  429. local t1 = os.clock()
  430. -- Must search through ALL registered items! Cannot use shortcut tables.
  431. for name, item in pairs(minetest.registered_items) do
  432. if name and name ~= "" then
  433. -- Ignore stairs nodes. They do have generic/standard recipes, but we
  434. -- wouldn't show them anway -- WAY too much CG spam.
  435. if not name:find("^%:?stairs:") then
  436. zcg.load_crafts(name)
  437. end
  438. end
  439. end
  440. table.sort(zcg.itemlist)
  441. local t2 = os.clock()
  442. minetest.log("action", "Loading craft recipes took " .. (t2 - t1) .. " seconds.")
  443. end)
  444. -- Register button once.
  445. inventory_plus.register_button("zcg", "Craft Journal")
  446. -- Per Lua docs, newest functions are called first.
  447. -- Therefore resister inside minetest.after() to ensure this function is
  448. -- registered AFTER all other mods have registered theirs.
  449. minetest.after(0, function()
  450. minetest.register_on_player_receive_fields(function(...)
  451. return zcg.on_receive_fields(...)
  452. end)
  453. end)
  454. local c = "zcg:core"
  455. local f = zcg.modpath .. "/init.lua"
  456. reload.register_file(c, f, false)
  457. zcg.registered = true
  458. end