#textdomain wesnoth-httt

# NOTE: This code is tailored for Li'sar in this campaign specifically, and will
# not work correctly elsewhere without modification.

#define ABILITY_INITIATIVE
    [dummy]
        id=initiative
        name= _ "initiative"
        description= _ "All adjacent friendly units will strike first in melee combat, even when defending."
    [/dummy]
#enddef

#define SPECIAL_NOTES_INITIATIVE
_" This unit’s grasp of melee tactics allows adjacent allies to strike the first blow even when defending."#enddef

#define INITIATIVE_OBJECT FILTER
    [object]
        silent=yes

        [filter]
            {FILTER}
        [/filter]

        [effect]
            apply_to=attack
            range=melee

            [set_specials]
                mode=append

                [firststrike]
                    id=initiative
                    name=""
                    description=""
                    [filter_self]
                        [filter_adjacent]
                            id="Li'sar"
                        [/filter_adjacent]
                    [/filter_self]
                [/firststrike]
            [/set_specials]
        [/effect]
    [/object]
#enddef

#define LACKS_INITIATIVE_FILTER
    side=1,2

    [filter_side]
        [allied_with]
            [has_unit]
                id="Li'sar"
            [/has_unit]
        [/allied_with]
    [/filter_side]

    [filter_wml]
        [attack]
            range=melee
        [/attack]
    [/filter_wml]

    [not]
        [filter_wml]
            [attack]
                range=melee
                [specials]
                    [firststrike]
                    [/firststrike]
                [/specials]
            [/attack]
        [/filter_wml]
    [/not]
#enddef

#define WEAPON_SPECIAL_INITIATIVE_EVENTS
    [event]
        id=initiative_1
        name=prerecruit,prerecall
        first_time_only=no

        [filter]
            {LACKS_INITIATIVE_FILTER}
        [/filter]
        [allow_undo]
        [/allow_undo]
        [on_undo]
            [modify_unit]
                [filter]
                    id=$unit.id
                [/filter]
                [effect]
                    apply_to=attack
                    range=melee
                    remove_specials=initiative
                [/effect]
            [/modify_unit]
        [/on_undo]

        {INITIATIVE_OBJECT x,y=$x1,$y1}
    [/event]

    [event]
        id=initiative_2
        name=side 1 turn, side 2 turn
        first_time_only=no

        [store_unit]
            [filter]
                {LACKS_INITIATIVE_FILTER}
            [/filter]

            kill=no
            variable=units_lacking_initiative
        [/store_unit]

        [foreach]
            array=units_lacking_initiative
            [do]
                {INITIATIVE_OBJECT x,y=$this_item.x,$this_item.y}
            [/do]
        [/foreach]

        {CLEAR_VARIABLE units_lacking_initiative}
    [/event]

    [event]
        id=initiative_3
        name=attack
        first_time_only=no

        [filter_second]
            {LACKS_INITIATIVE_FILTER}
        [/filter_second]

        {INITIATIVE_OBJECT x,y=$x2,$y2}
    [/event]
#enddef
