local RULE_INVENTORIES = {
	categories = {
		hero = {
			inventory_type = "hero",
		},
		npc = {
			inventory_type = "npc",
			level_range_multiplier = 3,
			initial_items = {
				torso = "armor",
			},
		},
		npc_armorer = {
			inventory_type = "npc_armorer",
			level_range_multiplier = 3,
			initial_items = {
				primary = "melee",
				head = "armor",
				torso = "armor",
				legs = "armor",
				feet = "armor",
				hands = "armor",
			},
		},
		npc_smith = {
			inventory_type = "npc_smith",
			level_range_multiplier = 3,
			initial_items = {
				primary = "melee",
				head = "clothes",
				torso = "armor",
			},
		},
		npc_wizard = {
			inventory_type = "npc_wizard",
			level_range_multiplier = 3,
			initial_items = {
				primary = "magic",
				secondary = "magic",
				head = "clothes",
				torso = "clothes",
			},
		},
		npc_ranger = {
			inventory_type = "npc_ranger",
			level_range_multiplier = 3,
			initial_items = {
				secondary = "ranged",
				head = "clothes",
				torso = "armor",
			},
		},
		npc_alchemist = {
			inventory_type = "npc_alchemist",
			level_range_multiplier = 3,
			initial_items = {
				primary = "melee",
				head = "clothes",
				torso = "clothes",
				hotbar = "potion",
			},
		},
		npc_merchant = {
			inventory_type = "npc_merchant",
			level_range_multiplier = 3,
			initial_items = {
				primary = "melee",
				secondary = "ranged",
				head = "clothes",
				torso = "clothes",
				legs = "clothes",
				feet = "clothes",
				hands = "clothes",
				hotbar = "potion",
			}
		},
		monster_melee = {
			inventory_type = "monster_melee",
			level_range_multiplier = 3,
			initial_items = {
				primary = "melee",
				head = "armor",
				torso = "armor",
			},
		},
		monster_ranged = {
			inventory_type = "monster_ranged",
			level_range_multiplier = 3,
			initial_items = {
				secondary = "ranged",
				head = "clothes",
				torso = "clothes",
			},
		},
		monster_mage = {
			inventory_type = "monster_mage",
			level_range_multiplier = 3,
			initial_items = {
				secondary = "magic",
				head = "clothes",
				torso = "clothes",
				finger = "jewlery",
			},
		},
		monster_mage_boss = {
			inventory_type = "monster_mage_boss",
			level_range_multiplier = 3,
			initial_items = {
				primary = "magic",
				secondary = "magic",
				head = "clothes",
				torso = "clothes",
				legs = "clothes",
				feet = "clothes",
				hands = "clothes",
				hotbar = "potion",
			},
		},
		monster_mixed = {
			inventory_type = "monster_mixed",
			level_range_multiplier = 3,
			initial_items = {
				primary = "melee",
				secondary = "ranged",
				head = "clothes",
				torso = "armor",
			},
		},
		monster_natural = {
			inventory_type = "monster_natural",
			level_range_multiplier = 3,
			initial_items = {
				head = "clothes",
				torso = "armor",
			},
		},
		shop = {
			inventory_type = "shop",
		},
	},
	generate = function(rules, def, level, index)
		local inv = assert(Inventory:new(def.inventory_type), "no inv", def.inventory_type)
		local initial = def.initial_items
		if initial then
			if not def[level] then
				local initial_filters = {}
				local item_rules = rules:get_categories("item")

				for slot, amt in pairs(inv.def.slots) do
					local cat_filter = initial[slot]
					if cat_filter then
						
						local slot_filter = {}
						initial_filters[slot] = slot_filter

						for category, cat_def in pairs(item_rules) do
							if slot == cat_def.item_slot then
								if cat_def.filters and cat_def.filters[cat_filter] 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*def.level_range_multiplier), 0, 1)
										--print(amount_factor, level, cat_def.level_peak, cat_def.level_range)
									end
									if amt_factor and amt_factor > 0 then
										local final_chance = amt*amt_factor*(cat_def.level_peak_factor or 1)
										slot_filter[category] = final_chance
										break
									end									
								end
							end
						end
					end
				end
				
				def[level] = initial_filters
			end

			local cached_filters = def[level]
			for slot, amt in pairs(inv.def.slots) do
				local slot_cache = cached_filters[slot]
				local total = table.calculate_total_pool(slot_cache)
				if total > 0 then
					for i=1,amt do
						local index = table.roll_pool(slot_cache, total)
						local item = rules:generate_instance("item", index, level)
						inv:add_item(item)
						--print("added", index)
					end
				else
					--print("skipped due to zero pool", slot, index)
				end
			end
		end
		
		return inv
	end,
}

return RULE_INVENTORIES