chiark / gitweb /
Comments, licensing, etc
[bedbugs.git] / src / mungetable.pl
index 705a198ccfaddc3b691c7a8833c4f3a1a29d5ef4..369d861dcf852cfaf52d20e43c956d3cc8752f5a 100755 (executable)
@@ -1,5 +1,30 @@
 #! /usr/bin/perl -w
-# Hack to munge life.table for bedbugs.
+# This awful single-use script expands out a description of a
+# two-state cellular automaton in Golly table format (such as Conway's
+# Life) into an equivalent rule table that collapses the 32 "Bedstead"
+# states (16 on, 16 off) into the 2 automaton states while applying
+# the same rule.
+# It's slightly complicated because it's designed to work with
+# the output of Golly make-ruletable.cpp, which is somewhat optimised
+# using variables, but this script makes no attempt to optimise its
+# output. It uses poorly-named variables to do a lot of its work.
+
+# This file is part of Bedbugs.
+# Copyright (C) 2014 Jacob Nevins.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 my %vars = ();
 my $done_vars = 0;
@@ -13,12 +38,17 @@ while(<>) {
         die "Complicated variable" unless m/^var (.)=\{0,1\}$/;
         $vars{$1} = 1;
     } else {
+        # Assume this is a table rule.
         chomp;
         my @cells = split /,/;
         my %myvars = ();
+        # Only supports 8-neighbour automata
         die "Unexpected number of comma-separated items" unless @cells == 10;
         my @dirs = qw/c n ne e se s sw w nw/;
         if (!$done_vars) {
+            # Variables to match 32 states rather than 2
+            # Numbering of states highly tied to bedbugs.c output
+            # Crap naming scheme: z=off, a=on, q=either
             print "var zz={0," . join(",", 2..17) . "}\n";
             print "var aa={" . join(",", 18..33) . "}\n";
             foreach my $d (@dirs) {
@@ -30,6 +60,8 @@ while(<>) {
         }
         my $i = 0;
         while (my $dir = pop @dirs) {
+            # Assume each variable in the input is only referenced once
+            # per table row
             die "Duplicate var" if exists($myvars{$cells[$i]});
             if (exists($vars{$cells[$i]})) {
                 $myvars{$cells[$i]} = 1;