#textdomain wesnoth-sota

# During the opening, characters move in and out of the trapdoors leading below decks,
# and into the passenger cabin doors as well. Macros open and close the various doors as
# necessary. A menu item is provided to remove the terrain image that
# contains the ship's sails. The macro for that is in utils because it is also used for
# the next scenario. The ship terrain is fairly complicated, but the terrain files have
# more details. The ship is the reason for SotA-only bats: Normal bats are drawn above
# all terrain, but that looks wrong with the ship sails, so SotA bats are drawn on a
# lower z-level.

[scenario]
    name= _ "Becalmed"
    map_file=04_Becalmed.map

    id=04_Becalmed
    next_scenario=05_Blackwater

    [story]
        [part]
            [background_layer]
                image=story/book.webp
                scale=no
            [/background_layer]
            story= _ "22 III, 23 YW

Research sometimes requires experiments, so I began. I knew the bandit had intended to rob me, and once I had killed his beloved, he had intended to torture and kill me. Now that I was in control, he was afraid and defensive, and while his hatred of me was unabated, his desire to live made him somewhat malleable."
        [/part]
        [part]
            [background_layer]
                image=story/book.webp
                scale=no
                base_layer=yes
            [/background_layer]
            [image]
                x,y=1,1
                centered=yes
                file=icons/coins_copper.png~CROP(4,4,50,50)~CS(10,10,-40)
            [/image]
            [image]
                x,y=60,38
                centered=yes
                file=icons/coins_copper.png~CROP(4,4,50,50)~CS(10,10,-30)~FL(horiz)
                delay=300
            [/image]
            [image]
                x,y=2,80
                centered=yes
                file=icons/coins_copper.png~CROP(4,4,50,50)~CS(10,10,-20)~SCALE_SHARP(75,75)
                delay=600
            [/image]
            [image]
                x,y=430,25
                centered=yes
                file=icons/ring_gold.png~CROP(4,4,50,50)~GS()~CS(-45,-80,-50)~XBRZ(2)
                delay=7500
            [/image]
            story= _ "I passed him a wineskin, to put him at ease. He eyed me suspiciously, then took a sip, calculating my intentions. I then placed a portion of my gold resources on the table, and his eyes locked upon it.

I then placed upon the table a ring stripped off the bandit named ‘Mossa’, his beloved partner in crime. His gaze shifted from the gold to the ring, and then quickly to my eyes. Had he not been tied to the chair, he would have attacked me.

I added more gold to the pile, but he didn’t notice, he only glared at me with such hatred. The hunger for vengeance radiated off him like heat from a thousand suns — I could feel it more strongly than the fear or hunger from any of my pets..."
        [/part]
        [part]
            [background_layer] # this needs to be replaced at some point
                image=attacks/lightbeam.png~BLEND(0,10,20,0.8)
                scale=yes
                base_layer=yes
            [/background_layer]
            [image]
                file=story/book.webp
                centered=yes
                x,y=30,30
            [/image]
            story= _ "This rich vein into the life-force of the bandit was like nothing I had ever seen, but I knew enough to bend his hunger to my will. With further study, I may one day be able to stop it there, but today I could not: The single-minded hunger consumed the man, it was all I could do to control its direction!

The bandit howled, thrashed, tore at his clothes and bindings, eventually digging into his own flesh. Eventually, when all but the hunger had been flensed from his being, he calmed down. Now hairless, bloody, naked, and unthinking, the bandit was unrecognizable as the man he once was."
        [/part]
        [part]
            [background_layer]
                image=story/black-background.webp
                scale=no
                base_layer=yes
            [/background_layer]
            [image]
                file=portraits/ardonna.webp
                x,y=64,0
            [/image]
            [image]
                file=portraits/undead/ghoul.webp~FL()
                x,y=320,0
            [/image]
            story= _ "No doubt whatever was left of the mind inside gibbered at what it had become, but the bandit was now my puppet and I stitched the wounds as best I could. In some ways, this experiment was a failure, but the creature it created would be useful..."
        [/part]
        [part]
            [background_layer]
                image=story/alduin-dock.webp
                scale=no
            [/background_layer]
            story= _ "Working through the next day in the bandit’s own keep, I repeated the procedure on several of his gravely wounded companions, though none of them became as strong a specimen as the leader. One of them didn’t survive, but it was no loss — I conscripted him into my army anyway. Then, I made my way to the docks, and under the cover of night, loaded my army into the hold of the waiting ship. The following afternoon, I embarked as a normal passenger."
        [/part]
        [part]
            [background_layer]
                image=story/becalmed.webp
                scale=no
            [/background_layer]
            story= _ "25 III, 23 YW

