#textdomain wesnoth-httt

################################
#
# HTTT MACROS FOLLOW
#
################################

#
# Portrait variation macros
#

#define KONRAD_VARIATION NAME
    image=portraits/konrad-human-{NAME}.webp
#enddef

#define KONRAD_VARIATION_ELF NAME
    image=portraits/konrad-elvish-{NAME}.webp
#enddef

#define DELFADOR_VARIATION NAME
    image=portraits/delfador-{NAME}.webp
#enddef

#define DELFADOR_VARIATION_ELF NAME
    image=portraits/delfador-elvish-{NAME}.webp
#enddef

#define DELFADOR_MENTORING
    image=portraits/delfador-mentoring.webp
#enddef

#define DELFADOR_MENTORING_ELF
    image=portraits/delfador-elvish-mentoring.webp
#enddef

#define LISAR_VARIATION NAME
    image=portraits/lisar-{NAME}.webp
#enddef

#define ASHEVIERE_VARIATION NAME
    image=portraits/asheviere-{NAME}.webp
#enddef

#
# HTTT-specific artifacts
#

#define OBJ_SWORD_FIRE X Y ID

    # Give a hint the first time a unit stops in a hex near the sword.
    # There is a village right there, so that's probably when it will occur,
    # but anywhere nearby will do.

    [event]
        name=moveto
        [filter]
            side=1
        [/filter]
        [filter_condition]
            [have_location]
                x,y=$x1,$y1
                [and]
                    x,y={X},{Y}
                    radius=2
                [/and]
                [not]
                    x,y={X},{Y}
                [/not]
            [/have_location]
        [/filter_condition]

        [message]
            speaker=narrator
            image="wesnoth-icon.png"
            message= _ "Nearby is a clearing with a huge tree standing alone in the center."
        [/message]
    [/event]

    [event]
        name=moveto

        [filter]
            side=1
            x={X}
            y={Y}
        [/filter]

        [message]
            speaker=narrator
            image="wesnoth-icon.png"
            message= _ "A large tree stands alone in a clearing. Driven into the trunk is a huge sword with flames dancing along its blade. Strangely, while the trunk near the sword is blackened and scorched, the tree seems otherwise unaffected by the flames."
        [/message]
        {PLACE_IMAGE items/flame-sword.png ({X}) ({Y})}
        {VARIABLE sword_taken 0}
        [event]
            name=victory
            {CLEAR_VARIABLE sword_taken}
        [/event]
    [/event]
    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=1
            x={X}
            y={Y}
        [/filter]
        [if]
            [variable]
                name=sword_taken
                numerical_equals=0
            [/variable]
            [then]
                [message]
                    speaker=$unit.id|
                    caption=""
                    image="wesnoth-icon.png"
                    male_message= _ "Should $unit.name| attempt to pick up the sword?"
                    female_message= _ "female^Should $unit.name| attempt to pick up the sword?"
                    [option]
                        label= _ "Yes"
                        [command]
                            [object]
                                id={ID}
                                name= _ "Flaming Sword"
                                image=attacks/sword-flaming.png
                                duration=forever
                                # wmllint: local spelling forgemasters
                                description= _ "This massive blade was created centuries ago by long-forgotten elvish forgemasters, who imbued the bluish steel with an inner magical fire. Tongues of flame dance on the surface, giving the metal a flawless mirrored finish."
                                cannot_use_message= _ "Only the leader of an army can wield this sword!"
                                [filter]
                                    type_adv_tree=Fighter,Princess,Elvish Captain,Elvish Hero,Paladin,Elvish Lord
                                    x,y={X},{Y}
                                [/filter]
                                [then]
                                    [remove_item]
                                        x,y={X},{Y}
                                    [/remove_item]
                                    [message]
                                        speaker=$unit.id|
                                        image="wesnoth-icon.png"
                                        caption=""
                                        male_message= _ "As $unit.name| places his hand around the glittering leather hilt, the sword roars to life! Strangely, he feels no heat once he picks it up, yet the grass at his feet bursts into flame as he tests the heft of this mighty weapon."
                                        female_message= _ "As $unit.name| places her hand around the glittering leather hilt, the sword roars to life! Strangely, she feels no heat once she picks it up, yet the grass at her feet bursts into flame as she tests the heft of this mighty weapon."
                                    [/message]
                                    {VARIABLE sword_taken 1}
                                [/then]
                                [effect]
                                    apply_to=attack
                                    name=sword,saber
                                    set_description= _ "flaming sword"
                                    set_icon=attacks/sword-flaming.png
                                    set_type=fire
                                    [set_specials]
                                        mode=append
                                        {WEAPON_SPECIAL_MAGICAL}
                                    [/set_specials]
                                    increase_damage=25%
                                [/effect]
                            [/object]
                        [/command]
                    [/option]

                    [option]
                        label= _ "No"

                        [command]
                            [allow_undo]
                            [/allow_undo]
                        [/command]
                    [/option]
                [/message]
            [/then]

            [else]
                [allow_undo]
                [/allow_undo]
            [/else]
        [/if]
    [/event]
#enddef

#define OBJ_VOID_ARMOR X Y ID
    {PLACE_IMAGE items/armor-golden.png ({X}) ({Y})}
    {VARIABLE armor_taken 0}
    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=1
            x,y={X},{Y}
        [/filter]

        [if]
            [variable]
                name=armor_taken
                numerical_equals=0
            [/variable]
            [then]
                [message]
                    speaker=$unit.id|
                    caption=""
                    image="wesnoth-icon.png"
                    male_message= _ "Should $unit.name| attempt to pick up the armor?"
                    female_message= _ "female^Should $unit.name| attempt to pick up the armor?"
                    [option]
                        label= _ "Yes"
                        [command]
                            [object]
                                id={ID}
                                name= _ "Void Armor"
                                image="icons/breastplate2.png"
                                description= _ "A beautiful chest plate crafted from shimmering black steel, the Void Armor is virtually impenetrable to physical weapons!"
                                cannot_use_message= _ "Only a powerful warrior may don this armor!"
                                duration=forever
                                [filter]
                                    side=1
                                    x,y={X},{Y}
                                    type_adv_tree=Fighter,Princess,Elvish Fighter,Elvish Lord,Elvish Ranger,Horseman,Dwarvish Fighter,Dwarvish Guardsman
                                [/filter]
                                [then]
                                    [remove_item]
                                        x,y={X},{Y}
                                    [/remove_item]
                                    [message]
                                        speaker=$unit.id|
                                        image="wesnoth-icon.png"
                                        caption=""
                                        male_message= _ "$unit.name| struggles to lift and don the heavy plate. Once worn, however, it is amazingly comfortable. He feels an increased resistance to all physical damage!"
                                        female_message= _ "$unit.name| struggles to lift and don the heavy plate. Once worn, however, it is amazingly comfortable. She feels an increased resistance to all physical damage!"
                                    [/message]
                                    {VARIABLE armor_taken 1}

                                    [modify_unit]
                                        [filter]
                                            x,y={X},{Y}
                                        [/filter]

                                        [trait]
                                            id=void_armor
                                            name= _ "void armor"
                                            description= _ "The Void Armor grants the following resistances:
blade:  50%
pierce: 40%
impact: 40%
fire:  +10%"
                                        [/trait]
                                    [/modify_unit]
                                [/then]
                                #
                                # Set physical resistance to 50/40/40
                                #
                                [effect]
                                    apply_to=resistance
                                    replace=yes
                                    [resistance]
                                        blade=50
                                        pierce=60
                                        impact=60
                                    [/resistance]
                                [/effect]
                                #
                                # Increase fire resistance by 10%
                                #
                                [effect]
                                    apply_to=resistance
                                    [resistance]
                                        fire=-10
                                    [/resistance]
                                [/effect]
                            [/object]
                        [/command]
                    [/option]
                    [option]
                        label= _ "No"
                    [/option]
                [/message]
            [/then]
        [/if]
    [/event]
#enddef

################################
#
# SCEPTRE OF FIRE MACROS FOLLOW
#
################################

#define SOF_TERRAIN_MASK
    [terrain_mask]
        x,y=1,1
        mask="{campaigns/Heir_To_The_Throne/masks/17_Scepter_of_Fire.mask}"

        [rule]      # Make sure your castle keep and cave walls are not converted
            old=Cud, Xu, Kud
            new=Re, Uu^Vud, Uh, Uu^Tf
            use_old=yes
        [/rule]
        [rule]      # Convert cave villages to stone villages
            old=Vu
            new=Re, Uu^Vud, Uh, Uu^Tf   # wmllint: ignore
            terrain=Uu^Vud
        [/rule]
        [rule]      # Add stone villages
            old=Uu
            new=Uu^Vud
            terrain=Uu^Vud
        [/rule]
        [rule]      # Add roads
            old=Uu
            new=Re
            terrain=Re
        [/rule]
        [rule]      # Add hilly caves
            old=Uu
            new=Uh
            terrain=Uh
        [/rule]
        [rule]      # Add a mushroom forest
            old=Uu
            new=Uu^Tf
            terrain=Uu^Tf
        [/rule]
    [/terrain_mask]
