station_fuel_pucks.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. stations.puck_fuel_table = {}
  2. stations.puckable_groups = {'flora', 'leaves', 'flower', 'sapling', 'food'}
  3. stations.puck_fuel_table['default:coal_lump'] = true
  4. stations.puck_fuel_table['charcoal:charcoal'] = true
  5. function stations.puckable(input)
  6. for _, v in pairs(stations.puckable_groups) do
  7. if minetest.get_item_group(input, v) > 0 then
  8. return true
  9. end
  10. end
  11. return false
  12. end
  13. function stations.puck_fuel(input)
  14. if stations.puck_fuel_table[input] then
  15. return true
  16. end
  17. return false
  18. end
  19. function stations.puck_formspec(cycles)
  20. return 'size[8,8.5]'..
  21. 'list[current_name;src;0.5,0.5;3,3;]'..
  22. 'list[current_name;dst;5.5,1;2,2;]'..
  23. 'list[current_name;fuel;4,2.5;1,1;]'..
  24. 'image[4,1.5;1,1;default_furnace_fire_bg.png^[lowpart:'..
  25. (cycles*10)..':default_furnace_fire_fg.png]'..
  26. 'list[current_player;main;0,4.5;8,4;]'..
  27. 'listring[context;dst]'..
  28. 'listring[current_player;main]'..
  29. 'listring[context;src]'
  30. end
  31. function stations.puck_can_create(pos)
  32. local timer = minetest.get_node_timer(pos)
  33. local meta = minetest.get_meta(pos)
  34. local cycles_count = tonumber(meta:get_string('cycles_count'))
  35. local inv = meta:get_inventory()
  36. local instack = inv:get_stack('src', 1)
  37. local outstack = inv:get_stack('dst', 1)
  38. local fuel = inv:get_stack('fuel', 1)
  39. local outstack_count = outstack:get_count()
  40. if stations.puck_count_input(pos) >= 6 and inv:room_for_item('dst', 'stations:fuel_pucks') then
  41. stations.puck_create_fuel(pos)
  42. inv:add_item('dst', 'stations:fuel_pucks')
  43. local cycles_count = (cycles_count - 1)
  44. meta:set_string('cycles_count', cycles_count)
  45. if cycles_count > 0 and stations.puck_count_input(pos) >= 6 then
  46. timer:start(10)
  47. meta:set_string('formspec', stations.puck_formspec(cycles_count))
  48. else
  49. local fuel_stack = fuel:get_count()
  50. if fuel_stack >= 1 then
  51. fuel:take_item(1)
  52. inv:set_stack('fuel', 1, fuel)
  53. timer:start(10)
  54. meta:set_string('cycles_count', 10)
  55. meta:set_string('formspec', stations.puck_formspec(cycles_count))
  56. meta:set_string('infotext', 'Making fuel pucks.')
  57. elseif instack:get_count() < 6 then
  58. meta:set_string('cycles_count', 0)
  59. meta:set_string('formspec', stations.puck_formspec(cycles_count))
  60. meta:set_string('infotext', 'Out of material/fuel.')
  61. else
  62. meta:set_string('formspec', stations.puck_formspec(0))
  63. meta:set_string('cycles_count', 0)
  64. meta:set_string('infotext', 'Fuel puck creator')
  65. end
  66. end
  67. else
  68. meta:set_string('formspec', stations.puck_formspec(0))
  69. meta:set_string('cycles_count', 0)
  70. end
  71. end
  72. function stations.puck_count_input(pos)
  73. local q = 0
  74. local meta = minetest.get_meta(pos)
  75. local inv = meta:get_inventory()
  76. local stacks = inv:get_list('src')
  77. for k in pairs(stacks) do
  78. q = q + inv:get_stack('src', k):get_count()
  79. end
  80. return q
  81. end
  82. function stations.puck_create_fuel(pos)
  83. local q = 6
  84. local meta = minetest.get_meta(pos)
  85. local inv = meta:get_inventory()
  86. local stacks = inv:get_list('src')
  87. for k in pairs(stacks) do
  88. local stack = inv:get_stack('src', k)
  89. if not stack:is_empty() then
  90. local count = stack:get_count()
  91. if count <= q then
  92. inv:set_stack('src', k, '')
  93. q = q - count
  94. else
  95. inv:set_stack('src', k, stack:get_name() .. ' ' .. (count - q))
  96. q = 0
  97. break
  98. end
  99. end
  100. end
  101. end
  102. minetest.register_node('stations:puck_creator', {
  103. description = 'Fuel puck creator',
  104. drawtype = 'mesh',
  105. mesh = 'stations_puck_stove.obj',
  106. tiles = {'stations_pucks.png'},
  107. use_texture_alpha = 'opaque',
  108. sounds = default.node_sound_metal_defaults(),
  109. paramtype2 = 'facedir',
  110. paramtype = 'light',
  111. selection_box = {
  112. type = 'fixed',
  113. fixed = {
  114. {-.5, -.5, -.5, .7, .5, .5},
  115. }
  116. },
  117. collision_box = {
  118. type = 'fixed',
  119. fixed = {
  120. {-.5, -.5, -.5, .5, .5, .5},
  121. {.7, -.5, -.4, 1.1, .0, .2},
  122. }
  123. },
  124. groups = {cracky =3},
  125. after_place_node = function(pos, placer, itemstack)
  126. if not epic.space_to_side(pos) then
  127. minetest.remove_node(pos)
  128. return itemstack
  129. end
  130. end,
  131. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  132. epic.remove_side_node(pos, oldnode)
  133. end,
  134. on_construct = function(pos)
  135. local meta = minetest.get_meta(pos)
  136. local inv = meta:get_inventory()
  137. inv:set_size('main', 8*4)
  138. inv:set_size('src', 3*3)
  139. inv:set_size('dst', 2*2)
  140. inv:set_size('fuel', 1)
  141. meta:set_string('cycles_count', 0)
  142. meta:set_string('infotext', 'Fuel puck creator')
  143. meta:set_string('formspec', 'size[8,8.5]'..
  144. 'list[current_name;src;0.5,0.5;3,3;]'..
  145. 'list[current_name;dst;5.5,1;2,2;]'..
  146. 'list[current_name;fuel;4,2.5;1,1;]'..
  147. 'image[4,1.5;1,1;default_furnace_fire_bg.png]'..
  148. 'list[current_player;main;0,4.5;8,4;]'..
  149. 'listring[context;dst]'..
  150. 'listring[current_player;main]'..
  151. 'listring[context;src]')
  152. end,
  153. can_dig = function(pos)
  154. local meta = minetest.get_meta(pos)
  155. local inv = meta:get_inventory()
  156. if inv:is_empty('src') and inv:is_empty('dst') and inv:is_empty('fuel') then
  157. return true
  158. else
  159. return false
  160. end
  161. end,
  162. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  163. if listname == 'src' and stations.puckable(stack:get_name()) then
  164. return stack:get_count()
  165. elseif listname == 'fuel' and stations.puck_fuel(stack:get_name()) then
  166. return stack:get_count()
  167. else
  168. return 0
  169. end
  170. end,
  171. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  172. local inv = minetest.get_meta(pos):get_inventory()
  173. if from_list == to_list then
  174. return inv:get_stack(from_list, from_index):get_count()
  175. else
  176. return 0
  177. end
  178. end,
  179. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  180. return stack:get_count()
  181. end,
  182. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  183. local timer = minetest.get_node_timer(pos)
  184. local meta = minetest.get_meta(pos)
  185. local inv = meta:get_inventory()
  186. local fuel = inv:get_stack('fuel', 1)
  187. local fuel_stack = fuel:get_count()
  188. local cycles_count = tonumber(meta:get_string('cycles_count'))
  189. if stations.puck_count_input(pos) >= 6 and fuel_stack >= 1 then
  190. if cycles_count == 0 then
  191. fuel:take_item(1)
  192. inv:set_stack('fuel', 1, fuel)
  193. timer:start(10)
  194. meta:set_string('cycles_count', 10)
  195. meta:set_string('formspec', stations.puck_formspec(10))
  196. end
  197. meta:set_string('infotext', 'Making fuel pucks.')
  198. end
  199. end,
  200. on_timer = stations.puck_can_create,
  201. on_rotate = function(pos, node)
  202. return false
  203. end,
  204. })
  205. minetest.register_node('stations:puck_creator_locked', {
  206. description = 'Fuel puck creator (locked)',
  207. drawtype = 'mesh',
  208. mesh = 'stations_puck_stove.obj',
  209. tiles = {'stations_pucks.png'},
  210. use_texture_alpha = 'opaque',
  211. sounds = default.node_sound_metal_defaults(),
  212. paramtype2 = 'facedir',
  213. paramtype = 'light',
  214. selection_box = {
  215. type = 'fixed',
  216. fixed = {
  217. {-.5, -.5, -.5, .7, .5, .5},
  218. }
  219. },
  220. collision_box = {
  221. type = 'fixed',
  222. fixed = {
  223. {-.5, -.5, -.5, .5, .5, .5},
  224. {.7, -.5, -.4, 1.1, .0, .2},
  225. }
  226. },
  227. groups = {cracky =3},
  228. on_construct = function(pos)
  229. local meta = minetest.get_meta(pos)
  230. local inv = meta:get_inventory()
  231. inv:set_size('main', 8*4)
  232. inv:set_size('src', 3*3)
  233. inv:set_size('dst', 2*2)
  234. inv:set_size('fuel', 1)
  235. meta:set_string('cycles_count', 0)
  236. meta:set_string('infotext', 'Fuel Puck Creator (locked)')
  237. meta:set_string('formspec', 'size[8,8.5]'..
  238. 'list[current_name;src;0.5,0.5;3,3;]'..
  239. 'list[current_name;dst;5.5,1;2,2;]'..
  240. 'list[current_name;fuel;4,2.5;1,1;]'..
  241. 'image[4,1.5;1,1;default_furnace_fire_bg.png]'..
  242. 'list[current_player;main;0,4.5;8,4;]'..
  243. 'listring[context;dst]'..
  244. 'listring[current_player;main]'..
  245. 'listring[context;src]')
  246. end,
  247. after_place_node = function(pos, placer, itemstack)
  248. if not epic.space_to_side(pos) then
  249. minetest.remove_node(pos)
  250. return itemstack
  251. else
  252. local meta = minetest.get_meta(pos)
  253. meta:set_string('owner',placer:get_player_name())
  254. end
  255. end,
  256. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  257. epic.remove_side_node(pos, oldnode)
  258. end,
  259. can_dig = function(pos)
  260. local meta = minetest.get_meta(pos)
  261. local inv = meta:get_inventory()
  262. if inv:is_empty('src') and inv:is_empty('dst') and inv:is_empty('fuel') then
  263. return true
  264. else
  265. return false
  266. end
  267. end,
  268. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  269. local meta = minetest.get_meta(pos)
  270. local owner = meta:get_string('owner')
  271. local player_name = player:get_player_name()
  272. if player_name == owner then
  273. if listname == 'src' and stations.puckable(stack:get_name()) then
  274. return stack:get_count()
  275. elseif listname == 'fuel' and stations.puck_fuel(stack:get_name()) then
  276. return stack:get_count()
  277. else
  278. return 0
  279. end
  280. else
  281. return 0
  282. end
  283. end,
  284. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  285. local inv = minetest.get_meta(pos):get_inventory()
  286. if from_list == to_list then
  287. return inv:get_stack(from_list, from_index):get_count()
  288. else
  289. return 0
  290. end
  291. end,
  292. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  293. local meta = minetest.get_meta(pos)
  294. local owner = meta:get_string('owner')
  295. local player_name = player:get_player_name()
  296. if player_name == owner then
  297. return stack:get_count()
  298. else
  299. return 0
  300. end
  301. end,
  302. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  303. local timer = minetest.get_node_timer(pos)
  304. local meta = minetest.get_meta(pos)
  305. local inv = meta:get_inventory()
  306. local fuel = inv:get_stack('fuel', 1)
  307. local fuel_stack = fuel:get_count()
  308. local cycles_count = tonumber(meta:get_string('cycles_count'))
  309. if stations.puck_count_input(pos) >= 6 and fuel_stack >= 1 then
  310. if cycles_count == 0 then
  311. fuel:take_item(1)
  312. inv:set_stack('fuel', 1, fuel)
  313. timer:start(10)
  314. meta:set_string('cycles_count', 10)
  315. meta:set_string('formspec', stations.puck_formspec(10))
  316. end
  317. meta:set_string('infotext', 'Making fuel pucks.')
  318. end
  319. end,
  320. on_timer = stations.puck_can_create,
  321. on_rotate = function(pos, node)
  322. return false
  323. end,
  324. })