circular_saw.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. --[[
  2. More Blocks: circular saw
  3. Copyright (c) 2011-2017 Hugo Locurcio, Sokomine and contributors.
  4. Licensed under the zlib license. See LICENSE.md for more information.
  5. --]]
  6. local S = moreblocks.intllib
  7. circular_saw = {}
  8. circular_saw.known_stairs = setmetatable({}, {
  9. __newindex = function(k, v)
  10. local modname = minetest.get_current_modname()
  11. print(("WARNING: mod %s tried to add node %s to the circular saw"
  12. .. " manually."):format(modname, v))
  13. end,
  14. })
  15. -- This is populated by stairsplus:register_all:
  16. circular_saw.known_nodes = {}
  17. -- How many microblocks does this shape at the output inventory cost:
  18. -- It may cause slight loss, but no gain.
  19. circular_saw.cost_in_microblocks = {
  20. 1, 1, 1, 1, 1, 1, 1, 2,
  21. 2, 3, 2, 4, 2, 4, 5, 6,
  22. 7, 1, 1, 2, 4, 6, 7, 8,
  23. 1, 2, 2, 3, 1, 1, 2, 4,
  24. 4, 2, 6, 7, 3, 7, 7, 4,
  25. 8, 3, 2, 6, 2, 1, 3, 4
  26. }
  27. circular_saw.names = {
  28. {"micro", "_1"},
  29. {"panel", "_1"},
  30. {"micro", "_2"},
  31. {"panel", "_2"},
  32. {"micro", "_4"},
  33. {"panel", "_4"},
  34. {"micro", ""},
  35. {"panel", ""},
  36. {"micro", "_12"},
  37. {"panel", "_12"},
  38. {"micro", "_14"},
  39. {"panel", "_14"},
  40. {"micro", "_15"},
  41. {"panel", "_15"},
  42. {"stair", "_outer"},
  43. {"stair", ""},
  44. {"stair", "_inner"},
  45. {"slab", "_1"},
  46. {"slab", "_2"},
  47. {"slab", "_quarter"},
  48. {"slab", ""},
  49. {"slab", "_three_quarter"},
  50. {"slab", "_14"},
  51. {"slab", "_15"},
  52. {"slab", "_two_sides"},
  53. {"slab", "_three_sides"},
  54. {"slab", "_three_sides_u"},
  55. {"stair", "_half"},
  56. {"stair", "_alt_1"},
  57. {"stair", "_alt_2"},
  58. {"stair", "_alt_4"},
  59. {"stair", "_alt"},
  60. {"slope", ""},
  61. {"slope", "_half"},
  62. {"slope", "_half_raised"},
  63. {"slope", "_inner"},
  64. {"slope", "_inner_half"},
  65. {"slope", "_inner_half_raised"},
  66. {"slope", "_inner_cut"},
  67. {"slope", "_inner_cut_half"},
  68. {"slope", "_inner_cut_half_raised"},
  69. {"slope", "_outer"},
  70. {"slope", "_outer_half"},
  71. {"slope", "_outer_half_raised"},
  72. {"slope", "_outer_cut"},
  73. {"slope", "_outer_cut_half"},
  74. {"slope", "_outer_cut_half_raised"},
  75. {"slope", "_cut"},
  76. }
  77. function circular_saw:get_cost(inv, stackname)
  78. for i, item in pairs(inv:get_list("output")) do
  79. if item:get_name() == stackname then
  80. return circular_saw.cost_in_microblocks[i]
  81. end
  82. end
  83. end
  84. function circular_saw:get_output_inv(modname, material, amount, max)
  85. if (not max or max < 1 or max > 99) then max = 99 end
  86. local list = {}
  87. local pos = #list
  88. -- If there is nothing inside, display empty inventory:
  89. if amount < 1 then
  90. return list
  91. end
  92. for i = 1, #circular_saw.names do
  93. local t = circular_saw.names[i]
  94. local cost = circular_saw.cost_in_microblocks[i]
  95. local balance = math.min(math.floor(amount/cost), max)
  96. local nodename = modname .. ":" .. t[1] .. "_" .. material .. t[2]
  97. if minetest.registered_nodes[nodename] then
  98. pos = pos + 1
  99. list[pos] = nodename .. " " .. balance
  100. end
  101. end
  102. return list
  103. end
  104. -- Reset empty circular_saw after last full block has been taken out
  105. -- (or the circular_saw has been placed the first time)
  106. -- Note: max_offered is not reset:
  107. function circular_saw:reset(pos)
  108. local meta = minetest.get_meta(pos)
  109. local inv = meta:get_inventory()
  110. inv:set_list("input", {})
  111. inv:set_list("micro", {})
  112. inv:set_list("output", {})
  113. meta:set_int("anz", 0)
  114. meta:set_string("infotext",
  115. S("Circular Saw is empty (owned by %s)")
  116. :format(meta:get_string("owner") or ""))
  117. end
  118. -- Player has taken something out of the box or placed something inside
  119. -- that amounts to count microblocks:
  120. function circular_saw:update_inventory(pos, amount)
  121. local meta = minetest.get_meta(pos)
  122. local inv = meta:get_inventory()
  123. amount = meta:get_int("anz") + amount
  124. -- The material is recycled automaticly.
  125. inv:set_list("recycle", {})
  126. if amount < 1 then -- If the last block is taken out.
  127. self:reset(pos)
  128. return
  129. end
  130. local stack = inv:get_stack("input", 1)
  131. -- At least one "normal" block is necessary to see what kind of stairs are requested.
  132. if stack:is_empty() then
  133. -- Any microblocks not taken out yet are now lost.
  134. -- (covers material loss in the machine)
  135. self:reset(pos)
  136. return
  137. end
  138. local node_name = stack:get_name() or ""
  139. local name_parts = circular_saw.known_nodes[node_name] or ""
  140. local modname = name_parts[1] or ""
  141. local material = name_parts[2] or ""
  142. inv:set_list("input", { -- Display as many full blocks as possible:
  143. node_name.. " " .. math.floor(amount / 8)
  144. })
  145. -- The stairnodes made of default nodes use moreblocks namespace, other mods keep own:
  146. if modname == "default" then
  147. modname = "moreblocks"
  148. end
  149. -- print("circular_saw set to " .. modname .. " : "
  150. -- .. material .. " with " .. (amount) .. " microblocks.")
  151. -- 0-7 microblocks may remain left-over:
  152. inv:set_list("micro", {
  153. modname .. ":micro_" .. material .. "_bottom " .. (amount % 8)
  154. })
  155. -- Display:
  156. inv:set_list("output",
  157. self:get_output_inv(modname, material, amount,
  158. meta:get_int("max_offered")))
  159. -- Store how many microblocks are available:
  160. meta:set_int("anz", amount)
  161. meta:set_string("infotext",
  162. S("Circular Saw is working on %s (owned by %s)")
  163. :format(material, meta:get_string("owner") or ""))
  164. end
  165. -- The amount of items offered per shape can be configured:
  166. function circular_saw.on_receive_fields(pos, formname, fields, sender)
  167. local meta = minetest.get_meta(pos)
  168. local max = tonumber(fields.max_offered)
  169. if max and max > 0 then
  170. meta:set_string("max_offered", max)
  171. -- Update to show the correct number of items:
  172. circular_saw:update_inventory(pos, 0)
  173. end
  174. end
  175. -- Moving the inventory of the circular_saw around is not allowed because it
  176. -- is a fictional inventory. Moving inventory around would be rather
  177. -- impractical and make things more difficult to calculate:
  178. function circular_saw.allow_metadata_inventory_move(
  179. pos, from_list, from_index, to_list, to_index, count, player)
  180. return 0
  181. end
  182. -- Only input- and recycle-slot are intended as input slots:
  183. function circular_saw.allow_metadata_inventory_put(
  184. pos, listname, index, stack, player)
  185. -- The player is not allowed to put something in there:
  186. if listname == "output" or listname == "micro" then
  187. return 0
  188. end
  189. local meta = minetest.get_meta(pos)
  190. local inv = meta:get_inventory()
  191. local stackname = stack:get_name()
  192. local count = stack:get_count()
  193. -- Only alow those items that are offered in the output inventory to be recycled:
  194. if listname == "recycle" then
  195. if not inv:contains_item("output", stackname) then
  196. return 0
  197. end
  198. local stackmax = stack:get_stack_max()
  199. local instack = inv:get_stack("input", 1)
  200. local microstack = inv:get_stack("micro", 1)
  201. local incount = instack:get_count()
  202. local incost = (incount * 8) + microstack:get_count()
  203. local maxcost = (stackmax * 8) + 7
  204. local cost = circular_saw:get_cost(inv, stackname)
  205. if not cost then
  206. return 0
  207. end
  208. if (incost + cost) > maxcost then
  209. return math.max((maxcost - incost) / cost, 0)
  210. end
  211. return count
  212. end
  213. -- Only accept certain blocks as input which are known to be craftable into stairs:
  214. if listname == "input" then
  215. if not inv:is_empty("input") then
  216. if inv:get_stack("input", index):get_name() ~= stackname then
  217. return 0
  218. end
  219. end
  220. if not inv:is_empty("micro") then
  221. local microstackname = inv:get_stack("micro", 1):get_name():gsub("^.+:micro_", "", 1)
  222. local cutstackname = stackname:gsub("^.+:", "", 1)
  223. if microstackname ~= cutstackname then
  224. return 0
  225. end
  226. end
  227. for name, t in pairs(circular_saw.known_nodes) do
  228. if name == stackname and inv:room_for_item("input", stack) then
  229. return count
  230. end
  231. end
  232. return 0
  233. end
  234. end
  235. -- Taking is allowed from all slots (even the internal microblock slot).
  236. -- Putting something in is slightly more complicated than taking anything
  237. -- because we have to make sure it is of a suitable material:
  238. function circular_saw.on_metadata_inventory_put(
  239. pos, listname, index, stack, player)
  240. -- We need to find out if the circular_saw is already set to a
  241. -- specific material or not:
  242. local meta = minetest.get_meta(pos)
  243. local inv = meta:get_inventory()
  244. local stackname = stack:get_name()
  245. local count = stack:get_count()
  246. -- Putting something into the input slot is only possible if that had
  247. -- been empty before or did contain something of the same material:
  248. if listname == "input" then
  249. -- Each new block is worth 8 microblocks:
  250. circular_saw:update_inventory(pos, 8 * count)
  251. elseif listname == "recycle" then
  252. -- Lets look which shape this represents:
  253. local cost = circular_saw:get_cost(inv, stackname)
  254. local input_stack = inv:get_stack("input", 1)
  255. -- check if this would not exceed input itemstack max_stacks
  256. if input_stack:get_count() + ((cost * count) / 8) <= input_stack:get_stack_max() then
  257. circular_saw:update_inventory(pos, cost * count)
  258. end
  259. end
  260. end
  261. function circular_saw.allow_metadata_inventory_take(pos, listname, index, stack, player)
  262. local meta = minetest.get_meta(pos)
  263. local inv = meta:get_inventory()
  264. local input_stack = inv:get_stack(listname, index)
  265. local player_inv = player:get_inventory()
  266. if not player_inv:room_for_item("main", input_stack) then
  267. return 0
  268. else return stack:get_count()
  269. end
  270. end
  271. function circular_saw.on_metadata_inventory_take(
  272. pos, listname, index, stack, player)
  273. -- Prevent (inbuilt) swapping between inventories with different blocks
  274. -- corrupting player inventory or Saw with 'unknown' items.
  275. local meta = minetest.get_meta(pos)
  276. local inv = meta:get_inventory()
  277. local input_stack = inv:get_stack(listname, index)
  278. if not input_stack:is_empty() and input_stack:get_name()~=stack:get_name() then
  279. local player_inv = player:get_inventory()
  280. if player_inv:room_for_item("main", input_stack) then
  281. player_inv:add_item("main", input_stack)
  282. end
  283. circular_saw:reset(pos)
  284. return
  285. end
  286. -- If it is one of the offered stairs: find out how many
  287. -- microblocks have to be substracted:
  288. if listname == "output" then
  289. -- We do know how much each block at each position costs:
  290. local cost = circular_saw.cost_in_microblocks[index]
  291. * stack:get_count()
  292. circular_saw:update_inventory(pos, -cost)
  293. elseif listname == "micro" then
  294. -- Each microblock costs 1 microblock:
  295. circular_saw:update_inventory(pos, -stack:get_count())
  296. elseif listname == "input" then
  297. -- Each normal (= full) block taken costs 8 microblocks:
  298. circular_saw:update_inventory(pos, 8 * -stack:get_count())
  299. end
  300. -- The recycle field plays no role here since it is processed immediately.
  301. end
  302. function circular_saw.on_construct(pos)
  303. local meta = minetest.get_meta(pos)
  304. local fancy_inv = default.gui_bg..default.gui_bg_img..default.gui_slots
  305. meta:set_string("formspec", "size[11,10]"..fancy_inv..
  306. "label[0,0;" ..S("Input\nmaterial").. "]" ..
  307. "list[current_name;input;1.5,0;1,1;]" ..
  308. "label[0,1;" ..S("Left-over").. "]" ..
  309. "list[current_name;micro;1.5,1;1,1;]" ..
  310. "label[0,2;" ..S("Recycle\noutput").. "]" ..
  311. "list[current_name;recycle;1.5,2;1,1;]" ..
  312. "field[0.3,3.5;1,1;max_offered;" ..S("Max").. ":;${max_offered}]" ..
  313. "button[1,3.2;1,1;Set;" ..S("Set").. "]" ..
  314. "list[current_name;output;2.8,0;8,6;]" ..
  315. "list[current_player;main;1.5,6.25;8,4;]")
  316. meta:set_int("anz", 0) -- No microblocks inside yet.
  317. meta:set_string("max_offered", 99) -- How many items of this kind are offered by default?
  318. meta:set_string("infotext", S("Circular Saw is empty"))
  319. local inv = meta:get_inventory()
  320. inv:set_size("input", 1) -- Input slot for full blocks of material x.
  321. inv:set_size("micro", 1) -- Storage for 1-7 surplus microblocks.
  322. inv:set_size("recycle", 1) -- Surplus partial blocks can be placed here.
  323. inv:set_size("output", 6*8) -- 6x8 versions of stair-parts of material x.
  324. circular_saw:reset(pos)
  325. end
  326. function circular_saw.can_dig(pos,player)
  327. local meta = minetest.get_meta(pos)
  328. local inv = meta:get_inventory()
  329. if not inv:is_empty("input") or
  330. not inv:is_empty("micro") or
  331. not inv:is_empty("recycle") then
  332. return false
  333. end
  334. -- Can be dug by anyone when empty, not only by the owner:
  335. return true
  336. end
  337. minetest.register_node("moreblocks:circular_saw", {
  338. description = S("Circular Saw"),
  339. drawtype = "nodebox",
  340. node_box = {
  341. type = "fixed",
  342. fixed = {
  343. {-0.4, -0.5, -0.4, -0.25, 0.25, -0.25}, -- Leg
  344. {0.25, -0.5, 0.25, 0.4, 0.25, 0.4}, -- Leg
  345. {-0.4, -0.5, 0.25, -0.25, 0.25, 0.4}, -- Leg
  346. {0.25, -0.5, -0.4, 0.4, 0.25, -0.25}, -- Leg
  347. {-0.5, 0.25, -0.5, 0.5, 0.375, 0.5}, -- Tabletop
  348. {-0.01, 0.4375, -0.125, 0.01, 0.5, 0.125}, -- Saw blade (top)
  349. {-0.01, 0.375, -0.1875, 0.01, 0.4375, 0.1875}, -- Saw blade (bottom)
  350. {-0.25, -0.0625, -0.25, 0.25, 0.25, 0.25}, -- Motor case
  351. },
  352. },
  353. tiles = {"moreblocks_circular_saw_top.png",
  354. "moreblocks_circular_saw_bottom.png",
  355. "moreblocks_circular_saw_side.png"},
  356. paramtype = "light",
  357. sunlight_propagates = true,
  358. paramtype2 = "facedir",
  359. groups = {choppy = 2,oddly_breakable_by_hand = 2},
  360. sounds = default.node_sound_wood_defaults(),
  361. on_construct = circular_saw.on_construct,
  362. can_dig = circular_saw.can_dig,
  363. -- Set the owner of this circular saw.
  364. after_place_node = function(pos, placer)
  365. local meta = minetest.get_meta(pos)
  366. local owner = placer and placer:get_player_name() or ""
  367. meta:set_string("owner", owner)
  368. meta:set_string("infotext",
  369. S("Circular Saw is empty (owned by %s)")
  370. :format(owner))
  371. end,
  372. -- The amount of items offered per shape can be configured:
  373. on_receive_fields = circular_saw.on_receive_fields,
  374. allow_metadata_inventory_move = circular_saw.allow_metadata_inventory_move,
  375. -- Only input- and recycle-slot are intended as input slots:
  376. allow_metadata_inventory_put = circular_saw.allow_metadata_inventory_put,
  377. allow_metadata_inventory_take = circular_saw.allow_metadata_inventory_take,
  378. -- Taking is allowed from all slots (even the internal microblock slot). Moving is forbidden.
  379. -- Putting something in is slightly more complicated than taking anything because we have to make sure it is of a suitable material:
  380. on_metadata_inventory_put = circular_saw.on_metadata_inventory_put,
  381. on_metadata_inventory_take = circular_saw.on_metadata_inventory_take,
  382. })