corn.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. --[[
  2. Copyright (C) 2015 - Auke Kok <sofar@foo-projects.org>
  3. "crops" is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1
  6. of the license, or (at your option) any later version.
  7. --]]
  8. -- Intllib
  9. local S = crops.intllib
  10. minetest.register_node("crops:corn", {
  11. description = S("Corn"),
  12. inventory_image = "crops_corn.png",
  13. wield_image = "crops_corn.png",
  14. tiles = { "crops_corn_base_seed.png" },
  15. drawtype = "plantlike",
  16. paramtype2 = "meshoptions",
  17. waving = 1,
  18. sunlight_propagates = true,
  19. use_texture_alpha = true,
  20. walkable = true,
  21. paramtype = "light",
  22. node_placement_prediction = "crops:corn_base_seed",
  23. groups = { snappy=3,flammable=3,flora=1,attached_node=1 },
  24. drop = {},
  25. sounds = default.node_sound_leaves_defaults(),
  26. on_place = function(itemstack, placer, pointed_thing)
  27. local under = minetest.get_node(pointed_thing.under)
  28. if minetest.get_item_group(under.name, "soil") <= 1 then
  29. return
  30. end
  31. crops.plant(pointed_thing.above, {name="crops:corn_base_seed", param2 = 3})
  32. if not minetest.settings:get_bool("creative_mode") then
  33. itemstack:take_item()
  34. end
  35. return itemstack
  36. end
  37. })
  38. minetest.register_craftitem("crops:corn_cob", {
  39. description = S("Corn Cob"),
  40. inventory_image = "crops_corn_cob.png",
  41. })
  42. minetest.register_craft({
  43. type = "shapeless",
  44. output = "crops:corn",
  45. recipe = { "crops:corn_cob" }
  46. })
  47. minetest.register_craftitem("crops:corn_on_the_cob", {
  48. description = S("Corn on the Cob"),
  49. inventory_image = "crops_corn_on_the_cob.png",
  50. on_use = minetest.item_eat(1)
  51. })
  52. minetest.register_craft({
  53. type = "cooking",
  54. output = "crops:corn_on_the_cob",
  55. recipe = "crops:corn_cob"
  56. })
  57. minetest.register_node("crops:corn_base_seed", {
  58. visual = "mesh",
  59. description = S("Corn plant"),
  60. drawtype = "plantlike",
  61. paramtype2 = "meshoptions",
  62. waving = 1,
  63. tiles = { "crops_corn_base_seed.png" },
  64. use_texture_alpha = true,
  65. walkable = false,
  66. sunlight_propagates = true,
  67. paramtype = "light",
  68. groups = { snappy=3,flammable=3,flora=1,attached_node=1,not_in_creative_inventory=1 },
  69. drop = {},
  70. sounds = default.node_sound_leaves_defaults(),
  71. selection_box = {
  72. type = "fixed",
  73. fixed = {-0.5, -0.5, -0.5, 0.5, -0.3, 0.5}
  74. }
  75. })
  76. minetest.register_abm({
  77. nodenames = { "crops:corn_base_seed" },
  78. neighbors = { "group:soil" },
  79. interval = crops.settings.interval,
  80. chance = crops.settings.chance,
  81. action = function(pos, node, active_object_count, active_object_count_wider)
  82. if not crops.can_grow(pos) then
  83. return
  84. end
  85. minetest.swap_node(pos, { name = "crops:corn_base_1", param2 = 3 })
  86. end
  87. })
  88. minetest.register_node("crops:corn_base_1", {
  89. visual = "mesh",
  90. description = S("Corn plant"),
  91. drawtype = "plantlike",
  92. paramtype2 = "meshoptions",
  93. tiles = { "crops_corn_base_1.png" },
  94. waving = 1,
  95. use_texture_alpha = true,
  96. walkable = false,
  97. sunlight_propagates = true,
  98. paramtype = "light",
  99. groups = { snappy=3,flammable=3,flora=1,attached_node=1,not_in_creative_inventory=1 },
  100. drop = {},
  101. sounds = default.node_sound_leaves_defaults(),
  102. })
  103. minetest.register_abm({
  104. nodenames = { "crops:corn_base_1" },
  105. neighbors = { "group:soil" },
  106. interval = crops.settings.interval,
  107. chance = crops.settings.chance,
  108. action = function(pos, node, active_object_count, active_object_count_wider)
  109. if not crops.can_grow(pos) then
  110. return
  111. end
  112. if not minetest.get_node({x = pos.x, y = pos.y + 1, z = pos.z}).name == "air" then
  113. return
  114. end
  115. minetest.swap_node(pos, { name = "crops:corn_base_2", param2 = 3 })
  116. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  117. minetest.set_node(above , { name = "crops:corn_top_1", param2 = 3 })
  118. local meta = minetest.get_meta(above)
  119. meta:set_int("crops_top_half", 1)
  120. end
  121. })
  122. minetest.register_node("crops:corn_base_2", {
  123. description = S("Corn plant"),
  124. drawtype = "plantlike",
  125. paramtype2 = "meshoptions",
  126. tiles = { "crops_corn_base_2.png" },
  127. use_texture_alpha = true,
  128. walkable = false,
  129. sunlight_propagates = true,
  130. paramtype = "light",
  131. groups = { snappy=3,flammable=3,flora=1,attached_node=1,not_in_creative_inventory=1 },
  132. drop = {},
  133. sounds = default.node_sound_leaves_defaults(),
  134. on_dig = function(pos, node, digger)
  135. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  136. if minetest.get_node(above).name == "crops:corn_top_1" or minetest.get_node(above).name == "crops:corn_top_2" then
  137. minetest.remove_node(above)
  138. minetest.remove_node(pos)
  139. return
  140. end
  141. if not minetest.get_node(above).name == "crops:corn_top_3" then
  142. minetest.remove_node(pos)
  143. end
  144. local meta = minetest.get_meta(pos)
  145. local damage = meta:get_int("crops_damage")
  146. local drops = {}
  147. -- 0 - 2-4
  148. -- 50 - 2-3
  149. -- 100 - 1-1
  150. for i = 1,math.random(2 - (damage / 100), 4 - (3 * (damage / 100))) do
  151. table.insert(drops, ('crops:corn_cob'))
  152. end
  153. minetest.set_node(pos, { name = "crops:corn_base_3", param2 = 3 })
  154. minetest.set_node(above, { name = "crops:corn_top_4", param2 = 3 })
  155. core.handle_node_drops(above, drops, digger)
  156. end
  157. })
  158. minetest.register_node("crops:corn_base_3", {
  159. description = S("Corn plant"),
  160. drawtype = "plantlike",
  161. paramtype2 = "meshoptions",
  162. tiles = { "crops_corn_base_3.png" },
  163. use_texture_alpha = true,
  164. walkable = false,
  165. sunlight_propagates = true,
  166. paramtype = "light",
  167. groups = { snappy=3,flammable=3,flora=1,attached_node=1,not_in_creative_inventory=1 },
  168. drop = {},
  169. sounds = default.node_sound_leaves_defaults(),
  170. on_dig = function(pos, node, digger)
  171. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  172. if minetest.get_node(above).name == "crops:corn_top_4" then
  173. minetest.remove_node(above)
  174. end
  175. minetest.remove_node(pos)
  176. end
  177. })
  178. minetest.register_node("crops:corn_top_1", {
  179. description = S("Corn plant"),
  180. drawtype = "plantlike",
  181. paramtype2 = "meshoptions",
  182. tiles = { "crops_corn_base_1.png" },
  183. waving = 1,
  184. use_texture_alpha = true,
  185. walkable = false,
  186. sunlight_propagates = true,
  187. paramtype = "light",
  188. groups = { snappy=3,flammable=3,flora=1,not_in_creative_inventory=1 },
  189. drop = {},
  190. sounds = default.node_sound_leaves_defaults(),
  191. on_dig = function(pos, node, digger)
  192. local below = {x = pos.x, y = pos.y - 1, z = pos.z}
  193. if not minetest.get_node(below).name == "crops:base_2" then
  194. return
  195. end
  196. minetest.remove_node(below)
  197. minetest.remove_node(pos)
  198. end
  199. })
  200. minetest.register_abm({
  201. nodenames = { "crops:corn_top_1" },
  202. neighbors = { "crops:corn_base_2" },
  203. interval = crops.settings.interval,
  204. chance = crops.settings.chance,
  205. action = function(pos, node, active_object_count, active_object_count_wider)
  206. if minetest.get_node_light(pos, nil) < crops.settings.light then
  207. return
  208. end
  209. minetest.swap_node(pos, { name = "crops:corn_top_2", param2 = 3 })
  210. end
  211. })
  212. minetest.register_node("crops:corn_top_2", {
  213. description = S("Corn plant"),
  214. drawtype = "plantlike",
  215. paramtype2 = "meshoptions",
  216. tiles = { "crops_corn_top_1.png" },
  217. waving = 1,
  218. use_texture_alpha = true,
  219. walkable = false,
  220. sunlight_propagates = true,
  221. paramtype = "light",
  222. groups = { snappy=3,flammable=3,flora=1,not_in_creative_inventory=1 },
  223. drop = {},
  224. sounds = default.node_sound_leaves_defaults(),
  225. on_dig = function(pos, node, digger)
  226. local below = {x = pos.x, y = pos.y - 1, z = pos.z}
  227. if not minetest.get_node(below).name == "crops:base_2" then
  228. return
  229. end
  230. minetest.remove_node(below)
  231. minetest.remove_node(pos)
  232. end
  233. })
  234. minetest.register_abm({
  235. nodenames = { "crops:corn_top_2" },
  236. neighbors = { "crops:corn_base_2" },
  237. interval = crops.settings.interval,
  238. chance = crops.settings.chance,
  239. action = function(pos, node, active_object_count, active_object_count_wider)
  240. -- we don't call crops.grow here otherwise there would be 2 abm's hitting
  241. -- this stack, and dmg needs to be applied to the bottom part
  242. if minetest.get_node_light(pos, nil) < crops.settings.light then
  243. return
  244. end
  245. minetest.swap_node(pos, { name = "crops:corn_top_3", param2 = 3 })
  246. end
  247. })
  248. minetest.register_node("crops:corn_top_3", {
  249. description = S("Corn plant"),
  250. drawtype = "plantlike",
  251. paramtype2 = "meshoptions",
  252. tiles = { "crops_corn_top_2.png" },
  253. waving = 1,
  254. use_texture_alpha = true,
  255. walkable = false,
  256. sunlight_propagates = true,
  257. paramtype = "light",
  258. groups = { snappy=3,flammable=3,flora=1,not_in_creative_inventory=1 },
  259. drop = {},
  260. sounds = default.node_sound_leaves_defaults(),
  261. on_dig = function(pos, node, digger)
  262. local below = { x = pos.x, y = pos.y - 1, z = pos.z }
  263. local meta = minetest.get_meta(below)
  264. local damage = meta:get_int("crops_damage")
  265. local drops = {}
  266. -- 0 - 2-4
  267. -- 50 - 2-3
  268. -- 100 - 1-1
  269. for i = 1,math.random(2 - (damage / 100), 4 - (3 * (damage / 100))) do
  270. table.insert(drops, ('crops:corn_cob'))
  271. end
  272. crops.die(below)
  273. core.handle_node_drops(pos, drops, digger)
  274. end
  275. })
  276. minetest.register_node("crops:corn_top_4", {
  277. description = S("Corn plant"),
  278. drawtype = "plantlike",
  279. paramtype2 = "meshoptions",
  280. tiles = { "crops_corn_top_3.png" },
  281. waving = 1,
  282. use_texture_alpha = true,
  283. walkable = false,
  284. sunlight_propagates = true,
  285. paramtype = "light",
  286. groups = { snappy=3,flammable=3,flora=1,not_in_creative_inventory=1 },
  287. drop = {},
  288. sounds = default.node_sound_leaves_defaults(),
  289. on_dig = function(pos, node, digger)
  290. local below = {x = pos.x, y = pos.y - 1, z = pos.z}
  291. if minetest.get_node(below).name == "crops:corn_base_3" then
  292. minetest.remove_node(below)
  293. end
  294. minetest.remove_node(pos)
  295. end
  296. })
  297. crops.corn_die = function(pos)
  298. minetest.set_node(pos, { name = "crops:corn_base_3", param2 = 3 })
  299. local above = {x = pos.x, y = pos.y + 1, z = pos.z}
  300. minetest.set_node(above, { name = "crops:corn_top_4", param2 = 3 })
  301. end
  302. local properties = {
  303. die = crops.corn_die,
  304. waterstart = 40,
  305. wateruse = 1,
  306. night = 5,
  307. soak = 60,
  308. soak_damage = 75,
  309. wither = 10,
  310. wither_damage = 5,
  311. doublesize = true,
  312. }
  313. crops.register({ name = "crops:corn_base_seed", properties = properties })
  314. crops.register({ name = "crops:corn_base_1", properties = properties })
  315. crops.register({ name = "crops:corn_base_2", properties = properties })
  316. crops.register({ name = "crops:corn_base_3", properties = properties })