| Ad_Infinitum |
| Administrator |

 |
 |
| Data inscrierii: 23/Iun/2007 |
| Mesaje: 212 |
|
|
|
 |
 |
 |
|
REMstart
==============================================================
= Title : Maze Challenge
= Author : latch
= Date : 2/19/2007
= Update : 02/20/2007
= Version:
==============================================================
Comments: Create AI TO get through maze AND move cones TO other side
==============================================================
REMend
REM =============================================================
REM = SET UP DISPLAY
REM =============================================================
AUTOCAM OFF
SET DISPLAY MODE 800,600,16
SYNC ON
SYNC RATE 40
GOSUB _init
REM MY ADDITION TO PROGRAM ********
GOSUB _init2
REM *******************************
REM =============================================================
REM = MAIN
REM =============================================================
_main:
DO
REM MY ADDITION TO PROGRAM ********
GOSUB Point_Karol
REM *******************************
REM can't change these ************
GOSUB _move_robot
`gosub _move_friends
GOSUB _timer
TEXT 0,0,STR$(SCREEN FPS() )
REM *******************************
SYNC
LOOP
END
REM =============================================================
REM = SUBROUTINES - PROCEDURES
REM =============================================================
_init:
GOSUB _plot_maze
GOSUB _karol_the_robot
REM camera vars
camx#=matx/2
camy#=300
camz#=-100
POSITION CAMERA camx#,camy#,camz#
POINT camera camx#,0,matz/2
REM timer variables
timestart=0
REPEAT
CENTER TEXT SCREEN WIDTH()/2,0,"Press CONTROL key to begin"
REM wait here for control key
SYNC
UNTIL CONTROLKEY()=1
RETURN
`----------------------------------------------------------------
_plot_maze:
REM maze variables
mazex=11
mazez=10
DIM grid(mazex,mazez)
REM read coordinates into an array
FOR z=1 TO mazez
FOR x=1 TO mazex
READ grid(x,z)
NEXT x
NEXT z
RESTORE
matx=mazex*25
matz=mazez*25
across=0
up=0
obj=0
size=mazez*mazex
REM make matrix as a floor
MAKE MATRIX 1,matx,matz,mazex,mazez
REM draw walls x direction
x=0
FOR z= 1 TO mazez
REPEAT
INC x
`read block
IF grid(x,z)=1
obflag=1
INC wallx,25
ELSE
IF obflag=1
INC obj
MAKE OBJECT BOX obj,wallx,25,25
POSITION OBJECT obj,(x*25)-(wallx/2)-25,12.5,((z-1)*25)+12.5
wallx=0
obflag=0
ENDIF
ENDIF
UNTIL x = mazex
IF obflag=1
INC obj
MAKE OBJECT BOX obj,wallx,25,25
POSITION OBJECT obj,(x*25)-(wallx/2),12.5,((z-1)*25)+12.5
wallx=0
obflag=0
x=0
ENDIF
NEXT z
REM draw walls z direction
z=0
FOR x= 1 TO 11
REPEAT
INC z
`read block
IF grid(x,z)=1
obflag=1
INC wallz,25
ELSE
IF obflag=1
INC obj
MAKE OBJECT BOX obj,25,25,wallz
POSITION OBJECT obj,((x-1)*25)+12.5,12.5,(((z-1)*25)-(wallz/2))
wallz=0
obflag=0
ENDIF
ENDIF
UNTIL z =mazez
IF obflag=1
INC obj
MAKE OBJECT BOX obj,25,25,wallz
POSITION OBJECT obj,((x-1)*25)+12.5,12.5,(z*25)-(wallz/2)
wallz=0
obflag=0
z=0
ELSE
wallz=0
obflag=0
z=0
ENDIF
NEXT x
REM cleanup individual box items and set collision for remaining objects (walls)
FOR n=1 TO obj
set OBJECT COLLISION TO boxes n
IF OBJECT SIZE Z(n)=25 AND OBJECT SIZE X(n)=25
DELETE OBJECT n
ENDIF
NEXT n
RETURN
`----------------------------------------------------------------
_karol_the_robot:
robot=avail_obj(1)
DIM friend(3)
REM create robot
MAKE OBJECT CONE robot,10
set OBJECT COLLISION TO boxes robot
POSITION OBJECT robot,0,5,-20
COLOR OBJECT robot,RGB(0,255,0)
XROTATE OBJECT robot,90
FIX OBJECT PIVOT robot
TURN OBJECT RIGHT robot,90
REM create robot friends
FOR n=1 TO 3
friend(n)=avail_obj(1)
MAKE OBJECT CONE friend(n),16
POSITION OBJECT friend(n),n*70,8,130+matz
COLOR OBJECT friend(n),RGB(n*50,n*50,n*50)
set OBJECT COLLISION TO boxes friend(n)
NEXT n
REM create target
MAKE MATRIX 2,75,25,3,1
POSITION MATRIX 2,210,0,-50
RETURN
`----------------------------------------------------------------
_collision_with_walls:
REM check for wall collision
bang=OBJECT COLLISION(robot,0)
IF bang = friend(1) OR bang = friend(2) OR bang = friend(3) OR bang=0
REM do nothing
ELSE
TURN OBJECT LEFT robot,180
ENDIF
RETURN
`----------------------------------------------------------------
_move_robot:
MOVE OBJECT robot,2
GOSUB _collision_with_walls
RETURN
`----------------------------------------------------------------
_timer:
IF timestart=0
thetimer=TIMER()
timestart=1
ENDIF
IF timestart=1
current_time=TIMER()-thetimer
TEXT 0,20,STR$(current_time)
ENDIF
REM find out if friends have been moved to the end matrix
f1_x#=OBJECT POSITION X(friend(1))
f1_z#=OBJECT POSITION Z(friend(1))
f2_x#=OBJECT POSITION X(friend(2))
f2_z#=OBJECT POSITION Z(friend(2))
f3_x#=OBJECT POSITION X(friend(3))
f3_z#=OBJECT POSITION Z(friend(3))
IF f1_x# > 209 AND f1_x# < 210+25 AND f1_z# >-51 AND f1_z# < -50+25
goal_1=1
ELSE
goal_1=0
ENDIF
IF f2_x# > 209+25 AND f2_x# < 210+50 AND f2_z# >-51 AND f2_z# < -50+25
goal_2=1
ELSE
goal_2=0
ENDIF
IF f3_x# > 209+50 AND f3_x# < 210+75 AND f3_z# >-51 AND f3_z# < -50+25
goal_3=1
ELSE
goal_3=0
ENDIF
finalgoal=goal_1+goal_2+goal_3
IF finalgoal=3
SET TEXT SIZE 100
SET TEXT OPAQUE
REPEAT
CENTER TEXT SCREEN WIDTH()/2,SCREEN HEIGHT()/2,"DONE! Time: "+STR$(current_time)
SYNC
UNTIL SCANCODE()>0
END
ENDIF
RETURN
`----------------------------------------------------------------
_move_friends:
IF current_time >= 6*1000
FOR n = 1 TO 3
POSITION OBJECT friend(n),210+((n*25)-12.5),OBJECT POSITION Y(friend(n)),-37.5
NEXT n
ENDIF
RETURN
_init2:
` Dimension Pathfind Arrays
DIM OpenList(198,3)
DIM ClosedList(198,1)
DIM Waypoint#(100,1)
DIM Hval(198)
DIM Gval(198)
DIM Fval(198)
` Dimension Grid Array
DIM Grid2(11,18)
` Set Grid To All Walkable
FOR x=1 TO 11
FOR y=1 TO 18
Grid2(x,y)=1
NEXT y
NEXT x
` Set Robot Start Position
DestX#=12.5
DestZ#=-12.5
CurrX#=1
CurrZ#=2
` Set High Hval for sort
Fval(0)=10000
` Create Collision Cube
Impact_Obj=100
MAKE OBJECT CUBE Impact_Obj,20
set OBJECT COLLISION TO boxes Impact_Obj
HIDE OBJECT Impact_Obj
REM Find Where Friends Are Located
x1=OBJECT POSITION X(friend(1))
z1=OBJECT POSITION Z(friend(1))
x2=OBJECT POSITION X(friend(2))
z2=OBJECT POSITION Z(friend(2))
x3=OBJECT POSITION X(friend(3))
z3=OBJECT POSITION Z(friend(3))
` Set destination to friend 1
DestX=x1/25+1
DestZ=(z1+50)/25
` Set State Counter
State=1
` Get Start Path
GOSUB Find_New_Path
RETURN
Point_Karol:
POINT object robot,Waypoint#(CurrWay,0),OBJECT POSITION Y(robot),Waypoint#(CurrWay,1)
SET CURSOR 0,60 : PRINT CurrWay
SET CURSOR 0,75 : PRINT Waypoint#(CurrWay,0)," ",Waypoint#(CurrWay,1)
FOR x=CurrWay TO WaypointMax
IF x=1 THEN INK RGB(0,0,0),0 ELSE INK RGB(255,255,255),0
SET CURSOR 700,x*15-15
PRINT ClosedList(x,0)," ",ClosedList(x,1)
NEXT x
SET CURSOR 500,0
FOR x=2 TO 11
FOR z=1 TO 18
PRINT Grid2(x,z)
NEXT z
NEXT x
IF SQRT((Waypoint#(CurrWay,0)-OBJECT POSITION X(robot))^2+(Waypoint#(CurrWay,1)-OBJECT POSITION Z(robot))^2)<=1
GOSUB Sensory_Input
GOSUB Inc_Waypoint
ENDIF
RETURN
Inc_Waypoint:
Waypoints=Waypoints-1
IF Waypoints=1
GOSUB Inc_State
GOSUB Find_New_Path
ELSE
FOR x=1 TO Counter2-1
IF Grid2(ClosedList(x,0),ClosedList(x,1))=0 THEN ReCalc=1
NEXT x
CurrWay=WaypointMax-Waypoints
ENDIF
RETURN
Sensory_Input:
` Have Robot Check for Walls Around it
FOR x=-1 TO 1
FOR y=-1 TO 1
POSITION OBJECT Impact_Obj,Waypoint#(CurrWay,0)+(x*25),OBJECT POSITION Y(Impact_Obj),Waypoint#(CurrWay,1)+(y*25)
TileX=CurrX+x
TileY=CurrZ+y
IF TileX>0 AND TileY>0 AND TileX<12 AND TileY<19
bang=OBJECT COLLISION(Impact_Obj,0)
IF bang<>friend(1) AND bang<>robot AND bang<>friend(2) AND bang<>friend(3) AND bang<>0
Grid2(TileX,TileY)=0
ENDIF
ENDIF
NEXT x
NEXT x
RETURN
Inc_State:
INC State
IF State=2
glue object TO limb friend(1),robot,0
DestX=9
DestZ=1
ENDIF
IF State=3
UNGLUE OBJECT friend(1)
DestX=x2/25
DestZ=(z2+50)/25
ENDIF
IF State=4
glue object TO limb friend(2),robot,0
DestX=10
DestZ=1
ENDIF
IF State=5
UNGLUE OBJECT friend(2)
DestX=x3/25
DestZ=(z3+50)/25
ENDIF
IF State=6
glue object TO limb friend(3),robot,0
DestX=11
DestZ=1
ENDIF
IF State=7
UNGLUE OBJECT friend(3)
ENDIF
RETURN
Find_New_Path:
` Initialize values
StartX=((OBJECT POSITION X(Robot)+12.5)/25)
StartY=((OBJECT POSITION Z(Robot)+62.5)/25)
Counter1=1
Counter2=1
CurrX=StartX
CurrZ=StartZ
OpenList(1,0)=CurrX
OpenList(1,1)=CurrZ
Finish=0
Fval(1)=1
REPEAT
` Find Lowest F-Cost Square
FOR x=1 TO Counter1-1
Skip=0
FOR y=1 TO Counter2-1
IF OpenList(x,0)=ClosedList(y,0) AND OpenList(x,1)=ClosedList(y,1) THEN Skip=1
NEXT y
IF Skip=0
IF Fval(x)<Fval(BestTile)
BestTile=x
ENDIF
ENDIF
NEXT x
` Add it to the Closed List
ClosedList(Counter2,0)=OpenList(BestTile,0)
ClosedList(Counter2,1)=OpenList(BestTile,1)
INC Counter2
` Set Current Tile
CurrX=ClosedList(Counter2-1,0)
CurrZ=ClosedList(Counter2-1,1)
` Set Current Tile as Parent
ParentX=CurrX
ParentZ=CurrZ
` Add Adjacent Squares to Open List
FOR x=-1 TO 1
FOR y=-1 TO 1
Run=1
Skip=0
TileX=CurrX+x
TileY=CurrZ+y
IF TileX>0 AND TileX<12 AND TileY>0 AND TileY<19
IF Grid2(TileX,TileY)=1
IF TileX<>CurrX OR TileY<>CurrZ
FOR z=0 TO Counter2-1
IF ClosedList(z,0)=TileX AND ClosedList(z,1)=TileY THEN Run=0
NEXT z
IF Run=1
FOR z=1 TO Counter1
IF OpenList(z,0)=TileX AND OpenList(z,1)=TileY
Skip=1
Zval=z
ENDIF
NEXT z
IF Skip=0
OpenList(Counter1,0)=TileX : OpenList(Counter1,1)=TileY
OpenList(Counter1,2)=ParentX : OpenList(Counter1,3)=ParentY
ValX=ABS(TileX-DestX)
ValZ=ABS(TileY-DestZ)
Hval(Counter1)=(ValX+ValZ)*10
IF y=0 OR x=0 THEN Gval(Counter1)=Gval(BestTile)+G2 ELSE Gval(Counter1)=Gval(BestTile)+G1
Fval(Counter1)=Hval(Counter1)+Gval(Counter1)
INC Counter1
ELSE
IF y=0 OR x=0 THEN Gval=Gval(BestTile)+G2 ELSE Gval=Gval(BestTile)+G1
IF Gval<Gval(Zval)
ValX=ABS(TileX-DestX)
ValZ=ABS(TileY-DestZ)
Hval=(ValX+ValZ)*10
OpenList(Zval,2)=ParentX : OpenList(Zval,3)=ParentZ
Gval(Zval)=Gval : Hval(Zval)=Hval
Fval(Zval)=Gval+Hval
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
NEXT y
NEXT x
REM Reset BestTile
BestTile=0
` Check for Dest on OpenList
FOR x=0 TO Counter1-1
IF OpenList(x,0)=DestX AND OpenList(x,1)=DestZ
Finish=1
Value=x
ENDIF
NEXT x
UNTIL Finish=1
Waypoint#(Counter2-1,0)=OpenList(Value,0)*25-12.5
Waypoint#(Counter2-1,1)=OpenList(Value,1)*25-62.5
FOR x=1 TO Counter2-2
Waypoint#(x,0)=ClosedList(x,0)*25-12.5
Waypoint#(x,1)=ClosedList(x,1)*25-62.5
NEXT x
Waypoints=Counter2-1
WaypointMax=Waypoints
CurrWay=2
RETURN
REM =============================================================
REM = FUNCTIONS
REM =============================================================
FUNCTION avail_obj(num)
WHILE OBJECT EXIST(num)=1
INC num
ENDWHILE
ENDFUNCTION num
REM =============================================================
REM = DATA STATEMENTS
REM =============================================================
DATA 1,1,0,1,1,1,1,1,1,1,1
DATA 1,0,0,0,0,0,1,0,0,0,1
DATA 1,0,1,1,1,0,1,1,0,0,1
DATA 1,0,0,0,1,0,0,0,0,0,1
DATA 1,1,1,0,1,0,1,0,1,1,1
DATA 1,0,1,0,0,0,1,0,0,0,1
DATA 1,0,1,1,1,0,1,0,1,0,1
DATA 1,0,0,0,0,0,1,0,1,0,1
DATA 1,0,0,1,0,0,0,0,1,0,1
DATA 1,1,0,1,1,1,1,1,1,0,1
-----------------------------------ScreenShot---------------------------------
 |
|