#textdomain wesnoth-dm

# During the plot of this campaign Delfador changes recall lists more
# often than most Wesnoth characters change their underwear.  This file
# exists to encapsulate all that machinery and the comments explaining
# what it does.

# That means: *ALL* recall list manipulation should be done *HERE*!
# The point is to have it in one place so the complexity can be audited.

#define MEMOIRS_STORE_MATCHING_UNITS WML VAR
    # Store filtered units on Defador's side, except him, into an array variable
    [store_unit]
        kill=yes
        variable={VAR}
        [filter]
            side=1
            [and]
                [not]
                    id=Delfador
                [/not]
            [/and]
            {WML}
        [/filter]
    [/store_unit]
#enddef

#define MEMOIRS_STORE_UNITS VAR
    # Store all units on Delfador's side, except him, into an array variable
    {MEMOIRS_STORE_MATCHING_UNITS () {VAR}}
#enddef

#define MEMOIRS_UNSTORE_UNITS VAR
    # Unstore units from an array back into existence
    {FOREACH {VAR} i}
        [unstore_unit]
            variable={VAR}[$i]
            find_vacant=yes
            x=recall
            y=recall
        [/unstore_unit]
        [heal_unit]
            [filter]
                id={VAR}[$i].id
            [/filter]
            amount=999
        [/heal_unit]
    {NEXT i}

    [clear_variable]
        name={VAR}
    [/clear_variable]
#enddef

#define SUBSUME_SIDE_AND_VILLAGES SIDE
    # first change village ownership
    [store_villages]
        owner_side={SIDE}

        variable=side_villages
    [/store_villages]
    {FOREACH side_villages i}
        [capture_village]
            side=1
            x,y=$side_villages[$i].x,$side_villages[$i].y
        [/capture_village]
    {NEXT i}
    {CLEAR_VARIABLE side_villages}

    #next, turn off canrecruit for the leader and change all allegiances.
    {MODIFY_UNIT (side={SIDE}
    canrecruit=yes) canrecruit no}
    {MODIFY_UNIT side={SIDE} side 1}

    [redraw]
        side=1
    [/redraw]
#enddef

# At the end of Ur-Thorodur (8) Delf loses Lionel and his Wesnothian guards.
# In the otherworld scenarios Delf starts with an empty recall list
# and can only recruit undead.

#define SAVE_WESNOTH_VETERANS
    [heal_unit]
        [filter]
            id=Lionel
        [/filter]
        amount=full
        animate=no
        moves=full
        restore_attacks=yes
        restore_statuses=yes
    [/heal_unit]
    [store_unit]
        # When next we meet, it will be in Garard's chambers
        [filter]
            id=Lionel
        [/filter]
        variable=Lionel
        kill=yes
    [/store_unit]
    {MEMOIRS_STORE_UNITS delfador_lionels_guard}
#enddef

# At the beginning of Wasteland (11), Delf loses his undead veterans
# and gets to recruit elves. The undead will get one more chance to
# shine in Showdown in the Northern Swamp (20). Note: this macro needs
# to be called from a start or prestart event, before recruiting can
# occur.

#define SAVE_UNDEAD_VETERANS
    {MEMOIRS_STORE_UNITS dead_recall}
#enddef

# At the end of Wasteland (11), Chantal's side 3 gets absorbed into Delfador's
# side 1.

#define CHANTAL_FOLLOWS_DELFADOR
    {MODIFY_UNIT (id=Chantal) canrecruit no}
    {MODIFY_UNIT (side=3) side 1}
#enddef

# In Terror at the Ford of Parthyn (12), Delfador gets control of some
# loyalist troops and later some mages.  We'll use SUBSUME_SIDE_AND_VILLAGES
# for these.

# At the end of Terror at the Ford of Parthyn (12), Chantal leaves to
# warn the North Elves; she won't be available until the Elves and
# Delfador meet again.

#define CHANTAL_DEPARTS
    [store_unit]
        variable=chantal_store
        kill=yes
        [filter]
            id=Chantal
        [/filter]
    [/store_unit]
#enddef

