#textdomain wesnoth-utbs

#define KALEH_DESCRIPTION
_"Kaleh is still a young elf of the Quenoth, on the verge of adulthood. He trained with his father with the bow and sword, joining him in lighter expeditions and raids. When his father was lost, Kaleh became more serious and solemn than is usual for those who walk under the two suns. He turned inwards for answers and guidance, not knowing how much his people would look towards him for guidance in times to come."#enddef

#define NYM_DESCRIPTION
_"Nym is Kaleh’s childhood friend, a young, rebellious lady with quick wits and strong will. Her high spirits are a great boon in dark times."#enddef

#define QUENOTH_YOUTH_DESCRIPTION
    {KALEH_DESCRIPTION}+"

"+{NYM_DESCRIPTION}#enddef

#define HEAL_EFFECTS
    # Make the XP bar blue instead of purple.
    major_amla=yes
    [effect]
        apply_to=hitpoints
        heal_full=yes
    [/effect]
    [effect]
        apply_to=status
        remove=poisoned
    [/effect]
    [effect]
        apply_to=status
        remove=slowed
    [/effect]
    [effect]
        apply_to=status
        remove=dazed
    [/effect]
#enddef

#define XP_INCREASE_EFFECT
    [effect]
        apply_to=max_experience
        increase=20%
    [/effect]
#enddef

#define QUENOTH_YOUTH_ADVANCEMENTS EXTRA_EFFECTS
    [advancement]
        id=warrior_1
        description= _ "Warrior"
        image=attacks/sword-elven.png
        [effect]
            [filter]
                type=Quenoth Youth
            [/filter]
            apply_to=type
            name=Quenoth Youth 2
        [/effect]
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=attack
            name=sword
            increase_damage=2
        [/effect]
        [effect]
            apply_to=attack
            name=bow
            increase_damage=2
            increase_attacks=1
        [/effect]
    [/advancement]
    [advancement]
        id=hunter_1
        description= _ "Hunter"
        image=attacks/bolas.png
        [effect]
            [filter]
                type=Quenoth Youth
            [/filter]
            apply_to=type
            name=Quenoth Youth 2
        [/effect]
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=new_attack
            name=bolas
            description= _"bolas"
            type=impact
            range=ranged
            damage=10
            number=2
            icon=attacks/bolas.png
            [specials]
                {WEAPON_SPECIAL_SLOW}
            [/specials]
        [/effect]
        [effect]
            apply_to=movement
            increase=1
        [/effect]
    [/advancement]
    [advancement]
        id=leader_1
        description= _ "Leader"
        image=attacks/leader.png
        [effect]
            [filter]
                type=Quenoth Youth
            [/filter]
            apply_to=type
            name=Quenoth Youth 2
        [/effect]
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=attack
            name=sword
            increase_damage=1
        [/effect]
        [effect]
            apply_to=attack
            name=bow
            increase_damage=1
        [/effect]
        [effect]
            apply_to=new_ability
            [abilities]
                {ABILITY_LEADERSHIP}
            [/abilities]
        [/effect]
    [/advancement]

    [advancement]
        id=warrior_2_1
        require_amla=warrior_1
        description= _ "Warrior (choose only one): specialize in melee combat"
        image=attacks/sword-elven.png
        exclude_amla=warrior_2_1,warrior_2_2
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=attack
            name=sword
            increase_damage=2
            increase_attacks=1
        [/effect]
    [/advancement]
    [advancement]
        id=warrior_2_2
        require_amla=warrior_1
        description= _ "Warrior (choose only one): specialize in archery"
        image=attacks/sword-elven.png
        exclude_amla=warrior_2_1,warrior_2_2
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=attack
            name=bow
            increase_damage=3
            [set_specials]
                mode=append
                {WEAPON_SPECIAL_MARKSMAN}
            [/set_specials]
        [/effect]
    [/advancement]
    [advancement]
        id=hunter_2_1
        require_amla=hunter_1
        description= _ "Hunter (choose only one): specialize in surprise attacks"
        image=attacks/bolas.png
        exclude_amla=hunter_2_1,hunter_2_2
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=attack
            name=sword
            [set_specials]
                mode=append
                {WEAPON_SPECIAL_BACKSTAB}
            [/set_specials]
        [/effect]
        [effect]
            apply_to=attack
            name=bolas
            increase_damage=3
            increase_attacks=1
        [/effect]
    [/advancement]
    [advancement]
        id=hunter_2_2
        require_amla=hunter_1
        description= _ "Hunter (choose only one): specialize in skirmishing"
        image=attacks/bolas.png
        exclude_amla=hunter_2_1,hunter_2_2
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=new_ability
            [abilities]
                {ABILITY_SKIRMISHER}
            [/abilities]
        [/effect]
        [effect]
            apply_to=attack
            name=bolas
            increase_damage=3
            increase_attacks=1
        [/effect]
    [/advancement]
    [advancement]
        id=leader_2_1
        require_amla=leader_1
        description= _ "Leader (choose only one): specialize in supporting allies"
        image=attacks/leader.png
        exclude_amla=leader_2_1,leader_2_2
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=new_ability
            [abilities]
                {ABILITY_SUPPORT}
            [/abilities]
        [/effect]
        [effect]
            apply_to=attack
            name=sword
            increase_damage=1
        [/effect]
        [effect]
            apply_to=attack
            name=bow
            increase_damage=1
        [/effect]
    [/advancement]
    [advancement]
        id=leader_2_2
        require_amla=leader_1
        description= _ "Leader (choose only one): specialize in distracting enemies"
        image=attacks/leader.png
        exclude_amla=leader_2_1,leader_2_2
        [effect]
            [filter]
                type=Quenoth Youth 2
            [/filter]
            apply_to=type
            name=Quenoth Youth 3
        [/effect]
        {HEAL_EFFECTS}
        {EXTRA_EFFECTS}
        [effect]
            apply_to=new_ability
            [abilities]
                {ABILITY_DISTRACT}
            [/abilities]
        [/effect]
        [effect]
            apply_to=attack
            name=sword
            increase_damage=1
        [/effect]
        [effect]
            apply_to=attack
            name=bow
            increase_damage=1
        [/effect]
    [/advancement]
#enddef

#define KALEH_ANIMATIONS
    {LEADING_ANIM "units/quenoth/kaleh-lead2.png" "units/quenoth/kaleh-lead1.png" (12,-32)}
    {DEFENSE_ANIM_RANGE "units/quenoth/kaleh-defend-2.png" "units/quenoth/kaleh-defend-1.png" {SOUND_LIST:ELF_HIT} melee}
    {DEFENSE_ANIM_FILTERED "units/quenoth/kaleh-bow-defend-2.png" "units/quenoth/kaleh-bow-defend-1.png" {SOUND_LIST:ELF_HIT} (
        [filter_second_attack]
            name=bow
        [/filter_second_attack]
    )}
    {DEFENSE_ANIM_FILTERED "units/quenoth/kaleh-defend-2.png" "units/quenoth/kaleh-defend-1.png" {SOUND_LIST:ELF_HIT} (
        [filter_second_attack]
            name=bolas
        [/filter_second_attack]
    )}
    [attack_anim]
        [filter_attack]
            name=bow
        [/filter_attack]
        missile_start_time=-100
        [missile_frame]
            duration=100
            image=projectiles/missile-n.png
            image_diagonal=projectiles/missile-ne.png
        [/missile_frame]
        start_time=-380
        [frame]
            image=units/quenoth/kaleh-bow.png:470
        [/frame]
        {SOUND:HIT_AND_MISS bow.ogg bow-miss.ogg -380}
    [/attack_anim]
    [attack_anim]
        [filter_attack]
            name=sword
        [/filter_attack]
        start_time=-420
        offset=0.0:210,0.0~0.5:210,0.5~0.0:250,0.0:1
        [frame]
            image="units/quenoth/kaleh-sword[1,2,3,4,5,6,7].png:[80,80,80,80,100,125,125],units/quenoth/kaleh.png:1"
        [/frame]
        {SOUND:HIT_AND_MISS {SOUND_LIST:SWORD_SWISH} {SOUND_LIST:MISS} -100}
    [/attack_anim]
    [attack_anim]
        [filter_attack]
            name=bolas
        [/filter_attack]
        missile_start_time=-150
        [missile_frame]
            duration=150
            image=projectiles/bolas-n.png
            image_diagonal=projectiles/bolas-ne.png
        [/missile_frame]
        start_time=-400
        [frame]
            image=units/quenoth/kaleh-bolas[1,2,3,4].png:[70,80,100,200]
        [/frame]
        {SOUND:HIT_AND_MISS hatchet.wav hatchet-miss.wav -300}
    [/attack_anim]
