/*
**  map
**
**   map manipulations functions
**
*/

/* 
**   Scott "Jerry" Lawrence
**   2000 January 23
**   j@absynth.com
*/

/*
** $Id: map.c,v 1.2 2000/02/07 22:21:02 sdlpci Exp sdlpci $
**
** $Log: map.c,v $
 * Revision 1.2  2000/02/07  22:21:02  sdlpci
 * removed the conditional structure -- it was not needed.
 *
 * Revision 1.1  2000/01/24  16:39:05  sdlpci
 * Initial revision
 *
 * Revision 1.1  2000/01/24  16:39:05  sdlpci
 * Initial revision
 *
*/

#include <stdio.h>
#include <stdlib.h>   /* for malloc/free */
#include <string.h>   /* for memset      */
#include "map.h"
#include "io_util.h"


void map_destroy(INTMAP * m)
{
    int c;

    if (m)
    {
	if (m->data)
	{
	    for(c=0 ; c<m->h ; c++)
	    {
		if(m->data[c])  free(m->data[c]);
		if(m->trav[c])  free(m->trav[c]);
	    }
	    free(m->data);
	    free(m->trav);
	}
	free(m);
    }
}


void map_reset_trav(INTMAP * map)
{
    int r;

    if (!map) return;
    if (!map->w || !map->h) return;
    if (!map->trav) return;

    map->x = 0;
    map->y = 0;
    map->max = 0;

    for (r=0 ; r<map->h ; r++)
	if (map->trav[r])  memset(map->trav[r], 0, sizeof(int) * map->w);
}


INTMAP * map_read_in_file(FILE * input_file)
{
    int w,h;
    int r,c;
    INTMAP * new_map = NULL;

    w = read_int(input_file);
    h = read_int(input_file);

    if (w == 0 || h == 0)
    {
	printf ("error: read in %d by %d.  bad buffer size\n", w, h);
	return NULL;
    }

    new_map = (INTMAP *) malloc (sizeof (INTMAP));
    if (!new_map)
    {
	printf ("error: malloc 1 failed\n");
	return NULL;
    }

    new_map->data = (unsigned int **)malloc(sizeof(unsigned int *) * (h+1) );
    new_map->trav = (unsigned int **)malloc(sizeof(unsigned int *) * (h+1) );
    if (!new_map->data || !new_map->trav)
    {
	printf ("error: malloc 2 failed\n");
	map_destroy(new_map);
	return NULL;
    }

    new_map->w = w;
    new_map->h = h;

    for (r=0 ; r<h ; r++)
    {
	new_map->data[r] = (unsigned int *) malloc (sizeof(unsigned int) * w);
	new_map->trav[r] = (unsigned int *) malloc (sizeof(unsigned int) * w);
	if (!new_map->data[r])
	{
	    printf ("error: malloc 3 failed\n");
	    map_destroy(new_map);
	    return NULL;
	}

	for (c=0 ; c<w ; c++)
	{
	    new_map->data[r][c] = read_char(input_file);
	}
    }

    map_reset_trav(new_map);
    return new_map;
}


void map_int_table_dump(int w, int h, unsigned int ** data, int chars)
{
    int r,c;

    for (r=0 ; r<h ; r++)
    {
	printf("%2d: ", r+1);
	if (data[r])
	{
	    /* do it this way, so we don't have to worry
	    ** about there being a '/0' in the string data.
	    */
	    for (c=0 ; c<w ; c++)
	    {
		if (chars)
		    printf("%c ", data[r][c]);
		else 
		    printf("%d ", data[r][c]);
	    }
	    printf("\n");
	} else {
	    printf("null\n");
	} 
    }
}

void map_dump(INTMAP * map)
{
    if (map)
    {
	printf("res:  %d by %d\n", map->w, map->h);
	if (map->data == NULL)
	{
	    printf("no map data loaded\n");
	    return;
	}

	map_int_table_dump(map->w, map->h, map->data, 1);
	printf("\n");
	map_int_table_dump(map->w, map->h, map->trav, 0);

    } else {
	printf("error: no map loaded.\n");
    } 
}

