WFL get adjecent units

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.
Post Reply
User avatar
ZombieKnight
Posts: 285
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

WFL get adjecent units

Post by ZombieKnight »

Hi,
I'm new to WFL.
Do you know how to get all adjacent units, fulfilling certain conditions(enemy=yes)?
(I don't really need to store them, I need sum(count)of them)
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
white_haired_uncle
Posts: 1313
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: WFL get adjecent units

Post by white_haired_uncle »

WFL? You couldn't just use [store_unit] and a filter to store them to an array and then take its length?
Speak softly, and carry Doombringer.
User avatar
ZombieKnight
Posts: 285
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: WFL get adjecent units

Post by ZombieKnight »

white_haired_uncle wrote: May 24th, 2024, 9:07 pm WFL? You couldn't just use [store_unit] and a filter to store them to an array and then take its length?
Yes,...
But not inside [ability][damage]
Maybe I could use lua somehow?
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
white_haired_uncle
Posts: 1313
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: WFL get adjecent units

Post by white_haired_uncle »

inside [abilities][damage] ? (not [ability]).

You MIGHT be able to use [filter_adjacent] https://wiki.wesnoth.org/AbilitiesWML#C ... on_special if you're looking to do something specific like unit is adjacent to exactly one enemy.

I don't know what you're trying to do, but I suspect you'll end up creating attacker_hits/defender_hits events filtering on ability= and using [harm_unit].
Speak softly, and carry Doombringer.
User avatar
ZombieKnight
Posts: 285
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: WFL get adjecent units

Post by ZombieKnight »

white_haired_uncle wrote: May 25th, 2024, 6:47 am You MIGHT be able to use [filter_adjacent] https://wiki.wesnoth.org/AbilitiesWML#C ... on_special if you're looking to do something specific like unit is adjacent to exactly one enemy.
I'd like to [damage] add = $($number_of_adjacent-enemies - 1)
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
ZombieKnight
Posts: 285
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: WFL get adjecent units

Post by ZombieKnight »

This works but only for max +1 damage

Code: Select all

[damage]
        id=battle_frenzy
        name= _ "battle frenzy"
        female_name= _ "female^battle frenzy"
        description=_ "When in the heat of battle, this unit enters a state of frenzy, significantly increasing its attack power.

"
        special_note=_ "TODO"
        add=1
        affect_self=yes
        apply_to=self
        [filter_self]
            [filter_adjacent]
                is_enemy=yes
                count=2-6
            [/filter_adjacent]
        [/filter_self]
    [/damage]
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
white_haired_uncle
Posts: 1313
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: WFL get adjecent units

Post by white_haired_uncle »

At the beginning of the attack and every attacker_hits/defender_hits, check the number of adjacent units and adjust the damage accordingly.

Code: Select all

$stored_unit.abilities.damage[$i].add, where $stored_unit.abilities.damage$[i].id=="battle_frenzy"
(or something close to that)
Speak softly, and carry Doombringer.
User avatar
Celtic_Minstrel
Developer
Posts: 2295
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: WFL get adjecent units

Post by Celtic_Minstrel »

ZombieKnight wrote: May 24th, 2024, 6:30 pm Do you know how to get all adjacent units, fulfilling certain conditions(enemy=yes)?
If you need to do this all in a single formula, I think something like the following should work (untested):

Code: Select all

size( filter( filter( map( adjacent_locs(loc(x,y)), 'loc', unit_at(loc)), 'maybe_unit', maybe_unit != null()), hitpoints < max_hitpoints / 2))
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 285
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: WFL get adjecent units

Post by ZombieKnight »

Poor, there's no easy formula for it...
Since there's a max of 6 adjacent units, I've made a workaround with 5 abilities, each for count 2,3,4,5, or 6.
Thanks...
Celtic_Minstrel wrote: May 27th, 2024, 12:34 am

Code: Select all

size( filter( filter( map( adjacent_locs(loc(x,y)), 'loc', unit_at(loc)), 'maybe_unit', maybe_unit != null()), hitpoints < max_hitpoints / 2))
Out of curiosity (and I'd really like to learn WFL),
1 Could you explain me how is that supposed to work.
2 Why you count hitpoints there?

Thanks
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
User avatar
Celtic_Minstrel
Developer
Posts: 2295
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: WFL get adjecent units

Post by Celtic_Minstrel »

ZombieKnight wrote: May 27th, 2024, 5:39 am Out of curiosity (and I'd really like to learn WFL),
1 Could you explain me how is that supposed to work.
  1. adjacent_locs(loc(x,y) gets all the locations adjacent to the unit under consideration (x,y is equivalent to self.x,self.y here).
  2. map(..., 'loc', unit_at(loc)) converts the list of locations to a list of the units on those locations (with a null for empty hexes).
  3. filter(..., 'maybe_unit', maybe_unit != null()) removes all the nulls from the list so you have only units remaining.
  4. filter(..., hitpoints < max_hitpoints / 2) filters the list down to units with less than half their hitpoints.
  5. size(...) returns the length of the resulting list.
ZombieKnight wrote: May 27th, 2024, 5:39 am 2 Why you count hitpoints there?
That was just a random condition I threw in. It could be replaced with pretty much anything you want. I think you originally said you wanted to filter for enemies, so that would probably look like 'unit', enemy_of(self, unit) instead of hitpoints < max_hitpoints / 2.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 285
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: WFL get adjecent units

Post by ZombieKnight »

Interesting!
Had no idea it can work like this. ^^
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
dwarftough
Posts: 494
Joined: August 4th, 2019, 5:27 pm
Contact:

Re: WFL get adjecent units

Post by dwarftough »

Celtic_Minstrel wrote: May 27th, 2024, 12:34 am If you need to do this all in a single formula, I think something like the following should work (untested):

Code: Select all

size( filter( filter( map( adjacent_locs(loc(x,y)), 'loc', unit_at(loc)), 'maybe_unit', maybe_unit != null()), hitpoints < max_hitpoints / 2))
Seems like WFL is a functional programming language inside Wesnoth. Haven't really thought about it that way before
Co-founder and current maintainer of IsarFoundation, Afterlife Rated and overall Wesnoth Autohost Project
Developer and maintainer of my fork of World Conquest, Invincibles Conquest II
User avatar
Celtic_Minstrel
Developer
Posts: 2295
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: WFL get adjecent units

Post by Celtic_Minstrel »

Yes, it's a pure functional language inside Wesnoth (pure meaning that there are no functions with side-effects).
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply