Author Topic: Handy dandy tile randomizer.  (Read 1179 times)

zez

  • Active Member
  • ***
  • Posts: 203
  • Karma: +0/-0
    • View Profile
    • Devlog
Handy dandy tile randomizer.
« on: Wed, Jan 5, 2011 »
I dunno about the rest of you, but I pretty much hate setting a million CSV values for tiles that basically act the same way, but have some minor difference visually too break up the 'grid'.

Thus, I made this helper class. It has two basic uses. The first is fairly simple and just generates a tile map that has a flood fill of random tiles. The later will take an embedded tile map, and randomize any tile(s) you tell it too.

Usage is fairly simple, you either flood fill like so
Code: [Select]
add(new randomtiles(tilesheet, true, a number between 0 and 1 that is the likelyhood of any given tile being random, the width in tiles, the height in tiles, the number of tiles on the sheet/ the last tile on the sheet you want too randomly show);

or you load an embedded tile map and randomize it like this
Code: [Select]
var ranmap:randomtiles = new randomtiles(tilesheet, false);
//make any changes too collideIndex or drawIndex or what have you the old fashioned way
ranmap.randomize(new embedded_map_csv, [tile, values, you, want, randomized], [range, of, tile, randomization], [chance, of, randomizing, tile]);
add(ranmap);
This bit should probably have the disclaimer that your arrays have to line up meaning tile[0] should be the first tile in whatever range of tiles the variation is for, randomrange[0] should be how many tiles away the last possible tile from tile[0] is, and variation[0] is how likely it is too change tile[0] too another tile.
Thus if tile[0] = 5, randomrange[0] = 5, and variation[0] =0.23, you have a 23% chance of changing every tile with a value of 5 too a tile with a value between 5 & 10.

Final bit of explanation, variation should be a number between 0 and 1. A value of 0 or less will NEVER randomize a tile, so you may as well just use FlxTilemap, and a value >= 1 will always randomize the tile (in the case of the second usage the tile corresponding the variation index.) You may very well want to use 1 sometimes, and in those cases there isnt really anything stopping you from using 1 billion instead of one, other then it being kind of silly.

Anyhow, heres the class in case anyone else wants it.
Code: [Select]
package com
{
import org.flixel.*;
public class randomtiles extends FlxTilemap
{
private var _tilessheet:Class;
public function randomtiles(tilesheet:Class, autogenerate:Boolean, variation:Number = 0, map_width:int = 0, map_height:int = 0, tiles:int = 0)
{
if (autogenerate == true)
{
var mapstring:String = "";
for (var i:int = 0; i < map_width * map_height; i++)
{
if (i % map_width == 0)
{
if (i > 0)
{
mapstring += "\n";
}
if (FlxU.random() < variation)
{
mapstring += (int(FlxU.random() * tiles)).toString();
}
else
{
mapstring += "0";
}
}
else if (FlxU.random() < variation)
{
mapstring += "," + (int(FlxU.random() * tiles)).toString();
}
else
{
mapstring += ",0";
}
}
drawIndex = 0;
loadMap(mapstring, tilesheet);
}
else
{
_tilessheet = tilesheet;
}
}
public function randomize(mapdata:String, tiles:Array, randomrange:Array, variation:Array):void
{
loadMap(mapdata, _tilessheet);
for (var t:int = 0; t < tiles.length; t++)
{
for (var i:int = 0; i < totalTiles; i++)
{
if (getTileByIndex(i) == tiles[t] && FlxU.random() < variation[t])
{
setTileByIndex(i, tiles[t] + int(FlxU.random() * randomrange[t]));
}
}
}

}

}

}

Should I maybe give it a user interface and make it just spit out CSV files, thus making it a "randomize tile map csv files" app instead of a flixel exclusive "make your maps randomize themselves on load" class?

It loads pretty quick and isnt particularly system intensive, even when doing the silly for loop in a for loop thing well randomizing existing maps (adds about 3ms to update on my box well doing stress tests, and doesnt change ram usage for long enough for me too read or notice the change,) so that isnt really an issue....

Gold_Ninja

  • Member
  • **
  • Posts: 80
  • Karma: +0/-0
  • Yeah Yeah!! Wall Echo Saw - War  よろしくね♡
    • View Profile
Re: Handy dandy tile randomizer.
« Reply #1 on: Wed, Jan 5, 2011 »
Cool, you should comment it up and add it to the Flash Game Dojo Snippet thing.
- Gold_Ninja