The journey started well enough, but on the dawn following our departure, the wind died completely, and we were stranded. There was enough food for the paying passengers for a couple days of this, but none for my companions, and the living ones soon got hungry. When one of the crew disappeared on the second evening, I suspected the cause. I was not wrong."
        [/part]
        {JOURNEY_PART 4}
    [/story]

    # This is the same as the default schedule, but starts at dusk.
    {DUSK}
    {FIRST_WATCH}
    {SECOND_WATCH}
    {DAWN}
    {MORNING}
    {AFTERNOON}

    [time_area]
        [time]
            id=lower_deck
            name= _ "Underground"
            image=misc/time-schedules/schedule-underground.png
            lawful_bonus=-25
            red=-35
            green=-30
            blue=-25
        [/time]
        x=14, 0-infinity
        y=12, 13-infinity
    [/time_area]

    {DEFAULT_MUSIC_PLAYLIST}
    {TURNS 14 15 16}

    # wmllint: validate-off
    [side]
        {SIDE_1_ARDONNA}
        {ARDONNA_RECRUIT_3}
        # We are assuming substantial gold from the previous scenario:
        {GOLD 60 40 20}
        facing=ne
    [/side]
    # wmllint: validate-on

    [side]
        side=2
        controller=ai
        team_name=bad
        user_team_name= _ "Ship’s Crew"
        type=Sea Captain
        name= _ "Rudic"
        id=Rudic
        profile=portraits/captain.webp
        recruit=Sailor
        {GOLD 80 100 120}
        {INCOME 4 5 6}
        facing=sw
        color=white
    [/side]

    # These two sides are only to provide color and ellipses for the other ship
    # passengers. The units look out of place without them.
    [side]
        side=3
        team_name=good
        controller=null
        hidden=yes
        color=teal
        [unit]
            type=Noble
            x,y=11, 7
            facing=se
        [/unit]
    [/side]

    [side]
        side=4
        team_name=good
        controller=null
        hidden=yes
        color=orange
        [unit]
            type=Dunefolk
            x,y=12, 16
            facing=se
        [/unit]
    [/side]

    {STARTING_VILLAGES 2 8}

#define OPEN_TRAP_DOORS X Y
    [remove_item]
        x,y={X},{Y}
        image=items/trap-doors-closed.png
    [/remove_item]
    [item]
        x,y={X},{Y}
        image=items/trap-doors-open.png
    [/item]
#enddef

#define CLOSE_TRAP_DOORS X Y
    [remove_item]
        x,y={X},{Y}
        image=items/trap-doors-open.png
    [/remove_item]
    [item]
        x,y={X},{Y}
        image=items/trap-doors-closed.png
    [/item]
