Lua location syntax

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

Moderator: Forum Moderators

User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Lua location syntax

Post by ZombieKnight »

Hi
how to set facing in animation:add?
https://wiki.wesnoth.org/LuaAPI/wesnoth ... e_animator
(I'd like unit to face x,y)

Code: Select all

... , facing = ???
Last edited by ZombieKnight on May 31st, 2024, 3:30 pm, edited 2 times in total.
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Lua location syntax

Post by white_haired_uncle »

Is this not enough?

Code: Select all

 animator:add(unit, flag, hits, { facing = {x = 3, y = 12} })
Speak softly, and carry Doombringer.
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

Thanks ^^
I couldn't find the right syntax.
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

white_haired_uncle wrote: May 31st, 2024, 10:34 am Is this not enough?

Code: Select all

 animator:add(unit, flag, hits, { facing = {x = 3, y = 12} })
No, this code doesn't work (playies animation, but doesn't care what is in facing)

Code: Select all

animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], facing = {x = 18, y = 28} })
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Lua location syntax

Post by Celtic_Minstrel »

Try the name of a direction, for example facing = 'nw'.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

Celtic_Minstrel wrote: May 31st, 2024, 11:20 pm Try the name of a direction, for example facing = 'nw'.
It doesn't work for me :( ...

Code: Select all

animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]],facing = 'nw'})
Can someone test this code (to see if the problem is on my side)?
(this code is tuned from :lua wesnoth.require("wml-utils").handle_event_commands(wml.tovconfig(wml.load("~add-ons/Bandits_from_Brown_Hills/test_WML_event.cfg"))))

Code: Select all

[store_unit]
    [filter]
        id="BfBH Fugitive-79"
    [/filter]
    variable = exploitation_units
[/store_unit]
[store_unit]
    [filter]
        id="BfBH Dwarvish Berserker-81"
    [/filter]
    variable = unit
[/store_unit]
[lua]
    code=<<
local attacker= wesnoth.units.get(wml.variables["exploitation_units[0].id"])
local defender= wesnoth.units.get(wml.variables["unit[0].id"])
local defender_t= wml.variables["unit[0]"]
local attacking_weapon_index = 0
local i = 0
while wml.variables["exploitation_units[0].attack["..i.."]"] ~= nil do
    local weapon = wml.find_child(wml.variables["exploitation_units[0].attack["..i.."]"],"specials",{{"dummy",{id="exploitation"}}})
    if weapon ~= nil then
        attacking_weapon_index = i + 1
        break
    end
    i = i + 1
end
defender.attacks[(#defender.attacks + 1)] = {}
local _, _, attacker_stats, defender_stats =wesnoth.simulate_combat(attacker,attacking_weapon_index, defender, #defender.attacks)
defender.attacks[#defender.attacks] = nil
for key, value in pairs(attacker_stats) do
    wml.variables["attacker_stats."..key] = tonumber(value) or tostring(value)
end
for key, value in pairs(defender_stats) do
    wml.variables["defender_stats."..key] = tonumber(value) or tostring(value)
end
>>
[/lua]
[lua] #TODO make lua animation
    code=<<
--making vars , v for shorter code
local v=wml.variables
local animation = wesnoth.units.create_animator()
local attacker= wesnoth.units.get(v["exploitation_units[0].id"])
local defender= wesnoth.units.get(v["unit[0].id"])
local attacking_weapon_index
animation:add(defender, "defend", "miss",{with_bars = false, primary = defender.attacks[v["defender_stats.name"]], secondary = defender.attacks[v["defender_stats.name"]]})
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]],facing = 'nw'})
animation:run()
animation:clear()
    >>
[/lua]
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Lua location syntax

Post by white_haired_uncle »

Try "target" instead of "facing", and probably use { x = X, y = Y }.
Speak softly, and carry Doombringer.
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

