User:Evin/generalized wireworld

From LifeWiki
Jump to navigation Jump to search

A generalized wireworld rule is a normal rule where the cells are confined to wires. For example, wireworld is the confined version of /12/3.

A script to make generalized wireworld rules:

def rulestring(string):
  return "Wire_" + string.replace("/", "_")

transitions = {
  "0": "00000000",
  "1c": "01000000", "1e": "10000000",
  "2c": "01010000", "2e": "10100000", "2k": "10010000", "2a": "11000000", "2i": "10001000", "2n": "01000100",
  "3c": "01010100", "3e": "10101000", "3k": "10100100", "3a": "11100000", "3i": "01110000", "3n": "11010000", "3y": "10010100", "3q": "11000100", "3j": "11000010", "3r": "11001000",
  "4c": "01010101", "4e": "10101010", "4k": "11010010", "4a": "11110000", "4i": "11011000", "4n": "11000101", "4y": "11010100", "4q": "11100100", "4j": "11010100", "4r": "11101000", "4t": "11001001", "4w": "11011000", "4z": "11001100",
  "5c": "10101011", "5e": "11010101", "5k": "11010110", "5a": "11110001", "5i": "11111000", "5n": "11101001", "5y": "11011010", "5q": "11101100", "5j": "11110100", "5r": "11011100",
  "6c": "11111010", "6e": "11110101", "6k": "10111101", "6a": "11111100", "6i": "11011101", "6n": "11101110",
  "7c": "11111110", "7e": "01111111",
  "8": "11111111"
}

def mix(n, s1, s2, c1, c2):
  i = (n - s2) / (s1 - s2)
  return int((c1 * i) + (c2 * (1 - i)))

def parse(string):
  s = string.replace("1", "|1").replace("2", "|2").replace("3", "|3").replace("4", "|4").replace("5", "|5").replace("5", "|5").replace("7", "|7").replace("8", "|8").split("|")
  st = []
  for i in s:
    if not (i == ""):
      n = i[0]
      t = list(i)
      del t[0]
      if t == []:
        for j in transitions.keys():
          if j[0] == str(n):
            st.append(f"{j}")
      elif t[0] == "-":
        del t[0]
        ts = []
        for j in transitions.keys():
          if j[0] == str(n):
            ts.append(j[1])
        for j in ts:
          if not (i in t):
            st.append(f"{n}{j}")
      else:
        for j in t:
          st.append(f"{n}{j}")
  return st

def generate(string):
  b = string.split("/")[1]
  s = string.split("/")[0]
  c = int(string.split("/")[2])
  result = ""
  result += f"""@RULE Wire_B{b}_S{s}_C{c}\n\n************************************\n* RULE AUTOGENERATED BY CIRCUITGEN *\n************************************\n\n@TABLE\nn_states: {c + 1}\nneighborhood: Moore\nsymmetries: rotate4reflect\n\n"""
  var_dead = "0,1"
  for i in range(3, c + 1):
    var_dead += ","
    var_dead += str(i)
  for i in range(8):
    result += "var dead.{} = {}\n".format(i, "{" + var_dead + "}")
  for i in range(8):
    result += "var any.{} = {}\n".format(i, "{2," + var_dead + "}")
  result += "\n"
  bt = parse(b)
  st = parse(s)
  if not (bt == []):
    for i in bt:
      j = 0
      t = list(transitions[i].replace("1", "2"))
      for i in range(8):
        if t[i] == "0":
          t[i] = f"dead.{j}"
          j += 1
      result += "1,{},2\n".format(",".join(t))
  if not (st == []):
    for i in st:
      j = 0
      t = list(transitions[i].replace("1", "2"))
      for i in range(8):
        if t[i] == "0":
          t[i] = f"dead.{j}"
          j += 1
      result += "2,{},2\n".format(",".join(t))
  result += "2,any.0,any.1,any.2,any.3,any.4,any.5,any.6,any.7,3\n"
  result += f"{c},any.0,any.1,any.2,any.3,any.4,any.5,any.6,any.7,1\n"
  for i in range(3, c):
    result += f"{i},any.0,any.1,any.2,any.3,any.4,any.5,any.6,any.7,{i + 1}\n"
  result += f"\n@COLORS\n0 48 48 48\n"
  result += "1 0 128 255\n"
  for i in range(2, c + 1):
    result += f"{i} {mix(i, c + 1, 2, 0, 255)} {mix(i, c + 1, 2, 128, 255)} {255}\n"
  return result

def main(string):
  s = string.split("_")
  del s[0]
  s = "/".join(s)
  return generate(s)