crusher.lua 9.8 KB

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