Thanks, I'll try tommorrow!
(sry for delay, internet wasn't really working today)
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

white_haired_uncle wrote: June 1st, 2024, 4:31 pm Try "target" instead of "facing", and probably use { x = X, y = Y }.
Like this target = {x=19,y=28}?
Doesn't work
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

Got it!!!
Got to dig inside WML tag implementation to find it.
Now it's clear animation:add() DOESN'T take facing as a parameter, but target.
The whole doc of the animation:add should be added (where to contact someone who can edit docs?)

My Working code:

Code: Select all

local facing = {x = 17, y = 30}
local unit = attacker
local facing_loc = wesnoth.map.find(facing)[1]
local dir = wesnoth.map.get_relative_dir(unit.x, unit.y, facing_loc[1], facing_loc[2])
unit.facing = dir
facing = wesnoth.map.get_direction(unit.x, unit.y, dir)
animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = facing})
It's taken from lua wml.actions.animate_unit.
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Lua location syntax

Post by white_haired_uncle »

Did/could you try this:

Code: Select all

animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = wesnoth.map.find{x = 17, y = 30}[1])
Speak softly, and carry Doombringer.
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

white_haired_uncle wrote: June 2nd, 2024, 11:21 am Did/could you try this:

Code: Select all

animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = wesnoth.map.find{x = 17, y = 30}[1])
Doesn't work
white_haired_uncle
Posts: 1456
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Lua location syntax

Post by white_haired_uncle »

Hmm. I think we can tell that the wiki is wrong and it should be "target", not "facing" from src/scripting/game_lua_kernel.cpp. And it appears target= needs a location, but I can't find a solid definition of location.

Based on the output of [store_locations], it appears a location is just { x=X, y=Y, terrain=TERRAIN}, though some comments seem to imply that just x,y is enough. Perhaps a terrain is necessary?

Could you try this, maybe substituting in the correct terrain?


animation:add(attacker, "attack", "miss",{with_bars = false, primary = attacker.attacks[v["attacker_stats.name"]], secondary = attacker.attacks[v["attacker_stats.name"]], target = { x = 17, y = 30, terrain = "Gg"})
Speak softly, and carry Doombringer.
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Lua location syntax

Post by Celtic_Minstrel »

The documentation is here for the record.
white_haired_uncle wrote: June 2nd, 2024, 12:03 pm Hmm. I think we can tell that the wiki is wrong and it should be "target", not "facing" from src/scripting/game_lua_kernel.cpp. And it appears target= needs a location, but I can't find a solid definition of location.
It should accept "facing" which is a direction, eg 'nw'. Thus this seems like it might be a bug. It's fine for it to accept "target" as an alternative though, taking a location. From the sounds of it, only the latter is currently supported?

A location is either a two-element array, eg {3, 5}, or anything that contains x and y tags, eg {x = 3, y = 5}. It does not require terrain, and usually does not have that. There's no need to use wesnoth.map.get or wesnoth.map.find to get a hex reference.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 371
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: Lua location syntax

Post by ZombieKnight »

Celtic_Minstrel wrote: June 3rd, 2024, 3:26 am The documentation is here for the record.
white_haired_uncle wrote: June 2nd, 2024, 12:03 pm Hmm. I think we can tell that the wiki is wrong and it should be "target", not "facing" from src/scripting/game_lua_kernel.cpp. And it appears target= needs a location, but I can't find a solid definition of location.
It should accept "facing" which is a direction, eg 'nw'. Thus this seems like it might be a bug. It's fine for it to accept "target" as an alternative though, taking a location. From the sounds of it, only the latter is currently supported?
Yes.

A location is either a two-element array, eg {3, 5}, or anything that contains x and y tags, eg {x = 3, y = 5}. It does not require terrain, and usually does not have that. There's no need to use wesnoth.map.get or wesnoth.map.find to get a hex reference.
If you look-up for the code I've posted, you discover, you aren't right (that code is from animate_unit implementation).
(Or it just doesn't work in animator)
Post Reply