Greetings Slinks. This forum is a really friendly place. I am happy I wandered in here. I guese I'll start with something short n sweet. A simple adaption of the snake demo. This code demonstrates basic AI in that each NPC has a set patrol that they stick to like glue. It is forbidden for two bots to occupy the same square. Also the bots are forbidden to collect the randomly placed items. Here goes ...
rem NPC Patrolling 2D Game
label init
st = 8
up = 16
rt = 32
dn = 64
lf = 128
m = 30
gridx = 15
gridy = 15
dim grid(gridx, gridy)
bots = 4
dim posx(bots), posy(bots)
dim patl(bots)
dim patx(bots, 3), paty(bots, 3)
dim laps(bots)
dim look(bots, 3), stay(bots, 3)
restore grid1
for y = 0 to gridy
for x = 0 to gridx
read grid(x, y)
next
next
for b = 1 to bots
for p = 0 to 3
read patx(b, p), paty(b, p)
read look(b, p), stay(b, p)
next
posx(b) = patx(b, 0)
posy(b) = paty(b, 0)
patl(b) = 1
grid(posx(b), posy(b)) = 9
next
posx(0) = 07
posy(0) = 07
grid(posx(0), posy(0)) = 9
open window 640, 512
label draw
setrgb 1, 000, 255, 000
for y = 0 to gridy
gy = y*m
for x = 0 to gridx
if (grid(x, y)=1) then
gx = x*m
fill box gx, gy to gx+m, gy+m
fi
next
next
setrgb 1, 255, 000, 000
for b = 1 to bots
px = posx(b)*m
py = posy(b)*m
fill box px, py to px+m, py+m
grid(posx(b), posy(b)) = 9
next
setrgb 1, 000, 000, 255
px = posx(0)*m
py = posy(0)*m
fill box px, py to px+m, py+m
grid(posx(0), posy(0)) = 9
label item
itemx = int(ran(gridx))
itemy = int(ran(gridy))
if (grid(itemx, itemy)<>0) goto item
grid(itemx, itemy) = 2
setrgb 1, 255, 255, 000
fill box itemx*m, itemy*m to itemx*m+m, itemy*m+m
label game
wait 0.01
bot = bot+1
if (bot > bots) bot = 1
desx = patx(bot, patl(bot))
desy = paty(bot, patl(bot))
if (desx > posx(bot)) then dirx = 1
elsif (desx < posx(bot)) then dirx =-1
else
dirx = 0
fi
if (desy > posy(bot)) then diry = 1
elsif (desy < posy(bot)) then diry =-1
else
diry = 0
fi
newx = posx(bot)+dirx
newy = posy(bot)+diry
if (desx = newx) and (desy = newy) then
laps(bot) = laps(bot)+1
if (laps(bot) = stay(bot, patl(bot))) then
laps(bot) = 0
patl(bot) = patl(bot)+1
if (patl(bot)>3) patl(bot) = 0
fi
fi
hit = grid(newx, newy)
if (hit <> 0) then
newx = posx(bot)
newy = posy(bot)
else
clear fill box posx(bot)*m, posy(bot)*m to posx(bot)*m+m, posy(bot)*m+m
setrgb 1, 255, 000, 000
fill box newx*m, newy*m to newx*m+m, newy*m+m
grid(posx(bot), posy(bot)) = 0
grid(newx, newy) = 9
fi
posx(bot) = newx
posy(bot) = newy
dirx = 0
diry = 0
ctrl = peek("port1")
if (and(ctrl, st) <> 0) exit
if (and(ctrl, up) <> 0) diry =-1
if (and(ctrl, dn) <> 0) diry = 1
if (and(ctrl, lf) <> 0) dirx =-1
if (and(ctrl, rt) <> 0) dirx = 1
newx = posx(0)+dirx
newy = posy(0)+diry
hit = grid(newx, newy)
if (hit = 1) or (hit = 9) then
newx = posx(0)
newy = posy(0)
else
clear fill box posx(0)*m, posy(0)*m to posx(0)*m+m, posy(0)*m+m
setrgb 1, 000, 000, 255
fill box newx*m, newy*m to newx*m+m, newy*m+m
grid(posx(0), posy(0)) = 0
grid(newx, newy) = 9
fi
posx(0) = newx
posy(0) = newy
if (hit = 2) goto item
goto game
label grid1
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
rem --------------------------------
rem Patrol Format - desx, desy, look, stay
data 03,03,03,03, 03,12,03,12, 12,12,12,12, 12,03,12,12
data 05,05,03,03, 10,05,03,03, 10,10,03,03, 05,10,03,03
data 01,01,03,03, 01,14,03,03, 14,14,03,03, 14,01,03,03
data 07,01,03,03, 01,07,03,03, 07,14,03,03, 14,07,03,03
Thats it, hope it works.