cocoa.lua 6.0 KB

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