dabber's questions: vision to location

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
beetlenaut
Developer
Posts: 2827
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: dabber's questions: unique object ids

Post by beetlenaut »

Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
dabber
Posts: 464
Joined: April 2nd, 2014, 6:41 pm

Re: dabber's questions: unique object ids

Post by dabber »

gfgtdf wrote: September 19th, 2023, 11:08 amwhile [object] id= needs to be unique, [modify_unit][object] id= doesn't need that, so i think you can just use [modify_unit][object]
It seems I don't understand properly. I thought the below did that in compliance with the wiki, but the object does not get removed from anyone. Can you explain what's wrong?

Code: Select all

#define RESTRICT_TO_KEEP_MODIFY_OBJECT_REMOVE UNIT_ID
    [modify_unit]
        [filter]
            id={UNIT_ID}
        [/filter]
        [remove_object]
            id={UNIT_ID}
            object_id=restrict_to_keep
        [/remove_object]
    [/modify_unit]
#enddef

#define RESTRICT_TO_KEEP_MODIFY_OBJECT UNIT_ID
    [modify_unit]
        [filter]
            id={UNIT_ID}
        [/filter]
        [object]
            id=restrict_to_keep
            [effect]
                apply_to=movement_costs
                replace="yes"
                [movement_costs]
                    flat={UNREACHABLE}
                [/movement_costs]
            [/effect]
        [/object]
    [/modify_unit]
#enddef
beetlenaut wrote: September 19th, 2023, 7:11 pmYou can replace substrings in WFL.
https://wiki.wesnoth.org/Wesnoth_Formul ... ge#replace
Thank you. I had not found that page on my own.
But it also means there isn't a "single function" to replace spaces with underscores - I have to while loop, find, replace.
User avatar
beetlenaut
Developer
Posts: 2827
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: dabber's questions: unique object ids

Post by beetlenaut »

It's shorter than that. It's a while loop that creates objects with IDs made using a formula with concatenate() and replace(). That should be just a few lines.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
gnombat
Posts: 710
Joined: June 10th, 2010, 8:49 pm

Re: dabber's questions: unique object ids

Post by gnombat »

dabber wrote: September 20th, 2023, 6:26 am

Code: Select all

#define RESTRICT_TO_KEEP_MODIFY_OBJECT_REMOVE UNIT_ID
    [modify_unit]
        [filter]
            id={UNIT_ID}
        [/filter]
        [remove_object]
            id={UNIT_ID}
            object_id=restrict_to_keep
        [/remove_object]
    [/modify_unit]
#enddef
That doesn't look right - is [remove_object] allowed inside [modify_unit]?
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: dabber's questions: unique object ids

Post by gfgtdf »

dabber wrote: September 20th, 2023, 6:26 am
gfgtdf wrote: September 19th, 2023, 11:08 amwhile [object] id= needs to be unique, [modify_unit][object] id= doesn't need that, so i think you can just use [modify_unit][object]
It seems I don't understand properly. I thought the below did that in compliance with the wiki, but the object does not get removed from anyone. Can you explain what's wrong?

Code: Select all

#define RESTRICT_TO_KEEP_MODIFY_OBJECT_REMOVE UNIT_ID
    [modify_unit]
        [filter]
            id={UNIT_ID}
        [/filter]
        [remove_object]
            id={UNIT_ID}
            object_id=restrict_to_keep
        [/remove_object]
    [/modify_unit]
#enddef

#define RESTRICT_TO_KEEP_MODIFY_OBJECT UNIT_ID
    [modify_unit]
        [filter]
            id={UNIT_ID}
        [/filter]
        [object]
            id=restrict_to_keep
            [effect]
                apply_to=movement_costs
                replace="yes"
                [movement_costs]
                    flat={UNREACHABLE}
                [/movement_costs]
            [/effect]
        [/object]
    [/modify_unit]
#enddef
the [remove_object] goes outside of [modify_unit] (like before) only the [object] goes into [modify_unit]

beetlenaut wrote: September 20th, 2023, 9:05 am It's shorter than that. It's a while loop that creates objects with IDs made using a formula with concatenate() and replace(). That should be just a few lines.
While this is certainly possible, it's not that trivial to do, since replace() (according to the linked page at least) only works by offset which need to be calculated first, and cannot directly replace occurances of a string with something else.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
dabber
Posts: 464
Joined: April 2nd, 2014, 6:41 pm

Re: dabber's questions: unique object ids

Post by dabber »

gfgtdf wrote: September 20th, 2023, 1:49 pmthe [remove_object] goes outside of [modify_unit] (like before) only the [object] goes into [modify_unit]
Thank you. That seems incredibly obvious now, but my brain wouldn't find it yesterday.
User avatar
dabber
Posts: 464
Joined: April 2nd, 2014, 6:41 pm

Re: dabber's questions: store reachable location

Post by dabber »

How do I determine if a location is empty? This seems like it should be simple, but I'm not finding it.
I can pre-populate a castle so it is full at the start of the game. But if some random other unit is in the castle, I want to not drop a new unit on top of it, because the new unit then bounces out of the castle. I thought checking vs movement would remove those spots, because you cannot move on top of another unit, but it doesn't. I thought I could add a [filter_location][filter] layer, but that didn't work.

This is the core of what I'm using to find locations. How do I exclude locations with a unit?

Code: Select all

## lock leader into castle, then find where he can move
{RESTRICT_TO_KEEP_OBJECT $populate_recruiter.id}
## give leader enough movement to get anywhere in castle
{MODIFY_UNIT id=$populate_recruiter.id moves 20}
[store_reachable_locations]
    [filter]
        id=$populate_recruiter.id
    [/filter]
    moves=current
    variable=populate_castle
    viewing_side={SIDE}