#enddef

#define NYM_ANIMATIONS
    {LEADING_ANIM "units/quenoth/nym-lead2.png" "units/quenoth/nym-lead1.png" (12,-32)}
    {DEFENSE_ANIM_RANGE "units/quenoth/nym-defend-2.png" "units/quenoth/nym-defend-1.png" {SOUND_LIST:ELF_FEMALE_HIT} melee}
    {DEFENSE_ANIM_FILTERED "units/quenoth/nym-bow-defend-2.png" "units/quenoth/nym-bow-defend-1.png" {SOUND_LIST:ELF_FEMALE_HIT} (
        [filter_second_attack]
            name=bow
        [/filter_second_attack]
    )}
    {DEFENSE_ANIM_FILTERED "units/quenoth/nym-defend-2.png" "units/quenoth/nym-defend-1.png" {SOUND_LIST:ELF_FEMALE_HIT} (
        [filter_second_attack]
            name=bolas
        [/filter_second_attack]
    )}
    [attack_anim]
        [filter_attack]
            name=bow
        [/filter_attack]
        missile_start_time=-100
        [missile_frame]
            duration=100
            image=projectiles/missile-n.png
            image_diagonal=projectiles/missile-ne.png
        [/missile_frame]
        start_time=-380
        [frame]
            image=units/quenoth/nym-bow.png:470
        [/frame]
        {SOUND:HIT_AND_MISS bow.ogg bow-miss.ogg -380}
    [/attack_anim]
    [attack_anim]
        [filter_attack]
            name=sword
        [/filter_attack]
        start_time=-420
        offset=0.0:210,0.0~0.5:210,0.5~0.0:250,0.0:1
        [frame]
            image="units/quenoth/nym-sword[1,2,3,4,5,6,7].png:[80,80,80,80,100,125,125],units/quenoth/nym.png:1"
        [/frame]
        {SOUND:HIT_AND_MISS {SOUND_LIST:SWORD_SWISH} {SOUND_LIST:MISS} -100}
    [/attack_anim]
    [attack_anim]
        [filter_attack]
            name=bolas
        [/filter_attack]
        missile_start_time=-150
        [missile_frame]
            duration=150
            image=projectiles/bolas-n.png
            image_diagonal=projectiles/bolas-ne.png
        [/missile_frame]
        start_time=-400
        [frame]
            image=units/quenoth/nym-bolas[1,2,3,4].png:[70,80,100,200]
        [/frame]
        {SOUND:HIT_AND_MISS hatchet.wav hatchet-miss.wav -300}
    [/attack_anim]
#enddef

[unit_type]
    id=Quenoth Youth
    race=quenoth
    ignore_race_traits=yes
    gender=male,female
    name= _ "Quenoth Youth"
    image=units/quenoth/kaleh.png
    movement_type=quenoth_foot
    movement=6
    hitpoints=32
    experience=40
    level=1
    profile=portraits/kaleh.png
    alignment=lawful
    advances_to=null
    cost=14
    usage=fighter
    description={QUENOTH_YOUTH_DESCRIPTION}
    die_sound={SOUND_LIST:ELF_HIT}
    [attack]
        name=sword
        #textdomain wesnoth-units
        description= _"sword"
        type=blade
        range=melee
        damage=5
        number=4
        icon=attacks/sword-elven.png
    [/attack]
    [attack]
        name=bow
        description= _"bow"
        #textdomain wesnoth-utbs
        type=pierce
        range=ranged
        damage=4
        number=3
        icon=attacks/bow-elven.png
    [/attack]
    {KALEH_ANIMATIONS}
    [male]
        description={KALEH_DESCRIPTION}
    [/male]
    [female]
        gender=female
        name= _ "female^Quenoth Youth"
        image="units/quenoth/nym.png"
        profile=portraits/nym.png
        die_sound={SOUND_LIST:ELF_FEMALE_HIT}
        description={NYM_DESCRIPTION}
        {NYM_ANIMATIONS}
    [/female]
    {QUENOTH_YOUTH_ADVANCEMENTS ()}
[/unit_type]

[unit_type]
    id=Quenoth Youth 2
    race=quenoth
    ignore_race_traits=yes
    gender=male,female
    name= _ "Quenoth Youth"
    image=units/quenoth/kaleh.png
    movement_type=quenoth_foot
    movement=6
    hitpoints=40
    experience=90
    level=2
    profile=portraits/kaleh.png
    alignment=lawful
    advances_to=null
    cost=14
    usage=fighter
    description={QUENOTH_YOUTH_DESCRIPTION}
    die_sound={SOUND_LIST:ELF_HIT}
    hide_help=yes
    do_not_list=yes
    [attack]
        name=sword
        #textdomain wesnoth-units
        description= _"sword"
        type=blade
        range=melee
        damage=5
        number=4
        icon=attacks/sword-elven.png
    [/attack]
    [attack]
        name=bow
        description= _"bow"
        #textdomain wesnoth-utbs
        type=pierce
        range=ranged
        damage=4
        number=3
        icon=attacks/bow-elven.png
    [/attack]
    {KALEH_ANIMATIONS}
    [male]
        description={KALEH_DESCRIPTION}
    [/male]
    [female]
        gender=female
        name= _ "female^Quenoth Youth"
        image="units/quenoth/nym.png"
        profile=portraits/nym.png
        die_sound={SOUND_LIST:ELF_FEMALE_HIT}
        description={NYM_DESCRIPTION}
        {NYM_ANIMATIONS}
    [/female]
    {QUENOTH_YOUTH_ADVANCEMENTS ()}
[/unit_type]

[unit_type]
    id=Quenoth Youth 3
    race=quenoth
    ignore_race_traits=yes
    gender=male,female
    name= _ "Quenoth Youth"
    image=units/quenoth/kaleh.png
    movement_type=quenoth_foot
    movement=6
    hitpoints=52
    experience=150
    level=3
    profile=portraits/kaleh.png
    alignment=lawful
    advances_to=null
    cost=14
    usage=fighter
    description={QUENOTH_YOUTH_DESCRIPTION}
    die_sound={SOUND_LIST:ELF_HIT}
    hide_help=yes
    do_not_list=yes
    [attack]
        name=sword
        #textdomain wesnoth-units
        description= _"sword"
        type=blade
        range=melee
        damage=5
        number=4
        icon=attacks/sword-elven.png
    [/attack]
    [attack]
        name=bow
        description= _"bow"
        #textdomain wesnoth-utbs
        type=pierce
        range=ranged
        damage=4
        number=3
        icon=attacks/bow-elven.png
    [/attack]
    {KALEH_ANIMATIONS}
    [male]
        description={KALEH_DESCRIPTION}
    [/male]
    [female]
        gender=female
        name= _ "female^Quenoth Youth"
        image="units/quenoth/nym.png"
        profile=portraits/nym.png
        die_sound={SOUND_LIST:ELF_FEMALE_HIT}
        description={NYM_DESCRIPTION}
        {NYM_ANIMATIONS}
    [/female]
    {QUENOTH_YOUTH_ADVANCEMENTS {XP_INCREASE_EFFECT}}
    {AMLA_DEFAULT}
[/unit_type]


#textdomain wesnoth-utbs
[unit_type]
    [base_unit]
        id=Ghost
    [/base_unit]
    id=EGhost
    name= _ "Ethereal Ghost"
    [movement_costs]
        impassable=1
    [/movement_costs]
    [defense]
        impassable=50
    [/defense]
    experience=30
    advances_to=EWraith,EShadow
    description= _ "It is one of the greater mercies of creation that a human soul is immutable, and cannot be destroyed. However, the many things a necromancer can do, despite this, are entirely horrifying.

Trapped within a shroud of vile enchantments, a spirit likens unto the wind in the sails of a ship. The contrivance that results from this prison is an unfailing servant, which can be bound to whatever task their master sees fit."
    {NOTE_ETHEREAL}
