harvester.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. -- The harvester
  2. local S = farmingNG.S
  3. -- farming
  4. farmingNG.harvester_names["farming:wheat_8"] = true
  5. farmingNG.harvester_names["farming:wheat_7"] = farmingNG.harvester_nofullg
  6. farmingNG.harvester_names["farming:wheat_6"] = farmingNG.harvester_nofullg
  7. farmingNG.harvester_names["farming:cotton_8"] = true
  8. farmingNG.harvester_names["farming:cotton_7"] = farmingNG.harvester_nofullg
  9. farmingNG.harvester_names["farming:cotton_6"] = farmingNG.harvester_nofullg
  10. farmingNG.harvester_names["farming:weed"] = false
  11. farmingNG.harvester_names["farming:pumpkin"] = true
  12. farmingNG.harvester_names["farming:barley_7"] = true
  13. farmingNG.harvester_names["farming:barley_6"] = farmingNG.harvester_nofullg
  14. farmingNG.harvester_names["farming:barley_5"] = farmingNG.harvester_nofullg
  15. farmingNG.harvester_names["farming:blueberry_4"] = true
  16. farmingNG.harvester_names["farming:carrot_8"] = true
  17. farmingNG.harvester_names["farming:chili_8"] = true
  18. farmingNG.harvester_names["farming:coffee_5"] = true
  19. farmingNG.harvester_names["farming:corn_8"] = true
  20. farmingNG.harvester_names["farming:cucumber_4"] = true
  21. farmingNG.harvester_names["farming:garlic_5"] = true
  22. farmingNG.harvester_names["farming:hemp_8"] = true
  23. farmingNG.harvester_names["farming:melon_8"] = true
  24. farmingNG.harvester_names["farming:onion_5"] = true
  25. farmingNG.harvester_names["farming:pepper_5"] = true
  26. farmingNG.harvester_names["farming:pineapple_8"] = true
  27. farmingNG.harvester_names["farming:potato_4"] = true
  28. farmingNG.harvester_names["farming:pumpkin_8"] = true
  29. farmingNG.harvester_names["farming:raspberry_4"] = true
  30. farmingNG.harvester_names["farming:rhubarb_3"] = true
  31. farmingNG.harvester_names["farming:tomato_8"] = true
  32. farmingNG.harvester_names["farming:grapes_8"] = true
  33. farmingNG.harvester_names["farming:beanpole_5"] = true
  34. farmingNG.harvester_names["farming:pea_5"] = true
  35. farmingNG.harvester_names["farming:beetroot_5"] = true
  36. farmingNG.harvester_names["farming:rice_8"] = true
  37. farmingNG.harvester_names["farming:rice_7"] = farmingNG.harvester_nofullg
  38. farmingNG.harvester_names["farming:rice_6"] = farmingNG.harvester_nofullg
  39. farmingNG.harvester_names["farming:oat_8"] = true
  40. farmingNG.harvester_names["farming:oat_7"] = farmingNG.harvester_nofullg
  41. farmingNG.harvester_names["farming:oat_6"] = farmingNG.harvester_nofullg
  42. farmingNG.harvester_names["farming:rye_8"] = true
  43. farmingNG.harvester_names["farming:rye_7"] = farmingNG.harvester_nofullg
  44. farmingNG.harvester_names["farming:rye_6"] = farmingNG.harvester_nofullg
  45. farmingNG.harvester_names["farming:cabbage_6"] = true
  46. farmingNG.harvester_names["farming:mint_4"] = true
  47. -- beer_test
  48. farmingNG.harvester_names["beer_test:oats_8"] = true
  49. -- farming_plus
  50. farmingNG.harvester_names["farming_plus:melon"] = true
  51. farmingNG.harvester_names["farming_plus:tomato"] = true
  52. farmingNG.harvester_names["farming_plus:chilli"] = true
  53. farmingNG.harvester_names["farming_plus:garlic"] = true
  54. farmingNG.harvester_names["farming_plus:soy_plant"] = true
  55. farmingNG.harvester_names["farming_plus:cucumber"] = true
  56. farmingNG.harvester_names["farming_plus:coffee"] = true
  57. farmingNG.harvester_names["farming_plus:potato"] = true
  58. farmingNG.harvester_names["farming_plus:carrot"] = true
  59. farmingNG.harvester_names["farming_plus:garlic"] = true
  60. farmingNG.harvester_names["farming_plus:rhubarb"]= true
  61. farmingNG.harvester_names["farming_plus:barley_7"]= true
  62. farmingNG.harvester_names["farming_plus:hemp_8"]= true
  63. farmingNG.harvester_names["farming_plus:beanpole_5"]= true
  64. farmingNG.harvester_names["farming_plus:grapes_8"]= true
  65. farmingNG.harvester_names["farming_plus:corn"] = true
  66. farmingNG.harvester_names["farming_plus:cornb"] = true
  67. farmingNG.harvester_names["farming_plus:cornc"] = true
  68. if not farmingNG.havetech then
  69. farmingNG.harvester_charge_per_node = math.floor(65535 / farmingNG.harvester_max_charge * farmingNG.harvester_charge_per_node)
  70. farmingNG.harvester_max_charge = 65535
  71. end
  72. -- Table for saving what was sawed down
  73. local produced = {}
  74. -- Save the items sawed down so that we can drop them in a nice single stack
  75. local function handle_drops(drops)
  76. for _, item in ipairs(drops) do
  77. local stack = ItemStack(item)
  78. local name = stack:get_name()
  79. local p = produced[name]
  80. if not p then
  81. produced[name] = stack
  82. else
  83. p:set_count(p:get_count() + stack:get_count())
  84. end
  85. end
  86. end
  87. -- This function does all the hard work. Recursively we dig the node at hand
  88. -- if it is in the table and then search the surroundings for more stuff to dig.
  89. local function recursive_harvest(pos, remaining_charge)
  90. if remaining_charge < farmingNG.harvester_charge_per_node then
  91. return remaining_charge
  92. end
  93. local node = minetest.get_node(pos)
  94. if not farmingNG.harvester_names[node.name] then
  95. return remaining_charge
  96. end
  97. handle_drops(minetest.get_node_drops(node.name, ""))
  98. minetest.remove_node(pos)
  99. remaining_charge = remaining_charge - farmingNG.harvester_charge_per_node
  100. if remaining_charge < 1 then remaining_charge = 1 end
  101. -- Check surroundings and run recursively if any charge left
  102. for npos in farmingNG.iterSawTries(pos) do
  103. if remaining_charge < farmingNG.harvester_charge_per_node then
  104. break
  105. end
  106. if farmingNG.harvester_names[minetest.get_node(npos).name] then
  107. remaining_charge = recursive_harvest(npos, remaining_charge)
  108. end
  109. end
  110. return remaining_charge
  111. end
  112. -- Function to randomize positions for new node drops
  113. local function get_drop_pos(pos)
  114. local drop_pos = {}
  115. for i = 0, 8 do
  116. -- Randomize position for a new drop
  117. drop_pos.x = pos.x + math.random(-3, 3)
  118. drop_pos.y = pos.y - 1
  119. drop_pos.z = pos.z + math.random(-3, 3)
  120. -- Move the randomized position upwards until
  121. -- the node is air or unloaded.
  122. for y = drop_pos.y, drop_pos.y + 5 do
  123. drop_pos.y = y
  124. local node = minetest.get_node_or_nil(drop_pos)
  125. if not node then
  126. -- If the node is not loaded yet simply drop
  127. -- the item at the original digging position.
  128. return pos
  129. elseif node.name == "air" then
  130. -- Add variation to the entity drop position,
  131. -- but don't let drops get too close to the edge
  132. drop_pos.x = drop_pos.x + (math.random() * 0.8) - 0.5
  133. drop_pos.z = drop_pos.z + (math.random() * 0.8) - 0.5
  134. return drop_pos
  135. end
  136. end
  137. end
  138. -- Return the original position if this takes too long
  139. return pos
  140. end
  141. -- entry point
  142. local function harvester_dig(pos, current_charge)
  143. -- Start sawing things down
  144. local remaining_charge = recursive_harvest(pos, current_charge)
  145. minetest.sound_play("farming_nextgen_seeder", {pos = pos, gain = 0.1,
  146. max_hear_distance = 10})
  147. -- Now drop items for the player
  148. for name, stack in pairs(produced) do
  149. -- Drop stacks of stack max or less
  150. local count, max = stack:get_count(), stack:get_stack_max()
  151. stack:set_count(max)
  152. while count > max do
  153. minetest.add_item(get_drop_pos(pos), stack)
  154. count = count - max
  155. end
  156. stack:set_count(count)
  157. minetest.add_item(get_drop_pos(pos), stack)
  158. end
  159. -- Clean up
  160. produced = {}
  161. return remaining_charge
  162. end
  163. if farmingNG.havetech then
  164. technic.register_power_tool("farming_nextgen:harvester", farmingNG.harvester_max_charge)
  165. minetest.register_tool("farming_nextgen:harvester", {
  166. description = S("Harvester"),
  167. inventory_image = "farming_nextgen_harvester.png",
  168. stack_max = 1,
  169. wear_represents = "technic_RE_charge",
  170. on_refill = technic.refill_RE_charge,
  171. on_use = function(itemstack, user, pointed_thing)
  172. local name = user:get_player_name()
  173. if pointed_thing.type ~= "node" then
  174. return itemstack
  175. end
  176. local meta = minetest.deserialize(itemstack:get_metadata())
  177. if not meta or not meta.charge or
  178. meta.charge < farmingNG.harvester_charge_per_node then
  179. return
  180. end
  181. if minetest.is_protected(pointed_thing.under, name) then
  182. minetest.record_protection_violation(pointed_thing.under, name)
  183. return
  184. end
  185. -- Send current charge to digging function so that the
  186. -- harvester will stop after digging a number of nodes
  187. meta.charge = harvester_dig(pointed_thing.under, meta.charge)
  188. if not technic.creative_mode then
  189. technic.set_RE_wear(itemstack, meta.charge, farmingNG.harvester_max_charge)
  190. itemstack:set_metadata(minetest.serialize(meta))
  191. end
  192. return itemstack
  193. end,
  194. })
  195. local mesecons_button = minetest.get_modpath("mesecons_button")
  196. local trigger = mesecons_button and "mesecons_button:button_off" or "default:mese_crystal_fragment"
  197. minetest.register_craft({
  198. output = "farming_nextgen:harvester",
  199. recipe = {
  200. {"technic:battery", trigger, "technic:battery"},
  201. {"technic:diamond_drill_head", "technic:machine_casing", "technic:diamond_drill_head"},
  202. {"technic:rubber", "", "technic:rubber"},
  203. }
  204. })
  205. else
  206. minetest.register_tool("farming_nextgen:harvester", {
  207. description = S("Harvester"),
  208. groups = {soil=3,soil=2},
  209. inventory_image = "farming_nextgen_harvester.png",
  210. stack_max=1,
  211. liquids_pointable = false,
  212. on_use = function(itemstack, user, pointed_thing)
  213. local seednum=0
  214. local name = user:get_player_name()
  215. local privs = minetest.get_player_privs(name)
  216. if pointed_thing.type ~= "node" then
  217. return itemstack
  218. end
  219. local charge = 65535 - itemstack:get_wear()
  220. if not charge or
  221. charge < farmingNG.harvester_charge_per_node then
  222. minetest.chat_send_player(name,S(" *** Your device needs to be serviced"))
  223. return
  224. end
  225. if minetest.is_protected(pointed_thing.under, name) then
  226. minetest.record_protection_violation(pointed_thing.under, name)
  227. return
  228. end
  229. charge = harvester_dig(pointed_thing.under, charge)
  230. itemstack:set_wear(65535-charge)
  231. return itemstack
  232. end,
  233. })
  234. minetest.register_craft({
  235. output = "farming_nextgen:harvester",
  236. recipe = {
  237. {"default:diamondblock", "default:mese_crystal_fragment", "default:diamondblock" },
  238. {"default:gold_ingot", "default:bronze_ingot", "default:gold_ingot"},
  239. {"default:mese_crystal_fragment", "", "default:mese_crystal_fragment"},
  240. }
  241. })
  242. end