crusher.lua 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. if not minetest.global_exists("grinder") then grinder = {} end
  2. grinder.crusher = grinder.crusher or {}
  3. local crusher = grinder.crusher
  4. -- Localize for performance.
  5. local math_floor = math.floor
  6. -- Get active formspec.
  7. crusher.get_active_formspec = function(fuel_percent, item_percent)
  8. local formspec =
  9. "size[8,8.5]"..
  10. default.formspec.get_form_colors() ..
  11. default.formspec.get_form_image() ..
  12. default.formspec.get_slot_colors() ..
  13. "label[2.75,0;Fuel & Input]" ..
  14. "list[context;src;2.75,0.5;1,1;]"..
  15. "list[context;fuel;2.75,2.5;1,1;]"..
  16. "image[2.75,1.5;1,1;machine_progress_bg.png^[lowpart:" ..
  17. (100-fuel_percent) .. ":machine_progress_fg.png]" ..
  18. "image[3.75,1.5;1,1;gui_furnace_arrow_bg.png^[lowpart:"..
  19. (item_percent)..":gui_furnace_arrow_fg.png^[transformR270]"..
  20. "label[4.75,0.46;Destination]" ..
  21. "list[context;dst;4.75,0.96;2,2;]"..
  22. "list[current_player;main;0,4.25;8,1;]"..
  23. "list[current_player;main;0,5.5;8,3;8]"..
  24. "listring[context;dst]"..
  25. "listring[current_player;main]"..
  26. "listring[context;src]"..
  27. "listring[current_player;main]"..
  28. "listring[context;fuel]"..
  29. "listring[current_player;main]"..
  30. default.get_hotbar_bg(0, 4.25)
  31. return formspec
  32. end
  33. crusher.get_inactive_formspec = function()
  34. return crusher.get_active_formspec(100, 0)
  35. end
  36. crusher.can_dig = function(pos, player)
  37. local meta = minetest.get_meta(pos);
  38. local inv = meta:get_inventory()
  39. return inv:is_empty("fuel") and inv:is_empty("dst") and inv:is_empty("src")
  40. end
  41. crusher.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  42. if minetest.test_protection(pos, player:get_player_name()) then
  43. return 0
  44. end
  45. local meta = minetest.get_meta(pos)
  46. local inv = meta:get_inventory()
  47. if listname == "fuel" then
  48. if minetest.get_craft_result({method="fuel", width=1, items={stack}}).time ~= 0 then
  49. if inv:is_empty("src") then
  50. meta:set_string("infotext", "Crushing Machine is Empty.")
  51. end
  52. return stack:get_count()
  53. else
  54. return 0
  55. end
  56. elseif listname == "src" then
  57. return stack:get_count()
  58. elseif listname == "dst" then
  59. return 0
  60. end
  61. end
  62. crusher.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  63. local meta = minetest.get_meta(pos)
  64. local inv = meta:get_inventory()
  65. local stack = inv:get_stack(from_list, from_index)
  66. return crusher.allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
  67. end
  68. crusher.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  69. if minetest.test_protection(pos, player:get_player_name()) then
  70. return 0
  71. end
  72. return stack:get_count()
  73. end
  74. crusher.on_timer = function(pos, elapsed)
  75. --
  76. -- Inizialize metadata
  77. --
  78. local meta = minetest.get_meta(pos)
  79. local fuel_time = meta:get_float("fuel_time") or 0
  80. local src_time = meta:get_float("src_time") or 0
  81. local fuel_totaltime = meta:get_float("fuel_totaltime") or 0
  82. local inv = meta:get_inventory()
  83. local srclist = inv:get_list("src")
  84. local fuellist = inv:get_list("fuel")
  85. --
  86. -- Cooking
  87. --
  88. -- Check if we have cookable content
  89. local cooked, aftercooked = minetest.get_craft_result({
  90. method = "crushing", width = 1, items = srclist})
  91. local cookable = true
  92. if cooked.time == 0 then
  93. cookable = false
  94. end
  95. -- Check if we have enough fuel to burn
  96. if fuel_time < fuel_totaltime then
  97. -- The furnace is currently active and has enough fuel
  98. fuel_time = fuel_time + 1
  99. -- If there is a cookable item then check if it is ready yet
  100. if cookable then
  101. src_time = src_time + 1
  102. if src_time >= cooked.time then
  103. -- Place result in dst list if possible
  104. if inv:room_for_item("dst", cooked.item) then
  105. inv:add_item("dst", cooked.item)
  106. inv:set_stack("src", 1, aftercooked.items[1])
  107. src_time = 0
  108. end
  109. end
  110. end
  111. else
  112. -- Furnace ran out of fuel
  113. if cookable then
  114. -- We need to get new fuel
  115. local fuel, afterfuel = minetest.get_craft_result({method = "fuel", width = 1, items = fuellist})
  116. if fuel.time == 0 then
  117. -- No valid fuel in fuel list
  118. fuel_totaltime = 0
  119. fuel_time = 0
  120. src_time = 0
  121. else
  122. -- Take fuel from fuel list
  123. inv:set_stack("fuel", 1, afterfuel.items[1])
  124. fuel_totaltime = fuel.time
  125. fuel_time = 0
  126. end
  127. else
  128. -- We don't need to get new fuel since there is no cookable item
  129. fuel_totaltime = 0
  130. fuel_time = 0
  131. src_time = 0
  132. end
  133. end
  134. --
  135. -- Update formspec, infotext and node
  136. --
  137. local formspec = crusher.get_inactive_formspec()
  138. local item_state
  139. local item_percent = 0
  140. if cookable then
  141. item_percent = math_floor(src_time / cooked.time * 100)
  142. if item_percent > 100 then
  143. item_state = "100% (Output Full)"
  144. else
  145. item_state = item_percent .. "%"
  146. end
  147. else
  148. if srclist[1]:is_empty() then
  149. item_state = "Empty"
  150. else
  151. item_state = "Not Crushable"
  152. end
  153. end
  154. local fuel_state = "Empty"
  155. local active = "Inactive "
  156. local result = false
  157. if fuel_time <= fuel_totaltime and fuel_totaltime ~= 0 then
  158. active = "Active "
  159. local fuel_percent = math_floor(fuel_time / fuel_totaltime * 100)
  160. fuel_state = fuel_percent .. "%"
  161. formspec = crusher.get_active_formspec(fuel_percent, item_percent)
  162. local swapped = machines.swap_node(pos, "crusher:active")
  163. if swapped then
  164. ambiance.spawn_sound_beacon("ambiance:grinder_active", pos)
  165. end
  166. -- make sure timer restarts automatically
  167. result = true
  168. else
  169. if not fuellist[1]:is_empty() then
  170. fuel_state = "0%"
  171. end
  172. machines.swap_node(pos, "crusher:inactive")
  173. -- stop timer on the inactive furnace
  174. local timer = minetest.get_node_timer(pos)
  175. timer:stop()
  176. end
  177. local infotext = "Crushing Machine " .. active .. "\n" ..
  178. "Item: " .. item_state .. "\n" .. "Fuel Burn: " .. fuel_state
  179. --
  180. -- Set meta values
  181. --
  182. meta:set_float("fuel_totaltime", fuel_totaltime)
  183. meta:set_float("fuel_time", fuel_time)
  184. meta:set_float("src_time", src_time)
  185. meta:set_string("formspec", formspec)
  186. meta:set_string("infotext", infotext)
  187. return result
  188. end
  189. crusher.on_blast = function(pos)
  190. local drops = {}
  191. default.get_inventory_drops(pos, "src", drops)
  192. default.get_inventory_drops(pos, "fuel", drops)
  193. default.get_inventory_drops(pos, "dst", drops)
  194. drops[#drops+1] = "crusher:inactive"
  195. minetest.remove_node(pos)
  196. return drops
  197. end
  198. crusher.on_construct = function(pos)
  199. local meta = minetest.get_meta(pos)
  200. meta:set_string("infotext", "Crushing Machine")
  201. meta:set_string("formspec", crusher.get_inactive_formspec())
  202. local inv = meta:get_inventory()
  203. inv:set_size('src', 1)
  204. inv:set_size('fuel', 1)
  205. inv:set_size('dst', 4)
  206. end
  207. crusher.on_metadata_inventory_move = function(pos)
  208. local timer = minetest.get_node_timer(pos)
  209. if not timer:is_started() then
  210. timer:start(1.0)
  211. end
  212. end
  213. crusher.on_metadata_inventory_put = function(pos)
  214. -- Start timer function, it will sort out whether furnace can burn or not.
  215. local timer = minetest.get_node_timer(pos)
  216. if not timer:is_started() then
  217. timer:start(1.0)
  218. end
  219. end
  220. crusher.on_metadata_inventory_take = function(pos)
  221. local timer = minetest.get_node_timer(pos)
  222. if not timer:is_started() then
  223. timer:start(1.0)
  224. end
  225. end
  226. if not crusher.registered then
  227. for k, v in ipairs({
  228. {name="inactive", light=0, tile="crusher_front.png"},
  229. {name="active", light=7, tile="crusher_front_active.png"},
  230. }) do
  231. minetest.register_node(":crusher:" .. v.name, {
  232. description = "Crushing Machine\n\nCrushes (or grinds) things, but slowly and inefficiently.\nUses coal or kalite for fuel.",
  233. tiles = {
  234. "crusher_top.png", "crusher_bottom.png",
  235. "crusher_side.png", "crusher_side.png",
  236. "crusher_side.png", v.tile,
  237. },
  238. paramtype2 = "facedir",
  239. groups = utility.dig_groups("machine", {immovable = 1}),
  240. light_source = v.light,
  241. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  242. is_ground_content = false,
  243. sounds = default.node_sound_stone_defaults(),
  244. drop = "crusher:inactive",
  245. can_dig = function(...)
  246. return crusher.can_dig(...) end,
  247. on_timer = function(...)
  248. return crusher.on_timer(...) end,
  249. on_construct = function(...)
  250. return crusher.on_construct(...) end,
  251. on_blast = function(...)
  252. return crusher.on_blast(...) end,
  253. on_metadata_inventory_move = function(...)
  254. return crusher.on_metadata_inventory_move(...) end,
  255. on_metadata_inventory_put = function(...)
  256. return crusher.on_metadata_inventory_put(...) end,
  257. on_metadata_inventory_take = function(...)
  258. return crusher.on_metadata_inventory_take(...) end,
  259. allow_metadata_inventory_put = function(...)
  260. return crusher.allow_metadata_inventory_put(...) end,
  261. allow_metadata_inventory_move = function(...)
  262. return crusher.allow_metadata_inventory_move(...) end,
  263. allow_metadata_inventory_take = function(...)
  264. return crusher.allow_metadata_inventory_take(...) end,
  265. })
  266. end
  267. minetest.register_craft({
  268. output = 'crusher:inactive',
  269. recipe = {
  270. {'default:cobble', 'default:steelblock','default:cobble'},
  271. {'', 'default:diamondblock', ''},
  272. {'default:stonebrick','default:cobble','default:stonebrick'},
  273. }
  274. })
  275. minetest.register_craft({
  276. type = "crushing",
  277. output = 'default:gravel',
  278. recipe = 'default:cobble',
  279. time = 60*2,
  280. })
  281. minetest.register_craft({
  282. type = "crushing",
  283. output = 'darkage:darkdirt',
  284. recipe = 'default:gravel',
  285. time = 60*1.5,
  286. })
  287. minetest.register_craft({
  288. type = "crushing",
  289. output = 'default:sand',
  290. recipe = 'default:stone',
  291. time = 60*2.5,
  292. })
  293. minetest.register_craft({
  294. type = "crushing",
  295. output = 'default:desert_sand',
  296. recipe = 'default:desert_stone',
  297. time = 60*2.5,
  298. })
  299. minetest.register_craft({
  300. type = "crushing",
  301. output = 'default:obsidian_shard 6',
  302. recipe = 'default:obsidian',
  303. time = 60*3,
  304. })
  305. crusher.registered = true
  306. end