All pastes #28415 Raw Edit

Miscellany

public text v1 · immutable
#28415 ·published 2005-11-11 22:35 UTC
rendered paste body
proc pathfinding (unitX, unitY, destX, destY : int) % Variables passed in Grid format!
    var openList : array 1 .. 20 of string
    var closedList : array 1 .. 20 of string
    var tempX, tempY, count, closedCount : int := 1
    var bestSquare : int := 0
    var F, G, H : real
    tempX := unitX
    tempY := unitY

    % Search for valid tiles, and calculate the F values from G and H
    for x : 1 .. 3
        for y : 1 .. 3
            if isPassable (tempX + (-2 + x), tempY + (-2 + y)) then
                for a : 1 .. upper (openList)
                    if index (openList (a), intstr (tempX + (-2 + x)) + intstr (tempY + (-2 + y))) = 0 then     % Is this tile already in the open list?
                        if (x = 1 and y = 1) or (x = 3 and y = 3) or (x = 1 and y = 3) or (x = 3 and y = 1) then    % Diagonal or Orthogonal?
                            G := 1.4
                        else
                            G := 1
                        end if
                        % Calculate H
                        H := abs ((destX - tempX)) + abs ((destY - tempY))
                        F := G + H
                        openList (count) := realstr (F, 2) + " " + intstr (tempX + (-2 + x)) + intstr (tempY + (-2 + y))
                        count += 1
                        F := 0
                        G := 0
                        H := 0
                    end if
                end for
            end if
        end for
    end for

    % Find the 'cheapest' tile to move to
    for i : 1 .. 20
        F := strreal (openList (i) (1 .. findCharInString (openList (i), " ") - 1))
        if F < bestSquare then
            bestSquare := i
        end if
    end for
    
    % Add to closed list
    closedList(closedCount) := openList(i)
    closedCount += 1
end pathfinding