/*******************************************************\
*	Black Art of Macintosh Game Programming				*
*		¨1995 Kevin Tieskoetter							*
*			Portions ¨1993 Waite Group Press			*
*														*
*	Chapter 18											*
*		Map.c											*
\*******************************************************/

#include "Map.h"

#pragma mark Constants
/**
 **  Constants
 **
 **/
#define kGridSize				160
#define kCollisionSlopFactor	5

#pragma mark Globals
/**
 **  Globals
 **
 **/

Boolean		gBarrierMap[10][10];


#pragma mark Prototypes
/**
 **  Prototypes
 **
 **/

#pragma mark -
/**
 **  Implementation
 **
 **/

/**  InitializeMap()
 **
 **  Fills in the global barrier map with the locations of all of the
 **  walls.
 **/
void	InitializeMap(void)
{
	short	i, j;
	
	/* First initialize them all to FALSE to clear the map */
	for (i = 0; i < 10; i++) {
		for (j = 0; j < 10; j++) {
			gBarrierMap[i][j] = FALSE;
		}
	}
	
	/* Set up the individual boxes */
	gBarrierMap[0][5] = TRUE;	gBarrierMap[0][8] = TRUE;
	gBarrierMap[0][9] = TRUE;	gBarrierMap[1][1] = TRUE;
	gBarrierMap[1][2] = TRUE;	gBarrierMap[1][3] = TRUE;
	gBarrierMap[1][5] = TRUE;	gBarrierMap[2][5] = TRUE;
	gBarrierMap[2][6] = TRUE;	gBarrierMap[2][7] = TRUE;
	gBarrierMap[3][5] = TRUE;	gBarrierMap[4][1] = TRUE;
	gBarrierMap[4][2] = TRUE;	gBarrierMap[4][7] = TRUE;
	gBarrierMap[5][1] = TRUE;	gBarrierMap[5][2] = TRUE;
	gBarrierMap[5][3] = TRUE;	gBarrierMap[5][4] = TRUE;
	gBarrierMap[5][5] = TRUE;	gBarrierMap[5][7] = TRUE;
	gBarrierMap[6][1] = TRUE;	gBarrierMap[6][2] = TRUE;
	gBarrierMap[6][5] = TRUE;	gBarrierMap[6][7] = TRUE;
	gBarrierMap[6][8] = TRUE;	gBarrierMap[6][9] = TRUE;
	gBarrierMap[7][1] = TRUE;	gBarrierMap[7][2] = TRUE;
	gBarrierMap[7][5] = TRUE;	gBarrierMap[7][7] = TRUE;
	gBarrierMap[7][8] = TRUE;	gBarrierMap[6][9] = TRUE;
	gBarrierMap[8][5] = TRUE;
}


/**
 **  CheckBarrierCollision()
 **
 **  Checks to see if the view position is intersecting one of the
 **  barriers.
 **/
Boolean	CheckBarrierCollision(short x, short z)
{
	short	mapX, mapZ;
	
	/* See if the position is outside the bounds of the map */
	if (	(x < kCollisionSlopFactor) || 
			(x > (kGridSize * 10) + kCollisionSlopFactor) ||
			(z < kCollisionSlopFactor) ||
			(z > (kGridSize * 10) + kCollisionSlopFactor))
		return TRUE;

	/* Add the slop factor to the coordinates and
	 * convert them to fit within the confines
	 * of the map (which is 1/160 scale).
	 */
	mapX = (x + kCollisionSlopFactor) / kGridSize;
	mapZ = (z + kCollisionSlopFactor) / kGridSize;
	
	/* Check to see if this position intersects a barrier */
	if (gBarrierMap[mapZ][mapX])
		return TRUE;
	
	/* Now subtract the slop factor from the coordinates
	 * and try again
	 */
	mapX = (x - kCollisionSlopFactor) / kGridSize;
	mapZ = (z - kCollisionSlopFactor) / kGridSize;
	
	/* Check to see if this position intersects a barrier */
	if (gBarrierMap[mapZ][mapX])
		return TRUE;
	
	/* No collision was detected */
	return FALSE;
}











