#ifndef _TPFMAP_CC
#define _TPFMAP_CC

#include <stdlib.h>
#include "map.h"
#include "tex.h"
#include "vec.h"
#include "ang.h"

tpfMap *TpfMap_New(int x,int y)
{
	tpfMap *m;
	m = (tpfMap *) malloc(sizeof(tpfMap));
	m->x = x;
	m->y = y;
	m->map = (tpfBlock *) malloc(sizeof(tpfBlock)*x*y);
	y=x*y;
	for (x=0;x<y;x++)
	{
		m->map[x].w[0] = 0;
		m->map[x].w[1] = 0;
		m->map[x].w[2] = 0;
		m->map[x].w[3] = 0;
	}
	for (x=0;x<TPFMAP_MAXWALLS;x++)
	{
		m->walls[x].s = 0;
		m->walls[x].flags = 0;
	}
	m->org.x = m->org.y = m->org.z = 0;
	m->rot = 0; /* 0 degrees */
	return m;
}

void TpfMap_Delete(tpfMap *m)
{
	free(m->map);
	free(m);
}

void TpfMap_SetSide(tpfMap *m,int x,int y,int s,int c)
{
	if ((x < 0) || (y < 0) || (x >= m->x) || (y >= m->y))
		return;
	m->map[TPFMAP_CALCINDEX(m,x,y)].w[s] = c;
}

int TpfMap_GetSide(tpfMap *m,int x,int y,int s)
{
	if ((x < 0) || (y < 0) || (x >= m->x) || (y >= m->y))
		return 0; /* Empty block */
	return (int) m->map[TPFMAP_CALCINDEX(m,x,y)].w[s];
}

void TpfMap_SetWallFlags(tpfMap *m,int w,int f)
{
	m->walls[w].flags = f;
}

void TpfMap_SetWallTex(tpfMap *m,int w,tpfTex *t)
{
	m->walls[w].s = t;
}

int TpfMap_GetWallFlags(tpfMap *m,int w)
{
	return m->walls[w].flags;
}

tpfTex *TpfMap_GetWallTex(tpfMap *m,int w)
{
	return m->walls[w].s;
}

void TpfMap_SetPos(tpfMap *m,tpfVec o)
{
	m->org = o;
}

tpfVec *TpfMap_GetPos(tpfMap *m)
{
	return &(m->org);
}

void TpfMap_SetRot(tpfMap *m,tpfAng a)
{
	m->rot = a;
}

tpfAng TpfMap_GetRot(tpfMap *m)
{
	return m->rot;
}

int TpfMap_GetWidth(tpfMap *m)
{
	return m->x;
}

int TpfMap_GetHeight(tpfMap *m)
{
	return m->y;
}

#endif
