local showMessages = 0
local koefmag = 1
local koefweapon = 1
local koefarmor = 1

local function onDamaged(e)
	if e.source == 'attack' then
		if e.attackerReference == tes3.player then
			koefweapon = (((e.reference.object.level * 5) - e.attackerReference.object.level) / (e.reference.object.level + 20)) * (e.damage / 50)
			if koefweapon < 0 then
				koefweapon = 0
			end
		end
		if e.reference == tes3.player then
			koefarmor = (((e.attackerReference.object.level * 5) - e.reference.object.level) / (e.attackerReference.object.level + 20)) * (e.damage / 20)
			if koefarmor < 0 then
				koefarmor = 0
			end
		end
	end
end

local function onSpellCast(e)
	if e.caster == tes3.player then
		local spellcost = e.source.magickaCost
		koefmag = (spellcost * 5) / (spellcost + 80)
	end
end

local function onExerciseSkill(e)
	local magSkills = {
		[10] = true, -- destruction
		[11] = true, -- alteration
		[12] = true, -- illusion
		[13] = true, -- conjuration
		[14] = true, -- mysticism
		[15] = true, -- restoration
    }

	local weaponSkills = {
		[4] = true, -- blunt
		[5] = true, -- long blade
		[6] = true, -- axe
		[7] = true, -- spear
		[22] = true, -- short blade
		[23] = true, -- marksman
		[26] = true, -- hand to hand
    }
	
	local armorSkills = {
		[0] = true, -- block
		[2] = true, -- medium armor
		[3] = true, -- heavy armor
		[17] = true, -- unarmored
		[21] = true, -- light armor
    }

    if weaponSkills[e.skill] then
		if showMessages == 1 then
			tes3.messageBox({ message = " exp: " .. math.round(koefweapon, 2)})
		end
		e.progress = e.progress * koefweapon
    end

    if armorSkills[e.skill] then
		if showMessages == 1 then
			tes3.messageBox({ message = " exp: " .. math.round(koefarmor, 2)})
		end
		e.progress = e.progress * koefarmor
    end
	
    if magSkills[e.skill] then
		if showMessages == 1 then
			tes3.messageBox({ message = " exp: " .. math.round(koefmag, 2)})
		end
		e.progress = e.progress * koefmag
    end
end

local function initialized(e)
	event.register("damaged", onDamaged)
	event.register("spellCast", onSpellCast)
	event.register("exerciseSkill", onExerciseSkill)
end
event.register("initialized", initialized)