Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / This Code Is Driving Me Nuts - Please Help

Author
Message
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 21st Oct 2003 11:58
Ok, this code is absolutely driving me crazy. I've been trying to figure out why is not working for about the last 3 hours but haven't gotten anywhere? Can any of you kind generous people help?

Its supposed to generate a checkered map of symbols, each checker being 10 X 10. In the first row it generates everything looks great. Each checker is 10 X 10. But in the second and third rows of checkers things get a little ugly. Checkers arent 10 X 10 and some overlap each other.

Two strange things I've noticed is that if MAPX is perfectly divisible by L then everything works and looks great. The other strange thing is that if you look at the right and left ends of the second and third rows they both have the same symbols. If you add the colums at the end of the rows to the amount thats at the beginning of the row (with identical symbols) it equals 10. If this doesnt make any sense, run the code you'll see.

Any help would be really really really really really really really appreciated, thanks.

Ok, now let me step you through the code. MAPX and MAPY are constants defining the map size. Map element is a type for each individual symbol of the map, which holds two values. One value is the symbol it is using for mountainous, grassy, sandy terrain, etc. The other variable holds information to mark whether or not that map element has been used.

Then I make the map in an array form. The first FOR NEXT loop sets each element to its starting value, where RangeType is set to uDef for UNDEFINED.

Then the next FOR NEXT loop generates the map. First a symbol for the terrain type is picked at random. Then it goes through to check if each element is undefined. If one is undefined then every map element within a 10X10 area of it will be filled with the randomly selected symbol and each one's RangeType variable is changed from uDef to taken so its symbol cant be changed again. Then the loop keeps repeating until the entire map array of MapElements is filled.

Then the next DO LOOP just displays everything. I would be so grateful if anyone can help.

Oh ya, heres the code:

Quote: "RANDOMIZE TIMER()

#CONSTANT MAPX 70
#CONSTANT MAPY 30

TYPE MapElement
RangeType AS STRING
Symbol AS STRING
ENDTYPE

DIM Map(MAPX,MAPY) AS MapElement

FOR Y=0 TO MAPY : `SET DEFAULT VALUES FOR MAP ELEMENTS
FOR X=0 TO MAPX
Map(X,Y).RangeType="uDef"
NEXT X
NEXT Y

FOR Y=0 TO MAPY : REM GENERATE MAP
FOR X=0 TO MAPX
SELECT RND(4)
CASE 0
eSymbol$="M" : `MOUNTAINS
ENDCASE
CASE 1
eSymbol$="'" : `GRASS
ENDCASE
CASE 2
eSymbol$=":" : `SAND
ENDCASE
CASE 3
eSymbol$="~" : `WATER
ENDCASE
CASE 4
eSymbol$="m" : `HILLS
ENDCASE
ENDSELECT
IF Map(X,Y).RangeType="uDef"
L=10; `RND(17)+3
W=10; `RND(17)+3
FOR X2=1 TO L
FOR Y2=1 TO W
IF MAP(X+X2-1, Y+Y2-1).RangeType="uDef"
MAP(X+X2-1, Y+Y2-1).RangeType="taken"
MAP(X+X2-1, Y+Y2-1).Symbol=eSymbol$
ENDIF
NEXT Y2
NEXT X2
ENDIF
NEXT X
NEXT Y

do
CLS
FOR Y=0 TO MAPY
FOR X=0 TO MAPX
TEXT X * 13, Y * 13, MAP(X,Y).Symbol
NEXT X
NEXT Y
loop"
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 21st Oct 2003 12:59
Voila!



Remember you have set up map values for 0-70 which is actually 71 values.

If your mansion house needs haunting, just call Rentaghost!
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 21st Oct 2003 19:09
Thanks Spooky, but thats not exactly what Im looking for. As you can see by the comments in after L=10 and W=10 in the end I want to generate random squares and rectangles. I was just using the number 10 for testing purposes, and it needs to be able to work with #CONSTANT MAPX 70 and #CONSTANT MAPY 30
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 21st Oct 2003 19:30
Right, found a bug in arrays - will report as bug.

Run following code to see what I mean



Your biggest problem is setting values in array outside of limit.

You say MAP(X+X2-1, Y+Y2-1).Symbol=eSymbol$

now if X goes from 0 to 70 and X2 goes from 1 to 10, then max value is 70+10-1 = 79 which is outside of array bounds and causes bugs in rest of array.

you need to limit x2 to remainder of map, not just 10. So if X is at 68, then X2 can only go up to 3.

You still have a problem in that your map goes from 0 to 70 which is actually 71 values, which is why you get an extra column at right.

If your mansion house needs haunting, just call Rentaghost!
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 21st Oct 2003 22:39
Thanks Spooky, I dont mind the extra column at right, because in the end the squares arent going to be 10X10, they'll be random. The 10X10 was just for testing purposes.

I'll make the changes you suggested and tell you what happens.
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 21st Oct 2003 23:03
GENIOUS!! Thanks for the tips Spooky.
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 22nd Oct 2003 06:39
The other side of the coin is to limit your starting locations so that maxStartX + maxSizeX can not exceed the array limits.
--
TAZ
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 22nd Oct 2003 13:56
Nah limiting the starting position wouldnt affect anything. They way I have it set up now is to draw random rectangles between 3 and 20 units in length and width. But I was able to work around it.

I set it up to check if the starting position + length (or width) is greater than MAPX or MAPY and if it is Lenth (or width)= the difference between MAPX (or MAPY) and the starting point. Heres the commands I added:

Quote: " IF X+L > MAPX THEN L = MAPX - X + 1
IF Y+W > MAPY THEN W = MAPY - Y + 1"
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 22nd Oct 2003 13:58
Oh wait I misread your post, ya thats exactly what I did
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 22nd Oct 2003 23:30
Ok I thought I had this figured out but I guess not. I have it setup now to draw random sized rectangles but some still seem to be overwriting each other. I've numbered each terrain rectangle drawn and you can see some ranges arent rectangular as theyre being overwritten by others. Every time a map element is set rangetype is set to something else other than "uDef" So it cant be overwritten again. But apparently it still is. Heres the code, thanks for the help:
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 23rd Oct 2003 00:21
L=RND(15)+1
W=RND(15)+1

Remember RND gives a range from 0 to a number, so your code is giving L and W random numbers from 1 to 16. This is why you are getting strips of 1 and is why it looks like some are overlapping

If your mansion house needs haunting, just call Rentaghost!
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 23rd Oct 2003 01:38
I know, I do want strips of 1, but it doesnt look as if some rectangles are overlapping, some are actually overlapping.
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 23rd Oct 2003 01:52
It's impossible to have whole scrren broken up into rectangles. The overlapping effect is correct as coded.

e.g. (imagine 0's are undefined areas)



So basically 4 is trying to build a 3*3 sqaure but cannot because you already have filled some with 2's

Run this modified version of your code and all will become clear



If your mansion house needs haunting, just call Rentaghost!
Vandetta
21
Years of Service
User Offline
Joined: 17th Dec 2002
Location:
Posted: 23rd Oct 2003 02:20
Oh ya, thats obvious, why didnt I think of that? Oh well...ok everything is well. Thanks again Spooky

Login to post a reply

Server time is: 2024-11-25 14:58:51
Your offset time is: 2024-11-25 14:58:51