Random AI recruitment

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
Rya
Posts: 350
Joined: September 23rd, 2009, 9:01 am

Random AI recruitment

Post by Rya »

Hey guys, I'm back for a little and was wondering if there has been any progress made with AI recruitment? I still seem to be facing the same problem that the AI recruits only the best units.

Now I used to do the following:

Code: Select all

[ai]
  recruitment_pattern=
  recruitment_ignore_bad_combat=yes
  recruitment_ignore_bad_movement=yes
[/ai]
But the wiki says bad_movement is obsolete now and bad_combat doesn't seem to work either. I've made several test runs now and even with these settings the AI will still always recruit the same units (always the best one of each category).

Now I also know that I can use LIMIT_CONTEMPORANEOUS_RECRUITS to get it to recruit other units, but with that there is the problem that the recruitment gets too predictable (I want it fully random) and I also want the AI faction to be selectable by the player, so in that case I'd need to check for each and every faction and write down the limits (lots of writing work).

So my question is: Is there any way now to just make the AI recruit randomly that doesn't require to write 10+ pages of WML scripts or changing the source? Basically the player should be able select a faction for the AI (or random) via the lobby and then that AI should just recruit randomly from whatever recruit list it has.
Wesnoth
The developer says "no".
User avatar
Alarantalara
Art Contributor
Posts: 787
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: Random AI recruitment

Post by Alarantalara »

Progress has been made indeed.

Some work was done this summer, though I don't know how visible it is.

In addition, you will probably want to check out these threads which include discussions of a variety of new recruit strategies.
http://forums.wesnoth.org/viewtopic.php?f=10&t=34976
http://forums.wesnoth.org/viewtopic.php?f=10&t=36642
The ones in those topics do much better than the default recruiting (and they're quite different in what they recruit as well).

You can also set up a simple random recruit AI using Lua. While it's longer than what you have, it's not 10 pages either.

It should look something like this (not tested):

Code: Select all

[ai]
    version=10710
    [engine]
        name="lua"
        code= <<
            local ai = ...
            local init = function(ai)
                local candidate_actions = {}
                local recruit
                
                candidate_actions:recruit_eval = function()
                    local possible_recruits = wesnoth.sides[wesnoth.current.side].recruit
                    recruit =  possible_recruits[math.random(#possible_recruits)]
                    if wesnoth.unit_types[recruit].cost <= wesnoth.sides[wesnoth.current.side].gold then
                        return 180000
                    end
                    return 0
                end
                
                candidate_actions:recruit_exec = function()
                    ai.recruit(recruit)
                end
            end
            return init(ai)
        >>
    [/engine]
    [stage]
        id=main_loop
        name=testing_ai_default::candidate_action_evaluation_loop
        {AI_CA_GOTO}
        #{AI_CA_RECRUITMENT}
        {AI_CA_MOVE_LEADER_TO_GOALS}
        {AI_CA_MOVE_LEADER_TO_KEEP}
        {AI_CA_COMBAT}
        {AI_CA_HEALING}
        {AI_CA_VILLAGES}
        {AI_CA_RETREAT}
        {AI_CA_MOVE_TO_TARGETS}
        {AI_CA_PASSIVE_LEADER_SHARES_KEEP}
    [/stage]
    {MODIFY_AI_ADD_CANDIDATE_ACTION 1 main_loop (
        [candidate_action]
            engine=lua
            name=recruit
            id=recruit
            max_score=180000
            evaluation="return (...):recruit_eval()"
            execution="(...):recruit_exec()"
        [/candidate_action]
    )}
[/ai]
Edit: The above code misses a test to see if a leader is on a keep before recruiting. Because of the way the AI works, this means that the AI above won't recruit on the turn it moves to a keep from a village. The check itself is easy to add.
Rya
Posts: 350
Joined: September 23rd, 2009, 9:01 am

Re: Random AI recruitment

Post by Rya »

Oh my, that seems to exceed my WML abilities. =o
Wish there was a simple ai option so I can do "random_recruit=yes" or something~
Is there no way to make the AI not consider unit quality when recruiting so I can still use the standard recruitment procedure?
Wesnoth
The developer says "no".
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Random AI recruitment

Post by mattsc »

Rya wrote:Is there no way to make the AI not consider unit quality when recruiting so I can still use the standard recruitment procedure?
There is no built-in way to do so, no. You have to write your own little candidate action for it and Alarantalara did that for you in his post. All you need to do is take it and include it in the side definition for the side you want to use it with (or in a macro that then gets included in the [side] tag).

One other thing we could do, since this question comes up from time to time, is to turn Alarantalara's code into a Micro_AIs. Have a brief look at that page and see if that would suit you. If so, it's not a lot of work to set up a random recruit Micro AI and you could use it like this:

Code: Select all

    [event]
        name=prestart

        [micro_ai]
            side=2
            ai_type=custom_recruiting
            action=add
            recruit_type=random
        [/micro_ai]
    [/event]
Let me know if that would work for you and I can set that up pretty easily. This does mean that you'd have to download the AI-Demos add-on for the time being (Micro AIs might get into Wesnoth itself at some point if they prove useful enough, but so far they're only available through that add-on), while Alarantalara's code works by itself.
Rya
Posts: 350
Joined: September 23rd, 2009, 9:01 am

Re: Random AI recruitment

Post by Rya »

Do all players need that add-on then?
Wesnoth
The developer says "no".
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: Random AI recruitment

Post by mattsc »

Rya wrote:Do all players need that add-on then?
If you'd want to use it without copying anything into your add-on, yes. You could copy over the relevant files, then nobody would need the AI-Demos add-on. Also, we're planning to have the Micro AIs mainlined eventually, so once that happens, you won't need anything at all, you can simply use the [micro_ai] tag.
Post Reply