-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
--------_____________________1D Cellular Automaton v1.0______________________--------
--------                                                                     --------
-------------------------------------------------------------------------------------
-- Written by dimitris gourdoukis.                                                 --
-- object-e architecture 2009.                                                     --
-------------------------------------------------------------------------------------
-- http://object-e.blogspot.com/                                                   --
-- object.e.architecture@gmail.com                                                 --
-------------------------------------------------------------------------------------
-- released under the creative commons license "share alike unported 3.0"          --
-- http://creativecommons.org/licenses/by-sa/3.0/                                  --
-------------------------------------------------------------------------------------
-- v1.0 Last Modified 07/13/09. Tested on 3DStudio Max 9.                          --
-- Use and modify at your own risk.                                                --
-------------------------------------------------------------------------------------
-- Description:                                                                    --
--	This script is a one-dimensional cellular automaton algorithm. First calcu-    --
--  lates the ca, stores it in an array, and then it 'builds' it using planes.     --
--	There are 18 different sets of rules that can be used. The initial generation  --
--  is random.                                                                     --
-------------------------------------------------------------------------------------


utility ca1d "1D_CA_v.1"
( 

	group "About:"
	(
	label ca_label01 "1D CA v1.0" 
	label ca_label02 "dimitris gourdoukis, 2009"
	hyperlink hl_hp "object-e architecture" color:(color 0 0 255) address:"http://object-e.blogspot.com" align:#center
	)
	group "Settings:"
	(
		dropdownlist rules "Rules" items:#("30","54","60","62","90","94","102","110","122","126","150","158","182","188","190","220","222","250") 
		spinner nGens "# of Gens:" range:[1,1000,50] type:#integer fieldwidth:40 align:#right
		spinner nCell "# of Cell:" range:[1,1000,20] type:#integer fieldwidth:40 align:#right 
	)
	--generate the main button
	button start_process "BUILD IT!" width:140 height:30
	
	on start_process pressed do
	(
		nGen = nGens.value
		nCells = nCell.value
		initialState = #()
		
		--create a random initial state
		for i in 1 to nCells do initialState[i] = random 0 1
		initialState
		
		--create the array that will contain the state of all the cells
		cellAuto = #()
		cellAuto[1] = initialState
		
		--start a progress indicator
		progressstart "calculating the CA..."
		
		for j in 2 to nGen do
		(
		
			cellLeftNeigh = #()
			cellRightNeigh = #()
			cellState = #()
			
			--update the progress indicator
			progressupdate (j as float /nGen *100) 
			
			for i in 1 to nCells do
			(
				
				--get the state of the left neighbor (take care of the extreme left cell)
				cellLeftNeigh [i] = case i of
				(	
					1: 0
					nCells: cellAuto [j-1][i-1]
					default: cellAuto [j-1][i-1]
				)
				
				--get the state of the right neighbor (take care of the extreme right cell)
				cellRightNeigh [i] = case i of
				(	
					1: cellAuto [j-1][i+1]
					nCells: 0
					default: cellAuto [j-1][i+1]
				)
				
				--create an array to contain the state of the three cells
				cellNeigh = #()
				cellNeigh = #(cellLeftNeigh [i], cellAuto[j-1][i], cellRightNeigh [i])
				
				--create a string from the elements of the previous array
				phrase = cellNeigh[1] as string + cellNeigh[2] as string + cellNeigh[3] as string
				
				--test the string against the 8 posible options and define the next state
				--here are the actual ca rules
				case rules.selection of
  				( 
					--rule 30
					1: cellState[i] = case phrase of
					(
						"111": 0
						"110": 0
						"101": 0
						"100": 1
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)
					
					--rule 54
					2: cellState[i] = case phrase of
					(
						"111": 0
						"110": 0
						"101": 1
						"100": 1
						"011": 0
						"010": 1
						"001": 1
						"000": 0
					)	
					
					--rule 60
					3: cellState[i] = case phrase of
					(
						"111": 0
						"110": 0
						"101": 1
						"100": 1
						"011": 1
						"010": 1
						"001": 0
						"000": 0
					)	
					
					--rule 62
					4: cellState[i] = case phrase of
					(
						"111": 0
						"110": 0
						"101": 1
						"100": 1
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)	
					
					--rule 90
					5: cellState[i] = case phrase of
					(
						"111": 0
						"110": 1
						"101": 0
						"100": 1
						"011": 1
						"010": 0
						"001": 1
						"000": 0
					)
					
					--rule 94
					6: cellState[i] = case phrase of
					(
						"111": 0
						"110": 1
						"101": 0
						"100": 1
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)
					
					--rule 102
					7: cellState[i] = case phrase of
					(
						"111": 0
						"110": 1
						"101": 1
						"100": 0
						"011": 0
						"010": 1
						"001": 1
						"000": 0
					)	
					
					--rule 110
					8: cellState[i] = case phrase of
					(
						"111": 0
						"110": 1
						"101": 1
						"100": 0
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)
					
					--rule 122
					9: cellState[i] = case phrase of
					(
						"111": 0
						"110": 1
						"101": 1
						"100": 1
						"011": 1
						"010": 0
						"001": 1
						"000": 0
					)
					
					--rule 126
					10: cellState[i] = case phrase of
					(
						"111": 0
						"110": 1
						"101": 1
						"100": 1
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)
					
					--rule 150
					11: cellState[i] = case phrase of
					(
						"111": 1
						"110": 0
						"101": 0
						"100": 1
						"011": 0
						"010": 1
						"001": 1
						"000": 0
					)	
					
					--rule 158
					12: cellState[i] = case phrase of
					(
						"111": 1
						"110": 0
						"101": 0
						"100": 1
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)
					
					--rule 182
					13: cellState[i] = case phrase of
					(
						"111": 1
						"110": 0
						"101": 1
						"100": 1
						"011": 1
						"010": 1
						"001": 0
						"000": 0
					)
					
					--rule 188
					14: cellState[i] = case phrase of
					(
						"111": 1
						"110": 0
						"101": 1
						"100": 1
						"011": 1
						"010": 1
						"001": 0
						"000": 0
					)
					
					--rule 190
					15: cellState[i] = case phrase of
					(
						"111": 1
						"110": 0
						"101": 1
						"100": 1
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)
					
					--rule 220
					16: cellState[i] = case phrase of
					(
						"111": 1
						"110": 1
						"101": 0
						"100": 1
						"011": 1
						"010": 1
						"001": 0
						"000": 0
					)
					
					--rule 222
					17: cellState[i] = case phrase of
					(
						"111": 1
						"110": 1
						"101": 0
						"100": 1
						"011": 1
						"010": 1
						"001": 1
						"000": 0
					)
					
					--rule 250
					18: cellState[i] = case phrase of
					(
						"111": 1
						"110": 1
						"101": 1
						"100": 1
						"011": 1
						"010": 0
						"001": 1
						"000": 0
					)
				)
			)
			--add the new array to the general array
			cellAuto[j] = cellState
		)
		
		--end the first progress indicator
		progressend () 
		
		--start a new progress indicator
		progressstart "Building the Cells..."
		--build the geometry
		for j in 1 to nGen do
		(
			
			--update the progress indicator
			progressupdate (j as float /nGen *100) 
			
			for i in 1 to nCells do
			(
				if (cellAuto[j][i] == 1) then
				(
					p = plane length:1 width:1
					p.pos.x = i
					p.pos.y = -j
					p.material = standard diffuse:color 0 0 70
				)
			)
		)
		
		--end the second progress indicator
		progressend ()		
	)
)
