#textdomain wesnoth-low

#define LOW_MAP NAME
    map_data="{campaigns/Legend_of_Wesmere/maps/{NAME}}"
#enddef

#define LOW_MASK NAME X Y
    [event]
        name=prestart
        [terrain_mask]
            x={X}
            y={Y}
            mask="{campaigns/Legend_of_Wesmere/masks/{NAME}}"
        [/terrain_mask]
    [/event]
#enddef

#define WINTER_MASK MASK_NAME
    [event]
        name=prestart
        [terrain_mask]
            x,y=1,1
            mask="{campaigns/Legend_of_Wesmere/masks/{MASK_NAME}}"
            border=yes

            #seems not to work
            #Windmill
            [rule]
                old=Gg^Wm
                new=Aa
                terrain=Aa^Wm
            [/rule]

            #human Village
            [rule]
                old=Gg^Vh
                new=Aa
                terrain=Aa^Vha
            [/rule]
            [rule]
                old=Gg^Vl
                new=Aa
                terrain=Aa^Vla
            [/rule]

            #Orc Village
            [rule]
                old=Gg^Vo
                new=Aa
                terrain=Aa^Voa
            [/rule]
            [rule]
                old=Hh^Vo
                new=Aa
                terrain=Ha^Voa
            [/rule]

            #dwarf Village
            [rule]
                old=Hh^Vhh
                new=Aa
                terrain=Ha^Vhha
            [/rule]
            [rule]
                old=Mm^Vhh
                new=Aa
                terrain=Ms^Vhha
            [/rule]

            #elvish Village
            [rule]
                old=Gg^Ve
                new=Aa
                terrain=Aa^Vea
            [/rule]
            #flat hut Village
            [rule]
                old=Gg^Vc
                new=Aa
                terrain=Aa^Vca
            [/rule]
            #hill hut Village
            [rule]
                old=Hh^Vc
                new=Aa
                terrain=Ha^Vca
            [/rule]
            #forest
            [rule]
                old=Gs^Fp
                new=Aa
                terrain=Aa^Fpa
            [/rule]
            #hills
            [rule]
                old=Hh
                new=Aa
                terrain=Ha
            [/rule]
            #forested hills
            [rule]
                old=Hh^Fp
                new=Aa
                terrain=Ha^Fpa
            [/rule]
            #deciduous forest
            [rule]
                old=Gs^Fds
                new=Aa
                terrain=Aa^Fda
            [/rule]
            #deciduous forested hills
            [rule]
                old=Hh^Fds
                new=Aa
                terrain=Ha^Fda
            [/rule]

            #water
            [rule]
                old=Ww
                new=Aa
                terrain=Ai
            [/rule]

            #River Ford --> ice
            [rule]
                old=Wwf
                new=Aa
                terrain=Ai
            [/rule]

            # that would brake the balance of S19
            #swamp
            #[rule]
            #    old=Ss
            #    new=Aa
            #    terrain=Ai
            #[/rule]
            #castle
            [rule]
                new=Ce,Ke
            [/rule]
            [rule]
                old=Ce,Ke
                new=Aa^Fpa
            [/rule]
            #mountains
            [rule]
                old=Mm
                new=Aa
                terrain=Ms
            [/rule]
            #mountain village
            [rule]
                old=Mm^Vhh
                new=Aa
                terrain=Ms^Vhha
            [/rule]

            #Grass
            [rule]
                old=Gg,Re,Ggf
                new=Aa
                #don't specify terrain and it just uses the new terrain
            [/rule]

            #Savannah
            [rule]
                old=Gs
                new=Aa
                terrain=Aa
            [/rule]

            [rule]
                old=Rd
                new=Aa
                terrain=Aa
            [/rule]

            #Mushroom lichtung
            [rule]
                old=Gs^Uf
                new=Aa
                terrain=Aa
            [/rule]

            #default: Will match everything, since 'old' and 'new' aren't
            #specified. Set 'use_old=yes' to signal no change.
            [rule]
                use_old=yes
            [/rule]
        [/terrain_mask]
    [/event]
#enddef

#define FILTER_LOYALS
    [filter_wml]
        upkeep="loyal"
    [/filter_wml]
#enddef

#define RECALL_LOYALS
    [store_unit]
        variable=loyals
        [filter]
            {FILTER_LOYALS}
            [not]
                canrecruit=yes
            [/not]
            x,y=recall,recall
        [/filter]
    [/store_unit]

    {FOREACH loyals loyal_one}
        [recall]
            id=$loyals[$loyal_one].id
        [/recall]
    {NEXT loyal_one}
    {CLEAR_VARIABLE loyals}
#enddef

#define LOOT AMOUNT SIDE
    {VARIABLE amount_gold {AMOUNT}}
    [message]
        speaker=narrator
        message= _ "You retrieve $amount_gold pieces of gold."
        image=wesnoth-icon.png
    [/message]
    {CLEAR_VARIABLE amount_gold}

    [sound]
        name=gold.ogg
    [/sound]

    [gold]
        side={SIDE}
        amount={AMOUNT}
    [/gold]
#enddef

#define SET_GOLD SIDE EASY_FACTOR NORMAL_FACTOR HARD_FACTOR
    [event]
        name=prestart

        [store_gold]
            side=1
        [/store_gold]

#ifdef EASY
        {VARIABLE_OP gold multiply {EASY_FACTOR}}
#endif

#ifdef NORMAL
        {VARIABLE_OP gold multiply {NORMAL_FACTOR}}
#endif

#ifdef HARD
        {VARIABLE_OP gold multiply {HARD_FACTOR}}
