require "Insurgent/InsurgentTraitDefinitions"

NervesOfSteelTrait = CreateInsurgentTrait()
NervesOfSteelTrait.name = "NervesOfSteel"
NervesOfSteelTrait.insurgentExclusive = true
NervesOfSteelTrait.mutualExclusions = {"CombatAnxiety", "Emotionless"}

function NervesOfSteelTrait.Create()
    return TraitFactory.addTrait("NervesOfSteel", getText("UI_trait_NervesOfSteel"), 2, getText("UI_trait_NervesOfSteelDesc"), false, false);
end

function NervesOfSteelTrait.EveryTenMinutes()
    local players = IsoPlayer.getPlayers()
	for i=0,players:size()-1 do
		local player = players:get(i)
		if player and player:isLocalPlayer() and not player:isDead() and player:HasTrait("NervesOfSteel") then
            NervesOfSteelTrait.Tick(player)
        end
    end
end

function NervesOfSteelTrait.Tick(player)
    local stats = player:getStats();
    local baseStresser = 0.001;
    local sadMulti = ((player:getBodyDamage():getUnhappynessLevel() / 100) * 2) + 1;
    local panicMulti = (stats:getPanic() * 3) + 1;

    if stats:getDrunkenness() > 0 or stats:getFatigue() < 0.15 then
         baseStresser = 0 
    end

    stats:setStress(stats:getStress() - (baseStresser * sadMulti * panicMulti));
end

function NervesOfSteelTrait.RegisterEvents()
    Events.EveryTenMinutes.Add(NervesOfSteelTrait.EveryTenMinutes)
end