#include "LatticeWorld.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>

LatticeWorld *CreateWorld(unsigned int width, unsigned int height,
        uint8_t *init) {
    LatticeWorld *world = malloc(sizeof(LatticeWorld));
    if(!world) {
        return NULL;
    }
    world->sites[0] = malloc(width*height);
    if(!world->sites[0]) {
        free(world);
        return NULL;
    }
    world->sites[1] = malloc(width*height);
    if(!world->sites[1]) {
        free(world->sites[0]);
        free(world);
        return NULL;
    }
    world->currentSite = 0;
    if(init) {
        memcpy(world->sites[0], init, width*height);
    }
    else {
        memset(world->sites[0], 0, width*height);
    }
    world->width = width;
    world->height = height;
    srand(time(NULL));
    // Initialize the rules here. If we allow multiple worlds we'll only need
    // to do this once
    InitRules();
    return world;
}

void DestroyWorld(LatticeWorld *world) {
    if(world) {
        free(world);
    }
}

uint8_t *GetWorldData(LatticeWorld *world) {
    return world->sites[world->currentSite];
}

