#textdomain wesnoth-sof

#define SOF_RUNIC_OPTION ID NAME DESC ICON COST EFFECT_WML
    image={ICON}
    cost={COST}
    label= {NAME} + " <span style='italic'> (" + _"$cost|g" + ")</span>"
    description = {DESC}
    [command]
        [if]
            [variable]
                name=unit.variables.{ID}_rune
                equals=yes
            [/variable]
            [then]
                [message]
                    speaker=unit
                    message=_"I already have this one..."
                [/message]
            [/then]
            [else]
                [store_gold]
                    side=1
                    variable=gold_check
                [/store_gold]
                [if]
                    [variable]
                        name=gold_check
                        less_than={COST}
                    [/variable]
                    [then]
                        [message]
                            speaker=unit
                            message=_"We don’t have the funds..."
                        [/message]
                        [set_variable]
                            name=no_funds
                            value=yes
                        [/set_variable]
                    [/then]
                    [else]
                        [set_variable]
                            name=unit.variables.{ID}_rune
                            value=yes
                        [/set_variable]
                        [unstore_unit]
                            variable=unit
                            find_vacant=no
                        [/unstore_unit]
                        [gold]
                            side=1
                            amount=-{COST}
                        [/gold]
                        [object]
                            silent=yes
                            duration=scenario
                            [filter]
                                id=$unit.id
                            [/filter]
                            [effect]
                                apply_to=overlay
                                add=misc/rune_overlay.png
                            [/effect]
                            {EFFECT_WML}
                        [/object]
                    [/else]
                [/if]
                {CLEAR_VARIABLE gold_check}
            [/else]
        [/if]
    [/command]
#enddef

# thursagan moves to keep, or starts side turn at a keep, then
# adjacent units are stored and player can choose which one gets a rune