# At the beginning of The Return of Trouble (13) Kalenz must temporarily
# become the leader of side 1, with an empty recall list. Kalenz will
# rejoin Delfador at the end of To Save The King (15).

#define KALENZ_TAKES_CONTROL
    # Delfador departs the scene
    [store_unit]
        variable=delfador_store
        kill=yes
        [filter]
            id=Delfador
        [/filter]
    [/store_unit]

    # Survivors from Terror at the Ford of Parthyn will be conditionally
    # available depending on which group Delfador is running with in
    # the future.  No need to filter out Chantal as she is already gone.
    {MEMOIRS_STORE_MATCHING_UNITS race=human delfador_human_veterans}
    {MEMOIRS_STORE_MATCHING_UNITS (race=elf
    [not]
        id=Kalenz
    [/not]) delfador_elvish_veterans}

    # Kalenz, the new protagonist, should have an empty recall list
    [kill]
        side=1
        [not]
            id=Kalenz
        [/not]
    [/kill]

    # New recruit list for Kalenz
    [set_recruit]
        side=1
        # Don't cut Kalenz back to recruiting L1s only, it's not surivable
        # in the scenario where this is used.
        recruit={ELVES},{ELVES2}
    [/set_recruit]
#enddef

# At the end of The Return of Trouble (13), Chantal rejoins Kalenz.

#define CHANTAL_RETURNS X Y
    [unstore_unit]
        variable=chantal_store
        find_vacant=yes
        x={X}
        y={Y}
    [/unstore_unit]
    {CLEAR_VARIABLE chantal_store}
#enddef

# In Shadows (14) Delfador reappears partway through the scenario,
# still attached to side 1 but not its leader (which is still Kalenz).
# He brings with him some Wesnothian troops; his recall list is
# otherwise empty, and he has no keep in this scenario.

#define DELFADOR_REAPPEARS X Y
    [move_unit_fake]
        type=$delfador_store.type
        side=1
        x={X}
        y={Y}
    [/move_unit_fake]

    [unstore_unit]
        variable=delfador_store
        find_vacant=yes
        x={X}
        y={Y}
    [/unstore_unit]

    # Force the fog to clear
    [redraw][/redraw]

    # He's making a dramatic entrance.
    [scroll_to]
        x,y = {X},{Y}
    [/scroll_to]

    # Delfador might have been wounded before he was stored. Full-heal him.
    {FULL_HEAL id=Delfador}

    {CLEAR_VARIABLE delfador_store}

    # FIXME: Relies on an implementation accident.
    # This sequence of declarations happens to put the units where we want
    # them - Shock Troopers and Royal Guard nortb of Delfador, towards the
    # enemy, and the White Mages behind him - but this is an accident of the
    # vacancy-finding algorithm.  Ideally we want to use formula syntax to
    # say things like x,y=$({X}-1),$({Y}+1) below, but  that doesn't work
    # reliably yet in 1.7.0 (units randomly fail to appear when it's tried).
    [unit]
        id=Clogrin
        name=_ "Clogrin"
        type=Royal Guard
        side=1
        x={X}
        y={Y}
    [/unit]
    [unit]
        id=Theorsten
        name=_ "Theorsten"
        type=White Mage
        side=1
        x={X}
        y={Y}
    [/unit]
    [unit]
        id=Igler
        name=_ "Igler"
        type=White Mage
        side=1
        x={X}
        y={Y}
    [/unit]
    [unit]
        id=Larsin
        name=_ "Larsin"
        type=White Mage
        side=1
        x={X}
        y={Y}
    [/unit]
    [unit]
        id=Aldrin
        name=_ "Aldrin"
        type=White Mage
        side=1
        x={X}
        y={Y}
    [/unit]
    [unit]
        id=Gren
        name=_ "Gren"
        type=Shock Trooper
        side=1
        x={X}
        y={Y}
    [/unit]
    [unit]
        id=Droni
        name=_ "Droni"
        type=Shock Trooper
        side=1
        x={X}
        y={Y}
    [/unit]
    [unit]
        id=Artor
        name=_ "Artor"
        type=Shock Trooper
        side=1
        x={X}
        y={Y}
    [/unit]