#endif

        [modify_side]
            side={SIDE}
            gold=$gold
        [/modify_side]

        [wml_message]
            message="gold of side {SIDE} set to $gold"	# wmllint: ignore no spellcheck
            logger=warn
        [/wml_message]

        {CLEAR_VARIABLE gold}
    [/event]
#enddef

#define ELVES
Elvish Fighter, Elvish Archer, Elvish Scout, Elvish Shaman#enddef

#define SHAMANS
Elvish Shaman, Elvish Shyde, Elvish Enchantress, Elvish Sorceress, Elvish Sylph, Elvish Druid#enddef

#define DWARVES
Dwarvish Fighter, Dwarvish Thunderer, Dwarvish Scout, Dwarvish Ulfserker, Dwarvish Berserker, Dwarvish Guardsman#enddef

#define DWARVES_NO_ULFS
Dwarvish Fighter, Dwarvish Thunderer, Dwarvish Scout, Dwarvish Guardsman#enddef

#define ORCS1
Orcish Archer, Orcish Assassin, Orcish Grunt, Wolf Rider, Goblin Spearman#enddef

#define VILLAGE_SIDE SIDE X Y
    [event]
        name=prestart

        [command]
            [capture_village]
                side={SIDE}
                x={X}
                y={Y}
            [/capture_village]
        [/command]

        [message_debug]
            logger=warn
            message="village at {X}, {Y} to side {SIDE}"	# wmllint: ignore no spellcheck
        [/message_debug]
    [/event]
#enddef

#define STARTING_VILLAGES_ORC SIDE
    [event]
        name=prestart

        #{CAPTURE_VILLAGES_OF_TYPE (*^Vhh) {SIDE} 1 1 {INFINITY}}
        {CAPTURE_FILTERED_VILLAGES {SIDE} (terrain=*^Vo*)}
    [/event]
#enddef

#define STARTING_VILLAGES_ELF SIDE
    [event]
        name=prestart

        {CAPTURE_FILTERED_VILLAGES {SIDE} (terrain=*^Ve*)}
    [/event]
#enddef

#define STARTING_VILLAGES_DWARF SIDE
    [event]
        name=prestart

        #{CAPTURE_VILLAGES_OF_TYPE (*^Vhh) {SIDE} 1 1 {INFINITY}}
        {CAPTURE_FILTERED_VILLAGES {SIDE} (terrain=*^Vhh*)}
    [/event]
#enddef

#define STARTING_VILLAGES_HUMAN SIDE
    [event]
        name=prestart

        {CAPTURE_FILTERED_VILLAGES {SIDE} (terrain=*^Vh,*^Vha,*^Vhc,*^Vl*)}
    [/event]
#enddef

#define STARTING_VILLAGES_TROLL SIDE
    [event]
        name=prestart

        {CAPTURE_FILTERED_VILLAGES {SIDE} (terrain=*^Vc*)}
    [/event]
#enddef

#define VILLAGE_KALENZ X Y
    {VILLAGE_SIDE 1 {X} {Y}}
#enddef

# Thanks to Shadow Master for this macro
# !***Macro to random place unit within area***
#define RANDOM_PLACEMENT _X _Y _RADIUS _WML
    [store_locations]
        x,y={_X},{_Y}
        radius={_RADIUS}
        variable=random_placement_locations
    [/store_locations]

    {VARIABLE_OP random_string format "0..$random_placement_locations.length"}

    {RANDOM $random_string}

    {VARIABLE_OP random_placement_x format "$random_placement_locations[$random].x"}
    {VARIABLE_OP random_placement_y format "$random_placement_locations[$random].y"}

    {CLEAR_VARIABLE random_placement_locations}

    {_WML}

    [+unit]
        x,y=$random_placement_x,$random_placement_y
    [/unit]

    {CLEAR_VARIABLE random_placement_x}
    {CLEAR_VARIABLE random_placement_y}
#enddef

#define LOW_ENSURE_CANRECRUIT
    {MODIFY_UNIT id=Olurf canrecruit yes}
    {MODIFY_UNIT id=Landar canrecruit yes}
    {MODIFY_UNIT id=Kalenz canrecruit yes}
    {MODIFY_UNIT id=Cleodil canrecruit yes}
#enddef

#define LOW_ENSURE_NOT_HERO
    {UNMAKE_HERO Olurf}
    {UNMAKE_HERO Landar}
    {UNMAKE_HERO Kalenz}
    {UNMAKE_HERO Cleodil}
#enddef

#define LOW_PRESTART
    #this is needed to allow old saves (where the leaders can't recruit) to pick canrecrut="yes"
    [event]
        name=prestart
        {LOW_ENSURE_CANRECRUIT}
        {LOW_ENSURE_NOT_HERO}
    [/event]
#enddef

#define LOW_STEAL_UNIT_TO ID SIDE

    #if we have {ID} not in side {SIDE} transfer him to side {SIDE}
    # at position determined by existing unit of {SIDE} with id={ID}

    [store_unit]
        variable=source_var
        kill=yes
        [filter]
            id={ID}
            [not]
                side={SIDE}
            [/not]
        [/filter]
    [/store_unit]
    [store_unit]
        variable=target_var
        kill=no
        [filter]
            id={ID}
            side={SIDE}
        [/filter]
    [/store_unit]
    [if]
        [variable]
            name=source_var.length
            greater_than=0
        [/variable]
        [variable]
            name=target_var.length
            greater_than=0
        [/variable]
        [then]
            {VARIABLE source_var[0].side {SIDE}}
            [unstore_unit]
                variable=source_var[0]
                x=$target_var[0].x
                y=$target_var[0].y
            [/unstore_unit]
        [/then]
    [/if]
    {CLEAR_VARIABLE source_var}
    {CLEAR_VARIABLE target_var}
#enddef
