Page 1 of 1

Script to display evolution of 2D patterns in 3 dimensions

Posted: August 18th, 2018, 2:26 am
by SuperSupermario24
As the title suggests, I have created a script that uses 3D.lua to display the entire evolution of a two-dimensional pattern, using the Y dimension as the time dimension.

Here is the script:

Code: Select all

-- For 3D.lua (make sure to copy this line)

-- This script will use 3D.lua to display the evolution of a 
-- two-dimensional rule in a bounded grid, using Y as the time 
-- dimension.

-- It will generate up to 99 steps of any size in any two-state rule, 
-- and any kind of bounded grid with dimensions <= 100. (The size 
-- limitations are due to the maximum grid size of 3D.lua.)

-- How to use:
-- 1. In Golly's normal interface, create a pattern using a 2-state 
--  rule, in a bounded grid with any dimensions <= 100.
-- 2. Start 3D.lua, and run this script through it.
-- 3. Enter the number of steps to run.

-- By SuperSupermario24, 2018-08-18

---------------------------------------------------------------------

local g = golly()
local gp = require "gplus"

-- function that displays an error message and aborts
-- (can't use g.exit() with a string argument because that bugs 3D.lua)
local function raiseError(message)
    g.warn(message)
    g.exit()
end

---------------------------------------------------------------------

-- preliminary error-checking
if g.numstates() > 2 then
    raiseError("2D rule simulated cannot have more than 2 states.")
elseif g.getwidth() == 0 or g.getheight() == 0 then
    raiseError("2D universe must be bounded.")
elseif g.getwidth() > 100 or g.getheight() > 100 then
    raiseError("2D universe cannot have a dimension that is greater than 100.")
end

---------------------------------------------------------------------

-- clear 3D grid
NewPattern()

-- get number of steps and step size
local genstring = g.getstring("Enter steps and optional step size (separated by a space):", "99 1", "Enter generations:")
local numgens, step = gp.split(genstring, " ")

-- convert to numbers
numgens = tonumber(numgens)

-- set default step size to 1 if not entered
if step == nil then
    step = 1
else
    step = tonumber(step)
end

-- check if valid generation and step count
if numgens < 0 then
    g.warn("Cannot run fewer than 0 generations. Running 0 generations instead...")
    numgens = 0
elseif numgens > 99 then -- 99 is max because gen 0 is included
    g.warn("Cannot run more than 99 steps. Running 99 steps instead...")
    numgens = 99
end

---------------------------------------------------------------------

-- set grid size to highest out of universe width, height, and number of gens displayed
local gridsize = math.max(g.getwidth(), g.getheight(), numgens + 1)
SetGridSize(gridsize)

---------------------------------------------------------------------

-- set initial current plane to lowest available y value
local currenty = math.ceil(-gridsize / 2)

for i = 1, numgens + 1 do
    if not g.empty() then
        -- get all the cells in the current universe
        local array2d = g.getcells(g.getrect())
        
        -- create 3D cell array out of 2D cell array, using 0 as Y coordinate of all of them
        local array3d = {}
        for i = 1, #array2d / 2 do
            table.insert(array3d, {array2d[2*i - 1], 0, array2d[2*i]})
        end
        
        -- put the cells in current plane
        PutCells(array3d, 0, currenty, 0)
        
        -- if not finished, update the pattern and move current plane up
        if i ~= numgens + 1 then
            g.run(step)
            currenty = currenty + 1
        end
    end
end

---------------------------------------------------------------------

-- set current mode to hand cursor (otherwise it will be set to draw)
MoveMode()
The script uses Golly's regular 2D universe in order to generate each step behind the scenes. As such, you don't need to fiddle with the 3D editing interface to enter a pattern to simulate; just enter a pattern using the regular Golly interface, and then open 3D.lua and run this script through it.

It will run any pattern for up to 99 steps (due to the max grid size in 3D.lua being 100, and the fact that it will include generation 0), of any step size. It supports any two-state rule, in any bounded grid where the X and Y dimensions are both 100 or less. The type of bounded grid doesn't matter either; go nuts with your tori and Klein bottles and whatnot if you so desire.

Here are a few example pictures:
A glider moving across a torus
A glider moving across a torus
3D_1.png (93.93 KiB) Viewed 4187 times
A spacefiller running for 72 generations
A spacefiller running for 72 generations
3D_2.png (152.24 KiB) Viewed 4187 times
Photons colliding in Seeds
Photons colliding in Seeds
3D_3.png (238.68 KiB) Viewed 4187 times
Let me know what you guys think, or if there are any problems with the script.

UPDATE 1: I've added the ability to enter a custom step size, so that multiple generations may be run per step. This effectively means that the maximum generation count is now theoretically unbounded, although obviously not all of them will be displayed.

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 18th, 2018, 8:27 am
by Saka
Neat! This is really cool!
What about a step setting so you can do multiple generations per layer?

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 18th, 2018, 2:06 pm
by SuperSupermario24
That's actually a really good idea. I went ahead and updated the script to support that, as well as the first post to reflect it.

Here's an example, using a glider going across a torus with a step size of 16:
Glider going across a torus with step size 16
Glider going across a torus with step size 16
3D_4.png (72.43 KiB) Viewed 4143 times
Thanks for the idea :D

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 19th, 2018, 2:05 pm
by Gamedziner
I wonder if you could make art with this? For example, pillars could be made with blinkers.

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 20th, 2018, 5:20 am
by Saka
Gamedziner wrote:I wonder if you could make art with this? For example, pillars could be made with blinkers.
You definitely can. This is my best "piece" so far in my opinion. It's an LTL spaceship (Though I forgot what ship it is) at step 1 for 99 generations
Image

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 20th, 2018, 10:26 am
by gameoflifemaniac
When pasting Golly doesn't recognize the clipboard as a script, so Golly tries to paste it as a pattern. Help!

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 20th, 2018, 3:26 pm
by calcyman
gameoflifemaniac wrote:When pasting Golly doesn't recognize the clipboard as a script, so Golly tries to paste it as a pattern. Help!
Run Clipboard rather than Open Clipboard

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 20th, 2018, 7:18 pm
by Saka
calcyman wrote:
gameoflifemaniac wrote:When pasting Golly doesn't recognize the clipboard as a script, so Golly tries to paste it as a pattern. Help!
Run Clipboard rather than Open Clipboard
To add to this, make sure you're using the script in 3D.lua.

Re: Script to display evolution of 2D patterns in 3 dimensions

Posted: August 22nd, 2018, 2:27 am
by gameoflifemaniac
calcyman wrote:
gameoflifemaniac wrote:When pasting Golly doesn't recognize the clipboard as a script, so Golly tries to paste it as a pattern. Help!
Run Clipboard rather than Open Clipboard
I'm dumb