View difference between Paste ID: 8CBaCqbz and Ru3nfViz
SHOW: | | - or go back to the newest paste.
1
--TangentAutomaton V. 1.4
2
--Notes:
3
--Code is now 100% all mine
4
--Removed my custom matrix, reducing memory usage by a factor of math.huge()
5
--Shortened a ton of long functions
6
--Removed all un-needed variables and functions
7
--Added some sexy line numbering
8
--Revised a few comments
9
--Re-arranged the order that data is asked for at the beginning
10
--Fixed the fickle way that some rules got cut off
11
--Tweaked the seed providing functions just a bit
12
--The rule table is now either "1" or "0" instead of "true" or "false"
13
14
cellon="#"
15
celloff="_"
16
17
seeddata={} --Put your seed information here!
18
19
states = {}
20
rulegiven={}
21
output={}
22
23
24
function dectobin(input)
25
	conversion={1,2,4,8,16,32,64,128} --I'll make this into 2^n eventually.
26
	output={0,0,0,0,0,0,0,0} --The output table, keeping it like this for now.
27
	i=8 --Starting number to decrement from
28
	for h=1,8 do --It's 8 bit!
29
		input=input-conversion[i] --Grabs the number to convert, and subtracts it from the lookup table.
30
		if input <0 then --If it's less than 0 (negative)...
31
			input=input+conversion[i] --...undo that subtraction.
32
		else --If it isn't less than 0 (greater than or equal to)...
33
			output[h]=1 --...set the output bit high (change "h" to "i" to swap endianess).
34
		end
35
		i=i-1 --The simple decrementer
36
	end
37
end
38
39
ruletable= --The rule lookup table, the heart of this script.
40
{
41
	{1,1,1},
42
	{1,1,0},
43
	{1,0,1},
44
	{1,0,0},
45
	{0,1,1},
46
	{0,1,0},
47
	{0,0,1},
48
	{0,0,0}
49
}
50
51
rule={} --Sets up the actual table the algorithm looks at.
52
53
function grabrule() --This function decodes the given rule.
54
	userinput=io.read() --Gets the user's input.
55
	dectobin(userinput) --Sends the input to get decoded into binary.
56
	rulegiven=output --Takes that binary number and sticks it into "rulegiven".
57
	rulecounter=1 --Starts a sync counter.
58
	for i=1,#rulegiven do --This will turn the binary into bollean logic.
59
		if rulegiven[i]==1 then --If the bit is 1...
60
			rule[rulecounter]=ruletable[i] --...set it to true (rather pointless, since I could combine this with the next part of the program).
61
			rulecounter=rulecounter+1
62
		end
63
	end
64
end
65
66
function grabseed() --This function asks for an input, and stores it to "state"
67
	seedgiven={} --I always store "binary" data in a table.
68
	print("Please enter a starting seed in binary!")
69
	print("Note: This seed will be placed in the center of the algorithm.")
70
	inputseed=io.read() --Asks for the seed.
71
	for i=1,string.len(inputseed) do --This loop chops the user input up, and stuffs it into my table.
72
		seedgiven[i]=string.sub(inputseed,i,i)
73
	end
74
	for i=1,#seedgiven do --Appearently, it's a string, and I need numbers.
75
		if seedgiven[i]=="1" then --If the string is a "1"...
76
			seedgiven[i]=1 --...make it into a number 1...
77
		else
78
			seedgiven[i]=0 --...else it's a 0.
79
		end
80
	end
81
	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.
82
	bitshift=math.floor(bitshift) --If it's an odd number, it gets rounded down (an even number is always created).
83
	j=bitshift --To keep the two numbers synced.
84
	for i=1,#seedgiven do --This stores the given string into the "states" table.
85
		states[j]=seedgiven[i] --Mirrors the value in "seedgiven" to "states".
86
		j=j+1 --To keep j and i in sync.
87
	end
88
end
89
90
function compute(row) --Here's where all the computation happens.
91
	io.write(row..":".."\t") --This adds line numbering.
92
	for i=1,#states do --For as many cells as there are in "states"...
93
		if states[i]==1 then io.write(cellon) else io.write(celloff) end --...if it's "1" then write an "on" state, else write an "off" state.
94
	end
95
	io.write("\n") --Writes a new line.
96
	current_gen = {} --This table stores the current information to get computed.
97
	for i = 1, #states do --This goes along the rows for as many starting states there are.
98
		current_gen[i] = 0 --Sets up every cell in this table as "0".
99
		for j = 1, #rule do --It computes for as many rules it is given.
100
			if (states[i - 1] == rule[j][1] and states[i] == rule[j][2] and states[i + 1] == rule[j][3]) then --If it complies with the rules...
101
				current_gen[i] = 1 --...set the current cell to an "on" state.
102
			end
103
		end
104
	end
105
	states = current_gen --changes "states" to reflect the changes made.
106
end
107
108
print("Please take a look at the top of this script to configure it!")
109
110
function interface() --The user interface function.
111
	print("Please enter the width to run for.")
112
	width=io.read() --Gets the width from the user.
113
	height = width / 2 --In an obtuse isosceles triangle, the height is usually half of the width (The trangles generated are usually 45 degrees).
114
 
115
	for i = 1, width+1 do states[i] = 0 end --Sets up the "states" table, which is where the computations begin.
116
	states[(width / 2)+1] = 1 --The default seed is a "1" in the center of the table.
117
118
	print("Please enter a rule in decimal.")
119
	grabrule() --Asks for the rule from the user.
120
121
	print("Would you like to input a seed?(y/n)")
122
	input=io.read() --Gets the user's choice.
123
	if input=="y" then --If the choice is "y", then...
124
		grabseed() --...go to the custom seed input function.
125
	else
126
		print("Would you like to use a randomly generated seed?(y/n)")
127
		input=io.read() --Gets the user's choice.
128
		if input=="y" then --If the choice is "y", then...
129
			for i=1,#states do --...for as many cells as there are in "states"...
130
				states[i]=math.random(0,1) --...set the cell to a random number of either "1" or "0".
131
			end
132
		else --If the user's decision is not one of the above options, "states" is not changed at all...
133
			print("Going with standard seed...") --...and goes with the default configuration set at the beginning.
134
		end
135
	end
136
end
137
138
interface() --Runs the above function.
139
140
for i = 1, height do --This runs the computation function for the desired height.
141
	compute(i)
142
end