#enddef

    # *************************** PRESTART ***************************
    [event]
        name=prestart
        {FREEZE_UNIT Rudic}  # Otherwise, he attacks suicidally on the first turn!

        [item]
            x,y=10,8
            image=items/trap-doors-closed.png
        [/item]
        [item]
            x,y=14,6
            image=items/trap-doors-closed.png
        [/item]
        [item]
            x,y=13,15
            image=items/trap-doors-closed.png
        [/item]

        [unit]
            type=Ghoul
            side=1
            name= _ "Garcyn"
            x,y=recall,recall
            [modifications]
                {TRAIT_STRONG}
            [/modifications]
        [/unit]

        [objectives]
            side=1
            [objective]
                description= _ "Defeat the enemy leader"
                condition=win
            [/objective]

            {HOW_TO_LOSE_WITH_ARDONNA}

            [gold_carryover]
                bonus=yes
                carryover_percentage=40
            [/gold_carryover]

            [note]
                description= _ "You can recruit from anywhere on the two highest decks."
            [/note]
            [note]
                description= _ "Player and Enemy units can move between the surface and the interior of the ship via stairs."
            [/note]
            [note]
                description= _ "Barrels act as villages."
            [/note]
            [note]
                description= _ "Right-click to simplify the ship graphics."
            [/note]
        [/objectives]

        [set_variable]
            name=sails_hidden
            value=no
        [/set_variable]
        {SHOW_SAILS_MENU_ITEM -1}
        {HIDE_SAILS_MENU_ITEM -1}

        [tunnel]
            [source]
                x,y=10,8
            [/source]
            [target]
                x,y=10,16
            [/target]
            [filter]
            [/filter]
            pass_allied_units=yes
        [/tunnel]
        [tunnel]
            [source]
                x,y=14,6
            [/source]
            [target]
                x,y=14,14
            [/target]
            [filter]
            [/filter]
            pass_allied_units=yes
        [/tunnel]

        [item]
            x,y=11,12
            halo=map-border.png
        [/item]
    [/event]

    # *************************** PLAY ***************************

#define MOVE_TO_LOWER_DECK X Y
    [move_unit]
        x,y={X},{Y}
        to_x=14
        to_y=6
    [/move_unit]
    [teleport]
        [filter]
            x,y=14,6
        [/filter]
        x,y=14,14
        animate=yes
    [/teleport]
    [move_unit]
        x,y=14,15
        to_x=13
        to_y=16
    [/move_unit]
#enddef

    # This next event scripts the first turn AI moves. If we don't do this, sometimes too
    # many units go to the lower deck, and sometimes none do. This has a large impact on
    # the difficulty of the scenario. Also, the ai units would box in the player's units and
    # block the trapdoor.
    [event]
        name=side 2 turn 1 refresh

        # If Ardonna has (stupidly) moved onto the main deck, the scripted moves
        # will cause the ai to ignore her zone of control and the ai units could
        # move behind her. To prevent this, we will skip the scripted moves and let
        # the ai attack her if she has moved off her deck or in range.
        [if]
            [have_unit]
                id=Ardonna
                [filter_location]
                    terrain=*^focS  # Ship forecastle
                    [or]
                        # She can take one of the first two barrels on the
                        # lower deck. The AI can't possibly kill her, so it
                        # will stick to the script. She's fair game on the
                        # third barrel though.
                        x=1-10
                        y=11-infinity
                    [/or]
                    [or]
                        # She also doesn't get *immediately* attacked if she
                        # is just guarding her trapdoor.
                        x=9,10
                        y=8
                    [/or]
                [/filter_location]
            [/have_unit]

            [then]
                [move_unit]
                    x,y=15,6
                    to_x=11
                    to_y=7
                [/move_unit]
                [move_unit]
                    y=6
                    to_x=13
                    to_y=8
                [/move_unit]
                {MOVE_TO_LOWER_DECK 14 5}
                {MOVE_TO_LOWER_DECK 15 5}

                [modify_unit]
                    [filter]
                        side=2
                    [/filter]
                    moves=0
                [/modify_unit]
            [/then]
        [/if]
    [/event]

#undef MOVE_TO_LOWER_DECK

    # Force more newly-recruited units to guard the lower deck:
    [event]
        name=side 2 turn 1 end
        [micro_ai]
            side=2
            ai_type=zone_guardian
            action=add
            [filter]
                y=6
            [/filter]
            [filter_location]
                x=0-infinity
                y=0-12
            [/filter_location]
            [filter_location_enemy]
                x=0-infinity
                y=13-99
            [/filter_location_enemy]
        [/micro_ai]
    [/event]

    # We need an event for each trap door, going each direction. The code for each is very
    # similar so we can make all four with one macro.
