I have discovered a new problem...
I get proper results with my maps when I use the algorithm I built (from a friend's THINK Pascal code) as a subroutine using GOSUB/RETURN, but not when I placed the same algorithm into a FUNCTION and called it from the main program...
I know there's a region system set up with variables called "scope", and that there are 'local' variables in the FUNCTION call that are either erased or destroyed upon leaving the function, there are 'global' variables that are used all over the application/program that work everywhere in the program (or at least, that's what they're supposed to do). Any #CONSTANT variables cannot be changed and are typically assumed to be 'global' as long as they are initialized at the start of the program.
Here's two programs, technically identical.
Program 1 is the FUNCTION called system.
Program 2 is the subroutine.
These are supposed to make a tiny 200x200 map, with pixels (DOT X,Y) and color them according to what's in their map(x,y) array coordinates. (The SET DISPLAY MODEs are set smallish to show what I'm doing in nearly a pixel-perfect state, in a window of that size.)
PROGRAM 1 - Function Call
mapsize = 200
DIM map(mapsize, mapsize)
SET DISPLAY MODE 640, 400, 32
blk = RGB(0,0,0)
red = RGB(255,0,0)
grn = RGB(0,255,0)
blu = RGB(0,0,255)
ylw = RGB(255,255,0)
cyn = RGB(0,255,255)
mag = RGB(255,0,255)
gry = RGB(128,128,128)
wht = RGB(255,255,255)
lgry = RGB(192,192,192)
dgry = RGB(64,64,64)
orn = RGB(255,128,64)
brn = RGB(128,64,32)
lblu = RGB(128,128,255)
lgrn = RGB(128,255,128)
lred = RGB(255,128,128)
lcyn = RGB(128,255,255)
lylw = RGB(255,255,128)
lmag = RGB(255,128,255)
RANDOMIZE TIMER()
`activate function call to build the town/city
BuildCity()
`display the town/city as a graphic representation
CLS
FOR x = 0 to mapsize - 1
FOR y = 0 to mapsize - 1
`CHECK FOR WALL (#)
IF map(x,y) = 35 THEN INK dgry, blk
`CHECK FOR FLOOR (:)
IF map(x,y) = 58 THEN INK lgry, blk
`CHECK FOR START POSITION (O) (technically UP)
IF map(x,y) = 79 THEN INK red, blk
`CHECK FOR START POSITION (U) (technically DOWN)
IF map(x,y) = 85 THEN INK blu, blk
`Check for 'text letters'...
`these are for the town names
IF (map(x,y) >= 97) AND (map(x,y) <= 122) THEN INK ylw, blk
`Check for 'room labels'...
IF map(x,y) = 49 THEN INK blu, blk
IF map(x,y) = 50 THEN INK lgrn, blk
IF map(x,y) = 51 THEN INK lcyn, blk
IF map(x,y) = 52 THEN INK lmag, blk
IF map(x,y) = 53 THEN INK lblu, blk
IF map(x,y) = 54 THEN INK lred, blk
IF map(x,y) = 55 THEN INK lylw, blk
IF map(x,y) = 56 THEN INK orn, blk
DOT x,y
NEXT y
NEXT x
INK wht, blk
TEXT 0, 205, "Town: Surface"
WAIT KEY
END
`this subroutine builds the town/city
FUNCTION BuildCity()
`fill entire map with floor
FOR x = 0 TO mapsize -1
FOR y = 0 TO mapsize -1
map(x,y) = 58
NEXT y
NEXT x
`make upper and lower map borders
FOR x = 0 TO mapsize - 1
map(x, 0) = 35 : `# normal wall
map(x, mapsize - 1) = 35 : `# normal wall
NEXT x
`make left and right map borders
` yes, the corners overlap...
FOR y = 0 TO mapsize - 1
map(0, y) = 35 : `# normal wall
map(mapsize - 1, y) = 35 : `# normal wall
NEXT y
FOR t = 1 TO 8
x = Roller(mapsize, 1)
y = Roller(mapsize, 1)
`checks for extreme map borders
IF (x + 2) >= 190 THEN x = 190
IF (x - 2) <= 10 THEN x = 10
IF (y + 2) >= 190 THEN y = 190
IF (y - 2) <= 10 THEN y = 10
`check for adjacent buildings
IF map(x - 3, y) = 35 THEN INC x, 1
IF map(x + 3, y) = 35 THEN DEC x, 1
IF map(x, y - 3) = 35 THEN INC y, 1
IF map(x, y + 3) = 35 THEN DEC y, 1
`start building with complete WALL box
FOR i = -2 TO 2
FOR j = -2 TO 2
map(x + i, y + j) = 35
NEXT j
NEXT i
`leave outer walls and create interior space in building
FOR k = -1 TO 1
FOR l = -1 TO 1
map(x + k, y + l) = 58
NEXT l
NEXT k
`make an opening in the bottom center wall
map(x, y + 2) = 58
`label the building accordingly, next to the door
` Room # 1 - Room # 8
map(x + 1, y + 2) = 48 + t
NEXT t
`randomly place player in town/city map
x = Roller(mapsize, 1)
y = Roller(mapsize, 1)
IF map(x,y) = 58 THEN map(x,y) = ASC("*")
`place town/city name into upper-left corner of perimeter wall
RESTORE _CityNameData
FOR x = 1 TO 10
READ ltr$
ltr = ASC(ltr$)
map(x,0) = ltr
NEXT x
ENDFUNCTION
FUNCTION Roller( sides AS INTEGER , addOne AS BOOLEAN )
temp = 0
temp = RND(sides) + 1
IF addOne = 1 THEN INC temp, 1
IF (addOne = 1) AND (temp = 0) THEN temp = 1
IF temp > sides THEN temp = sides
ENDFUNCTION temp
_CityNameData:
DATA "q","u","a","r","t","z","s","i","d","e"
PROGRAM 2 - Subroutine
mapsize = 200
DIM map(mapsize, mapsize)
SET DISPLAY MODE 640, 400, 32
blk = RGB(0,0,0)
red = RGB(255,0,0)
grn = RGB(0,255,0)
blu = RGB(0,0,255)
ylw = RGB(255,255,0)
cyn = RGB(0,255,255)
mag = RGB(255,0,255)
gry = RGB(128,128,128)
wht = RGB(255,255,255)
lgry = RGB(192,192,192)
dgry = RGB(64,64,64)
orn = RGB(255,128,64)
brn = RGB(128,64,32)
lblu = RGB(128,128,255)
lgrn = RGB(128,255,128)
lred = RGB(255,128,128)
lcyn = RGB(128,255,255)
lylw = RGB(255,255,128)
lmag = RGB(255,128,255)
RANDOMIZE TIMER()
`activate function call to build the town/city
GOSUB _BuildCity
`display the town/city as a graphic representation
CLS
FOR x = 0 to mapsize - 1
FOR y = 0 to mapsize - 1
`CHECK FOR WALL (#)
IF map(x,y) = 35 THEN INK dgry, blk
`CHECK FOR FLOOR (:)
IF map(x,y) = 58 THEN INK lgry, blk
`CHECK FOR START POSITION (O) (technically UP)
IF map(x,y) = 79 THEN INK red, blk
`CHECK FOR START POSITION (U) (technically DOWN)
IF map(x,y) = 85 THEN INK blu, blk
`Check for 'text letters'...
`these are for the town names
IF (map(x,y) >= 97) AND (map(x,y) <= 122) THEN INK ylw, blk
`Check for 'room labels'...
IF map(x,y) = 49 THEN INK blu, blk
IF map(x,y) = 50 THEN INK lgrn, blk
IF map(x,y) = 51 THEN INK lcyn, blk
IF map(x,y) = 52 THEN INK lmag, blk
IF map(x,y) = 53 THEN INK lblu, blk
IF map(x,y) = 54 THEN INK lred, blk
IF map(x,y) = 55 THEN INK lylw, blk
IF map(x,y) = 56 THEN INK orn, blk
DOT x,y
NEXT y
NEXT x
INK wht, blk
TEXT 0, 205, "Town: Surface"
WAIT KEY
END
`this subroutine builds the town/city
_BuildCity:
`fill entire map with floor
FOR x = 0 TO mapsize -1
FOR y = 0 TO mapsize -1
map(x,y) = 58
NEXT y
NEXT x
`make upper and lower map borders
FOR x = 0 TO mapsize - 1
map(x, 0) = 35 : `# normal wall
map(x, mapsize - 1) = 35 : `# normal wall
NEXT x
`make left and right map borders
` yes, the corners overlap...
FOR y = 0 TO mapsize - 1
map(0, y) = 35 : `# normal wall
map(mapsize - 1, y) = 35 : `# normal wall
NEXT y
FOR t = 1 TO 8
x = Roller(mapsize, 1)
y = Roller(mapsize, 1)
`checks for extreme map borders
IF (x + 2) >= 190 THEN x = 190
IF (x - 2) <= 10 THEN x = 10
IF (y + 2) >= 190 THEN y = 190
IF (y - 2) <= 10 THEN y = 10
`check for adjacent buildings
IF map(x - 3, y) = 35 THEN INC x, 1
IF map(x + 3, y) = 35 THEN DEC x, 1
IF map(x, y - 3) = 35 THEN INC y, 1
IF map(x, y + 3) = 35 THEN DEC y, 1
`start building with complete WALL box
FOR i = -2 TO 2
FOR j = -2 TO 2
map(x + i, y + j) = 35
NEXT j
NEXT i
`leave outer walls and create interior space in building
FOR k = -1 TO 1
FOR l = -1 TO 1
map(x + k, y + l) = 58
NEXT l
NEXT k
`make an opening in the bottom center wall
map(x, y + 2) = 58
`label the building accordingly, next to the door
` Room # 1 - Room # 8
map(x + 1, y + 2) = 48 + t
NEXT t
`randomly place player in town/city map
x = Roller(mapsize, 1)
y = Roller(mapsize, 1)
IF map(x,y) = 58 THEN map(x,y) = ASC("*")
`place town/city name into upper-left corner of perimeter wall
RESTORE _CityNameData
FOR x = 1 TO 10
READ ltr$
ltr = ASC(ltr$)
map(x,0) = ltr
NEXT x
RETURN
FUNCTION Roller( sides AS INTEGER , addOne AS BOOLEAN )
temp = 0
temp = RND(sides) + 1
IF addOne = 1 THEN INC temp, 1
IF (addOne = 1) AND (temp = 0) THEN temp = 1
IF temp > sides THEN temp = sides
ENDFUNCTION temp
_CityNameData:
DATA "q","u","a","r","t","z","s","i","d","e"
Can you tell me why these two programs do not work when they are technically identical program? Any input or additional ideas are welcome.
This is bothering me as I wanted to use these algorithms as FUNCTIONs instead of subroutines (GOSUB/RETURN). Please help.
All trees have bark. All dogs bark. All dogs are trees.