改善されたAce Taソースコード

3512 ワード

その他のソースコード

void User::AstarGo(const int& inStartX, const int& inStartY, 
			const int& EndX, const int& EndY)
{

	Pos destPos = { EndX , EndY };
	Pos startPos = { inStartX, inStartY };

	const int direction = FOUR;
	const int size = mIsoInform->GetMapSize();

	vector<vector<bool>> closed(size, vector<bool>(size, false));
	vector<vector<int>> best(size, vector<int>(size, INT32_MAX));

	// 부모 추적 용도
	map<Pos, Pos> parent;

	int cost[] =
	{
		10,
		10,
		10,
		10,
		14, // UP
		14, // LEFT
		14, // DOWN
		14, // RIGHT		
	};

	vector<Pos> nextDir;
	//4방면 또는 8방면 선택하는 구간 
	if (direction == 4)
	{
		nextDir = { {0,-1}, {-1,0}, {1,0}, {0,1} };
	}
	else
	{
		nextDir = { {0,-1}, {0,1}, {1,0}, {-1,0}
			,{1,1},{1,-1},{-1,-1},{-1,1} };
	}

	priority_queue<PQNode, vector<PQNode>, greater<PQNode>> pq;
	int g = 0;
	int h = 10 * (abs(destPos.y - startPos.y) + abs(destPos.x - startPos.x));
	pq.push(PQNode{ g + h, g, startPos });
	best[startPos.x][startPos.y] = g + h;
	parent[startPos] = startPos;


	while (pq.empty() == false)
	{
		PQNode node = pq.top();
		pq.pop();

		if (closed[node.pos.x][node.pos.y])
			continue;
		if (best[node.pos.x][node.pos.y] < node.f)
			continue;

		closed[node.pos.x][node.pos.y] = true;

		// 종료 처리
		if (node.pos == destPos)
			break;

		for (int dir = 0; dir < direction; dir++)
		{
			Pos nextPos = node.pos + nextDir[dir];
			
			if (CanGo(nextPos) == false)
				continue;

			if (closed[nextPos.x][nextPos.y])
				continue;

			// 비용 계산
			int g = node.g + cost[dir];
			int h = 10 * (abs(destPos.y - nextPos.y) + abs(destPos.x - nextPos.x));
			
			if (best[nextPos.x][nextPos.y] <= g + h)
				continue;

			best[nextPos.x][nextPos.y] = g + h;
			pq.push(PQNode{ g + h, g, nextPos });
			mIsoInform->SetTileState(ISOTILE_BFS_READY, nextPos.x, nextPos.y);
			vOrigin.push_back({ nextPos.x, nextPos.y });
			parent[nextPos] = node.pos;
		}
	}
	startPos = destPos;
	while (true)
	{
		way.emplace_back(startPos);
		mIsoInform->SetTileState(ISOTILE_BFS_GO, startPos.x, startPos.y);
		vOrigin.push_back({ startPos.x, startPos.y });

		if (startPos == parent[startPos])
			break;

		startPos = parent[startPos];
	}
	goAhead = true;

}