Yet another basic lua query

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

Moderator: Forum Moderators

Post Reply
User avatar
Spannerbag
Posts: 952
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Yet another basic lua query

Post by Spannerbag »

Hi,
getting lost in the API. :x

I have this code (which works fine):

Code: Select all

function wesnoth.wml_conditionals.ret_check(cfg)
  local unit = wesnoth.units.find_on_map(cfg)[1]
  local attm = 0				-- Total melee attacks
  local attr = 0				-- Total ranged attacks

  if not unit then return false end		-- No filter match

  for i=1,#unit.attacks do
    if unit.attacks[i].range=="melee" and unit.attacks[i].name ~= "slam" and not string.match(unit.attacks[i].name, "^petrifyturn") then attm = attm +1 end
    if unit.attacks[i].range=="ranged" then attr = attr +1 end
  end
...
I'd like to also exclude a melee attack with a custom special:

Code: Select all

[dummy_stunmove]
  id=stunmove
  name= _ "stunmove"
  female_name= _ "female^stunmove"
...
However this special might not be the only attack special, so is there an elegant way to trap this and exclude it same way as "slam" and "petrifyturn"(s)?

Can't use attack name as this special is appended to an existing attack and not all units with this attack will have this custom special.
I'm thinking something like and not unit.attacks[i].specials.dummy_stunmove.id (assuming nonexistent value=false) but really not sure of syntax here.
Would some sort of weapon filter be a better way to go?
Originally I'd thought there'd only be the one exclusion ("slam") but there are a few now... :augh:

Any help gratefully received!

Cheers!
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2487
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Yet another basic lua query

Post by Celtic_Minstrel »

Spannerbag wrote: July 28th, 2026, 5:11 pm I'm thinking something like and not unit.attacks[i].specials.dummy_stunmove.id (assuming nonexistent value=false) but really not sure of syntax here.
That won't work (the specials are exposed to the Lua API, but as an array, not a mapping).

I think something like this should work:

Code: Select all

if unit.attacks[i]:matches{special_type = 'dummy_stunmove'} then
or

Code: Select all

if unit.attacks[i]:matches{special_id = 'stunmove'} then
You could search unit.attacks[i].specials for a match, but I think using matches would be easier.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Spannerbag
Posts: 952
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Yet another basic lua query

Post by Spannerbag »

Wow, very helpful, thanks.

So where I now have:

Code: Select all

if unit.attacks[i].range=="melee"
 and unit.attacks[i].name ~= "slam"
 and not string.match(unit.attacks[i].name, "^petrifyturn")
 then attm = attm +1 end
I just append something like:
unit.attacks[i]:matches{special_id = 'stunmove'}

So new statement is:

Code: Select all

if unit.attacks[i].range=="melee"
 and unit.attacks[i].name ~= "slam"
 and not string.match(unit.attacks[i].name, "^petrifyturn")
 and not unit.attacks[i]:matches{special_id = 'stunmove'}            # Exclude "stunmove" WS
 then attm = attm +1 end
Many thanks again for your assistance, saved me ages of thrashing around!
Very much appreciated. :D
Cheers!
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply