#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

/*
 * Some Constraints on the world Size
 *
 * "19,250,000 locations should be
 *  enough for everyone!"
 *          Quote, Thri Oct 17, 2005
 */
#define MAX_X   80
#define MAX_Y   170
#define MAX_Z   20


/*
 * Question, since 5000x9000x11 is only
 * 500million, long is 2 billion, why
 * do i get size of variable 'map' is too large
 * on compile if it goes any higher?
 */
int map[MAX_X][MAX_Y][MAX_Z];

void load_world ();
void save_world();

int main()
{
    printf("Thing is %d\n\r", map[1][1][1]);
    printf("Starting Save world Function\n\r");
    //save_world();
    printf("Save world finished, starting loading\n\r");

    load_world();
    printf("Load world finished!\n\r");
    
    printf("Thing is %d\n\r", map[1][1][1]);
    return (0);
}
    

/*
 * load_world()
 *
 * Loads the entire world.
 */
void load_world()
{
    FILE * fp;
    long x = 0;
    long y = 0;
    long z = 0;
    
    size_t object_size = sizeof(int);
    size_t object_count = (MAX_X * MAX_Y * MAX_Z);
    size_t op_return;
    
    fp = fopen ("world.bin", "r");
    op_return = fread (&map, object_size, object_count, fp);
    if (op_return != object_count)
    {
        printf ("Error read data file.\n");
    }
    else
    {
        printf ("Successfully read data file.\n");
    }
    fclose(fp);
}
 
/*
 * save_world()
 *
 * Save the world!
 * Not just for Jehovah witnessess anymore.
 */
void save_world()
{
    FILE * fp;
    long x = 0;
    long y = 0;
    long z = 0;
    
    size_t object_size = sizeof(int);
    size_t object_count = (MAX_X * MAX_Y * MAX_Z);
    size_t op_return;
    
    fp = fopen ("world.bin", "w");
    op_return = fwrite (&map, object_size, object_count, fp);
    if (op_return != object_count)
    {
        printf ("Error writing data to file.\n");
    }
    else
    {
        printf ("Successfully wrote data to file.\n");
    }
    fclose(fp);
}