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()
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:
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.