#define SOF_RUNIC_EVENTS
    [event]
        name=moveto
        id=sof_sfe_moveto
        first_time_only=no
        [filter]
            id=Thursagan
            [filter_location]
                terrain=K*
            [/filter_location]
        [/filter]
        [store_unit]
            [filter]
                side=1
                race=dwarf
                [filter_adjacent]
                    id=Thursagan
                [/filter_adjacent]
            [/filter]
            variable=rune_customer
            kill=no
        [/store_unit]
        [fire_event]
            id=sof_rune_shop
        [/fire_event]
    [/event]
    [event]
        name=moveto
        id=sof_rune_shop_flavor_text
        first_time_only=yes
        [filter]
            id=Thursagan
            [filter_location]
                terrain=K*
            [/filter_location]
        [/filter]
        [message]
            speaker=Thursagan
            message= _ "My tools are ready. Step closer, if you want to witness my rune-work."
        [/message]
    [/event]
    [event]
        name=side 1 turn
        id=sof_sfe_side1turn
        first_time_only=no
        [filter_condition]
            [have_unit]
                id=Thursagan
                [filter_location]
                    terrain=K*
                [/filter_location]
            [/have_unit]
        [/filter_condition]
        [store_unit]
            [filter]
                side=1
                race=dwarf
                [filter_adjacent]
                    id=Thursagan
                [/filter_adjacent]
            [/filter]
            variable=rune_customer
            kill=no
        [/store_unit]
        [fire_event]
            id=sof_rune_shop
        [/fire_event]
    [/event]
    [event]
        name=exit_hex
        id=sof_thursagan_exithex
        first_time_only=no
        [filter]
            id=Thursagan
            [filter_location]
                terrain=K*
            [/filter_location]
        [/filter]
        [remove_item]
            image=rune_shop_overlay
            x,y=$x1|,$y1|
            radius=1
        [/remove_item]
        {CLEAR_VARIABLE rune_customer}
    [/event]
    [event]
        name=exit_hex
        id=sof_sfe_exithex
        first_time_only=no
        [filter]
            side=1
            race=dwarf
            [filter_adjacent]
                id=Thursagan
                [filter_location]
                    terrain=K*
                [/filter_location]
            [/filter_adjacent]
        [/filter]
        [remove_item]
            image=rune_shop_overlay
            x,y=$x1|,$y1|
        [/remove_item]
        [store_unit]
            [filter]
                id=$unit.id
            [/filter]
            variable=ex_rune_customer
            kill=no
        [/store_unit]
        [foreach]
            array=rune_customer
            readonly=yes
            [do]
                [if]
                    [variable]
                        name=ex_rune_customer.id
                        equals=$this_item.id
                    [/variable]
                    [then]
                        [set_variable]
                            name=index_TEMP
                            value=$i|
                        [/set_variable]
                    [/then]
                [/if]
            [/do]
        [/foreach]
        {CLEAR_VARIABLE rune_customer[$index_TEMP]}
        {CLEAR_VARIABLE ex_rune_customer}
        {CLEAR_VARIABLE index_TEMP}
    [/event]
    [event]
        name=moveto
        id=sof_sfe_2moveto
        first_time_only=no
        [filter]
            side=1
            race=dwarf
            [filter_adjacent]
                id=Thursagan
                [filter_location]
                    terrain=K*
                [/filter_location]
            [/filter_adjacent]
        [/filter]
        [store_unit]
            [filter]
                id=$unit.id
            [/filter]
            variable=rune_customer
            kill=no
            mode=append
        [/store_unit]
        [fire_event]
            id=sof_rune_shop
        [/fire_event]
    [/event]

    [event]
        id=sof_rune_shop
        name=sof_rune_shop
        first_time_only=no
        [remove_item]
            [and]
                terrain=K*
                [filter]
                    id=Thursagan
                [/filter]
            [/and]
            radius=1
            image=rune_shop_overlay
        [/remove_item]
        {CLEAR_VARIABLE abort_select_id}
        [foreach]
            array=rune_customer
            readonly=yes
            [do]
                [if]
                    [variable]
                        name=this_item.variables.no_rune
                        equals=yes
                    [/variable]
                    [else]
                        [item]
                            halo="scenery/summoning-circle[4,5,6].png:[250,250,250]"
                            x,y=$this_item.x|,$this_item.y|
                            name=rune_shop_overlay
                        [/item]
                    [/else]
                [/if]
            [/do]
        [/foreach]
        [event]
            name=select
            id=rc_select
            first_time_only=no
            [filter_condition]
                [not]
                    [variable]
                        name=unit.variables.no_rune
                        equals=yes
                    [/variable]
                [/not]
            [/filter_condition]
            [filter]
                find_in=rune_customer
            [/filter]
            [rune_choice]
                speaker=Thursagan
                message= _ "I can hammer out a quick little runic charm, it should help you somewhat. This isn’t the place to do really good work though, so the effects will vanish in the future. What do you say, is it worth a bit of gold and crystal?"
                [option]
                    default=yes
                    image=attacks/blank-attack.png~BLIT(icons/unit-groups/cross_30.png~SCALE(60,60),0,0)
                    label= _ "Exit"
                    description = _ "Nothing for now."
                    [command]
                    [/command]
                [/option]
                [option]
                    default=yes
                    image=attacks/blank-attack.png~BLIT(icons/unit-groups/cross_30.png~BLEND(200,0,0,1.0)~SCALE(60,60),0,0)
                    label= _ "Reject"
                    description = _ "No thanks, don’t ask again."
                    [command]
                        [set_variable]
                            name=unit.variables.no_rune
                            value=yes
                        [/set_variable]
                        [unstore_unit]
                            variable=unit
                            find_vacant=no
                        [/unstore_unit]
                        [remove_item]
                            x,y=$x1|,$y1|
                            image=rune_shop_overlay
                        [/remove_item]
                    [/command]
                [/option]
                [option]
                    {SOF_RUNIC_OPTION swiftness (_ "Swiftness") (_ "Adds 2 MP") ("misc/rune_icon.png~BLIT(scenery/summoning-circle4.png~CROP(6,6,60,60),0,0)") 13 (
                        [effect]
                            apply_to=movement
                            increase=2
                        [/effect]
                    )}
                [/option]
                [option]
                    {SOF_RUNIC_OPTION accuracy (_ "Accuracy") (_ "Increases ranged weapon accuracy by 20%") ("misc/rune_icon.png~BLIT(scenery/summoning-circle5.png~CROP(6,6,60,60),0,0)") 16 (
                        [effect]
                            apply_to=attack
                            range=ranged
                            increase_accuracy=20
                        [/effect]
                    )}
                [/option]
                [option]
                    {SOF_RUNIC_OPTION "force" (_ "Force") (_ "Increases melee weapon damage by 4") ("misc/rune_icon.png~BLIT(scenery/summoning-circle6.png~CROP(6,6,60,60),0,0)") 14 (
                        [effect]
                            apply_to=attack
                            range=melee
                            increase_damage=4
                        [/effect]
                    )}
                [/option]
            [/rune_choice]
        [/event]
    [/event]

#enddef

