Abm check all neighbours
- markthesmeagol
- Member
- Posts: 34
- Joined: Fri Dec 21, 2018 13:15
- GitHub: smeagolthellama
- In-game: haivets
Abm check all neighbours
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.
- Krock
- Developer
- Posts: 4650
- Joined: Thu Oct 03, 2013 07:48
- GitHub: SmallJoker
- Location: Switzerland
- Contact:
Re: Abm check all neighbours
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)
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>
- markthesmeagol
- Member
- Posts: 34
- Joined: Fri Dec 21, 2018 13:15
- GitHub: smeagolthellama
- In-game: haivets
Re: Abm check all neighbours
Thanks! That is actually a lot easier to understand then the api, or the book! I'mma go implement that now...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
I am not very good at lua. I am learning though.
- 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
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 codemarkthesmeagol wrote:Thanks! That is actually a lot easier to understand then the api, or the book! I'mma go implement that now...
- markthesmeagol
- Member
- Posts: 34
- Joined: Fri Dec 21, 2018 13:15
- GitHub: smeagolthellama
- In-game: haivets
Re: Abm check all neighbours
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.rubenwardy wrote: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 codemarkthesmeagol wrote: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.
- 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
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:markthesmeagol wrote:Actually, the api said something about a second return value, which doesn't make very much sense to my c/c++ background
Code: Select all
local one, two = do_thing()
print(one)
print(two)
The # is an anchor link which allows you to link to specific section like so: https://rubenwardy.com/minetest_modding ... -modifiersmarkthesmeagol wrote:and the book just sticks a # or something at the end, without explaining anything.
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.
Who is online
Users browsing this forum: CalebJ and 9 guests