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

#include "blocks/ca.h"
#include "blocks/calife.h"

IMPLEMENT_CONOBJECT(CaLife);

CaLife::CaLife(): CaModel()
{

}

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

    return true;
}

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


int CaLife::rule( int i, int j )
{
    int sum = 0; //neigbours
    int cell;

    Moore_neighbourhood( i, j );
    if ( N == ON ) sum++;
    if ( S == ON ) sum++;
    if ( E == ON ) sum++;
    if ( W == ON ) sum++;
    if ( NE == ON ) sum++;
    if ( NW == ON ) sum++;
    if ( SW == ON ) sum++;
    if ( SE == ON ) sum++;

    if ( sum == 2 ) cell = state; //keep same state
    else if ( sum == 3 ) cell = ON;
    else cell = OFF;
    return cell;
}

ConsoleMethod( CaLife, get_value, S32, 4, 4, "get_value( int x, int y )" )
{
    int i, j;
    i = dAtoi( argv[2] );
    j = dAtoi( argv[3] );
    return object->get_value( i, j );
}

ConsoleMethod( CaLife, set_value, void, 5, 5, "set_value( int x, int y, int N )" )
{
    int i, j, N;
    i = dAtoi( argv[2] );
    j = dAtoi( argv[3] );
    N = dAtoi( argv[4] );
    object->set_value( i, j, N );
}

ConsoleMethod( CaLife, get_size_x, S32, 2, 2, "get_size_x()" )
{
    return object->size_x;
}

ConsoleMethod( CaLife, get_size_y, S32, 2, 2, "get_size_y()" )
{
    return object->size_y;
}

ConsoleMethod( CaLife, apply_rule, void, 2, 2, "apply_rule()" )
{
    object->apply_rule();
}

