init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. -- Minetest 0.4 mod: farming
  2. -- See README.txt for licensing and other information.
  3. farming = {}
  4. --
  5. -- Soil
  6. --
  7. minetest.register_node("farming:soil", {
  8. description = "Soil",
  9. tiles = {"farming_soil.png", "default_dirt.png"},
  10. drop = "default:dirt",
  11. is_ground_content = true,
  12. groups = {crumbly=3, not_in_creative_inventory=1, soil=2},
  13. sounds = default.node_sound_dirt_defaults(),
  14. })
  15. minetest.register_node("farming:soil_wet", {
  16. description = "Wet Soil",
  17. tiles = {"farming_soil_wet.png", "farming_soil_wet_side.png"},
  18. drop = "default:dirt",
  19. is_ground_content = true,
  20. groups = {crumbly=3, not_in_creative_inventory=1, soil=3},
  21. sounds = default.node_sound_dirt_defaults(),
  22. })
  23. minetest.register_abm({
  24. nodenames = {"farming:soil", "farming:soil_wet"},
  25. interval = 15,
  26. chance = 4,
  27. action = function(pos, node)
  28. pos.y = pos.y+1
  29. local nn = minetest.get_node(pos).name
  30. pos.y = pos.y-1
  31. if minetest.registered_nodes[nn] and
  32. minetest.registered_nodes[nn].walkable and
  33. minetest.get_item_group(nn, "plant") == 0
  34. then
  35. minetest.set_node(pos, {name="default:dirt"})
  36. end
  37. -- check if there is water nearby
  38. if minetest.find_node_near(pos, 3, {"group:water"}) then
  39. -- if it is dry soil turn it into wet soil
  40. if node.name == "farming:soil" then
  41. minetest.set_node(pos, {name="farming:soil_wet"})
  42. end
  43. else
  44. -- turn it back into dirt if it is already dry
  45. if node.name == "farming:soil" then
  46. -- only turn it back if there is no plant on top of it
  47. if minetest.get_item_group(nn, "plant") == 0 then
  48. minetest.set_node(pos, {name="default:dirt"})
  49. end
  50. -- if its wet turn it back into dry soil
  51. elseif node.name == "farming:soil_wet" then
  52. minetest.set_node(pos, {name="farming:soil"})
  53. end
  54. end
  55. end,
  56. })
  57. --
  58. -- Hoes
  59. --
  60. -- turns nodes with group soil=1 into soil
  61. function farming.hoe_on_use(itemstack, user, pointed_thing, uses)
  62. local pt = pointed_thing
  63. -- check if pointing at a node
  64. if not pt then
  65. return
  66. end
  67. if pt.type ~= "node" then
  68. return
  69. end
  70. local under = minetest.get_node(pt.under)
  71. local p = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
  72. local above = minetest.get_node(p)
  73. -- return if any of the nodes is not registered
  74. if not minetest.registered_nodes[under.name] then
  75. return
  76. end
  77. if not minetest.registered_nodes[above.name] then
  78. return
  79. end
  80. -- check if the node above the pointed thing is air
  81. if above.name ~= "air" then
  82. return
  83. end
  84. -- check if pointing at dirt
  85. if minetest.get_item_group(under.name, "soil") ~= 1 then
  86. return
  87. end
  88. -- turn the node into soil, wear out item and play sound
  89. minetest.set_node(pt.under, {name="farming:soil"})
  90. minetest.sound_play("default_dig_crumbly", {
  91. pos = pt.under,
  92. gain = 0.5,
  93. })
  94. itemstack:add_wear(65535/(uses-1))
  95. return itemstack
  96. end
  97. minetest.register_tool("farming:hoe_wood", {
  98. description = "Wooden Hoe",
  99. inventory_image = "farming_tool_woodhoe.png",
  100. on_use = function(itemstack, user, pointed_thing)
  101. return farming.hoe_on_use(itemstack, user, pointed_thing, 30)
  102. end,
  103. })
  104. minetest.register_tool("farming:hoe_stone", {
  105. description = "Stone Hoe",
  106. inventory_image = "farming_tool_stonehoe.png",
  107. on_use = function(itemstack, user, pointed_thing)
  108. return farming.hoe_on_use(itemstack, user, pointed_thing, 90)
  109. end,
  110. })
  111. minetest.register_tool("farming:hoe_steel", {
  112. description = "Steel Hoe",
  113. inventory_image = "farming_tool_steelhoe.png",
  114. on_use = function(itemstack, user, pointed_thing)
  115. return farming.hoe_on_use(itemstack, user, pointed_thing, 200)
  116. end,
  117. })
  118. minetest.register_tool("farming:hoe_bronze", {
  119. description = "Bronze Hoe",
  120. inventory_image = "farming_tool_bronzehoe.png",
  121. on_use = function(itemstack, user, pointed_thing)
  122. return farming.hoe_on_use(itemstack, user, pointed_thing, 220)
  123. end,
  124. })
  125. minetest.register_craft({
  126. output = "farming:hoe_wood",
  127. recipe = {
  128. {"group:wood", "group:wood"},
  129. {"", "group:stick"},
  130. {"", "group:stick"},
  131. }
  132. })
  133. minetest.register_craft({
  134. output = "farming:hoe_stone",
  135. recipe = {
  136. {"group:stone", "group:stone"},
  137. {"", "group:stick"},
  138. {"", "group:stick"},
  139. }
  140. })
  141. minetest.register_craft({
  142. output = "farming:hoe_steel",
  143. recipe = {
  144. {"default:steel_ingot", "default:steel_ingot"},
  145. {"", "group:stick"},
  146. {"", "group:stick"},
  147. }
  148. })
  149. minetest.register_craft({
  150. output = "farming:hoe_bronze",
  151. recipe = {
  152. {"default:bronze_ingot", "default:bronze_ingot"},
  153. {"", "group:stick"},
  154. {"", "group:stick"},
  155. }
  156. })
  157. --
  158. -- Override grass for drops
  159. --
  160. minetest.override_item("default:grass_1", {
  161. drop = {
  162. max_items = 1,
  163. items = {
  164. {items = {'farming:seed_wheat'},rarity = 7},
  165. {items = {'farming:seed_cotton'},rarity = 7},
  166. {items = {'default:grass_1'}},
  167. }
  168. }
  169. })
  170. for i=2,5 do
  171. minetest.override_item("default:grass_"..i, {
  172. drop = {
  173. max_items = 1,
  174. items = {
  175. {items = {'farming:seed_wheat'},rarity = 7},
  176. {items = {'farming:seed_cotton'},rarity = 7},
  177. {items = {'default:grass_1'}},
  178. }
  179. }
  180. })
  181. end
  182. minetest.register_node(":default:junglegrass", {
  183. description = "Jungle Grass",
  184. drawtype = "plantlike",
  185. visual_scale = 1.3,
  186. tiles = {"default_junglegrass.png"},
  187. inventory_image = "default_junglegrass.png",
  188. wield_image = "default_junglegrass.png",
  189. paramtype = "light",
  190. waving = 1,
  191. walkable = false,
  192. buildable_to = true,
  193. is_ground_content = true,
  194. drop = {
  195. max_items = 1,
  196. items = {
  197. {items = {'farming:seed_cotton'},rarity = 8},
  198. {items = {'default:junglegrass'}},
  199. }
  200. },
  201. groups = {snappy=3,flammable=2,flora=1,attached_node=1},
  202. sounds = default.node_sound_leaves_defaults(),
  203. selection_box = {
  204. type = "fixed",
  205. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  206. },
  207. })
  208. --
  209. -- Place seeds
  210. --
  211. local function place_seed(itemstack, placer, pointed_thing, plantname)
  212. local pt = pointed_thing
  213. -- check if pointing at a node
  214. if not pt then
  215. return
  216. end
  217. if pt.type ~= "node" then
  218. return
  219. end
  220. local under = minetest.get_node(pt.under)
  221. local above = minetest.get_node(pt.above)
  222. -- return if any of the nodes is not registered
  223. if not minetest.registered_nodes[under.name] then
  224. return
  225. end
  226. if not minetest.registered_nodes[above.name] then
  227. return
  228. end
  229. -- check if pointing at the top of the node
  230. if pt.above.y ~= pt.under.y+1 then
  231. return
  232. end
  233. -- check if you can replace the node above the pointed node
  234. if not minetest.registered_nodes[above.name].buildable_to then
  235. return
  236. end
  237. -- check if pointing at soil
  238. if minetest.get_item_group(under.name, "soil") <= 1 then
  239. return
  240. end
  241. -- add the node and remove 1 item from the itemstack
  242. minetest.add_node(pt.above, {name=plantname})
  243. if not minetest.setting_getbool("creative_mode") then
  244. itemstack:take_item()
  245. end
  246. return itemstack
  247. end
  248. --
  249. -- Wheat
  250. --
  251. minetest.register_craftitem("farming:seed_wheat", {
  252. description = "Wheat Seed",
  253. inventory_image = "farming_wheat_seed.png",
  254. on_place = function(itemstack, placer, pointed_thing)
  255. return place_seed(itemstack, placer, pointed_thing, "farming:wheat_1")
  256. end,
  257. })
  258. minetest.register_craftitem("farming:wheat", {
  259. description = "Wheat",
  260. inventory_image = "farming_wheat.png",
  261. })
  262. minetest.register_craftitem("farming:flour", {
  263. description = "Flour",
  264. inventory_image = "farming_flour.png",
  265. })
  266. minetest.register_craftitem("farming:bread", {
  267. description = "Bread",
  268. inventory_image = "farming_bread.png",
  269. on_use = minetest.item_eat(4),
  270. })
  271. minetest.register_craft({
  272. type = "shapeless",
  273. output = "farming:flour",
  274. recipe = {"farming:wheat", "farming:wheat", "farming:wheat", "farming:wheat"}
  275. })
  276. minetest.register_craft({
  277. type = "cooking",
  278. cooktime = 15,
  279. output = "farming:bread",
  280. recipe = "farming:flour"
  281. })
  282. for i=1,8 do
  283. local drop = {
  284. items = {
  285. {items = {'farming:wheat'},rarity=9-i},
  286. {items = {'farming:wheat'},rarity=18-i*2},
  287. {items = {'farming:seed_wheat'},rarity=9-i},
  288. {items = {'farming:seed_wheat'},rarity=18-i*2},
  289. }
  290. }
  291. minetest.register_node("farming:wheat_"..i, {
  292. drawtype = "plantlike",
  293. tiles = {"farming_wheat_"..i..".png"},
  294. paramtype = "light",
  295. waving = 1,
  296. walkable = false,
  297. buildable_to = true,
  298. is_ground_content = true,
  299. drop = drop,
  300. selection_box = {
  301. type = "fixed",
  302. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  303. },
  304. groups = {snappy=3,flammable=2,plant=1,wheat=i,not_in_creative_inventory=1,attached_node=1},
  305. sounds = default.node_sound_leaves_defaults(),
  306. })
  307. end
  308. minetest.register_abm({
  309. nodenames = {"group:wheat"},
  310. neighbors = {"group:soil"},
  311. interval = 60,
  312. chance = 20,
  313. action = function(pos, node)
  314. -- return if already full grown
  315. if minetest.get_item_group(node.name, "wheat") == 8 then
  316. return
  317. end
  318. -- check if on wet soil
  319. pos.y = pos.y-1
  320. local n = minetest.get_node(pos)
  321. if minetest.get_item_group(n.name, "soil") < 3 then
  322. return
  323. end
  324. pos.y = pos.y+1
  325. -- check light
  326. local light_level = minetest.get_node_light(pos)
  327. if not light_level then
  328. return
  329. end
  330. local c = math.ceil(2 * (light_level - 13) ^ 2 + 1)
  331. if light_level > 7 and (math.random(1, c) == 1 or light_level >= 13) then
  332. local height = minetest.get_item_group(node.name, "wheat") + 1
  333. minetest.set_node(pos, {name="farming:wheat_" .. height})
  334. end
  335. end
  336. })
  337. --
  338. -- Cotton
  339. --
  340. minetest.register_craftitem("farming:seed_cotton", {
  341. description = "Cotton Seed",
  342. inventory_image = "farming_cotton_seed.png",
  343. on_place = function(itemstack, placer, pointed_thing)
  344. return place_seed(itemstack, placer, pointed_thing, "farming:cotton_1")
  345. end,
  346. })
  347. minetest.register_craftitem("farming:string", {
  348. description = "String",
  349. inventory_image = "farming_string.png",
  350. })
  351. minetest.register_craft({
  352. output = "wool:white",
  353. recipe = {
  354. {"farming:string", "farming:string"},
  355. {"farming:string", "farming:string"},
  356. }
  357. })
  358. for i=1,8 do
  359. local drop = {
  360. items = {
  361. {items = {'farming:string'},rarity=9-i},
  362. {items = {'farming:string'},rarity=18-i*2},
  363. {items = {'farming:string'},rarity=27-i*3},
  364. {items = {'farming:seed_cotton'},rarity=9-i},
  365. {items = {'farming:seed_cotton'},rarity=18-i*2},
  366. {items = {'farming:seed_cotton'},rarity=27-i*3},
  367. }
  368. }
  369. minetest.register_node("farming:cotton_"..i, {
  370. drawtype = "plantlike",
  371. tiles = {"farming_cotton_"..i..".png"},
  372. paramtype = "light",
  373. waving = 1,
  374. walkable = false,
  375. buildable_to = true,
  376. is_ground_content = true,
  377. drop = drop,
  378. selection_box = {
  379. type = "fixed",
  380. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  381. },
  382. groups = {snappy=3,flammable=2,plant=1,cotton=i,not_in_creative_inventory=1,attached_node=1},
  383. sounds = default.node_sound_leaves_defaults(),
  384. })
  385. end
  386. minetest.register_abm({
  387. nodenames = {"group:cotton"},
  388. neighbors = {"group:soil"},
  389. interval = 60,
  390. chance = 20,
  391. action = function(pos, node)
  392. -- return if already full grown
  393. if minetest.get_item_group(node.name, "cotton") == 8 then
  394. return
  395. end
  396. -- check if on wet soil
  397. pos.y = pos.y-1
  398. local n = minetest.get_node(pos)
  399. if minetest.get_item_group(n.name, "soil") < 3 then
  400. return
  401. end
  402. pos.y = pos.y+1
  403. -- check light
  404. local light_level = minetest.get_node_light(pos)
  405. if not light_level then
  406. return
  407. end
  408. local c = math.ceil(2 * (light_level - 13) ^ 2 + 1)
  409. if light_level > 7 and (math.random(1, c) == 1 or light_level >= 13) then
  410. local height = minetest.get_item_group(node.name, "cotton") + 1
  411. minetest.set_node(pos, {name="farming:cotton_"..height})
  412. end
  413. end
  414. })
  415. minetest.register_node("farming:straw", {
  416. description = "Straw",
  417. tiles = {"farming_straw.png"},
  418. is_ground_content = false,
  419. groups = {snappy=3, flammable=4, fall_damage_add_percent=-30},
  420. sounds = default.node_sound_leaves_defaults(),
  421. })
  422. minetest.register_craft({
  423. output = "farming:straw 3",
  424. recipe = {
  425. {"farming:wheat", "farming:wheat", "farming:wheat"},
  426. {"farming:wheat", "farming:wheat", "farming:wheat"},
  427. {"farming:wheat", "farming:wheat", "farming:wheat"},
  428. }
  429. })
  430. minetest.register_craft({
  431. output = "farming:wheat 3",
  432. recipe = {
  433. {"farming:straw"},
  434. }
  435. })