TILES = {
	{
		id = "blocking",
		variations = 4,
		edge_prio = 0,
		blocking = true,
	},
	{
		id = "village_road",
		variations = 4,
		edge_prio = 10,
	},
	{
		id = "village_dirt",
		variations = 4,
		edge_prio = 3,
	},
	{
		id = "village_rocky",
		variations = 4,
		edge_prio = 5,
	},
	{
		id = "village_mud",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "house_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "house_carpet",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "library_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "library_carpet",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "kitchen_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "kitchen_carpet",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "graveyard_dirt",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "graveyard_path",
		variations = 4,
		edge_prio = 10,
	},
	{
		id = "graveyard_rocky",
		variations = 4,
		edge_prio = 4,
	},
	{
		id = "crypt_slabs",
		variations = 4,
		edge_prio = 10,
	},
	{
		id = "crypt_dirt",
		variations = 4,
		edge_prio = 3,
	},
	{
		id = "armory_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "armory_carpet",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "smithy_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "smithy_workarea",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "wizards_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "wizards_carpet",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "alchemy_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "alchemy_carpet",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "ranger_floor",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "ranger_carpet",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "camp_dirt",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "camp_rocky",
		variations = 4,
		edge_prio = 4,
	},
	{
		id = "forest_dirt",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "forest_rocky",
		variations = 4,
		edge_prio = 4,
	},
	{
		id = "forest_leaves",
		variations = 4,
		edge_prio = 3,
	},
	{
		id = "forest_grass",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "forest_mud",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "forest_tracks",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "forest_bark",
		variations = 4,
		edge_prio = 3,
	},
	{
		id = "plains_dirt",
		variations = 4,
		edge_prio = 1,
	},
	{
		id = "plains_pebbles",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "plains_grass",
		variations = 4,
		edge_prio = 4,
	},
	{
		id = "plains_rocky",
		variations = 4,
		edge_prio = 2,
	},
	{
		id = "plains_twigs",
		variations = 4,
		edge_prio = 2,
	},
}


TILESET_EDGES = {"l", "r", "t", "b", "tl", "tr", "bl", "br"}
TILESET_INVERTED_EDGES = {"r", "l", "b", "t", "br", "bl", "tr", "tl"}

local function gen_name(id, name)
	local id_str = tostring(id)
	local id_string = string.rep("0", 4-id_str:len())..id_str
	local name = id_string.."-"..name
	--print(name)
	return name
end
local function gen_series(start_id, num, name)
	local series = {}
	for i=start_id, start_id +num do
		table.insert(series, gen_name(i, name))
	end
	return series
end

local piece_order = {
	{key = "c", num = 4},
	{key = "t", num = 1},
	{key = "b", num = 1},
	{key = "l", num = 1},
	{key = "r", num = 1},
	{key = "tr", num = 1},
	{key = "br", num = 1},
	{key = "tl", num = 1},
	{key = "bl", num = 1},
	{key = "itr", num = 1},
	{key = "ibr", num = 1},
	{key = "itl", num = 1},
	{key = "ibr", num = 1},
}
local function gen_set(start_id, name)
	local names = {}
	local offset = 0
	for _, def in ipairs(piece_order) do
		names[def.key] = gen_series(start_id+offset, def.num, name)
		offset = offset + def.num
	end
	return names, offset
end

tiles = {}
function tiles.init()
	local offset = 1
	for index, def in ipairs(TILES) do
		local names,num_added = gen_set(offset, "tiles")
		offset = offset + num_added
		def.spriteNames = names
		def.index = index
		def.edge_prio = def.edge_prio or index
		TILES[def.id] = def
		--def.color = def.color or {r = math.random(60,128), g = math.random(60,128), b = math.random(60,128)}
	end
end