lootblocks = {}

local S = core.get_translator"lootblocks"

lootblocks.get_translator = S
lootblocks.get_modpath = core.get_modpath"lootblocks"

lootblocks.registered_drops = {}
lootblocks.registered_spawns = {}

function lootblocks.register_drop(stacks, chance)
	if type(stacks) == "string" then stacks = {stacks} end
    table.insert(lootblocks.registered_drops, {stacks=stacks, chance=chance})
end

function lootblocks.register_spawn(ents, chance)
	if type(ents) == "string" then ents = {ents} end
    table.insert(lootblocks.registered_spawns, {ents=ents, chance=chance})
end

lootblocks.defaultdrop = {"lootblocks:lootblock"}

function lootblocks.register_defaultdrop(stacks)
	if type(stacks) == "string" then stacks = {stacks} end
	lootblocks.defaultdrop = stacks
end

function lootblocks.gen_spawn(pos)
	local t = {}
	for _,v in ipairs(lootblocks.registered_spawns) do
		table.insert(t, {v.ents, math.random() * v.chance})
	end
	table.sort(t, function(a,b)
		return a[2] > b[2]
	end)
    local ents = t[1][1]
	if not ents then return end
    for _,w in ipairs(ents) do
        core.add_entity(pos, w)
    end
end

function lootblocks.gen_drop(pos)
	local t = {}
	for _,v in ipairs(lootblocks.registered_drops) do
		table.insert(t, {v.stacks, math.random() * v.chance})
	end
	table.sort(t, function(a,b)
		return a[2] > b[2]
	end)
    local stacks = t[1][1]
	if not stacks or #stacks <= 0 then stacks = lootblocks.defaultdrop end
    for _,w in ipairs(stacks) do
        core.add_item(pos, ItemStack(w))
    end
end

core.register_node("lootblocks:lootblock", {
    description = S"Lootblock",
    groups = {lootblock = 1},
    drawtype = "normal",
    tiles = {"lootblocks_lootblock.png"},
    on_punch = function(pos, node, puncher)
        core.set_node(pos, {name="air"})
		if math.random() < 0.01 and lootblocks.gen_spawn(pos) then
			return
		end
        lootblocks.gen_drop(pos)
		while math.random() > 0.95 do
			lootblocks.gen_drop(pos)
		end
    end,
})

core.register_node("lootblocks:lootblock_rare", {
    description = S"Rare Lootblock",
    groups = {lootblock = 1},
    drawtype = "normal",
    tiles = {"lootblocks_lootblock.png^[hsl:120"},
    on_punch = function(pos, node, puncher)
        core.set_node(pos, {name="air"})
        lootblocks.gen_drop(pos)
		if math.random() > 0.75 and lootblocks.gen_spawn(pos) then
			return
		end
		while math.random() > 0.5 do
			lootblocks.gen_drop(pos)
		end
    end,
	light_source = 4
})


core.register_node("lootblocks:lootblock_super", {
    description = S"Super Lootblock",
    groups = {lootblock = 1},
    drawtype = "normal",
    tiles = {"lootblocks_lootblock.png^[hsl:180"},
    on_punch = function(pos, node, puncher)
        core.set_node(pos, {name="air"})
		if math.random() < 0.01 and lootblocks.gen_spawn(pos) then
			return
		end
        lootblocks.gen_drop(pos)
        lootblocks.gen_drop(pos)
		while math.random() > 0.25 do
			lootblocks.gen_drop(pos)
		end
    end,
	light_source = 8
})

core.register_decoration({
	name = "lootblocks:lootblock",
	deco_type = "simple",
	place_on = {"mapgen_stone"},
	sidelen = 1,
	noise_params = {
		offset = -0.25,
		scale = 0.25,
		spread = {x = 16, y = 16, z = 16},
		seed = 230,
		octaves = 3,
		persist = 0.6
	},
	y_max = 30,
	y_min = -50,
	decoration = "lootblocks:lootblock",
})
core.register_decoration({
	name = "lootblocks:lootblock_rare",
	deco_type = "simple",
	place_on = {"mapgen_stone"},
	sidelen = 1,
	noise_params = {
		offset = -0.5,
		scale = 0.25,
		spread = {x = 16, y = 16, z = 16},
		seed = 230,
		octaves = 3,
		persist = 0.5
	},
	y_max = -50,
	y_min = -31000,
	decoration = "lootblocks:lootblock_rare",
})

core.register_ore({
	ore_type       = "scatter",
	ore            = "lootblocks:lootblock_rare",
	wherein        = "mapgen_stone",
	clust_scarcity = 24 * 24 * 24,
	clust_num_ores = 27,
	clust_size     = 1,
	y_max          = 0,
	y_min          = -100,
})

core.register_ore({
	ore_type       = "scatter",
	ore            = "lootblocks:lootblock_super",
	wherein        = "mapgen_stone",
	clust_scarcity = 32 * 32 * 32,
	clust_num_ores = 27,
	clust_size     = 1,
	y_max          = -20,
	y_min          = -100,
})

core.register_ore({
	ore_type       = "scatter",
	ore            = "lootblocks:lootblock_rare",
	wherein        = "mapgen_stone",
	clust_scarcity = 8 * 8 * 8,
	clust_num_ores = 27,
	clust_size     = 1,
	y_max          = -100,
	y_min          = -31000,
})
core.register_ore({
	ore_type       = "scatter",
	ore            = "lootblocks:lootblock_super",
	wherein        = "mapgen_stone",
	clust_scarcity = 16 * 16 * 16,
	clust_num_ores = 27,
	clust_size     = 1,
	y_max          = -100,
	y_min          = -31000,
})
lootblocks.register_drop("lootblocks:lootblock_rare", 0.1)
lootblocks.register_drop("lootblocks:lootblock_super", 0.01)