cocoa.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. local S = farming.translate
  2. local a = farming.recipe_items
  3. -- place cocoa
  4. local function place_cocoa(itemstack, placer, pointed_thing, plantname)
  5. local pt = pointed_thing
  6. -- check if pointing at a node
  7. if not pt or pt.type ~= "node" then
  8. return
  9. end
  10. local under = minetest.get_node(pt.under)
  11. -- return if any of the nodes are not registered
  12. if not minetest.registered_nodes[under.name] then
  13. return
  14. end
  15. -- am I right-clicking on something that has a custom on_place set?
  16. -- thanks to Krock for helping with this issue :)
  17. local def = minetest.registered_nodes[under.name]
  18. if placer and itemstack and def and def.on_rightclick then
  19. return def.on_rightclick(pt.under, under, placer, itemstack, pt)
  20. end
  21. -- check if pointing at jungletree
  22. if (under.name ~= "default:jungletree" and under.name ~= "mcl_core:jungletree")
  23. or minetest.get_node(pt.above).name ~= "air" then
  24. return
  25. end
  26. -- is player planting crop?
  27. local name = placer and placer:get_player_name() or ""
  28. -- check for protection
  29. if minetest.is_protected(pt.above, name) then
  30. return
  31. end
  32. -- add the node and remove 1 item from the itemstack
  33. minetest.set_node(pt.above, {name = plantname})
  34. minetest.sound_play("default_place_node", {pos = pt.above, gain = 1.0})
  35. if placer and not farming.is_creative(placer:get_player_name()) then
  36. itemstack:take_item()
  37. -- check for refill
  38. if itemstack:get_count() == 0 then
  39. minetest.after(0.20,
  40. farming.refill_plant,
  41. placer,
  42. "farming:cocoa_beans_raw",
  43. placer:get_wield_index()
  44. )
  45. end
  46. end
  47. return itemstack
  48. end
  49. -- cocoa beans
  50. minetest.register_craftitem("farming:cocoa_beans_raw", {
  51. description = S("Raw Cocoa Beans"),
  52. inventory_image = "farming_cocoa_beans.png^[brighten",
  53. groups = {compostability = 48, seed = 1, flammable = 2},
  54. on_place = function(itemstack, placer, pointed_thing)
  55. return place_cocoa(itemstack, placer, pointed_thing, "farming:cocoa_1")
  56. end
  57. })
  58. minetest.register_craftitem("farming:cocoa_beans", {
  59. description = S("Cocoa Beans"),
  60. inventory_image = "farming_cocoa_beans.png",
  61. groups = {compostability = 65, food_cocoa = 1, flammable = 2}
  62. })
  63. minetest.register_craft({
  64. type = "cooking",
  65. cooktime = 5,
  66. output = "farming:cocoa_beans",
  67. recipe = "farming:cocoa_beans_raw"
  68. })
  69. minetest.register_craft( {
  70. output = a.dye_brown,
  71. recipe = {{"farming:cocoa_beans"}}
  72. })
  73. -- chocolate cookie
  74. minetest.register_craftitem("farming:cookie", {
  75. description = S("Cookie"),
  76. inventory_image = "farming_cookie.png",
  77. on_use = minetest.item_eat(2)
  78. })
  79. minetest.register_craft( {
  80. output = "farming:cookie 8",
  81. recipe = {
  82. {"group:food_wheat", "group:food_cocoa", "group:food_wheat" }
  83. }
  84. })
  85. -- bar of dark chocolate (thanks to Ice Pandora for her deviantart.com chocolate tutorial)
  86. minetest.register_craftitem("farming:chocolate_dark", {
  87. description = S("Bar of Dark Chocolate"),
  88. inventory_image = "farming_chocolate_dark.png",
  89. on_use = minetest.item_eat(3)
  90. })
  91. minetest.register_craft( {
  92. output = "farming:chocolate_dark",
  93. recipe = {
  94. {"group:food_cocoa", "group:food_cocoa", "group:food_cocoa"}
  95. }
  96. })
  97. -- chocolate block
  98. minetest.register_node("farming:chocolate_block", {
  99. description = S("Chocolate Block"),
  100. tiles = {"farming_chocolate_block.png"},
  101. is_ground_content = false,
  102. groups = {cracky = 2, oddly_breakable_by_hand = 2},
  103. sounds = farming.sounds.node_sound_stone_defaults()
  104. })
  105. minetest.register_craft({
  106. output = "farming:chocolate_block",
  107. recipe = {
  108. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"},
  109. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"},
  110. {"farming:chocolate_dark", "farming:chocolate_dark", "farming:chocolate_dark"}
  111. }
  112. })
  113. minetest.register_craft({
  114. output = "farming:chocolate_dark 9",
  115. recipe = {{"farming:chocolate_block"}}
  116. })
  117. -- cocoa definition
  118. local def = {
  119. drawtype = "plantlike",
  120. tiles = {"farming_cocoa_1.png"},
  121. paramtype = "light",
  122. walkable = false,
  123. selection_box = {
  124. type = "fixed",
  125. fixed = {-0.3, -0.5, -0.3, 0.3, 0.5, 0.3}
  126. },
  127. drop = {},
  128. groups = {
  129. handy = 1, snappy = 3, flammable = 2, plant = 1, growing = 1,
  130. not_in_creative_inventory = 1, leafdecay = 1, leafdecay_drop = 1
  131. },
  132. sounds = farming.sounds.node_sound_leaves_defaults(),
  133. growth_check = function(pos, node_name)
  134. if minetest.find_node_near(pos, 1,
  135. {"default:jungletree", "mcl_core:jungletree"}) then
  136. return true -- place next growth stage
  137. end
  138. return false -- condition not met, skip growth stage until next check
  139. end
  140. }
  141. -- stage 1
  142. minetest.register_node("farming:cocoa_1", table.copy(def))
  143. -- stage 2
  144. def.tiles = {"farming_cocoa_2.png"}
  145. minetest.register_node("farming:cocoa_2", table.copy(def))
  146. -- stage3
  147. def.tiles = {"farming_cocoa_3.png"}
  148. def.drop = {
  149. items = {
  150. {items = {"farming:cocoa_beans_raw 1"}, rarity = 1}
  151. }
  152. }
  153. minetest.register_node("farming:cocoa_3", table.copy(def))
  154. -- stage 4 (final)
  155. def.tiles = {"farming_cocoa_4.png"}
  156. def.groups.growing = nil
  157. def.growth_check = nil
  158. def.drop = {
  159. items = {
  160. {items = {"farming:cocoa_beans_raw 2"}, rarity = 1},
  161. {items = {"farming:cocoa_beans_raw 1"}, rarity = 2},
  162. {items = {"farming:cocoa_beans_raw 1"}, rarity = 4}
  163. }
  164. }
  165. minetest.register_node("farming:cocoa_4", table.copy(def))
  166. -- add to registered_plants
  167. farming.registered_plants["farming:cocoa_beans"] = {
  168. trellis = "default:jungletree",
  169. crop = "farming:cocoa",
  170. seed = "farming:cocoa_beans_raw",
  171. minlight = farming.min_light,
  172. maxlight = farming.max_light,
  173. steps = 4
  174. }
  175. -- localize math.random for speed
  176. local random = math.random
  177. -- add random cocoa pods to jungle tree's
  178. minetest.register_on_generated(function(minp, maxp)
  179. if maxp.y < 0 then
  180. return
  181. end
  182. local pos, dir
  183. local cocoa = minetest.find_nodes_in_area(minp, maxp,
  184. {"default:jungletree", "mcl_core:jungletree"})
  185. for n = 1, #cocoa do
  186. pos = cocoa[n]
  187. if minetest.find_node_near(pos, 1,
  188. {"default:jungleleaves", "moretrees:jungletree_leaves_green",
  189. "mcl_core:jungleleaves"}) then
  190. dir = random(80)
  191. if dir == 1 then pos.x = pos.x + 1
  192. elseif dir == 2 then pos.x = pos.x - 1
  193. elseif dir == 3 then pos.z = pos.z + 1
  194. elseif dir == 4 then pos.z = pos.z -1
  195. end
  196. if dir < 5
  197. and minetest.get_node(pos).name == "air"
  198. and minetest.get_node_light(pos) > 12 then
  199. --print ("Cocoa Pod added at " .. minetest.pos_to_string(pos))
  200. minetest.swap_node(pos, {
  201. name = "farming:cocoa_" .. tostring(random(4))
  202. })
  203. end
  204. end
  205. end
  206. end)