Ok, I put all the functions in the main program and I get the exact same results. I was having problems with the variable like you said, so I started passing them in as parameters.
Here's what I have:
` #######################################################################
` DarkBasic Dungeon by Hubdule
` Shows huge dungeons
` (c) by Color Arts 2002
` #######################################################################
`Set Display Mode 800, 600, 16
Sync On
Sync Rate 0
set global object creation 1
` Attention ... This value is the size of the map ! change it to fit your map file
mapsize = 21
Dim map$(mapsize, mapsize)
maxcubes = 16
cubesize = 200
backcolor = RGB(0, 0, 0)
Fog On
Fog Color backcolor
Fog Distance (int(maxcubes) * cubesize)
Set Camera range 1, (maxcubes * cubesize)
` Current map
Loadmap("maps\dungeon2.txt", mapsize)
Autocam Off
Load image "media\textures\wall2.jpg", 1
Load image "media\textures\ground.jpg", 2
Load image "media\textures\wall.jpg", 3
Load image "media\Spectre\Spook.dds", 4
` Create enemy Path.
AI start
AI Make Path 1
AI Path Add Point 1, (2 * cubesize), (2 * cubesize)
AI Path Add Point 1, (18 * cubesize), (2 * cubesize)
AI Path Add Point 1, (18 * cubesize), (18 * cubesize)
AI Path Add Point 1, (2 * cubesize), (18 * cubesize)
` Enemy
Load Object "media\Spectre\L-Spectre-Move.x", 1
Texture Object 1, 4
Loop Object 1
Set Object Speed 1, 5
Position Object 1, (2 * cubesize), -100, (2 * cubesize)
Scale Object 1, (100 * cubesize / 2), (100 * cubesize / 2), (100 * cubesize / 2)
yrotate object 1, 180
fix object pivot 1
AI Add Enemy 1, 1
AI Set Entity Speed 1, 500.0
AI Entity Assign Patrol Path 1, 1
` Wall … you can change the walls to other objects J
for i=1 to maxcubes * maxcubes
make object cube 100 + i, cubesize
texture object 100 + i, 1
next i
` floor
for i=1 to maxcubes * maxcubes
make object plain 10000 + i, cubesize, cubesize
rotate object 10000 + i, 90, 0, 0
texture object 10000 + i, 2
next i
` ceiling
for i = 1 to maxcubes * maxcubes
make object plain 20000 + i, cubesize, cubesize
rotate object 20000 + i, 270, 0, 0
texture object 20000 + i, 3
next i
set ambient light 30
make light 1
make light 2
set point light 1, 0, 0, 0
set spot light 2, 45, 90
color light 2, RGB(252, 216, 141)
color light 1, RGB(236, 182, 100)
color light 0, RGB(0, 0, 0)
` Search startpoint
for z=1 to mapsize
for x = 1 to mapsize
if map$(x, z) = "O"
cx = x * cubesize
cz = z * cubesize
endif
next x
next zd
position camera cx, 0, cz
Randomize Timer()
cls
do
moveplayer(maxcubes, cubesize)
collisiondetection(maxcubes, cubesize)
Position light 1, camera position x(), camera position y(), camera position z()
Position light 2, camera position x(), camera position y(), camera position z()
Color light 1, RGB(200 + int(rnd(50) - 25), 120, 60)
Rotate light 2, camera angle x(), camera angle y(), camera angle z()
AI Update
Sync
Loop
function loadmap(filename$, size)
open to read 1,filename$
for y=1 to size
read string 1, tString$
for x=1 to size
map$(x,y) = mid$(tString$, x)
next x
next y
close file 1
endfunction
function moveplayer(maxcubes, cubesize)
Text 0, 40, "twist: " + Str$(joystick twist z())
Text 0, 50, "maxcubes: " + Str$(maxcubes)
twist# = joystick twist z() - 32767
if (twist# - 10000) > 0 then xrotate camera (twist# - 10000) / 32767 * 30
if (twist# + 10000) < 0 then xrotate camera (twist# + 10000) / 32767 * 30
`Straf the camera left and right.
if (joystick x() + 100) < 0
y# = camera angle y()
cx# = camera angle x()
cz# = camera angle z()
y# = wrapvalue(y# - 90)
rotate camera 0, y#, 0
move camera -(maxcubes * .0001 * (joystick x() - 100))
y# = wrapvalue(y# + 90)
rotate camera cx#, y#, cz#
endif
if (joystick x() - 100) > 0
y# = camera angle y()
cx# = camera angle x()
cz# = camera angle z()
y# = wrapvalue(y# + 90)
rotate camera 0, y#, 0
move camera (maxcubes * .0001 * (joystick x() - 100))
y# = wrapvalue(y# - 90)
rotate camera cx#, y#, cz#
endif
Rem - Make left joystick move forward and backward with up and down movement.
if (joystick y() + 200) < 0
cay# = camera angle y()
cx# = camera position x()
cy# = camera position y()
cz# = camera position z()
nx# = newxvalue(cx#, cay#, -(joystick y() - 200) * .001)
nz# = newzvalue(cz#, cay#, -(joystick y() - 200) * .001)
position camera nx#, cy#, nz#
endif
if (joystick y() - 200) > 0
cay# = camera angle y()
cx# = camera position x()
cy# = camera position y()
cz# = camera position z()
nx# = newxvalue(cx#, cay#, -(joystick y() - 200) * .001)
nz# = newzvalue(cz#, cay#, -(joystick y() - 200) * .001)
position camera nx#, cy#, nz#
endif
Rem - Make right joystick rotate screen left and right.
if (joystick z() + 400) < 0
y# = camera angle y()
y# = wrapvalue(y# + ((joystick z() + 400) * .001))
yrotate camera y#
endif
if (joystick z() - 400) > 0
y# = camera angle y()
y# = wrapvalue(y# + ((joystick z() - 400) * .001))
yrotate camera y#
endif
endfunction
function collisiondetection(maxcubes, cubesize)
Text 0, 10, "oldposX: : " + Str$(camera position x())
Text 0, 20, "oldposY: : " + Str$(camera position y())
Text 0, 30, "oldposZ: : " + Str$(camera position z())
collisionstep = Int(cubesize / 10)
oldpositionx# = camera position x()
oldpositiony# = camera position y()
oldpositionz# = camera position z()
cx# = int(camera position x()/cubesize)-int(maxcubes/2)
cz# = int(camera position z()/cubesize)-int(maxcubes/2)
tx# = camera position x()
tz# = camera position z()
` Collision Detection.
xbad = 0
zbad = 0
zzz=0
for zz = 1 to maxcubes
for xx = 1 to maxcubes
zzz = zzz + 1
curposx = int(cx#) + xx
curposz = int(cz#) + zz
if curposx < 1 then curposx = 1
if curposx > mapsize then curposx = mapsize
if curposz < 1 then curposz = 1
if curposz > mapsize then curposz = mapsize
` wall
if map$(int(curposx), int(curposz)) = "#"
show object 100 + zzz
position object 100 + zzz, curposx * cubesize, 0.0, curposz * cubesize
` Collision detection
if map$(int(curposx), int(curposz)) = "#"
if tx# >= ((curposx * cubesize) - (cubesize/2)) - collisionstep and tx# <= ((curposx*cubesize) + (cubesize/2)) + collisionstep and tz# >= ((curposz * cubesize) - (cubesize/2)) - collisionstep and tz# <= ((curposz * cubesize) + (cubesize/2)) + collisionstep
` 1
if tx# >= ((curposx * cubesize) - (cubesize/2)) - collisionstep and tx# <= ((curposx*cubesize) + (cubesize/2)) + collisionstep and oldpositionz# >= ((curposz * cubesize) - (cubesize/2)) - collisionstep and oldpositionz# <= ((curposz * cubesize) + (cubesize/2)) + collisionstep
xbad = 1
endif
` 2
if oldpositionx# >= ((curposx * cubesize) - (cubesize/2)) - collisionstep and oldpositionx# <= ((curposx*cubesize) + (cubesize/2)) + collisionstep and tz# >= ((curposz * cubesize) - (cubesize/2)) - collisionstep and tz# <= ((curposz * cubesize) + (cubesize/2)) + collisionstep
zbad = 1
endif
endif
endif
else
hide object 100 + zzz
endif
` floor
if map$(int(curposx), int(curposz)) = ":" or map$(int(curposx), int(curposz)) = "A" or map$(int(curposx), int(curposz)) = "O"
show object 10000 + zzz
position object 10000 + zzz, curposx * cubesize, (cubesize/2) * (-1), curposz * cubesize
else
hide object 10000 + zzz
endif
` ceiling
if map$(int(curposx), int(curposz)) = ":" or map$(int(curposx), int(curposz)) = "A" or map$(int(curposx), int(curposz)) = "O"
show object 20000 + zzz
position object 20000 + zzz, curposx * cubesize, (cubesize/2), curposz * cubesize
else
hide object 20000 + zzz
endif
Next xx#
Next yy#
` Reset the collision before continuing.
if xbad = 0 and zbad = 1 then position camera tx#, oldpositiony#, oldpositionz#
if xbad = 1 and zbad = 0 then position camera oldpositionx#, oldpositiony#, tz#
if xbad = 1 and zbad = 1 then position camera oldpositionx#, oldpositiony#, oldpositionz#
endfunction
I'm building a dungeon off of the tutorial, so a lot of the code might look familiar.