GLBasic forum

Codesnippets => Code Snippets => Topic started by: MrTAToad on 2012-Oct-12

Title: World creation
Post by: MrTAToad on 2012-Oct-12
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]
Title: Re: World creation
Post by: mentalthink on 2012-Oct-12
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
Title: Re: World creation
Post by: MrTAToad on 2012-Oct-12
The code is based on some Javascript code I found somewhere - must find the link at some point :)

Gernot has posted more than me...
Title: Re: World creation
Post by: bigsofty on 2012-Oct-12
I like this, I have a Roguelike on my (long) to-do list, this would be great for the world map.  :good:
Title: Re: World creation
Post by: erico on 2012-Oct-13
have the rogue in plans too! =D

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

congrats for 5k! :booze:
Title: Re: World creation
Post by: MrTAToad on 2012-Oct-13
Thanks!
Title: Re: World creation
Post by: Schranz0r on 2012-Oct-13
Spamer! :D  <3
Title: Re: World creation
Post by: MrTAToad on 2012-Oct-14
Heh   :P