[/unit_type]


#textdomain wesnoth-utbs
[unit_type]
    [base_unit]
        id=Nightgaunt
    [/base_unit]
    id=ENightgaunt
    name= _ "Ethereal Nightgaunt"
    [movement_costs]
        impassable=1
    [/movement_costs]
    [defense]
        impassable=50
    [/defense]
    [abilities]
        # Remove inherited nightstalk
        [hides]
            __remove=yes
        [/hides]
    [/abilities]
    description= _ "The purpose of the masks that these creatures wear is unknown, as is the countenance that they obscure. These terrible forms are rarely seen by the living, and those who live to speak of them had no leisure to study their foe."
    {NOTE_ETHEREAL}
    [special_note]
        note=_"SPECIAL_NOTE^Unlike normal Nightgaunts, Ethereal Nightgaunts cannot hide at night."
    [/special_note]
[/unit_type]


#textdomain wesnoth-utbs
[unit_type]
    [base_unit]
        id=Shadow
    [/base_unit]
    id=EShadow
    name= _ "Ethereal Shadow"
    [movement_costs]
        impassable=1
    [/movement_costs]
    [defense]
        impassable=50
    [/defense]
    [abilities]
        # Remove inherited nightstalk
        [hides]
            __remove=yes
        [/hides]
    [/abilities]
    advances_to=ENightgaunt
    description= _ "When light came into the world and gave form to the unknown, fear was forced to retreat into darkness. Since that day, the shadows of the world have held terror for humanity, though it knows not why.

That is a question which is easily answered by a necromancer."
    {NOTE_ETHEREAL}
    [special_note]
        note=_"SPECIAL_NOTE^Unlike normal Shadows, Ethereal Shadows cannot hide at night."
    [/special_note]
[/unit_type]


#textdomain wesnoth-utbs
[unit_type]
    [base_unit]
        id=Spectre
    [/base_unit]
    id=ESpectre
    name= _ "Ethereal Spectre"
    [movement_costs]
        impassable=1
    [/movement_costs]
    [defense]
        impassable=50
    [/defense]
    description= _ "Sometimes called the ‘hollow men’, spectres form the right arm of their master’s power. These abominations are a rightful terror to the living, and keep a sleepless vigil over their master’s domain.

The creation of these is no mean feat; the real danger in encountering one is that it is likely only the harbinger of a much more dangerous force that will follow in its wake."
    {NOTE_ETHEREAL}
[/unit_type]


#textdomain wesnoth-utbs
[unit_type]
    [base_unit]
        id=Wraith
    [/base_unit]
    id=EWraith
    name= _ "Ethereal Wraith"
    [movement_costs]
        impassable=1
    [/movement_costs]
    [defense]
        impassable=50
    [/defense]
    advances_to=ESpectre
    description= _ "These tortured forms of what were once warriors are among the most terrifying things a necromancer can create, for a sword will cleave right through them, as through air itself. What inspires such fear is the thought that these beasts are invincible, a belief that is actually far from the truth.

Because of this, a means was made by which the enchantments that drive these creatures could renew themselves through the very thing which threatened them."
    {NOTE_ETHEREAL}
[/unit_type]


#textdomain wesnoth-utbs
# same as a ghost, except that a haunt can't move
[unit_type]
    [base_unit]
        id=Wraith
    [/base_unit]
    id=Haunt
    name= _ "Haunt"
    [movement_costs]
        cave={UNREACHABLE}
        flat={UNREACHABLE}
        shallow_water={UNREACHABLE}
        deep_water={UNREACHABLE}
    [/movement_costs]
    experience=30
    description= _ "Haunts are the tortured souls of those who have died suddenly and never been laid to rest. Paralyzed by the shock of their death, they are cursed to forever haunt the place of their death and relive their final moments over and over and over."
[/unit_type]


#textdomain wesnoth-utbs
[unit_type]
    id=Spider Lich
    name= _ "Spider Lich"
    race=undead
    image="units/undead/spider-lich.png"
    {DEFENSE_ANIM "units/undead/spider-lich-defend.png" "units/undead/spider-lich.png" {SOUND_LIST:LICH_HIT} }
    hitpoints=52
    movement_type=undeadfoot
    movement=6
    experience=100
    {AMLA_DEFAULT}
    level=2
    alignment=chaotic
    advances_to=null
    cost=62
    usage=fighter
    description= _ "No one is quite sure how spider liches are created, but they are a horrific sight to behold. Crawling around on huge skeletal legs, and wielding huge magical staves, these abominations are fearsome undead foes. "
    die_sound=lich-die.ogg
    [attack]
        name=touch
        #textdomain wesnoth-units
        description= _"touch"
        type=cold
        range=melee
        damage=5
        number=3
        icon=attacks/touch-undead.png
        [specials]
            {WEAPON_SPECIAL_DRAIN}
        [/specials]
    [/attack]
    [attack]
        name=chill wave
        description= _"chill wave"
        type=cold
        [specials]
            {WEAPON_SPECIAL_MAGICAL}
        [/specials]
        range=ranged
        damage=9
        number=3
        icon=attacks/iceball.png
    [/attack]
    [attack]
        name=shadow wave
        description=_"shadow wave"
        #textdomain wesnoth-utbs
        type=arcane
        [specials]
            {WEAPON_SPECIAL_MAGICAL}
        [/specials]
        range=ranged
        damage=6
        number=3
        icon=attacks/dark-missile.png
    [/attack]
    [attack_anim]
        [filter_attack]
            name=chill wave
        [/filter_attack]
        offset=0
        {MISSILE_FRAME_CHILL_TEMPEST 0 -15}
        start_time=-355
        [frame]
            image="units/undead/spider-lich-magic-[1,2].png:75"
        [/frame]
        [frame]
            image="units/undead/spider-lich-magic-3.png"
            halo=halo/undead/black-magic-[1~5].png:[75*4,50]
        [/frame]
        {SOUND:HIT_AND_MISS magic-dark-big.ogg magic-dark-big-miss.ogg -150}
        [frame]
            image="units/undead/spider-lich-magic-[2,1].png:50"
        [/frame]
    [/attack_anim]
    [attack_anim]
        [filter_attack]
            name=shadow wave
        [/filter_attack]
        {MISSILE_FRAME_SHADOW_WAVE}
        start_time=-675
        [frame]
            image="units/undead/spider-lich-magic-[1,2].png:75"
        [/frame]
        [frame]
            image="units/undead/spider-lich-magic-3.png"
            halo=halo/undead/black-magic-[1~5].png:[75*4,50]
        [/frame]
        [frame]
            image="units/undead/spider-lich-magic-3.png:200"
        [/frame]
        {SOUND:HIT_AND_MISS magic-dark-big.ogg magic-dark-big-miss.ogg -150}
        [frame]
            image="units/undead/spider-lich-magic-[2,1].png:50"
        [/frame]
        [frame]
            image="units/undead/spider-lich.png:50"
        [/frame]
    [/attack_anim]
    [attack_anim]
        [filter_attack]
            name=touch
        [/filter_attack]
        start_time=-250
        [frame]
            image="units/undead/spider-lich.png:50"
        [/frame]
        [frame]
            image="units/undead/spider-lich-melee-[1~2].png:[100,200]"
        [/frame]
        {SOUND:HIT_AND_MISS wail-sml.wav {SOUND_LIST:MISS} -100}
        [frame]
            image="units/undead/spider-lich-melee-1.png:75"
        [/frame]
        [frame]
            image="units/undead/spider-lich.png:75"
        [/frame]
    [/attack_anim]
[/unit_type]


#textdomain wesnoth-utbs

[unit_type]
    id=Skeleton Rider
    name= _ "Skeleton Rider"
    race=undead
    image="units/undead-skeletal/rider.png"
    {DEFENSE_ANIM "units/undead-skeletal/rider-defend.png" "units/undead-skeletal/rider.png" {SOUND_LIST:SKELETON_HIT} }
    [movement_anim]
        start_time=0
        [frame]
            image="units/undead-skeletal/rider-moving.png:150"
        [/frame]
    [/movement_anim]
    hitpoints=36
    movement_type=mounted
    movement=7
    # a cross between the mounted and undeadfoot resistances
    [resistance]
        blade=80
        pierce=70
        impact=110
        fire=120
        cold=40
        arcane=150
    [/resistance]
    experience=50
    level=1
    alignment=chaotic
    advances_to=null
    {AMLA_DEFAULT}
    cost=19
    usage=scout
    description= _ "Once great warriors across the plains, these mounted riders atop their skeletal horses were raised from the sands by unholy magic to spread fear and destruction."
    die_sound={SOUND_LIST:SKELETON_DIE}
    [attack]
        name=axe
        #textdomain wesnoth-units
        description= _"axe"
        #textdomain wesnoth-utbs
        type=blade
        range=melee
        damage=6
        number=3
    [/attack]
    [attack_anim]
        [filter_attack]
            name=axe
        [/filter_attack]

        start_time=-250
        horse_sound_start_time=-250

        [frame]
            image="units/undead-skeletal/rider-moving.png:150"
        [/frame]
        [frame]
            image="units/undead-skeletal/rider-attack.png:200"
        [/frame]
        [frame]
            image="units/undead-skeletal/rider-moving.png:50"
        [/frame]

        [horse_sound_frame]
            sound=horse-canter.wav
        [/horse_sound_frame]

        {SOUND:HIT_AND_MISS axe.ogg {SOUND_LIST:MISS} -50}
    [/attack_anim]
[/unit_type]


#textdomain wesnoth-help

# These are all direct clones of the mainline healing abilities, except that
# they are set to exclude units suffering from dehydration. Dehydration is only
# delayed by healers, so they must not actually heal a dehydrated unit any
# hitpoints.
#
# The ability macros go into wesnoth textdomain since the strings are identical
# to their core counterparts, but the special notes belong to wesnoth-utbs.

#define UTBS_ABILITY_HEALS
    [heals]
        value=4
        id=healing
        affect_allies=yes
        name= _ "heals +4"
        female_name= _ "female^heals +4"
        description=  _ "Allows the unit to heal adjacent allied units at the beginning of our turn.

A unit cared for by this healer may heal up to 4 HP per turn, or stop poison from taking effect for that turn.
A poisoned unit cannot be cured of its poison by a healer, and must seek the care of a village or a unit that can cure."
        special_note=_"This unit is capable of basic healing and slowing dehydration."
        affect_self=no
        poison=slowed
        [affect_adjacent]
            [filter]
                [not]
                    status=dehydration_slowed_by_healer
                [/not]
            [/filter]
        [/affect_adjacent]
    [/heals]
#enddef

#define UTBS_ABILITY_EXTRA_HEAL_NO_NOTES
    [heals]
        value=8
        id=healing
        affect_allies=yes
        name= _ "heals +8"
        female_name= _ "female^heals +8"
        description= _ "This unit combines herbal remedies with magic to heal units more quickly than is normally possible on the battlefield.

A unit cared for by this healer may heal up to 8 HP per turn, or stop poison from taking effect for that turn.
A poisoned unit cannot be cured of its poison by a healer, and must seek the care of a village or a unit that can cure."
        affect_self=no
        poison=slowed
        [affect_adjacent]
            [filter]
                [not]
                    status=dehydration_slowed_by_healer
                [/not]
            [/filter]
        [/affect_adjacent]
    [/heals]
#enddef

#define UTBS_ABILITY_EXTRA_HEAL
    {UTBS_ABILITY_EXTRA_HEAL_NO_NOTES}
    [+heals]
        special_note=_"This unit is capable of basic healing and slowing dehydration."
    [/heals]
#enddef

#define UTBS_ABILITY_SUPER_HEAL_NO_NOTES
    [heals]
        value=12
        id=healing
        affect_allies=yes
        name= _ "heals +12"
        female_name= _ "female^heals +12"
        description= _ "This unit combines herbal remedies with magic to heal units more quickly than is normally possible on the battlefield.

A unit cared for by this healer may heal up to 12 HP per turn, or stop poison from taking effect for that turn.
A poisoned unit cannot be cured of its poison by a healer, and must seek the care of a village or a unit that can cure."
        affect_self=no
        poison=slowed
        [affect_adjacent]
            [filter]
                [not]
                    status=dehydration_slowed_by_healer
                [/not]
            [/filter]
        [/affect_adjacent]
    [/heals]
#enddef

#define UTBS_ABILITY_SUPER_HEAL
    {UTBS_ABILITY_SUPER_HEAL_NO_NOTES}
    [+heals]
        special_note=_"This unit is capable of basic healing and slowing dehydration."
    [/heals]
#enddef

#define UTBS_NOTE_CURES
    [+heals]
        special_note=_"This unit is capable of healing those around it, slowing dehydration, and curing them of poison."
    [/heals]
#enddef

#define UTBS_ABILITY_CURES
    {INTERNAL:ABILITY_UNPOISON_NO_NOTES}
    {UTBS_ABILITY_EXTRA_HEAL}
    {UTBS_NOTE_CURES}
#enddef

#define UTBS_ABILITY_SUPER_CURES
    {INTERNAL:ABILITY_UNPOISON_NO_NOTES}
    {UTBS_ABILITY_SUPER_HEAL}
    {UTBS_NOTE_CURES}
#enddef

#textdomain wesnoth-utbs

#define NOTE_ETHEREAL
    [special_note]
        note=_"SPECIAL_NOTE^This unit is able to move through solid stone walls."
    [/special_note]
#enddef

# This is the Teaching ability owned by Garak

#define ABILITY_TEACHING
    [dummy]
        id=teaching
        name= _ "teaching"
        female_name= _ "female^teaching"
        description= _ "At the start of every turn, this unit redistributes its experience points to all the units of the same side adjacent to it. If no suitable unit is adjacent, its experience just goes back to zero."
    [/dummy]
#enddef

#define WEAPON_SPECIAL_SHOCK
    # Canned definition of the Shock ability to be included in a
    # [specials] clause.
    [attacks]
        id=shock
        name= _ "shock"
        description= _ "When this attack is used on offense, the opponent will retaliate with one less strike than normally, to a minimum of one strike."
        special_note=_"This unit’s melee attack can overwhelm the defenses of enemies, preventing them from retaliating as effectively."
        sub=1
        active_on=offense
        apply_to=opponent
        [filter_base_value]
            greater_than=1
        [/filter_base_value]
    [/attacks]
#enddef

#define FORMATION ID GREATER_THAN COUNT
    [chance_to_hit]
        id={ID}
        apply_to=opponent
        sub=10
        [filter_base_value]
            greater_than_equal_to={GREATER_THAN}
        [/filter_base_value]
        [filter_self]
            [filter_adjacent]
                is_enemy=no
                count={COUNT}
                ability=formation
            [/filter_adjacent]
        [/filter_self]
    [/chance_to_hit]
#enddef

#define ABILITY_FORMATION
    [dummy]
        id=formation
        name= _ "formation"
        female_name= _ "female^formation"
        description= _ "This unit gains a +10% bonus to defense when another unit with the same ability is adjacent to it. However, this cannot raise the unit’s defense above 70%."
        special_note=_"Groups of units of this type are able to shield each other in combat."
        [filter]
            [filter_adjacent]
                ability=formation
                is_enemy=no
                count=1-5
            [/filter_adjacent]
        [/filter]
    [/dummy]
    {FORMATION formation1 40 1-5}
    {FORMATION formation2 50 2-5}
    {FORMATION formation3 60 3-5}
    {FORMATION formation4 70 4-5}
    {FORMATION formation5 80 5}
#enddef

#define ABILITY_DISENGAGE
    [dummy]
        id=disengage
        name= _ "disengage"
        female_name= _ "female^disengage"
        description= _ "If this unit doesn’t move before attacking, it will retain its movement points after the attack."
        special_note=_"This unit can move either before or after attacking."
    [/dummy]
#enddef

#define WEAPON_SPECIAL_DAZE
    [dummy]
        id=daze
        name= _ "daze"
        description= _ "When hit with this attack, an enemy suffers a 10% penalty both to their defense and chance to hit for one turn. Other specials that affect chance to hit (e.g. magical and marksman) take precedence over this special.

Magical attacks will still have a 70% chance to hit.
Marksman attacks are only affected if the chance to hit is greater than 60%."
        special_note=_"This unit can daze its enemies, reducing their accuracy and defense until they end a turn."
    [/dummy]
#enddef

