#textdomain wesnoth-sof

######################################################
# Assuming this is working correctly, all the campaign
# author needs to use is something like this:
# {SOF_MINECART_MENU cart1 5 12}
######################################################

#define SOF_MINECART_PLACE_IMAGE X Y

    [if]
        [have_location]
            terrain=*^Br/
            x,y={X},{Y}
        [/have_location]
        [then]
            [item]
                # This IPF is needed to keep the cart from sitting too lowin the hex.
                # Cannot just move image around in the 72x72 canvas because then
                # it won't overlay the unit sprite correctly.
                # this might be making the shadows brighter, for some reason -> removing shadows for now
                image=misc/blank-hex.png~BLIT(misc/mine-cart-ne.png~CROP(0,24,72,48),0,2)
                x,y={X},{Y}
            [/item]
        [/then]
        [elseif]
            [have_location]
                terrain=*^Br\
                x,y={X},{Y}
            [/have_location]
            [then]
                [item]
                    image=misc/blank-hex.png~BLIT(misc/mine-cart-ne.png~FL(horiz)~CROP(0,24,72,48),0,2)
                    x,y={X},{Y}
                [/item]
            [/then]
        [/elseif]
        [else]
            [item]
                image=misc/blank-hex.png~BLIT(misc/mine-cart-n.png~CROP(0,20,72,52),0,1)
                x,y={X},{Y}
            [/item]
        [/else]
    [/if]

#enddef

#define SOF_OBJ_MINECART VAR
    [object]
        silent=yes
        id=sof_mine_cart_{VAR}
        take_only_once=no
        [filter]
            id=${VAR}.id
            race=dwarf
        [/filter]
        [effect]
            apply_to=new_animation
            [standing_anim]
                start_time=0
                base_score=2
                terrain_type=*^Br*
                [if]
                    terrain_type=*^Br|
                    [frame]
                        image=misc/mine-cart-n.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-n.png,0,0),0,0):100
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/if]
                [else]
                    terrain_type=*^Br\
                    [frame]
                        image=misc/mine-cart-ne.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-ne.png,0,0),0,0)~FL(horiz):100
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/else]
                [else]
                    terrain_type=*^Br/
                    [frame]
                        image=misc/mine-cart-ne.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-ne.png,0,0),0,0):100
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/else]
            [/standing_anim]
        [/effect]
        [effect]
            apply_to=new_animation
            [idle_anim]
                start_time=0
                base_score=2
                terrain_type=*^Br*
                [if]
                    terrain_type=*^Br|
                    [frame]
                        image=misc/mine-cart-n.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-n.png,0,0),0,0):100
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/if]
                [else]
                    terrain_type=*^Br\
                    [frame]
                        image=misc/mine-cart-ne.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-ne.png,0,0),0,0)~FL(horiz):100
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/else]
                [else]
                    terrain_type=*^Br/
                    [frame]
                        image=misc/mine-cart-ne.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-ne.png,0,0),0,0):100
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/else]
            [/idle_anim]
        [/effect]
        [effect]
            apply_to=new_animation
            [movement_anim]
                start_time=0
                base_score=2
                terrain_type=*^Br*
                [if]
                    terrain_type=*^Br|
                    [frame]
                        image=misc/mine-cart-n.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-n.png,0,0),0,0):200
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/if]
                [else]
                    terrain_type=*^Br\
                    [frame]
                        image=misc/mine-cart-ne.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-ne.png,0,0),0,0)~FL(horiz):200
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/else]
                [else]
                    terrain_type=*^Br/
                    [frame]
                        image=misc/mine-cart-ne.png~BLIT(${VAR}.image|~TC(${VAR}.side|,magenta)~MASK(misc/mine-cart-mask-ne.png,0,0),0,0):200
                        auto_hflip=no
                        auto_vflip=no
                    [/frame]
                [/else]
            [/movement_anim]
        [/effect]
        [effect]
            apply_to=movement
            increase=5
        [/effect]
        [effect]
            apply_to=movement_costs
            replace=yes
            [movement_costs]
                shallow_water=99
                reef=99
                swamp_water=99
                flat=99
                sand=99
                forest=99
                hills=99
                mountains=99
                village=99
                castle=99
                cave=99
                frozen=99
                fungus=99
                unwalkable=99
                impassable=99
                rails=1
            [/movement_costs]
        [/effect]
        [then]
            # it's one of these
            [remove_item]
                image=misc/blank-hex.png~BLIT(misc/mine-cart-n.png~CROP(0,20,72,52),0,1)
                x,y=${VAR}.x|,${VAR}.y|
            [/remove_item]
            [remove_item]
                image=misc/blank-hex.png~BLIT(misc/mine-cart-ne.png~CROP(0,24,72,48),0,2)
                x,y=${VAR}.x|,${VAR}.y|
            [/remove_item]
            [remove_item]
                image=misc/blank-hex.png~BLIT(misc/mine-cart-ne.png~FL(horiz)~CROP(0,24,72,48),0,2)
                x,y=${VAR}.x|,${VAR}.y|
            [/remove_item]
        [/then]
    [/object]

#enddef

#define SOF_REMOVE_OBJ_MINECART VAR
    [remove_object]
        id=${VAR}.id
        object_id=sof_mine_cart_{VAR}
    [/remove_object]

#enddef