#enddef

# At the end of Shadows (14), control of side 1 needs to be handed
# back to Delfador. He gets his elvish veterans back, but he's not
# Kalenz (e.g current or former High Lord of the Elves) so he can't
# recruit L2s.  This macro will be used in Prince of Wesnoth (20) as
# well.

#define DELFADOR_TAKES_CONTROL
    {MODIFY_UNIT id=Kalenz   canrecruit no }
    {MODIFY_UNIT id=Delfador canrecruit yes}
    {MEMOIRS_UNSTORE_UNITS delfador_elvish_veterans}
    [disallow_recruit]
        side=1
        type={ELVES2}
    [/disallow_recruit]
#enddef

# At the end of Save the Prince (15), Delfador loses Kalenz, Chantal, his
# Elvish veterans, and the ability to recruit elves.  This is as it
# should be; he's returning to human lands. He also loses his rescue
# force of Wesnothians.

#define ELVES_DEPART
    #[kill]
    #    id=Delfador
    #[/kill]
    [kill]
        id=Clogrin
    [/kill]
    [kill]
        id=Aldrin
    [/kill]
    [kill]
        id=Theorsten
    [/kill]
    [kill]
        id=Igler
    [/kill]
    [kill]
        id=Larsin
    [/kill]
    [kill]
        id=Artos
    [/kill]
    [kill]
        id=Droni
    [/kill]
    [kill]
        id=Glen
    [/kill]

    # now store elvish NPCs
    [store_unit]
        variable=chantal_store
        kill=yes
        [filter]
            id=Chantal
        [/filter]
    [/store_unit]
    [store_unit]
        variable=kalenz_store
        kill=yes
        [filter]
            id=Kalenz
        [/filter]
    [/store_unit]
    # store elves (to reappear later)
    {MEMOIRS_STORE_UNITS delfador_elvish_veterans}
    [disallow_recruit]
        side=1
        type={ELVES}
    [/disallow_recruit]
#enddef

# Delfador returns to Weldyn in The King's Council (16).  This is when he
# plugs back into the Wesnothian power structure, though the recruiting
# consequences are delayed because 16 is talk-only.
#
# At the beginning of A New Ally (17), Delfador gets back the
# Wesnothians (his original recruit list), plus the human survivors
# from Terror at the Ford of Parthyn (12). However we don't bring back
# Lionel, the cutscene in Dark Skies over Weldyn (16) has him
# commanding the defenses of Weldyn.
#

#define RESTORE_WESNOTHIAN_VETERANS
    {MEMOIRS_UNSTORE_UNITS delfador_lionels_guard}
    {MEMOIRS_UNSTORE_UNITS delfador_human_veterans}
#enddef

# At the end of A New Ally (17), the surviving Dwarvish units (and Ulrek)
# follow Delfador, going on his (human) recall list.

#define DWARVES_FOLLOW_DELFADOR
    # Relgorn departs for Knalga
    [kill]
        id=Relgorn
        fire_event=no
    [/kill]
    # Ulrek and the rest of the dwarves accompany Delfador
    {MODIFY_UNIT (id=Ulrek) canrecruit no}
    {MODIFY_UNIT (side=2) side 1}
#enddef

# At the beginning of Portal of Doom (18), Delf acquires the ability to
# recruit dwarves via Ulrek.  (Should be called from the prestart event.)

#define DELFADOR_GETS_DWARVES
    [allow_recruit]
        side=1
        type={DWARVES},{DWARVES2}
    [/allow_recruit]
#enddef

# At the beginning of Showdown in the Northern Swamp (19), Kalenz
# rejoins Delfador.  Chantal is not here for this one.

#define KALENZ_REJOINS_DELFADOR X Y
    [unstore_unit]
        variable=kalenz_store
        find_vacant=yes
        x={X}
        y={Y}
    [/unstore_unit]
    # Delfador regains the ability to recruit L1 elves through Kalenz
    # This is mostly for storyline purpose, as Delfador will almost
    # certainly already have better units for this sort of battle.
    [allow_recruit]
        side=1
        type={ELVES}
    [/allow_recruit]
    # FIXME: Should side 1 get Kalenz's veterans or ability to recruit L2s?
