Exemple #1
0
uint32 WorldLocation::GetZoneId() const
{
    if (!HasMap())
        return 0;

    return sTerrainMgr.GetZoneId(GetMapId(), coord_x, coord_y, coord_z);
}
uint32 WorldLocation::GetZoneId() const
{
    if (!HasMap())
        return 0;

    return sTerrainMgr.GetZoneId(GetMapId(), getX(), getY(), getZ());
}
bool WorldLocation::operator == (WorldLocation const& loc) const
{
    return (
            (realmid == 0 || realmid == loc.realmid)
        && (!HasMap() || GetMapId()  == loc.GetMapId())
        && (GetInstanceId() == 0 || GetInstanceId() == loc.GetInstanceId())
        && ((Position)*this) == ((Position)loc));
}
float WorldLocation::GetDistance(WorldLocation const& loc) const
{
    return (!HasMap() || !loc.HasMap() || ((GetMapId() == loc.GetMapId()) && (GetInstanceId() == loc.GetInstanceId()))) ?
        ((Position)*this).GetDistance((Position)loc) :
        MAX_VISIBILITY_DISTANCE + 1.0f;
};
bool CQueueMazeSln::Run(int refInterval/*=100*/)
{
	if(!HasMap()) return false;

	refreshInterval=refInterval;


	int x = slnMap.GetInPos().GetW();
	int y = slnMap.GetInPos().GetH();


	path.InitQueue(x,y);
	QueuePtr M;//广度优先的上一层节点信息


	int (*dir)[2]; //指向数组的指针!
	int dir4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};  // 顺序:下右上左
	int dir8[8][2]={{1,1},{1,0},{0,1},{1,-1},{-1,1},{-1,0},{0,-1},{-1,-1}};  // 顺序:右下,下,右,左下,右上,上,左,左上
	if(slnMap.GetDirectionMode()==4){dir=dir4;}
	else {dir=dir8;}

	bool isFinished=false;
	bool findAccess=false;
	bool B;
	char v;
	int count = 0;



	system("cls");
	Draw();
	Sleep(refreshInterval);
	
		

		do
		{
			GetInput();
			M=path.Q.front;
			v=slnMap.GetValue(CPosition(y,x-1));
			if(x!=0 &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y,x-1),MAPVALUE_USED);
					path.EnQueue(x-1,y,M);
					system("cls");
					round++;
					Draw();
					
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			//-----------------------------------------------------
			v=slnMap.GetValue(CPosition(y+1,x-1));
			if(x!=0&&y!= slnMap.GetHeight()  &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y+1,x-1),MAPVALUE_USED);
					path.EnQueue(x-1,y+1,M);
					system("cls");
					round++;
					Draw();
					
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			//-----------------------------------------------------
			v=slnMap.GetValue(CPosition(y+1,x));
			if(y!= slnMap.GetHeight() &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y+1,x),MAPVALUE_USED);
					path.EnQueue(x,y+1,M);
					system("cls");
					round++;
					Draw();
					
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			//------------------------------------------------------
			v=slnMap.GetValue(CPosition(y+1,x+1));
			if(x!=slnMap.GetWidth()&&y!= slnMap.GetHeight()  &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y+1,x+1),MAPVALUE_USED);
					path.EnQueue(x+1,y+1,M);
					system("cls");
					round++;
					Draw();
					
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			//-----------------------------------------------------
			v=slnMap.GetValue(CPosition(y,x+1));
			if(x!=slnMap.GetWidth() &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y,x+1),MAPVALUE_USED);
					path.EnQueue(x+1,y,M);
					system("cls");
					round++;
					Draw();
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			//------------------------------------------------------
			v=slnMap.GetValue(CPosition(y-1,x+1));
			if(x!=slnMap.GetWidth()&&y!= 0  &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y-1,x+1),MAPVALUE_USED);
					path.EnQueue(x+1,y-1,M);
					system("cls");
					round++;
					Draw();
					
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			//------------------------------------------------------
			
			v=slnMap.GetValue(CPosition(y-1,x));
			if(y!=0 &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y-1,x),MAPVALUE_USED);
					path.EnQueue(x,y-1,M);
					system("cls");
					round++;
					Draw();
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			//------------------------------------------------------
			v=slnMap.GetValue(CPosition(y-1,x-1));
			if(x!=0&&y!= 0  &&v!=MAPVALUE_WALL &&v!=MAPVALUE_USED && v!= MAPVALUE_IN)//上
			{
				if(v!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y-1,x-1),MAPVALUE_USED);
					path.EnQueue(x-1,y-1,M);
					system("cls");
					round++;
					Draw();
					
					Sleep(refreshInterval);
				}
				else
				{
					refreshInterval = 100;
					findAccess=true;
					break;
				}
			}
			B=path.DeQueue(x,y,M);
		}while(B==1);



		
	

	if(findAccess)
	{

		if(B==1)
		{
			while(path.MeQueue(x,y,M)==1)
			{
				count++;
				if(slnMap.GetValue(CPosition(y,x))!=MAPVALUE_OUT)
				{
					slnMap.SetValue(CPosition(y,x),MAPVALUE_INPATH);
					system("cls");
					Draw();
				}
				M=M->prier;
			}
		}
		cout<<"A path has been found, the length of the shortest path is "<<count<<endl<<endl;
	}
	else
	{
		cout<<"Sorry,no path can be found."<<endl<<endl;
		return false;
	}

}