--------------------------
--       VARIABLES      --
--------------------------

var_ms_grid={}                   -- Main grid
var_ms_dummy={}                  -- Dummy grid
var_back={}                      -- Backup grid

var_col=1                        -- Bright colour
var_colour={'ffffff','445a62'}   -- Bright / Dim
var_shapes=4                     -- 4 Predefined shapes
var_shape=0                      -- Shape counter
var_ms_i=0                       -- Iterations counter
tweens.tap=0                     -- Single / double tap

s=169                            -- Number of tiles
h=13                             -- Tiles in a row

var_time=true                    -- Toggle time
var_run=false                    -- Auto run on/off


--------------------------
--       FUNCTIONS      --
--------------------------

function clear_grid()
for i=1,s do
  var_ms_grid[i]=false
  var_ms_dummy[i]=false
end
var_run=false
var_ms_i=0
end


function on_millisecond()
if var_run then var_life() end
end


function var_life()
-- Remember the initial shape

if var_ms_i==0 then backup() end

-- Check the adjacent tiles of each tile

for i=1,s do
  check(i)

-- Store the results in the dummy grid first

  if var_ms_grid[i]==true then
    var_ms_dummy[i]=true
    if c<2 or c>3 then var_ms_dummy[i]=false end
   else
    if c==3 then var_ms_dummy[i]=true end
  end
end

-- Detect is something has changed

if var_ms_i>0 then same=compare() end

-- Copy the dummy grid to the actual grid,
-- unless evolution stop is detected

if not same or var_ms_i==0 then
  for i=1,s do
    var_ms_grid[i]=var_ms_dummy[i]
  end
  var_ms_i=var_ms_i+1  -- counts the iterations
 else
  var_run=false        -- evolution stop detected
end
end


function check(i)
-- CHECK THE 8 ADJACENT TILES

ul=i-h-1  um=i-h  ur=i-h+1   -- up left\middle\right
ml=i-1              mr=i+1   -- middle left\right
dl=i+h-1  dm=i+h  dr=i+h+1   -- down left\middle\right

-- HORIZONTAL EDGES

if i>0 and i<h+1 then ul=s+ul um=s+um ur=s+ur
 else
 if i>s-h and i<s+1 then dl=dl%h dm=dm%h dr=dr%h
  else

-- VERTICAL EDGES

   if i%h==1 then ul=ul+h ml=ml+h dl=dl+h end
   if i%h==0 then ur=ur-h mr=mr-h dr=dr-h end
  end
end

c=0 -- c counts the adjacent true tiles
if var_ms_grid[ul]==true then c=c+1 end
if var_ms_grid[um]==true then c=c+1 end
if var_ms_grid[ur]==true then c=c+1 end
if var_ms_grid[ml]==true then c=c+1 end
if var_ms_grid[mr]==true then c=c+1 end
if var_ms_grid[dl]==true then c=c+1 end
if var_ms_grid[dm]==true then c=c+1 end
if var_ms_grid[dr]==true then c=c+1 end
end


function compare()
-- Detects wether evolution has ended or not
-- Infinitely oscillating shapes are considered alive
i=0
repeat
  i=i+1
until var_ms_dummy[i] ~= var_ms_grid[i] or i>s
if i<s+1 then return(false) else return(true) end
end


function backup()
for i=1,s do
  var_back[i]=var_ms_grid[i]
end
end


function shape(f)
if f==1 then
  var_ms_grid[136]=true var_ms_grid[137]=true
  var_ms_grid[138]=true var_ms_grid[151]=true
  var_ms_grid[163]=true
end
if f==2 then
  var_ms_grid[55]=true var_ms_grid[56]=true
  var_ms_grid[57]=true var_ms_grid[58]=true
  var_ms_grid[67]=true var_ms_grid[71]=true
  var_ms_grid[84]=true var_ms_grid[93]=true
  var_ms_grid[96]=true
end
if f==3 then
  var_ms_grid[136]=true var_ms_grid[137]=true
  var_ms_grid[138]=true var_ms_grid[151]=true
  var_ms_grid[163]=true var_ms_grid[41] =true
  var_ms_grid[55] =true var_ms_grid[66] =true
  var_ms_grid[67] =true var_ms_grid[68] =true
end
if f==4 then
  for i=1,math.random(40,100) do
    var_ms_grid[math.random(1,s)]=true
  end
end
end


function on_display_not_bright()
var_col=2
end

function on_display_bright()
var_col=1
end


function tap()
wm_schedule{action = 'tween', tween = 'tap', from = 1, to = 0, duration = 0.25, easing = linear} 
end