#textdomain wesnoth-low

#define PLAYER_GOLD_04
    {GOLD 280 250 220}
#enddef

#define MULTIPLAYER_GOLD_04
    {GOLD 90 80 70}
#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}}
    #TODO add message for the other players!
    [message]
        side_for={SIDE}
        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 ELVES_SCOUTS
Elvish Scout, Elvish Rider, Elvish Outrider#enddef

#define ELVES_STEEL
Elvish Fighter, Elvish Hero, Elvish Captain, Elvish Marshal, Elvish Champion, Elvish Archer, Elvish Marksman, Elvish Sharpshooter, Elvish Ranger, Elvish Avenger, {ELVES_SCOUTS}#enddef

#define ELVES_NON_STEEL
Elvish Shaman, Elvish Shyde, Elvish Enchantress, Elvish Sorceress, Elvish Sylph, Elvish Druid, Wose, Elder Wose, Ancient Wose, {ELVES_SCOUTS}#enddef

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

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

        [capture_village]
            side={SIDE}
            x={X}
            y={Y}
        [/capture_village]
    [/event]
#enddef

#define STARTING_VILLAGES_ORC SIDE
    [event]
        name=prestart

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

#define STARTING_VILLAGES_ELF SIDE
    [event]
        name=prestart

        [capture_village]
            side={SIDE}
            terrain=*^Ve*
        [/capture_village]
    [/event]
#enddef

#define STARTING_VILLAGES_DWARF SIDE
    [event]
        name=prestart

        [capture_village]
            side={SIDE}
            terrain=*^Vhh*
        [/capture_village]
    [/event]
#enddef

#define STARTING_VILLAGES_HUMAN SIDE
    [event]
        name=prestart

        [capture_village]
            side={SIDE}
            terrain=*^Vh,*^Vha,*^Vhc,*^Vl*
        [/capture_village]
    [/event]
#enddef

