Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My own try at A*
#81
Well, the reason it looks like mine is because I didn't do diagonal scoring. I probably should've. Oh well...
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#82
Try sticking the distance formula in there. :lol:
Reply
#83
I have to admit, walking through walls makes moving a lot easier! For example, try this map using dumbledore's code and then try Torahteen's (since mine does the same thing, only with ASCII):
Code:
40,20
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
4,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,0,0,0,0,2,4
4,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,6,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4
4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,3,3,3,3,5,4
4,3,1,1,1,1,3,3,3,3,2,2,2,2,1,1,1,1,3,3,3,3,4,4,4,4,1,1,1,1,3,3,3,3,3,3,3,3,1,4
4,1,3,3,3,3,2,2,2,2,1,1,1,1,3,3,3,3,4,4,4,4,1,1,1,1,3,3,3,3,3,3,3,3,1,1,1,1,3,4
4,3,1,1,1,1,1,1,1,1,3,3,3,3,4,4,4,4,1,1,1,1,3,3,3,3,2,2,2,2,1,1,1,3,3,3,3,4,4,4
4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,4
4,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,4
4,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4
4,4,4,4,4,1,1,1,1,3,3,3,3,3,3,3,3,1,1,1,1,3,3,3,3,2,2,2,2,1,1,1,1,3,3,3,3,4,4,4
4,1,1,1,1,3,3,3,3,3,3,3,3,1,1,1,1,3,3,3,3,1,1,1,1,1,1,1,1,3,3,3,3,4,4,4,1,1,1,4
4,3,3,3,2,2,2,2,1,1,1,1,3,3,3,3,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,1,1,1,1,3,3,3,4
4,2,2,2,1,1,1,1,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4
4,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,4
4,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,4
4,4,4,4,0,0,0,0,2,2,2,2,3,3,3,3,4,4,4,4,1,1,1,1,3,3,3,3,3,3,3,3,1,1,1,1,3,3,3,4
4,1,1,1,1,1,1,1,3,3,3,4,4,4,4,1,1,1,1,3,3,3,3,2,2,2,2,1,1,1,1,3,3,3,3,1,1,1,1,4
4,1,1,3,3,3,3,3,3,3,3,1,1,1,1,3,3,3,3,2,2,2,2,1,1,1,1,3,3,3,3,4,4,4,4,1,1,1,1,4
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4

You'll have to change dumbledore's slightly to make it open it from an external file (or type DATA for every line. . .). Also, dumbledore's code is ignorant of all obstacles (1-3) except walls, which appears to make him use an extra step (adds to the gScore).

