Use this script by Saka and SuperSupermmario24 to generate a ruletable.
Code: Select all
-- altRuleGen.lua
-- Script to generate alternating non-totalistic rules.
-- (Note: this makes no attempt to canonize the rulestrings.)
--
-- Original Python script by Saka.
--
-- Translated to Lua and then modified to include
-- non-totalistic rules by SuperSupermario24.
--
-- NOTE: NEVER ENTER B0
local g = golly()
local gp = require "gplus"
local r = g.getstring("Enter rule, in format Bx_Sx--Bx_Sx","B3_S2-i34q--B3_S23")
local rule1, rule2 = gp.split(r, "--")
local br1, sr1 = gp.split(rule1, "_")
br1 = string.gsub(br1, "B", "")
sr1 = string.gsub(sr1, "S", "")
local br2, sr2 = gp.split(rule2, "_")
br2 = string.gsub(br2, "B", "")
sr2 = string.gsub(sr2, "S", "")
trans0 = {
",0,0,0,0,0,0,0,0,y"
}
trans1 = {
["c"] = ",0,0,0,0,0,0,0,x,y",
["e"] = ",x,0,0,0,0,0,0,0,y"
}
trans2 = {
["c"] = ",0,x,0,0,0,0,0,x,y",
["e"] = ",x,0,0,0,0,0,x,0,y",
["k"] = ",0,0,x,0,0,0,0,x,y",
["a"] = ",x,0,0,0,0,0,0,x,y",
["i"] = ",x,0,0,0,x,0,0,0,y",
["n"] = ",0,0,0,x,0,0,0,x,y"
}
trans3 = {
["c"] = ",0,x,0,0,0,x,0,x,y",
["e"] = ",x,0,x,0,0,0,x,0,y",
["k"] = ",0,0,x,0,x,0,0,x,y",
["a"] = ",x,0,0,0,0,0,x,x,y",
["i"] = ",x,x,0,0,0,0,0,x,y",
["n"] = ",0,x,x,0,0,0,0,x,y",
["y"] = ",0,x,0,0,x,0,0,x,y",
["q"] = ",x,0,0,x,0,0,0,x,y",
["j"] = ",x,0,x,0,0,0,0,x,y",
["r"] = ",x,0,0,0,x,0,0,x,y"
}
trans4 = {
["c"] = ",0,x,0,x,0,x,0,x,y",
["e"] = ",x,0,x,0,x,0,x,0,y",
["k"] = ",0,x,0,0,x,0,x,x,y",
["a"] = ",x,x,x,0,0,0,0,x,y",
["i"] = ",0,x,x,0,0,0,x,x,y",
["n"] = ",x,x,0,0,0,x,0,x,y",
["y"] = ",0,x,x,0,0,x,0,x,y",
["q"] = ",x,0,0,x,0,0,x,x,y",
["j"] = ",x,0,x,0,x,0,0,x,y",
["r"] = ",x,0,x,0,0,0,x,x,y",
["t"] = ",x,x,0,0,x,0,0,x,y",
["w"] = ",x,0,x,x,0,0,0,x,y",
["z"] = ",x,0,0,x,x,0,0,x,y"
}
trans5 = {
["c"] = ",x,0,x,x,x,0,x,0,y",
["e"] = ",0,x,0,x,x,x,0,x,y",
["k"] = ",x,x,0,x,0,x,x,0,y",
["a"] = ",0,x,x,x,x,x,0,0,y",
["i"] = ",0,0,x,x,x,x,x,0,y",
["n"] = ",x,0,0,x,x,x,x,0,y",
["y"] = ",x,0,x,x,0,x,x,0,y",
["q"] = ",0,x,x,0,x,x,x,0,y",
["j"] = ",0,x,0,x,x,x,x,0,y",
["r"] = ",0,x,x,x,0,x,x,0,y"
}
trans6 = {
["c"] = ",x,0,x,x,x,x,x,0,y",
["e"] = ",0,x,x,x,x,x,0,x,y",
["k"] = ",x,x,0,x,x,x,x,0,y",
["a"] = ",0,x,x,x,x,x,x,0,y",
["i"] = ",0,x,x,x,0,x,x,x,y",
["n"] = ",x,x,x,0,x,x,x,0,y"
}
trans7 = {
["c"] = ",x,x,x,x,x,x,x,0,y",
["e"] = ",0,x,x,x,x,x,x,x,y"
}
trans8 = {
",x,x,x,x,x,x,x,x,y"
}
config0 = {1}
config1 = {"c", "e"}
config2 = {"c", "e", "k", "a", "i", "n"}
config3 = {"c", "e", "k", "a", "i", "n", "y", "q", "j", "r"}
config4 = {"c", "e", "k", "a", "i", "n", "y", "q", "j", "r", "t", "w", "z"}
local function getChar(a, l)
return string.sub(a, l, l)
end
transitions = {
["0"] = trans0,
["1"] = trans1,
["2"] = trans2,
["3"] = trans3,
["4"] = trans4,
["5"] = trans5,
["6"] = trans6,
["7"] = trans7,
["8"] = trans8
}
configs = {
["0"] = config0,
["1"] = config1,
["2"] = config2,
["3"] = config3,
["4"] = config4,
["5"] = config3,
["6"] = config2,
["7"] = config1,
["8"] = config0
}
local function fixTransition(s, n)
if(n == 1) then
s = string.gsub(s, "x", 1)
s = string.gsub(s, "y", 2)
elseif(n == 2) then
s = string.gsub(s, "x", 2)
s = string.gsub(s, "y", 1)
end
return s
end
local function genTransitions(B, S, n)
t = {}
for i in string.gmatch(B, "%d[%-%a]*") do
if(getChar(i, 2) == "-") then
local t2 = {table.unpack(configs[getChar(i, 1)])}
for j = 3, string.len(i) do
for k = 1, #t2 do
if(getChar(i, j) == t2[k]) then table.remove(t2, k) end
end
end
for j = 1, #t2 do
table.insert(t, "0"..fixTransition(transitions[getChar(i, 1)][t2[j]], n))
end
elseif(getChar(i, 2) == "") then
for j = 1, #configs[i] do
table.insert(t, "0"..fixTransition(transitions[i][configs[i][j]], n))
end
else
for j = 2, string.len(i) do
table.insert(t, "0"..fixTransition(transitions[getChar(i, 1)][getChar(i, j)], n))
end
end
end
for i in string.gmatch(S, "%d[%-%a]*") do
if(getChar(i, 2) == "-") then
local t2 = {table.unpack(configs[getChar(i, 1)])}
for j = 3, string.len(i) do
for k = 1, #t2 do
if(getChar(i, j) == t2[k]) then table.remove(t2, k) end
end
end
for j = 1, #t2 do
table.insert(t, n..fixTransition(transitions[getChar(i, 1)][t2[j]], n))
end
elseif(getChar(i, 2) == "") then
for j = 1, #configs[i] do
table.insert(t, n..fixTransition(transitions[i][configs[i][j]], n))
end
else
for j = 2, string.len(i) do
table.insert(t, n..fixTransition(transitions[getChar(i, 1)][getChar(i, j)], n))
end
end
end
return t
end
local function makeRuleTable(ruleName,nStates,neighborhood,symmetries,transitionsList)
local rule = "@RULE "..ruleName.."\n\nAutomatically generated by a Lua script.\n\n"
local ruletable = "@TABLE\n".."n_states:"..tostring(nStates).."\n".."neighborhood:"..neighborhood.."\n".."symmetries:"..symmetries.."\n"
local transitions = "\n"
for i = 1, #transitionsList do
transitions = transitions..tostring(transitionsList[i]).."\n"
end
return rule..ruletable..transitions
end
local trans = {}
local transr1 = genTransitions(br1, sr1, 1)
local transr2 = genTransitions(br2, sr2, 2)
table.insert(trans, "var a={0,1,2}")
table.insert(trans, "var b=a")
table.insert(trans, "var c=a")
table.insert(trans, "var d=a")
table.insert(trans, "var e=a")
table.insert(trans, "var f=a")
table.insert(trans, "var g=a")
table.insert(trans, "var h=a")
table.insert(trans, "#Rule 1")
for i = 1, #transr1 do
table.insert(trans, transr1[i])
end
table.insert(trans, "1,a,b,c,d,e,f,g,h,0")
table.insert(trans, "#Rule 2")
for i = 1, #transr2 do
table.insert(trans, transr2[i])
end
table.insert(trans, "2,a,b,c,d,e,f,g,h,0")
local theRule = makeRuleTable(r,3,"Moore","rotate4reflect",trans)
local function fileExists(name)
local a
local f = io.open(name, "r")
if f == nil then
return false
else
f:close()
return true
end
end
local a = 0
local function saveRule(name, ruleFile)
local ruledir = g.getdir("rules")
local filename = ruledir..name..".rule"
if not fileExists(filename) then
a = 1
local file = assert(io.open(filename, "w"), "Unable to create rule table:\n"..filename)
file:write(ruleFile)
file:close()
end
end
saveRule(r, theRule)
g.setrule(r)
if a == 1 then
g.show("Created and switched to rule "..r..".")
else
g.show("Switched to rule "..r..".")
end