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

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

#define PLACE_UNITS_RANDOMLY NUMBER SIDE TYPE ROLE_STRING NAME_STRING TRAIT_WML
    [repeat]
        times={NUMBER}
        [do]
            {RANDOM "0..$($random_placement_locations.length - 1)"}
            {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
                role={ROLE_STRING}
                name={NAME_STRING}
                animate=yes
                {TRAIT_WML}
            [/unit]
        [/do]
    [/repeat]
#enddef

#define CLEAR_PLACEMENT_AREA
    {CLEAR_VARIABLE random_placement_x}
    {CLEAR_VARIABLE random_placement_y}
    {CLEAR_VARIABLE random_placement_locations}
#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 FighterX". The second
# call then disallows recruitment of "Desert FighterY" and allows recruitment of
# "Desert Fighter(X+Y)", and so on. After $recruit_cost_extra has reached 10,
# nothing will happen anymore.

#define INCREASE_RECRUIT_COSTS X
    {VARIABLE num $recruit_cost_extra}
    {VARIABLE_OP recruit_cost_extra add {X}}
    [if]
        [variable]
            name=recruit_cost_extra
            greater_than=10
        [/variable]
        [then]
            {VARIABLE recruit_cost_extra 10}
        [/then]
    [/if]

    #{DEBUG_MSG "disallowing: Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num"}
    [disallow_recruit]
        type=Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num
        side=1
    [/disallow_recruit]

    {VARIABLE num $recruit_cost_extra}
    #{DEBUG_MSG "allowing: Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num"}
    [allow_recruit]
        type=Quenoth Fighter$num,Tauroch Rider$num,Quenoth Scout$num,Quenoth Mystic$num
        side=1
    [/allow_recruit]

    {CLEAR_VARIABLE num}
#enddef

#define UTBS_SHAKE_SCREEN
    [scroll]
        x=20
    [/scroll]
    [scroll]
        x=-20
    [/scroll]
    [scroll]
        x=20
    [/scroll]
    [scroll]
        x=-20
    [/scroll]
#enddef

# To give the human units in scenarios 8 and 9 a more unique look, we shift
# the blue steel colors to more of a bronze hue. Because the blue colors don't
# adhere to an exact consistent palette, we can't use ~RC() for this but instead
# we need to do it with a rather hacky formula.

#define ARMOR_SHIFT NEWCHAN ORIGCHAN
if(red <= green and green <= blue + 3, ({NEWCHAN} / 255.0)^2 * 255.0, {ORIGCHAN})#enddef

#define ARMOR_SHIFT_IPF
"CHAN({ARMOR_SHIFT blue red}, {ARMOR_SHIFT green green}, {ARMOR_SHIFT red blue})"#enddef

#define LOYALIST_COLOR_SHIFT
    [event]
        name=unit placed
        first_time_only=no

        [filter]
            race=human

            [not]
                type_adv_tree=Heavy Infantryman
            [/not]

            [not]
                [filter_wml]
                    [modifications]
                        [object]
                            armor_color_shift=yes
                        [/object]
                    [/modifications]
                [/filter_wml]
            [/not]
        [/filter]

        [object]
            armor_color_shift=yes
            silent=yes

            [effect]
                apply_to=image_mod
                add={ARMOR_SHIFT_IPF}
            [/effect]
        [/object]
    [/event]
#enddef
