#textdomain wesnoth
# This file contains shortcuts for common WML events (such as prestart, side
# turn, and such), which can be used to write events faster and in less space.
#
# It is recommended that you only use these if you're confident you could write
# the expanded form as well; these are mostly intended as shortcuts for
# experienced WML authors.

# These don't depend on any other macros.  Please don't change this.
# ! in comments is used in generating HTML documentation, ignore it otherwise.

#define CALL_FUNCTION EVENT_NAME PARAMETER_WML
    # This will fire an event with a set of parameters, for example:
    #! {CALL_FUNCTION my_event a,b,c=1,2,3}
    #
    # That example would fire an event with name "my_event"
    # and inside that event, $param.c will be equal to "3"
    # This will hold true even if the event is fired recursively
    [set_variables]
        mode=insert
        name=param[0]
        [value]
            {PARAMETER_WML}
        [/value]
    [/set_variables]
    [fire_event]
        name={EVENT_NAME}
        [primary_unit]
            x,y=$unit.x,$unit.y
        [/primary_unit]
        [second_unit]
            x,y=$second_unit.x,$second_unit.y
        [/second_unit]
    [/fire_event]
    [clear_variable]
        name=param[0]
    [/clear_variable]
#enddef

#define ON_SIGHTING ID SIGHTING_SIDE SIGHTED_FILTER ACTION_WML
    # This is intended to be used when a regular sighted event can't be relied
    # on (that'd be most of the time). This event triggers when a unit matching
    # SIGHTED_FILTER is first seen by a unit of side SIGHTING_SIDE. This works
    # whether the sighting happens due to moving, recruiting, recalling,
    # attacking (in case DSU was on before the attack) or advancing. If none of
    # those actions manage to "catch" the moment of sighting, then it will occur
    # when the current side ends their turn.
    #
    # The 'unit' and 'second_unit' message speaker shortcuts as well as the
    # auto-stored $unit and $second_unit should work normally: the sighting unit
    # is the primary unit and the sighted unit is the secondary unit.
    #
    # You need to provide a unique ID if you use several of these events in the
    # same scenario. Otherwise it can be left empty.
    #
    # Only works if SIGHTING_SIDE has fog and/or shroud.
    #
    # Example:
    #! {ON_SIGHTING () 1 side=2,3 (
    #!     [message]
    #!         speaker=unit
    #!         message= _ "I can see a side $second_unit.side $second_unit.language_name|!"
    #!     [/message]
    #!     [message]
    #!         speaker=second_unit
    #!         message= _ "Oh my! I was seen by a side $unit.side $unit.language_name|!"
    #!     [/message]
    #! )}
    #!
    [event]
        name=SIGHTED_{ID}_trigger_action_WML

        {ACTION_WML}
    [/event]

    [event]
        name=moveto,recruit,recall,attack,post advance,side turn
        first_time_only=no

        [filter]
            side={SIGHTING_SIDE}

            [or]
                {SIGHTED_FILTER}
            [/or]
        [/filter]

        [if]
            [variable]
                name=SIGHTED_{ID}_finished
                not_equals=yes
            [/variable]

            [then]
                [store_side]
                    side={SIGHTING_SIDE}
                    variable=stored_sighting_side
                [/store_side]

                [if]
                    [have_unit]
                        {SIGHTED_FILTER}

                        [filter_vision]
                            viewing_side={SIGHTING_SIDE}
                        [/filter_vision]
                    [/have_unit]

                    [and]
                        [variable]
                            name=stored_sighting_side.fog
                            boolean_equals=yes
                        [/variable]

                        [or]
                            [variable]
                                name=stored_sighting_side.shroud
                                boolean_equals=yes
                            [/variable]
                        [/or]
                    [/and]

                    [then]
                        [fire_event]
                            name=SIGHTED_{ID}_trigger_action_WML

                            [primary_unit]
                                side={SIGHTING_SIDE}

                                [filter_location]
                                    [filter]
                                        {SIGHTED_FILTER}

                                        [not]
                                            side={SIGHTING_SIDE}
                                        [/not]
                                    [/filter]

                                    radius="$($this_unit.max_moves + 1)"
                                [/filter_location]
                            [/primary_unit]

                            [secondary_unit]
                                {SIGHTED_FILTER}

                                [filter_vision]
                                    viewing_side={SIGHTING_SIDE}
                                [/filter_vision]
                            [/secondary_unit]
                        [/fire_event]

                        [set_variable]
                            name=SIGHTED_{ID}_finished
                            value=yes
                        [/set_variable]
                    [/then]

                    [else]
                        [allow_undo]
                        [/allow_undo]
                    [/else]
                [/if]

                [clear_variable]
                    name=stored_sighting_side
                [/clear_variable]
            [/then]

            [else]
                [allow_undo]
                [/allow_undo]
            [/else]
        [/if]
    [/event]

    [event]
        name=victory

        [clear_variable]
            name=SIGHTED_{ID}_finished
        [/clear_variable]
    [/event]
#enddef