#define ABILITY_DISTRACT
    [skirmisher]
        id=distract
        name= _ "distract"
        female_name= _ "female^distract"
        description= _ "This unit negates enemy Zones of Control around itself for allied units (but not for itself)."
        special_note=_"This unit is capable of distracting opponents, allowing allied units to trespass their Zones of Control and move unhindered around them."
        affect_self=no
        affect_allies=yes
        [affect_adjacent]
        [/affect_adjacent]
    [/skirmisher]
#enddef

#define ABILITY_SUPPORT
    [dummy]
        id=support
        name= _ "support"
        female_name= _ "female^support"
        description= _ "The upkeep costs of adjacent friendly units are lowered by 1."
    [/dummy]
#enddef

#define ABILITY_DISENGAGE_EVENTS
    [event]
        name=attack end
        first_time_only=no

        [filter]
            ability=disengage

            [not]
                [filter_wml]
                    moves=$this_unit.max_moves
                [/filter_wml]
            [/not]
        [/filter]

        {VARIABLE unit.moves 0}

        [unstore_unit]
            variable=unit
            find_vacant=no
        [/unstore_unit]
    [/event]
#enddef

#define WEAPON_SPECIAL_DAZE_EVENTS
    [event]
        id=weapon_special_daze_event_2
        name=attacker hits
        first_time_only=no

        [filter_attack]
            special_id=daze
        [/filter_attack]

        [filter_second]
            [not]
                [filter_wml]
                    [status]
                        dazed=yes
                    [/status]
                [/filter_wml]
            [/not]
        [/filter_second]

        {VARIABLE second_unit.status.dazed yes}

        [unstore_unit]
            variable=second_unit
            find_vacant=no
            text= _ "dazed"
            female_text = _ "female^dazed"
            red,green,blue=196,196,128
        [/unstore_unit]

        [object]
            silent=yes
            duration=scenario

            [filter]
                x,y=$x2,$y2
                [not]
                    ability=self_dazed
                [/not]
            [/filter]

            [effect]
                apply_to=new_ability
                [abilities]
                    [chance_to_hit]
                        id=enemy_dazed
                        name=""
                        description=""
                        add=10
                        apply_to=opponent
                        [filter_student]
                            [filter_wml]
                                [status]
                                    dazed=yes
                                [/status]
                            [/filter_wml]
                        [/filter_student]
                        [filter_opponent]
                            [filter_weapon]
                                [not]
                                    special_id=magical
                                [/not]
                                [not]
                                    special_id_active=marksman
                                [/not]
                            [/filter_weapon]
                        [/filter_opponent]
                    [/chance_to_hit]
                    [chance_to_hit]
                        id=enemy_dazed
                        name=""
                        description=""
                        add=10
                        apply_to=opponent
                        [filter_base_value]
                            greater_than_equal_to=60
                        [/filter_base_value]
                        [filter_student]
                            [filter_wml]
                                [status]
                                    dazed=yes
                                [/status]
                            [/filter_wml]
                        [/filter_student]
                        [filter_opponent]
                            [filter_weapon]
                                special_id_active=marksman
                            [/filter_weapon]
                        [/filter_opponent]
                    [/chance_to_hit]
                    [chance_to_hit]
                        id=self_dazed
                        name=""
                        description=""
                        sub=10
                        [filter_student]
                            [filter_weapon]
                                [not]
                                    special_id=magical
                                [/not]
                                [not]
                                    special_id_active=marksman
                                [/not]
                            [/filter_weapon]
                            [filter_wml]
                                [status]
                                    dazed=yes
                                [/status]
                            [/filter_wml]
                        [/filter_student]
                    [/chance_to_hit]
                    [chance_to_hit]
                        id=self_dazed
                        name=""
                        description=""
                        sub=10
                        [filter_base_value]
                            greater_than_equal_to=70
                        [/filter_base_value]
                        [filter_student]
                            [filter_weapon]
                                special_id_active=marksman
                            [/filter_weapon]
                            [filter_wml]
                                [status]
                                    dazed=yes
                                [/status]
                            [/filter_wml]
                        [/filter_student]
                    [/chance_to_hit]
                [/abilities]
            [/effect]
        [/object]
    [/event]

    [event]
        id=weapon_special_daze_event_3
        name=side turn
        first_time_only=no

        [event]
            name=side turn
            delayed_variable_substitution=no

            #{DEBUG_MSG "side $|side_number turn started, clearing daze from side $side_number units"}

            [modify_unit]
                [filter]
                    side=$side_number
                    [filter_wml]
                        [status]
                            dazed=yes
                        [/status]
                    [/filter_wml]
                [/filter]

                [status]
                    dazed=no
                [/status]
            [/modify_unit]
        [/event]
    [/event]
#enddef

#define ABILITY_SUPPORT_EVENTS
    [event]
        name=unit placed
        first_time_only=no

        [filter]
            side=1

            [not]
                level=0
            [/not]

            [filter_wml]
                upkeep=full
            [/filter_wml]

            [filter_adjacent]
                ability=support
            [/filter_adjacent]
        [/filter]

        {VARIABLE unit.variables.original_upkeep $unit.upkeep}
        {VARIABLE unit.variables.upkeep_supported yes}
        {VARIABLE unit.upkeep "$($unit.level - 1)"}

        [unstore_unit]
            variable=unit
            find_vacant=no
            fire_event=no
        [/unstore_unit]
    [/event]

    [event]
        name=set_support_upkeep
        first_time_only=no

        [foreach]
            array=update_support_units
            variable=support_unit

            [do]
                [if]
                    [have_unit]
                        x,y=$support_unit.x,$support_unit.y

                        [not]
                            level=0
                        [/not]

                        [filter_wml]
                            upkeep=full
                        [/filter_wml]

                        [filter_adjacent]
                            ability=support
                        [/filter_adjacent]
                    [/have_unit]

                    [then]
                        {VARIABLE support_unit.variables.original_upkeep $support_unit.upkeep}
                        {VARIABLE support_unit.variables.upkeep_supported yes}
                        {VARIABLE support_unit.upkeep "$($support_unit.level - 1)"}
                    [/then]

                    [else]
                        [if]
                            [have_unit]
                                x,y=$support_unit.x,$support_unit.y

                                [not]
                                    level=0
                                [/not]

                                [filter_wml]
                                    [variables]
                                        upkeep_supported=yes
                                    [/variables]
                                [/filter_wml]

                                [not]
                                    [filter_adjacent]
                                        ability=support
                                    [/filter_adjacent]
                                [/not]
                            [/have_unit]

                            [then]
                                {VARIABLE support_unit.upkeep $support_unit.variables.original_upkeep}
                                {VARIABLE support_unit.variables.upkeep_supported no}
                            [/then]

                            [else]
                                [continue][/continue]
                            [/else]
                        [/if]
                    [/else]
                [/if]

                [unstore_unit]
                    variable=support_unit
                    find_vacant=no
                    fire_event=no
                [/unstore_unit]
            [/do]
        [/foreach]
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1
            ability=support
        [/filter]

        [store_unit]
            [filter]
                [filter_wml]
                    upkeep=full
                [/filter_wml]

                [filter_adjacent]
                    x,y=$x1,$y1
                [/filter_adjacent]

                [or]
                    [filter_wml]
                        [variables]
                            upkeep_supported=yes
                        [/variables]
                    [/filter_wml]

                    [filter_location]
                        [filter_adjacent_location]
                            x,y=$x2,$y2
                        [/filter_adjacent_location]
                    [/filter_location]
                [/or]
            [/filter]

            kill=no
            variable=update_support_units
        [/store_unit]

        [fire_event]
            name=set_support_upkeep
        [/fire_event]

        {CLEAR_VARIABLE update_support_units}

        [on_undo]
            [store_unit]
                [filter]
                    [filter_wml]
                        upkeep=full
                    [/filter_wml]

                    [filter_location]
                        [filter_adjacent_location]
                            x,y=$x2,$y2
                        [/filter_adjacent_location]
                    [/filter_location]

                    [or]
                        [filter_wml]
                            [variables]
                                upkeep_supported=yes
                            [/variables]
                        [/filter_wml]

                        [filter_location]
                            [filter_adjacent_location]
                                x,y=$x1,$y1
                            [/filter_adjacent_location]
                        [/filter_location]
                    [/or]
                [/filter]

                kill=no
                variable=update_support_units
            [/store_unit]

            [fire_event]
                name=set_support_upkeep
            [/fire_event]

            {CLEAR_VARIABLE update_support_units}
        [/on_undo]

        [allow_undo]
        [/allow_undo]
    [/event]

    [event]
        name=moveto
        first_time_only=no

        [filter]
            side=1

            [not]
                ability=support
            [/not]

            [and]
                [filter_wml]
                    upkeep=full
                [/filter_wml]

                [filter_adjacent]
                    side=1
                    ability=support
                [/filter_adjacent]

                [or]
                    [filter_wml]
                        [variables]
                            upkeep_supported=yes
                        [/variables]
                    [/filter_wml]

                    [not]
                        [filter_adjacent]
                            side=1
                            ability=support
                        [/filter_adjacent]
                    [/not]
                [/or]
            [/and]
        [/filter]

        [store_unit]
            [filter]
                x,y=$x1,$y1
            [/filter]

            kill=no
            variable=update_support_units
        [/store_unit]

        [fire_event]
            name=set_support_upkeep
        [/fire_event]

        {CLEAR_VARIABLE update_support_units}

        [on_undo]
            [store_unit]
                [filter]
                    x,y=$x2,$y2
                [/filter]

                kill=no
                variable=update_support_units
            [/store_unit]

            [fire_event]
                name=set_support_upkeep
            [/fire_event]

            {CLEAR_VARIABLE update_support_units}
        [/on_undo]

        [allow_undo]
        [/allow_undo]
    [/event]

    [event]
        name=die,post advance
        first_time_only=no

        [filter]
            side=1
            ability=support
        [/filter]

        [store_unit]
            [filter]
                side=1

                [filter_adjacent]
                    x,y=$x1,$y1
                [/filter_adjacent]
            [/filter]

            kill=no
            variable=update_support_units
        [/store_unit]

        [fire_event]
            name=set_support_upkeep
        [/fire_event]

        {CLEAR_VARIABLE update_support_units}
    [/event]

    [event]
        name=post advance
        first_time_only=no

        [filter]
            side=1

            [filter_adjacent]
                ability=support
            [/filter_adjacent]
        [/filter]

        [store_unit]
            [filter]
                x,y=$x1,$y1
            [/filter]

            kill=no
            variable=update_support_units
        [/store_unit]

        [fire_event]
            name=set_support_upkeep
        [/fire_event]

        {CLEAR_VARIABLE update_support_units}
    [/event]
#enddef


#textdomain wesnoth-utbs
# Macros for setting animations.

# Quenoth elf animation macros

#define QUENOTH_FLAME_INTERNAL X Y Y2
misc/blank-hex.png~BLIT(units/quenoth/fire/flame-color.png,{X},{Y2})~MASK(units/quenoth/fire/flame-mask.png,{X},{Y}):75#enddef

#define QUENOTH_FLAME_POS X Y
    {QUENOTH_FLAME_INTERNAL {X} {Y} 15},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 14},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 13},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 12},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 11},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 10},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 9},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 8},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 7},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 6},
    {QUENOTH_FLAME_INTERNAL {X} {Y} 5},
{QUENOTH_FLAME_INTERNAL {X} {Y} 4}#enddef

# Not all unwalkable, but still supposed to provide some visual cue that movement is different.
# This is only used for animation, it's the list of terrains on which the Sun Sylph will fly.
# Both the old ^Uf* and new ^Tf* mushrooms are supported here - if the Quenoth elves are moved
# to core then it seems sensible to still have the old behavior on old maps.
#define QUENOTH_UNWALKABLE_TERRAINS
{DRAKE_UNWALKABLE_TERRAINS},A*^*,Uu*^*,Uh*^*,*^Uf*,Tb*^*,*^Tf*#enddef


#textdomain wesnoth-utbs

#define KALEH
    id=Kaleh
    name= _ "Kaleh"
    gender=male
    type=Quenoth Youth
    canrecruit=yes
    unrenamable=yes
#enddef

#define NYM
    type=Quenoth Youth
    id=Nym
    name= _ "Nym"
    gender=female
    unrenamable=yes
    random_traits=no
    profile=portraits/nym.png
    upkeep=loyal
    [modifications]
        {TRAIT_LOYAL_HERO}
        {TRAIT_DEXTROUS}
    [/modifications]
#enddef

#define GARAK
    type=Quenoth Champion
    id=Garak
    name= _ "Garak"
    profile=portraits/garak.png
    unrenamable=yes
    [modifications]
        {TRAIT_LOYAL_HERO}
        {TRAIT_STRONG}
        [object]
            [effect]
                apply_to=new_ability
                [abilities]
                    {ABILITY_TEACHING}
                [/abilities]
            [/effect]
            [effect]
                apply_to=max_experience
                increase=850
            [/effect]
        [/object]
    [/modifications]
#enddef

#define ZHUL
    type=Quenoth Shaman
    id=Zhul
    name= _ "Zhul"
    profile=portraits/zhul.png
    unrenamable=yes
    [modifications]
        {TRAIT_LOYAL_HERO}
        {TRAIT_INTELLIGENT}
        [object]
            [effect]
                [filter]
                    type=Quenoth Shyde
                [/filter]
                apply_to=profile
                portrait=portraits/zhul_shyde.png
            [/effect]
        [/object]
    [/modifications]
#enddef


#textdomain wesnoth-utbs
# This file contains message to display when heroes die
# Some of these are conditional on in_final_fight, which
# will be set to true for the final battle.

[event]
    name=last breath
    [filter]
        id=Kaleh
    [/filter]

#ifdef UTBS_IN_FINAL_FIGHT
    [message]
        speaker=unit
        message= _ "To die on the cusp of victory. Oh, the irony!"
    [/message]
#else
    [message]
        speaker=unit
        message= _ "I’m too young to die. What will happen to my people?"
    [/message]
#endif
    [endlevel]
        result=defeat
    [/endlevel]
[/event]

[event]
    name=last breath

    [filter]
        id=Nym
    [/filter]

    [message]
        speaker=unit
        message= _ "Save me, Kaleh, I’m dying..."
        image=portraits/nym_moody.png
    [/message]

#ifdef UTBS_IN_FINAL_FIGHT
    {VARIABLE_OP allies_killed add 1}
#else
    [endlevel]
        result=defeat
    [/endlevel]
#endif
[/event]

[event]
    name=last breath
    [filter]
        id=Zhul
    [/filter]

    [message]
        speaker=unit
        message= _ "Eloh protect us, I have fallen."
    [/message]

#ifdef UTBS_IN_FINAL_FIGHT
    {VARIABLE_OP allies_killed add 1}
#else
    [endlevel]
        result=defeat
    [/endlevel]
#endif
[/event]

#ifdef UTBS_GARAK_MUST_LIVE
[event]
    name=last breath

    [filter]
        id=Garak
    [/filter]

    [message]
        speaker=unit
        message= _ "Aaargh! Curse you!"
    [/message]

    [endlevel]
        result=defeat
    [/endlevel]
[/event]
#endif

[event]
    name=last breath

    [filter]
        id=Grog,Nog,Rogrimir,Jarl
    [/filter]

    [if]
        [variable]
            name=ally_race
            equals=dwarf
        [/variable]

        [then]
            [message]
                speaker=unit
                message= _ "I go now to join my forefathers..."
            [/message]
        [/then]

        [else]
            [message]
                speaker=unit
                message= _ "No, $unit.name can’t die now!"
            [/message]
        [/else]
    [/if]

#ifdef UTBS_IN_FINAL_FIGHT
    {VARIABLE ally_died_in_final_fight yes}
    {VARIABLE_OP allies_killed add 1}
#else
    [if]
        [variable]
            name=ally_must_live
            boolean_equals=yes
        [/variable]

        [then]
            [message]
                speaker=Kaleh
                message= _ "Without his guidance, how can we escape from these infernal caves? We are lost without him!"
            [/message]

            [endlevel]
                result=defeat
            [/endlevel]
        [/then]
        [else]
            [message]
                speaker=Kaleh
                message= _ "He was a brave warrior and a true ally. He will be missed."
            [/message]
        [/else]
    [/if]
#endif
[/event]

[event]
    name=last breath
    [filter]
        id=Esanoo
    [/filter]

    [message]
        speaker=unit
        message= _ "Farewell Nym, I go to the sea god. You elves must go on without me."
    [/message]

    [message]
        speaker=Nym
        message= _ "Farewell Esanoo. You were my favorite of the merfolk."
        image=portraits/nym_moody.png
    [/message]
[/event]

#ifndef UTBS_IN_FINAL_FIGHT
[event]
    name=last breath
    [filter]
        id=Kromph
    [/filter]

    [message]
        speaker=unit
        message= _ "Aaurrgghh!!"
    [/message]
[/event]

[event]
    name=last breath

    [filter]
        id=Elyssa
    [/filter]

    [message]
        speaker=unit
        message= _ "This can’t be the end, I have too much left to see."
    [/message]

    [message]
        speaker=Kaleh
        message= _ "How could we have let her die? I’ve never seen fire magic like that before. She will be sorely missed."
    [/message]
[/event]
#endif


#textdomain wesnoth-utbs

#define HYDRATING_TILES
Ww*, *^V* #enddef

#define DEHYDRATING_TILES
Hd, Dd*, Dd^E*, Rd #enddef

#define DEHYDRATABLE_UNITS_FILTER
    [not]
        race=undead
    [/not]
    [not]
        type=Giant Scorpion
    [/not]
    [not]
        type=Dust Devil
    [/not]
    [not]
        # traveller's ring
        role=immune
    [/not]
    [and]
        side=1
    [/and]
#enddef

# This dehydrates the given units. However, if the unit is adjacent to a
# friendly healer, the dehydration will be delayed instead: the unit suffers no
# ill effects, but it cannot receive any healing on this turn either. Basically,
# dehydration works just like poison.
#
# Each turn of dehydration lowers the unit’s attack damage by 1 point and
# decreases hitpoints by -$dehydration_loss.

#define APPLY_DEHYDRATION_EFFECT FILTER
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable=dehydrating_units
    [/store_unit]

    [for]
        array=dehydrating_units
        [do]
            [if]
                [have_unit]
                    x,y=$dehydrating_units[$i].x,$dehydrating_units[$i].y

                    [filter_adjacent]
                        is_enemy=no
                        [and]
                            ability=healing
                            [or]
                                ability=curing
                            [/or]
                        [/and]
                    [/filter_adjacent]
                [/have_unit]

                [then]
                    {VARIABLE dehydrating_units[$i].resting no}
                    {VARIABLE dehydrating_units[$i].status.dehydration_slowed_by_healer yes}

                    [unstore_unit]
                        variable=dehydrating_units[$i]
                        find_vacant=no
                    [/unstore_unit]
                [/then]

                [else]
                    [modify_unit]
                        [filter]
                            id=$dehydrating_units[$i].id
                        [/filter]
                        ## this object might be obtained multiple times, but that shouldn't be a problem.
                        [object]
                            duration=scenario
                            id="dehydration_effect"
                            [effect]
                                apply_to=attack
                                formula="damage>1"
                                increase_damage=-1
                            [/effect]
                        [/object]
                        [object]
                            duration=scenario
                            id="dehydration_overlay"
                            [effect]
                                apply_to=overlay
                                add="misc/dehydration-icon.png"
                            [/effect]
                        [/object]
                        hitpoints="$(max(1, $dehydrating_units[$i].hitpoints - $dehydration_loss))"
                        resting=no
                        [status]
                            dehydrated=yes
                        [/status]
                    [/modify_unit]

                    [if]
                        [variable]
                            name=dehydrating_units[$i].gender
                            equals=female
                        [/variable]

                        [then]
                            [floating_text]
                                x=$dehydrating_units[$i].x
                                y=$dehydrating_units[$i].y
                                text= _ "female^thirst"
                                {COLOR_HARM}
                            [/floating_text]
                        [/then]

                        [else]
                            [floating_text]
                                x=$dehydrating_units[$i].x
                                y=$dehydrating_units[$i].y
                                text= _ "thirst"
                                {COLOR_HARM}
                            [/floating_text]
                        [/else]
                    [/if]
                [/else]
            [/if]
        [/do]
    [/for]

    {CLEAR_VARIABLE dehydrating_units}
#enddef

# This rehydrates the given units. The units gain back their full attack damage.
#define APPLY_HYDRATION_EFFECT FILTER
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable=hydrating_units
    [/store_unit]

    [for]
        array=hydrating_units
        [do]
            [if]
                [variable]
                    name=hydrating_units[$i].status.dehydrated
                    equals=yes
                [/variable]

                [then]
                    [remove_object]
                        x,y=$hydrating_units[$i].x,$hydrating_units[$i].y
                        object_id=dehydration_effect
                    [/remove_object]
                [/then]
            [/if]

            [modify_unit]
                [filter]
                    id=$hydrating_units[$i].id
                [/filter]
                [status]
                    dehydrated=no
                [/status]
            [/modify_unit]

            [if]
                [variable]
                    name=hydrating_units[$i].gender
                    equals=female
                [/variable]

                [then]
                    [floating_text]
                        x=$hydrating_units[$i].x
                        y=$hydrating_units[$i].y
                        text= _ "female^refreshed"
                        {COLOR_HEAL}
                    [/floating_text]
                [/then]

                [else]
                    [floating_text]
                        x=$hydrating_units[$i].x
                        y=$hydrating_units[$i].y
                        text= _ "refreshed"
                        {COLOR_HEAL}
                    [/floating_text]
                [/else]
            [/if]

            [remove_object]
                x,y=$hydrating_units[$i].x,$hydrating_units[$i].y
                object_id=dehydration_overlay
            [/remove_object]
        [/do]
    [/for]

    {CLEAR_VARIABLE hydrating_units}
#enddef

#
# Events
#
#define DEHYDRATION_EVENTS
    [event]
        name=prestart

        # dehydration loss - a variable for the dialogue
#ifdef EASY
        {VARIABLE dehydration_loss 3}
#endif
#ifdef NORMAL
        {VARIABLE dehydration_loss 5}
#endif
#ifdef HARD
        {VARIABLE dehydration_loss 7}
#endif
    [/event]

    [event]
        name=side turn
        first_time_only=no

        # dehydration:
        # only at dawn, morning, mid-day, afternoon and dusk
        {APPLY_DEHYDRATION_EFFECT (
            side=$side_number
            {DEHYDRATABLE_UNITS_FILTER}

            [filter_location]
                terrain={DEHYDRATING_TILES}
                [not]
                    time_of_day_id=short_dark,long_dark1,long_dark2,long_dark3,long_dark4
                [/not]
            [/filter_location]
        )}
    [/event]

    [event]
        name=side turn
        first_time_only=no

        # rehydration in water, villages or when next to healers
        {APPLY_HYDRATION_EFFECT (
            side=$side_number
            status=dehydrated

            [and]
                [filter_location]
                    terrain={HYDRATING_TILES}
                [/filter_location]
            [/and]
        )}
    [/event]

    [event]
        name=turn refresh
        first_time_only=no

        [if]
            [variable]
                name=side_number
                equals=1
            [/variable]

            [then]
                {MODIFY_UNIT (
                    side=1
                    status=dehydration_slowed_by_healer
                ) status.dehydration_slowed_by_healer no}
            [/then]
        [/if]
    [/event]

    [event]
        name=advance
        first_time_only=no

        [filter]
            status=dehydrated
        [/filter]

        {VARIABLE unit.status.dehydrated no}

        [unstore_unit]
            variable=unit
            find_vacant=no
            male_text= _ "refreshed"
            female_text= _ "female^refreshed"
            {COLOR_HEAL}
            advance=no
        [/unstore_unit]

        [remove_object]
            x,y=$unit.x,$unit.y
            object_id=dehydration_overlay
        [/remove_object]
    [/event]

    [event]
        name="victory"

        [modify_unit]
            [filter]
                status=dehydrated
            [/filter]

            [status]
                dehydrated=no
            [/status]
        [/modify_unit]

        [modify_unit]
            [filter]
                status=dehydration_slowed_by_healer
            [/filter]

            [status]
                dehydration_slowed_by_healer=no
            [/status]
        [/modify_unit]
    [/event]
#enddef