#define CREATE_TRAP_DOOR Y1 Y2 TRAP_X TRAP_Y
    [event]
        name=exit_hex
        first_time_only=no
        [if]
            # Checking the unit's x position is not necessary. There is only one
            # way the unit's y position can change by the amount being used here:
            [variable]
                name=y1
                equals={Y1}
            [/variable]
            [and]
                [variable]
                    name=y2
                    equals={Y2}
                [/variable]
            [/and]
            [then]
                {OPEN_TRAP_DOORS {TRAP_X} {TRAP_Y}}
            [/then]
        [/if]
        [allow_undo][/allow_undo]
    [/event]
#enddef

    {CREATE_TRAP_DOOR 8 16 10 8}
    {CREATE_TRAP_DOOR 16 8 10 8}
    {CREATE_TRAP_DOOR 6 14 14 6}
    {CREATE_TRAP_DOOR 14 6 14 6}

#undef CREATE_TRAP_DOOR

    [event]
        name=moveto
        first_time_only=no
        {CLOSE_TRAP_DOORS 10 8}
        {CLOSE_TRAP_DOORS 14 6}
        [allow_undo][/allow_undo]
    [/event]

    # *************************** OPENING / ENDING ***************************
#define OPEN_DOOR X Y
    [item]
        x,y={X},{Y}
        image=items/door-open-bottom.png
        redraw=no
    [/item]
    [item]
        x={X}
        y="$({Y}-1)"
        image=items/door-open-top.png
    [/item]
    [redraw][/redraw]
#enddef

#define CLOSE_DOOR X Y
    [remove_item]
        x={X}
        y="$({Y}-1)"
    [/remove_item]
    [remove_item]
        x,y={X},{Y}
    [/remove_item]
#enddef

#define PLACE_SAILOR
    {NEAREST_HEX 16 6 3 (terrain=*^pooS
    [not][filter][/filter][/not]) location}
    [unit]
        x=$location.x
        y=$location.y
        type=Sailor
        side=2
        facing=sw
    [/unit]
    [clear_variable]
        name=location
    [/clear_variable]
