Randomize tiles

randomize non-empty tiles or change them all at once
very simple script, but it does what it does.

randomize-tiles-v0.01

before and after:
tileset-tilemap-colormode-test-1_00 tileset-tilemap-colormode-test-1_01

HOW TO USE:

  1. create a tileset with some tiles in it
  2. draw tiles on the tilemap layer
  3. run script
  4. set min and max values
  5. click on ‘randomize’ button

NOTEs:
– preview window needs refresh to update the changes (and app only shows them because of app.refresh)
– min value must be smaller than max (lua…)
– but you can set both to same value and use the script as flood-fill
– you better make sure you don’t try to randomize empty cel or use non existent tiles. that’s on you, pal.

as usual, always make a backup before running anything.

-- RANDOMIZE TILES 0.01 
-- b236 
-- 
-- make a tile set and draw where you want the tiles to be 
-- run script 
-- set range of tile indices to randomize 
-- click on randomize 
-- 


local cel 
local rng_min = 1
local rng_max = 1

local img_h 
local img_w 


local dlg = Dialog{ title = "RANDOMIZE TILES 0.01  " }

dlg:number{
	id = "in_min",
	--label = "min",
	text = "min",
	decimals = 0,
	onchange =  function() 
					rng_min = dlg.data.in_min
				end 
}

dlg:number{
	id = "in_max",
	--label = "max",
	text = "max",
	decimals = 0,
	onchange =  function() 
					rng_max = dlg.data.in_max 
				end 
}

dlg:button{ 	
	id = "confirm_madness", 
	--label = string, 
	text = " RANDOMIZE ",
	selected = true,
	focus = true,
	onclick = function() randomize_tiles() end 
}

dlg:button{ 	
	id = " close ", 
	--label = string, 
	text = "close",
	selected = false,
	focus = false,
	onclick = function() dlg:close() end 
}

dlg:show{ wait=false }


function randomize_tiles() 
	cel = app.cel 
	img_h = cel.image.height
	img_w = cel.image.width 
	
	if cel.image.colorMode == ColorMode.TILEMAP then
		local tilemap = cel.image 

		for yy = 0, img_h-1, 1 do 
			for xx = 0, img_w-1, 1 do 
				local pxl = cel.image:getPixel(xx,yy) 
				local val = math.random(rng_min, rng_max)
				if pxl > 0 then 
					cel.image:putPixel(xx,yy, val) 
				end 
			end 
		end 
		app.refresh() -- uuu, naughty! 
	else 
		print("select tilemap!") 
		--dlg:close()
	end 
	
end 
7 Likes

hey, Im not sure what you mean by “create tileset with some tiles in it” ive tried to run this script and it just keeps sending back “select a tilemap” thank you

well, you can’t randomize tiles when you don’t have any. and you can have tiles only in tilemap layer. so you have to create it first - either from scratch or by converting existing layer.

I changed one of my layer into “tilemap” and it still doesnt work.

that’s weird, the fact you did get the message to select the tilemap implies the script is working. do you have the layer selected?

Im sure I have the layer selected. if what you mean is being on the “tilemap” layer

yes. well… i’m honestly not sure, what’s wrong then.

Oh wow, I’m trying to make animations that emulate the look of various levels of EM radiation hitting a camera sensor, and placing tiles by hand was taking forever. This is just what I needed, thank you for making it!

1 Like