#textdomain wesnoth-utbs
# check to see if the Dust Devil is the unit
# triggering the event, and if it is, use Nym
# as the speaker
#define CHECK_EXPLORER
    [if]
        [variable]
            name=unit.type
            equals=Dust Devil
        [/variable]
        [or]
            [variable]
                name=unit.type
                equals="Flesh Golem"
            [/variable]
        [/or]
        [then]
            [store_unit]
                variable=explorer
                kill=no
                [filter]
                    id=Nym
                [/filter]
            [/store_unit]
        [/then]
        [else]
            [store_unit]
                variable=explorer
                kill=no
                [filter]
                    x=$unit.x
                    y=$unit.y
                [/filter]
            [/store_unit]
        [/else]
    [/if]
#enddef
# for second_unit
#define CHECK_SPEAKER
    [if]
        [variable]
            name=second_unit.type
            equals=Dust Devil
        [/variable]
        [or]
            [variable]
                name=second_unit.type
                equals="Flesh Golem"
            [/variable]
        [/or]
        [then]
            [store_unit]
                variable=speaking_unit
                kill=no
                [filter]
                    id=Nym
                [/filter]
            [/store_unit]
        [/then]
        [else]
            [store_unit]
                variable=speaking_unit
                kill=no
                [filter]
                    x=$second_unit.x
                    y=$second_unit.y
                [/filter]
            [/store_unit]
        [/else]
    [/if]
#enddef

#define MESSAGE_DEPEND_ON_ALLY GROG_WML NOG_WML ROGRIMIR_WML JARL_WML
    [switch]
        variable=ally_name
        [case]
            value=Grog
            {GROG_WML}
        [/case]

        [case]
            value=Nog
            {NOG_WML}
        [/case]

        [case]
            value=Rogrimir
            {ROGRIMIR_WML}
        [/case]

        [case]
            value=Jarl
            {JARL_WML}
        [/case]
    [/switch]
#enddef


#textdomain wesnoth-utbs

# event for the teaching ability
#define ABILITY_TEACHING_EVENT
    [event]
        name=turn refresh,victory
        id=garak_teaching
        first_time_only=no

        [lua]
            # wmllint: markcheck off
            code=<<
                local _ = wesnoth.textdomain "wesnoth-utbs"

                local teachers = wesnoth.units.find_on_map { ability = "teaching", side = wesnoth.current.side }

                for i, teacher in ipairs(teachers) do
                    local students = wesnoth.units.find_on_map { side = wesnoth.current.side, { "filter_adjacent", { id = teacher.id } } }

                    if #students > 0 then -- don't divide by zero
                        wesnoth.interface.scroll_to_hex(teacher.x, teacher.y)

                        local function cf(u1, u2)
                            -- criteria: the unit with lower level comes first
                            -- if level is equal, the unit with less experience comes first
                            if u1.level < u2.level then return true end
                            if u1.level > u2.level then return false end
                            if u1.experience < u2.experience then return true end
                            return false
                        end

                        table.sort(students, cf)

                        -- now we need to divide Garak's xp between all the units
                        -- we can't just round the result of the division, because this will mean
                        -- either not redistributing all the XP points, or generating them out of thin air
                        -- so we perform both a division and a modulo
                        -- # (length) has a higher precedence than *, / and %
                        local div = math.floor(teacher.experience / #students)
                        local mod = teacher.experience % #students

                        for j, student in ipairs(students) do
                            -- the modulo operation is used to redistribute every remaining XP
                            -- so at the start of each turn Garak goes back to 0 XP
                            -- concrete example: we have 9 XP and 4 units
                            -- the first unit will receive 3 XP, while the other units 2
                            local xp_to_add = div
                            if j <= mod then
                                xp_to_add = xp_to_add + 1
                            end

                            -- having a message that tells you that the unit gained 0 XP is simply obnoxious
                            if xp_to_add > 0 then
                                student.experience = student.experience + xp_to_add
                                -- po: Floating text shown when Garak's teaching ability transfers experience points
                                local xp_string = _ "+$amount XP"
                                xp_string = xp_string:vformat{amount = xp_to_add}
                                wesnoth.interface.float_label(student.x, student.y, "" .. xp_string .. "")
                                if student.experience >= student.max_experience then
                                    -- advance unit, animate and fire events
                                    student:advance(true, true)
                                end
                            end
                        end
                    end

                    -- in case that Garak has no "students", his experience goes wasted instead of being redistributed...
                    teacher.experience = 0
                end
            >>
            # wmllint: markcheck off
        [/lua]
    [/event]
#enddef


#textdomain wesnoth-utbs
# The file contains useful macros I use within the campaign

#define RANDOM_PLACEMENT_AREA X Y RADIUS
    # set a placement area for subsequent random placement of units
    [store_locations]
        x,y={X},{Y}
        radius={RADIUS}
        variable=random_placement_locations
    [/store_locations]
#enddef

#define PLACE_UNITS_RANDOMLY NUMBER SIDE TYPE ROLE_STRING NAME_STRING TRAIT_WML
    [repeat]
        times={NUMBER}
        [do]
            {RANDOM "0..$($random_placement_locations.length - 1)"}
            {VARIABLE random_placement_x $random_placement_locations[$random].x}
            {VARIABLE random_placement_y $random_placement_locations[$random].y}

            [unit]
                side={SIDE}
                type={TYPE}
                x=$random_placement_x
                y=$random_placement_y
                role={ROLE_STRING}
                name={NAME_STRING}
                animate=yes
                {TRAIT_WML}
            [/unit]
        [/do]
    [/repeat]
#enddef

#define CLEAR_PLACEMENT_AREA
    {CLEAR_VARIABLE random_placement_x}
    {CLEAR_VARIABLE random_placement_y}
    {CLEAR_VARIABLE random_placement_locations}
#enddef

# The amount of extra recruit cost is kept in $recruit_cost_extra; when this
# macro is called for the first time, it'll disallow recruitment of
# "Desert Fighter" and then allow recruitment of "Desert FighterX". The second
# call then disallows recruitment of "Desert FighterY" and allows recruitment of
# "Desert Fighter(X+Y)", and so on. After $recruit_cost_extra has reached 10,
# nothing will happen anymore.

#define INCREASE_RECRUIT_COSTS X
    {VARIABLE num $recruit_cost_extra}
    {VARIABLE_OP recruit_cost_extra add {X}}
    [if]
        [variable]
            name=recruit_cost_extra
            greater_than=10
        [/variable]
        [then]
            {VARIABLE recruit_cost_extra 10}
        [/then]
    [/if]

    #{DEBUG_MSG "disallowing: Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num"}
    [disallow_recruit]
        type=Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num
        side=1
    [/disallow_recruit]

    {VARIABLE num $recruit_cost_extra}
    #{DEBUG_MSG "allowing: Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num"}
    [allow_recruit]
        type=Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num
        side=1
    [/allow_recruit]

    {CLEAR_VARIABLE num}
#enddef

#define UTBS_SHAKE_SCREEN
    [scroll]
        x=20
    [/scroll]
    [scroll]
        x=-20
    [/scroll]
    [scroll]
        x=20
    [/scroll]
    [scroll]
        x=-20
    [/scroll]
#enddef

# To give the human units in scenarios 8 and 9 a more unique look, we shift
# the blue steel colors to more of a bronze hue. Because the blue colors don't
# adhere to an exact consistent palette, we can't use ~RC() for this but instead
# we need to do it with a rather hacky formula.

#define ARMOR_SHIFT NEWCHAN ORIGCHAN
if(red <= green and green <= blue + 3, ({NEWCHAN} / 255.0)^2 * 255.0, {ORIGCHAN})#enddef

#define ARMOR_SHIFT_IPF
"CHAN({ARMOR_SHIFT blue red}, {ARMOR_SHIFT green green}, {ARMOR_SHIFT red blue})"#enddef

#define LOYALIST_COLOR_SHIFT
    [event]
        name=unit placed
        first_time_only=no

        [filter]
            race=human

            [not]
                type_adv_tree=Heavy Infantryman
            [/not]

            [not]
                [filter_wml]
                    [modifications]
                        [object]
                            armor_color_shift=yes
                        [/object]
                    [/modifications]
                [/filter_wml]
            [/not]
        [/filter]

        [object]
            armor_color_shift=yes
            silent=yes

            [effect]
                apply_to=image_mod
                add={ARMOR_SHIFT_IPF}
            [/effect]
        [/object]
    [/event]
#enddef