📄 games_fallball_path.c
字号:
==================================================================================================*/
static OP_BOOLEAN IsFloor(PosType pos)
{
OP_INT16 *box = OP_NULL;
box = GetBox();
if(pos.x < 0 || pos.x >= MAX_COLUMN || pos.y < 0 || pos.y >= MAX_LINE )
{
return OP_FAIL;
}
if( *(box+pos.y*MAX_COLUMN+pos.x) == FLOOR )
{
return OP_SUCCESS;
}
return OP_FAIL;
}
/*==================================================================================================
FUNCTION: Add
DESCRIPTION:
Description of this specific function.
ARGUMENTS PASSED:
RETURN VALUE:
IMPORTANT NOTES:
==================================================================================================*/
OP_BOOLEAN Add(PathElem *Path,OP_INT8 CurPath, PathElem *Elem)
{
op_memcpy(Path+CurPath, Elem,sizeof(PathElem));
FootMark(Elem->seat);
return OP_SUCCESS;
}
/*==================================================================================================
FUNCTION: Del
DESCRIPTION:
Description of this specific function.
ARGUMENTS PASSED:
RETURN VALUE:
IMPORTANT NOTES:
==================================================================================================*/
OP_BOOLEAN Del(PathElem *Path,OP_INT8 CurPath,PathElem *Elem)
{
op_memcpy(Elem,Path+CurPath,sizeof(PathElem));
op_memset(Path+CurPath, 0, sizeof(PathElem));
FootMark(Elem->seat);
return OP_SUCCESS;
}
/*==================================================================================================
FUNCTION: SearchBallRoute
DESCRIPTION:
Description of this specific function.
ARGUMENTS PASSED:
RETURN VALUE:
IMPORTANT NOTES:
==================================================================================================*/
OP_BOOLEAN SearchBallRoute(PosType start,PosType end)
{
PosType CurPos;
PathElem Elem;
PathElem TempPath[MAX_LINE*MAX_COLUMN]={0};
OP_INT16 CurStep = 1;
OP_INT8 choice,distance;
OP_INT8 i,j;
op_debug(DEBUG_MED,"SearchBallRoute()\n");
op_memset(FallballPath,0,sizeof(FallballPath));
FallballCurPath = MAX_LINE*MAX_COLUMN;
for(i=0;i<MAX_LINE;i++)
{
for(j=0;j<MAX_COLUMN;j++)
{
mark[i][j] = OP_FALSE;
}
}
distance = GetDistance(start,end);
CurPos.x = start.x;
CurPos.y = start.y;
do {
/* If the current position can be passed,then add it to the route and get the next position.*/
if(Pass(CurPos) == OP_SUCCESS ||
(CurPos.x == start.x && CurPos.y == start.y && mark[start.y][start.x] == OP_FALSE) )
{
Elem.ord = CurStep;
Elem.seat.x = CurPos.x;
Elem.seat.y = CurPos.y;
Elem.di = 1;
Add(TempPath,CurStep-1,&Elem);
op_debug(DEBUG_MED, "Add:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep++;
choice = GetChoice(Elem.seat,end);
NextPos(Elem.seat,Elem.di,choice,&CurPos);
if(Elem.seat.x == end.x && Elem.seat.y == end.y)
{
/* If the current step is less than last time's path step,then replace it.*/
if(CurStep<FallballCurPath)
{
op_memcpy(FallballPath,TempPath,sizeof(TempPath));
FallballCurPath = CurStep;
}
if(CurStep-2 == distance)
{
break;
}
/* If it can't be go ahead,then go back.*/
if(CurStep>1)
{
for(i=1;i<=4;i++)
{
Del(TempPath,CurStep-2,&Elem);
op_debug(DEBUG_MED, "Del:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep--;
}
while(Elem.di == 4 && CurStep > 1)
{
Del(TempPath,CurStep-2,&Elem);
op_debug(DEBUG_MED, "Del:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep--;
}
if(Elem.di < 4)
{
Elem.di++;
Add(TempPath,CurStep-1,&Elem);
op_debug(DEBUG_MED, "Add:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep++;
choice = GetChoice(Elem.seat,end);
NextPos(Elem.seat,Elem.di,choice,&CurPos);
}
}
break;
}
/*If the current step is more than the last time's path step,then stop and go back.*/
if(CurStep >= FallballCurPath)
{
for(i=1;i<=2;i++)
{
Del(TempPath,CurStep-2,&Elem);
op_debug(DEBUG_MED, "Del:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep--;
}
while(Elem.di == 4 && CurStep > 1)
{
Del(TempPath,CurStep-2,&Elem);
op_debug(DEBUG_MED, "Del:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep--;
}
if(Elem.di < 4)
{
Elem.di++;
Add(TempPath,CurStep-1,&Elem);
op_debug(DEBUG_MED, "Add:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep++;
choice = GetChoice(Elem.seat,end);
NextPos(Elem.seat,Elem.di,choice,&CurPos);
}
}
op_debug(DEBUG_MED, "CurStep = %d\n",CurStep);
}
else
{
/*If the current position can't be passed,then chance the direction or go back.*/
if(CurStep>1)
{
Del(TempPath,CurStep-2,&Elem);
op_debug(DEBUG_MED, "Del:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep--;
while(Elem.di == 4 && CurStep > 1)
{
Del(TempPath,CurStep-2,&Elem);
op_debug(DEBUG_MED, "Del:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep--;
}
if(CurStep>1)
{
PathElem TempElem;
Del(TempPath,CurStep-2,&TempElem);
CurStep--;
choice = GetChoice(TempElem.seat,end);
Add(TempPath,CurStep-1,&TempElem);
CurStep++;
}
if(Elem.di < 4)
{
OP_INT8 TempChoice;
Elem.di++;
Add(TempPath,CurStep-1,&Elem);
op_debug(DEBUG_MED, "Add:No[%d] Elem = [%d][%d]\n",Elem.ord,Elem.seat.x,Elem.seat.y);
op_debug(DEBUG_MED,"Elem.di=[%d]\n",Elem.di);
CurStep++;
if(CurStep == 2)
{
int label=9;
}
TempChoice = GetChoice(Elem.seat,end);
if( (CurStep>2) && (Elem.di == 3 || Elem.di == 4) && ( choice == TempChoice ) )
{
if( choice %2 == 0 )
{
choice -= 1;
}
else
{
choice += 1;
}
}
else
{
choice = TempChoice;
}
NextPos(Elem.seat,Elem.di,choice,&CurPos);
}
}
}
}while(CurStep>1);
OptimizeRoute();
op_debug(DEBUG_MED,"Pathstep=%d\n",FallballCurPath);
for(i=0;i<FallballCurPath;i++)
{
op_debug(DEBUG_MED, "Path[%d] = [%d][%d]\n",i,FallballPath[i].seat.x,FallballPath[i].seat.y);
}
return OP_SUCCESS;
}
/*==================================================================================================
FUNCTION: OptimizeRoute
DESCRIPTION:
Description of this specific function.
ARGUMENTS PASSED:
RETURN VALUE:
IMPORTANT NOTES:
==================================================================================================*/
static void OptimizeRoute()
{
PathElem TempPath[MAX_LINE*MAX_COLUMN]={0};
PathElem TempElem;
OP_INT8 i,j,k,flag,cursor=0;
Add(TempPath,cursor,&FallballPath[0]);
cursor++;
for(i=0;i<=FallballCurPath-3;i++)
{
flag = i+1;
for(j=i+1;j<=FallballCurPath-2;j++)
{
if( JudgeLinePath(FallballPath[i].seat,FallballPath[j].seat) == OP_SUCCESS )
{
flag = j;
}
}
if(flag > i+1)
{
if(FallballPath[i].seat.x == FallballPath[flag].seat.x)
{
TempElem.seat.x = FallballPath[i].seat.x;
if(FallballPath[i].seat.y < FallballPath[flag].seat.y)
{
for(k=FallballPath[i].seat.y+1;k<=FallballPath[flag].seat.y;k++)
{
TempElem.seat.y = k;
Add(TempPath,cursor,&TempElem);
cursor++;
}
}
else
{
for(k=FallballPath[i].seat.y-1;k>=FallballPath[flag].seat.y;k--)
{
TempElem.seat.y = k;
Add(TempPath,cursor,&TempElem);
cursor++;
}
}
}
else
{
TempElem.seat.y = FallballPath[i].seat.y;
if(FallballPath[i].seat.x < FallballPath[flag].seat.x)
{
for(k=FallballPath[i].seat.x+1;k<=FallballPath[flag].seat.x;k++)
{
TempElem.seat.x = k;
Add(TempPath,cursor,&TempElem);
cursor++;
}
}
else
{
for(k=FallballPath[i].seat.x-1;k>=FallballPath[flag].seat.x;k--)
{
TempElem.seat.x = k;
Add(TempPath,cursor,&TempElem);
cursor++;
}
}
}
}
else
{
Add(TempPath,cursor,&FallballPath[flag]);
cursor++;
}
i = flag-1;
}
op_memset(FallballPath,0x0, sizeof(FallballPath));
op_memcpy(FallballPath,TempPath,cursor*sizeof(PathElem));
FallballCurPath = cursor+1;
}
/*==================================================================================================
FUNCTION: JudgeLinePath
DESCRIPTION:
If there is a beeline path between the start point and end point, return success,else return fail.
ARGUMENTS PASSED:
RETURN VALUE:
IMPORTANT NOTES:
==================================================================================================*/
static OP_BOOLEAN JudgeLinePath(PosType start, PosType end)
{
OP_INT8 i;
PosType position;
OP_BOOLEAN result = OP_SUCCESS;
if(start.x == end.x)
{
position.x = start.x;
if(start.y < end.y)
{
for(i=start.y+1;i<end.y;i++)
{
position.y = i;
if(IsFloor(position) == OP_FAIL)
{
result = OP_FAIL;
break;
}
}
}
else
{
for(i=start.y-1;i>end.y;i--)
{
position.y = i;
if(IsFloor(position) == OP_FAIL)
{
result = OP_FAIL;
break;
}
}
}
}
else if(start.y == end.y)
{
position.y = start.y;
if(start.x < end.x)
{
for(i=start.x+1;i<end.x;i++)
{
position.x = i;
if(IsFloor(position) == OP_FAIL)
{
result = OP_FAIL;
break;
}
}
}
else
{
for(i=start.x-1;i>end.x;i--)
{
position.x = i;
if(IsFloor(position) == OP_FAIL)
{
result = OP_FAIL;
break;
}
}
}
}
else
{
result = OP_FAIL;
}
return result;
}
/*==================================================================================================*/
#endif
#ifdef __cplusplus
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -