I'm studying lua AI, and I have problems with the following

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Post Reply
pku_ai_study
Posts: 1
Joined: June 13th, 2015, 8:40 am

I'm studying lua AI, and I have problems with the following

Post by pku_ai_study »

Code: Select all

local keeps = wesnoth.get_locations 
            {
                terrain = 'K*,K*^*,*^K*', -- Keeps
                x = '1-'..width,
                y = '1-'..height,
                { "not", { {"filter", {}} }}, -- That have no unit
                { "not", { radius = 6, {"filter", { canrecruit = 'yes',
                    { "filter_side", { { "enemy_of", {side = wesnoth.current.side} } } }
                }} }}, -- That are not too close to an enemy leader
                { "not", {
                    x = leader.x, y = leader.y, terrain = 'K*,K*^*,*^K*',
                    radius = 3,
                    { "filter_radius", { terrain = 'C*,K*,C*^*,K*^*,*^K*,*^C*' } }
                }}, -- That are not close and connected to a keep the leader is on
                { "filter_adjacent_location", {
                    terrain = 'C*,K*,C*^*,K*^*,*^K*,*^C*'
                }} -- That are not one-hex keeps
            }
I am just wondering what the params of the function get_locations mean, and how can I define those params.
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: I'm studying lua AI, and I have problems with the follow

Post by mattsc »

wesnoth.get_locations() takes a Standard Location Filter (SLF) as defined on the wiki here. So that will tell you what the parameters mean.

As for the syntax, a WML tag such as

Code: Select all

[tag]
    key1=value1
    key2=value2
[/tag]
is coded in Lua like this

Code: Select all

{ "tag", { key1 = value1, key2 = value2 } }
Formatted somewhat differently (possibly easier to decipher), since line breaks don't matter in Lua, you could also write it like this

Code: Select all

{ "tag", 
    {
        key1 = value1,
        key2 = value2
    }
}
Note that the "", the commas and all the brackets are important and cannot be omitted.

Thus, the beginning of your example is the Lua version of the following WML code (for simplicity for this example, I assumed that the Lua variable 'width' equals 20, and 'height' equals 15):

Code: Select all

    terrain = 'K*,K*^*,*^K*'    # Keeps
    x = 1-20
    y = 1-15
    [not]    # That have no unit
        [filter]
        [/filter]
    [/not]
    [not]    # That are not too close to an enemy leader
        radius = 6
        [filter]
            canrecruit = yes
            [filter_side]
                [enemy_of]
                    side = $side_number
                [/enemy_of]
            [/filter_side]
       [/filter]
    [/not]
    ...
Hope that helps and it's always great having somebody trying to work on Wesnoth AIs!
Post Reply