#textdomain wesnoth-ei

# Mal-Ravanal has this trait, and Dacyn gets it after he advances to Fallen Mage
# (it's completely useless for Dacyn, who has obscures, but it parallels his similarities with Mal-Ravanal)
#define TRAIT_AUDACIOUS
    [trait] # clone of fearless
        id=audacious
        male_name= _ "audacious"
        description= _ "Fights normally during unfavorable times of day/night"
        help_text= _ "Aversion to light and dark holds no sway over these audacious individuals."
        [effect]
            apply_to="fearless"
        [/effect]
    [/trait]
#enddef

#define TRAIT_DESERTER
    [trait]
        id=deserter
        male_name= _ "deserter"
        female_name= _ "female^deserter"
        description= _ "When this unit drops below half health, it will flee the battle and reappear on your recall list."
        # NOTE - implementation is handled using the events below, the macros are instantiated in _main.cfg
    [/trait]
#enddef

#define DESERTER_EVENT FLT UNIT
    first_time_only=no
    [{FLT}]
        side=1 # don't use this trait for the AI humans in S20
        trait=deserter
        formula=(self.hitpoints < self.max_hitpoints/2)
    [/{FLT}]
    [filter_condition]
        [have_unit]
            side=1
            id=Dacyn # prevent this from triggering at the very beginning of S11_Captured, when fighting bats (since you can't desert when you're already a prisoner)
        [/have_unit]        # also prevents this from triggering in S20, when the deserters are AI-controlled
    [/filter_condition]

    [fire_event]
        name=deserter deserts
        [primary_unit]
            id=${UNIT}.id
        [/primary_unit]
    [/fire_event]
#enddef

#define GLOBAL__TRAIT_DESERTER
    [event]
        name=attacker hits
        {DESERTER_EVENT filter_second second_unit}
    [/event]
    [event]
        name=defender hits
        {DESERTER_EVENT filter unit}
    [/event]

    [event]
        # This event is triggered from "attacker hits" or "defender hits", it's a separate event
        # so that the primary/secondary unit in combat can be treated as $unit in this code.
        name=deserter deserts
        first_time_only=no
        {RANDOM (1,2,3)}
        [switch]
            variable=random
            [case]
                value=1
                [message]
                    speaker=$unit.id
                    #po: The unit is about to desert
                    message=_ "Forget this!"
                [/message]
            [/case]
            [case]
                value=2
                [message]
                    speaker=$unit.id
                    message=_"Run for your lives!"
                [/message]
            [/case]
            [case]
                value=3
                [message]
                    speaker=$unit.id
                    #po: the speaker is male
                    message=_"Get me out of here!"
                    female_message=_"female^Get me out of here!"
                [/message]
            [/case]
        [/switch]
        {CLEAR_VARIABLE random}
        [put_to_recall_list]
            id=$unit.id
            heal=yes
        [/put_to_recall_list]
        [if]
            [have_unit]
                id=$unit.id
                gender=male
                search_recall_list=yes
            [/have_unit]
            [then]
                [message]
                    speaker=narrator
                    #po: unit is male
                    message= _ "The ‘deserter’ trait has caused $unit.name to flee back to your recall list."
                    image=wesnoth-icon.png
                [/message]
            [/then]
            [else]
                [message]
                    speaker=narrator
                    message= _ "female^The ‘deserter’ trait has caused $unit.name to flee back to your recall list."
                    image=wesnoth-icon.png
                [/message]
            [/else]
        [/if]
    [/event]
#enddef

#define TRAIT_MERCENARY
    [trait]
        id=mercenary
        male_name= _ "mercenary"
        female_name= _ "female^mercenary"
        description= _ "<span color='#00FF00'>+30%</span> base damage when gold is positive.
<span color='#FF0000'>-30%</span> base damage when gold is 0 or negative."
        [effect]
            apply_to=new_ability
            [abilities]
                [damage]
                    [filter]
                        [filter_side]
                            formula="gold > 0"
                        [/filter_side]
                    [/filter]
                    multiply=1.3
                [/damage]
                [damage]
                    [filter]
                        [filter_side]
                            formula="gold <= 0"
                        [/filter_side]
                    [/filter]
                    multiply=0.7
                [/damage]
            [/abilities]
        [/effect]
    [/trait]
#enddef

#define TRAIT_SURVIVOR
    [trait]
        id=survivor
        male_name= _ "survivor"
        female_name= _ "female^survivor"
        description= _ "<span color='#00FF00'>+1</span> damage to and <span color='#00FF00'>-1</span> damage from undead, necromancers, and bats.

Against all odds, this unit has endured the terrors and tortures of Mal-Ravanal’s prisons and lived to tell the tale. Survivors bear a heavy burden from their time in captivity, but have also learned much of the strengths and weaknesses of their former captors."

        # nice and simple, just give +1 damage vs undead
        [effect]
            apply_to=attack # all attacks
            [set_specials]
                mode=append
                [damage]
                    apply_to=self
                    [filter_opponent] # note that I also use this filter in the implementation for the Sentinel shield
                        trait=undead
                        [or]
                            type_adv_tree=Dark Adept,Vampire Bat
                        [/or]
                    [/filter_opponent]
                    add=1
                [/damage]
            [/set_specials]
        [/effect]
        # -1 damage for undead against self
        [effect]
            apply_to=new_ability
            [abilities]
                [damage]
                    id=survivor_reduce_damage # id prevents multiple survivor traits on different units from stacking
                    affect_enemies,affect_allies,affect_self=yes,no,no
                    [filter_opponent]
                        trait=survivor
                    [/filter_opponent]
                    [affect_adjacent]
                        adjacent=n,ne,se,s,sw,nw
                        [filter]
                            trait=undead
                            [or]
                                type_adv_tree=Dark Adept,Vampire Bat
                            [/or]
                        [/filter]
                    [/affect_adjacent]
                    sub=1
                [/damage]
            [/abilities]
        [/effect]
    [/trait]
#enddef
