Building up a lua AI (for Nightmares of Meloen)

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
User avatar
FAAB
Inactive Developer
Posts: 52
Joined: November 15th, 2008, 12:15 pm

Building up a lua AI (for Nightmares of Meloen)

Post by FAAB »

Trying to improve the experience of playing Nightmares of Meloen, I have started to write some formule AI routines so that the AI takes into account some of the specials and abilities of the faction.
As I am not knowledgeable in AI development nor do I know anything of wesnoth's formula AI, my questions are numerous and I have been suggested on the IRC channel to open my own thread on this forum.
So here it is.

The first task is to make the AI jinx/curse aware.
The jinx weapon special will curse an unit when hit. A cursed unit will see its defense be lowered by 15% until it is healing on a village or is cured by a healer with cure ability. Only living units can be cursed (no undead, nightmares, ...).
The AI should first be able to use the jinx special (when controlling Nightmares of Meleon) to:
  • - avoid using it on not living units
    - avoid using it on already cursed units
    - to some extend, avoid using it on units on village
The second task for the AI is to get the cursed units healed (when facing Nightmares of Meloen).

Currently my AI contains these two candidate actions. As I failed to put them into the same stage (the second action seems never executed), I put them in two different stage definition blocks. Hence my structure looks like (omitting the fallback stage):

Code: Select all

    [stage]
        engine=cpp
        name=testing_ai_default::candidate_action_evaluation_loop #this is candidate action evaluation loop stage
        [candidate_action]
            engine=fai
            name=jinx
            type=attack # some directives on how to use jinx special when attacking
            [...]
        [/candidate_action]
    [/stage]
    [stage]
        engine=cpp
        name=testing_ai_default::candidate_action_evaluation_loop
        [candidate_action]
            engine=fai
            name=cursed
            type=movement # some directives on how to handle cursed units when moving
            [...]
        [/candidate_action]
    [/stage]
:?: My first question is about the type to put in the candidate action. While in the first one the type=attack came from an example in trunk, for the second, type=movement is just a guess of mine. Was I just lucky to get it right or is any type valid ?
:arrow: So it appears I have been lucky. As specified in this page, the only valid types are attack and movement.

The first stage is containing some AI logic for picking up a target when attacking. The second one is for healing units. In order to give priority to the different evaluated targets, each one receives a score based on the expected benefit of the attack within AI's strategy, the best moves getting a higher score. All the possible attack scores get a common offset AI_CA_COMBAT_SCORE (=100,000) so that they get run in one block. Similarly all the healing actions are evaluated and given a score around the healing score offset AI_CA_HEALING_SCORE (=80,000).

:?: Now my question is this one: considering a case where the formula AI evaluates and selects some healing moves, how comes the attack moves selected by the default AI on the fallback stage are executed after the healing moves selected by the formula AI despite them having a higher score (their offset is higher) ? Would is be so that all stages are executed in sequence and their candidate actions are performed without being mixed with another stage's candidate actions ?

Forgive me for the mix-up in terms and poor phrasing but the topic is quite new to me. I am still hoping some can get my meaning.

The complete dump of the current version of my formula AI can be found here.

[last edit, November 9th] change title
[old edits]
- answer the first question
- add some detail on context
Last edited by FAAB on November 9th, 2010, 4:47 am, edited 2 times in total.
User avatar
FAAB
Inactive Developer
Posts: 52
Joined: November 15th, 2008, 12:15 pm

Re: Nightmares of Meloen's formula AI thread

Post by FAAB »

I have tested the Nightmares of Meloen's AI on BFW 1.9 (rev. 41919) and the candidate action that let the cursed and poisoned units to retreat on village for curing does not work. On the other hand, the same code on the version BFW 1.8 is working fine.

In order to investigate more easily this problem, I have created a scenario and a simple AI configuration that performs the same action on poisoned units. This scenario can be loaded into the [wiki]AI arena[/wiki]. The patch file put in attachment has been generated on the revision 41919 of the trunk repository. It can be deployed from the directory where your checked out wesnoth trunk by running the following command

Code: Select all

patch -p0 -i PATH_OF_DOWNLOAD/ai_arena.diff
The test to select in the AI arena is called Retreat to cure test and the AI configuration to load formulaAI-based cure poison improvements. The scenario contains two poisoned mages to should go on village to heal. The AI only implements retreating poisoned units on village and do no have any fallback stage.

While the mages actually run on village with the AI on BFW 1.8, on BFW 1.9, they stay on the initial position with the following message in the console:

Code: Select all

ERROR: type error:  expected object but found null ((null)0)
  
  if
  move_partial
formula type error: type error:  expected object but found null ((null)0)
This problem is fixed in BFW 1.9 trunk (by revision 42102).

[EDIT] give fix SVN revision.
Attachments
ai_arena.diff
Scenario and AI configuration for AI arena
(4.06 KiB) Downloaded 681 times
Last edited by FAAB on April 12th, 2010, 3:44 am, edited 2 times in total.
We are highly interested in translation for Nightmares of Meloen. Incomplete translations are appreciated too!
So if anyone is kind enough to contribute for a translation, here is the po file to use.
User avatar
Crab
Inactive Developer
Posts: 200
Joined: March 18th, 2009, 9:42 pm

Re: Nightmares of Meloen's formula AI thread

Post by Crab »

the bug with formula_ai is fixed.

be sure to read http://wiki.wesnoth.org/Customizing_AI_in_Wesnoth_1.8 in more detail, or clarify it where needed.
the priorities are only compared within the same stage. to allow lower-priority actions to be executed, you need to return <0 in evaluation of higher-priority actions if there's nothing good to do for them.
User avatar
FAAB
Inactive Developer
Posts: 52
Joined: November 15th, 2008, 12:15 pm

lua AI

Post by FAAB »

I have rewritten the attacking part of the formula AI logic in lua.
That was a tedious job as a lot of convenience functions are not available.
The resulting code is available here. Not that it counts as standard lua coding but it might be interesting for anyone trying to write a lua AI.

As a comparison, the latest formula AI code is available splitted in three files.
Post Reply