#enddef

#define PLACE_SCEPTRE
    [event]
        name=prestart
        [item]
            location_id="sceptre"
            image=items/sceptre-of-fire.png
        [/item]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=1
            [filter_location]
                location_id="sceptre"
            [/filter_location]
        [/filter]
        [object]
            id=object_sceptre
            name= _ "Sceptre of Fire"
            image=items/sceptre-of-fire.png
            duration=forever
            description= _ "This ancient Sceptre was forged by the great Dwarves of the Heart Mountains. A symbol of the kingship of Wesnoth, the Sceptre has the power to shoot fireballs at enemies of the bearer!"
            cannot_use_message= _ "This is the Sceptre of Fire. Only a true successor to the throne can possibly dare to take this!"
            [filter]
                type_adv_tree=Fighter,Princess
                x,y=$x1,$y1
            [/filter]
            [effect]
                apply_to=variation
                name=sceptre
            [/effect]
        [/object]
    [/event]
#enddef

#define KONRAD_GETS_SCEPTRE
    [event]
        name=moveto
        [filter]
            id=Konrad
            [filter_location]
                location_id="sceptre"
            [/filter_location]
        [/filter]
        [set_variable]
            name=sceptre
            value="Konrad"
        [/set_variable]

        {MODIFY_UNIT id=Konrad profile "portraits/konrad-human-sceptre.webp"}

        [message]
            speaker=Konrad
            message= _ "Here it is at last, I have the Sceptre!"
        [/message]
        [message]
            speaker=Kalenz
            message= _ "So it is in our hands! Now let us leave this stinking pit."
        [/message]
        [message]
            speaker="Li'sar"
            message= _ "I think that if we travel just a little north, we might be able to get out."
        [/message]
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]
#enddef

#define LISAR_GETS_SCEPTRE
    [event]
        name=moveto
        [filter]
            id="Li'sar"
            [filter_location]
                location_id="sceptre"
            [/filter_location]
        [/filter]
        [set_variable]
            name=sceptre
            value="Li'sar"
        [/set_variable]

        {MODIFY_UNIT id="Li'sar" profile "portraits/lisar-sceptre.webp"}

        [message]
            speaker="Li'sar"
            message= _ "At last! I have the Sceptre!"
        [/message]
        [message]
            speaker=Konrad
            message= _ "Indeed. You managed to reach it, Li’sar. I hope you shall use it wisely."
        [/message]
        [message]
            speaker="Li'sar"
            message= _ "My first use for it is going to be to help us get out of this hole! I hope you consider that wise."
        [/message]
        [message]
            speaker=Delfador
            {DELFADOR_MENTORING}
            message= _ "The Sceptre makes its wielder powerful, but hardly immortal, child. Use it prudently. Now come, I believe there is an exit to the north!"
        [/message]
        [message]
            speaker="Li'sar"
            message= _ "I think I know what I’m doing. Come, let us go!"
        [/message]

        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]
#enddef

#define PASSAGE_NORMAL DESTINATION_NAME WIDTH_NUMBER WINDINESS_NUMBER JAGGED_NUMBER
    [passage]
        destination={DESTINATION_NAME}
        width={WIDTH_NUMBER}
        windiness={WINDINESS_NUMBER}
        jagged={JAGGED_NUMBER}
    [/passage]
#enddef

#define PASSAGE_CHANCE CHANCE_NUMBER DESTINATION_NAME WIDTH_NUMBER WINDINESS_NUMBER JAGGED_NUMBER
    [passage]
        chance={CHANCE_NUMBER}
        destination={DESTINATION_NAME}
        width={WIDTH_NUMBER}
        windiness={WINDINESS_NUMBER}
        jagged={JAGGED_NUMBER}
    [/passage]
#enddef

