Abm check all neighbours

Post Reply
User avatar
markthesmeagol
Member
Posts: 34
Joined: Fri Dec 21, 2018 13:15
GitHub: smeagolthellama
In-game: haivets

Abm check all neighbours

by markthesmeagol » Post

I am writing a 3d game of life thing, since the one that existed previously has no link, and uses 0.4. I am currently adding the actual rule thing (using an abm). However, for that I would need to check all the neighbours of a given node. Is there a fast way to do this, or do I need to manually check all 26 neighbours?
I am not very good at lua. I am learning though.

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

Re: Abm check all neighbours

by Krock » Post

Within the ABM registration you can specify neighbour nodes. One of them is required in order to trigger the "action" function. It your code should only run for - say - three similar neighbours, then you'd need to guard the code within the "action" function using a minetest.find_nodes_in_area check.

Such code could look as follows: (untested, quote to preserve tabulators)

Code: Select all

action = function(pos, node)
	local positions, nodes_count = minetest.find_nodes_in_area(
		vector.add(pos, -1), vector.add(pos, 1), {"default:mese"})

	if (nodes_count["default:mese"] or 0) < 2 then
		return
	end

	-- Code to run if there are >= 3 mese nodes
end
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
markthesmeagol
Member
Posts: 34
Joined: Fri Dec 21, 2018 13:15
GitHub: smeagolthellama
In-game: haivets

Re: Abm check all neighbours

by markthesmeagol » Post

Krock wrote:Within the ABM registration you can specify neighbour nodes. One of them is required in order to trigger the "action" function. It your code should only run for - say - three similar neighbours, then you'd need to guard the code within the "action" function using a minetest.find_nodes_in_area check.

Such code could look as follows: (untested, quote to preserve tabulators)

Code: Select all

action = function(pos, node)
	local positions, nodes_count = minetest.find_nodes_in_area(
		vector.add(pos, -1), vector.add(pos, 1), {"default:mese"})

	if (nodes_count["default:mese"] or 0) < 2 then
		return
	end

	-- Code to run if there are >= 3 mese nodes
end
Thanks! That is actually a lot easier to understand then the api, or the book! I'mma go implement that now...
I am not very good at lua. I am learning though.

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Abm check all neighbours

by rubenwardy » Post

markthesmeagol wrote:Thanks! That is actually a lot easier to understand then the api, or the book! I'mma go implement that now...
Because he wrote the exact code you needed to use. The API is a list of functions, and the book can't contain every single possible snippet of code
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
markthesmeagol
Member
Posts: 34
Joined: Fri Dec 21, 2018 13:15
GitHub: smeagolthellama
In-game: haivets

Re: Abm check all neighbours

by markthesmeagol » Post

rubenwardy wrote:
markthesmeagol wrote:Thanks! That is actually a lot easier to understand then the api, or the book! I'mma go implement that now...
Because he wrote the exact code you needed to use. The API is a list of functions, and the book can't contain every single possible snippet of code
Actually, the api said something about a second return value, which doesn't make very much sense to my c/c++ background, and the book just sticks a # or something at the end, without explaining anything.
I am not very good at lua. I am learning though.

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Abm check all neighbours

by rubenwardy » Post

markthesmeagol wrote:Actually, the api said something about a second return value, which doesn't make very much sense to my c/c++ background
Higher-level languages add convenience features. In C, register 0 is used to return a single value. In Lua, the equivalent of register 0 indicates how many values should be returned. The caller of a function unpacks the return values like so:

Code: Select all

local one, two = do_thing()
print(one)
print(two)
markthesmeagol wrote:and the book just sticks a # or something at the end, without explaining anything.
The # is an anchor link which allows you to link to specific section like so: https://rubenwardy.com/minetest_modding ... -modifiers

This section of the book is fairly old, and so doesn't "tell a story" as well as other sections. Perhaps it should introduce a simple ABM before introducing chances and more complex ABMs

I'm trying to work out exactly what it is that's unclear, so I can improve it

Edit: are you referring to the Basic Map Operations section? This makes what you said make sense, as the book doesn't explain it very well - it doesn't mention the second return value, and just uses the # operator to count all values in the table.
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

Post Reply

Who is online

Users browsing this forum: CalebJ and 9 guests