I started work on the pathfinding algorithm. I know the stages of the algorithm (how it should look like in the end), bellow is almost pseudocode (it`s real code but I haven`t started building/debugging)
void FindPath(int Startx, int Startz, int Endx, int Endz)
{
Surrounding Sectors[8];
for(int i = 0; i < 8; i++)
{
Sectors[i].distance = -1;
Sectors[i].excluded = false;
}
if(NodeCoord(Startx, Startz +1).access)//N
{
Distancex = NodeCoord(Startx, Startz +1).x - Endx;
Distancez = NodeCoord(Startx, Startz +1).z - Endz;
Sectors[0].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[0].excluded = true;
}
if(NodeCoord(Startx+1,Startz+1).access)//NE
{
Distancex = NodeCoord(Startx+1,Startz+1).x - Endx;
Distancez = NodeCoord(Startx+1,Startz+1).z - Endz;
Sectors[1].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[1].excluded = true;
}
if(NodeCoord(Startx+1,Startz).access)//NE
{
Distancex = NodeCoord(Startx+1,Startz).x - Endx;
Distancez = NodeCoord(Startx+1,Startz).z - Endz;
Sectors[2].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[2].excluded = true;
}
if(NodeCoord(Startx+1,Startz-1).access)//SE
{
Distancex = NodeCoord(Startx+1,Startz-1).x - Endx;
Distancez = NodeCoord(Startx+1,Startz-1).z - Endz;
Sectors[3].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[3].excluded = true;
}
if(NodeCoord(Startx,Startz-1).access)//S
{
Distancex = NodeCoord(Startx,Startz-1).x - Endx;
Distancez = NodeCoord(Startx,Startz-1).z - Endz;
Sectors[4].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[4].excluded = true;
}
if(NodeCoord(Startx-1,Startz-1).access)//SW
{
Distancex = NodeCoord(Startx-1,Startz-1).x - Endx;
Distancez = NodeCoord(Startx-1,Startz-1).z - Endz;
Sectors[5].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[5].excluded = true;
}
if(NodeCoord(Startx-1,Startz).access)//W
{
Distancex = NodeCoord(Startx-1,Startz).x - Endx;
Distancez = NodeCoord(Startx-1,Startz).z - Endz;
Sectors[6].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[6].excluded = true;
}
if(NodeCoord(Startx-1,Startz+1).access)//NW
{
Distancex = NodeCoord(Startx-1,Startz+1).x - Endx;
Distancez = NodeCoord(Startx-1,Startz+1).z - Endz;
Sectors[7].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
else
{
Sectors[7].excluded = true;
}
for (int i =0; i < 8; i++)
{
if(!Sectors[i].excluded)
{
TrunkListCount++;
TrunkList[TrunkListCount].x = Sectors[i].x;
TrunkList[TrunkListCount].z = Sectors[i].z;
}
}
int Closest = 1000;
int j = -1;
for(int i =0; i< 8; i ++)
{
if(Sectors[i].distance < Closest)
{
Closest = Sectors[i].distance;
j = i;
}
}
ChartNode Candidate;
Candidate.x = TrunkList[TrunkListCount].x;
Candidate.z = TrunkList[TrunkListCount].z;
FindNext( Sectors[j].x, Sectors[j].z, Endx,Endz);
StringCchPrintfA(IGmessage,1024,"Closest %d",Closest);
MessageBox(NULL, IGmessage, "Textures.exe", MB_OK);
}
Surrounding FindNext(int Startx, int Startz, int Endx, int Endz)
{
Surrounding Sectors[8];
if(NodeCoord(Startx, Startz +1).access)//N
{
Distancex = NodeCoord(Startx, Startz +1).x - Endx;
Distancez = NodeCoord(Startx, Startz +1).z - Endz;
Sectors[0].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
if(NodeCoord(Startx+1,Startz+1).access)//NE
{
Distancex = NodeCoord(Startx+1,Startz+1).x - Endx;
Distancez = NodeCoord(Startx+1,Startz+1).z - Endz;
Sectors[1].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
if(NodeCoord(Startx+1,Startz).access)//NE
{
Distancex = NodeCoord(Startx+1,Startz).x - Endx;
Distancez = NodeCoord(Startx+1,Startz).z - Endz;
Sectors[2].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
if(NodeCoord(Startx+1,Startz-1).access)//SE
{
Distancex = NodeCoord(Startx+1,Startz-1).x - Endx;
Distancez = NodeCoord(Startx+1,Startz-1).z - Endz;
Sectors[3].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
if(NodeCoord(Startx,Startz-1).access)//S
{
Distancex = NodeCoord(Startx,Startz-1).x - Endx;
Distancez = NodeCoord(Startx,Startz-1).z - Endz;
Sectors[4].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
if(NodeCoord(Startx-1,Startz-1).access)//SW
{
Distancex = NodeCoord(Startx-1,Startz-1).x - Endx;
Distancez = NodeCoord(Startx-1,Startz-1).z - Endz;
Sectors[5].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
if(NodeCoord(Startx-1,Startz).access)//W
{
Distancex = NodeCoord(Startx-1,Startz).x - Endx;
Distancez = NodeCoord(Startx-1,Startz).z - Endz;
Sectors[6].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
if(NodeCoord(Startx-1,Startz+1).access)//NW
{
Distancex = NodeCoord(Startx-1,Startz+1).x - Endx;
Distancez = NodeCoord(Startx-1,Startz+1).z - Endz;
Sectors[7].distance = sqrtf(Distancex * Distancex + Distancez*Distancez);
}
int Closest = 1000;
int j = -1;
for(int i =0; i< 8; i ++)
{
if(Sectors[i].distance < Closest)
{
Closest = Sectors[i].distance;
j = i;
}
}
Surrounding Result;
Result.x = Sectors[j].x;
Result.z = Sectors[j].z;
return Result;
}