World creation

Previous topic - Next topic

MrTAToad

For my 5000th post, I present this very simple world creation routine :

Code (glbasic) Select
CONSTANT UNDEFINED% = 0
CONSTANT LAND% = 1
CONSTANT WATER% = 2
CONSTANT mapWidth% = 64
CONSTANT mapHeight% = 64
CONSTANT simultaneously%= 0
CONSTANT landRatio% = 50

GLOBAL landArray%[]
GLOBAL newLandArray%[]

DIM landArray%[mapWidth%][mapHeight%]
DIM newLandArray%[mapWidth%][mapHeight%]

generateMap()

FUNCTION generateMap%:
landOrWater()
display()
SHOWSCREEN
KEYWAIT
ENDFUNCTION

FUNCTION display%:
LOCAL i%,j%
LOCAL col%

FOR i%=0 TO mapWidth%-1
FOR j%=0 TO mapHeight%-1
IF landArray%[i%][j%]=LAND%
col%=RGB(127,255,127)
ELSE
col%=RGB(0,0,255)
ENDIF

DRAWRECT i%*16,j%*16,15,15,col%
NEXT
NEXT
ENDFUNCTION

FUNCTION landOrWater%:
LOCAL i%,j%

FOR i%=0 TO mapWidth%-1
FOR j%=0 TO mapHeight%-1
IF RND(100)<landRatio%
landArray%[i%][j%]=LAND%
ELSE
landArray%[i%][j%]=WATER%
ENDIF
NEXT
NEXT

FOR i%=1 TO 1
iterateLand(LAND%,WATER%)
NEXT
ENDFUNCTION

FUNCTION iterateLand%:land%,defaultLand%
LOCAL i%,j%

FOR i%=0 TO mapWidth%-1
FOR j%=0 TO mapHeight%-1
IF landArray[i%][j%]=land% OR landArray[i%][j%]=defaultLand%
IF adjacentLand(i%,j%,land%)>=5
IF NOT(simultaneously%)
landArray[i%][j%]=land%
ELSE
newLandArray%[i%][j%]=land%
ENDIF
ELSE
IF NOT(simultaneously%)
landArray%[i%][j%]=defaultLand%
ELSE
newLandArray%[i%][j%]=defaultLand%
ENDIF
ENDIF
ENDIF
NEXT
NEXT

IF simultaneously%
FOR i%=0 TO mapWidth%-1
FOR j%=0 TO mapHeight%-1
landArray[i%][j%]=newLandArray%[i%][j%]
NEXT
NEXT
ENDIF
ENDFUNCTION

FUNCTION adjacentLand%:col%,row%,lookFor%
LOCAL i%,j%,found%,x%,y%

found%=0
FOR i%=-1 TO 1
FOR j%=-1 TO 1
x%=col%+i%
y%=row%+j%
IF (x%>=0 AND x%<mapWidth%) AND (y%>=0 AND y%<mapHeight%)
IF landArray%[x%][y%]=lookFor%
INC found%
ENDIF
ENDIF
NEXT
NEXT

RETURN found%
ENDFUNCTION


Whilst it's not as elegant as the one in the showroom, that one does have problems when trying to put into an extended type.

[attachment deleted by admin]

mentalthink

5000th post... I think will see you in the world record winnes... =D =D =D

Thanks for this post... It´s very usefull at least for me... always the matrix of 2 Dimensions... are complex to fill data into them...

Just I beggin the last week a project, that´s use similar routines...

Thanks a lot..

I think at 9999th post you have Extra Live!!!  =D =D

MrTAToad

The code is based on some Javascript code I found somewhere - must find the link at some point :)

Gernot has posted more than me...

bigsofty

I like this, I have a Roguelike on my (long) to-do list, this would be great for the world map.  :good:
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

erico

have the rogue in plans too! =D

This is really great Tatoad!
I will read and try understand.

congrats for 5k! :booze:

MrTAToad


Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

MrTAToad