local modpath = minetest.get_modpath("ip_destruction_counter")

destruction_counter = {}
destruction_counter.nodesDestroyed = 0
local nodesDestroyedByHand = 0

local idText
local idMeter
minetest.register_on_joinplayer(function(player)
	meta = player:get_meta()
	

	
	
	player:hud_add({
		hud_elem_type = "image",
		position = {x = .8, y = .85},
		offset = {x = -830, y = 23},
		text = "destruction_counter_meter_empty.png",
		scale = {x = 4, y = 4},
		alignment = {x = 1, y = 0},
	})

	idMeter = player:hud_add({
		hud_elem_type = "image",
		position = {x = .8, y = .85},
		offset = {x = -830, y = 23},
		text = "destruction_counter_meter_full.png",
		scale = {x = 0, y = 4},
		alignment = {x = 1, y = 0},
	})
	
	idText = player:hud_add({
		hud_elem_type = "text",
		position = {x = 0.3, y = 0.7},
		scale = {x = 0.3, y = 0.5},
		text = "Destruct-o-meter",
		number = 0xff3c0a,
		alignment = {x = 1},
		offset = {x = 200, y = 46},
	})
end)

minetest.register_on_dignode(function(pos, oldnode, digger)
	nodesDestroyedByHand = nodesDestroyedByHand + 1
	destruction_counter.updateCounter(digger)
end)

function destruction_counter.updateCounter(player)
	if not player then
		return
	end
	
	local totalDestruction = destruction_counter.nodesDestroyed + math.floor(nodesDestroyedByHand / 10)
	local percentage = (totalDestruction / 1000 * 4)
	if percentage > 100 then
		percentage = 100
	end
	player:hud_change(idText, "text", "Destruction meter: " .. totalDestruction)
	player:hud_change(idMeter, "scale", {x = percentage, y = 4})
end

