Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --One Dimensional Cellular Automaton
- --Adapted from "dominic"'s Lua CA written to use LOVE.
- --Original version can be found here: http://blog.signalsondisplay.com/?p=60
- --The only thing I borrowed from his version is the algorithm computer.
- -----------------------------------------------------------------------------
- --Tangent Automaton V. 1.2.2
- --Notes:
- --Added the ability to define a custom seed to start with.
- width = 64 --Set your desired width here!
- debug = false --Do you want debugging messages?
- random = false --Start with a randomly generated seed?
- seed = true --Start with a pre-defined seed?
- center = false --Start with one center cell?
- seeddata={} --Put your seed information here!
- height = width / 2
- t=true
- f=false
- states = {}
- rulegiven={}
- mt={}
- for i = 1, width do states[i] = 0 end
- states[width / 2] = 1
- output={}
- ruletable= --The rule lookup table.
- {
- {t,t,t},
- {t,t,f},
- {t,f,t},
- {t,f,f},
- {f,t,t},
- {f,t,f},
- {f,f,t},
- {f,f,f}
- }
- function dectobin(input)
- conversion={1,2,4,8,16,32,64,128} --I'll make this into 2^n eventually.
- output={0,0,0,0,0,0,0,0} --The output table, keeping it like this for now.
- i=8 --Starting number to decrement from
- for h=1,8 do --It's 8 bit!
- input=input-conversion[i] --Grabs the number to convert, and subtracts it from the lookup table.
- if debug then print(input) end
- if input <0 then --If it's less than 0 (negative)...
- input=input+conversion[i] --...undo that subtraction.
- else --If it isn't less than 0 (greater than or equal to)...
- output[h]=1 --...set the output bit high (change "h" to "i" to swap endianess).
- end
- i=i-1 --The simple decrementer
- end
- end
- ruletable= --My second rule lookup table. I have no idea why I have two.
- {
- {true,true,true},
- {true,true,false},
- {true,false,true},
- {true,false,false},
- {false,true,true},
- {false,true,false},
- {false,false,true},
- {false,false,false}
- }
- rule={} --Sets up the actual table the algorithm looks at.
- function grabrule() --This function decodes the given rule.
- userinput=io.read() --Gets the user's input.
- dectobin(userinput) --Sends the input to get decoded into binary.
- rulegiven=output --Takes that binary number and sticks it into "rulegiven".
- if debug then print(#rulegiven) end
- --print(string.len(userinput)) --I might re-implement this later. For now, You'll have to use the decimal system.
- --for i=1,string.len(userinput) do
- -- rulegiven[i]=string.sub(userinput,i,i)
- --end
- if #rulegiven~=8 then --Rather pointless warning since an 8-bit number is ALWAYS created...
- print("You must enter an 8-bit number!")
- end
- if debug then --more debugging
- for j=1,#rulegiven do
- io.write(rulegiven[j].." ")
- end
- io.write("\n")
- end
- for k=1,#rulegiven do --This will turn the binary into bollean logic.
- if rulegiven[k]==1 then --If the bit is 1...
- rulegiven[k]=true --...set it to true (rather pointless, since I could combine this with the next part of the program).
- else
- rulegiven[k]=false --This is rather pointless, since it's set to "false" by default.
- end
- end
- rulecounter=1 --A temporary number that helps with keeping the rule table from skipping ahead.
- for i=1,#rulegiven do --For as many rules as there are given...
- if rulegiven[i]then --...if the state of the rule is true...
- rule[rulecounter]=ruletable[i] --...look at the lookup table, and stick that value into "rule".
- rulecounter=rulecounter+1 --Decrements the counter for the "rule" table.
- end
- end
- end
- function grabseed() --This function asks for an input, and stores it to "state"
- seedgiven={} --I always store "binary" data in a table.
- print("Please enter a starting seed in binary!")
- print("Note: This seed will be placed in the center of the algorithm.")
- inputseed=io.read() --Asks for the seed.
- for i=1,string.len(inputseed) do --This loop chops the user input up, and stuffs it into my table.
- seedgiven[i]=string.sub(inputseed,i,i)
- end
- for i=1,#seedgiven do --Appearently, it's a string, and I need numbers.
- if seedgiven[i]=="1" then --If the string is a "1"...
- seedgiven[i]=1 --...make it into a number 1...
- else
- seedgiven[i]=0 --...else it's a 0.
- end
- end
- 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.
- bitshift=math.floor(bitshift) --If it's an odd number, it gets rounded down.
- j=bitshift --To keep the two numbers synced.
- for i=1,#seedgiven do --This stores the given string into the "states" table.
- states[j]=seedgiven[i]
- j=j+1 --To keep j and i in sync.
- end
- end
- function compute_next_gen(index) --Here's where all the computation happens.
- --mt[1][(#states / 2)+1]="#"
- current_gen = {} --This table stores the current information to get computed.
- for i = 1, #states do --This just stores the data from the current bit into the p, c, and n registers.
- p = states[i - 1] == 1
- c = states[i] == 1
- n = states[i + 1] == 1
- current_gen[i] = 0 --Once that's done, it sets the gen back to 0 to do the actual computation.
- for j = 1, #rule do --It computes for as many rules it is given.
- if (p == rule[j][1] and c == rule[j][2] and n == rule[j][3]) then --If it complies with the rules...
- current_gen[i] = 1 --...set the current cell to an "on" state.
- mt[index+1][i + 1]="#" --Stores it in my matrix.
- end
- end
- end
- states = current_gen
- end
- print("Please take a look at the top of this script to configure it!")
- if seed then
- grabseed()
- grabrule()
- elseif center then
- grabrule()
- elseif random then
- grabrule()
- end
- for i=1,height+1 do --This loop sets up my matrix with a spacing character.
- mt[i] = {}
- for j=1,width+1 do
- mt[i][j] = " "
- end
- end
- --for i=1,width do
- -- states[i]=math.random(0,1)
- --end
- for i=1,width do
- if states[i]==1 then
- mt[1][i+1]="#"
- else
- mt[1][i+1]=" "
- end
- end
- if debug then print("This automaton uses "..#rule.." rules.") end --Disregard this, it's for debugging.
- if debug then
- for a=1,#rule do
- for b=1,3 do
- if rule[a][b] then
- io.write("true")
- else
- io.write("false")
- end
- end
- io.write("\n")
- end
- end
- for i = 1, height do --This runs the computation function for the desired height.
- compute_next_gen(i)
- end
- for a=1,height do --This loop actually prints out my matrix, displaying the pretty pattern!
- for b=1,width do
- io.write(mt[a][b])
- end
- io.write("\n")
- end
Add Comment
Please, Sign In to add comment