steve c,
I have written for you a couple of programs, the second which will demonstrate waypoint guard following. The two snippets below are the two seperate programs. Copy and paste them as seperate .DBA files, but in the same folder.
The first snippet allows you to use your mouse to create waypoints on the coordinates of x and y, within the dimensions of the screen. The second will have a guard walk from waypoint to waypoint, either sequentially(in order from start to finnish), or randomly. In the second snippet, right under the REM statement that says "<<<<<<<<main loop>>>>>>>>>>", is where you want to focus your study on, in which you will be able to learn simple waypoint movement of characters.
Using the first program, the waypoint generator, draw a circle of waypoints. Then, in the second program, the waypoint follower, set the follow type to 1, sequential, and watch the guard walk in a circle. Or, place just two waypoints and set the movement to sequential, and watch the guard patrol a "hallway". Enjoy, and I hope you learn a great deal from this.
set display mode 800,600,32
sync on
sync rate 60
REM << if file already exist then delete it
if file exist("waypoints.txt") then delete file "waypoints.txt"
REM << open new file to write to
open to write 1,"waypoints.txt"
REM << display instructions on screen
ink rgb(255,255,255),0
print "press left mouse button to record a waypoint at mouse coordinates"
print "press any key to end program"
ink rgb(150,150,150),0
REM <<<<<<<<< main loop >>>>>>>>>>
repeat
REM << record waypoint to file when mouse is clicked
if mouseclick() = 1 and norepeat = 0
norepeat = 1
write string 1,str$(mousex())
write string 1,str$(mousey())
REM << place a circle at mouse coordinate
circle mousex(),mousey(),5
endif
REM << enable waypoint recording if mouse button is let off of
if mouseclick() = 0 then norepeat = 0
sync
until scancode() > 0
close file 1
cls
end
set display mode 800,600,32
sync on
sync rate 60
REM << read file and store coordinates into array
if file exist("waypoints.txt")
gonext = 1
open to read 1,"waypoints.txt"
REM << figure size of array needed by checking amount of strings within text file
repeat
read string 1,string1$
if string1$ <> "" then inc count
until string1$ = ""
REM << make array at needed capacity
dim coords(count,2)
REM << fill array with coordinates
close file 1
open to read 1,"waypoints.txt"
repeat
inc a
read string 1,string$
coords(a,1) = val(string$)
read string 1,string$
coords(a,2) = val(string$)
until a = count
close file 1
REM << error capture
else
gonext = 0
endif
REM << print error message if needed file does not exist
if gonext = 0
repeat
center text 400,300,"File does not exist!"
center text 400,320,"Press any key to end program"
sync
cls
until scancode() > 0
end
REM << run program normally
else
REM << prompt user for type of movement
repeat
if try = 0 then input "Type 1 for sequential movement. Type 2 for random movement.",type$
if try = 1 then input "Try again:Type 1 for sequential movement. Type 2 for random movement.",type$
if val(type$) < 1 or val(type$) > 2
try = 1
else
try = 0
endif
cls
until try = 0
wait 2000
REM << randomly or sequentially, depending on user choice, initialize guard start coordinates
if type$ = "1"
inc nextpoint
endif
if type$ = "2" then nextpoint = rnd(count / 2)
if nextpoint < 0 then nextpoint = 1
x# = coords(nextpoint,1)
y# = coords(nextpoint,2)
getnextpoint = 1
REM <<<<<<<< main loop >>>>>>>>>
repeat
REM << if guard has reached destination then grab set of coords from array as next waypoint
if getnextpoint = 1
if type$ = "1"
inc nextpoint
if nextpoint > count / 2 then nextpoint = 1
endif
if type$ = "2" then nextpoint = rnd(count / 2)
if nextpoint = 0 then nextpoint = 1
xdest = coords(nextpoint,1)
ydest = coords(nextpoint,2)
REM << get angle between current point and destination point
angle# = atanfull(xdest - x#,ydest - y#)
if angle# < 0 then inc angle#,360
getnextpoint = 0
else
REM << get distance from guard to destination;if within 1.50 units then get next destination
if sqrt((xdest - x#)^2 + (ydest - y#)^2) < 1.50 then getnextpoint = 1
REM << move guard towards destination
x# = x# + (sin(angle#) * 2)
y# = y# + (cos(angle#) * 2)
endif
REM << draw waypoints as small circles to the screen
for t = 1 to count / 2
REM << highlight destination point circle red
if t = nextpoint
ink rgb(255,0,0),0
else
ink rgb(100,100,100),0
endif
circle coords(t,1),coords(t,2),5
next t
REM << draw guard as circle and directional line to screen
ink rgb(0,0,150),0
circle x#,y#,10
line x# + (sin(angle#) * 10),y# + (cos(angle#) * 10),x# + (sin(angle#) * 20),y# + (cos(angle#) * 20)
REM << print instructions to screen
print "Press any key to end program"
sync
cls
until scancode() > 0
end
endif

+NanoBrain+