[mod] game-of-life (will eventually be a game) [cells]

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

[mod] game-of-life (will eventually be a game) [cells]

by markthesmeagol » Post

I have started work on rewriting flux. It will be a 3d game-of-life thingimabob. At the moment the ABMs are so inefficient, that it doesn't really do anything other than slowly disappear. Any help is welcome.

https://github.com/smeagolthellama/cells
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: [mod] game-of-life (will eventually be a game) [cells]

by Krock » Post

For the 3D-loops, use minetest.find_nodes_in_area. It's faster to write and faster to execute.

Also a hacky speed-up is possible: Replace minetest.remove_node(pos) with minetest.swap_node(pos, { name = "air" }). Instead of properly removing the node, it swaps the content ID without running any callbacks (such as "on_place" or "on_dig"). Only do this as long your "cell" node does not have any metadata.

PS: Does this really work? I have my doubts. The correct vector notation is {x = x, y = y, z = z}.
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: [mod] game-of-life (will eventually be a game) [cells]

by markthesmeagol » Post

Krock wrote:For the 3D-loops, use minetest.find_nodes_in_area. It's faster to write and faster to execute.
I Was actually thinking about using that, but the documentation in the modding book is... lacking to say the least. I couldn't really understand how to use it. looking at the wiki, would something like

Code: Select all

local pos1={x=pos.x-1,y=pos.y-1,z=pos.z-1}
local pos2={x=pos.x+1,y=pos.y+1,z=pos.z+1}
local count=minetest.find_nodes_in_area(pos1,pos2,"cells:cell")
work, or would I need to do some special counting syntax stuff?
Krock wrote: Also a hacky speed-up is possible: Replace minetest.remove_node(pos) with minetest.swap_node(pos, { name = "air" }). Instead of properly removing the node, it swaps the content ID without running any callbacks (such as "on_place" or "on_dig"). Only do this as long your "cell" node does not have any metadata.
Huh. the modding book said that the two were identical.
Krock wrote: PS: Does this really work? I have my doubts. The correct vector notation is {x = x, y = y, z = z}.
I have no idea. It might not. I'll see if that fixes it.
I am not very good at lua. I am learning though.

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

Re: [mod] game-of-life (will eventually be a game) [cells]

by markthesmeagol » Post

I tried the {x,y,z} replacement thing, and there was no noticeable difference. I also tried the set_node thing. Still no noticeable improvement, though if the thing isn't counting things properly, then that is to be expected.

Something I thought of, would each node's removal happen after all the nodes have tested the environment, or before? If before, that may lead to some fun bugs.
I am not very good at lua. I am learning though.

User avatar
Skamiz Kazzarch
Member
Posts: 613
Joined: Fri Mar 09, 2018 20:34
GitHub: Skamiz
In-game: Skamiz
Location: la lojbaugag.

Re: [mod] game-of-life (will eventually be a game) [cells]

by Skamiz Kazzarch » Post

The action callback is called for each node sequentialy, so that the next call is affected by the previous ones.
You can try to prevent that by delaying the set_node() call, so that all nodes get to check thier suroundings before they start changeing.
Generic function for delaying stuff in minetest:
https://github.com/minetest/minetest/bl ... .txt#L4869

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

Re: [mod] game-of-life (will eventually be a game) [cells]

by markthesmeagol » Post

based on some help from viewtopic.php?f=47&t=23731, I have now reduced the "abms took too long" warnings by about 90%. However, it currently doesn't add cells, just kills them. I am quite confused.
I am not very good at lua. I am learning though.

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

Re: [mod] game-of-life (will eventually be a game) [cells]

by markthesmeagol » Post

Skamiz Kazzarch wrote:The action callback is called for each node sequentialy, so that the next call is affected by the previous ones.
You can try to prevent that by delaying the set_node() call, so that all nodes get to check thier suroundings before they start changeing.
Generic function for delaying stuff in minetest:
https://github.com/minetest/minetest/bl ... .txt#L4869
Thanks for the tip! added.
I am not very good at lua. I am learning though.

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

