station_threshing.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. stations.grains = {}
  2. stations.grains['farming:barley'] = true
  3. stations.grains['farming:corn'] = true
  4. stations.grains['farming:oat'] = true
  5. stations.grains['farming:rice'] = true
  6. stations.grains['farming:rye'] = true
  7. stations.grains['farming:wheat'] = true
  8. local function threshing_formspec(pos)
  9. local meta = minetest.get_meta(pos)
  10. local speed = meta:get_string('speed')
  11. local formspec =
  12. 'size[8,8.5]'..
  13. 'list[current_name;input;.5,.5;1,1;]'..
  14. 'list[current_name;output;6,1;2,3;]'..
  15. 'list[current_name;straw;2.5,3;3,1]'..
  16. 'list[current_player;main;0,4.5;8,4;]'..
  17. 'label[2,.25;Break grain from the stems, your choice, slow or fast.]'..
  18. 'label[2,.75;Currently running at '..speed..' speed.]'..
  19. 'button[0,2.5;2,1;slow;Slow]'..
  20. 'button[0,3.25;2,1;fast;Fast]'..
  21. 'listring[context;output]'..
  22. 'listring[current_player;main]'..
  23. 'listring[context;input]'
  24. return formspec
  25. end
  26. minetest.register_node('stations:threshing', {
  27. description = 'Threshing Machine',
  28. drawtype = 'mesh',
  29. mesh = 'stations_threshing.obj',
  30. tiles = {'stations_threshing.png'},
  31. use_texture_alpha = 'opaque',
  32. sounds = default.node_sound_wood_defaults(),
  33. paramtype2 = 'facedir',
  34. paramtype = 'light',
  35. selection_box = {
  36. type = 'fixed',
  37. fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
  38. collision_box = {
  39. type = 'fixed',
  40. fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
  41. groups = {oddly_breakable_by_hand=3, choppy=3},
  42. on_construct = function(pos)
  43. local meta = minetest.get_meta(pos)
  44. local inv = meta:get_inventory()
  45. inv:set_size('input', 1)
  46. inv:set_size('output', 6)
  47. inv:set_size('straw', 3)
  48. meta:set_string('speed', 'slow')
  49. meta:set_string('infotext', 'Threshing Machine')
  50. meta:set_string('formspec', threshing_formspec(pos))
  51. end,
  52. after_place_node = function(pos, placer, itemstack)
  53. if not epic.space_to_side(pos) then
  54. minetest.remove_node(pos)
  55. return itemstack
  56. end
  57. end,
  58. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  59. epic.remove_side_node(pos, oldnode)
  60. end,
  61. can_dig = function(pos,player)
  62. local meta = minetest.get_meta(pos);
  63. local inv = meta:get_inventory()
  64. if inv:is_empty('input') and inv:is_empty('output') then
  65. return true
  66. else
  67. return false
  68. end
  69. end,
  70. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  71. local input = stack:get_name()
  72. if listname == 'input' then
  73. if stations.grains[input] then
  74. return 99
  75. else
  76. return 0
  77. end
  78. return 0
  79. else
  80. return 0
  81. end
  82. end,
  83. on_receive_fields = function(pos, formname, fields, sender)
  84. local meta = minetest.get_meta(pos)
  85. local timer = minetest.get_node_timer(pos)
  86. if fields ['slow'] then
  87. meta:set_string('speed', 'slow')
  88. meta:set_string('formspec', threshing_formspec(pos))
  89. timer:start(10)
  90. elseif fields ['fast'] then
  91. meta:set_string('speed', 'fast')
  92. meta:set_string('formspec', threshing_formspec(pos))
  93. timer:start(3)
  94. end
  95. end,
  96. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  97. local timer = minetest.get_node_timer(pos)
  98. local meta = minetest.get_meta(pos)
  99. local speed = meta:get_string('speed')
  100. if speed == 'slow' then
  101. timer:start(10)
  102. elseif speed == 'fast' then
  103. timer:start(3)
  104. end
  105. end,
  106. on_timer = function(pos)
  107. local timer = minetest.get_node_timer(pos)
  108. local meta = minetest.get_meta(pos)
  109. local inv = meta:get_inventory()
  110. local input = inv:get_stack('input', 1)
  111. local input_count = input:get_count()
  112. if input_count >= 1 then
  113. local output = inv:get_stack('output', 1)
  114. local grain_name = input:get_name()
  115. local grain = string.sub(grain_name, 9,-1)
  116. local speed = meta:get_string('speed')
  117. if speed == 'slow' then
  118. local num = math.random(3, 7)
  119. inv:add_item('output', 'farming:seed_'..grain..' '..num)
  120. inv:add_item('straw', 'farming:hay')
  121. input:take_item(1)
  122. inv:set_stack('input',1,input)
  123. timer:start(10)
  124. elseif speed == 'fast' then
  125. local num = math.random(1, 3)
  126. inv:add_item('output', 'farming:seed_'..grain..' '..num)
  127. inv:add_item('straw', 'farming:hay')
  128. input:take_item(1)
  129. inv:set_stack('input',1,input)
  130. timer:start(3)
  131. end
  132. end
  133. end,
  134. on_rotate = function(pos, node)
  135. return false
  136. end,
  137. })
  138. minetest.register_node('stations:threshing_locked', {
  139. description = 'Threshing Machine (locked)',
  140. drawtype = 'mesh',
  141. mesh = 'stations_threshing.obj',
  142. tiles = {'stations_threshing.png'},
  143. use_texture_alpha = 'opaque',
  144. sounds = default.node_sound_wood_defaults(),
  145. paramtype2 = 'facedir',
  146. paramtype = 'light',
  147. selection_box = {
  148. type = 'fixed',
  149. fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
  150. collision_box = {
  151. type = 'fixed',
  152. fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
  153. groups = {oddly_breakable_by_hand=3, choppy=3},
  154. on_construct = function(pos)
  155. local meta = minetest.get_meta(pos)
  156. local inv = meta:get_inventory()
  157. inv:set_size('input', 1)
  158. inv:set_size('output', 6)
  159. inv:set_size('straw', 3)
  160. meta:set_string('speed', 'slow')
  161. meta:set_string('infotext', 'Threshing Machine (locked)')
  162. meta:set_string('formspec', threshing_formspec(pos))
  163. end,
  164. after_place_node = function(pos, placer, itemstack)
  165. if not epic.space_to_side(pos) then
  166. minetest.remove_node(pos)
  167. return itemstack
  168. end
  169. end,
  170. after_dig_node = function(pos, oldnode, oldmetadata, digger)
  171. epic.remove_side_node(pos, oldnode)
  172. end,
  173. can_dig = function(pos,player)
  174. local meta = minetest.get_meta(pos);
  175. local inv = meta:get_inventory()
  176. if inv:is_empty('input') and inv:is_empty('output') then
  177. return true
  178. else
  179. return false
  180. end
  181. end,
  182. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  183. local player_name = player:get_player_name()
  184. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  185. return 0
  186. else
  187. local input = stack:get_name()
  188. if listname == 'input' then
  189. if stations.grains[input] then
  190. return 99
  191. else
  192. return 0
  193. end
  194. return 0
  195. else
  196. return 0
  197. end
  198. end
  199. end,
  200. on_receive_fields = function(pos, formname, fields, sender)
  201. local meta = minetest.get_meta(pos)
  202. local timer = minetest.get_node_timer(pos)
  203. if fields ['slow'] then
  204. meta:set_string('speed', 'slow')
  205. meta:set_string('formspec', threshing_formspec(pos))
  206. timer:start(10)
  207. elseif fields ['fast'] then
  208. meta:set_string('speed', 'fast')
  209. meta:set_string('formspec', threshing_formspec(pos))
  210. timer:start(3)
  211. end
  212. end,
  213. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  214. local timer = minetest.get_node_timer(pos)
  215. local meta = minetest.get_meta(pos)
  216. local speed = meta:get_string('speed')
  217. if speed == 'slow' then
  218. timer:start(10)
  219. elseif speed == 'fast' then
  220. timer:start(3)
  221. end
  222. end,
  223. on_timer = function(pos)
  224. local timer = minetest.get_node_timer(pos)
  225. local meta = minetest.get_meta(pos)
  226. local inv = meta:get_inventory()
  227. local input = inv:get_stack('input', 1)
  228. local input_count = input:get_count()
  229. if input_count >= 1 then
  230. local output = inv:get_stack('output', 1)
  231. local grain_name = input:get_name()
  232. local grain = string.sub(grain_name, 9,-1)
  233. local speed = meta:get_string('speed')
  234. if speed == 'slow' then
  235. local num = math.random(3, 7)
  236. inv:add_item('output', 'farming:seed_'..grain..' '..num)
  237. inv:add_item('straw', 'farming:hay')
  238. input:take_item(1)
  239. inv:set_stack('input',1,input)
  240. timer:start(10)
  241. elseif speed == 'fast' then
  242. local num = math.random(1, 3)
  243. inv:add_item('output', 'farming:seed_'..grain..' '..num)
  244. inv:add_item('straw', 'farming:hay')
  245. input:take_item(1)
  246. inv:set_stack('input',1,input)
  247. timer:start(3)
  248. end
  249. end
  250. end,
  251. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  252. local player_name = player:get_player_name()
  253. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
  254. return 0
  255. else
  256. return 99
  257. end
  258. end,
  259. on_rotate = function(pos, node)
  260. return false
  261. end,
  262. })