Here's the map part of the code:
type point2Dint
x as integer
y as integer
endtype
type mapType
ground as integer
item as integer
typ as integer
torch as integer
endtype
#constant tile_empty 0
#constant tile_room 1
#constant tile_door 2
#constant tile_corr 3
#constant tile_wall 4
#constant tile_entrance 5
#constant tile_spawn 6
type roomType
x as integer
y as integer
w as integer
h as integer
endtype
#constant MapSize 64
global TileSize as float
function CreateRandomMap()
repeat
ERROR = 0
DeleteMap()
TileSize = GetDeviceHeight()/(MapSize-1)
RoomTexture as integer[]
num = 4
for i=1 to num
RoomTexture.insert(Random(1, groundCount))
next
CorrTexture as integer[]
num = 2
for i=1 to num
CorrTexture.insert(Random(1, groundCount))
next
// Create rooms
Room as roomType[]
global topX as float
global topY as float
topX = GetDeviceWidth()*0.5 - GetDeviceHeight()*0.5 + TileSize*0.5
topY = TileSize*0.5
done = 0
failCount = 0
maxFailCount = 100
while done=0
w = Random(3, 8)
h = Random(3, 8)
x = Random(4, MapSize-w-4)
y = Random(4, MapSize-h-4)
found = 0
for tx=x-1 to x+w
for ty=y-1 to y+h
if tx<=MapSize-1
if ty<=MapSize-1
if Map[tx, ty].typ<>tile_empty
found=1
exit
endif
endif
endif
next
if found>0
exit
endif
next
f = RoomTexture[Random(0, RoomTexture.length)]
if found=0
IsOctagonal = 0
if w>4
if w=h then IsOctagonal = Random(0,1)
endif
rm as roomType
rm.x = x
rm.y = y
rm.w = w
rm.h = h
Room.insert(rm)
for tx=x to x+w-1
for ty=y to y+h-1
ok = 1
if IsOctagonal=1
cx = tx-x
cy = ty-y
vw = w*0.25
if cx<vw
if cy<vw
if cx+cy<vw
ok = 0
endif
endif
if w-cy-1<vw
if cx+w-cy-1<vw
ok = 0
endif
endif
else
if w-cx-1<vw
if cy<vw
if cy+w-cx-1<vw
ok = 0
endif
endif
if w-cy-1<vw
if w-cx-1+w-cy-1<vw
ok = 0
endif
endif
endif
endif
endif
if ok>0
AddRoom(tx, ty, f)
endif
next
next
endif
inc failCount
if failCount>maxFailCount
done = 1
endif
endwhile
// Create corridoors
for i=0 to Room.length
`message("Room "+str(i))
quad = -1
if Room[i].x+Room[i].w/2<MapSize/2
if Room[i].y+Room[i].h/2<MapSize/2
quad = 1
else
quad = 3
endif
else
if Room[i].y+Room[i].h/2<MapSize/2
quad = 2
else
quad = 4
endif
endif
RunList as integer[]
RunTimes = Random(0,2)
if RunTimes=0 then RunTimes = 1
for rc=1 to RunTimes
side = -1
sideSelect = -1
cnt = 0
startX = -1
startY = -1
while sideSelect=-1
select quad
case 1 : side = Random(0,1)*2 : endcase
case 2 : side = 1 + Random(0,1)*1 : endcase
case 3 : side = Random(0,1)*3 : endcase
case 4 : side = 1 + Random(0,1)*2 : endcase
endselect
while RunList.find(side)>=0
select quad
case 1 : side = Random(0,1)*2 : endcase
case 2 : side = 1 + Random(0,1)*1 : endcase
case 3 : side = Random(0,1)*3 : endcase
case 4 : side = 1 + Random(0,1)*2 : endcase
endselect
endwhile
select side
case 0
// right side
startX = Room[i].x + Room[i].w
x = startX
y = Room[i].y + (Room[i].h/2)-1
sideSelect = 0
for cy=y to y+2
if Map[x,cy].typ<>tile_empty
sideSelect = -1
endif
next
startY = y+1
endcase
case 1
// left side
startX = Room[i].x - 1
x = startX
y = Room[i].y + (Room[i].h/2)-1
sideSelect = 1
for cy=y to y+2
if Map[x,cy].typ<>tile_empty
sideSelect = -1
endif
next
startY = y+1
endcase
case 2
// bottom
startY = Room[i].y + Room[i].h
y = startY
x = Room[i].x + (Room[i].w/2)-1
sideSelect = 0
for cx=x to x+2
if Map[cx,y].typ<>tile_empty
sideSelect = -1
endif
next
startX = x+1
endcase
case 3
// top
startY = Room[i].y - 1
y = startY
x = Room[i].x + (Room[i].w/2)-1
sideSelect = 1
for cx=x to x+2
if Map[cx,y].typ<>tile_empty
sideSelect = -1
endif
next
startX = x+1
endcase
endselect
inc cnt
if cnt>20
sideSelect = -2
endif
endwhile
dir = side
RunList.insert(dir)
// Start new corridoor from current room
cnt = 0
f = CorrTexture[Random(0, CorrTexture.length)]
doneCorridoor = 0
RunCount = 0
while doneCorridoor=0
`message("Side "+str(dir))
`print("Side "+str(dir))
l = Random(4, 10)
select dir
case 0
// right
for j=0 to l
x = startX + j
y = startY
if x<MapSize-2
if Map[x, y].typ=tile_empty
if j=l
if Map[x+1,y-1].typ=tile_empty
if Map[x+1,y+1].typ=tile_empty
AddCorridoor(x, y, f)
else
dec x
exit
endif
else
dec x
exit
endif
else
AddCorridoor(x, y, f)
endif
else
doneCorridoor = 1
exit
endif
else
dec x
exit
endif
if Map[x+1, y].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x, y-1].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x, y+1].typ<>tile_empty
doneCorridoor = 1
exit
endif
next
endcase
case 1
// left
for j=0 to l
x = startX - j
y = startY
if x>1
if Map[x, y].typ=tile_empty
if j=l
if Map[x-1,y-1].typ=tile_empty
if Map[x-1,y+1].typ=tile_empty
AddCorridoor(x, y, f)
else
inc x
exit
endif
else
inc x
exit
endif
else
AddCorridoor(x, y, f)
endif
else
doneCorridoor = 1
exit
endif
else
inc x
exit
endif
if Map[x-1, y].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x, y-1].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x, y+1].typ<>tile_empty
doneCorridoor = 1
exit
endif
next
endcase
case 2
// down
for j=0 to l
x = startX
y = startY + j
if y<MapSize-2
if Map[x, y].typ=tile_empty
if j=l
if Map[x-1,y+1].typ=tile_empty
if Map[x+1,y+1].typ=tile_empty
AddCorridoor(x, y, f)
else
dec y
exit
endif
else
dec y
exit
endif
else
AddCorridoor(x, y, f)
endif
else
doneCorridoor = 1
exit
endif
else
dec y
exit
endif
if Map[x, y+1].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x-1, y].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x+1, y].typ<>tile_empty
doneCorridoor = 1
exit
endif
next
endcase
case 3
// up
for j=0 to l
x = startX
y = startY - j
if y>1
if Map[x, y].typ=tile_empty
if j=l
if Map[x-1,y-1].typ=tile_empty
if Map[x+1,y-1].typ=tile_empty
AddCorridoor(x, y, f)
else
inc y
exit
endif
else
inc y
exit
endif
else
AddCorridoor(x, y, f)
endif
else
doneCorridoor = 1
exit
endif
else
inc y
exit
endif
if Map[x, y-1].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x-1, y].typ<>tile_empty
doneCorridoor = 1
exit
endif
if Map[x+1, y].typ<>tile_empty
doneCorridoor = 1
exit
endif
next
endcase
endselect
startX = x
startY = y
if doneCorridoor=0
if dir<2
select quad
case 1 : dir = 2 : endcase
case 2 : dir = 2 : endcase
case 3 : dir = 3 : endcase
case 4 : dir = 3 : endcase
endselect
else
select quad
case 1 : dir = 0 : endcase
case 2 : dir = 1 : endcase
case 3 : dir = 0 : endcase
case 4 : dir = 1 : endcase
endselect
endif
endif
select dir
case 0 : inc startX : endcase
case 1 : dec startX : endcase
case 2 : inc startY : endcase
case 3 : dec startY : endcase
endselect
select dir
case 0
if startX>MapSize-2 then dir = 1
endcase
case 1
if startX<2 then dir = 0
endcase
case 2
if startY>MapSize-2 then dir = 3
endcase
case 3
if startY<2 then dir = 2
endcase
endselect
inc cnt
if cnt>MapSize*2
doneCorridoor = 1
ERROR = 1
endif
endwhile
next
RunList.length = -1
next
Room.length = -1
// Create Walls
f = 1
for x=1 to MapSize-2
for y=1 to MapSize-2
if Map[x,y].typ=tile_room or Map[x,y].typ=tile_corr
if Map[x-1,y-1].typ=tile_empty
AddWall(x-1, y-1, f)
endif
if Map[x-1,y].typ=tile_empty
AddWall(x-1, y, f)
endif
if Map[x-1,y+1].typ=tile_empty
AddWall(x-1, y+1, f)
endif
if Map[x,y-1].typ=tile_empty
AddWall(x, y-1, f)
endif
if Map[x,y+1].typ=tile_empty
AddWall(x, y+1, f)
endif
if Map[x+1,y-1].typ=tile_empty
AddWall(x+1, y-1, f)
endif
if Map[x+1,y].typ=tile_empty
AddWall(x+1, y, f)
endif
if Map[x+1,y+1].typ=tile_empty
AddWall(x+1, y+1, f)
endif
endif
next
next
// Add torches
size = Sqrt(MapSize)
for zx=0 to 7
for zy=0 to 7
torchNum = Random(0, 1)
for i=1 to torchNum
foundSite = 0
cnt = 0
repeat
x = zx*size + Random(1, size-1)
y = zy*size + Random(1, size-1)
if Map[x,y].typ=tile_room
foundSite = 1
else
if Map[x,y].typ=tile_corr
foundSite = 2
endif
endif
if Map[x,y].torch>0
foundSite = 0
endif
if foundSite>0
L = 0
R = 0
B = 0
T = 0
if Map[x-1, y].typ=tile_wall then L = 1
if Map[x+1, y].typ=tile_wall then R = 1
if Map[x, y+1].typ=tile_wall then B = 1
if Map[x, y-1].typ=tile_wall then T = 1
if L>0
if B>0
// Left and Bottom
pos = Random(0, 2)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 2)
endcase
case 2
AddLightTile(x, y, 3)
endcase
endselect
else
if T>0
// Left and Top
pos = Random(0, 2)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 2)
endcase
case 2
AddLightTile(x, y, 4)
endcase
endselect
else
// Left only
pos = Random(0, 1)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 2)
endcase
endselect
endif
endif
else
if R>0
if B>0
// Right and Bottom
pos = Random(0, 2)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 1)
endcase
case 2
AddLightTile(x, y, 3)
endcase
endselect
else
if T>0
// Right and Top
pos = Random(0, 2)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 1)
endcase
case 2
AddLightTile(x, y, 4)
endcase
endselect
else
// Right only
pos = Random(0, 1)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 1)
endcase
endselect
endif
endif
else
if B>0
// Bottom only
pos = Random(0, 1)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 3)
endcase
endselect
else
if T>0
// Top only
pos = Random(0, 1)
select pos
case 0
AddLightTile(x, y, 5)
endcase
case 1
AddLightTile(x, y, 4)
endcase
endselect
else
// No Sides
AddLightTile(x, y, 5)
endif
endif
endif
endif
endif
inc cnt
if cnt>MapSize*MapSize
foundSite=1
endif
until foundSite>0
next
next
next
// Add player start
playerQuad = Random(1, 4)
siteFound = 0
repeat
select playerQuad
case 1
x = Random(2, MapSize/2 - 2)
y = Random(2, MapSize/2 - 2)
endcase
case 2
x = Random(MapSize/2 + 2, MapSize - 2)
y = Random(2, MapSize/2 - 2)
endcase
case 3
x = Random(2, MapSize/2 - 2)
y = Random(MapSize/2 + 2, MapSize - 2)
endcase
case 4
x = Random(MapSize/2 + 2, MapSize - 2)
y = Random(MapSize/2 + 2, MapSize - 2)
endcase
endselect
if Map[x,y].typ=tile_room
repeat
dec y
until Map[x,y].typ<>tile_room
if Map[x,y].typ<>tile_wall
repeat
dec x
until Map[x,y].typ=tile_wall
if Map[x,y+1].typ<>tile_room
repeat
inc x
until Map[x,y].typ=tile_wall
endif
endif
siteFound = 1
DeleteSprite(Map[x,y].ground)
AddPlayerStart(x, y)
endif
sync()
until siteFound>0
// Add Spawn points
for q=1 to 4
siteFound = 0
repeat
select q
case 1
x = Random(2, MapSize/2 - 2)
y = Random(2, MapSize/2 - 2)
endcase
case 2
x = Random(MapSize/2 + 2, MapSize - 2)
y = Random(2, MapSize/2 - 2)
endcase
case 3
x = Random(2, MapSize/2 - 2)
y = Random(MapSize/2 + 2, MapSize - 2)
endcase
case 4
x = Random(MapSize/2 + 2, MapSize - 2)
y = Random(MapSize/2 + 2, MapSize - 2)
endcase
endselect
if Map[x,y].typ=tile_room
repeat
dec y
until Map[x,y].typ<>tile_room
if Map[x,y].typ<>tile_wall
repeat
dec x
until Map[x,y].typ=tile_wall
if Map[x,y+1].typ<>tile_room
repeat
inc x
until Map[x,y].typ=tile_wall
endif
endif
siteFound = 1
DeleteSprite(Map[x,y].ground)
AddSpawnPoint(x, y)
endif
sync()
until siteFound>0
next
// Add treasure
repeat
treasureQuad = Random(1, 4)
until treasureQuad<>playerQuad
siteFound = 0
repeat
select treasureQuad
case 1
x = Random(2, MapSize/2 - 2)
y = Random(2, MapSize/2 - 2)
endcase
case 2
x = Random(MapSize/2 + 2, MapSize - 2)
y = Random(2, MapSize/2 - 2)
endcase
case 3
x = Random(2, MapSize/2 - 2)
y = Random(MapSize/2 + 2, MapSize - 2)
endcase
case 4
x = Random(MapSize/2 + 2, MapSize - 2)
y = Random(MapSize/2 + 2, MapSize - 2)
endcase
endselect
if Map[x,y].typ=tile_room
siteFound = 1
AddTreasure(x, y)
endif
sync()
until siteFound>0
until ERROR=0
endfunction
function DeleteMap()
for x=0 to MapSize-1
for y=0 to MapSize-1
DeleteSprite(Map[x,y].ground)
DeleteSprite(Map[x,y].item)
DeleteSprite(Map[x,y].torch)
Map[x,y].ground = 0
Map[x,y].item = 0
Map[x,y].torch = 0
Map[x,y].typ = tile_empty
next
next
game.enemyStart.length = -1
endfunction
#constant d_player 6980
#constant d_item 6990
#constant d_ground 7000
function AddSpawnPoint(x, y)
spr = CreateSprite(image.entrance)
SetSpriteSize(spr, TileSize*1.01, TileSize*1.01)
SetSpritePositionByOffset(spr, topX + x*TileSize, topY + y*TileSize)
SetSpriteColor(spr, 255, 0, 0, 255)
SetSpriteDepth(spr, d_ground)
SetSpritePhysicsOn(spr, 1)
Map[x, y].ground = spr
Map[x, y].typ = tile_spawn
p as point2Dint
p.x = x
p.y = y+1
game.enemyStart.insert(p)
endfunction
function AddTreasure(x, y)
spr = CreateSprite(image.treasure)
SetSpriteSize(spr, TileSize*1.01, TileSize*1.01)
SetSpritePositionByOffset(spr, topX + x*TileSize, topY + y*TileSize)
SetSpriteDepth(spr, d_item)
Map[x, y].item = spr
Map[x, y].typ = tile_entrance
endfunction
function AddPlayerStart(x, y)
spr = CreateSprite(image.entrance)
SetSpriteSize(spr, TileSize*1.01, TileSize*1.01)
SetSpritePositionByOffset(spr, topX + x*TileSize, topY + y*TileSize)
SetSpriteColor(spr, 0, 255, 0, 255)
SetSpriteDepth(spr, d_ground)
SetSpritePhysicsOn(spr, 1)
Map[x, y].ground = spr
Map[x, y].typ = tile_entrance
game.playerStart.x = x
game.playerStart.y = y+1
endfunction
function AddWall(x, y, f)
spr = CreateSprite(image.ground)
SetSpriteSize(spr, TileSize*1.01, TileSize*1.01)
SetSpriteAnimation(spr, 16, 16, groundCount)
SetSpriteFrame(spr, f)
SetSpritePositionByOffset(spr, topX + x*TileSize, topY + y*TileSize)
SetSpriteAngle(spr, Random(0,3)*90)
SetSpriteFlip(spr, Random(0,1), 0)
SetSpriteColor(spr, 64, 64, 64, 255)
SetSpriteDepth(spr, d_ground)
SetSpritePhysicsOn(spr, 1)
Map[x, y].ground = spr
Map[x, y].typ = tile_wall
endfunction
function AddRoom(x, y, f)
spr = CreateSprite(image.ground)
SetSpriteSize(spr, TileSize*1.01, TileSize*1.01)
SetSpriteAnimation(spr, 16, 16, groundCount)
SetSpriteFrame(spr, f)
SetSpritePositionByOffset(spr, topX + x*TileSize, topY + y*TileSize)
SetSpriteAngle(spr, Random(0,3)*90)
SetSpriteFlip(spr, Random(0,1), 0)
SetSpriteDepth(spr, d_ground)
Map[x, y].ground = spr
Map[x, y].typ = tile_room
endfunction
function AddCorridoor(x, y, f)
spr = CreateSprite(image.ground)
SetSpriteSize(spr, TileSize*1.01, TileSize*1.01)
SetSpriteAnimation(spr, 16, 16, groundCount)
SetSpriteFrame(spr, f)
SetSpritePositionByOffset(spr, topX + x*TileSize, topY + y*TileSize)
SetSpriteAngle(spr, Random(0,3)*90)
SetSpriteFlip(spr, Random(0,1), 0)
SetSpriteDepth(spr, d_ground)
Map[x, y].ground = spr
Map[x, y].typ = tile_corr
endfunction
function AddLightTile(x, y, f)
spr = CreateSprite(image.torch)
SetSpriteSize(spr, TileSize*1.01, TileSize*1.01)
SetSpriteAnimation(spr, 16, 16, groundCount)
SetSpriteFrame(spr, f)
SetSpritePositionByOffset(spr, topX + x*TileSize, topY + y*TileSize)
SetSpriteDepth(spr, d_item)
Map[x, y].torch = spr
endfunction
Using AppGameKit V2 Tier 1