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

#define RANDOM_TRAIT_UNIT SIDE TYPE X Y DESCRIPTION USER_DESCRIPTION TRAIT_WML
    [unit]
        side={SIDE}
        type={TYPE}
        x={X}
        y={Y}
        description={DESCRIPTION}
        user_description={USER_DESCRIPTION}
        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]
    {VARIABLE_OP random_string format "0..$random_placement_locations.length"}
#enddef

#define PLACE_UNITS_RANDOMLY NUMBER SIDE TYPE DESCRIPTION USER_DESCRIPTION TRAIT_WML
    {VARIABLE number {NUMBER}}
    {VARIABLE i 0}
    [while]
        [variable]
            name=i
            less_than=$number
        [/variable]
        [do]
            {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
                description={DESCRIPTION}
                role={DESCRIPTION}
                user_description={USER_DESCRIPTION}
                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

# create a unit with full upkeep
#define UNIT_ TYPE DESCRIPTION USER_DESCRIPTION SIDE X Y
    [unit]
        type={TYPE}
        description={DESCRIPTION}
        user_description={USER_DESCRIPTION}
        side={SIDE}
        upkeep=full
        x={X}
        y={Y}
    [/unit]
#enddef

# create a unit with no upkeep
#define FREE_UNIT TYPE DESCRIPTION USER_DESCRIPTION SIDE X Y
    [unit]
        type={TYPE}
        description={DESCRIPTION}
        user_description={USER_DESCRIPTION}
        side={SIDE}
        upkeep=free
        x={X}
        y={Y}
    [/unit]
#enddef

# create a unit with full upkeep and random traits

#define UNIT_T TYPE DESCRIPTION USER_DESCRIPTION SIDE X Y
    [unit]
        side={SIDE}
        type={TYPE}
        description={DESCRIPTION}
        user_description={USER_DESCRIPTION}
        upkeep=full
        x={X}
        y={Y}
        random_traits="yes"
    [/unit]
#enddef

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

        [filter]
            {FILTER}
        [/filter]

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

    {MODIFY_UNIT {FILTER} unit_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 SET_PORTRAIT_ON_ADVANCE NAME TYPE PROFILE
    # Create an event that hacks a character's portrait on level advance.
    [event]
        name=post_advance

        [filter]
            description={NAME}
            type={TYPE}
        [/filter]

        {MODIFY_UNIT description={NAME} profile {PROFILE}}
    [/event]
#enddef