[/store_reachable_locations]

In case you are curious, this is the whole macro:
Spoiler:
User avatar
Ravana
Forum Moderator
Posts: 3015
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: dabber's questions: store reachable location

Post by Ravana »

[filter_location][not][filter]
User avatar
dabber
Posts: 464
Joined: April 2nd, 2014, 6:41 pm

Re: dabber's questions: store reachable location

Post by dabber »

Thanks. I'm sure I'm missing something, but this seems to accomplish the goal.

Code: Select all

[store_reachable_locations]
    [filter]
        id=$populate_recruiter.id
    [/filter]
    [filter_location]
        [not]
            [filter]
                [not]
                    side=1
                [/not]
            [/filter]
        [/not]
    [/filter_location]
    moves=current
    variable=populate_castle
    viewing_side={SIDE}
[/store_reachable_locations]
I thought I could use an empty inner filter to mean all units, but that gave an error. Adding the [not] side=1 [/not] fixed it, although I don't like it. Isn't a standard unit filter that returns all units just [filter] [/filter] with nothing inside?
I was wrong. I must have messed up in another way. That does work.
Last edited by dabber on September 27th, 2023, 6:29 pm, edited 1 time in total.
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: dabber's questions: store reachable location

Post by gfgtdf »

dabber wrote: September 27th, 2023, 5:13 pm Isn't a standard unit filter that returns all units just [filter] [/filter] with nothing inside?
I thought so too, not sure why it didnt work here.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
User avatar
dabber
Posts: 464
Joined: April 2nd, 2014, 6:41 pm

Re: dabber's questions: store reachable location

Post by dabber »

I apologize for wasting your time. Apparently [filter] [/filter] does work. I thought I tried that yesterday and it didn't work. I definitely tried it today after Ravana's post and it didn't work. But I try it now and it does work.

Big code:

Code: Select all

#define PRE_POPULATE_KEEP_REACHABLE SIDE
    [event]
        name=side 1 turn 1
        ## find a leader (only supports one leader)
        [store_unit]
            [filter]
                canrecruit=true
                side={SIDE}
                [filter_location]
                    terrain=K*
                [/filter_location]
            [/filter]
            variable=populate_recruiter
        [/store_unit]
        [if]
            [variable]
                name=populate_recruiter.length
                greater_than=0
            [/variable]
            [then]
                ## lock leader into castle, then find where he can move
                {RESTRICT_TO_KEEP_OBJECT $populate_recruiter.id}
                ## give leader enough movement to get anywhere in castle
                {MODIFY_UNIT id=$populate_recruiter.id moves 20}
                [store_reachable_locations]
                    [filter]
                        id=$populate_recruiter.id
                    [/filter]
                    ## filter out locations with any unit
                    [filter_location]
                        [not]
                            [filter]
                            [/filter]
                        [/not]
                    [/filter_location]
                    moves=current
                    variable=populate_castle
                    viewing_side={SIDE}
                [/store_reachable_locations]
                ## return leader to normal movement
                {RESTRICT_TO_KEEP_OBJECT_REMOVE $populate_recruiter.id}
                ## no need to change movement back because that will naturally reset on turn
                ## grab side in order to get recruit list
                [store_side]
                    side={SIDE}
                    variable=this_side
                [/store_side]
                ## put recruits into those locations, subtracting gold along the way
                [foreach]
                    array=populate_castle
                    [do]
                        [if]
                            [variable]
                                name=this_item.x
                                not_equals=$populate_recruiter.x
                            [/variable]
                            [or]
                                [variable]
                                    name=this_item.y
                                    not_equals=$populate_recruiter.y
                                [/variable]
                            [/or]
                            [then]
                                {VARIABLE_OP recruit_string rand $this_side.recruit}
                                [unit]
                                    type=$recruit_string
                                    x,y=$this_item.x,$this_item.y
                                    random_traits=yes
                                    side={SIDE}
                                [/unit]
                                [store_unit_type]
                                    type=$recruit_string
                                    variable=recruited_type
                                [/store_unit_type]
                                {VARIABLE gold_spent $recruited_type.cost}
                                {VARIABLE_OP gold_spent multiply -1}
                                [gold]
                                    side={SIDE}
                                    amount=$gold_spent
                                [/gold]
                            [/then]
                        [/if]
                    [/do]
                [/foreach]
            [/then]
        [/if]
        {CLEAR_VARIABLE populate_recruiter,populate_castle,this_side,recruit_string,recruited_type,gold_spent}
    [/event]
#enddef
User avatar
dabber
Posts: 464
Joined: April 2nd, 2014, 6:41 pm

Re: dabber's questions: losing leader but keeping villages

Post by dabber »

When I remove the canrecruit ability from the one unit that can recruit (in an event), that side instantly loses all its villages. Is there a way to prevent that? The side is going to regain a leader later.
User avatar
Ravana
Forum Moderator
Posts: 3015
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: dabber's questions: store reachable location

Post by Ravana »

Not really. You can have fake/unreachable leader. Or you can store the villages and recapture them later.
User avatar
dabber
Posts: 464
Joined: April 2nd, 2014, 6:41 pm

Re: dabber's questions: vision to location

Post by dabber »

Is there a way to trigger an event based on visibility? Specifically, I want to trigger when a unit moves and is now able to see a specific location or a specific second unit. I don't see anything remotely close.
Last edited by dabber on January 23rd, 2024, 1:53 pm, edited 1 time in total.
User avatar
Ravana
Forum Moderator
Posts: 3015
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: dabber's questions: vision to location

Post by Ravana »

[filter_vision]
Post Reply