/******************************************************************************
 Snippet: Wilderness system (v1.0).
 Author:  Richard Woolcock (aka KaVir).
 Date:    2nd September 1998.
 ******************************************************************************
 File:    wild_io.c
 Purpose: Deals with input/output files.
 ******************************************************************************
 This code is copyright (C) 1998 by Richard Woolcock.  It may be used and
 distributed freely, as long as you do not remove this copyright notice.
 ******************************************************************************/

/******************************************************************************
 Required libraries
 ******************************************************************************/

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "merc.h"
#include "wild.h"

/******************************************************************************
 Local literals
 ******************************************************************************/

#define WORLD_DIR	"../world/"	/* World files */

/******************************************************************************
 Local operation prototypes
 ******************************************************************************/

bool world_exists	args( ( int x, int y ) );

/******************************************************************************
 Global operations
 ******************************************************************************/

void wild_write( int x, int y )
{
    char strsave[MAX_INPUT_LENGTH];
    FILE *fp;
    int  loopx, loopy, xx, yy;
    int  linecount = 0;

    if ( x < 0 || x >= (WILDERNESS_SIZE/100) || 
	 y < 0 || y >= (WILDERNESS_SIZE/100) )
    {
	bug( "Map location out of range.", 0 );
	return;
    }

    xx = x * 100;
    yy = y * 100;

    fflush( fpReserve );
    fclose( fpReserve );
    sprintf( strsave, "%sX%d.Y%d", WORLD_DIR, x, y );
    if ( ( fp = fopen( strsave, "w" ) ) == NULL )
	bug("Cannot Open map data file.", 0);
    else
    {
	for ( loopx = 0; loopx < 100; loopx++ )
	{
	    for ( loopy = 0; loopy < 100; loopy++ )
	    {
		fprintf( fp, "%c", map_data(loopx+xx,loopy+yy) );
		if (++linecount % 80 == 0)
		    fprintf( fp, "\n");
	    }
	}
    }
    fflush( fp );
    fclose( fp );
    fpReserve = fopen( NULL_FILE, "r" );
    return;
}

void wild_read( int x, int y )
{
    char strsave[MAX_INPUT_LENGTH];
    char letter;
    FILE *fp;
    int  loopx, loopy, xx, yy;
    bool done_reading = FALSE;
    bool yet_finished;

    if ( x < 0 || x >= (WILDERNESS_SIZE/100) || 
	 y < 0 || y >= (WILDERNESS_SIZE/100) )
    {
	bug( "Map location out of range.", 0 );
	return;
    }

    xx = x * 100;
    yy = y * 100;

    if ( !world_exists(x, y) )
    {
	for ( loopx = 0; loopx < 100; loopx++ )
	{
	    for ( loopy = 0; loopy < 100; loopy++ )
	    {
		set_map_data(loopx+xx,loopy+yy, map_big(x,y));
	    }
	}
	wild_write(x,y);
	return;
    }

    fflush( fpReserve );
    fclose( fpReserve );
    sprintf( strsave, "%sX%d.Y%d", WORLD_DIR, x, y );
    if ( ( fp = fopen( strsave, "r" ) ) != NULL )
    {
	for ( loopx = 0; loopx < 100; loopx++ )
	{
	    for ( loopy = 0; loopy < 100; loopy++ )
	    {
		if ( done_reading == TRUE )
		    set_map_data(loopx+xx, loopy+yy, WILD_PLAINS);
		else if ( ( letter = fread_letter( fp ) ) == EOF)
		{
		    done_reading = TRUE;
		    set_map_data(loopx+xx, loopy+yy, WILD_PLAINS);
		}
		else
		{
		    yet_finished = FALSE;
		    while ( !yet_finished )
		    {
			switch ( letter )
			{
			    default:
				if ( ( letter = fread_letter( fp ) ) == EOF )
				{
				    done_reading = TRUE;
				    set_map_data(loopx+xx, loopy+yy, WILD_PLAINS);
				    yet_finished = TRUE;
				}
				break;
			    case 'a': case 'b': case 'c': case 'd':
			    case 'e': case 'f': case 'g': case 'h':
			    case 'i': case 'j': case 'k': case 'l':
			    case 'm': case 'n': case 'o': case 'p':
			    case 'q': case 'r': case 's': case 't':
			    case 'u': case 'v': case 'w': case 'x':
			    case 'y': case 'z': case '_': case '@':
			    case 'A': case 'B': case 'C': case 'D':
			    case 'E': case 'F': case 'G': case 'H':
			    case 'I': case 'J': case 'K': case 'L':
			    case 'M': case 'N': case 'O': case 'P':
			    case 'Q': case 'R': case 'S': case 'T':
			    case 'U': case 'V': case 'W': case 'X':
			    case 'Y': case 'Z': case '1': case '2':
			    case '3': case '4': case '5': case '6':
			    case '7': case '8': case '9': case '0':
				set_map_data(loopx+xx, loopy+yy, letter);
				yet_finished = TRUE;
				break;
			}
		    }
		}
	    }
	}
    }
    else
    {
	bug( "wild_read: fopen", 0 );
	perror( strsave );
    }
    fflush( fp );
    fclose( fp );
    fpReserve = fopen( NULL_FILE, "r" );
    return;
}

void world_read( void )
{
    int x, y;
    for ( x = 0; x < 10; x++ )
    {
	for ( y = 0; y < 10; y++ )
	{
	    set_map_changed(x, y, FALSE);
	    wild_read(x,y);
	}
    }
    return;
}

/******************************************************************************
 Local operations
 ******************************************************************************/

bool world_exists( int x, int y )
{
    FILE *fp;
    char buf [MAX_STRING_LENGTH];
    bool found = FALSE;

    fflush( fpReserve );
    fclose( fpReserve );
    sprintf( buf, "%sX%d.Y%d", WORLD_DIR, x, y );
    if ( ( fp = fopen( buf, "r" ) ) != NULL )
    {
	found = TRUE;
	fflush( fp );
	fclose( fp );
	fpReserve = fopen( NULL_FILE, "r" );
    }
    return ( found );
}