#define SOF_MINECART_MENU VAR X Y

    {SOF_MINECART_PLACE_IMAGE {X} {Y}}
    [set_variables]
        name={VAR}
        mode=replace
        [value]
            load="empty"
            x={X}
            y={Y}
        [/value]
    [/set_variables]

    [set_menu_item]
        id=sof_minecart_boarding_{VAR}
        description= _ "Board Cart"
        image=buttons/WML-custom.png
        [show_if]
            [variable]
                name={VAR}.load
                equals=empty
            [/variable]
            [variable]
                name={VAR}.x
                equals=$x1
            [/variable]
            [variable]
                name={VAR}.y
                equals=$y1
            [/variable]
            [have_unit]
                x,y=${VAR}.x|,${VAR}.y|
                race=dwarf
            [/have_unit]
        [/show_if]
        [filter_location]
            terrain=*^Br*
        [/filter_location]
        [command]
            [store_unit]
                [filter]
                    x,y=${VAR}.x|,${VAR}.y|
                [/filter]
                variable=passenger_{VAR}
                kill=no
            [/store_unit]
            [set_variable]
                name={VAR}.load
                value=$passenger_{VAR}.id
            [/set_variable]
            {SOF_OBJ_MINECART passenger_{VAR}}
            # this is needed for graphics reasons, not just the movement
            [store_unit]
                [filter]
                    x,y=${VAR}.x|,${VAR}.y|
                [/filter]
                variable=passenger_redraw
                kill=no
            [/store_unit]
            [if]
                # this variable is removed every new turn
                [variable]
                    name=passenger_moves
                    equals=yes
                [/variable]
                [else]
                    [set_variable]
                        name=passenger_redraw.moves
                        add=5
                    [/set_variable]
                    [set_variable]
                        name=passenger_moves
                        value=yes
                    [/set_variable]
                [/else]
            [/if]
            [unstore_unit]
                variable=passenger_redraw
                find_vacant=no
            [/unstore_unit]
            {CLEAR_VARIABLE passenger_redraw}
            [redraw]
                side=1
            [/redraw]
        [/command]
    [/set_menu_item]
    [set_menu_item]
        id=sof_minecart_unboarding_{VAR}
        description= _ "Leave Cart"
        image=buttons/WML-custom.png
        [show_if]
            [have_unit]
                x,y=$x1|,$y1|
                id=${VAR}.load
            [/have_unit]
        [/show_if]
        [filter_location]
            terrain=*^Br*
        [/filter_location]
        [command]
            [set_variable]
                name={VAR}.load
                value="empty"
            [/set_variable]
            [set_variable]
                name={VAR}.x
                value=$x1
            [/set_variable]
            [set_variable]
                name={VAR}.y
                value=$y1
            [/set_variable]
            {SOF_REMOVE_OBJ_MINECART passenger_{VAR}}
            [store_unit]
                [filter]
                    x,y=${VAR}.x|,${VAR}.y|
                [/filter]
                variable=passenger_redraw
                kill=no
            [/store_unit]
            [if]
                [variable]
                    name=passenger_redraw.moves
                    greater_than=$passenger_redraw.max_moves
                [/variable]
                [then]
                    [set_variable]
                        name=passenger_redraw.moves
                        value=$passenger_redraw.max_moves
                    [/set_variable]
                [/then]
            [/if]
            [unstore_unit]
                variable=passenger_redraw
                find_vacant=no
            [/unstore_unit]
            {CLEAR_VARIABLE passenger_redraw}
            {SOF_MINECART_PLACE_IMAGE $x1 $y1}
            {CLEAR_VARIABLE passenger_{VAR}}
        [/command]
    [/set_menu_item]

    # if a cart-rider attacks, they exit the cart
    [event]
        name=attack
        first_time_only=no
        [filter_condition]
            [variable]
                name={VAR}.load
                equals=$unit.id
            [/variable]
        [/filter_condition]
        [set_variable]
            name={VAR}.load
            value="empty"
        [/set_variable]
        [set_variable]
            name={VAR}.x
            value=$x1
        [/set_variable]
        [set_variable]
            name={VAR}.y
            value=$y1
        [/set_variable]
        {SOF_REMOVE_OBJ_MINECART passenger_{VAR}}
        {SOF_MINECART_PLACE_IMAGE $x1 $y1}
        {CLEAR_VARIABLE passenger_{VAR}}
    [/event]

    # if a cart-rider is attacked, they exit the cart
    [event]
        name=attack
        first_time_only=no
        [filter_condition]
            [variable]
                name={VAR}.load
                equals=$second_unit.id
            [/variable]
        [/filter_condition]
        [set_variable]
            name={VAR}.load
            value="empty"
        [/set_variable]
        [set_variable]
            name={VAR}.x
            value=$x2
        [/set_variable]
        [set_variable]
            name={VAR}.y
            value=$y2
        [/set_variable]
        {SOF_REMOVE_OBJ_MINECART passenger_{VAR}}
        {SOF_MINECART_PLACE_IMAGE $x2 $y2}
        {CLEAR_VARIABLE passenger_{VAR}}
    [/event]

    # part of mechanism that prevents infinite movement by reboarding the cart
    [event]
        name=side 1 turn
        first_time_only=no
        {CLEAR_VARIABLE passenger_moves}
    [/event]

    # variables cleanup
    [event]
        name=scenario_end
        {SOF_REMOVE_OBJ_MINECART passenger_{VAR}}
        {CLEAR_VARIABLE passenger_{VAR}}
    [/event]

#enddef