#enddef

    [event]
        name=start

        [scroll_to]
            x,y=12,6
        [/scroll_to]

        [delay]
            time=1000
        [/delay]

        # Ghoul added to side recruit this scenario
        [message]
            speaker=narrator
            image=portraits/undead/ghoul.webp~SCALE(93, 93)
            message= _ "You can now recruit ghouls!"
        [/message]

        [scroll_to]
            x,y=12,15
        [/scroll_to]

        [delay]
            time=700
        [/delay]

        # Joc the sailor comes upstairs to talk to the captain:
        {OPEN_TRAP_DOORS 13 15}
        [delay]
            time=400
        [/delay]
        [move_unit_fake]
            type=Sailor
            side=2
            x=13, 14
            y=15, 14
        [/move_unit_fake]
        {CLOSE_TRAP_DOORS 13 15}
        {OPEN_TRAP_DOORS 14 6}
        [move_unit_fake]
            type=Sailor
            side=2
            x=14, 16
            y=6,  6
        [/move_unit_fake]
        [unit]
            side=2
            x,y=16, 6
            type=Sailor
            name=_ "Joc"
            id=Joc
            facing=nw
        [/unit]
        {CLOSE_TRAP_DOORS 14 6}

        [message]
            speaker=Joc
            message= _ "I found Janryn in the hold. Or, ah... some of him. He’s dead. I didn’t stay to find out what killed him, but there was someone shuffling around in the shadows, and he smelled foul."
        [/message]
        [message]
            speaker=Rudic
            message= _ "Murderous stowaways on my ship? I won’t have it! Go round up the rest of the crew and get them up here. We’re going hunting. All passengers are confined to quarters immediately."
        [/message]

        # Joc goes back down and through the bottom trap door:
        [kill]
            id=Joc
        [/kill]
        {OPEN_TRAP_DOORS 14 6}
        [move_unit_fake]
            type=Sailor
            side=2
            x=16, 14
            y=6,  6
        [/move_unit_fake]
        {CLOSE_TRAP_DOORS 14 6}
        {OPEN_TRAP_DOORS 13 15}
        [move_unit_fake]
            type=Sailor
            side=2
            x=14, 13
            y=14, 15
        [/move_unit_fake]
        {CLOSE_TRAP_DOORS 13 15}

        # The other two passengers go through doors and disappear:
        [kill]
            side=4
        [/kill]
        {OPEN_DOOR 14 13}
        [move_unit_fake]
            type=Dunefolk
            side=4
            x=12, 14
            y=16,  13
        [/move_unit_fake]
        {CLOSE_DOOR 14 13}

        {OPEN_TRAP_DOORS 10 8}
        [kill]
            side=3
        [/kill]
        [move_unit_fake]
            type=Noble
            side=3
            x=11, 10
            y=7,  8
        [/move_unit_fake]
        {CLOSE_TRAP_DOORS 10 8}
        {OPEN_DOOR 16 14}
        [move_unit_fake]
            type=Noble
            side=4
            x=10, 16
            y=16,  14
        [/move_unit_fake]
        {CLOSE_DOOR 16 14}

        {REPEAT 5 {PLACE_SAILOR}}

        [message]
            speaker=Rudic
            message= _ "You! Dark girl. Why are you not in your cabin?"
        [/message]
        [message]
            speaker=Ardonna
            message= _ "What if I said something in defense of the stowaways?"
        [/message]
        [message]
            speaker=Rudic
            message= _ "I would pitch you over the side with them. What do you know?"
        [/message]
        [message]
            speaker=Ardonna
            message= _ "They are my companions, and the poor things are just terribly hungry. I’m sorry we didn’t pay the full amount, but maybe we can reach some arrangement."
        [/message]
        [message]
            speaker=Rudic
            message= _ "They killed my crew member and played me for a fool! The only arrangement that suits me is the lot of you off my ship, preferably at the bottom of the sea."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Oh dear. We must do this again. Come to me my friends! If you are still hungry, feed on a passenger first."
        [/message]
    [/event]

    [event]
        name=die
        [filter]
            id=Rudic
        [/filter]

        [message]
            speaker=Ardonna
            message= _ "Good riddance. But, with the captain dead, who will sail the ship and guide us into port now?"
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Rise Captain, and please retain your memories."
        [/message]

        [store_unit]
            [filter]
                id=Rudic
            [/filter]
            variable=rudic_stored
            kill=yes
        [/store_unit]
        [unit]
            # wmllint: who CHARACTER_STATS_BONE_CAPTAIN is Bone Captain
            {CHARACTER_STATS_BONE_CAPTAIN}
            side=1
            x=$rudic_stored.x
            y=$rudic_stored.y
            animate=yes
        [/unit]
        [clear_variable]
            name=rudic_stored
        [/clear_variable]

        [message]
            speaker=Ardonna
            message= _ "Can you still sail this ship?"
        [/message]
        [message]
            speaker=Bone Captain
            image=portraits/bone-captain1.webp
            message= _ "Sail. Yes. Need wind."
        [/message]
        [message]
            speaker=Ardonna
            message= _ "Oh! It worked! Take us to port, Captain, as soon as you do have the wind."
        [/message]
    [/event]

    [event]
        name=enemies defeated
        [endlevel]
            result=victory
            bonus=yes
            {NEW_GOLD_CARRYOVER 40}
        [/endlevel]
    [/event]

    {HERO_DEATHS}
    {TRANSFORM_BATS_FOR_SHIP}
    {ZOMBIES}

#undef PLACE_SAILOR
#undef OPEN_DOOR
#undef CLOSE_DOOR
#undef OPEN_TRAP_DOORS
#undef CLOSE_TRAP_DOORS
[/scenario]
