Code: Select all
local g = golly()
require "gplus.NewCA"
SCRIPT_NAME = "MyCA" -- must match the name of your .lua file
DEFAULT_RULE = "XXX" -- must be a valid rule in your CA
RULE_HELP = "HTML code describing the rules allowed by your CA."
function ParseRule(newrule)
-- Parse the given rule string.
-- If valid then return nil, the canonical rule string,
-- the width and height of the grid, and the number of states.
-- If not valid then just return an appropriate error message.
-- parsing code for your rule syntax goes here
-- (and usually sets some global info for use in NextPattern,
-- such as tables with birth/survival info)
return nil, canonrule, wd, ht, numstates
-- Note that wd and ht must be from 4 to 4000,
-- and numstates must be from 2 to 256.
end
function NextPattern(currcells, minx, miny, maxx, maxy)
-- Create the next pattern using the given parameters:
-- currcells is a non-empty cell array containing the current pattern.
-- minx, miny, maxx, maxy are the cell coordinates of the grid edges.
-- This function must return the new pattern as a cell array.
local newcells = {}
-- code to create the next pattern goes here
-- (typically uses the global info set in ParseRule to help
-- determine which live cells survive and which empty cells
-- become births)
return newcells
-- Note that currcells and newcells are one-state cell arrays if
-- g.numstates() == 2, otherwise they are both multi-state.
end
StartNewCA()
- Full Golly-like editing (cut/copy/paste/select/etc) including unlimited undo/redo. All of Golly's usual cursor modes are supported.
- The interface is essentially the same as Golly. The only really new feature is a combined menu+tool bar that appears under the edit bar. It has some menus (File, Edit, View) on the left side and various buttons (Start, Reset, etc) on the right side, along with a slider for controlling the step size.
- Patterns can be saved as .rle files. These files can be opened via the File menu's Open Pattern command. You can also open a pattern stored in the clipboard via the Open Clipboard command (or by typing shift-O).
- Users can get help in various ways (type H or click the "?" button) and see documentation about the required rule syntax, the menu commands, a list of all the keyboard shortcuts, etc.
- Your script can run other Lua scripts to do searches, build patterns, etc. In particular, users can specify a script to be run whenever your script starts up. A startup script can do lots of customizations (add/modify keyboard shortcuts, change the color scheme, specify a different DEFAULT_RULE, add aliases for specific rules, etc).
To implement a new CA you'll typically need to write a couple of hundred lines of Lua code to parse the rule strings allowed by your CA and to calculate the next pattern. To help with this task I've written a number of example scripts that you can download and examine (listed roughly in order of increasing complexity):
http://www.trevorrow.com/golly/RandomLife.lua
http://www.trevorrow.com/golly/BSFKL.lua
http://www.trevorrow.com/golly/CoordCA.lua
TIP: These scripts can be run from any location, but I strongly advise that you put each file in a separate folder.
Golly 3.3 also comes with a couple of examples in Scripts/Lua: 1D.lua and Margolus.lua. I'll discuss all the examples in later posts.