#textdomain wesnoth-ei

#define ANIMATE
    [+unit]
        animate=yes
    [/unit]
#enddef

# specialty macro used for S11 and S12 - no need to modify this to be more flexible
#define STORE_RECALLS FLT VARIABLE
    [store_unit]
        [filter]
            side=1
            x,y=recall,recall

            [not]
                trait=undead
            [/not]
            # WCs/Soulless look weird caged with humans
            {FLT}
        [/filter]
        kill=no
        variable={VARIABLE}
        mode=append
    [/store_unit]
#enddef

#define UNSTORE_AND_RECALL VARIABLE ID X Y
    [unstore_unit]
        variable={VARIABLE}
    [/unstore_unit]
    [put_to_recall_list]
        id={ID}
        heal=yes
    [/put_to_recall_list]
    {RECALL_XY {ID} {X} {Y}} # side 2 right now
#enddef

#define KILL FLT
#arg ANIMATE
no#endarg
#arg FIRE_EVENT
no#endarg
    [kill]
        {FLT}
        animate={ANIMATE}
        fire_event={FIRE_EVENT}
    [/kill]
#enddef

#define KILL_COUNT COUNT FLT
#arg ANIMATE
no#endarg
#arg FIRE_EVENT
no#endarg
    [store_unit]
        [filter]
            {FLT}
        [/filter]

        kill=no
        variable=KILL_COUNT_stored_units
    [/store_unit]
    {VARIABLE KILL_COUNT_i 0}
    [foreach]
        array=KILL_COUNT_stored_units
        variable=KILL_COUNT_unit
        [do]
            [if] {VARIABLE_CONDITIONAL i less_than {COUNT}}
                [then]
                    {VARIABLE_OP i add 1}
                    [kill]
                        id=$KILL_COUNT_unit.id
                        animate={ANIMATE}
                        fire_event={FIRE_EVENT}
                    [/kill]
                [/then]
            [/if]
        [/do]
    [/foreach]
    {CLEAR_VARIABLE KILL_COUNT_stored_units,KILL_COUNT_i}
#enddef

# how many levels of units are in our army? (plus gold/20)
#define CALCULATE_ARMY_STRENGTH FLT GOLDSIDE
    {VARIABLE army_strength 0}

    [store_unit]
        [filter]
            [not]
                x,y=recall,recall
            [/not]

            {FLT}
            level=3-99
        [/filter]
        kill=no
        variable=army
        mode=always_clear
    [/store_unit]
    {VARIABLE army_count $army.length}
    {VARIABLE_OP army_count multiply 3}
    {VARIABLE_OP army_strength add $army_count}

    [store_unit]
        [filter]
            [not]
                x,y=recall,recall
            [/not]

            {FLT}
            level=2
        [/filter]
        kill=no
        variable=army
        mode=always_clear
    [/store_unit]
    {VARIABLE army_count $army.length}
    {VARIABLE_OP army_count multiply 2}
    {VARIABLE_OP army_strength add $army_count}

    [store_unit]
        [filter]
            [not]
                x,y=recall,recall
            [/not]

            {FLT}
            level=1
        [/filter]
        kill=no
        variable=army
        mode=always_clear
    [/store_unit]
    {VARIABLE army_count $army.length}
    {VARIABLE_OP army_count multiply 1}
    {VARIABLE_OP army_strength add $army_count}

    [store_unit]
        [filter]
            [not]
                x,y=recall,recall
            [/not]

            {FLT}
            level=0
        [/filter]
        kill=no
        variable=army
        mode=always_clear
    [/store_unit]
    {VARIABLE army_count $army.length}
    {VARIABLE_OP army_count multiply 0.5}
    {VARIABLE_OP army_strength add $army_count}

    [store_gold]
        side={GOLDSIDE}
        variable=gold
    [/store_gold]
    [if] {VARIABLE_CONDITIONAL gold greater_than 0}
        [then]
            {VARIABLE_OP gold divide 20} # count 20 gold as roughly 1 unit-level of army strength
            {VARIABLE_OP army_strength add $gold}
        [/then]
    [/if]

    {CLEAR_VARIABLE army,army_count,gold} # not army_strength, that's our return value
#enddef

#define FIND_NEARBY_NOT_DACYN_PREFER_HEROES X Y LIMIT
    {FIND_NEARBY id=Gweddry {X} {Y} {LIMIT}}
    [if]
        [not]
            [variable]
                name=found_unit.length
                greater_than=0
            [/variable]
        [/not]

        [then]
            {FIND_NEARBY id=Owaec {X} {Y} {LIMIT}}
            [if]
                [not]
                    [variable]
                        name=found_unit.length
                        greater_than=0
                    [/variable]
                [/not]

                [then]
                    {FIND_NEARBY id=Terraent {X} {Y} {LIMIT}}
                    [if]
                        [not]
                            [variable]
                                name=found_unit.length
                                greater_than=0
                            [/variable]
                        [/not]

                        [then]
                            {FIND_NEARBY id=Konrad {X} {Y} {LIMIT}}
                            [if]
                                [not]
                                    [variable]
                                        name=found_unit.length
                                        greater_than=0
                                    [/variable]
                                [/not]

                                [then]
                                    {FIND_NEARBY (
                                        side=1
                                        [not]
                                            id=Dacyn
                                        [/not]
                                    ) {X} {Y} {LIMIT}}
                                    [if]
                                        [not]
                                            [variable]
                                                name=found_unit.length
                                                greater_than=0
                                            [/variable]
                                        [/not]

                                        [then]
                                            {FIND_NEARBY id=Dacyn {X} {Y} {LIMIT}}
                                        [/then]
                                    [/if]
                                [/then]
                            [/if]
                        [/then]
                    [/if]
                [/then]
            [/if]
        [/then]
    [/if]

