mp_compactor.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. --[[
  2. Source: https://github.com/minetest/minetest_game/tree/master/mods/default/furnace.lua
  3. Adapted to work as manual powered grinder
  4. ]]
  5. local S = minetest.get_translator("default")
  6. -- List of sound handles for active furnace
  7. local grinder_fire_sounds = {}
  8. --
  9. -- Formspecs
  10. --
  11. function technic_farming.get_compactor_active_formspec(item_percent)
  12. return "size[8,8.5]"..
  13. "list[context;src;2.75,0.5;1,1;]"..
  14. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  15. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  16. "list[context;dst;4.75,0.96;2,2;]"..
  17. "list[current_player;main;0,4.25;8,1;]"..
  18. "list[current_player;main;0,5.5;8,3;8]"..
  19. "listring[context;dst]"..
  20. "listring[current_player;main]"..
  21. "listring[context;src]"..
  22. "listring[current_player;main]"..
  23. default.get_hotbar_bg(0, 4.25)
  24. end
  25. function technic_farming.get_compactor_inactive_formspec()
  26. return "size[8,8.5]"..
  27. "list[context;src;2.75,0.5;1,1;]"..
  28. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[transformR270]"..
  29. "list[context;dst;4.75,0.96;2,2;]"..
  30. "list[current_player;main;0,4.25;8,1;]"..
  31. "list[current_player;main;0,5.5;8,3;8]"..
  32. "listring[context;dst]"..
  33. "listring[current_player;main]"..
  34. "listring[context;src]"..
  35. "listring[current_player;main]"..
  36. default.get_hotbar_bg(0, 4.25)
  37. end
  38. local function stop_compactor_sound(pos, fadeout_step)
  39. local hash = minetest.hash_node_position(pos)
  40. local sound_ids = grinder_fire_sounds[hash]
  41. if sound_ids then
  42. for _, sound_id in ipairs(sound_ids) do
  43. minetest.sound_fade(sound_id, -1, 0)
  44. end
  45. grinder_fire_sounds[hash] = nil
  46. end
  47. end
  48. local function check_node_around_mill(pos)
  49. local node = minetest.get_node(pos)
  50. if node.name == "default:water_flowing"
  51. or node.name == "default:river_water_flowing" then
  52. return node.param2 or 0-- returns approx. water flow, if any
  53. end
  54. return 0
  55. end
  56. local function check_node_around_windmill(pos)
  57. local node = minetest.get_node(pos)
  58. if node.name == "air" then
  59. return math.random(5,12)-- returns approx. water flow, if any
  60. end
  61. return 0
  62. end
  63. local function compactor_node_timer(pos, elapsed)
  64. --
  65. -- Initialize metadata
  66. --
  67. local meta = minetest.get_meta(pos)
  68. local src_time = meta:get_float("src_time") or 0
  69. local inv = meta:get_inventory()
  70. local srclist
  71. local dst_full = false
  72. local full_flow = 120
  73. local timer_elapsed = meta:get_int("timer_elapsed") or 0
  74. meta:set_int("timer_elapsed", timer_elapsed + 1)
  75. -- get flows from water and/or wind mill
  76. local water_flow = technic_farming.get_water_flow(pos)
  77. local air_flow = technic_farming.get_air_flow(pos)
  78. -- ensure that grinding with mp machines are slower than with electric machines
  79. local water_rel_time = 0.75 * water_flow / full_flow
  80. local air_rel_time = 0.75 * air_flow / full_flow
  81. local rel_time = math.min(water_rel_time + air_rel_time, full_flow)
  82. local update = true
  83. local grindable
  84. local grindtime = 0
  85. while elapsed > 0 and update do
  86. update = false
  87. srclist = inv:get_list("src")
  88. -- Check if we have grindable content
  89. local grinded = technic.get_recipe("compacting",srclist)
  90. grindtime = 0
  91. local grindoutput
  92. if grinded then
  93. grindtime = grinded.time or 1
  94. end
  95. grindable = grindtime ~= 0
  96. if grindable then
  97. src_time = src_time + rel_time
  98. if src_time >= grindtime then
  99. -- Place result in dst list if possible
  100. if inv:room_for_item("dst", grindoutput) then
  101. inv:add_item("dst", grinded.output)
  102. inv:set_stack("src",1,grinded.new_input[1])
  103. src_time = src_time - grindtime
  104. update = true
  105. else
  106. dst_full = true
  107. end
  108. -- Play cooling sound
  109. minetest.sound_play("default_cool_lava",
  110. {pos = pos, max_hear_distance = 16, gain = 0.07}, true)
  111. else
  112. -- Item could not be grinded
  113. update = true
  114. end
  115. end
  116. elapsed = elapsed - 1
  117. end
  118. --
  119. -- Update formspec, infotext and node
  120. --
  121. local formspec
  122. local item_state
  123. local item_percent = 0
  124. if grindable then
  125. item_percent = math.floor(src_time / grindtime * 100)
  126. if dst_full then
  127. item_state = S("100% (output full)")
  128. else
  129. item_state = S("@1%", item_percent)
  130. end
  131. else
  132. if srclist and not srclist[1]:is_empty() then
  133. item_state = S("Not grindable")
  134. else
  135. item_state = S("Empty")
  136. end
  137. end
  138. local active = true
  139. if (srclist and srclist[1]:is_empty()) then
  140. print("empty")
  141. active = false
  142. elseif (srclist == nil) then
  143. -- elapsed was zero
  144. active = false
  145. end
  146. local result = false
  147. if active then
  148. -- active = true
  149. formspec = technic_farming.get_compactor_active_formspec(item_percent)
  150. technic_farming.swap_node(pos, "technic_farming:mp_compactor_active")
  151. -- make sure timer restarts automatically
  152. result = true
  153. -- Play sound every 5 seconds while the furnace is active
  154. if timer_elapsed == 0 or (timer_elapsed + 1) % 5 == 0 then
  155. local sound_id = minetest.sound_play("technic_farming:mp_compactor_active",
  156. {pos = pos, max_hear_distance = 16, gain = 0.25})
  157. local hash = minetest.hash_node_position(pos)
  158. compactor_fire_sounds[hash] = compactor_fire_sounds[hash] or {}
  159. table.insert(compactor_fire_sounds[hash], sound_id)
  160. -- Only remember the 3 last sound handles
  161. if #compactor_fire_sounds[hash] > 3 then
  162. table.remove(compactor_fire_sounds[hash], 1)
  163. end
  164. -- Remove the sound ID automatically from table after 11 seconds
  165. minetest.after(11, function()
  166. if not compactor_fire_sounds[hash] then
  167. return
  168. end
  169. for f=#compactor_fire_sounds[hash], 1, -1 do
  170. if compactor_fire_sounds[hash][f] == sound_id then
  171. table.remove(compactor_fire_sounds[hash], f)
  172. end
  173. end
  174. if #compactor_fire_sounds[hash] == 0 then
  175. compactor_fire_sounds[hash] = nil
  176. end
  177. end)
  178. end
  179. else
  180. formspec = technic_farming.get_compactor_inactive_formspec()
  181. technic_farming.swap_node(pos, "technic_farming:mp_compactor")
  182. -- stop timer on the inactive furnace
  183. minetest.get_node_timer(pos):stop()
  184. meta:set_int("timer_elapsed", 0)
  185. stop_compactor_sound(pos)
  186. end
  187. local infotext
  188. if active then
  189. infotext = S("Compactor active")
  190. else
  191. infotext = S("Compactor inactive")
  192. end
  193. infotext = infotext .. "\n" .. S("(Item: @1)", item_state)
  194. --
  195. -- Set meta values
  196. --
  197. meta:set_float("src_time", src_time)
  198. meta:set_string("formspec", formspec)
  199. meta:set_string("infotext", infotext)
  200. return result
  201. end
  202. --
  203. -- Node definitions
  204. --
  205. minetest.register_node("technic_farming:mp_compactor", {
  206. description = S("MP Compactor"),
  207. tiles = {
  208. "technic_farming_mp_compactor_top.png",
  209. "technic_farming_mp_compactor_bottom.png",
  210. "technic_farming_mp_compactor_side.png",
  211. "technic_farming_mp_compactor_side.png",
  212. "technic_farming_mp_compactor_side.png",
  213. "technic_farming_mp_compactor_front.png"
  214. },
  215. paramtype2 = "facedir",
  216. groups = {cracky=2},
  217. legacy_facedir_simple = true,
  218. is_ground_content = false,
  219. sounds = default.node_sound_stone_defaults(),
  220. can_dig = technic_farming.can_dig,
  221. on_timer = compactor_node_timer,
  222. on_construct = function(pos)
  223. local meta = minetest.get_meta(pos)
  224. local inv = meta:get_inventory()
  225. inv:set_size('src', 1)
  226. inv:set_size('dst', 4)
  227. compactor_node_timer(pos, 0)
  228. end,
  229. on_metadata_inventory_move = function(pos)
  230. minetest.get_node_timer(pos):start(1.0)
  231. end,
  232. on_metadata_inventory_put = function(pos)
  233. minetest.get_node_timer(pos):start(1.0)
  234. end,
  235. on_metadata_inventory_take = function(pos)
  236. minetest.get_node_timer(pos):start(1.0)
  237. end,
  238. on_blast = function(pos)
  239. local drops = {}
  240. default.get_inventory_drops(pos, "src", drops)
  241. default.get_inventory_drops(pos, "dst", drops)
  242. drops[#drops+1] = "technic_farming:mp_compactor"
  243. minetest.remove_node(pos)
  244. return drops
  245. end,
  246. allow_metadata_inventory_put = technic_farming.allow_metadata_inventory_put,
  247. allow_metadata_inventory_move = technic_farming.allow_metadata_inventory_move,
  248. allow_metadata_inventory_take = technic_farming.allow_metadata_inventory_take,
  249. })
  250. minetest.register_node("technic_farming:mp_compactor_active", {
  251. description = S("Compactor"),
  252. tiles = {
  253. "technic_farming_mp_compactor_top.png",
  254. "technic_farming_mp_compactor_bottom.png",
  255. "technic_farming_mp_compactor_side.png",
  256. "technic_farming_mp_compactor_side.png",
  257. "technic_farming_mp_compactor_side.png",
  258. "technic_farming_mp_compactor_front.png"
  259. },
  260. paramtype2 = "facedir",
  261. light_source = 8,
  262. drop = "technic_farming:mp_compactor",
  263. groups = {cracky=2, not_in_creative_inventory=1},
  264. legacy_facedir_simple = true,
  265. is_ground_content = false,
  266. sounds = default.node_sound_stone_defaults(),
  267. on_timer = compactor_node_timer,
  268. on_destruct = function(pos)
  269. stop_compactor_sound(pos)
  270. end,
  271. can_dig = technic_farming.can_dig,
  272. allow_metadata_inventory_put = technic_farming.allow_metadata_inventory_put,
  273. allow_metadata_inventory_move = technic_farming.allow_metadata_inventory_move,
  274. allow_metadata_inventory_take = technic_farming.allow_metadata_inventory_take,
  275. })
  276. minetest.register_craft({
  277. output = 'technic_farming:mp_compactor',
  278. recipe = {
  279. {'technic_farming:wooden_screw', 'technic_farming:wooden_screw','technic_farming:wooden_screw'},
  280. {'group:wood', 'technic_farming:stone_plate', 'group:wood'},
  281. {'group:stone', 'technic_farming:stone_plate', 'group:stone'},
  282. }
  283. })