#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]

        {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 units_lacking_initiative i}
            {INITIATIVE_OBJECT x,y=$units_lacking_initiative[$i].x,$units_lacking_initiative[$i].y}
        {NEXT i}

        {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