Re: [mod] game-of-life (will eventually be a game) [cells]

by markthesmeagol » Post

aaand the delay causes a crash. I can't interpret the error.

Code: Select all

2019-12-01 17:03:56: ERROR[Main]: ServerError: AsyncErr: ServerThread::run Lua: Runtime error from mod 'cells' in callback LuaABM::trigger(): .../src/minetest/bin/../builtin/common/after.lua:31: Invalid minetest.after invocation
2019-12-01 17:03:56: ERROR[Main]: stack traceback:
2019-12-01 17:03:56: ERROR[Main]: 	[C]: in function 'assert'
2019-12-01 17:03:56: ERROR[Main]: 	.../src/minetest/bin/../builtin/common/after.lua:31: in function 'after'
2019-12-01 17:03:56: ERROR[Main]: 	.../src/minetest/bin/../mods/cells/init.lua:25: in function </home/mark/src/minetest/bin/../mods/cells/init.lua:19>
I am not very good at lua. I am learning though.

User avatar
Skamiz Kazzarch
Member
Posts: 613
Joined: Fri Mar 09, 2018 20:34
GitHub: Skamiz
In-game: Skamiz
Location: la lojbaugag.

Re: [mod] game-of-life (will eventually be a game) [cells]

by Skamiz Kazzarch » Post

Code: Select all

minetest.after(0.5,minetest.set_node(pos,{name="air"}))
should be:

Code: Select all

minetest.after(0.5,minetest.set_node, pos, {name="air"})
You need to pas on the function and the arguments separately. The way you have it right now it tries to execute
minetest.set_node(pos,{name="air"})
imediatly and then pass on the return value to after()

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

Re: [mod] game-of-life (will eventually be a game) [cells]

by markthesmeagol » Post

thanks! now it works... sort of. For some reason, it doesn't seem to be respecting the rules of the automaton. say, I start with a hovering plus sign, it sometimes adds nodes, sometimes deletes them, and almost never does this in a symmetrical manner! I think I'll leave this here for today, and come back in a week.
I am not very good at lua. I am learning though.

User avatar
Skamiz Kazzarch
Member
Posts: 613
Joined: Fri Mar 09, 2018 20:34
GitHub: Skamiz
In-game: Skamiz
Location: la lojbaugag.

Re: [mod] game-of-life (will eventually be a game) [cells]

by Skamiz Kazzarch » Post

Another thing that may be causing issues is that the two ABMs aren't synchronized? See if the nodes appear and disappear at the same time.

ShadMOrdre
Member
Posts: 1118
Joined: Mon Dec 29, 2014 08:07
Location: USA

Re: [mod] game-of-life (will eventually be a game) [cells]

by ShadMOrdre » Post

I think that as long as you use ABMs, you will be limited in the performance improvements.

I haven't looked at the code for Flux, but what if you simply defined three nodes, a blue node, a red node, and an air node? All nodes are defined with the same def, except for name, description, and texture. Each node uses swapnode, on a timer, to self determine which state to be in, blue red, or air. I wouldn't use the default air node, instead defining a specific "air" node, that has the appropriate callbacks.

This would run far more efficiently that an ABM that must scan for the appropriate nodes, and then act on those nodes. Recently mentioned by paramat or Krock, on a different thread, was that the engine loops used by timers vs. ABMs is roughly an 8 to 31 ratio. Or something like that.

Shad

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

Re: [mod] game-of-life (will eventually be a game) [cells]

by paramat » Post

> The action callback is called for each node sequentialy, so that the next call is affected by the previous ones.

Critical point.

> You can try to prevent that by delaying the set_node() call,

That is a bad workaround, is unreliable and limits performance
So ABM is not a good way to do Game Of Life.
How large a volume is active? If not too large i would use the Lua Voxel Manipulator:
Read the volume into LVM.
Scan through the volume and using the rules, store the result in a flat array.
Once the whole volume is scanned, write the flat array into the LVM, then write the LVM to the map.

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests