Abm check all neighbours

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

Abm check all neighbours

by markthesmeagol » Sun Dec 01, 2019 10:18

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: 4409
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

Re: Abm check all neighbours

by Krock » Sun Dec 01, 2019 13:16

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: 32
Joined: Fri Dec 21, 2018 13:15
GitHub: smeagolthellama
In-game: haivets

Re: Abm check all neighbours

by markthesmeagol » Sun Dec 01, 2019 14:51

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: 5886
Joined: Tue Jun 12, 2012 18:11
Location: United Kingdom
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: Abm check all neighbours

by rubenwardy » Sun Dec 01, 2019 15:53

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
 

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

Re: Abm check all neighbours

by markthesmeagol » Sun Dec 01, 2019 16:17

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: 5886
Joined: Tue Jun 12, 2012 18:11
Location: United Kingdom
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: Abm check all neighbours

by rubenwardy » Sun Dec 01, 2019 17:37

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_book/en/map/timers.html#active-block-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.
 


Return to Modding Discussion



Who is online

Users browsing this forum: Bing [Bot] and 1 guest