# Unused, but left as-is for a change that backports to 1.18.
#define ERASE_CASTLE SIDE TERRAIN
    [store_unit]
        variable=side_store
        [filter]
            side={SIDE}
            canrecruit=yes
        [/filter]
    [/store_unit]
    {VARIABLE min_x $side_store.x}
    {VARIABLE_OP min_x sub 1}
    {VARIABLE min_y $side_store.y}
    {VARIABLE_OP min_y sub 1}
    {VARIABLE max_x $side_store.x}
    {VARIABLE_OP max_x add 1}
    {VARIABLE max_y $side_store.y}
    {VARIABLE_OP max_y add 1}
    {VARIABLE x_range ("$min_x|-$max_x")}
    {VARIABLE y_range ("$min_y|-$max_y")}
    [terrain]
        x=$x_range
        y=$y_range
        terrain={TERRAIN}
    [/terrain]
    {CLEAR_VARIABLE side_store,min_x,min_y,max_x,max_y,x_range,y_range}
#enddef

#textdomain wesnoth-help
#define UNDERGROUND_VOLCANO RED GREEN BLUE
    #
    # -30, -40, -40 are the default good values for an underground
    # reddish hue
    #
    [time]
        id=underground
        name= _ "Underground"
        image=misc/time-schedules/schedule-underground.png
        lawful_bonus=-25
        red={RED}
        green={GREEN}
        blue={BLUE}
    [/time]
#enddef

#textdomain wesnoth-httt
#define NEXT_LAVA
    [store_locations]
        variable=potential_locs
        terrain=Xu
        [filter_adjacent_location]
            terrain=U*, Re
            [filter_adjacent_location]
                terrain=U*, Re
                count=4-5
            [/filter_adjacent_location]
        [/filter_adjacent_location]
        [not]
            # don't start near the cave floor around the sceptre
            location_id=sceptre
            radius=5
        [/not]
        [not]
            # don't start near the existing lava
            terrain=Ql
            radius=5
        [/not]
    [/store_locations]
    [if]
        [variable]
            name=potential_locs.length
            greater_than=0
        [/variable]
        [then]
            {VARIABLE lava_count 0}
            {VARIABLE randrange $potential_locs.length}
            {VARIABLE_OP randrange sub 1}
            {VARIABLE randrange "0..$randrange"}
            {RANDOM $randrange}
            {CLEAR_VARIABLE lava_body}
            {VARIABLE lava_body.x $potential_locs[$random].x}
            {VARIABLE lava_body.y $potential_locs[$random].y}
            {CLEAR_VARIABLE random}
            [terrain]
                x=$lava_body.x
                y=$lava_body.y
                terrain=Ql
            [/terrain]
            {CLEAR_VARIABLE randrange}
        [/then]
    [/if]
    {CLEAR_VARIABLE potential_locs}
#enddef

#define EXPAND_LAVA
    # the lava should expand to any cave floor that is not next to a cavewall,
    # or cave floor that is next to cavewall that is already next to lava
    # also if it comes near another lavabody, it should not expand in that direction

    # first we store cavewall that is not already next to this lava
    [store_locations]
        variable=far_walls
        terrain=Xu
        [not]
            find_in=lava_body
            radius=1
        [/not]
    [/store_locations]

    # next we store lava that is not part of this body
    [store_locations]
        variable=far_lava
        terrain=Ql
        [not]
            find_in=lava_body
        [/not]
    [/store_locations]

    # now we store cave floor nearby that doesn't approach the bad stuff
    [store_locations]
        variable=good_floor
        terrain=U*,Re
        [and]
            find_in=lava_body
            radius=1
        [/and]
        [not]
            find_in=far_walls
            radius=1
        [/not]
        [not]
            find_in=far_lava
            radius=1
        [/not]
        [not]
            # don't go anywhere near the cave floor around the sceptre
            location_id=sceptre
            radius=4
            [filter_radius]
                terrain=U*,Re
            [/filter_radius]
        [/not]
    [/store_locations]

    [if]
        [variable]
            name=good_floor.length
            greater_than=0
        [/variable]
        [then]
            [foreach]
                array=good_floor
                [do]
                    [terrain]
                        x=$this_item.x
                        y=$this_item.y
                        terrain=Ql
                    [/terrain]
                [/do]
            [/foreach]
            [store_locations]
                variable=lava_body
                find_in=lava_body
                [or]
                    find_in=good_floor
                [/or]
            [/store_locations]
        [/then]
        [else]
            # this lava can't expand so force a new eruption
            {NEXT_LAVA}
        [/else]
    [/if]
    {CLEAR_VARIABLE far_walls}
    {CLEAR_VARIABLE far_lava}
    {CLEAR_VARIABLE good_floor}

    # everyone on lava dies
    [kill]
        [not]
            type_adv_tree=Elvish Shyde,Elvish Sylph,Gryphon Rider
        [/not]
        [filter_location]
            terrain=Ql
        [/filter_location]
        animate=yes
        fire_event=yes
    [/kill]
