init.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. local workbench = {}
  2. WB = {}
  3. screwdriver = screwdriver or {}
  4. local min, ceil = math.min, math.ceil
  5. -- Nodes allowed to be cut
  6. -- Only the regular, solid blocks without metas or explosivity can be cut
  7. local nodes = {}
  8. for node, def in pairs(minetest.registered_nodes) do
  9. if (def.drawtype == "normal" or def.drawtype:sub(1,5) == "glass") and
  10. (def.groups.cracky or def.groups.choppy) and
  11. not def.on_construct and
  12. not def.after_place_node and
  13. not def.on_rightclick and
  14. not def.on_blast and
  15. not def.allow_metadata_inventory_take and
  16. not (def.groups.not_in_creative_inventory == 1) and
  17. not (def.groups.not_cuttable == 1) and
  18. not def.groups.wool and
  19. (def.tiles and type(def.tiles[1]) == "string" and not
  20. def.tiles[1]:find("default_mineral")) and
  21. not def.mesecons and
  22. def.description and
  23. def.description ~= "" and
  24. def.light_source == 0
  25. then
  26. nodes[#nodes+1] = node
  27. end
  28. end
  29. -- Optionally, you can register custom cuttable nodes in the workbench
  30. WB.custom_nodes_register = {
  31. -- "default:leaves",
  32. }
  33. setmetatable(nodes, {
  34. __concat = function(t1, t2)
  35. for i=1, #t2 do
  36. t1[#t1+1] = t2[i]
  37. end
  38. return t1
  39. end
  40. })
  41. nodes = nodes..WB.custom_nodes_register
  42. -- Nodeboxes definitions
  43. workbench.defs = {
  44. -- Name Yield X Y Z W H L
  45. {"nanoslab", 16, { 0, 0, 0, 8, 1, 8 }},
  46. {"micropanel", 16, { 0, 0, 0, 16, 1, 8 }},
  47. {"microslab", 8, { 0, 0, 0, 16, 1, 16 }},
  48. {"thinstair", 8, { 0, 7, 0, 16, 1, 8 },
  49. { 0, 15, 8, 16, 1, 8 }},
  50. {"cube", 4, { 0, 0, 0, 8, 8, 8 }},
  51. {"panel", 4, { 0, 0, 0, 16, 8, 8 }},
  52. {"slab", 2, nil },
  53. {"doublepanel", 2, { 0, 0, 0, 16, 8, 8 },
  54. { 0, 8, 8, 16, 8, 8 }},
  55. {"halfstair", 2, { 0, 0, 0, 8, 8, 16 },
  56. { 0, 8, 8, 8, 8, 8 }},
  57. {"outerstair", 1, { 0, 0, 0, 16, 8, 16 },
  58. { 0, 8, 8, 8, 8, 8 }},
  59. {"stair", 1, nil },
  60. {"innerstair", 1, { 0, 0, 0, 16, 8, 16 },
  61. { 0, 8, 8, 16, 8, 8 },
  62. { 0, 8, 0, 8, 8, 8 }}
  63. }
  64. -- Tools allowed to be repaired
  65. function workbench:repairable(stack)
  66. local tools = {"pick", "axe", "shovel", "sword", "hoe", "armor", "shield"}
  67. for _, t in pairs(tools) do
  68. if stack:find(t) then return true end
  69. end
  70. return false
  71. end
  72. function workbench:get_output(inv, input, name)
  73. local output = {}
  74. for _, n in pairs(self.defs) do
  75. local count = min(n[2] * input:get_count(), input:get_stack_max())
  76. local item = name.."_"..n[1]
  77. if not n[3] then item = "stairs:"..n[1].."_"..name:match(":(.*)") end
  78. output[#output+1] = item.." "..count
  79. end
  80. inv:set_list("forms", output)
  81. end
  82. -- Thanks to kaeza for this function
  83. function workbench:pixelbox(size, boxes)
  84. local fixed = {}
  85. for _, box in pairs(boxes) do
  86. -- `unpack` has been changed to `table.unpack` in newest Lua versions
  87. local x, y, z, w, h, l = unpack(box)
  88. fixed[#fixed+1] = {
  89. (x / size) - 0.5,
  90. (y / size) - 0.5,
  91. (z / size) - 0.5,
  92. ((x + w) / size) - 0.5,
  93. ((y + h) / size) - 0.5,
  94. ((z + l) / size) - 0.5
  95. }
  96. end
  97. return {type="fixed", fixed=fixed}
  98. end
  99. local formspecs = {
  100. -- Main formspec
  101. [[ label[0.9,1.23;Cut]
  102. label[0.9,2.23;Repair]
  103. box[-0.05,1;2.05,0.9;#555555]
  104. box[-0.05,2;2.05,0.9;#555555]
  105. button[0,0;2,1;craft;Crafting]
  106. button[2,0;2,1;storage;Storage]
  107. image[3,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
  108. image[0,1;1,1;workbench_saw.png]
  109. image[0,2;1,1;workbench_anvil.png]
  110. image[3,2;1,1;hammer_layout.png]
  111. list[context;input;2,1;1,1;]
  112. list[context;tool;2,2;1,1;]
  113. list[context;hammer;3,2;1,1;]
  114. list[context;forms;4,0;4,3;] ]],
  115. -- Crafting formspec
  116. [[ image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
  117. button[0,0;1.5,1;back;< Back]
  118. list[current_player;craft;2,0;3,3;]
  119. list[current_player;craftpreview;6,1;1,1;]
  120. listring[current_player;main]
  121. listring[current_player;craft] ]],
  122. -- Storage formspec
  123. [[ list[context;storage;0,1;8,2;]
  124. button[0,0;1.5,1;back;< Back]
  125. listring[context;storage]
  126. listring[current_player;main] ]]
  127. }
  128. function workbench:set_formspec(meta, id)
  129. meta:set_string("formspec", "size[8,7;]list[current_player;main;0,3.25;8,4;]"..
  130. formspecs[id]..default.gui_bg..default.gui_bg_img..
  131. default.gui_slots..default.get_hotbar_bg(0,3.25))
  132. end
  133. function workbench.construct(pos)
  134. local meta = minetest.get_meta(pos)
  135. local inv = meta:get_inventory()
  136. inv:set_size("tool", 1)
  137. inv:set_size("input", 1)
  138. inv:set_size("hammer", 1)
  139. inv:set_size("forms", 4*3)
  140. inv:set_size("storage", 8*2)
  141. meta:set_string("infotext", "Work Bench")
  142. workbench:set_formspec(meta, 1)
  143. end
  144. function workbench.fields(pos, _, fields)
  145. local meta = minetest.get_meta(pos)
  146. if fields.back then workbench:set_formspec(meta, 1)
  147. elseif fields.craft then workbench:set_formspec(meta, 2)
  148. elseif fields.storage then workbench:set_formspec(meta, 3) end
  149. end
  150. function workbench.dig(pos)
  151. local inv = minetest.get_meta(pos):get_inventory()
  152. return inv:is_empty("input") and inv:is_empty("hammer") and
  153. inv:is_empty("tool") and inv:is_empty("storage")
  154. end
  155. function workbench.timer(pos)
  156. local timer = minetest.get_node_timer(pos)
  157. local inv = minetest.get_meta(pos):get_inventory()
  158. local tool = inv:get_stack("tool", 1)
  159. local hammer = inv:get_stack("hammer", 1)
  160. if tool:is_empty() or hammer:is_empty() or tool:get_wear() == 0 then
  161. timer:stop()
  162. return
  163. end
  164. -- Tool's wearing range: 0-65535 | 0 = new condition
  165. tool:add_wear(-500)
  166. hammer:add_wear(700)
  167. inv:set_stack("tool", 1, tool)
  168. inv:set_stack("hammer", 1, hammer)
  169. return true
  170. end
  171. function workbench.put(_, listname, _, stack)
  172. local stackname = stack:get_name()
  173. if (listname == "tool" and stack:get_wear() > 0 and
  174. workbench:repairable(stackname)) or
  175. (listname == "input" and minetest.registered_nodes[stackname.."_cube"]) or
  176. (listname == "hammer" and stackname == "xdecor:hammer") or
  177. listname == "storage" then
  178. return stack:get_count()
  179. end
  180. return 0
  181. end
  182. function workbench.move(_, from_list, _, to_list, _, count)
  183. return (to_list == "storage" and from_list ~= "forms") and count or 0
  184. end
  185. function workbench.on_put(pos, listname, _, stack)
  186. local inv = minetest.get_meta(pos):get_inventory()
  187. if listname == "input" then
  188. local input = inv:get_stack("input", 1)
  189. workbench:get_output(inv, input, stack:get_name())
  190. elseif listname == "tool" or listname == "hammer" then
  191. local timer = minetest.get_node_timer(pos)
  192. timer:start(3.0)
  193. end
  194. end
  195. function workbench.on_take(pos, listname, index, stack, player)
  196. local inv = minetest.get_meta(pos):get_inventory()
  197. local input = inv:get_stack("input", 1)
  198. local inputname = input:get_name()
  199. local stackname = stack:get_name()
  200. if listname == "input" then
  201. if stackname == inputname then
  202. workbench:get_output(inv, input, stackname)
  203. else
  204. inv:set_list("forms", {})
  205. end
  206. elseif listname == "forms" then
  207. local fromstack = inv:get_stack(listname, index)
  208. if not fromstack:is_empty() and fromstack:get_name() ~= stackname then
  209. local player_inv = player:get_inventory()
  210. if player_inv:room_for_item("main", fromstack) then
  211. player_inv:add_item("main", fromstack)
  212. end
  213. end
  214. input:take_item(ceil(stack:get_count() / workbench.defs[index][2]))
  215. inv:set_stack("input", 1, input)
  216. workbench:get_output(inv, input, inputname)
  217. end
  218. end
  219. minetest.register_node(":xdecor:workbench", {
  220. description = "Work Bench",
  221. paramtype = "light",
  222. paramtype2 = "facedir",
  223. groups = {cracky=2, choppy=2, oddly_breakable_by_hand=1},
  224. sounds = default.node_sound_wood_defaults(),
  225. tiles = {"workbench_top.png", "workbench_top.png",
  226. "workbench_sides.png", "workbench_sides.png",
  227. "workbench_front.png", "workbench_front.png"},
  228. on_rotate = screwdriver.rotate_simple,
  229. can_dig = workbench.dig,
  230. on_timer = workbench.timer,
  231. on_construct = workbench.construct,
  232. on_receive_fields = workbench.fields,
  233. on_metadata_inventory_put = workbench.on_put,
  234. on_metadata_inventory_take = workbench.on_take,
  235. allow_metadata_inventory_put = workbench.put,
  236. allow_metadata_inventory_move = workbench.move
  237. })
  238. minetest.register_tool(":xdecor:hammer", {
  239. description = "Hammer",
  240. inventory_image = "xdecor_hammer.png",
  241. wield_image = "xdecor_hammer.png",
  242. on_use = function() do return end end
  243. })
  244. minetest.register_craft({
  245. output = "xdecor:workbench",
  246. recipe = {
  247. {"group:wood", "group:wood"},
  248. {"group:wood", "group:wood"}
  249. }
  250. })
  251. minetest.register_craft({
  252. output = "xdecor:hammer",
  253. recipe = {
  254. {"default:steel_ingot", "group:stick", "default:steel_ingot"},
  255. {"", "group:stick", ""}
  256. }
  257. })
  258. for _, d in pairs(workbench.defs) do
  259. for i=1, #nodes do
  260. local node = nodes[i]
  261. local def = minetest.registered_nodes[node]
  262. if d[3] then
  263. local groups = {}
  264. local tiles
  265. groups.not_in_creative_inventory = 1
  266. for k, v in pairs(def.groups) do
  267. if k ~= "wood" and k ~= "stone" and k ~= "level" then
  268. groups[k] = v
  269. end
  270. end
  271. if def.tiles then
  272. if #def.tiles > 1 and not (def.drawtype:sub(1,5) == "glass") then
  273. tiles = def.tiles
  274. else
  275. tiles = {def.tiles[1]}
  276. end
  277. else
  278. tiles = {def.tile_images[1]}
  279. end
  280. if not minetest.registered_nodes["stairs:slab_"..node:match(":(.*)")] then
  281. stairs.register_stair_and_slab(node:match(":(.*)"), node,
  282. groups, tiles, def.description.." Stair",
  283. def.description.." Slab", def.sounds)
  284. end
  285. minetest.register_node(":"..node.."_"..d[1], {
  286. description = def.description.." "..d[1]:gsub("^%l", string.upper),
  287. paramtype = "light",
  288. paramtype2 = "facedir",
  289. drawtype = "nodebox",
  290. sounds = def.sounds,
  291. tiles = tiles,
  292. groups = groups,
  293. -- `unpack` has been changed to `table.unpack` in newest Lua versions.
  294. node_box = workbench:pixelbox(16, {unpack(d, 3)}),
  295. sunlight_propagates = true,
  296. on_place = minetest.rotate_node
  297. })
  298. end
  299. end
  300. end