require "Insurgent/InsurgentTraitDefinitions"

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

function CombatAnxietyTrait.Create()
    return TraitFactory.addTrait("CombatAnxiety", getText("UI_trait_CombatAnxiety"), -4, getText("UI_trait_CombatAnxietyDesc"), false, false);
end

function CombatAnxietyTrait.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("CombatAnxiety") then
            CombatAnxietyTrait.AnxietyTick(player)
        end
    end
end

function CombatAnxietyTrait.AnxietyTick(player)
    local stats = player:getStats();
    local zombieStress = 0;
    local baseStresser = 0.02;
    local sadMulti = ((player:getBodyDamage():getUnhappynessLevel() / 100) * 2) + 1;
    local panicMulti = (stats:getPanic() * 3) + 1;
    
    local zeds = player:getSpottedList();
    if zeds:size() > 0 then
        for i = 0, zeds:size() - 1 do
            if zeds:get(i):isZombie() then
                local stress = 0.005;
                if zeds:get(i):DistTo(player) <= 3 then
                    stress = 0.008;
                end
                zombieStress = zombieStress + stress;
            end
        end
    end
    if stats:getDrunkenness() > 0 or stats:getFatigue() < 0.15 then 
        baseStresser = 0 
    end

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


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