local RULE_ZONES = {
	categories = {
		blocking = {
			tiles = {
				blocking = 1,
			},			
		},
		village_street = {
			characters = {
			},
			tiles = {
				village_road = 10,
				village_dirt = 1,
			},
		},
		village_sidewalk = {
			characters = {
				cart = 1,
				wheel_stack = 1,
				haybale = 3,
			},
			tiles = {
				village_rocky = 10,
				village_dirt = 2,
			},
		},
		village = {
			characters = {
				villager = 1,
				stool = 2,
				crate = 2,
				barrel = 1,					
			},
			tiles = {
				village_dirt = 10,
				village_rocky = 1,
			},
		},
		well = {
			characters = {
				rope_coil = 1,
				bucket = 1,
				well = 1,
			},
			tiles = {
				village_rocky = 5,
				village_dirt = 1,
			},
		},
		house = {
			characters = {
				mouse = 1,
				butterfly = 1,
				house_table = 1,
				house_chair = 2,
			},
			tiles = {
				house_carpet = 1,
			},
		},
		house_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_house= 1,
			},
			corners = {
				corner_house= 1,
			}
		},
		house_wall = {
			characters = {
				house_shelf = 1,
				house_chest = 1,
			},
			tiles = {
				house_floor = 1,
			},
		},
		library = {
			characters = {
				moth = 1,
				librarian = 1,
				library_table = 1,
				library_chair = 2,
			},
			tiles = {
				library_carpet = 1,
			},
		},
		library_wall = {
			characters = {
				book_shelf = 5,
				library_chest = 1,
			},
			tiles = {
				library_floor = 1,
			},
		},
		library_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_library = 1,
			},
			corners = {
				corner_library = 1,
			}
		},
		kitchen = {
			characters = {
				maggot = 1,
				chef = 1,
				kitchen_table = 1,
				kitchen_chair = 2,				
				food_basket = 1,
			},
			tiles = {
				kitchen_carpet = 1,
			},
		},
		kitchen_wall = {
			characters = {
				cockroach = 1,
				kitchen_chest = 1,
				kitchen_bin = 1,
				food_crate = 1,
				food_basket = 2,
			},
			tiles = {
				kitchen_floor = 1,
			},
		},
		kitchen_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_kitchen = 1,
			},
			corners = {
				corner_kitchen = 1,
			}
		},
		graveyard = {
			characters = {
				bat = 3,
				grave_mound = 5,
				grave_stone = 3,
			},
			tiles = {
				graveyard_dirt = 1,
			},
		},
		graveyard_core = {
			characters = {
				zombie = 2,				
				graveyard_chest = 1,
				grave_statue = 1,
			},
			tiles = {
				graveyard_path = 2,
				graveyard_dirt = 1,
				graveyard_rocky = 2,
			},
		},
		graveyard_path = {
			characters = {
				zombie = 1,
				skeleton = 1,
				graveyard_chest = 1,
			},
			tiles = {
				graveyard_path = 6,
				graveyard_dirt = 1,
			},
		},
		graveyard_wall = {
			characters = {
				small_urn = 3,
				big_urn = 1,
			},
			tiles = {
				graveyard_rocky = 5,
				graveyard_dirt = 3,
			},
		},
		graveyard_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_graveyard = 1,
			},
			corners = {
				corner_graveyard = 1,
			}
		},
		vampire_crypt = {
			characters = {
				zombie = 3,
				skeleton = 3,
				small_urn = 2,
				big_urn = 3,
			},
			tiles = {
				crypt_dirt = 10,
				crypt_slabs = 1,
			},
		},
		vampire_crypt_core = {
			characters = {
				vampire = 1,
				big_urn = 2,
				crypt_chest = 1,
			},
			tiles = {
				crypt_dirt = 2,
				crypt_slabs = 6,
			},
		},
		vampire_crypt_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_necromancer_crypt = 1,
			},
			corners = {
				corner_necromancer_crypt = 1,
			}
		},
		necromancer_crypt = {
			characters = {
				mummy = 6,
				small_urn = 2,
				big_urn = 3,
			},
			tiles = {
				crypt_dirt = 10,
				crypt_slabs = 1,
			},
		},
		necromancer_crypt_core = {
			characters = {
				necromancer = 1,
				skeleton_mage = 4,
				crypt_chest = 1,
				big_urn = 2,
			},
			tiles = {
				crypt_dirt = 2,
				crypt_slabs = 6,
			},
		},
		necromancer_crypt_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_necromancer_crypt = 1,
			},
			corners = {
				corner_necromancer_crypt = 1,
			}
		},
		armory = {
			characters = {
				armorer = 1,
				magpie = 1,
				work_chair = 2,
				work_table = 1,
				armor_rack = 1,
			},
			tiles = {
				armory_carpet = 1,
			},
		},
		armory_wall = {
			characters = {
				tool_box = 1,
				armorer_chest = 1,
				armorer_cabinet = 1,
			},
			tiles = {
				armory_floor = 1,
			},
		},
		armory_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_house = 1,
			},
			corners = {
				corner_house = 1,
			}
		},
		smithy = {
			characters = {
				smith = 1,
				firefly = 1,
				weapon_rack = 1,
				work_chair = 2,
				work_table = 1,
				anvil = 1,
			},
			tiles = {
				smithy_workarea = 1,
			},
		},
		smithy_wall = {
			characters = {
				smith_chest = 1,
				smith_cabinet = 1,
				tool_box = 1,
			},
			tiles = {
				smithy_floor = 1,
			},
		},
		smithy_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_house = 1,
			},
			corners = {
				corner_house = 1,
			}
		},
		wizards_tower = {
			characters = {
				wizard = 1,
				lizard = 1,
				owl = 1,
				jewlery_box = 1,
			},
			tiles = {
				wizards_carpet = 1,
			},
		},
		wizards_tower_wall = {
			characters = {
				wizard_chest = 1,
				robe_closet = 1,
				staff_rack = 1,
			},
			tiles = {
				wizards_floor = 1,
			}
		},
		wizards_tower_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_library = 1,
			},
			corners = {
				corner_library = 1,
			}
		},
		merchant_wagon = {
			characters = {
				merchant = 1,
				merchant_wagon = 1,
				dog = 1,
				hawk = 1,
				merchant_knapsack = 1,
				merchant_crate = 2,
			},
			tiles = {
				village_dirt = 3,
				village_road = 10,
				village_mud = 4,
			},
		},
		alchemy_shop = {
			characters = {
				alchemist = 1,
				toad = 1,
				raven = 1,
			},
			tiles = {
				alchemy_carpet = 1,
			}
		},
		alchemy_shop_wall = {
			characters = {
				potion_rack = 1,
				alchemist_chest = 1,
			},
			tiles = {
				alchemy_floor = 1,
			}
		},
		alchemy_shop_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_alchemy = 1,
			},
			corners = {
				corner_alchemy = 1,
			}
		},
		ranger_shop = {
			characters = {
				ranger = 1,
				cat = 1,
				gecko = 1,
			},
			tiles = {
				ranger_carpet = 1,
			},
		},
		ranger_shop_wall = {
			characters = {
				ranger_chest = 1,
				writing_desk = 1,
				pen_box = 1,
			},
			tiles = {
				ranger_floor = 1,
			},
		},
		ranger_shop_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_house = 1,
			},
			corners = {
				corner_house = 1,
			}
		},
		insane_ritual_camp = {
			characters = {
				mentalist_striker = 3,
				arachnid_small = 2,
				arachnid_big = 1,
				mentalist_chest = 1,
				mentalist_tent = 3,
				ritual_urn = 2,
				mentalist_barrel = 1,
				ingredient_crate = 1,
			},
			tiles = {
				camp_dirt = 10,
				camp_rocky = 1,
			},
		},
		insane_ritual_camp_core = {
			characters = {
				mentalist_strangler = 2,
				ritual_stake = 5,
				ritual_fire = 1,
			},
			tiles = {
				camp_dirt = 1,
				camp_rocky = 4,
			},
		},
		insane_warrior_camp = {
			characters = {
				mentalist_striker = 3,
				weredog = 1,
				mentalist_chest = 1,
				mentalist_tent = 2,
				bed_roll = 2,
				food_crate = 1,
			},
			tiles = {
				camp_dirt = 10,
				camp_rocky = 1,
			},

		},
		insane_warrior_camp_core = {
			characters = {
				mentalist_thrower = 2,
				barbed_stake = 2,
				camp_fire = 1,
			},
			tiles = {
				camp_dirt = 1,
				camp_rocky = 4,
			},
		},
		insane_invoker_camp = {
			characters = {
				mentalist_striker = 3,
				mentalist_chest = 1,
				mentalist_tent = 3,
				ritual_urn = 1,
				barbed_stake = 3,
				jewlery_box = 1,
			},
			tiles = {
				camp_dirt = 10,
				camp_rocky = 1,
			},
		},
		insane_invoker_camp_core = {
			characters = {
				mentalist_invoker = 2,
				murdercrow = 1,
				camp_fire = 1,
			},
			tiles = {
				camp_dirt = 1,
				camp_rocky = 4,
			},
		},
		insane_breeder_camp = {
			characters = {
				weredog = 5,
				animal_cage = 3,
				mentalist_chest = 1,
				mentalist_tent = 1,
				bed_roll = 2,
				barbed_stake = 8,
				mentalist_barrel = 2,
			},
			tiles = {
				camp_dirt = 10,
				camp_rocky = 1,
			},
		},
		insane_breeder_camp_core = {
			characters = {
				mentalist_striker = 3,
				camp_fire = 1,
				food_crate = 2,
			},
			tiles = {
				camp_dirt = 1,
				camp_rocky = 4,
			},
		},		
		possum_den = {
			characters = {
				possum = 2,
				nest_small = 1,
			},
			tiles = {
				forest_dirt = 3,
				forest_leaves = 5,
				forest_grass = 3,
			},
		},
		bunny_den = {
			characters = {
				bunny = 5,
				nest_small = 1,
			},
			tiles = {
				forest_dirt = 1,
				forest_leaves = 3,
				forest_grass = 6,
			},
		},
		turtle_den = {
			characters = {
				turtle = 1,
				nest_small = 1,
			},
			tiles = {
				forest_dirt = 1,
				forest_leaves = 2,
				forest_grass = 3,
				forest_mud = 2,
			},
		},
		snake_den = {
			characters = {
				snake = 1,
				nest_small = 1,
			},
			tiles = {
				forest_dirt = 1,
				forest_rocky = 4,
				forest_leaves = 2,
			},
		},
		fox_den = {
			characters = {
				fox = 2,
				nest_medium = 1,
			},
			tiles = {
				forest_rocky = 6,
				forest_grass = 5,
			},
		},
		wolf_den = {
			characters = {
				wolf = 4,
				nest_medium = 1,
			},
			tiles = {
				forest_dirt = 5,
				forest_grass = 4,
				forest_tracks = 2,
			},
		},
		deer_den = {
			characters = {
				deer = 3,
				nest_medium = 1,
			},
			tiles = {
				forest_dirt = 1,
				forest_leaves = 1,
				forest_grass = 6,
			},
		},
		wolverine_den = {
			characters = {
				wolverine = 1,
				nest_medium = 1,
			},
			tiles = {
				forest_leaves = 3,
				forest_grass = 2,
				forest_bark = 3,
			},
		},
		moose_den = {
			characters = {
				moose = 1,
				nest_big = 1,
			},
			tiles = {
				forest_dirt = 5,
				forest_mud = 3,
				forest_tracks = 2,
				forest_bark = 5,
			},
		},
		bear_den = {
			characters = {
				bear = 1,
				nest_big = 1,
			},
			tiles = {
				forest_rocky = 4,
				forest_grass = 3,
				forest_tracks = 6,
			},
		},
		tiger_den = {
			characters = {
				tiger = 3,
				nest_big = 1,
			},
			tiles = {
				plains_dirt = 5,
				plains_pebbles = 3,
				plains_grass = 3,
				plains_twigs = 2,
			},
		},
		lion_den = {
			characters = {
				lion = 2,
				nest_big = 1,
			},
			tiles = {
				plains_dirt = 5,
				plains_grass = 3,
				plains_rocky = 4,
			},
		},
		plains = {
			characters = {
				plant_pretty = 3,
				plant_potent = 1,
				grass = 2,
			},
			tiles = {
				plains_dirt = 5,
				plains_grass = 2,
				plains_rocky = 1,
			},
		},
		plains_shrubbery = {
			characters = {
				plant_potent = 1,
				bush_small = 2,
				grass = 2,
				grass_tall = 1,
			},
			tiles = {
				plains_dirt = 5,
				plains_pebbles = 2,
				plains_grass = 5,
				plains_twigs = 2,
			},
		},
		plains_rocky = {
			characters = {
				plant_potent = 1,
				rock_big = 1,
				rock_small = 3,
				bush_small = 1,
				rock_loose = 1,
				grass = 2,
			},
			tiles = {
				plains_dirt = 4,
				plains_pebbles = 4,
				plains_grass = 2,
				plains_rocky = 8,
			},
		},
		forest_light = {
			characters = {
				plant_potent = 1,
				fruit_bush = 1,
				tree_small = 3,
				bush_small = 3,
				grass = 5,
			},
			tiles = {
				forest_dirt = 5,
				forest_rocky = 1,
				forest_grass = 3,
			},
		},
		forest_dense = {
			characters = {
				plant_potent = 2,
				fruit_bush = 1,
				tree_small = 1,
				bush_small = 1,
				bush_big = 2,
				tree_big = 2,
				grass = 3,
				grass_tall = 2,
				rotten_log = 1,
				rock_small = 1,
			},
			tiles = {
				forest_dirt = 3,
				forest_rocky = 1,
				forest_leaves = 2,
				forest_grass = 10,
				forest_bark = 2,
			},
		},
		forest_old = {
			characters = {
				plant_potent = 2,
				spiky_bush = 2,
				bush_big = 3,
				tree_big = 3,
				grass_tall = 3,
				rotten_log = 2,
				rock_small = 2,
				rock_big = 1,
				tangled_roots = 1,
				rock_loose = 1,
			},
			tiles = {
				forest_rocky = 2,
				forest_leaves = 3,
				forest_grass = 7,
				forest_mud = 1,
				forest_bark = 5,
			},
		},
		forest_vile = {
			characters = {
				spiky_bush = 3,
				bush_big = 2,
				tree_big = 3,
				grass_tall = 2,
				rotten_log = 3,
				rock_small = 1,
				rock_big = 2,
				tangled_roots = 2,
				rock_loose = 1,
				bog_puddle = 1,
			},
			tiles = {
				forest_leaves = 4,
				forest_grass = 3,
				forest_mud = 4,
				forest_bark = 4,
			},
		},
		forest_rotten = {
			characters = {
				spiky_bush = 5,
				bush_big = 1,
				tree_big = 4,
				grass_tall = 1,
				rotten_log = 4,
				rock_small = 2,
				rock_big = 3,
				tangled_roots = 3,
				rock_loose = 1,
				bog_puddle = 2,
			},
			tiles = {
				forest_rocky = 6,
				forest_mud = 8,
				forest_bark = 8,
			},
		},

		monster_house = {
			characters = {
				skeleton_mage = 2,
				skeleton = 2,
				bat = 1,
				weredog = 1,
				house_table = 1,
				house_chair = 2,
			},
			tiles = {
				house_carpet = 1,
			},
		},
		monster_house_blocking = {
			tiles = {
				blocking = 1,
			},
			walls = {
				wall_house= 1,
			},
			corners = {
				corner_house= 1,
			}
		},
		monster_house_wall = {
			characters = {
				house_shelf = 1,
				house_chest = 1,
			},
			tiles = {
				house_floor = 1,
			},
		},
		monster_village_street = {
			characters = {
			},
			tiles = {
				village_road = 10,
				village_dirt = 1,
			},
		},
		monster_village = {
			characters = {
				zombie = 2,
				stool = 2,
				crate = 2,
				barrel = 1,					
			},
			tiles = {
				village_dirt = 10,
				village_rocky = 1,
			},
		},
		monster_well = {
			characters = {
				rope_coil = 1,
				bucket = 1,
				well = 1,
			},
			tiles = {
				village_rocky = 5,
				village_dirt = 1,
			},
		},
	},
	generate = function(rules, def, level)
		if not def[level] then
			local level_def = {}
			level_def.tiles = def.tiles
			level_def.walls = def.walls
			level_def.corners = def.corners
						
			if def.characters then
				local characters = {}
				for char,amt in pairs(def.characters) do
					local character_rules = rules:get_categories("character")
					if character_rules[char] then
						characters[char] = amt
					else
						local best = nil
						local best_amt = nil
						for category, cat_def in pairs(character_rules) do
							if cat_def.filters then
								assert(cat_def.filters, category)
								if cat_def.filters[char] then
									local amt_factor = 1
									if cat_def.level_peak then
										local diff = math.abs(level-cat_def.level_peak)
										amt_factor = 1-math.clamp(diff/(cat_def.level_range*0.5), 0, 1)
										--print(amount_factor, level, cat_def.level_peak, cat_def.level_range)
									end
									if amt_factor and amt_factor > 0 then
										if not best or amt_factor > best_amt then
											best = category
											best_amt = amt_factor
										end																				
									end
								end
							end
						end
						if best then
							characters[best] = amt
						end
					end
				end
				level_def.characters = characters
			end

			def[level] = level_def
		end

		return def[level]
	end,
}

return RULE_ZONES