[filter_formula]

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.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

[filter_formula]

Post by Helmet »

[filter_formula] seems extremely useful. It was added to WML recently, starting with BfW version 1.17.6.

I've been tinkering with it, but haven't been able to accomplish anything. I think I need some functional examples using Wesnoth Formula Language.

If you know how to use [filter_formula], please post some examples. For example, how would you use the filter to make an event fire at "dusk" every day?

Any other examples using [filter_formula] would also be appreciated. The more the merrier. I'd especially like to see an example that uses event_data.

Thanks.
From the Wiki:
filter_formula (Version 1.17.6 and later only)
Similar to [filter_condition], but the condition is expressed in Wesnoth Formula Language. The formula has access to the following keys:
Event information:
event - the event name
event_id
event_data
loc, unit - primary event location
second_loc, second_unit - secondary event location
weapon, second_weapon
Gamestate information:
turn_number
time_of_day - the time of day ID
side_number - currently active side
sides - a list of all sides
units - a list of all units on the map
map - the entire game map as a two-dimensional array
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: [filter_formula]

Post by Helmet »

Yikes. That is about 100 times more complicated than what I was doing. I was waaaay off.

The odds of me using [filter_formula] in my BfW campaign just plummeted like a paralyzed falcon.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [filter_formula]

Post by Ravana »

It isnt even [filter_formula], its key not tag.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: [filter_formula]

Post by Celtic_Minstrel »

Helmet wrote: November 30th, 2022, 8:33 pm Yikes. That is about 100 times more complicated than what I was doing. I was waaaay off.
What are you talking about? There's nothing complicated there… though the two examples in the tests probably aren't very useful in practice.

Here's some examples that might actually have practical application (untested):

Code: Select all

[event]
	# Event triggers if a heavily injured unit (<25% hp) moves to a keep
	name=moveto
	filter_formula="unit.hitpoints < 0.25 * unit.max_hitpoints and unit.terrain.keep"
	# Or, an alternate way of writing the same thing
	filter_formula="unit.hitpoints < 0.25 * unit.max_hitpoints and map.gamemap[loc].keep"
[/event]

[event]
	# Event triggers if a falcon is attacked by a ranged weapon
	name=attack
	filter_formula="unit.race = 'falcon' and weapon.range = 'ranged'"
[/event]

[event]
	# Event triggers if you move within 6 hexes of (7,8)
	name=moveto
	filter_formula="distance_between(loc, loc(7,8)) < 6"
[/event]

[event]
	# Event triggers if a unit at full health is killed in one hit
	name=attacker hits
	filter_formula="second_unit.hitpoints = second_unit.max_hitpoints and event_data.damage_inflicted >= second_unit.max_hitpoints"
[/event]
I also edited the wiki to add some explanation of what event_data is in general.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: [filter_formula]

Post by Helmet »

Celtic_Minstrel wrote: December 1st, 2022, 2:36 am Here's some examples that might actually have practical application (untested):
Thank you. Those examples were helpful.
Celtic_Minstrel wrote: December 1st, 2022, 2:36 am I also edited the wiki to add some explanation of what event_data is in general.
I'll post that information below, for anybody else reading the thread.
From the Wiki:
event_data - additional information specific to the event, such as owner_side or damage_inflicted, or anything passed in [fire_event][data].
More examples of [filter_formula] in action would be nice.

I expect WML coders will be reading this thread for years, looking for examples.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: [filter_formula]

Post by Helmet »

I tested the untested examples, and only 1 of the 3 examples worked.
Celtic_Minstrel wrote: December 1st, 2022, 2:36 am 1) filter_formula="unit.hitpoints < 0.25 * unit.max_hitpoints and unit.terrain.keep"

2) filter_formula="unit.hitpoints < 0.25 * unit.max_hitpoints and map.gamemap[loc].keep"

3) filter_formula="unit.race = 'falcon' and weapon.range = 'ranged'"
1) Didn't work but the test game was not interrupted.
2) Worked perfectly.
3) Test game crashed.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: [filter_formula]

Post by Celtic_Minstrel »

1 of 3? It looks like you missed two of the examples I gave. That code box with the examples scrolls down.

I'd like more information on the crash.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: [filter_formula]

Post by Helmet »

Celtic_Minstrel wrote: December 1st, 2022, 1:29 pm 1 of 3? It looks like you missed two of the examples I gave. That code box with the examples scrolls down.
Oops. I had missed the other examples. Those are great examples. Would you post a few more? You're good at knowing the exact sort of filters scenario designers like to create.
Celtic_Minstrel wrote: December 1st, 2022, 1:29 pm I'd like more information on the crash.
Okay. I'll show the code I used and tell the result. I re-tested everything just now, in case I had made a mistake yesterday (as it turns out, I did).

Code: Select all

[event]
	# Event triggers if a heavily injured unit (<25% hp) moves to a keep
	name=moveto
    first_time_only=no
	filter_formula="unit.hitpoints < 0.25 * unit.max_hitpoints and unit.terrain.keep"
    [message]
        speaker=unit
        message="I am hurt."
    [/message]
[/event]
The code worked. A unit with only 1 hp entered the keep and said, "I am hurt."
This event didn't work yesterday, but it was my fault. I had left out the first_time_only=no while play-testing.

Code: Select all

[event]
	# Event triggers if a heavily injured unit (<25% hp) moves to a keep
	name=moveto
    first_time_only=no
	filter_formula="unit.hitpoints < 0.25 * unit.max_hitpoints and map.gamemap[loc].keep"
    [message]
        speaker=unit
        message="I am hurt."
    [/message]
[/event]
The code worked. Same result as above.

Code: Select all

