// Used by walk functions to pick a new direction
void __Ghost_NewDir4(int rate, int homing, int hunger)
{
int newDir=-1;
// Go for bait?
if(Rand(4)<hunger)
{
// See if any is on the screen
lweapon bait=LoadLWeaponOf(LW_BAIT);
if(bait->isValid())
{
// Found bait; try to move toward it
if(Abs(Ghost_Y-bait->Y)>14)
{
if(bait->Y<Ghost_Y)
newDir=DIR_UP;
else
newDir=DIR_DOWN;
if(__Ghost_CanMoveNES(newDir, 1, 0))
{
Ghost_Dir=newDir;
return;
}
}
if(bait->X<Ghost_X)
newDir=DIR_LEFT;
else
newDir=DIR_RIGHT;
if(__Ghost_CanMoveNES(newDir, 1, 0))
{
Ghost_Dir=newDir;
return;
}
}
} // End hunger check
// Homing?
if(Rand(256)<homing)
{
newDir=__Ghost_LinedUp(8, false);
if(newDir>=0 && __Ghost_CanMoveNES(newDir, 1, 0))
{
Ghost_Dir=newDir;
return;
}
}
// Check solidity of surrounding combos
bool combos[4];
int numDirs;
int ndir;
for(int i=0; i<4; i++)
{
if(__Ghost_CanMoveNES(i, 1, 0))
{
combos[i]=true;
// numDirs++;
}
}
int i=0;
for(; i <32; i++)
{
int r=Rand(255);
if((r&15)<rate)
ndir=(r>>4)&3;
else
ndir=Ghost_Dir;
if(combos[ndir])
break;
}
if(i==32)
{
for(ndir=0; ndir<4; ndir++)
{
if(combos[ndir])
break;
}
if(ndir==4) //Trapped
{
// if(IsSideView)
// ndir=Cond(Rand(1,DIR_LEFT,DIR_RIGHT)) //Sideview Enemies bug out if their dir becomes -1
// else
ndir=-1;
}
}
Ghost_Dir=ndir;
}