Spreading Crops LBM

Post Reply
User avatar
Annalysa
Member
Posts: 19
Joined: Sat Jun 23, 2018 12:03
GitHub: AnnalysaTheMinetester
In-game: Annalysa
Location: Italia

Spreading Crops LBM

by Annalysa » Post

Hello!

I wanted to make a peanut crop for my mod, using the farming redo api, but i wanted to
give it a little something, by using a lbm so that whenever it reaches stage 5 it will spread on a nearby soil node.

The thing is that i'm new to modding and lua in general so i'm a little confused.
This is what i have written so far, can somebody help me?
By the way the lbm is in the same .lua as all the crops stages, 5 in total and it grows.

Code: Select all

minetest.register_lbm({
 name = "sandwiches:peanut_spreading_lbm",
 nodenames = {"sandwiches:peanut_5"},
 action = function(pos, node)
  local soil_pos = minetest.find_node_near({x= pos.x, y= pos.y-1, z= pos.z},1,{"farming:soil_wet","farming:soil"})
  local above_node = minetest.get_node({x= soil_pos.x, y= soil_pos.y+1, z= soil_pos.z})
  if(above_node.name == "air") then
   minetest.set_node({x= soil_pos.x, y= soil_pos.y+1, z= soil_pos.z}, {name= "sandwiches:peanut_1"})
  end
 end,
})
Jesus is the Lord! Halleluja!

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: Spreading Crops LBM

by Krock » Post

I think what you need is an ABM, instead of an LBM: https://dev.minetest.net/Terminology
1) ABMs are triggered after the given time (and according to the chance parameter) in each mapblock. On the other side are the LBMs, which are only triggered when loading new mapblocks - either each time they load or only once in the entire map lifetime.

2) Another issue is that you're only checking one soil node, which then might always be the same position. This results either in spreading the plant only into one direction or not spreading at all (i.e. above_node == pos).

Give this code a try: (untested), quote this post to copy&paste the code with tabulators as indents.

Code: Select all

minetest.register_abm({
	name = "sandwiches:peanut_spreading_lbm",
	nodenames = {"sandwiches:peanut_5"},
	interval = 5,
	chance = 5,
	action = function(pos, node)
		-- Check 3x3x3 nodes around the currently triggered node
		pos.y = pos.y - 1 -- Find soil
		local soil_positions = minetest.find_nodes_in_area_under_air(
			vector.add(pos, -1), vector.add(pos, 1),
			{"farming:soil_wet", "farming:soil"}
		)
		if #soil_positions == 0 then
			return -- no soil found
		end
		-- Pick a random result
		local soil_pos = soil_positions[math.random(#soil_positions)]
		soil_pos.y = soil_pos.y + 1
		minetest.set_node(soil_pos, {name= "sandwiches:peanut_1"})
	end,
})
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
paramat
Developer
Posts: 3700
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat
IRC: paramat
Location: UK

Re: Spreading Crops LBM

by paramat » Post

Yes use an ABM.

User avatar
Annalysa
Member
Posts: 19
Joined: Sat Jun 23, 2018 12:03
GitHub: AnnalysaTheMinetester
In-game: Annalysa
Location: Italia

Re: Spreading Crops LBM

by Annalysa » Post

Ciao!

First of all, sorry, i'm a noob at modding: i thought that lbm triggered an action only once, as that's what i was
aiming at with the crop.
Now i created a 6ht stage so that the plant get "exhausted" due to the spreading and then stops.

Secondly: The code that you gave me was useful and i managed to successfully trigger the spreading, after some time.
This is the result:

Code: Select all

minetest.register_abm({
   name = "sandwiches:peanut_spreading_abm",
   nodenames = {"sandwiches:peanut_5"},
   interval = 5,
   chance = 5,
   action = function(pos, node)
      -- Check 3x3x3 nodes around the currently triggered node
      local soil_positions = minetest.find_nodes_in_area_under_air( vector.add(pos, -1), vector.add(pos, 1), {"farming:soil_wet", "farming:soil"})
      if ( next(soil_positions) ~= null) then
        local found_soil_pos = soil_positions[math.random(#soil_positions)]
		found_soil_pos.y = found_soil_pos.y +1
		minetest.set_node(found_soil_pos, {name="sandwiches:peanut_1"})
		if(math.random(10) > 5) then
			minetest.set_node(pos,{name="sandwiches:peanut_6"})
		end
	  end
   end
 })

In the end, Thank you.
Jesus is the Lord! Halleluja!

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests