NewCA.lua

For scripts to aid with computation or simulation in cellular automata.
wildmyron
Posts: 1542
Joined: August 9th, 2013, 12:45 am
Location: Western Australia

Re: NewCA.lua

Post by wildmyron » October 14th, 2019, 12:35 am

Thank you for your responses Andrew.
Andrew wrote:
October 12th, 2019, 7:22 pm
* The biggest change is that *all* of the variables declared at the top are now global, so your RandomFill function can simply set dirty = true after calling RememberCurrentState(), and you now have access to pastecells, pasteht, pastewd, etc so you can redefine FlipPaste and RotatePaste to do whatever you like. This is a somewhat dangerous change so I've added a note of caution at the end of the "Script functions" section.
OK, I'll work with this version for a while and see how it goes. I might try to allow PCA.lua to continue working with the current version as well (without the additional functionality) so that it's easy for other's to use it with current Golly 3.3.

After my previous post I did have an idea about how NewCA.lua could support editing function overrides without making everything global. The mechanism would be to have an EditFunctionCaller. When the User CA wants to edit the pattern it calls the EditFunctionCaller, passing it a reference to EditPattern (the editing function it wants to call). When EditPattern completes it returns a value(s) indicating whether the state changed (and an Undo checkpoint should be stored) and whether the pattern changed. EditFunctionCaller then updates the private state which needs to be changed. Of course, functions that modify the PastePattern would need a different caller, and some other custom CA might need to modify other parts of the local state. So you may prefer to go with the more flexible but slightly dangerous option in the new version of NewCA.lua. (Probably you've thought about this but I thought it's worthwhile having a discussion about it.)
The mechanism to change and show the Files directory via the startup script is nice, but when the user CA is quit and the original Files directory is restored the open/close state of all the subfolders is lost.
Unfortunately I can't find any way to avoid that. The best solution would be to just have g.setoption("showfiles",1) in your startup script but don't call g.setdir. That way NewCA.lua won't change the state of your files panel when it exits.
That seems reasonable. I've changed my startup scripts to do that instead.
The 5S project (Smallest Spaceships Supporting Specific Speeds) is now maintained by AforAmpere. The latest collection is hosted on GitHub and contains well over 1,000,000 spaceships.

Semi-active here - recovering from a severe case of LWTDS.

User avatar
Andrew
Moderator
Posts: 919
Joined: June 2nd, 2009, 2:08 am
Location: Melbourne, Australia
Contact:

Re: NewCA.lua

Post by Andrew » October 15th, 2019, 7:00 pm

wildmyron wrote:
October 14th, 2019, 12:35 am
After my previous post I did have an idea about how NewCA.lua could support editing function overrides without making everything global. The mechanism would be to have an EditFunctionCaller. ... Of course, functions that modify the PastePattern would need a different caller, and some other custom CA might need to modify other parts of the local state.
Yep, that's the problem with providing various callers. It's hard to predict what future script writers might want to customize. It's a lot more flexible just to make most of the variables global. It does mean that it's easier to stuff things up and see weird results or (more likely) a Lua error of some sort. It also means we have to avoid changing the names of those variables in future releases, but that's currently true for all the global function names. I think the greater flexibility is worth the price of those drawbacks.

I've uploaded a newer version of NewCA.lua to my website with a better version of RandomFill that fills with all live states rather than only state 1, so no need to redefine that function in PCA.lua. I've also committed this new version to the sf repo so it will be included in any future release of Golly. I'll keep the version on my website in sync with the sf version so I don't see any problems using the new features in PCA.lua or any other scripts you might write. Just tell people to download NewCA.lua from my website (or sf) and replace the version in Scripts/Lua/gplus because that will eventually become the official version.
Use Glu to explore CA rules on non-periodic tilings: DominoLife and HatLife

User avatar
Andrew
Moderator
Posts: 919
Joined: June 2nd, 2009, 2:08 am
Location: Melbourne, Australia
Contact:

Re: NewCA.lua

Post by Andrew » July 15th, 2020, 8:01 am

Download and unzip the following file to try out Mark Niemiec's Integer Life CA in Golly:
http://www.trevorrow.com/golly/IntegerLife.zip

See Mark's description here:
https://conwaylife.com/forums/viewtopic ... 47#p100447

Feel free to modify the color scheme defined in the SetColors function near the end of IntegerLife.lua. Or the random pattern created by the HandleKey function.
Use Glu to explore CA rules on non-periodic tilings: DominoLife and HatLife

PraseodymiumSpike
Posts: 11
Joined: February 8th, 2023, 2:44 am

Help with NewCA.lua

Post by PraseodymiumSpike » February 8th, 2023, 3:08 am

Hi, I'm trying to create a script with NewCA.lua in Golly. It asks to provide part of the `... code to create the next pattern in newcells ...` as part of the function `NextPattern`. However, this function does not take in a parameter for the rule being used, so I don't see how to access that information from inside the function. Is `ParseRule` supposed to set a global variable containing the rule information like it appears to do in Margolus.lua? Someone suggested using `g.getrule()`, but I don't know if that will work in this context or not. Margolus.lua and 1D.lua don't use it anywhere. Also, I think that the template for using NewCA.lua at https://golly.sourceforge.net/Help/lua.html#newca should be improved in general so people don't need to ask questions like this.

User avatar
Andrew
Moderator
Posts: 919
Joined: June 2nd, 2009, 2:08 am
Location: Melbourne, Australia
Contact:

Re: Help with NewCA.lua

Post by Andrew » February 9th, 2023, 5:42 pm

PraseodymiumSpike wrote:
February 8th, 2023, 3:08 am
Hi, I'm trying to create a script with NewCA.lua in Golly. It asks to provide part of the `... code to create the next pattern in newcells ...` as part of the function `NextPattern`. However, this function does not take in a parameter for the rule being used, so I don't see how to access that information from inside the function. Is `ParseRule` supposed to set a global variable containing the rule information like it appears to do in Margolus.lua?
Yes, ParseRule typically needs to set global info (in tables/variables/flags/whatever) for use in NextPattern. For example, in RandomLife.lua (see link in 1st post of this thread), ParseRule sets births, survivals, bpercentage, spercentage.
Also, I think that the template for using NewCA.lua at https://golly.sourceforge.net/Help/lua.html#newca should be improved in general so people don't need to ask questions like this.
Apologies if the documentation is not helping. Like most(?) programmers, I leave the docs to last and don't put as much effort into them as I should. I was sort of hoping that all the example scripts I wrote would be sufficient. If you need more help, please provide more details about the rule(s) you want to implement.
Use Glu to explore CA rules on non-periodic tilings: DominoLife and HatLife

PraseodymiumSpike
Posts: 11
Joined: February 8th, 2023, 2:44 am

Re: NewCA.lua

Post by PraseodymiumSpike » February 9th, 2023, 6:53 pm

Here are some of the more specific issues I had and some ideas to make it easier for future users. The function `ParseRule` does not indicate that it should be setting global variables at all, and considering that it's generally considered a bad idea to set global variables, that isn't something most people would think of doing. So perhaps, in the template, a line that reads "... code to set globals to be used in NextPattern ..." before the return statement would be useful. Also, it might be useful to specify which of the normal g.* commands are safe to use within ParseRule and within NextPattern. It's not clear whether it would be a bad idea to use, for example, `g.duplicate` in the `NextPattern` function. I also think the documentation could be improved by giving an example that more closely follows the template given. Perhaps, an example script that exactly follows the template and can decipher totalistic rules would be useful as it's easy to understand what it's trying to do, and it should be fairly easy to understand how the `ParseRule` and `NextPattern` functions actually work in that case.

User avatar
Andrew
Moderator
Posts: 919
Joined: June 2nd, 2009, 2:08 am
Location: Melbourne, Australia
Contact:

Re: NewCA.lua

Post by Andrew » February 9th, 2023, 8:18 pm

PraseodymiumSpike wrote:
February 9th, 2023, 6:53 pm
... So perhaps, in the template, a line that reads "... code to set globals to be used in NextPattern ..." before the return statement would be useful.
Good idea. I've added more comments to the template in the 1st post. If they're ok I'll do the same in the docs for the next Golly release.
Also, it might be useful to specify which of the normal g.* commands are safe to use within ParseRule and within NextPattern.
Hmm, there are over 100 g.* commands so I'm not going to go through all of them and state which are safe to use. If you think a particular command might be useful just try it and see if it works. Should be obvious if it doesn't!
I also think the documentation could be improved by giving an example that more closely follows the template given. Perhaps, an example script that exactly follows the template and can decipher totalistic rules would be useful ...
That's exactly what RandomLife.lua does (did you look at it?). Maybe I should include it in the next Golly release and add an "edit:" link to it in the docs so it will open in your preferred text editor.
Use Glu to explore CA rules on non-periodic tilings: DominoLife and HatLife

PraseodymiumSpike
Posts: 11
Joined: February 8th, 2023, 2:44 am

Re: NewCA.lua

Post by PraseodymiumSpike » February 9th, 2023, 8:43 pm

I did not actually look at RandomLife.lua. Sorry, I really should have looked through the thread before posting my question.

Post Reply