api.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. -- farming/api.lua
  2. -- support for MT game translation.
  3. local S = farming.get_translator
  4. -- Wear out hoes, place soil
  5. -- TODO Ignore group:flower
  6. farming.registered_plants = {}
  7. farming.hoe_on_use = function(itemstack, user, pointed_thing, uses)
  8. local pt = pointed_thing
  9. -- check if pointing at a node
  10. if not pt then
  11. return
  12. end
  13. if pt.type ~= "node" then
  14. return
  15. end
  16. local under = minetest.get_node(pt.under)
  17. local p = {x=pt.under.x, y=pt.under.y+1, z=pt.under.z}
  18. local above = minetest.get_node(p)
  19. -- return if any of the nodes is not registered
  20. if not minetest.registered_nodes[under.name] then
  21. return
  22. end
  23. if not minetest.registered_nodes[above.name] then
  24. return
  25. end
  26. -- check if the node above the pointed thing is air
  27. if above.name ~= "air" then
  28. return
  29. end
  30. -- check if pointing at soil
  31. if minetest.get_item_group(under.name, "soil") ~= 1 then
  32. return
  33. end
  34. -- check if (wet) soil defined
  35. local regN = minetest.registered_nodes
  36. if regN[under.name].soil == nil or regN[under.name].soil.wet == nil or regN[under.name].soil.dry == nil then
  37. return
  38. end
  39. if minetest.is_protected(pt.under, user:get_player_name()) then
  40. minetest.record_protection_violation(pt.under, user:get_player_name())
  41. return
  42. end
  43. if minetest.is_protected(pt.above, user:get_player_name()) then
  44. minetest.record_protection_violation(pt.above, user:get_player_name())
  45. return
  46. end
  47. -- turn the node into soil and play sound
  48. minetest.set_node(pt.under, {name = regN[under.name].soil.dry})
  49. minetest.sound_play("default_dig_crumbly", {
  50. pos = pt.under,
  51. gain = 0.5,
  52. }, true)
  53. if not (creative and creative.is_enabled_for
  54. and creative.is_enabled_for(user:get_player_name())) then
  55. -- wear tool
  56. local wdef = itemstack:get_definition()
  57. itemstack:add_wear(65535/(uses-1))
  58. -- tool break sound
  59. if itemstack:get_count() == 0 and wdef.sound and wdef.sound.breaks then
  60. minetest.sound_play(wdef.sound.breaks, {pos = pt.above,
  61. gain = 0.5}, true)
  62. end
  63. end
  64. return itemstack
  65. end
  66. -- Register new hoes
  67. farming.register_hoe = function(name, def)
  68. -- Check for : prefix (register new hoes in your mod's namespace)
  69. if name:sub(1,1) ~= ":" then
  70. name = ":" .. name
  71. end
  72. -- Check def table
  73. if def.description == nil then
  74. def.description = S("Hoe")
  75. end
  76. if def.inventory_image == nil then
  77. def.inventory_image = "unknown_item.png"
  78. end
  79. if def.max_uses == nil then
  80. def.max_uses = 30
  81. end
  82. -- Register the tool
  83. minetest.register_tool(name, {
  84. description = def.description,
  85. inventory_image = def.inventory_image,
  86. on_use = function(itemstack, user, pointed_thing)
  87. return farming.hoe_on_use(itemstack, user, pointed_thing, def.max_uses)
  88. end,
  89. groups = def.groups,
  90. sound = {breaks = "default_tool_breaks"},
  91. })
  92. -- Register its recipe
  93. if def.recipe then
  94. minetest.register_craft({
  95. output = name:sub(2),
  96. recipe = def.recipe
  97. })
  98. elseif def.material then
  99. minetest.register_craft({
  100. output = name:sub(2),
  101. recipe = {
  102. {def.material, def.material},
  103. {"", "group:stick"},
  104. {"", "group:stick"}
  105. }
  106. })
  107. end
  108. end
  109. -- how often node timers for plants will tick, +/- some random value
  110. local function tick(pos)
  111. minetest.get_node_timer(pos):start(math.random(166, 286))
  112. end
  113. -- how often a growth failure tick is retried (e.g. too dark)
  114. local function tick_again(pos)
  115. minetest.get_node_timer(pos):start(math.random(40, 80))
  116. end
  117. -- Seed placement
  118. farming.place_seed = function(itemstack, placer, pointed_thing, plantname)
  119. local pt = pointed_thing
  120. -- check if pointing at a node
  121. if not pt then
  122. return itemstack
  123. end
  124. if pt.type ~= "node" then
  125. return itemstack
  126. end
  127. local under = minetest.get_node(pt.under)
  128. local above = minetest.get_node(pt.above)
  129. local player_name = placer and placer:get_player_name() or ""
  130. if minetest.is_protected(pt.under, player_name) then
  131. minetest.record_protection_violation(pt.under, player_name)
  132. return
  133. end
  134. if minetest.is_protected(pt.above, player_name) then
  135. minetest.record_protection_violation(pt.above, player_name)
  136. return
  137. end
  138. -- return if any of the nodes is not registered
  139. if not minetest.registered_nodes[under.name] then
  140. return itemstack
  141. end
  142. if not minetest.registered_nodes[above.name] then
  143. return itemstack
  144. end
  145. -- check if pointing at the top of the node
  146. if pt.above.y ~= pt.under.y+1 then
  147. return itemstack
  148. end
  149. -- check if you can replace the node above the pointed node
  150. if not minetest.registered_nodes[above.name].buildable_to then
  151. return itemstack
  152. end
  153. -- check if pointing at soil
  154. if minetest.get_item_group(under.name, "soil") < 2 then
  155. return itemstack
  156. end
  157. -- add the node and remove 1 item from the itemstack
  158. minetest.log("action", player_name .. " places node " .. plantname .. " at " ..
  159. minetest.pos_to_string(pt.above))
  160. minetest.add_node(pt.above, {name = plantname, param2 = 1})
  161. tick(pt.above)
  162. if not (creative and creative.is_enabled_for
  163. and creative.is_enabled_for(player_name)) then
  164. itemstack:take_item()
  165. end
  166. return itemstack
  167. end
  168. farming.grow_plant = function(pos, elapsed)
  169. local node = minetest.get_node(pos)
  170. local name = node.name
  171. local def = minetest.registered_nodes[name]
  172. if not def.next_plant then
  173. -- disable timer for fully grown plant
  174. return
  175. end
  176. -- grow seed
  177. if minetest.get_item_group(node.name, "seed") and def.fertility then
  178. local soil_node = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
  179. if not soil_node then
  180. tick_again(pos)
  181. return
  182. end
  183. -- omitted is a check for light, we assume seeds can germinate in the dark.
  184. for _, v in pairs(def.fertility) do
  185. if minetest.get_item_group(soil_node.name, v) ~= 0 then
  186. local placenode = {name = def.next_plant}
  187. if def.place_param2 then
  188. placenode.param2 = def.place_param2
  189. end
  190. minetest.swap_node(pos, placenode)
  191. if minetest.registered_nodes[def.next_plant].next_plant then
  192. tick(pos)
  193. return
  194. end
  195. end
  196. end
  197. return
  198. end
  199. -- check if on wet soil
  200. local below = minetest.get_node({x = pos.x, y = pos.y - 1, z = pos.z})
  201. if minetest.get_item_group(below.name, "soil") < 3 then
  202. tick_again(pos)
  203. return
  204. end
  205. -- check light
  206. local light = minetest.get_node_light(pos)
  207. if not light or light < def.minlight or light > def.maxlight then
  208. tick_again(pos)
  209. return
  210. end
  211. -- grow
  212. local placenode = {name = def.next_plant}
  213. if def.place_param2 then
  214. placenode.param2 = def.place_param2
  215. end
  216. minetest.swap_node(pos, placenode)
  217. -- new timer needed?
  218. if minetest.registered_nodes[def.next_plant].next_plant then
  219. tick(pos)
  220. end
  221. return
  222. end
  223. -- Register plants
  224. farming.register_plant = function(name, def)
  225. local mname = name:split(":")[1]
  226. local pname = name:split(":")[2]
  227. -- Check def table
  228. if not def.description then
  229. def.description = S("Seed")
  230. end
  231. if not def.harvest_description then
  232. def.harvest_description = pname:gsub("^%l", string.upper)
  233. end
  234. if not def.inventory_image then
  235. def.inventory_image = "unknown_item.png"
  236. end
  237. if not def.steps then
  238. return nil
  239. end
  240. if not def.minlight then
  241. def.minlight = 1
  242. end
  243. if not def.maxlight then
  244. def.maxlight = 14
  245. end
  246. if not def.fertility then
  247. def.fertility = {}
  248. end
  249. farming.registered_plants[pname] = def
  250. -- Register seed
  251. local lbm_nodes = {mname .. ":seed_" .. pname}
  252. local g = {seed = 1, snappy = 3, attached_node = 1, flammable = 2}
  253. for k, v in pairs(def.fertility) do
  254. g[v] = 1
  255. end
  256. minetest.register_node(":" .. mname .. ":seed_" .. pname, {
  257. description = def.description,
  258. tiles = {def.inventory_image},
  259. inventory_image = def.inventory_image,
  260. wield_image = def.inventory_image,
  261. drawtype = "signlike",
  262. groups = g,
  263. paramtype = "light",
  264. paramtype2 = "wallmounted",
  265. place_param2 = def.place_param2 or nil, -- this isn't actually used for placement
  266. walkable = false,
  267. sunlight_propagates = true,
  268. selection_box = {
  269. type = "fixed",
  270. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  271. },
  272. fertility = def.fertility,
  273. sounds = default.node_sound_dirt_defaults({
  274. dig = {name = "", gain = 0},
  275. dug = {name = "default_grass_footstep", gain = 0.2},
  276. place = {name = "default_place_node", gain = 0.25},
  277. }),
  278. on_place = function(itemstack, placer, pointed_thing)
  279. local under = pointed_thing.under
  280. local node = minetest.get_node(under)
  281. local udef = minetest.registered_nodes[node.name]
  282. if udef and udef.on_rightclick and
  283. not (placer and placer:is_player() and
  284. placer:get_player_control().sneak) then
  285. return udef.on_rightclick(under, node, placer, itemstack,
  286. pointed_thing) or itemstack
  287. end
  288. return farming.place_seed(itemstack, placer, pointed_thing, mname .. ":seed_" .. pname)
  289. end,
  290. next_plant = mname .. ":" .. pname .. "_1",
  291. on_timer = farming.grow_plant,
  292. minlight = def.minlight,
  293. maxlight = def.maxlight,
  294. })
  295. -- Register harvest
  296. minetest.register_craftitem(":" .. mname .. ":" .. pname, {
  297. description = def.harvest_description,
  298. inventory_image = mname .. "_" .. pname .. ".png",
  299. groups = def.groups or {flammable = 2},
  300. })
  301. -- Register growing steps
  302. for i = 1, def.steps do
  303. local base_rarity = 1
  304. if def.steps ~= 1 then
  305. base_rarity = 8 - (i - 1) * 7 / (def.steps - 1)
  306. end
  307. local drop = {
  308. items = {
  309. {items = {mname .. ":" .. pname}, rarity = base_rarity},
  310. {items = {mname .. ":" .. pname}, rarity = base_rarity * 2},
  311. {items = {mname .. ":seed_" .. pname}, rarity = base_rarity},
  312. {items = {mname .. ":seed_" .. pname}, rarity = base_rarity * 2},
  313. }
  314. }
  315. local nodegroups = {snappy = 3, flammable = 2, plant = 1, not_in_creative_inventory = 1, attached_node = 1}
  316. nodegroups[pname] = i
  317. local next_plant = nil
  318. if i < def.steps then
  319. next_plant = mname .. ":" .. pname .. "_" .. (i + 1)
  320. lbm_nodes[#lbm_nodes + 1] = mname .. ":" .. pname .. "_" .. i
  321. end
  322. minetest.register_node(":" .. mname .. ":" .. pname .. "_" .. i, {
  323. drawtype = "plantlike",
  324. waving = 1,
  325. tiles = {mname .. "_" .. pname .. "_" .. i .. ".png"},
  326. paramtype = "light",
  327. paramtype2 = def.paramtype2 or nil,
  328. place_param2 = def.place_param2 or nil,
  329. walkable = false,
  330. buildable_to = true,
  331. drop = drop,
  332. selection_box = {
  333. type = "fixed",
  334. fixed = {-0.5, -0.5, -0.5, 0.5, -5/16, 0.5},
  335. },
  336. groups = nodegroups,
  337. sounds = default.node_sound_leaves_defaults(),
  338. next_plant = next_plant,
  339. on_timer = farming.grow_plant,
  340. minlight = def.minlight,
  341. maxlight = def.maxlight,
  342. })
  343. end
  344. -- replacement LBM for pre-nodetimer plants
  345. minetest.register_lbm({
  346. name = ":" .. mname .. ":start_nodetimer_" .. pname,
  347. nodenames = lbm_nodes,
  348. action = function(pos, node)
  349. tick_again(pos)
  350. end,
  351. })
  352. -- Return
  353. local r = {
  354. seed = mname .. ":seed_" .. pname,
  355. harvest = mname .. ":" .. pname
  356. }
  357. return r
  358. end