#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
        format="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

#***Macro that will unclutter scenarios somewhat***
#define ADD VAR AMOUNT
    [set_variable]
        name={VAR}
        add={AMOUNT}
    [/set_variable]
#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} description ( _ "These undead trolls fight again in death as they did in life, except this time they smell much worse.")}

    [redraw][/redraw]
#enddef

# UNIT_OVERLAY adds an overlay to a unit, taking in a standard filter
#
# Example that gives all spearmen a book:
# {UNIT_OVERLAY type=Spearman items/book1.png}

#define UNIT_OVERLAY FILTER IMAGE
    [store_unit]
        [filter]
            {FILTER}
        [/filter]
        variable=UNIT_OVERLAY_store
        kill=no
    [/store_unit]
    {FOREACH UNIT_OVERLAY_store UNIT_OVERLAY_i}
        {VARIABLE_OP UNIT_OVERLAY_tempx format
        $UNIT_OVERLAY_store[$UNIT_OVERLAY_i].x}
        {VARIABLE_OP UNIT_OVERLAY_tempy format
        $UNIT_OVERLAY_store[$UNIT_OVERLAY_i].y}
        [unit_overlay]
            x=$UNIT_OVERLAY_tempx
            y=$UNIT_OVERLAY_tempy
            image={IMAGE}
        [/unit_overlay]
    {NEXT UNIT_OVERLAY_i}
    {CLEAR_VARIABLE UNIT_OVERLAY_store}
#enddef

#define REMOVE_UNIT_OVERLAY FILTER IMAGE
    [store_unit]
        [filter]
            {FILTER}
        [/filter]
        variable=UNIT_OVERLAY_store
        kill=no
    [/store_unit]
    {FOREACH UNIT_OVERLAY_store UNIT_OVERLAY_i}
        {VARIABLE_OP UNIT_OVERLAY_tempx format
        $UNIT_OVERLAY_store[$UNIT_OVERLAY_i].x}
        {VARIABLE_OP UNIT_OVERLAY_tempy format
        $UNIT_OVERLAY_store[$UNIT_OVERLAY_i].y}
        [remove_unit_overlay]
            x=$UNIT_OVERLAY_tempx
            y=$UNIT_OVERLAY_tempy
            image={IMAGE}
        [/remove_unit_overlay]
    {NEXT UNIT_OVERLAY_i}
    {CLEAR_VARIABLE UNIT_OVERLAY_store}
#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 random "1..$possible_assassin_locations.length"}
    {VARIABLE_OP assassin_location_i add -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
