#textdomain wesnoth-utbs

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

#define DEHYDRATING_TILES
Hd, Dd*, Dd^E*, 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]
    [and]
        # Prevent unseen AI guardians from being depleted by dehydration
        [filter_vision]
            side=1
        [/filter_vision]
        [or]
            [not]
                ai_special=guardian
            [/not]
        [/or]
    [/and]
#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]

    [for]
        array=dehydrating_units
        [do]
            {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]
                    [for]
                        array=dehydrating_units[$i].attack
                        variable=j
                        [do]
                            # 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]
                        [/do]
                    [/for]

                    # 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]
        [/do]
    [/for]

    {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]

    [for]
        array=hydrating_units
        [do]
            [if]
                [variable]
                    name=hydrating_units[$i].status.dehydrated
                    equals=yes
                [/variable]

                [then]
                    [for]
                        array=hydrating_units[$i].attack
                        variable=j
                        [do]
                            {VARIABLE hydrating_units[$i].attack[$j].damage $hydrating_units[$i].variables.original_attack[$j].damage}
                        [/do]
                    [/for]
                [/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]
        [/do]
    [/for]

    {CLEAR_VARIABLE hydrating_units}
#enddef

#
# Events
#
#define DEHYDRATION_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}
                [not]
                    time_of_day=chaotic
                [/not]
            [/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
            status=dehydrated

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

    [event]
        name=turn refresh
        first_time_only=no

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

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

    [event]
        name=advance
        first_time_only=no

        [filter]
            status=dehydrated
        [/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]

        [for]
            array=hydrating_units
            [do]
                [if]
                    [variable]
                        name=hydrating_units[$i].status.dehydrated
                        equals=yes
                    [/variable]

                    [then]
                        [for]
                            array=hydrating_units[$i].attack
                            variable=j
                            [do]
                                {VARIABLE hydrating_units[$i].attack[$j].damage $hydrating_units[$i].variables.original_attack[$j].damage}
                            [/do]
                        [/for]
                    [/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]
            [/do]
        [/for]

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