#enddef

# copied (with modifications) from https://wiki.wesnoth.org/WML_Utilities
#define FIND_NEARBY FLT X Y LIMIT
    # Does a search for a nearby unit that matches the given filter.
    # Basically just looks for such a unit with increasing radius until it finds at least one
    [clear_variable]
        name=found_unit
    [/clear_variable]
    [set_variable]
        name=search_radius
        value=0
    [/set_variable]
    [while]
        [not]
            [variable]
                name=found_unit.length
                greater_than=0
            [/variable]
        [/not]
        [and]
            [variable]
                name=search_radius
                less_than_equal_to={LIMIT}
            [/variable]
        [/and]
        [do]
            #             {DEBUG "Searching depth $search_radius around ({X}, {Y})..."}
            [store_unit]
                variable=found_unit
                [filter]
                    {FLT}
                    [filter_location]
                        x,y={X},{Y}
                        radius=$search_radius
                    [/filter_location]
                [/filter]
            [/store_unit]
            #             {DEBUG "...found $found_unit.length units."}
            [set_variable]
                name=search_radius
                add=1
            [/set_variable]
        [/do]
    [/while]
#enddef

#define SPAWN_IF_UNFOUND SIDE TYPE X Y
    [set_variable]
        name=randx
        rand={X}
    [/set_variable]
    [set_variable]
        name=randy
        rand={Y}
    [/set_variable]

    [if]
        [not]
            [variable]
                name=found_unit.length
                greater_than=0
            [/variable]
        [/not]

        [then]
            {NAMED_UNIT {SIDE} ({TYPE}) $randx $randy spawned () ()} {ANIMATE}
            [store_unit]
                variable=found_unit
                [filter]
                    id=spawned
                [/filter]
            [/store_unit]
        [/then]
    [/if]

    {CLEAR_VARIABLE randx}
    {CLEAR_VARIABLE randy}
#enddef

#define CLEANUP_SEARCH
    # Clears variables involved in searching (the FIND_NEARBY macro). Put this
    # in your name=victory,defeat tag to clean up if you use FIND_NEARBY within
    # a scenario.
    [clear_variable]
        name=found_unit, search_radius
    [/clear_variable]
#enddef

#define RAVANAL_POSSESS SPIRIT_X SPIRIT_Y POSSESS_FILTER FACING
    {CLEAR_VARIABLE possessee}
    [store_unit]
        [filter]
            {POSSESS_FILTER}
        [/filter]
        variable=possessee
    [/store_unit]

    # move dark shape to possessee
    [move_unit_fake]
        type=Dark Shape
        x={SPIRIT_X},$possessee.x
        y={SPIRIT_Y},$possessee.y
        side=$possessee.side
    [/move_unit_fake]

    # "kill" possessee
    [animate_unit]
        [filter]
            {POSSESS_FILTER}
        [/filter]
        flag=levelout
    [/animate_unit]
    [kill]
        {POSSESS_FILTER}
    [/kill]

    # create ravanal and animate
    [unit]
        {CHARACTER_STATS_RAVANAL}
        x=$possessee.x
        y=$possessee.y
        side=$possessee.side
        facing={FACING}
    [/unit]
    [animate_unit]
        [filter]
            id=Mal-Ravanal
        [/filter]
        flag=levelin
    [/animate_unit]
#enddef

#define RAVANAL_DEPOSSESS SPIRIT_X SPIRIT_Y
    # "kill" ravanal
    [animate_unit]
        [filter]
            id=Mal-Ravanal
        [/filter]

        flag=levelout
    [/animate_unit]
    [fire_event]
        name=terror_despawn
        [primary_unit]
            id=Mal-Ravanal
        [/primary_unit]
    [/fire_event]
    [kill]
        id=Mal-Ravanal
    [/kill]

    # recreate possessee
    [unstore_unit]
        variable=possessee
        find_vacant=yes
    [/unstore_unit]
    [animate_unit]
        [filter]
            id=$possessee.id
        [/filter]
        flag=levelin
    [/animate_unit]

    [move_unit_fake]
        type=Dark Shape
        x=$possessee.x,{SPIRIT_X}
        y=$possessee.y,{SPIRIT_Y}
        side=$possessee.side
    [/move_unit_fake]

    {CLEAR_VARIABLE possessee}
#enddef