[event]
	# Event triggers if a falcon is attacked by a ranged weapon
	name=attack
    first_time_only=no
	filter_formula="unit.race = 'falcon' and weapon.range = 'ranged'"
    [message]
        speaker=MyLeader
        message="The falcon was attacked by a ranged weapon!"
    [/message]
[/event]
In this test, an Elvish Archer (side 1, attacker) used a ranged attack on a Falcon (side 2). The test game crashed. I checked the core code to make sure "falcon" was the race, and it is correct.

Code: Select all

[event]
	# Event triggers if you move within 6 hexes of (7,8)
	name=moveto
	filter_formula="distance_between(loc, loc(7,8)) < 6"
    [message]
        speaker=unit
        message="I moved within 6 hexes of hex (7,8)."
    [/message]
[/event]
The code worked. In the test, any unit that moves triggers the event when expected.

Code: Select all

[event]
	# Event triggers if a unit at full health is killed in one hit
	name=attacker hits
    first_time_only=no
	filter_formula="second_unit.hitpoints = second_unit.max_hitpoints and event_data.damage_inflicted >= second_unit.max_hitpoints"
    [message]
        speaker=unit
        message="I killed the enemy in one hit!"
    [/message]
[/event]
A Dwarvish Ulfserker (side 1, attacker) did not trigger the event when killing a Ruffian (side 2) by multiple hits. This is correct behavior.
A Sea Serpent (side 1, attacker) has no choice but to kill a Ruffian (side 2) in a single hit. But the game crashed when the Sea Serpent attacked.
The Sea Serpent should have said, "I killed the enemy in one hit!"

I thought Mac OS finally got a crash log in this latest version of BfW, but when I clicked the "i" and looked in the logs folder, it was empty.
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: [filter_formula]

Post by Helmet »

Celtic_Minstrel wrote: December 1st, 2022, 2:36 am What are you talking about? There's nothing complicated there…
It's kinda funny, after you ridiculed me for finding something complicated, that your subsequent code didn't work. :)

Actually, it's reassuring to know that even the developers sometimes write code with critical errors.

I've tried several different things to get the falcon code you wrote to run properly, but nothing I tried helped.

I tried attack.range, for example, and second_unit.race = 'falcon' plus some silly ideas that were long-shots.

It would be awesome if your code worked. Do you know what's wrong with it? Does anybody know?

Code: Select all

[event] # DOES NOT WORK
	# Event should trigger if a falcon is attacked by a ranged weapon
	name=attack
        first_time_only=no
	filter_formula="unit.race = 'falcon' and weapon.range = 'ranged'"
        [message]
            speaker=MyLeader
            message="The falcon was attacked by a ranged weapon!"
        [/message]
[/event]
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [filter_formula]

Post by Ravana »

Helmet wrote: December 2nd, 2022, 5:40 pm It's kinda funny, after you ridiculed me for finding something complicated, that your subsequent code didn't work. :)
I wouldnt count filter_formula="turn_number = side_number" and filter_formula="loc.x = loc.y" complicated.
User avatar
beetlenaut
Developer
Posts: 2814
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: [filter_formula]

Post by beetlenaut »

Helmet wrote: December 2nd, 2022, 5:40 pm It would be awesome if your code worked. Do you know what's wrong with it? Does anybody know?
There must be a bug in the engine around this key. No error in WML code should crash the whole game, but several formulas apparently do, including any one using 'weapon'. There is an error message in the terminal:
Battle for Wesnoth wrote: Caught general 'St12bad_weak_ptr' exception: bad_weak_ptr
I'm pretty sure C_M meant that formula to have second_unit.race='falcon', but it was marked "untested" after all. For the time being, you will have to take out the weapon clause if you want it to do something.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
Helmet
Posts: 641
Joined: December 19th, 2006, 5:28 pm
Location: Florida, USA

Re: [filter_formula]

Post by Helmet »

Ravana wrote: December 2nd, 2022, 8:05 pm I wouldnt count filter_formula="turn_number = side_number" and filter_formula="loc.x = loc.y" complicated.
I was confused about filter_formula in the beginning of this thread, but it makes a lot more sense now, thanks to Celtic_Minstrel's examples.
beetlenaut wrote: December 3rd, 2022, 2:41 am I'm pretty sure C_M meant that formula to have second_unit.race='falcon', but it was marked "untested" after all. For the time being, you will have to take out the weapon clause if you want it to do something.
I like it when new code tags and keys and so forth get added to WML. Developers clearly want to make WML easier to use and more powerful, which is great. I probably should've waited until 1.17 was stable before I began experimenting, though. I've crashed Wesnoth a dozen different ways using filter_formula.

Huh? The green text in my signature went back to plain old black. :(

Anyhow... filter_formula is amazing, and I bet all of Celtic_Minstrel's code examples will work after 1.17 is stable. :)
Author of:
DIY Campaign, Confederacy of Swamp Creatures: Big Battle 1, Confederacy of Swamp Creatures: Big Battle 2, Frogfolk Delivery Service, The Pool of Ek.
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [filter_formula]

Post by Ravana »

Thats the purpose of dev versions. If you dont try the features, issues wont be found, and once it is already stable, it cant be changed since it would break compatibility.
User avatar
beetlenaut
Developer
Posts: 2814
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: [filter_formula]

Post by beetlenaut »

Helmet wrote: December 4th, 2022, 1:58 am I've crashed Wesnoth a dozen different ways using filter_formula.
This is why I didn't file a bug report on GitHub. As far as I can tell, using "weapon" doesn't ever work, but I don't know what other things you have tried that also crash the game. That may be useful information, so you should probably be the one to file that report. Assuming that you do, you should mention the error message about pointers that I posted. That would save the developers some time and make sure they were targeting the actual problem you are having.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
Post Reply