コード例 #1
0
/*
Performs the actual search.
*/
IPath::SearchResult CPathFinder::DoSearch(const MoveData& moveData, const CPathFinderDef& pfDef) {
    bool foundGoal = false;
    while(!openSquares.empty() && openSquareBufferPointer - openSquareBuffer < (maxNodesToBeSearched - 8)) {
        //Gets the open square with lowest expected path-cost.
        OpenSquare* os = openSquares.top();
        openSquares.pop();

        //Check if this OpenSquare-holder have become obsolete.
        if(squareState[os->sqr].cost != os->cost)
            continue;

        //Check if the goal is reached.
        if(pfDef.IsGoal(os->square.x, os->square.y)) {
            goalSquare = os->sqr;
            goalHeuristic = 0;
            foundGoal = true;
            break;
        }

        //Testing the 8 surrounding squares.
        bool right=TestSquare(moveData, pfDef, os, PATHOPT_RIGHT);
        bool left=TestSquare(moveData, pfDef, os, PATHOPT_LEFT);
        bool up=TestSquare(moveData, pfDef, os, PATHOPT_UP);
        bool down=TestSquare(moveData, pfDef, os, PATHOPT_DOWN);
        if(up) {		//we dont want to search diagonally if there is a blocking object (not blocking terrain) in one of the two side squares
            if(right)
                TestSquare(moveData, pfDef, os, (PATHOPT_RIGHT | PATHOPT_UP));
            if(left)
                TestSquare(moveData, pfDef, os, (PATHOPT_LEFT | PATHOPT_UP));
        }
        if(down) {
            if(right)
                TestSquare(moveData, pfDef, os, (PATHOPT_RIGHT | PATHOPT_DOWN));
            if(left)
                TestSquare(moveData, pfDef, os, (PATHOPT_LEFT | PATHOPT_DOWN));
        }

        //Mark this square as closed.
        squareState[os->sqr].status |= PATHOPT_CLOSED;
    }

    //Returning search-result.
    if(foundGoal)
        return Ok;

    //Could not reach the goal.
    if(openSquareBufferPointer - openSquareBuffer >= (maxNodesToBeSearched - 8))
        return GoalOutOfRange;

    //Search could not reach the goal, due to the unit being locked in.
    if(openSquares.empty())
        return GoalOutOfRange;

    //Below shall never be runned.
    *info << "ERROR: CPathFinder::DoSearch() - Unhandled end of search!\n";
    return Error;
}
コード例 #2
0
ファイル: PathFinder.cpp プロジェクト: eXLabT/spring
/**
 * Performs the actual search.
 */
IPath::SearchResult CPathFinder::DoSearch(const MoveData& moveData, const CPathFinderDef& pfDef, int ownerId, bool synced) {
	bool foundGoal = false;

	while (!openSquares.empty() && (openSquareBuffer.GetSize() < maxSquaresToBeSearched)) {
		// Get the open square with lowest expected path-cost.
		PathNode* os = const_cast<PathNode*>(openSquares.top());
		openSquares.pop();

		// check if this PathNode has become obsolete
		if (squareStates[os->nodeNum].fCost != os->fCost)
			continue;

		// Check if the goal is reached.
		if (pfDef.IsGoal(os->nodePos.x, os->nodePos.y)) {
			goalSquare = os->nodeNum;
			goalHeuristic = 0;
			foundGoal = true;
			break;
		}

		// Test the 8 surrounding squares.
		const bool right = TestSquare(moveData, pfDef, os, PATHOPT_RIGHT, ownerId, synced);
		const bool left  = TestSquare(moveData, pfDef, os, PATHOPT_LEFT,  ownerId, synced);
		const bool up    = TestSquare(moveData, pfDef, os, PATHOPT_UP,    ownerId, synced);
		const bool down  = TestSquare(moveData, pfDef, os, PATHOPT_DOWN,  ownerId, synced);

		if (up) {
			// we dont want to search diagonally if there is a blocking object
			// (not blocking terrain) in one of the two side squares
			if (right) { TestSquare(moveData, pfDef, os, (PATHOPT_RIGHT | PATHOPT_UP), ownerId, synced); }
			if (left) { TestSquare(moveData, pfDef, os, (PATHOPT_LEFT | PATHOPT_UP), ownerId, synced); }
		}
		if (down) {
			if (right) { TestSquare(moveData, pfDef, os, (PATHOPT_RIGHT | PATHOPT_DOWN), ownerId, synced); }
			if (left) { TestSquare(moveData, pfDef, os, (PATHOPT_LEFT | PATHOPT_DOWN), ownerId, synced); }
		}

		// Mark this square as closed.
		squareStates[os->nodeNum].nodeMask |= PATHOPT_CLOSED;
	}

	if (foundGoal)
		return IPath::Ok;

	// Could not reach the goal.
	if (openSquareBuffer.GetSize() >= maxSquaresToBeSearched)
		return IPath::GoalOutOfRange;

	// Search could not reach the goal, due to the unit being locked in.
	if (openSquares.empty())
		return IPath::GoalOutOfRange;

	// Below shall never be runned.
	LogObject() << "ERROR: CPathFinder::DoSearch() - Unhandled end of search!\n";
	return IPath::Error;
}
コード例 #3
0
int main(int argc, char* argv[]) {
  TestSine();
  TestSquare();
  TestTriangle();
  TestTriangle2();
  TestSawtooth();
  TestSawtooth2();
  TestReverseSawtooth();
  std::cout << "PASS" << std::endl;
  return 0;
}