#  mgas
#  Copyright (C) 1992  J Dana Eckart
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 1, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with CELLULAR; see the file COPYING.  If not, write to the 
#  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Simulate gas diffusion by `randomly' rotating the cell values in a
# Margolus-neighborhood either clockwise or counter-clockwise.  The 
# groupings are alternations of the two overlapping square neighborhoods:
#
#			0  1
#			2  3  2
#			   1  0
#
2 dimensions of 
	which of 0..3
	rand   of 0..1
	gas   of 0..1
end

neighbors[] for this, cw, ccw, opp 

	:= cell, [1, 0], [0, -1], [1, -1]
		when (cell.which=0 & time%2=1) | (cell.which=3 & time%2=0)

	:= cell, [0, -1], [-1, 0], [-1, -1]
		when (cell.which=1 & time%2=1) | (cell.which=2 & time%2=0)

	:= cell, [-1, 0], [0, 1], [-1, 1]
		when (cell.which=3 & time%2=1) | (cell.which=0 & time%2=0)

	:= cell, [0, 1], [1, 0], [1, 1]
		otherwise

# Calculate the sum of the random fields in the neighborhood.
#
rand := 0
forall i
	rand := rand + neighbors[i].rand
end

cell.gas := ccw.gas when rand%2 = 1	# Clockwise rotation
	 := cw.gas otherwise		# Counter-clockwise rotation

# Stir the random values (using a von neumann neighborhood), so that they
# keep changing.
#
cell.rand := ([1, 0].rand + [0, 1].rand + [-1, 0].rand + [0, -1].rand
		+ cell.rand)%2