#define SOF_RUNIC_CHEST_GENERIC OPTION_WML X Y

    [event]
        name=prestart
        [item]
            x,y={X},{Y}
            image=items/chest-plain-closed.png
            name=rune_shop_overlay
        [/item]
        [set_variable]
            name=chest_{X}_{Y}
            value=yes
        [/set_variable]
    [/event]

    [event]
        name=moveto
        first_time_only=no
        [filter]
            side=1
            race=dwarf
            x,y={X},{Y}
        [/filter]
        [filter_condition]
            [variable]
                name=chest_{X}_{Y}
                equals=yes
            [/variable]
        [/filter_condition]
        {CLEAR_VARIABLE chest_{X}_{Y}}
        [rune_choice]
            speaker=unit
            message= _ "This is part of our stash of field runes, we can use them to get a small temporary boost. We don’t have very many though, and need to contribute funds for replacements. Should I take this one?"
            [option]
                default=yes
                image=attacks/blank-attack.png~BLIT(icons/unit-groups/cross_30.png~SCALE(60,60),0,0)
                label= _ "No"
                description = _ "Not now."
                [command]
                    [set_variable]
                        name=chest_{X}_{Y}
                        value=yes
                    [/set_variable]
                [/command]
            [/option]
            {OPTION_WML}
        [/rune_choice]
    [/event]

#enddef

#define SOF_RUNIC_CHEST_SWIFTNESS X Y
    {SOF_RUNIC_CHEST_GENERIC (
        [option]
            {SOF_RUNIC_OPTION "swiftness" (_ "Swiftness") (_ "Adds 1 MP") ("misc/rune_icon.png~BLIT(scenery/summoning-circle4.png~CROP(6,6,60,60),0,0)") 8 (
                [effect]
                    apply_to=movement
                    increase=1
                [/effect]
            )}
            [+command]
                [if]
                    [variable]
                        name=no_funds
                        equals=yes
                    [/variable]
                    [then]
                        [set_variable]
                            name=chest_{X}_{Y}
                            value=yes
                        [/set_variable]
                    [/then]
                    [else]
                        [remove_item]
                            x,y={X},{Y}
                            image=items/chest-plain-closed.png
                        [/remove_item]
                        [item]
                            x,y={X},{Y}
                            image=items/chest-plain-open.png
                        [/item]
                    [/else]
                [/if]
                {CLEAR_VARIABLE no_funds}
            [/command]
        [/option]
    ) {X} {Y}}
#enddef

#define SOF_RUNIC_CHEST_ACCURACY X Y
    {SOF_RUNIC_CHEST_GENERIC (
        [option]
            {SOF_RUNIC_OPTION "accuracy" (_ "Accuracy") (_ "Increases ranged weapon accuracy by 10%") ("misc/rune_icon.png~BLIT(scenery/summoning-circle5.png~CROP(6,6,60,60),0,0)") 10 (
                [effect]
                    apply_to=attack
                    range=ranged
                    increase_accuracy=10
                [/effect]
            )}
            [+command]
                [if]
                    [variable]
                        name=no_funds
                        equals=yes
                    [/variable]
                    [then]
                        [set_variable]
                            name=chest_{X}_{Y}
                            value=yes
                        [/set_variable]
                    [/then]
                    [else]
                        [remove_item]
                            x,y={X},{Y}
                            image=items/chest-plain-closed.png
                        [/remove_item]
                        [item]
                            x,y={X},{Y}
                            image=items/chest-plain-open.png
                        [/item]
                    [/else]
                [/if]
                {CLEAR_VARIABLE no_funds}
            [/command]
        [/option]
    ) {X} {Y}}
#enddef

#define SOF_RUNIC_CHEST_FORCE X Y
    {SOF_RUNIC_CHEST_GENERIC (
        [option]
            {SOF_RUNIC_OPTION "force" (_ "Force") (_ "Increases melee weapon damage by 2") ("misc/rune_icon.png~BLIT(scenery/summoning-circle6.png~CROP(6,6,60,60),0,0)") 8 (
                [effect]
                    apply_to=attack
                    range=melee
                    increase_damage=2
                [/effect]
            )}
            [+command]
                [if]
                    [variable]
                        name=no_funds
                        equals=yes
                    [/variable]
                    [then]
                        [set_variable]
                            name=chest_{X}_{Y}
                            value=yes
                        [/set_variable]
                    [/then]
                    [else]
                        [remove_item]
                            x,y={X},{Y}
                            image=items/chest-plain-closed.png
                        [/remove_item]
                        [item]
                            x,y={X},{Y}
                            image=items/chest-plain-open.png
                        [/item]
                    [/else]
                [/if]
                {CLEAR_VARIABLE no_funds}
            [/command]
        [/option]
    ) {X} {Y}}
#enddef

#define SOF_CLEAR_RUNE_VARS SIDE
    [modify_unit]
        [filter]
            side={SIDE}
        [/filter]
        {CLEAR_VARIABLE swiftness_rune,accuracy_rune,force_rune}
    [/modify_unit]
#enddef
