#textdomain wesnoth-utbs

#define HYDRATING_TILES
Ww*, *^V* #enddef

#define DEHYDRATING_TILES
Hd, Dd*, Rd #enddef

#define DEHYDRATABLE_UNITS_FILTER
    [not]
        race=undead
    [/not]
    [not]
        type=Giant Scorpion
    [/not]
    [not]
        type=Dust Devil
    [/not]
    [not]
        # traveller's ring
        role=immune
    [/not]
    [not]
        # prevent AI units from being depleted
        # by hydration before the player shows up
        ai_special=guardian
    [/not]
#enddef

# This dehydrates the given units. However, if the unit is adjacent to a
# friendly healer, the dehydration will be delayed instead: the unit suffers no
# ill effects, but it cannot receive any healing on this turn either. Basically,
# dehydration works just like poison.
#
# Each turn of dehydration lowers the unit’s attack damage by 1 point and
# decreases hitpoints by -$dehydration_loss.

#define APPLY_DEHYDRATION_EFFECT FILTER
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable=dehydrating_units
    [/store_unit]

    {FOREACH dehydrating_units i}
        {VARIABLE dehydrating_units[$i].resting no}

        [if]
            [have_unit]
                x,y=$dehydrating_units[$i].x,$dehydrating_units[$i].y

                [filter_adjacent]
                    is_enemy=no
                    [and]
                        ability=healing
                        [or]
                            ability=curing
                        [/or]
                    [/and]
                [/filter_adjacent]
            [/have_unit]

            [then]
                {VARIABLE dehydrating_units[$i].status.dehydration_slowed_by_healer yes}

                [unstore_unit]
                    variable=dehydrating_units[$i]
                    find_vacant=no
                [/unstore_unit]
            [/then]

            [else]
                {FOREACH dehydrating_units[$i].attack j}
                    # if dehydrating for the first time, store the original attack stats
                    [if]
                        [variable]
                            name=dehydrating_units[$i].status.dehydrated
                            not_equals=yes
                        [/variable]

                        [then]
                            {VARIABLE dehydrating_units[$i].variables.original_attack[$j].damage $dehydrating_units[$i].attack[$j].damage}
                        [/then]
                    [/if]

                    # the attack's damage drops by 1 point, but cannot drop to zero
                    [if]
                        [variable]
                            name=dehydrating_units[$i].attack[$j].damage
                            greater_than=1
                        [/variable]

                        [then]
                            {VARIABLE_OP dehydrating_units[$i].attack[$j].damage sub 1}
                        [/then]
                    [/if]
                {NEXT j}

                # unit’s hitpoints drop by $dehydration_loss, but cannot kill it
                [if]
                    [variable]
                        name=dehydrating_units[$i].hitpoints
                        greater_than=$dehydration_loss
                    [/variable]

                    [then]
                        {VARIABLE_OP dehydrating_units[$i].hitpoints sub $dehydration_loss}
                    [/then]

                    [else]
                        {VARIABLE dehydrating_units[$i].hitpoints 1}
                    [/else]
                [/if]

                {VARIABLE dehydrating_units[$i].status.dehydrated yes}

                [unstore_unit]
                    variable=dehydrating_units[$i]
                    find_vacant=no
                    text= _ "thirst"
                    {COLOR_HARM}
                [/unstore_unit]

                [unit_overlay]
                    x,y=$dehydrating_units[$i].x,$dehydrating_units[$i].y
                    image=misc/dehydration-icon.png
                [/unit_overlay]
            [/else]
        [/if]
    {NEXT i}

    {CLEAR_VARIABLE dehydrating_units}
#enddef

