[SOLVED] Perlin Noise Random Terrain Generation

tristonhopkins

  • Posts: 69
So I'm having a bit of a complicated problem with Stencyl.

I'm attempting to implement randomized terrain generation for a game I'm working on.  Currently, I have the behavior to read a 2D array as a map and output it as tiles completed.  Where I'm having issue is a behavior to create a 2D array map that is generated by the perlin noise algorithm.  I've spent the last few days doing some research, and I haven't been able to make any progress.

Does the Stencyl API even support the math functions needed to calculate the algorithm?  Unless I was doing something wrong, which is likely, even with what little I got Stencyl to attempt to do, it was throwing errors.  Ideally I'd like to make it a full behavior that I can control with attributes, but for now I'd just like to get the dang thing to work at all.

Thanks in advance!

« Last Edit: May 09, 2012, 02:35:00 pm by tristonhopkins »

Hectate

  • *
  • Posts: 4643
There's nothing in Stencyl or Flixel, but there's no reason you can't use Flash's built in perlin noise generator.

http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000799.html

That will generate a BitmapData which you could then sample individual pixels of to get a value for use in your tiles.

Edit: Further API details...
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#perlinNoise%28%29
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

tristonhopkins

  • Posts: 69
Thanks; generating the perlin noise into a bitmap sounds simple enough.

However, now I'm stumped finding a function to translate bitmapdata into a 2D array.  I've done some searching but I've come up empty handed.

EDIT: Okay.. I feel stupid.  I think I just found it as a method of BitmapData.  Going to see if it's what I need in the meantime.  Feel free to stop me if I'm wrong. :P

« Last Edit: May 08, 2012, 12:30:10 pm by tristonhopkins »

tristonhopkins

  • Posts: 69
Well, I've made progress!  I was able to generate the perlin noise with BitmapData, then I used getPixel to input each pixel into my 2D array.  Now I just have to get my map drawing behavior to interpret hexidecimal color codes.  The hex values are of a large range where as I only need to draw a few tiles..  :'(

Nomosoft

  • Posts: 122
Well, I've made progress!  I was able to generate the perlin noise with BitmapData, then I used getPixel to input each pixel into my 2D array.  Now I just have to get my map drawing behavior to interpret hexidecimal color codes.  The hex values are of a large range where as I only need to draw a few tiles..  :'(

Are you saying you want something like "Color 1 = tile1," "Color 2 = tile 2," etc, but you have too many colors? I'm not sure why the range of colors is an issue.

tristonhopkins

  • Posts: 69
Well when my behavior checks each x,y in my 2D array, it's passed a hexidecimal color code.  For example, everytime it's passed a hexidecimal greater than #999999, it will draw a grass tile.  Anything less, a water tile.  I have the behavior for it checking each hex in the array working just fine.  I just need to fix the part in the behavior where it draws tiles so that it can interpret the entire hex range of colors.

Hectate

  • *
  • Posts: 4643
I think there is a HexToRGB method somewhere if you'd rather work with 0-255 values or something.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

tristonhopkins

  • Posts: 69
I think there is a HexToRGB method somewhere if you'd rather work with 0-255 values or something.

That honestly might be a bit easier.

tristonhopkins

  • Posts: 69
And now for the fruit of my labor!  I finally have it working. :D

http://www.stencyl.com/game/play/12566

Ryusui

  • Posts: 827
In the event of a firestorm, the salad bar will remain open.

tristonhopkins

  • Posts: 69
Color me impressed!

I'm glad I could help you with the array business. ;)

Haha, yes, thank you very much for your help!  You definitely set me down the right path.

I'm a bit proud of a new feature I've implemented on my local version.  Now it rejects maps if they don't fit certain criteria.  It needs some expanding upon, but it works. :D

Hectate

  • *
  • Posts: 4643
Awesome work there! What's the final scene size in tiles that is being generated? That looks like more than the original 40 that you had.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

tristonhopkins

  • Posts: 69
Awesome work there! What's the final scene size in tiles that is being generated? That looks like more than the original 40 that you had.
Thanks!  I actually increased it to 120x120.