Advertisement
TangentDelta

TangentAutomaton V.1.3

Oct 8th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 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.3
  7. --Notes:
  8. --Added a friendly way to select what to do
  9. --Cleaned up the code a bit
  10. --Removed some debugging stuff
  11. --To Do:
  12. --Better pattern centering
  13.  
  14. width = 64 --Set your desired width here!
  15. debug = false --Do you want debugging messages?
  16.  
  17. cellon="#"
  18. celloff=" "
  19.  
  20. seeddata={} --Put your seed information here!
  21.  
  22. height = width / 2
  23.  
  24. t=true
  25. f=false
  26.  
  27. states = {}
  28. rulegiven={}
  29. mt={}
  30. for i = 1, width do states[i] = 0 end
  31. states[width / 2] = 1
  32. output={}
  33.  
  34. for i=1,height+1 do --This loop sets up my matrix with a spacing character.
  35.     mt[i] = {}
  36.     for j=1,width+1 do
  37.         mt[i][j] = celloff
  38.     end
  39. end
  40.  
  41. function dectobin(input)
  42.     conversion={1,2,4,8,16,32,64,128} --I'll make this into 2^n eventually.
  43.     output={0,0,0,0,0,0,0,0} --The output table, keeping it like this for now.
  44.     i=8 --Starting number to decrement from
  45.     for h=1,8 do --It's 8 bit!
  46.         input=input-conversion[i] --Grabs the number to convert, and subtracts it from the lookup table.
  47.         if input <0 then --If it's less than 0 (negative)...
  48.             input=input+conversion[i] --...undo that subtraction.
  49.         else --If it isn't less than 0 (greater than or equal to)...
  50.             output[h]=1 --...set the output bit high (change "h" to "i" to swap endianess).
  51.         end
  52.         i=i-1 --The simple decrementer
  53.     end
  54. end
  55.  
  56. ruletable= --My second rule lookup table. I have no idea why I have two.
  57. {
  58.     {true,true,true},
  59.     {true,true,false},
  60.     {true,false,true},
  61.     {true,false,false},
  62.     {false,true,true},
  63.     {false,true,false},
  64.     {false,false,true},
  65.     {false,false,false}
  66. }
  67.  
  68. rule={} --Sets up the actual table the algorithm looks at.
  69.  
  70. function grabrule() --This function decodes the given rule.
  71.     userinput=io.read() --Gets the user's input.
  72.     dectobin(userinput) --Sends the input to get decoded into binary.
  73.     rulegiven=output --Takes that binary number and sticks it into "rulegiven".
  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.     for k=1,#rulegiven do --This will turn the binary into bollean logic.
  80.         if rulegiven[k]==1 then --If the bit is 1...
  81.         rulegiven[k]=true --...set it to true (rather pointless, since I could combine this with the next part of the program).
  82.         else
  83.             rulegiven[k]=false --This is rather pointless, since it's set to "false" by default.
  84.         end
  85.     end
  86.  
  87.     rulecounter=1 --A temporary number that helps with keeping the rule table from skipping ahead.
  88.  
  89.     for i=1,#rulegiven do --For as many rules as there are given...
  90.         if rulegiven[i]then --...if the state of the rule is true...
  91.             rule[rulecounter]=ruletable[i] --...look at the lookup table, and stick that value into "rule".
  92.             rulecounter=rulecounter+1 --Decrements the counter for the "rule" table.
  93.         end
  94.     end
  95. end
  96.  
  97. function grabseed() --This function asks for an input, and stores it to "state"
  98.     seedgiven={} --I always store "binary" data in a table.
  99.     print("Please enter a starting seed in binary!")
  100.     print("Note: This seed will be placed in the center of the algorithm.")
  101.     inputseed=io.read() --Asks for the seed.
  102.     for i=1,string.len(inputseed) do --This loop chops the user input up, and stuffs it into my table.
  103.         seedgiven[i]=string.sub(inputseed,i,i)
  104.     end
  105.     for i=1,#seedgiven do --Appearently, it's a string, and I need numbers.
  106.         if seedgiven[i]=="1" then --If the string is a "1"...
  107.             seedgiven[i]=1 --...make it into a number 1...
  108.         else
  109.             seedgiven[i]=0 --...else it's a 0.
  110.         end
  111.     end
  112.     bitshift=((width-#seedgiven)/2) --To find how much the data should be shifted, the lenght of the given seed is subtracted from the width, then divided by 2.
  113.     bitshift=math.floor(bitshift) --If it's an odd number, it gets rounded down.
  114.     j=bitshift --To keep the two numbers synced.
  115.     for i=1,#seedgiven do --This stores the given string into the "states" table.
  116.         states[j]=seedgiven[i]
  117.         j=j+1 --To keep j and i in sync.
  118.     end
  119.  
  120. end
  121.  
  122.  
  123. function compute_next_gen(index) --Here's where all the computation happens.
  124.     current_gen = {} --This table stores the current information to get computed.
  125.     for i = 1, #states do --This just stores the data from the current bit into the p, c, and n registers.
  126.         p = states[i - 1] == 1
  127.         c = states[i] == 1
  128.         n = states[i + 1] == 1
  129.         current_gen[i] = 0 --Once that's done, it sets the gen back to 0 to do the actual computation.
  130.         for j = 1, #rule do --It computes for as many rules it is given.
  131.             if (p == rule[j][1] and c == rule[j][2] and n == rule[j][3]) then --If it complies with the rules...
  132.                 current_gen[i] = 1 --...set the current cell to an "on" state.
  133.                 mt[index+1][i + 1]=cellon --Stores it in my matrix.
  134.             end
  135.         end
  136.     end
  137.     states = current_gen
  138. end
  139.  
  140. print("Please take a look at the top of this script to configure it!")
  141.  
  142. function interface()
  143.     print("Would you like to input a seed?(y/n)")
  144.     input=io.read()
  145.     if input=="y" then
  146.         grabseed()
  147.     else
  148.         print("Would you like to use a randomly generated seed?(y/n)")
  149.         input=io.read()
  150.         if input=="y" then
  151.             for i=1,width do
  152.                 states[i]=math.random(0,1)
  153.             end
  154.         else
  155.             print("Going with standard seed...")
  156.         end
  157.     end
  158.  
  159.     print("Please enter the width to run for.")
  160.     width=io.read()
  161.     width=math.floor(width)
  162.  
  163.     print("Please enter a rule in decimal.")
  164.     grabrule()
  165. end
  166.  
  167. interface()
  168.  
  169. for i=1,width do
  170.     if states[i]==1 then
  171.         mt[1][i+1]=cellon
  172.     else
  173.         mt[1][i+1]=celloff
  174.     end
  175. end
  176.  
  177. for i = 1, height do --This runs the computation function for the desired height.
  178.     compute_next_gen(i)
  179. end
  180.  
  181. for a=1,height do --This loop actually prints out my matrix, displaying the pretty pattern!
  182.     for b=1,width do
  183.         io.write(mt[a][b])
  184.     end
  185.     io.write("\n")
  186. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement