Amoeba Pathfinding by Elthan17th Nov 2004 9:27
|
---|
Summary Easy, fast and completed pathfinding in -80 lines (No media). Description Are you a newbie in IA questions? With this code, you only must know how to use user defined types and arrays. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com rem Amoeba PAthfinding by Elthan rem (www.elthanart.tk // www.darkbasico.tk) rem From Spanish Community set display mode 800,600,32 sync on : sync rate 30 REM type user define for array mapa(=Map) type datos obstaculo as integer generacion as integer xPadre as integer yPadre as integer endtype rem Map dim mapa(6,6) as datos rem Walls mapa(3,1).obstaculo=1 mapa(3,2).obstaculo=1 : mapa(5,2).obstaculo=1 mapa(2,3).obstaculo=1 : mapa(3,3).obstaculo=1 : mapa(5,3).obstaculo=1 mapa(5,4).obstaculo=1 mapa(1,5).obstaculo=1 : mapa(2,5).obstaculo=1 : mapa(4,5).obstaculo=1 : mapa(5,5).obstaculo=1 mapa(4,6).obstaculo=1 rem Start and End squares. rem Change to make test. mapa(2,2).obstaculo=2 : mapa(5,6).obstaculo=3 for x=1 to 6 for y=1 to 6 if mapa(x,y).obstaculo=2 mapa(x,y).generacion=1 : paso=1 xP=x : yP=y endif if mapa(x,y).obstaculo=3 then xF=x : yF=y next y next x rem Main Loop do cls gosub texto_informativo gosub dibujar_mapa if encontrado=0 and spacekey()=1 then gosub ameba sync loop rem Pathfinding sub ameba: while encontrado=0 rem Search in array for x=1 to 6 for y=1 to 6 if mapa(x,y).generacion=paso xA=x : yA=y rem First Square (like degrees) rem 4 3 2 rem 5 * 1 rem 6 7 8 rem Check if fisrt square is into the array if xA+1<7 rem Check if first square is a wall and not processed if mapa(xA+1,yA).obstaculo<>1 and mapa(xA+1,yA).generacion=0 rem Assign next generation mapa(xA+1,yA).generacion=paso+1 rem Assign a father to this square mapa(xA+1,yA).xPadre=xA : mapa(xA+1,yA).yPadre=yA rem Check if we arrive to the end if xA+1=xF and yA=yF then encontrado=1 endif endif rem 2nd if xA+1<7 and yA-1>0 if mapa(xA+1,yA-1).obstaculo<>1 and mapa(xA+1,yA-1).generacion=0 mapa(xA+1,yA-1).generacion=paso+1 mapa(xA+1,yA-1).xPadre=xA : mapa(xA+1,yA-1).yPadre=yA if xA+1=xF and yA-1=yF then encontrado=1 endif endif rem 3rd if yA-1>0 if mapa(xA,yA-1).obstaculo<>1 and mapa(xA,yA-1).generacion=0 mapa(xA,yA-1).generacion=paso+1 mapa(xA,yA-1).xPadre=xA : mapa(xA,yA-1).yPadre=yA if xA=xF and yA-1=yF then encontrado=1 endif endif rem 4th if xA-1>0 and yA-1>0 if mapa(xA-1,yA-1).obstaculo<>1 and mapa(xA-1,yA-1).generacion=0 mapa(xA-1,yA-1).generacion=paso+1 mapa(xA-1,yA-1).xPadre=xA : mapa(xA-1,yA-1).yPadre=yA if xA-1=xF and yA-1=yF then encontrado=1 endif endif rem 5th if xA-1>0 if mapa(xA-1,yA).obstaculo<>1 and mapa(xA-1,yA).generacion=0 mapa(xA-1,yA).generacion=paso+1 mapa(xA-1,yA).xPadre=xA : mapa(xA-1,yA).yPadre=yA if xA-1=xF and yA=yF then encontrado=1 endif endif rem 6th if xA-1>0 and yA+1<7 if mapa(xA-1,yA+1).obstaculo<>1 and mapa(xA-1,yA+1).generacion=0 mapa(xA-1,yA+1).generacion=paso+1 mapa(xA-1,yA+1).xPadre=xA : mapa(xA-1,yA+1).yPadre=yA if xA-1=xF and yA+1=yF then encontrado=1 endif endif rem 7th if yA+1<7 if mapa(xA,yA+1).obstaculo<>1 and mapa(xA,yA+1).generacion=0 mapa(xA,yA+1).generacion=paso+1 mapa(xA,yA+1).xPadre=xA : mapa(xA,yA+1).yPadre=yA if xA=xF and yA+1=yF then encontrado=1 endif endif rem 8th if xA+1<7 and yA+1<7 if mapa(xA+1,yA+1).obstaculo<>1 and mapa(xA+1,yA+1).generacion=0 mapa(xA+1,yA+1).generacion=paso+1 mapa(xA+1,yA+1).xPadre=xA : mapa(xA+1,yA+1).yPadre=yA if xA+1=xF and yA+1=yF then encontrado=1 endif endif endif next y next x inc paso endwhile xA=xF : yA=yF rem Follow the path in inverse order while xA<>xP or yA<>yP x1=xA : y1=yA xA=mapa(x1,y1).xPadre yA=mapa(x1,y1).yPadre rem To draw a purple box on this square mapa(x1,y1).obstaculo=4 endwhile return rem Draw map dibujar_mapa: ink RGB(255,255,255),0 box 48,48,351,351 ink 0,0 box 49,49,350,350 set text size 20 for x=1 to 6 for y=1 to 6 ink RGB(0,120,0),0 if mapa(x,y).obstaculo=1 then ink RGB(255,0,0),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 center text (x*50)+25,(y*50)+10,str$(mapa(x,y).xPadre)+" : "+str$(mapa(x,y).yPadre) if mapa(x,y).obstaculo=2 ink RGB(0,0,255),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 text (x*50)+18,(y*50)+10,"P" endif if mapa(x,y).obstaculo=3 ink RGB(255,255,0),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 text (x*50)+18,(y*50)+10,"F" endif if mapa(x,y).obstaculo=4 ink RGB(100,0,100),0 box x*50,y*50,x*50+49,y*50+49 ink RGB(255,128,0),0 center text (x*50)+25,(y*50)+10,str$(mapa(x,y).xPadre)+" : "+str$(mapa(x,y).yPadre) endif next y next x return rem Show text texto_informativo: set text size 20 text 5,5,"AMOEBA PATHFINDING" text 400,50,"Press Espace to continue." text 400,70,"Each square shows coords of its father." return |