#textdomain wesnoth-did
[scenario]
    id=Peaceful_Valley
    next_scenario=A_Haunting_in_Winter

    name=_ "Peaceful Valley"
    map_data="{campaigns/Descent_Into_Darkness/maps/Peaceful_valley.map}"
    {TURNS 29 26 23}

    victory_when_enemies_defeated=no

    {STORY_PEACEFUL_VALLEY}

    {MORNING}
    {AFTERNOON}
    {DUSK}
    {FIRST_WATCH}
    {SECOND_WATCH}
    {DAWN}

    [side]
        side=1
        controller=human
        type=Apprentice Mage
        description=Malin Keshar
        user_description=_ "Malin Keshar"
        unrenamable=yes
        canrecruit=yes
        recruit=Walking Corpse,Vampire Bat
        {GOLD 120 100 80}
        fog=yes
        shroud=yes
    [/side]

    [side]
        side=2
        controller=ai
        type=Goblin Knight
        description="T'shar Lggi"
        user_description=_ "T'shar Lggi"
        canrecruit=yes
        recruit=Goblin Rouser,Goblin Impaler,Wolf Rider
        {GOLD 70 90 110}
        fog=yes
        shroud=no
        [ai]
            village_value=2
            caution=0.5
            recruitment_pattern=scout,fighter
        [/ai]
    [/side]

    {STARTING_VILLAGES 2 99}

    [event]
        name=prestart

        [music]
            name=revelation.ogg
        [/music]

        [objectives]
            side=1
            [objective]
                condition=win
                description=_ "Occupy all of the goblin villages"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Malin Keshar"
            [/objective]
            [objective]
                condition=lose
                description=_ "Death of Darken Volk"
            [/objective]
        [/objectives]
    [/event]

    [event]
        name=start
        # {SCATTER_IMAGE (terrain=Ss) 3 scenery/pine1.png}
        [unit]
            description=Darken Volk
            user_description=_ "Darken Volk"
            profile=portraits/darken_volk.png
            type=Dark Sorcerer DiD
            unrenamable=yes
            random_traits=no
            [modifications]
                [trait]
                    id=loyal
                    name= _ "loyal"
                    description= _ "to himself"
                    [effect]
                        apply_to=loyal
                    [/effect]
                [/trait]
                {TRAIT_QUICK}
            [/modifications]
            side=1
            x=15
            y=16
            {IS_HERO}
        [/unit]

        {DIALOGUE_PV_START}
    [/event]

    # Generate a random number of guards between 0 and 2 around X,Y
#define RANDOM_GUARDS TYPE DESCRIPTION USER_DESCRIPTION SIDE X Y
    {VARIABLE_OP number_of_guards random 0..2}

    {REPEAT $number_of_guards (
    [store_locations]
        [filter_adjacent_location]
            x,y={X},{Y}
        [/filter_adjacent_location]

        [not]
            [filter]
            [/filter]
        [/not]

        variable=possible_guard_locations
    [/store_locations]

    {IF_VAR possible_guard_locations.length greater_than 0 (
    [then]
        {VARIABLE_OP random_location_index random 0..$possible_guard_locations.length}

        [move_unit_fake]
            side={SIDE}
            type={TYPE}
            x={X},$possible_guard_locations[$random_location_index].x
            y={Y},$possible_guard_locations[$random_location_index].y
        [/move_unit_fake]

        [unit]
            description={DESCRIPTION}
            user_description={USER_DESCRIPTION}	# wmllint: ignore
            type={TYPE}
            side={SIDE}
            x,y=$possible_guard_locations[$random_location_index].x,$possible_guard_locations[$random_location_index].y
            random_traits=yes
        [/unit]
    [/then]
    )}
    )}

    {CLEAR_VARIABLE number_of_guards}
    {CLEAR_VARIABLE possible_guard_locations}
    {CLEAR_VARIABLE random_location_index}
#enddef

    # In-scenario events
    [event]
        name=capture
        first_time_only=yes
        [filter]
            side=1
        [/filter]
        {DIALOGUE_PV_CAPTURE}
    [/event]

    [event]
        name=capture
        first_time_only=no
        [filter]
            side=1
        [/filter]

        {IF_VAR village_$x1,$y1|_cleared not_equals yes (
        [then]
            {RANDOM_GUARDS (Goblin Spearman) Villager (_ "Villager") 2 $x1 $y1}

            {VARIABLE village_$x1,$y1|_cleared yes}
        [/then]

        [else]
            [allow_undo][/allow_undo]
        [/else]
        )}
    [/event]

    [event]
        name=moveto
        [filter]
            side=1
            x,y=15,11
        [/filter]
        {DIALOGUE_PV_SWAMP1}
        [allow_undo][/allow_undo]
    [/event]

    [event]
        name=moveto
        [filter]
            [not]
                [not]
                    description=Darken Volk
                [/not]
                [not]
                    description=Malin Keshar
                [/not]
            [/not]
            x=14-15,16-20,19-20
            y=9-10,6-10,11
        [/filter]
        {DIALOGUE_PV_SWAMP2}
        [scroll_to]
            x,y=17,8
        [/scroll_to]
        {LOYAL_UNDEAD_UNIT 1 Ghoul 19 7}
        {LOYAL_UNDEAD_UNIT 1 Ghoul 17 9}
        {LOYAL_UNDEAD_UNIT 1 Ghoul 17 8}
        {DIALOGUE_PV_SWAMP3}
    [/event]

    {DIALOGUE_PV_EE}

    # Defeat Conditions
    {MALIN_DEATH}
    {VOLK_DEATH}
    {TIME_OUT_LOSE}

    # Victory Condition

    [event]
        name=capture
        first_time_only=no
        [filter]
            side=1
        [/filter]
        #	{VARIABLE_OP villages_owned add 1}
        #when capturing a village we check if we own all villages of the map
        [if]
            [have_location]
                terrain=*^V*

                [not]
                    owner_side=1
                [/not]
            [/have_location]

            [else]
                {DIALOGUE_PV_END}
                [endlevel]
                    bonus=yes
                    result=victory
                [/endlevel]
            [/else]
        [/if]
    [/event]

    [event]
        name=victory

        [store_villages]
            variable=villages
        [/store_villages]

        {FOREACH villages i}
        {VARIABLE_OP village_var format "village_$villages[$i].x,$villages[$i].y|_cleared"}

        {CLEAR_VARIABLE $village_var}
        {NEXT i}

        {CLEAR_VARIABLE villages}
        {CLEAR_VARIABLE village_var}
    [/event]

    {@campaigns/Descent_Into_Darkness/utils/global-events.cfg}
[/scenario]
