GGELUA/gastar/Astart.cpp

219 lines
4.7 KiB
C++

#include "Astart.h"
#include "双向链表.h"
DWORD (
POINT 1,
POINT 2)
{
DWORD x,y;
x = (1.x>2.x) ? 1.x-2.x:2.x-1.x;
y = (1.y>2.y) ? 1.y-2.y:2.y-1.y;
return (x+y)*10;
}
* WINAPI MapCreate(
DWORD ,
DWORD ,
BYTE* )
{
* = (*)(sizeof());
if (!)
return NULL;
-> = ;
-> = ;
-> = ;
-> = *;
if (!(-> = (*)(->*sizeof())))
{
();
= NULL;
}
return ;
}
VOID WINAPI MapDestroy(
* )
{
(->);
();
}
* WINAPI MapCreate2(
* ,
DWORD ,
DWORD ,
BYTE* )
{
-> = ;
-> = ;
-> = ;
-> = *;
if (!(-> = (*)(->*sizeof())))
return NULL;
return ;
}
VOID WINAPI MapDestroy2(
* )
{
(->);
}
BOOL WINAPI FindPath(
* ,
POINT* ,
POINT* ,
BOOL )
{
/* 终点开始查找 */
POINT = *;
POINT = *;
BYTE* = ->;
DWORD = ->;
DWORD = ->;
DWORD = ->;
();
BOOLEAN = FALSE;
static int x[] = {-1,-1,0,1,1,1,0,-1};
static int y[] = {0,-1,-1,-1,0,1,1,1};
* = ->;
(,*sizeof());
DWORD = *.y+.x;
if (>=)
return FALSE;
[]. = ;
[]. = (,);
[]. = [].;
.((LPVOID),[].);
while (.() != 0 && == FALSE)
{
/* 在待检链表中取出 F(总距离) 最小的节点, 并将其选为当前点 */
.();
DWORD = (DWORD).();
.();
[]. = ;
POINT =
{
% ,
/
};
/* 遍历当前坐标的八个相邻坐标 */
for (int i=0;i<8;i++)
{
if ( && i%2)
continue;
POINT =
{
.x + x[i],
.y + y[i]
};
/* 检查坐标有效性 */
if (.x<0 || (DWORD).x>=
|| .y<0 || (DWORD).y>=)
continue;
DWORD = *.y+.x;
if ([] != ||+1>=||
+>=||-1<0||-<0)
continue;
switch (i)
{
case 1:
if ([+1] != || [+] != )
continue;
case 3:
if ([-1] != || [+] != )
continue;
case 5:
if ([-1] != || [-] != )
continue;
case 7:
if ([+1] != || [-] != )
continue;
}
/* 检查是否已到达终点 */
if (.x == .x && .y == .y)
{
= TRUE;
[]. = ;
break;
}
DWORD g = ((i%2) ? 14:10) + abs([]-[]);
if ([]. == )
{
/* 放入待检链表中 */
[]. = ;
[]. = []. + g;
[]. = (,);
[]. = []. + [].;
[]. = ;
DWORD = [].;
if (.() == 0 )
.((LPVOID),[].);
else
{
.();
while (TRUE)
{
if (.()>=)
{
.((LPVOID),[].);
break;
}
if (!.())
{
.((LPVOID),[].,::);
break;
}
}
}
}
else if ([]. == )
{
/* 如果将当前点设为父 G(距起点) 值是否更小 */
if ([]. > []. + g)
{
[]. = ;
[]. = []. + g;
[]. = []. + [].;
}
}
}
}
return ;
}
BOOL WINAPI NextPath(
* ,
POINT* )
{
DWORD = ->y*->+->x;
if (>=0 && <->)
{
* = ->[].;
return TRUE;
}
return FALSE;
}