New micro_ai: attack_enemy_leader

Brainstorm ideas of possible additions to the game. Read this before posting!

Moderator: Forum Moderators

Forum rules
Before posting a new idea, you must read the following:
Post Reply
User avatar
Helmet
Posts: 507
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

New micro_ai: attack_enemy_leader

Post by Helmet »

Hi, Game Designers.

As a scenario designer, I would be grateful if the following micro_ai was added to the game:

ai_type=attack_enemy_leader: Any unit qualified by the filter will attack an enemy leader if they are within range. Nearby enemy units will not distract them, and the unit's own damage will not be a consideration.

A key that assigns a percentage for this behavior would also be useful. Otherwise, the default percentage is 100%.

For example, frequency=70 would indicate that an ai-controlled unit would have a 70% probability of replacing its default behavior with an attack on an enemy leader whenever an enemy leader is within range.

I have tried various other methods of encouraging the ai to prefer attacking an enemy Leader rather than other units, and they do not work. At best, they encourage the ai's units to move toward an enemy leader. However, once the enemy leader is within attack range, it remain highly likely that the ai's unit will attack a nearby 0-level unit or a badly injured unit instead.

I think ai_type=attack_enemy_leader would make the ai seem more human-like, as most players are ever-watchful for an opportunity to attack an enemy leader.

Thanks. The Battle for Wesnoth is an awesome game. Keep up the good work! :D
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2148
Joined: May 24th, 2010, 5:26 pm
Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
Contact:

Re: New micro_ai: attack_enemy_leader

Post by Lord-Knightmare »

Simple Attack MAI with a lua function to target enemy leader would be sufficient for this actually. No new MAI is needed.
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
User avatar
Helmet
Posts: 507
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: New micro_ai: attack_enemy_leader

Post by Helmet »

Lord-Knightmare wrote: November 9th, 2022, 11:20 pm Simple Attack MAI with a lua function to target enemy leader would be sufficient for this actually. No new MAI is needed.
Well, people who can code lua may not need it, but only a small percentage of scenario-makers would qualify. I certainly couldn't do it.

With all due respect, if writing the code is so easy, would you mind contributing the code within this thread so people like me can copy/paste it into their scenarios?
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2148
Joined: May 24th, 2010, 5:26 pm
Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
Contact:

Re: New micro_ai: attack_enemy_leader

Post by Lord-Knightmare »

Helmet wrote: November 10th, 2022, 2:22 am
Lord-Knightmare wrote: November 9th, 2022, 11:20 pm Simple Attack MAI with a lua function to target enemy leader would be sufficient for this actually. No new MAI is needed.
Well, people who can code lua may not need it, but only a small percentage of scenario-makers would qualify. I certainly couldn't do it.

With all due respect, if writing the code is so easy, would you mind contributing the code within this thread so people like me can copy/paste it into their scenarios?
Of course. Instructing people on making better UMC is what I do these days.

So, we start with defining Lua functions.

Code: Select all

    # preload for Lua events
    [event]
        name=preload
        first_time_only=no

        # focus me
        # since I am the main
        [lua]
            code=<<
                function prioritise_player_leader(unit)
                    if (unit.id == "Jahin") then
                        return true
                    else
                        return false
                    end
                end
            >>
        [/lua]

        # Lua function to priortise 
        # higher level units
        [lua]
            code=<<
                function high_level_unit(unit)
                    if (unit.level >= 2) then
                        return true
                    else
                        return false
                    end
                end
            >>
        [/lua]
    [/event]
Then we load it in

Code: Select all

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

            ca_score=110002
            # focuses on me cause I am canrecruit=yes
            [filter_second]
                lua_function = "prioritise_player_leader"
            [/filter_second]
        [/micro_ai]

        # focus on my high level units
        [micro_ai]
            side=3
            ai_type=simple_attack
            action=add

            ca_score=110001
            [filter_second]
                lua_function = "high_level_unit"
            [/filter_second]
        [/micro_ai]
Adding the custom functions is not that hard. Just needs some basic knowledge on eventWML and MAI code structure.

For further inquiries, feel free to reach out to me here or in the the Wesnoth UMC discord (invite link on request).
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
User avatar
Helmet
Posts: 507
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: New micro_ai: attack_enemy_leader

Post by Helmet »

Thanks!
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Celtic_Minstrel
Developer
Posts: 1957
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: New micro_ai: attack_enemy_leader

Post by Celtic_Minstrel »

Or you could do it without any Lua coding at all.

Code: Select all

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

            ca_score=110002
            # focuses on me cause I am canrecruit=yes
            [filter_second]
                formula="id = 'Jahin' "
            [/filter_second]
[/micro_ai]
# focus on my high level units
[micro_ai]
            side=3
            ai_type=simple_attack
            action=add

            ca_score=110001
            [filter_second]
                formula="level >= 2"
            [/filter_second]
[/micro_ai]
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.
User avatar
Helmet
Posts: 507
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: New micro_ai: attack_enemy_leader

Post by Helmet »

Celtic_Minstrel wrote: November 21st, 2022, 2:54 pm Or you could do it without any Lua coding at all...
Wow, I just tried this code in a new scenario. It works great.

I was under the impression that it had to be encased within an [ai] tag within a [side] tag, but that is not accurate. I tossed it into a start event all by itself and it worked like a charm, just like the Wiki indicated.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
Post Reply