⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 games_cargador_path.c

📁 几个嵌入式手机平台小游戏c源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
						default:
							break;
					}
				break;
			default:
				break;
		}
	return OP_SUCCESS;
}
/*==================================================================================================
    FUNCTION: GetChoice

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
    
==================================================================================================*/
OP_INT8 GetChoice(PosType start,PosType end)
{
	OP_INT8 choice,distx,disty;
	distx = end.x - start.x;
	disty = end.y - start.y;
	if(end.x - start.x > 0)
		{
			if(end.y - start.y > 0)
				{
					if(distx > disty)
						{
							choice = 1;
						}
					else
						{
							choice = 2;
						}
				}
			else
				{
					if(distx > -disty)
						{
							choice = 3;
						}
					else
						{
							choice = 4;
						}
				}
		}
	else
		{
			if(end.y - start.y > 0)
				{
					if(-distx > disty)
						{
							choice = 5;
						}
					else
						{
							choice = 6;
						}
				}
			else
				{
					if(-distx > -disty)
						{
							choice = 7;
						}
					else
						{
							choice = 8;
						}
				}
		}
	return choice;
}
/*==================================================================================================
    FUNCTION: GetDistance

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
    
==================================================================================================*/
OP_INT8 GetDistance(PosType start,PosType end)
{
	OP_INT8 distance = 0;
	distance += end.x > start.x ? (end.x - start.x) : (start.x - end.x);
	distance += end.y > start.y ? (end.y - start.y) : (start.y - end.y);
	return distance;
}
/*==================================================================================================
    FUNCTION: Pass

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
    
==================================================================================================*/
OP_BOOLEAN Pass(PosType pos)
{
	if(mark[pos.y][pos.x] == OP_TRUE)
		{
			return OP_FAIL;
		}
	if( Current_Box[pos.y][pos.x] == ROAD_WHITE
		|| Current_Box[pos.y][pos.x] == GUY_SEAT_ONE
		|| Current_Box[pos.y][pos.x] == GUY_SEAT_TWO
		|| Current_Box[pos.y][pos.x] == ROAD_YELLOW )
		{
			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: SearchRoute

    DESCRIPTION:
        Description of this specific function.

    ARGUMENTS PASSED:

    RETURN VALUE:

    IMPORTANT NOTES:
    
==================================================================================================*/
OP_BOOLEAN SearchRoute(PosType start,PosType end)
{
	PosType CurPos;
	PathElem Elem;
	PathElem TempPath[rowNum*colNum]={0};
	OP_INT8 CurStep = 1;
	OP_INT8 choice,distance;
	OP_INT8 i,j;

	op_debug(DEBUG_MED,"SearchRoute()\n");
	op_memset(CargadorPath,0,sizeof(CargadorPath));
	CargadorCurPath = rowNum*colNum;
	for(i=0;i<rowNum;i++)
		{
		for(j=0;j<colNum;j++)
			{
				mark[i][j] = OP_FALSE;
			}
		}
	choice = GetChoice(start,end);
	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)
			{
				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);
				CurStep++;
				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<CargadorCurPath)
						{
							op_memcpy(CargadorPath,TempPath,sizeof(TempPath));
							CargadorCurPath = 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);
									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);
									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);
									CurStep++;
									NextPos(Elem.seat,Elem.di,choice,&CurPos);
								}
						}
					}
				/*If the current step is more than the last time's path step,then stop and go back.*/
				if(CurStep >= CargadorCurPath)
					{
						for(i=1;i<=2;i++)
						{
							Del(TempPath,CurStep-2,&Elem);
							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);
							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);
							CurStep++;
							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);
						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);
									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);
									CurStep++;
									NextPos(Elem.seat,Elem.di,choice,&CurPos);
								}
					}
			}
		}while(CurStep>1);
	for(i=1;i<CargadorCurPath;i++)
		{
			op_debug(DEBUG_MED, "Path[%d] = [%d][%d]\n",i,CargadorPath[i].seat.x,CargadorPath[i].seat.y);
		}
	return OP_SUCCESS;
}

/*==================================================================================================*/

#endif

#ifdef __cplusplus
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -