
#include "platform/platform.h"
#include "console/simBase.h"
#include "console/consoleTypes.h"
#include "console/simBase.h"
#include "math/mRandom.h"

#include "blocks/ca.h"

IMPLEMENT_CONOBJECT(CaModel);

CaModel::CaModel()
: ScriptObject()
{
    int i;
    int j;

    size_x = BLOCK_GRID_XSIZE;
    size_y = BLOCK_GRID_YSIZE;
    for( int i = 0; i < size_x; ++i )
    {
        for( int j = 0; j < size_y; ++j )
        {
            value[i][j] = v[i][j] = OFF;
        }
    }

}

bool CaModel::onAdd()
{
    if (!Parent::onAdd())
        return false;

    return true;
}

void CaModel::onRemove()
{
    Parent::onRemove();
}


int CaModel::get_value( int i, int j )
{
    if( i < 0 || i >= size_x || j < 0 || j >= size_y )
        return -1;
    return value[i][j];
}


void CaModel::set_value( int i, int j, int N )
{
    if( i < 0 || i >= size_x || j < 0 || j >= size_y )
        return;
    value[i][j] = N;
}

void CaModel::apply_rule()
{
    for ( int i = 1; i < size_x - 1; i++ )
    {
        for ( int j = 1; j < size_y - 1; j++ )
        {
            v[i][j] = rule( i, j );
        }
    }

    for ( int i = 0; i < size_x; i++ )
    {
        for ( int j = 0; j < size_y; j++ )
        {
            value[i][j] = v[i][j];
        }
    }
    

}

void CaModel::VonNeumann_neighbourhood( int i, int j )
{
    state = value[i][j];
    N = value[i-1][j]; 
    S = value[i+1][j];
    W = value[i][j-1];
    E = value[i][j+1];
}

void CaModel::Moore_neighbourhood( int i, int j )
{
    VonNeumann_neighbourhood( i, j );
    NW = value[i-1][j-1];
    SE = value[i+1][j+1];
    NE = value[i-1][j+1];
    SW = value[i+1][j-1];
}


int CaModel::rule( int i, int j )
{

    return OFF;
}


