#textdomain wesnoth-ai

[test]
    id=simple_attack
    name= _ "Simple Attack"
    next_scenario=micro_ai_test

    map_data="{multiplayer/maps/Dark_Forecast_basic.map}"

    {DEFAULT_SCHEDULE}
    turns=unlimited

    [side]
        side=1
        controller=human
        suppress_end_turn_confirmation=yes
        id=Grospur
        name= _ "Grospur"
        type=General
        x,y=15,7

        persistent=no
        canrecruit=yes
        recruit=Swordsman,Longbowman,Spearman,Bowman
        gold=100
    [/side]

    [side]
        side=2
        controller=ai
        type=Ancient Lich
        id=Uralt
        name= _ "Uralt"
        x,y=18,2

        persistent=no

        canrecruit=yes
        gold=10000
    [/side]

    [side]  # This side is only here because we need one persistent side for the game to go on
        side=3
        controller=null
        persistent=yes
        save_id=Grnk
        hidden=yes
    [/side]

    [event]
        name=preload
        first_time_only=no
        [lua]
            code=<<
                function close_to_advancing(unit)
                    if (unit.experience >= unit.max_experience-1) then
                        return true
                    else
                        return false
                    end
                end
            >>
        [/lua]
    [/event]

    [event]
        name=prestart

        {VARIABLE scenario_name simple_attack}

        {UNIT 1 Longbowman 11 7 random_traits,experience=no,67}
        {UNIT 1 Longbowman 15 5 random_traits,experience=no,67}
        {UNIT 1 Longbowman 13 10 random_traits,experience=no,67}
        {UNIT 1 (Master Bowman) 13 6 random_traits,experience=no,149}
        {UNIT 1 (Master Bowman) 11 9 random_traits,experience=no,149}
        {UNIT 1 (Master Bowman) 15 11 random_traits,experience=no,149}
        {UNIT 1 Sergeant 14 7 id=sergeant}
        {GENERIC_UNIT 1 Javelineer 15 6}
        {GENERIC_UNIT 1 (Iron Mauler) 15 10}

        {SCATTER_UNITS 12 "Soulless" 1 (x,y=5-9,1-15) (side=2)}
        {SCATTER_UNITS 6 "Skeleton,Skeleton Archer" 1 (x,y=5-9,1-15) (side=2)}

        [micro_ai]
            side=2
            ai_type=simple_attack
            action=add

            ca_score=110001
            [filter]
                type=Soulless  # No Walking Corpses; L0 units don't advance enemy
            [/filter]
            [filter_second]
                lua_function = "close_to_advancing"
            [/filter_second]
        [/micro_ai]

        [micro_ai]
            side=2
            ai_type=simple_attack
            action=add

            ca_score=110000
            [filter]
                type=Soulless,Walking Corpse
            [/filter]
        [/micro_ai]

        [modify_side]
            side=2
            [ai]
                aggression=1.0
                caution=-9999
            [/ai]
        [/modify_side]
    [/event]

    [event]
        name=start

        {SCROLL_TO 11 9}

        [message]
            speaker=sergeant
            message= _ "General Grospur, what do we do?  These undead will surely wipe us out."
        [/message]
        [message]
            speaker=Grospur
            message= _ "Don't be such a chicken, Sergeant!  I have placed units with lots of experience around the perimeter. The undead will not dare to attack them. And those few that sneak through... we can easily dispose of them once they make it inside.

<i>In other words, the Wesnoth AI does generally not attack units one XP from leveling if there is no chance of killing the unit with a single attack. However, some of the attacks by the undead are handled by the Simple Attack Micro AI in this scenario. General Grospur might be in for a surprise.</i>"
        [/message]

        [objectives]
            summary= _ "Watch the undead take care of business"
            [objective]
                description= _ "Don't even try. You can't reach the Lich."
                condition=win
            [/objective]
            [objective]
                description= _ "Death of the last of Grospur's units"
                condition=lose
            [/objective]
            [note]
                description= _ "When your leader dies, side leadership passes on to another unit"
            [/note]
        [/objectives]
    [/event]

    # Guards don't get moves on Turn 1
    [event]
        name=turn refresh

        [modify_unit]
            [filter]
                side=1
                type=Longbowman,Swordsman
            [/filter]
            moves=0
        [/modify_unit]
    [/event]

    [event]
        name=attack
        [filter_second]
            side=1
            lua_function = "close_to_advancing"
        [/filter_second]

        [message]
            speaker=$second_unit.id
            message= _ "What the ... ?!?  They are not supposed to attack me. That just doesn't happen in Wesnoth!"
        [/message]
        [message]
            speaker=Uralt
            message= _ "Hahahahaha!! I have given special instruction to my Soulless to attack all your almost-advanced units first. Also watch how those same Soulless will throw themselves mercilessly at your pitiful soldiers after that, saving my more valuable skeleton minions for later. I have taken the term 'disposable units' to a whole new level. Watch in awe!!

<i>Translation: The undead side includes two instances of the Simple Attack Micro AI.  The first makes the Soulless attack all units 1 XP from leveling up, such that they can be eliminated afterward.  The second executes all remaining attacks possible by Soulless (and Walking Corpses), without regard for their own safety.  Only after that does the default Wesnoth attack mechanism kick in to attack with the remaining units (skeletons).</i>"
        [/message]
    [/event]

    # Put more undead out there when less than 25 left
    [event]
        name=side 2 turn end
        first_time_only=no

        [if]
            [have_unit]
                side=2
                count=0-24
            [/have_unit]
            [then]
                [message]
                    speaker=Uralt
                    message= _ "Rise, minions!"
                [/message]
                {SCATTER_UNITS 6 "Soulless" 1 (x,y=5-9,1-15) (side=2)}
                {SCATTER_UNITS 3 "Skeleton,Skeleton Archer" 1 (x,y=5-9,1-15) (side=2)}
            [/then]
        [/if]
    [/event]

    # When the leader dies, transfer leadership to another unit.
    # If this was the last unit, end scenario.
    [event]
        name=die
        first_time_only=no
        [filter]
            side=1
            canrecruit=yes
        [/filter]

        [if]
            [have_unit]
                side=1
            [/have_unit]
            [then]
                [store_unit]
                    [filter]
                        side=1
                    [/filter]
                    variable=tmp_units
                [/store_unit]

                {MODIFY_UNIT id=$tmp_units[1].id canrecruit yes}
                {CLEAR_VARIABLE tmp_units}
            [/then]
            [else]
                [kill]
                    id=$unit.id
                [/kill]

                # So that game goes on to next scenario
                [modify_side]
                    side=3
                    controller=human
                [/modify_side]

                [message]
                    speaker=Uralt
                    message= _ "And that's how the undead AI executes total annihilation ..."
                [/message]

                [endlevel]
                    result=victory
                    bonus=no
                    carryover_percentage=0
                    carryover_report=no
                    linger_mode=no
                [/endlevel]
            [/else]
        [/if]
    [/event]
[/test]
