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

#define RANDOM_TRAIT_UNIT SIDE TYPE X Y ID_STRING NAME_STRING TRAIT_WML
    [unit]
        side={SIDE}
        type={TYPE}
        x={X}
        y={Y}
        id={ID_STRING}
        name={NAME_STRING}
        upkeep=full
        animate=yes
        {TRAIT_WML}
    [/unit]
#enddef

#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]
    [set_variable]
        name=random_string
        value="0..$($random_placement_locations.length - 1)"
    [/set_variable]
#enddef

#define PLACE_UNITS_RANDOMLY NUMBER SIDE TYPE ID_STRING NAME_STRING TRAIT_WML
    {VARIABLE number {NUMBER}}
    {VARIABLE i 0}
    [while]
        [variable]
            name=i
            less_than=$number
        [/variable]
        [do]
            {CLEAR_VARIABLE random}
            {RANDOM $random_string}
            {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
                id={ID_STRING}
                role={ID_STRING}
                name={NAME_STRING}
                upkeep=full
                animate=yes
                {TRAIT_WML}
            [/unit]
            {VARIABLE_OP i add 1}
        [/do]
    [/while]
#enddef

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

# wmlindent: start ignoring
#define STARTLOOP VAR
# Loop VAR times, counting from -VAR to -1.
[set_variable]
name={VAR}
multiply=-1
[/set_variable]

[while]
    [variable]
    name={VAR}
    less_than=0
    [/variable]
    [do] #enddef

# Same as mainline NEXT
#define ENDLOOP VAR
# Macro to end a WML clause that iterates over an array.
    [set_variable]
    name={VAR}
    add=1
    [/set_variable]
    [/do]
[/while]
{CLEAR_VARIABLE {VAR}}
#enddef
# wmlindent: stop ignoring

#define INVOKE_TROLL_VARIATION FILTER
    [object]
        silent=yes
        duration=forever

        [filter]
            {FILTER}
        [/filter]

        [effect]
            apply_to=variation
            name=troll
        [/effect]
    [/object]

    [modify_unit]
        [filter]
            {FILTER}
        [/filter]

        description= _ "These undead trolls fight again in death as they did in life, except this time they smell much worse."
    [/modify_unit]

    [redraw][/redraw]
#enddef

#define FIND_LOCATION_FOR_DARK_ASSASSIN
    # This finds the nearest empty location around Kaleh - if there is none
    # immediately adjacent to him, then the radius is expanded until an empty
    # passable hex is found
    #
    # Stores the result in the variables $dark_assassin_location.x and
    # $dark_assassin_location.y

    {VARIABLE assassin_location_seek_radius 1}

    [while]
        [not]
            [have_location]
                [not]
                    [filter]
                    [/filter]
                [/not]

                [and]
                    [filter]
                        id="Kaleh"
                    [/filter]

                    radius=$assassin_location_seek_radius

                    [filter_radius]
                        [not]
                            terrain=X*,*^X*,Q*
                        [/not]
                    [/filter_radius]
                [/and]
            [/have_location]
        [/not]

        [do]
            {VARIABLE_OP assassin_location_seek_radius add 1}
        [/do]
    [/while]

    [store_locations]
        [not]
            [filter]
            [/filter]
        [/not]

        [and]
            [filter]
                id="Kaleh"
            [/filter]

            radius=$assassin_location_seek_radius

            [filter_radius]
                [not]
                    terrain=X*,*^X*,Q*
                [/not]
            [/filter_radius]
        [/and]

        variable=possible_assassin_locations
    [/store_locations]

    {VARIABLE_OP assassin_location_i rand "1..$possible_assassin_locations.length"}
    {VARIABLE_OP assassin_location_i sub 1}

    {VARIABLE dark_assassin_location.x $possible_assassin_locations[$assassin_location_i].x}
    {VARIABLE dark_assassin_location.y $possible_assassin_locations[$assassin_location_i].y}

    {CLEAR_VARIABLE possible_assassin_locations,assassin_location_i}
#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 Fighter1". The second
# call then disallows recruitment of "Desert Fighter1" and allows recruitment of
# "Desert Fighter2", and so on. After $recruit_cost_extra has reached 10,
# nothing will happen anymore.

#define INCREASE_RECRUIT_COSTS_BY_ONE
    [if]
        [variable]
            name=recruit_cost_extra
            less_than=10
        [/variable]

        [then]
            {VARIABLE num $recruit_cost_extra}
            #{DEBUG_MSG "disallowing: Desert Fighter$num,Desert Archer$num,Desert Hunter$num,Desert Shaman$num,Desert Scout$num"}
            [disallow_recruit]
                type=Desert Fighter$num,Desert Archer$num,Desert Hunter$num,Desert Shaman$num,Desert Scout$num
                side=1
            [/disallow_recruit]

            {VARIABLE_OP recruit_cost_extra add 1}

            {VARIABLE num $recruit_cost_extra}
            #{DEBUG_MSG "allowing: Desert Fighter$num,Desert Archer$num,Desert Hunter$num,Desert Shaman$num,Desert Scout$num"}
            [allow_recruit]
                type=Desert Fighter$num,Desert Archer$num,Desert Hunter$num,Desert Shaman$num,Desert Scout$num
                side=1
            [/allow_recruit]
        [/then]

        #[else]
        #    {DEBUG_MSG "Error: tried to raise recruit costs more than 10g!"}
        #[/else]
    [/if]
#enddef
