crusher.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. grinder = grinder or {}
  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. machines.swap_node(pos, "crusher:active")
  163. -- make sure timer restarts automatically
  164. result = true
  165. else
  166. if not fuellist[1]:is_empty() then
  167. fuel_state = "0%"
  168. end
  169. machines.swap_node(pos, "crusher:inactive")
  170. -- stop timer on the inactive furnace
  171. local timer = minetest.get_node_timer(pos)
  172. timer:stop()
  173. end
  174. local infotext = "Crushing Machine " .. active .. "\n" ..
  175. "Item: " .. item_state .. "\n" .. "Fuel Burn: " .. fuel_state
  176. --
  177. -- Set meta values
  178. --
  179. meta:set_float("fuel_totaltime", fuel_totaltime)
  180. meta:set_float("fuel_time", fuel_time)
  181. meta:set_float("src_time", src_time)
  182. meta:set_string("formspec", formspec)
  183. meta:set_string("infotext", infotext)
  184. return result
  185. end
  186. crusher.on_blast = function(pos)
  187. local drops = {}
  188. default.get_inventory_drops(pos, "src", drops)
  189. default.get_inventory_drops(pos, "fuel", drops)
  190. default.get_inventory_drops(pos, "dst", drops)
  191. drops[#drops+1] = "crusher:inactive"
  192. minetest.remove_node(pos)
  193. return drops
  194. end
  195. crusher.on_construct = function(pos)
  196. local meta = minetest.get_meta(pos)
  197. meta:set_string("infotext", "Crushing Machine")
  198. meta:set_string("formspec", crusher.get_inactive_formspec())
  199. local inv = meta:get_inventory()
  200. inv:set_size('src', 1)
  201. inv:set_size('fuel', 1)
  202. inv:set_size('dst', 4)
  203. end
  204. crusher.on_metadata_inventory_move = function(pos)
  205. local timer = minetest.get_node_timer(pos)
  206. if not timer:is_started() then
  207. timer:start(1.0)
  208. end
  209. end
  210. crusher.on_metadata_inventory_put = function(pos)
  211. -- Start timer function, it will sort out whether furnace can burn or not.
  212. local timer = minetest.get_node_timer(pos)
  213. if not timer:is_started() then
  214. timer:start(1.0)
  215. end
  216. end
  217. crusher.on_metadata_inventory_take = function(pos)
  218. local timer = minetest.get_node_timer(pos)
  219. if not timer:is_started() then
  220. timer:start(1.0)
  221. end
  222. end
  223. if not crusher.registered then
  224. for k, v in ipairs({
  225. {name="inactive", light=0, tile="crusher_front.png"},
  226. {name="active", light=7, tile="crusher_front_active.png"},
  227. }) do
  228. minetest.register_node(":crusher:" .. v.name, {
  229. description = "Crushing Machine\n\nCrushes (or grinds) things, but slowly and inefficiently.\nUses coal or kalite for fuel.",
  230. tiles = {
  231. "crusher_top.png", "crusher_bottom.png",
  232. "crusher_side.png", "crusher_side.png",
  233. "crusher_side.png", v.tile,
  234. },
  235. paramtype2 = "facedir",
  236. groups = utility.dig_groups("machine", {immovable = 1}),
  237. light_source = v.light,
  238. on_rotate = function(...) return screwdriver.rotate_simple(...) end,
  239. is_ground_content = false,
  240. sounds = default.node_sound_stone_defaults(),
  241. drop = "crusher:inactive",
  242. can_dig = function(...)
  243. return crusher.can_dig(...) end,
  244. on_timer = function(...)
  245. return crusher.on_timer(...) end,
  246. on_construct = function(...)
  247. return crusher.on_construct(...) end,
  248. on_blast = function(...)
  249. return crusher.on_blast(...) end,
  250. on_metadata_inventory_move = function(...)
  251. return crusher.on_metadata_inventory_move(...) end,
  252. on_metadata_inventory_put = function(...)
  253. return crusher.on_metadata_inventory_put(...) end,
  254. on_metadata_inventory_take = function(...)
  255. return crusher.on_metadata_inventory_take(...) end,
  256. allow_metadata_inventory_put = function(...)
  257. return crusher.allow_metadata_inventory_put(...) end,
  258. allow_metadata_inventory_move = function(...)
  259. return crusher.allow_metadata_inventory_move(...) end,
  260. allow_metadata_inventory_take = function(...)
  261. return crusher.allow_metadata_inventory_take(...) end,
  262. })
  263. end
  264. minetest.register_craft({
  265. output = 'crusher:inactive',
  266. recipe = {
  267. {'default:cobble', 'default:steelblock','default:cobble'},
  268. {'', 'default:diamondblock', ''},
  269. {'default:stonebrick','default:cobble','default:stonebrick'},
  270. }
  271. })
  272. minetest.register_craft({
  273. type = "crushing",
  274. output = 'default:gravel',
  275. recipe = 'default:cobble',
  276. time = 60*2,
  277. })
  278. minetest.register_craft({
  279. type = "crushing",
  280. output = 'darkage:darkdirt',
  281. recipe = 'default:gravel',
  282. time = 60*1.5,
  283. })
  284. minetest.register_craft({
  285. type = "crushing",
  286. output = 'default:sand',
  287. recipe = 'default:stone',
  288. time = 60*2.5,
  289. })
  290. minetest.register_craft({
  291. type = "crushing",
  292. output = 'default:desert_sand',
  293. recipe = 'default:desert_stone',
  294. time = 60*2.5,
  295. })
  296. minetest.register_craft({
  297. type = "crushing",
  298. output = 'default:obsidian_shard 6',
  299. recipe = 'default:obsidian',
  300. time = 60*3,
  301. })
  302. crusher.registered = true
  303. end