local modname = minetest.get_current_modname()
local S = minetest.get_translator("eg_flowers")

local prototype = {
	drawtype = "plantlike",
	groups = {dig_immediate = 2, attached_node = 1},
	walkable = false,
	paramtype = "light",
	paramtype2 = "degrotate",
	floodable = true,
	node_placement_prediction = "",
	on_place = function(itemstack, placer, pointed_thing)
		--can only place on top of a node
		if pointed_thing.type == "node" then
			local under = vector.new(pointed_thing.above)
			under.y = under.y - 1
			local node_under = minetest.get_node(under)
			if minetest.get_item_group(node_under.name, "crumbly") > 0 then
				local param2 = math.random(90) - 1
				return minetest.item_place_node(itemstack, placer, pointed_thing, param2)
			end
		end
	end,
	after_place_node = function(pos, placer, itemstack)
		minetest.sound_play("plant_place", {pos = pos})
	end,
	sounds = {
		dig = "plant_dig",
		dug = "plant_dug",
		footstep = "plant_footstep",
		place = "plant_place"
	},
	selection_box = {
		type = "fixed",
		fixed = {-0.25, -0.5, -0.25, 0.25, 0.25, 0.25},
	},
}

local function register_plant(name, def)
	local pr = table.copy(prototype)
	for k, v in pairs(def) do
		pr[k] = v
	end
	minetest.register_node(name, pr)
end

register_plant(modname .. ":white", {
		description = S("White Flower"),
		tiles = {{name = "eg_flowers_white.png", backface_culling = false}},
	})
register_plant(modname .. ":red", {
		description = S("Red Flower"),
		tiles = {{name = "eg_flowers_red.png", backface_culling = false}},
	})
register_plant(modname .. ":purple", {
		description = S("Purple Flower"),
		tiles = {{name = "eg_flowers_purple.png", backface_culling = false}},
	})
register_plant(modname .. ":yellow", {
		description = S("Yellow Flower"),
		tiles = {{name = "eg_flowers_yellow.png", backface_culling = false}},
	})
register_plant(modname .. ":blue", {
		description = S("Blue Flower"),
		tiles = {{name = "eg_flowers_blue.png", backface_culling = false}},
	})
register_plant(modname .. ":orange", {
		description = S("Orange Flower"),
		tiles = {{name = "eg_flowers_orange.png", backface_culling = false}},
	})

register_plant(modname .. ":pink", {
		description = S("Pink Flower"),
		tiles = {{name = "eg_flowers_pink.png", backface_culling = false}},
	})

if minetest.get_modpath("eg_mapgen") then
	local function register_deco(name, seed)
		minetest.register_decoration({
			decoration = name,
			deco_type = "simple",
			place_on = "eg_mapgen:grass",
			sidelen = 8,
			noise_params = {
				offset = -0.25,
				scale = 0.2,
				spread = {x = 500, y = 500, z = 500},
				seed = seed,
				octaves = 3,
				persist = 0.7,
				lacunarity = 16.0,
			},
			y_min = -31000,
			y_max = 31000,
			param2 = 0,
			param2_max = 89,
		})
	end
	register_deco(modname .. ":white", 63554)
	register_deco(modname .. ":red", 23456)
	register_deco(modname .. ":purple", 134678)
	register_deco(modname .. ":yellow", 247684)
	register_deco(modname .. ":blue", 1358964)
	register_deco(modname .. ":orange", 178685)
	register_deco(modname .. ":pink", 95837592)
end