#define STARTING_VILLAGES_TROLL SIDE
    [event]
        name=prestart

        [capture_village]
            side={SIDE}
            terrain=*^Vc*
        [/capture_village]
    [/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 value "0..$random_placement_locations.length"}

    {RANDOM $random_string}

    {VARIABLE_OP random_placement_x value "$random_placement_locations[$random].x"}
    {VARIABLE_OP random_placement_y value "$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 STORE_SIDE SIDE
    [store_unit]
        [filter]
            side={SIDE}
        [/filter]
        variable=party_store
    [/store_unit]
#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

#define ELF_SETUP
    {FLAG_VARIANT wood-elvish}
#enddef

#define SAURIAN_SETUP
    {FLAG_VARIANT undead}
#enddef

#define ORC_SETUP
    {FLAG_VARIANT ragged}
#enddef

#define DWARF_SETUP
    {FLAG_VARIANT knalgan}
#enddef

#define LOYALIST_SETUP
    {FLAG_VARIANT loyalist}
#enddef

#define TROLL_SETUP
    {FLAG_VARIANT undead}
#enddef

##### Persistent Global Variable macros for multiplayer mode #####

#define LOAD_PERSISTENT_IF_LOCAL_EMPTY SIDE NAMESPACE GLOBAL_VAR LOCAL_VAR
    [if]
        {VARIABLE_CONDITIONAL ({LOCAL_VAR}.length) equals 0}
        [then]
            {DEBUG ("Global Variable {GLOBAL_VAR} is empty for side {SIDE}.")}
            {VARIABLE_FROM_GLOBAL {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR} {SIDE}}
        [/then]
        [else]
            {DEBUG ("Global Variable {GLOBAL_VAR} is at size: ${LOCAL_VAR}.length|, so {SIDE} got not considered.")}
        [/else]
    [/if]
#enddef

#define LOAD_PERSISTENT_2PLAYER SIDE1 SIDE2 NAMESPACE GLOBAL_VAR LOCAL_VAR
    #Loads a global variable into a local one.
    #Specify two sides, the variable is loaded from the second one if it is empty
    #after reading from the first.
    {VARIABLE_FROM_GLOBAL {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR} {SIDE1}}
    {LOAD_PERSISTENT_IF_LOCAL_EMPTY {SIDE2} {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR}}
#enddef

#define LOAD_PERSISTENT_3PLAYER SIDE1 SIDE2 SIDE3 NAMESPACE GLOBAL_VAR LOCAL_VAR
    #Loads a global variable into a local one.
    #Specify two sides, the variable is loaded from the second one if it is empty
    #after reading from the first.
    {VARIABLE_FROM_GLOBAL {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR} {SIDE1}}
    {LOAD_PERSISTENT_IF_LOCAL_EMPTY {SIDE2} {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR}}
    {LOAD_PERSISTENT_IF_LOCAL_EMPTY {SIDE3} {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR}}
#enddef

#define LOAD_PERSISTENT_4PLAYER SIDE1 SIDE2 SIDE3 SIDE4 NAMESPACE GLOBAL_VAR LOCAL_VAR
    #Loads a global variable into a local one.
    #Specify two sides, the variable is loaded from the second one if it is empty
    #after reading from the first.
    {VARIABLE_FROM_GLOBAL {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR} {SIDE1}}
    {LOAD_PERSISTENT_IF_LOCAL_EMPTY {SIDE2} {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR}}
    {LOAD_PERSISTENT_IF_LOCAL_EMPTY {SIDE3} {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR}}
    {LOAD_PERSISTENT_IF_LOCAL_EMPTY {SIDE4} {NAMESPACE} {GLOBAL_VAR} {LOCAL_VAR}}
#enddef

#define UNSTORE_PARTY SIDE
    {FOREACH party_store member}
        {VARIABLE (party_store[$member].side) {SIDE}}
        [unstore_unit]
            variable=party_store[$member]
            x,y=recall,recall
        [/unstore_unit]

        {DEBUG "unstoring $party_store[$member].name to side {SIDE}."}
    {NEXT member}
    {CLEAR_VARIABLE member}
    {CLEAR_VARIABLE party_store}
#enddef

#define SET_MP_GOLD SIDE
    [modify_side]
        side={SIDE}
        gold=$gold_store
    [/modify_side]
    {CLEAR_VARIABLE gold_store}
#enddef

#define GET_PERSISTENT_RECALLS_FROM2 SIDE1 SIDE2 NAMESPACE GLOBAL_VAR
    #Loads the recall list variable from both sides.
    {LOAD_PERSISTENT_2PLAYER {SIDE1} {SIDE2} {NAMESPACE} {GLOBAL_VAR} party_store}
    {UNSTORE_PARTY {SIDE1} }
#enddef

#define GET_PERSISTENT_RECALLS_FROM3 SIDE1 SIDE2 SIDE3 NAMESPACE GLOBAL_VAR
    #Loads the recall list variable from three sides.
    {LOAD_PERSISTENT_3PLAYER {SIDE1} {SIDE2} {SIDE3} {NAMESPACE} {GLOBAL_VAR} party_store}
    {UNSTORE_PARTY {SIDE1} }
#enddef

#define GET_PERSISTENT_RECALLS_FROM4 SIDE1 SIDE2 SIDE3 SIDE4 NAMESPACE GLOBAL_VAR
    #Loads the recall list variable from four sides.
    {LOAD_PERSISTENT_4PLAYER {SIDE1} {SIDE2} {SIDE3} {SIDE4} {NAMESPACE} {GLOBAL_VAR} party_store}
    {UNSTORE_PARTY {SIDE1} }
#enddef

#define GET_PERSISTENT_GOLD_FROM2 SIDE1 SIDE2 NAMESPACE GLOBAL_VAR
    #Loads the recall list variable from both sides.
    {LOAD_PERSISTENT_2PLAYER {SIDE1} {SIDE2} {NAMESPACE} {GLOBAL_VAR} gold_store}
    {SET_MP_GOLD {SIDE1} }
#enddef

#define GET_PERSISTENT_GOLD_FROM3 SIDE1 SIDE2 SIDE3 NAMESPACE GLOBAL_VAR
    #Loads the recall list variable from both sides.
    {LOAD_PERSISTENT_3PLAYER {SIDE1} {SIDE2} {SIDE3} {NAMESPACE} {GLOBAL_VAR} gold_store}
    {SET_MP_GOLD {SIDE1} }
#enddef

#define GET_PERSISTENT_GOLD_FROM4 SIDE1 SIDE2 SIDE3 SIDE4 NAMESPACE GLOBAL_VAR
    #Loads the recall list variable from both sides.
    {LOAD_PERSISTENT_4PLAYER {SIDE1} {SIDE2} {SIDE3} {SIDE4} {NAMESPACE} {GLOBAL_VAR} gold_store}
    {SET_MP_GOLD {SIDE1} }
#enddef
