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

/* including main header file */
#include "mud.h"

/*
 * load_world_data()
 *
 * called from load_muddata in utils.c
 */

/* Called in load_muddata
 * Just to load the world on starup/copyover */
void load_world_data()
{
    /*save_zworld();
    log_string("*** WORLD *** Saved World.\n\r"); */
    load_zworld();
    log_string("*** WORLD *** Loaded ZWorld.");
}
/* cmd to forcefuly save the world */
void cmd_save_world(D_MOBILE * ch, char *arg)
{
    stc("Saving zworld structure...\n\r", ch);
    save_zworld();
    stc("Save zworld complete!\n\r", ch);
}
/* cmd to forcerful reload the world */
void cmd_revert_world(D_MOBILE * ch, char *arg)
{
    stc("Reverting zworld structure...\n\r", ch);
    load_zworld();
    stc("Revert zworld complete!\n\r", ch);
}

/*
 * load/save_world()
 *
 * New Load/save world functions
 * Thank you Tyche for the zlib functions =)
 */
void save_zworld()
{
    gzFile *fp;

    fp = gzopen(WORLD_FILE2,"wb9");
    gzwrite(fp,map,sizeof(map));
    gzclose(fp);
}

void load_zworld()
{
    gzFile *fp;

    fp = gzopen(WORLD_FILE2,"rb9");
    gzread(fp,map,sizeof(map));
    gzclose(fp);
}

/*
 * cmd_load_image()
 *
 * Loads a 1000x750 raw image file
 * into the map on current ch's z axis
 */
void cmd_load_image(D_MOBILE * ch, char *arg)
{
    FILE * fp;
    int graph1, graph2, graph3, x, y;
    
    stc("Loading Image to current Depth\n\r", ch);
    
    if( !( fp = fopen( "../world/hworld.raw", "rb" ) ) )
    {
        printf("Error: Cannot open file?\n");
        return;
    }
    
    for (y = 0; y < MAX_Y; y++)
    {
        for (x = 0; x < MAX_X; x++)
        {
            graph1 = fgetc(fp);
            graph2 = fgetc(fp);
            graph3 = fgetc(fp);
            
            map[y][x] = return_sector_number(graph1, graph2, graph3);
        }
    }
    printf("Sucessfuly loaded file\n");
    fclose( fp );
}

/* cmd_save_image()
 *
 * Outputs a raw image file based on map
 * currently broken
 */
void cmd_save_image(D_MOBILE * ch, char *arg)
{
    FILE * fp;
    int graph1, graph2, graph3, x, y;
    
    stc("Saving ../world/hworld_output.raw\n\r", ch);
    
    if( !( fp = fopen( "../world/hworld_output.raw", "w" ) ) )
    {
        printf("Error: Cannot open file?\n");
        return;
    }
    
    for (x = 0; x < MAX_X; x++)
    {
        for (y = 0; y < MAX_Y; y++)
        {
            
            graph1 = sector_table[map[y][x]].red;
            graph1 = sector_table[map[y][x]].green;
            graph1 = sector_table[map[y][x]].blue;
            
            fputc(graph1, fp);
            fputc(graph2, fp);
            fputc(graph3, fp);
        }
    }
    printf("Sucessfuly saved file\n");
    fclose( fp );
}

/*
 * return_sector_number()
 *
 * Takes the RBG values from the image
 * and finds the correct sector_table entry
 * and returns the internal sector number
 */
int return_sector_number(int r, int g, int b)
{
    int i;
    int final;
    
    for (i = 0; i < MAX_SECTOR; i++)
    {
        if (sector_table[i].red == r && sector_table[i].green == g && sector_table[i].blue == b)
        {
            final = sector_table[i].sector;
            break;
        }
        else
            final = MAX_SECTOR + 1;
    }
    return final;
}
/*
 * cmd_load_small()
 *
 * Loads a small image into current ch's x/y location
 * Used for loading premade maps/citys/dungeons
 * into the world ontop of the actual map
 */
void cmd_load_small(D_MOBILE * ch, char *arg)
{
    FILE * fp;
    int graph1, graph2, graph3, x, y;
    
    stc("Loading Small Image to current area\n\r", ch);
    
    if( !( fp = fopen( "../world/vandagard.raw", "r" ) ) )
    {
        printf("Error: Cannot open file?\n");
        return;
    }
    
    for (y = ch->y; y < (ch->y + 51); y++)
    {
        for (x = ch->x; x < (ch->x + 51); x++)
        {
            graph1 = fgetc(fp);
            graph2 = fgetc(fp);
            graph3 = fgetc(fp);
            
            map[y][x] = return_sector_number(graph1, graph2, graph3);
        }
    }
    printf("Sucessfuly loaded small file\n");
    fclose( fp );
}

void cmd_goto_inside(D_M * ch, char * arg)
{
    int x;
    int y;
    char buf[MAX_BUFFER];
    
    for (y = 0; y < MAX_Y; y++)
    {
        for (x = 0; x < MAX_X; x++)
        {
            if (map[x][y] == 16)
            {
                sprintf(buf, "Inside X = %d, inside Y = %d\n\r", x, y);
                stc(buf, ch);
                break;
            }
        }
    }
    return;
}