#enddef

#define WATERFALL_MASK X Y
    [item]
        image=projectiles/icemissile-n-6.png
        x={X}
        y={Y}
    [/item]
    [item]
        image=projectiles/icemissile-n-5.png
        x={X}
        y={Y}
    [/item]
    [item]
        image=projectiles/icemissile-n-4.png
        x={X}
        y={Y}
    [/item]
    [item]
        image=projectiles/icemissile-n-3.png
        x={X}
        y={Y}
    [/item]
    [item]
        image=halo/elven/nature-halo1.png
        x={X}
        y={Y}
    [/item]
#enddef

#define CHECK_VARIABLE NAME VALUE
    [variable]
        name={NAME}
        equals={VALUE}
    [/variable]
#enddef

#define NEED_DELFADOR PLACEMENT
    [unit]
        id=Delfador
        name= _ "Delfador"
        side=1
        profile=portraits/delfador-elvish.webp~RIGHT()
        unrenamable=yes
        type=Elder Mage
        {PLACEMENT}
        [modifications]
            {TRAIT_LOYAL_HERO}
            {TRAIT_INTELLIGENT}
        [/modifications]
    [/unit]
#enddef

#define NEED_KALENZ PLACEMENT
    [unit]
        id=Kalenz
        name= _ "Kalenz"
        side=1
        profile=portraits/kalenz.webp
        unrenamable=yes
        type=Elvish Lord
        {PLACEMENT}
        random_traits=no
        [modifications]
            {TRAIT_LOYAL_HERO}
            {TRAIT_RESILIENT}
            {TRAIT_QUICK}
        [/modifications]
    [/unit]
#enddef

#define NEED_LISAR PLACEMENT
    [unit]
        id="Li'sar"
        name= _ "Li’sar"
        side=1
        profile=portraits/lisar.webp
        unrenamable=yes
        type=Princess
        {PLACEMENT}
        random_traits=no
        [modifications]
            {TRAIT_LOYAL_HERO}
        [/modifications]
    [/unit]
#enddef

#define STORE_DELFADOR
    [if]
        [have_unit]
            id=Delfador
            search_recall_list=yes
        [/have_unit]
        [then]
            [store_unit]
                [filter]
                    id=Delfador
                [/filter]
                kill=yes
                variable=delfador_store
            [/store_unit]
        [/then]
    [/if]
#enddef

#define RESTORE_DELFADOR
    [if]
        [variable]
            name=delfador_store.id
            equals="Delfador"
        [/variable]
        [then]
            [unstore_unit]
                variable=delfador_store
                x,y=recall,recall
            [/unstore_unit]
            {CLEAR_VARIABLE delfador_store}
        [/then]
    [/if]
#enddef

#define STORE_KALENZ
    [if]
        [have_unit]
            id=Kalenz
            search_recall_list=yes
        [/have_unit]
        [then]
            [store_unit]
                [filter]
                    id=Kalenz
                [/filter]
                kill=yes
                variable=kalenz_store
            [/store_unit]
        [/then]
    [/if]
#enddef

#define RESTORE_KALENZ
    [if]
        [variable]
            name=kalenz_store.id
            equals="Kalenz"
        [/variable]
        [then]
            [unstore_unit]
                variable=kalenz_store
                x,y=recall,recall
            [/unstore_unit]
            {CLEAR_VARIABLE kalenz_store}
        [/then]
    [/if]
#enddef

#define STORE_LISAR
    [if]
        [have_unit]
            side=1
            id="Li'sar"
            search_recall_list=yes
        [/have_unit]
        [then]
            [store_unit]
                [filter]
                    side=1
                    id="Li'sar"
                [/filter]
                kill=yes
                variable=lisar_store
            [/store_unit]
        [/then]
    [/if]
#enddef

#define RESTORE_LISAR
    [if]
        [variable]
            name=lisar_store.id
            equals="Li'sar"
        [/variable]
        [then]
            [unstore_unit]
                variable=lisar_store
                x,y=recall,recall
            [/unstore_unit]
            {CLEAR_VARIABLE lisar_store}
        [/then]
    [/if]
#enddef

#define NARRATOR_MESSAGE MSG
    [message]
        speaker=narrator
        image="wesnoth-icon.png"
        message= {MSG}
    [/message]
#enddef
