Advertisement
TangentDelta

Tangent Automaton

Oct 5th, 2012
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1. --One Dimensional Cellular Automaton
  2. --Adapted from "dominic"'s Lua CA written to use LOVE.
  3. --Original version can be found here: http://blog.signalsondisplay.com/?p=60
  4. --The only thing I borrowed from his version is the algorithm computer.
  5. -----------------------------------------------------------------------------
  6. --Tangent Automaton V. 1.0
  7.  
  8. width = 128 --Set your desired width here!
  9. height = width / 2
  10.  
  11. t=true
  12. f=false
  13.  
  14. debug=false --Do you want debugging messages?
  15.  
  16. states = {}
  17. rulegiven={}
  18. mt={}
  19. for i = 1, width do states[i] = 0 end
  20. states[width / 2] = 1
  21. output={}
  22.  
  23. ruletable= --The rule lookup table.
  24. {
  25.     {t,t,t},
  26.     {t,t,f},
  27.     {t,f,t},
  28.     {t,f,f},
  29.     {f,t,t},
  30.     {f,t,f},
  31.     {f,f,t},
  32.     {f,f,f}
  33. }
  34.  
  35. function dectobin(input)
  36.     conversion={1,2,4,8,16,32,64,128} --I'll make this into 2^n eventually.
  37.     output={0,0,0,0,0,0,0,0} --The output table, keeping it like this for now.
  38.     i=8 --Starting number to decrement from
  39.     for h=1,8 do --It's 8 bit!
  40.         input=input-conversion[i] --Grabs the number to convert, and subtracts it from the lookup table.
  41.         if debug then print(input) end
  42.         if input <0 then --If it's less than 0 (negative)...
  43.             input=input+conversion[i] --...undo that subtraction.
  44.         else --If it isn't less than 0 (greater than or equal to)...
  45.             output[h]=1 --...set the output bit high (change "h" to "i" to swap endianess).
  46.         end
  47.         i=i-1 --The simple decrementer
  48.     end
  49. end
  50.  
  51. ruletable= --My second rule lookup table. I have no idea why I have two.
  52. {
  53.     {true,true,true},
  54.     {true,true,false},
  55.     {true,false,true},
  56.     {true,false,false},
  57.     {false,true,true},
  58.     {false,true,false},
  59.     {false,false,true},
  60.     {false,false,false}
  61. }
  62.  
  63. rule={} --Sets up the actual table the algorithm looks at.
  64.  
  65. function grabrule() --This function decodes the given rule.
  66.     userinput=io.read() --Gets the user's input.
  67.     dectobin(userinput) --Sends the input to get decoded into binary.
  68.     rulegiven=output --Takes that binary number and sticks it into "rulegiven".
  69.     if debug then print(#rulegiven) end
  70.     --print(string.len(userinput)) --I might re-implement this later. For now, You'll have to use the decimal system.
  71.     --for i=1,string.len(userinput) do
  72.     --  rulegiven[i]=string.sub(userinput,i,i)
  73.     --end
  74.  
  75.     if #rulegiven~=8 then --Rather pointless warning since an 8-bit number is ALWAYS created...
  76.         print("You must enter an 8-bit number!")
  77.     end
  78.  
  79.     if debug then --more debugging
  80.         for j=1,#rulegiven do
  81.             io.write(rulegiven[j].." ")
  82.         end
  83.         io.write("\n")
  84.     end
  85.  
  86.     for k=1,#rulegiven do --This will turn the binary into bollean logic.
  87.         if rulegiven[k]==1 then --If the bit is 1...
  88.         rulegiven[k]=true --...set it to true (rather pointless, since I could combine this with the next part of the program).
  89.         else
  90.             rulegiven[k]=false --This is rather pointless, since it's set to "false" by default.
  91.         end
  92.     end
  93.  
  94.     rulecounter=1 --A temporary number that helps with keeping the rule table from skipping ahead.
  95.  
  96.     for i=1,#rulegiven do --For as many rules as there are given...
  97.         if rulegiven[i]then --...if the state of the rule is true...
  98.             rule[rulecounter]=ruletable[i] --...look at the lookup table, and stick that value into "rule".
  99.             rulecounter=rulecounter+1 --Decrements the counter for the "rule" table.
  100.         end
  101.     end
  102. end
  103.  
  104. function compute_next_gen(index) --Here's where all the computation happens.
  105.     current_gen = {} --This table stores the current information to get computed.
  106.     for i = 1, #states do --This just stores the data from the current bit into the p, c, and n registers.
  107.         p = states[i - 1] == 1
  108.         c = states[i] == 1
  109.         n = states[i + 1] == 1
  110.         current_gen[i] = 0 --Once that's done, it sets the gen back to 0 to do the actual computation.
  111.         for j = 1, #rule do --It computes for as many rules it is given.
  112.             if (p == rule[j][1] and c == rule[j][2] and n == rule[j][3]) then --If it complies with the rules...
  113.                 current_gen[i] = 1 --...set the current cell to an "on" state.
  114.                 mt[index][i]="#" --Stores it in my matrix.
  115.             end
  116.         end
  117.     end
  118.     states = current_gen
  119. end
  120.  
  121. grabrule()
  122.  
  123. for i=1,height do --This loop sets up my matrix with a spacing character.
  124.     mt[i] = {}
  125.     for j=1,width do
  126.         mt[i][j] = "_"
  127.     end
  128. end
  129.  
  130. if debug then print("This automaton uses "..#rule.." rules.") end --Disregard this, it's for debugging.
  131. if debug then
  132.     for a=1,#rule do
  133.         for b=1,3 do
  134.             if rule[a][b] then
  135.                 io.write("true")
  136.             else
  137.                 io.write("false")
  138.             end
  139.         end
  140.         io.write("\n")
  141.     end
  142. end
  143.  
  144. for i = 1, height do --This runs the computation function for the desired height.
  145.     compute_next_gen(i)
  146. end
  147.  
  148. for a=1,height do --This loop actually prints out my matrix, displaying the pretty pattern!
  149.     for b=1,width do
  150.         io.write(mt[a][b])
  151.     end
  152.     io.write("\n")
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement