Procedural generation of a 2d map

560 views
Skip to first unread message

Bl4ckb0ne

unread,
Jun 14, 2014, 4:14:39 PM6/14/14
to haxef...@googlegroups.com
Hi,
For a game idea, I need to create a random map with a regular tileset (this one for exemple).

I was doing some researches on the internet about the 2d random generation, but I found nothing about the proper positioning of the sand spots, the trees or the flower.

So, if you guys got some interesting articles/algorithm/examples that would be great. And if my stuff works, I'll make a demo for the github repo.

Thanks.

SruloArt

unread,
Jun 14, 2014, 7:48:50 PM6/14/14
to
The Best thing I can think of is to look at the way Ogmo / Tiled layers and terrains are handled on Flixel, and then extend Flixel's autotiles system to create them procedurally on top of (or within) generated maps (like the one on this cool demo: https://github.com/HaxeFlixel/flixel-demos/tree/dev/Other/BSPMapGen).

Here's a quick Pseudo Algorithm (or whatever you want to call it) for such a class, (lot's of stuff to discuss but Google groups is really a poor place for it, Github should be the place to raise this):
- You create a tile object (which is basically a group of tiles).
- You set up -
  • the size of each fixed object (A tree is made out of 2 tile, for example, 9 tiles required for a 9slice and so on)
  • the tiles graphics (which should be the same size of your object, by order, can also have random tiles you can load instead of the ones you chose so you can have different looking trees for example).
  • the min-max values for its appearance on the map, let's say, you want 2-10 trees and 2 flowers, and a 100water (lake or not). You can also try to use Ratio for easy map building, but let's ignore it for now.
  • Good-Neighbors  - game objects that can be placed near it (including it). This can be used to make lakes, bridges (dynamic stuff) or just place a tree far away from a flower.
  • Bad-Neighbors - game objects that can't be placed near it (including it).  
  • Every game object have a min/max distance from each Good and Bad neighbor tiles (with directions, that's tricky).
  • You set up your logic to go through all of this to determine how all the tiles can be placed correctly. (A tree is made out of 2 tiles, it can't be placed more than 2 tiles north and south from a flower, a tree can be placed inside of a lake [inside is represented with a fake-minus tiles, e.g. -2,], the lake size is a 100water, a water tile can only be placed near 3 other waters [a lake square], but then you need to change the corners graphics to work with other tile objects...the list goes on and on...)

Bl4ckb0ne

unread,
Jun 14, 2014, 9:57:25 PM6/14/14
to haxef...@googlegroups.com
I was trying something tonight, just for generate the grass with the 2 tiles and 1 flower. But I got some troubles with the 2d array x), it keeps yelling "Invalid array access" when I try " array[i][j] = var; "

Otherwise, is there a way to have more chance to get one number or less chances to get another number with the FlxRandom? Like there's 66% chances to get 1 or 2, and 33% chances to get a 3 

Gama11

unread,
Jun 15, 2014, 5:46:45 AM6/15/14
to haxef...@googlegroups.com
Have you made sure to initialize the array properly? The inner array as well? array = []; won't do for 2D array. Have a look at this.

There's FlxRandom.weightedPick().

Sebastien V.

unread,
Jun 15, 2014, 1:58:16 PM6/15/14
to haxef...@googlegroups.com
Another option is to make tile chunks in Tiled or DAME and load those at runtime. That way you don't have to worry about loading and arranging individual objects/tiles, only the sections of map.

Bl4ckb0ne

unread,
Jun 15, 2014, 2:32:23 PM6/15/14
to haxef...@googlegroups.com
I really want to try something home made.
Currently I resolve my problem. The FlxTilemap function loadMap do not want an Array<Array<Int>> as MapData. It wants something dynamic. So I'll save my created array in a file, and then put the array with a Assets.getText(file) into the loadMap.
It's hard because there's not so much doc and example about the File IO.

Gama11

unread,
Jun 15, 2014, 2:48:02 PM6/15/14
to
loadMap() works with an Array<Int> actually:

package;
 
import flixel.FlxState;
import flixel.tile.FlxTilemap;
using flixel.util.FlxSpriteUtil;

class PlayState extends FlxState
{
override public function create()
{
var mapData = [0, 0, 1, 0, 1, 1];
var tilemap = new FlxTilemap();
tilemap.widthInTiles = 2;
tilemap.heightInTiles = 3;
tilemap.loadMap(mapData, GraphicAuto, 8, 8);
add(tilemap);
}
}

Bl4ckb0ne

unread,
Jun 15, 2014, 5:08:53 PM6/15/14
to
Here's what I've got so far, I still need to adjust some flower generation and stuff.
If someone want to take a look, where's the project.
Hordes.zip
Reply all
Reply to author
Forward
0 new messages