require "Insurgent/InsurgentTraitDefinitions"

RoughLandingTrait = CreateInsurgentTrait()
RoughLandingTrait.name = "RoughLanding"
RoughLandingTrait.insurgentExclusive = true

function RoughLandingTrait.Create()
    return TraitFactory.addTrait("RoughLanding", getText("UI_trait_RoughLanding"), -8, getText("UI_trait_RoughLandingDesc"), false, false);
end

function RoughLandingTrait.OnNewGame(player)
    if not player:HasTrait("RoughLanding") then
        return
    end

    local bodydamage = player:getBodyDamage();
    local iterations = ZombRand(2) + 1;
    
    local i = 0
    while i < iterations do
        local damage = ZombRand(17, 22)
        local randompart = ZombRand(0, 16);
        local injuries = ZombRand(0, 5);
        local bodypart = bodydamage:getBodyPart(BodyPartType.FromIndex(randompart));
        
        local skip = bodypart:HasInjury();
        if skip then
            i = i - 1;
        else
            if injuries <= 1 then
                bodypart:AddDamage(damage);
                bodypart:setScratched(true, true);
            elseif injuries == 2 then
                bodypart:AddDamage(damage);
                bodypart:setScratched(true, true);
            elseif injuries == 3 then
                bodypart:AddDamage(damage);
                bodypart:setCut(true, true);
            elseif injuries >= 4 then
                bodypart:AddDamage(damage);
                bodypart:setDeepWounded(true);
            end
        end

        i = i + 1;
    end
    
    bodydamage:setInfected(false);
    bodydamage:setInfectionLevel(0);
    player:getBodyDamage():Update();
end

function RoughLandingTrait.RegisterEvents()
    Events.OnNewGame.Add(RoughLandingTrait.OnNewGame)
end