All pastes #28596 Raw Edit

Someone

public text v1 · immutable
#28596 ·published 2005-11-13 18:54 UTC
rendered paste body
View.Set ("graphics:max,max")
const CITY := 1
const GRASS := 2
const FOREST := 3
const MOUNTAIN := 4
const ROAD := 5
const SHALLOW := 6
const WATER := 7
const SHORE := 8

var openListx, openListy, openListg, openListh, openListf : flexible array 1 .. 1 of int
var closeListx, closeListy : flexible array 1 .. 1 of int
var map : array 1 .. 100, 1 .. 60 of int
var mapPass : array 1 .. 100, 1 .. 60 of boolean
var terrainTypes : array 1 .. 8 of int := init (42, 48, 2, 66, 15, 53, 55, 91)
var mx, my, mb, gridx, gridy, font, readtest, lowest : int
var parentx, parenty, startx, starty, endx, endy, tempx, tempy, count, endcount : int := 0

font := Font.New ("Verdana:10")
startx := 9
parentx := startx
starty := 10
parenty := starty
endx := 60
endy := 40

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%PROCESSES

proc drawMap
    %DRAWS THE MAP
    for i : 1 .. 100
        for j : 1 .. 60
            drawbox ((i * 10), (j * 10), (i * 10) + 10, (j * 10) + 10, black)
        end for
    end for
end drawMap

proc grid (var gridx, gridy : int, var mapPass : array 1 .. *, 1 .. * of boolean)
    % CHECKS GRID FOR PASSIBILITY - IF RED THEN CANNOT PASS
    mousewhere (mx, my, mb)
    gridx := mx div 10
    gridy := my div 10
    for i : 1 .. 100
        for j : 1 .. 60
            if whatdotcolor (((i * 10) + 1), ((j * 10) + 1)) = red then
                mapPass (i, j) := true
            end if
        end for
    end for
end grid

proc fillgrid
    % LAYS DOWN AN OBSTACLE EVERYTIME CLICKED
    if mb = 1 then
        drawfill (mx, my, red, black)
    end if
end fillgrid

proc setGotoStop
    Font.Draw ("X", parentx * 10 + 1, parenty * 10 + 1, font, black)
    Font.Draw ("X", endx * 10 + 1, endy * 10 + 1, font, black)
end setGotoStop


proc searchPath
    % SEARCHES A PATH BETWEEN THE OBSTACLES -A* PATHFINDING

    open : readtest, "test.txt", put

    closeListx (1) := parentx
    closeListy (1) := parenty

    count := 1

    loop
    
    exit when closeListx (upper (closeListx)) = endx and closeListy (upper (closeListy)) = endy

        for i : 1 .. 3
            % X AXIS
            for j : 1 .. 3
                %Y AXIS
                tempx := ((startx - 2) + i)
                tempy := ((starty - 2) + j)

                if mapPass (tempx, tempy) not= true then
                    if tempx = startx and tempy = starty then
                    else
                        new openListx, upper (openListx) + 1
                        new openListy, upper (openListx) + 1
                        new openListg, upper (openListx) + 1
                        new openListh, upper (openListx) + 1
                        new openListf, upper (openListx) + 1

                        openListx (count) := tempx
                        openListy (count) := tempy

                        put : readtest, openListx (count), "X", openListy (count), "Y ", i, " ", j, " "

                        % Calculate H
                        openListh (count) := round (abs (sqrt (intreal ((endx - tempx) ** 2) + intreal ((endy - tempy) ** 2))))
                        put : readtest, "H value of :", openListh (count)

                        % Calculate G
                        if (i = 1 and j = 1) or (i = 3 and j = 3) or (i = 1 and j = 3) or (i = 3 and j = 1) then     % Diagonal or Orthogonal?
                            openListg (count) := 14
                        else
                            openListg (count) := 10
                        end if
                        put : readtest, "G value of :", openListg (count)

                        % Calculate F
                        openListf (count) := openListh (count) + openListg (count)
                        put : readtest, "F value of :", openListf (count)
                        drawfilloval (openListx (count) * 10 + 5, openListy (count) * 10 + 5, 2, 2, green)
                        count += 1
                        
                    end if
                end if


            end for
        end for



        % FIND THE LOWEST F VALUE - SCREWS UP IF ONE OR NO SPACE IS AVAILABLE

        for k : 2 + endcount .. count - 1
            if openListf (count - 1) < openListf (k) then
                lowest := openListf (k)

                if k = count - 1 then
                    locate (1, 1)
                    put upper (closeListx) ..
                    new closeListx, upper (closeListx) + 1
                    new closeListy, upper (closeListy) + 1

                    closeListx (upper (closeListx)) := openListx (k)
                    closeListy (upper (closeListy)) := openListy (k)
                    locate (1, 1)
                    put upper (closeListx) ..
                    endcount := count - 1
                    locate (3, 1)
                    put endcount ..

                    startx := closeListx (upper (closeListx))
                    starty := closeListy (upper (closeListy))

                end if
            else
                lowest := openListf (count - 1)
                if k = count - 1 then
                    locate (1, 1)
                    put upper (closeListx) ..
                    new closeListx, upper (closeListx) + 1
                    new closeListy, upper (closeListy) + 1
                    closeListx (upper (closeListx)) := openListx (k)
                    closeListy (upper (closeListy)) := openListy (k)
                    locate (2, 1)
                    put upper (closeListx) ..
                    endcount := count - 1
                    locate (3, 1)
                    put endcount ..

                    startx := closeListx (upper (closeListx))
                    starty := closeListy (upper (closeListy))
                end if
            end if
        end for

        put : readtest, "           "

    end loop


    for d : 1 .. upper (closeListx)
        put : readtest, "DEFINITE SQUARES  ", closeListx (d), "x", closeListy (d), "y ", d
    end for

    close : readtest
end searchPath







%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for z : 1 .. 100
    for h : 1 .. 60
        mapPass (z, h) := false
    end for
end for
drawMap
loop

    grid (gridx, gridy, mapPass)
    fillgrid
    setGotoStop
    locate (1, 150)
    put gridx, " ", gridy, " " ..
    locate (2, 145)
    if mx > 10 and mx < 1010 and my > 10 and my < 610 then
        put mapPass (mx div 10, my div 10)
    else
        put " Not on grid "
    end if

    if mb = 1 and mx >= maxx - 20 then
        searchPath
    end if


end loop