#enddef

# Also at the beginning of Showdown in the Northern Swamp (19),
# any undead veterans Delfador has left from the Houses of the Dead
# and The Gate Between Worlds scenarios return to fight for the living.
# They get finally dispelled at the end of the scenario.

#define UNDEAD_REJOIN_DELFADOR
    {MEMOIRS_UNSTORE_UNITS dead_recall}
#enddef

# Shortly afterwards, Lionel puts his troops under Delfador's command.
# We'll use SUBSUME_SIDE_AND_VILLAGES for this.

# At the end of Showdown in the Northern Swamp (19), Delfador departs
# the scene: Kalenz will lead side 1 in the next scenario, 26 years
# later.  This is preparation.
#define DELFADOR_DEPARTS_AGAIN
    # He's not going to be where he can recruit dwarves again.
    [disallow_recruit]
        side=1
        type={DWARVES},{DWARVES2}
    [/disallow_recruit]
    # Delfador goes away
    [store_unit]
        variable=delfador_store
        kill=yes
        [filter]
            id=Delfador
        [/filter]
    [/store_unit]
    # Delfador dismissed his undead veterans
    {CLEAR_VARIABLE dead_recall}
    # It would be nice to let Delfador keep his other veterans, but that
    # would stretch the storyline a bit far.  Human veterans will get
    # old; dwarves have longer lifespans, but letting Delfador have
    # them back would imply that dwarves had been following him around
    # *in Wesnoth* for 26 years.  Revisit this if we let Delfador
    # have his elvish veterans for this battle.
    {MEMOIRS_STORE_UNITS wesnoth_victors}
    {CLEAR_VARIABLE wesnoth_victors}
    # Kalenz will lead in next scenario
    {MODIFY_UNIT id=Kalenz canrecruit yes}
#enddef

# At the beginning of Prince of Wesnoth (20) Kalenz is running
# things again.  Delfador will make another entrance, but without the ability
# to recruit loyalists and mages (he's going against Prince Eldred)
# and gets back the Elvish recall list. Chantal reappears as well.

#define DELFADOR_JOINS_ELVES
    [unstore_unit]
        variable=chantal_store
        find_vacant=yes
        x=4
        y=18
    [/unstore_unit]
    {MEMOIRS_UNSTORE_UNITS delfador_elvish_veterans}
    {FULL_HEAL side=1}
    #Lowel and Eldred are the enemy humans when this fires
    [kill]
        race=human
        [not]
            id=Lowel
            [or]
                id=Eldred
            [/or]
        [/not]
    [/kill]
    [disallow_recruit]
        side=1
        type={LOYALISTS}
    [/disallow_recruit]
    [disallow_recruit]
        side=1
        type=Mage, Arch Mage, Great Mage, Silver Mage, White Mage, Red Mage
    [/disallow_recruit]
#enddef

# At the end of Prince of Wesnoth (20), Delfador intervenes.

#define DELFADOR_ARRIVES X Y
    [unstore_unit]
        variable=delfador_store
        find_vacant=yes
        x,y={X},{Y}
    [/unstore_unit]
    {CLEAR_VARIABLE delfador_store}
    {FULL_HEAL id=Delfador}
    [redraw]
        side=1
    [/redraw]
#enddef

# At the end of Prince of Wesnoth (20), Delfador gets control of side 1 again.
# We reuse the macro from scenario 14.

# At the beginning of Clash at the Manor (21), our favorite Elvish NPCs
# reappear. From this point onward Delfador recruits only elves.

#define ELVISH_NPCS_REAPPEAR
    #wmllint: validate-off
    [unit]
        id=Kalenz
        type=Elvish High Lord
        placement=leader
    [/unit]

    [unit]
        id=Chantal
        type=Elvish Shyde
        placement=leader
    [/unit]
    #wmllint: validate-on
#enddef

# We're done.