# This rehydrates the given units. The units gain back their full attack damage.
#define APPLY_HYDRATION_EFFECT FILTER
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable=hydrating_units
    [/store_unit]

    {FOREACH hydrating_units i}
        [if]
            [variable]
                name=hydrating_units[$i].status.dehydrated
                equals=yes
            [/variable]

            [then]
                {FOREACH hydrating_units[$i].attack j}
                    {VARIABLE hydrating_units[$i].attack[$j].damage $hydrating_units[$i].variables.original_attack[$j].damage}
                {NEXT j}
            [/then]
        [/if]

        {VARIABLE hydrating_units[$i].status.dehydrated no}

        [unstore_unit]
            variable=hydrating_units[$i]
            find_vacant=no
            text= _ "refreshed"
            {COLOR_HEAL}
        [/unstore_unit]

        [remove_unit_overlay]
            x,y=$hydrating_units[$i].x,$hydrating_units[$i].y
            image=misc/dehydration-icon.png
        [/remove_unit_overlay]
    {NEXT i}

    {CLEAR_VARIABLE hydrating_units}
#enddef

#
# Events
#

[event]
    name=prestart

    # dehydration loss - a variable for the dialogue
    {VARIABLE dehydration_loss 4}
[/event]

[event]
    name=side turn
    first_time_only=no

    # dehydration:
    # only at dawn, morning, mid-day, afternoon and dusk
    {APPLY_DEHYDRATION_EFFECT (
        side=$side_number
        {DEHYDRATABLE_UNITS_FILTER}

        [filter_location]
            terrain={DEHYDRATING_TILES}
            time_of_day=lawful
        [/filter_location]
    )}
[/event]

[event]
    name=side turn
    first_time_only=no

    # rehydration in water, villages or when next to healers
    {APPLY_HYDRATION_EFFECT (
        side=$side_number
        [filter_wml]
            [status]
                dehydrated=yes
            [/status]
        [/filter_wml]
        {DEHYDRATABLE_UNITS_FILTER}

        [and]
            [filter_location]
                terrain={HYDRATING_TILES}
            [/filter_location]

            # adjacent healers will remove dehydration
            [or]
                [filter_adjacent]
                    is_enemy=no
                    [and]
                        ability=healing
                        [or]
                            ability=curing
                        [/or]
                    [/and]
                [/filter_adjacent]
            [/or]
        [/and]
    )}
[/event]

[event]
    name=turn refresh
    first_time_only=no

    [if]
        [variable]
            name=side_number
            equals=1
        [/variable]

        [then]
            {MODIFY_UNIT (
                side=1
                [filter_wml]
                    [status]
                        dehydration_slowed_by_healer=yes
                    [/status]
                [/filter_wml]
            ) status.dehydration_slowed_by_healer no}
        [/then]
    [/if]
[/event]

[event]
    name=advance
    first_time_only=no

    [filter]
        [filter_wml]
            [status]
                dehydrated=yes
            [/status]
        [/filter_wml]
    [/filter]

    {VARIABLE unit.status.dehydrated no}

    [unstore_unit]
        variable=unit
        find_vacant=no
        text= _ "refreshed"
        {COLOR_HEAL}
        advance=no
    [/unstore_unit]

    [remove_unit_overlay]
        x,y=$unit.x,$unit.y
        image=misc/dehydration-icon.png
    [/remove_unit_overlay]
[/event]

[event]
    name=victory

    # Rehydrate all units for the next scenario
    # Do this more quietly than usual...
    [store_unit]
        [filter]
            side=1
        [/filter]

        kill=no
        variable=hydrating_units
    [/store_unit]

    {FOREACH hydrating_units i}
        [if]
            [variable]
                name=hydrating_units[$i].status.dehydrated
                equals=yes
            [/variable]

            [then]
                {FOREACH hydrating_units[$i].attack j}
                    {VARIABLE hydrating_units[$i].attack[$j].damage $hydrating_units[$i].variables.original_attack[$j].damage}
                {NEXT j}
            [/then]
        [/if]

        {VARIABLE hydrating_units[$i].status.dehydrated no}

        [unstore_unit]
            variable=hydrating_units[$i]
            find_vacant=no
        [/unstore_unit]

        [remove_unit_overlay]
            id=$hydrating_units[$i].id
            image=misc/dehydration-icon.png
        [/remove_unit_overlay]
    {NEXT i}

    # Clear related variables
    {CLEAR_VARIABLE hydrating_units}
    {CLEAR_VARIABLE dehydration_loss}
[/event]
