GameSource/DirectionTable.c

#include "ZAMProtos.h"
#include <FixMath.h>
 
#define kNumDirectionAngles 32
 
static fixPt    gDirectionTable[kNumDirectionAngles];
 
#define kRadiansInCircle 6.283185307178
 
 
void InitDirectionTable(void)
/*
    this procedure builds a table of fixed point vectors.
    These vectors are indexed by the direction number from 0 to kNumDirectionAngles
    and are evenly spaced around the compass.
*/
{
    short i;
    double angle = 0;
    double angleIncrement = kRadiansInCircle / kNumDirectionAngles;
 
    for (i = 0; i < kNumDirectionAngles; i++) {
 
        gDirectionTable[i].h = Frac2Fix( FracSin( X2Fix(angle) ));
        gDirectionTable[i].v = Frac2Fix( -FracCos( X2Fix(angle) ));
 
        angle += angleIncrement;
    }
}
 
 
void DirectionToVelocity(short dir, fixPt *vel)
/*
    grab a vector from the table, indexed by the direction number
    this would be better as a Macro, but hey, it isn't.
*/
{
    *vel = gDirectionTable[dir];
}
 
 
short VelocityToDirection(fixPt *vel)
/*
    Get a direction number from a velocity.
    This is a pretty LAME thing to have to do, because it requires a search of the vector table
*/
{
    short   i;
    
    for(i = 0; i < kNumDirectionAngles; i++)
        if( (gDirectionTable[i].h == vel->h) && (gDirectionTable[i].v == vel->v))
            return i;
            
    return -1;
}