Maybe we should give a name to the solving of this phenomenon of walking through walls in programming. . . Something like "no-ghost code". . . (cheesy, I know :rotflSmile
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#84
For those who don't know what the distance formula is (and for those who are too lazy to look it up):

SQR((x2 - x1) ^ 2 + (y2 - y1) ^ 2)

where the coordinates (x2, y2) and (x1, y1) refer to the current node/square and the possible next node/square. The order doesn't matter since the result of the calculation is squared, which also means you don't have to add in the absolute value. Smile

Unless Dr. D is referring to a distance formula other than the standard mathematical distance formula. . .

Edit: sorry for the double post. . .
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#85
Nope, that's what I'm talking about. It really slows it down though. Wink
Reply
#86
hehe, hills / elevation would be hard to implement, you'd have to be able to figure if you're going up / downhill and everything, ow... it would have to detect if the square you're walking to is elevated, then check if the square you're on is elevated, and calc the difference, that'd be a quick way to slow it down ;P
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply
#87
Hehe. . . Imagine trying to design a game that uses elevation where you can't enter from a certain side (like at the bottom of a cliff where it goes straight up) :???:

I just generated a map, dumbledore. Your code appears to move on/through the walls unless you set a SCREEN because of the height (which makes it scroll) and the way your code is designed, so I recommend adding a SCREEN statement (probably before you get the data). Here is the map:
Code:
50,25
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
4,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4
4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,4,4,4,4,0,0,0,0,2,2,2,2,3,3,3,3,5,4,4,4,1,1,1,1,3,3,3,4
4,2,2,2,1,1,1,1,3,3,3,3,1,1,1,1,1,1,1,3,6,3,3,3,3,3,3,1,1,1,1,3,3,3,3,2,2,2,2,1,1,1,1,3,3,3,3,4,4,4
4,1,1,1,1,3,3,3,3,3,3,3,3,1,1,1,1,3,3,3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,4,4,4,4,4,4

I've updated my code to show a more illustrative representation. It goes step by step like dumbledore's code, but the generation is based on Torahteen's. It's slightly different because it includes diagonal movement, so beware!
Code:
DEFINT A-Z

DECLARE FUNCTION addPath(x, y)
DECLARE FUNCTION drawPath()
DECLARE FUNCTION getPath()
DECLARE FUNCTION isClosed(x, y)
DECLARE FUNCTION isOpened(x, y)

TYPE coordType
    AS INTEGER x, y
END TYPE

TYPE nodeType
    AS INTEGER terrain, fScore, gScore, hScore
    parent AS coordType
END TYPE

CONST false = 0
CONST true = NOT false

CONST ground = 0
CONST hill = 1
CONST water = 2
CONST cliff = 3
CONST wall = 4
CONST start = 5
CONST goal = 6

CONST groundCost = 10
CONST hillCost = 25
CONST waterCost = 50
CONST cliffCost = 100

DIM SHARED mapW, mapH

SCREEN 18,,2
OPEN "board.brd" FOR INPUT AS #1
INPUT #1, mapW, mapH
IF mapW = 0 AND mapH = 0 THEN PRINT "board.brd not found!": SLEEP: END
DIM SHARED map(1 TO mapW, 1 TO mapH) AS nodeType
DIM SHARED AS coordType startNode, goalNode
DIM SHARED AS coordType path(), openedNodes(), closedNodes()

FOR y = 1 TO mapH
    FOR x = 1 TO mapW
        INPUT #1, map(x, y).terrain
        IF map(x, y).terrain = start THEN
            startNode.x = x
            startNode.y = y
        ELSEIF map(x, y).terrain = goal THEN
            goalNode.x = x
            goalNode.y = y
        END IF
    NEXT x
NEXT y
CLOSE #1

LOCATE 23, 1
PRINT "Finding path. . ."
SCREENCOPY 1, 2

getPath

PRINT "Path found! Generating map. . ."
SCREENCOPY 1, 2

drawPath

PRINT UBOUND(path) 'number of moves
SCREENCOPY 1, 2
SLEEP

END

FUNCTION addPath(x, y)
    REDIM PRESERVE path(UBOUND(path) + 1) AS coordType
    
    path(UBOUND(path)).x = x
    path(UBOUND(path)).y = y
END FUNCTION

FUNCTION drawPath
    FOR y = 1 TO mapH
        FOR x = 1 TO mapW
            SELECT CASE map(x, y).terrain
            CASE ground: colour = 6
            CASE hill: colour = 2
            CASE water: colour = 1
            CASE cliff: colour = 0
            CASE wall: colour = 8
            CASE start: colour = 5
            CASE goal: colour = 4
            END SELECT
            LINE (x * 10, y * 10)-(x * 10 + 10, y * 10 + 10), colour, BF
        NEXT x
    NEXT y
    PRINT "Map generation complete!"
    SCREENCOPY 1, 2
    targetx = goalNode.x
    targety = goalNode.y
    FOR i = 1 TO UBOUND(path)
        x = path(i).x
        y = path(i).y
        
        LINE (targetx * 10 + 5, targety * 10 + 5)-(x * 10 + 5, y * 10 + 5), 15
        SLEEP
        targetx = x
        targety = y
    NEXT i
END FUNCTION

FUNCTION getPath
    DIM cNode AS coordType
    cNode.x = startNode.x
    cNode.y = startNode.y
    
    DO
        REDIM PRESERVE closedNodes(UBOUND(closedNodes) + 1) AS coordType
        closedNodes(UBOUND(closedNodes)).x = cNode.x
        closedNodes(UBOUND(closedNodes)).y = cNode.y
        
        FOR yCoord = -1 TO 1
            FOR xCoord = -1 TO 1
                x = cNode.x + xCoord
                y = cNode.y + yCoord
                IF (map(x, y).terrain <> wall) AND (map(cNode.x, y).terrain <> wall) AND (map(x, cNode.y).terrain <> wall) THEN
                    IF NOT isClosed(x, y) THEN
                        IF NOT isOpened(x, y) THEN
                            IF map(x, y).terrain = ground THEN
                                map(x, y).gScore = map(cNode.x, cNode.y).gScore + groundCost
                            ELSEIF map(x, y).terrain = hill THEN
                                map(x, y).gScore = map(cNode.x, cNode.y).gScore + hillCost
                            ELSEIF map(x, y).terrain = water THEN
                                map(x, y).gScore = map(cNode.x, cNode.y).gScore + waterCost
                            ELSEIF map(x, y).terrain = cliff THEN
                                map(x, y).gScore = map(cNode.x, cNode.y).gScore + cliffCost
                            END IF
                            
                            map(x, y).hScore = SQR((cNode.x - goalNode.x) ^ 2 + (cNode.y - goalNode.y) ^ 2)
                            
                            map(x, y).fScore = map(x, y).gScore + map(x, y).hScore
                            
                            map(x, y).parent.x = cNode.x
                            map(x, y).parent.y = cNode.y
                            
                            REDIM PRESERVE openedNodes(UBOUND(openedNodes) + 1) AS coordType
                            openedNodes(UBOUND(openedNodes)).x = x
                            openedNodes(UBOUND(openedNodes)).y = y
                            
                            IF x = goalNode.x AND y = goalNode.y THEN goalFound = true
                        ELSE
                            IF map(x, y).terrain = ground THEN
                                tempGScore = map(cNode.x, cNode.y).gScore + groundCost
                            ELSEIF map(x, y).terrain = hill THEN
                                tempGScore = map(cNode.x, cNode.y).gScore + hillCost
                            ELSEIF map(x, y).terrain = water THEN
                                tempGScore = map(cNode.x, cNode.y).gScore + waterCost
                            ELSEIF map(x, y).terrain = cliff THEN
                                tempGScore = map(cNode.x, cNode.y).gScore + cliffCost
                            END IF
                            
                            IF tempGScore < map(x, y).gScore THEN
                                map(x, y).parent.x = cNode.x
                                map(x, y).parent.y = cNode.y
                                
                                map(x, y).gScore = tempGScore
                                
                                map(x, y).fScore = map(x, y).gScore + map(x, y).hScore
                            END IF
                        END IF
                    END IF
                END IF
            NEXT xCoord
        NEXT yCoord
        
        score = 500000
        
        FOR i = 1 TO UBOUND(openedNodes)
            IF NOT isClosed(openedNodes(i).x, openedNodes(i).y) THEN
                IF map(openedNodes(i).x, openedNodes(i).y).fScore <= score THEN
                    cNode.x = openedNodes(i).x
                    cNode.y = openedNodes(i).y
                    
                    score = map(openedNodes(i).x, openedNodes(i).y).fScore
                END IF
            END IF
        NEXT i
    LOOP UNTIL goalFound = true
    
    cNode.x = goalNode.x
    cNode.y = goalNode.y
    
    DO
        addPath cNode.x, cNode.y
        
        IF cNode.x = startNode.x AND cNode.y = startNode.y THEN goalReached = true
        
        x = cNode.x
        y = cNode.y
        
        cNode.x = map(x, y).parent.x
        cNode.y = map(x, y).parent.y
    LOOP UNTIL goalReached = true
END FUNCTION

FUNCTION isClosed(x, y)
    FOR i = 1 TO UBOUND(closedNodes)
        IF x = closedNodes(i).x AND y = closedNodes(i).y THEN isClosed = true: EXIT FOR
    NEXT i
END FUNCTION

FUNCTION isOpened(x, y)
    FOR i = 1 TO UBOUND(openedNodes)
        IF x = openedNodes(i).x AND y = openedNodes(i).y THEN isOpened = true: EXIT FOR
    NEXT i
END FUNCTION

Try using the map above to test. Again, I take no credit for any of the code. Any optimizations or criticism (other than code length) is welcome.
974277320612072617420666C61696C21 (Hexadecimal for those who don't know)
Reply
#88
Hmm... It won't work on my computer. It can't find the Board.brd file. Any clue?
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#89
mine worked fine for me. it should only look like it's going through walls if you're on windows 98 or 95 Tongue and besides, the point behind mine was to be able to make it into a function, therefore what it looks like is irrelevant.

btw, here's my new code, modified with constants and reads from a file, if you're too lazy to hack it yourself:

Code:
#include once "crt.bi"
#define WALL 4
#define FINISH 5
#define START 6

option explicit
option escape

type coord
    x as ubyte
    y as ubyte
end type
type node
    x as ubyte
    y as ubyte
    g as integer
    f as integer
    p as node ptr
end type

declare function timesvisited( byval x as integer, byval y as integer ) as byte
declare function outofbounds( byval x as integer, byval y as integer ) as byte
declare function onclosedlist( byref what as coord ) as byte
declare function removefromopenlist( byval idx as integer ) as byte
declare function onopenlist( byref what as coord ) as byte
declare sub _read( byref var as integer )

redim preserve shared openlist( 0 ) as node ptr, closedlist( 1 ) as coord, allnodes( 0 ) as node ptr
dim as integer rx, ry, cx, cy, ex, ey, i
dim as string yn

input "show path? [y/n]:", yn
yn = ucase$( left$( yn, 1 ) )

cls
dim shared as integer mx, my
open "board.brd" for binary as #1
_read mx
_read my
dim as integer room( mx * my )
for ry = 0 to my - 1
    for rx = 0 to mx - 1
        _read room( rx + ry * mx )
        select case room( rx + ry * mx )
        case WALL
            ? "|";
        case START
            ? "@";
            cx = rx
            cy = ry
            closedlist( 0 ).x = rx
            closedlist( 0 ).y = ry
        case FINISH
            ? "*";
            ex = rx
            ey = ry
        case else
            ? " ";
        end select
    next
    ?
next

if yn = "Y" then sleep

dim as node ptr tmpnode
tmpnode = callocate( len( node ) )
tmpnode->x = cx
tmpnode->y = cy
allnodes( ubound( allnodes ) ) = tmpnode
redim preserve shared allnodes( ubound( allnodes ) + 1 )
while cx <> ex or cy <> ey
    
    dim as integer max = 100, aye, x, y
    
    aye = 0
    
    for x = -1 to 1
        for y = -1 to 1 step 1 - ( x = 0 )
            
            dim as coord working
            working.x = cx + x
            working.y = cy + y
            
            if onclosedlist( working ) = 0 and onopenlist( working ) = 0 and _
               room( cx + x + ( cy + y ) * mx ) <> WALL and _
               room( cx + ( cy + y ) * mx ) <> WALL and _
               room( cx + x + cy * mx ) <> WALL and _
               outofbounds( cx + x, cy + y ) = 0 then
                
                openlist( ubound( openlist ) ) = callocate( len( node ) )
                dim as node ptr temp
                temp = openlist( ubound( openlist ) )  '' use a temp var so we don't have to
                                                       '' keep using ubound()
                temp->x = working.x
                temp->y = working.y
                
                temp->f = 10 * sqr( abs( x ) + abs( y ) ) + _                   '' the g score
                          10 * sqr( ( cx + x - ex ) ^ 2 + ( cy + y - ey ) ^ 2 ) '' the h score
                if temp->f > max then max = temp->f
                temp->p = tmpnode
                
                allnodes( ubound( allnodes ) ) = temp
                redim preserve shared allnodes( ubound( allnodes ) + 1 )
                
                redim preserve shared openlist( ubound( openlist ) + 1 )
                
            end if
            
        next
    next
    
    dim as integer lowest, lowestidx = 0
    lowest = max + 1
    for i = 0 to ubound( openlist ) - 1
        if openlist( i )->f < lowest and openlist( i )->f <> -1 then
            lowest = openlist( i )->f
            lowestidx = i
        end if
    next
    
    if yn = "Y" then locate cy + 1, cx + 1: ? " "
    
    if lowest <> max + 1 then
        cx = openlist( lowestidx )->x
        cy = openlist( lowestidx )->y
    end if
    
    if yn = "Y" then locate cy + 1, cx + 1: ? "@"
    
    closedlist( ubound( closedlist ) ).x = cx
    closedlist( ubound( closedlist ) ).y = cy
    redim preserve shared closedlist( ubound( closedlist ) + 1 )
    
    tmpnode = openlist( lowestidx )
    removefromopenlist( lowestidx )
    
    if yn = "Y" then sleep
    
wend

'' as a function this would return tmpnode

while tmpnode <> 0
    locate tmpnode->y + 1, tmpnode->x + 1
    ? "*"
    tmpnode = tmpnode->p
wend

'' it will be necessary to clean up memory afterward, otherwise we'll end up with
'' a leak the size of the pacific ocean ;P

for i = 0 to ubound( allnodes )
    
    deallocate allnodes( i )
    
next

sleep

end

function outofbounds( byval x as integer, byval y as integer ) as byte
    
    return x < 0 or x > mx - 1 or y < 0 or y > my - 1
    
end function

function onclosedlist( byref what as coord ) as byte
    
    dim as integer i
    for i = 0 to ubound( closedlist ) - 1
        if memcmp( @closedlist( i ), @what, len( coord ) ) = 0 then return -1
    next
    return 0
    
end function

function removefromopenlist( byval idx as integer ) as byte
    
    if idx > ubound( openlist ) then return 0
    dim as integer i
    for i = idx to ubound( openlist ) - 1
        openlist( i ) = openlist( i + 1 )
    next
    redim preserve openlist( ubound( openlist ) - 1 )
    
    return -1
    
end function

function onopenlist( byref what as coord ) as byte
    
    dim as integer i
    for i = 0 to ubound( openlist ) - 1
        if memcmp( openlist( i ), @what, len( coord ) ) = 0 then return -1
    next
    return 0
    
end function

sub _read( byref var as integer )
    
    dim as ubyte char
    dim as string thing
    
    get #1, , char
    while char <> asc( "," ) and char <> asc( "\n" ) and eof( 1 ) = 0
        thing += chr$( char )
        get #1, , char
    wend
    
    var = val( thing )
    
end sub
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply
#90
btw, you could optimize your for loops by using something like this:

Code:
for x = -1 to 1
        for y = -1 to 1 step 1 - ( x = 0 )

instead. this way it doesn't check the square you're currently "standing" on, since it's on the closed list automatically anyway.
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side
...phear
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)