Filtering units with no advancements (while - have_unit)

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
Araja
Posts: 718
Joined: May 28th, 2009, 9:56 am
Location: Bath, England

Filtering units with no advancements (while - have_unit)

Post by Araja »

Hello all. I've been working on a macro to level up target units without leaving them with excessive experience or AMLA HP.

Essentially, it says "While there's a level 2 unit here, give it a bit of XP, then repeat until there's no longer a level 2 unit here, then set their XP to 0".

On the downside, the do-while loop promptly causes horrendous lag and crashes the game when it encounters a level 2 unit that cannot advance, such as a Javelineer or Saurian Oracle. I've been trying to include a filter into the loop that excludes units with no advancement options, but nothing has quite worked.

The entire definition is included here for the sake of completeness, but I'm only asking for help with the first [have_unit] filter.
Does anyone know the correct way of filtering out units with no advancements?

Code: Select all

#define PROMOTELEADER TURN X Y

[event]
name=turn {TURN}

[while]

	[have_unit]
	level=2
	x={X}
	y={Y}

		[and]
			[not]
				[filter_wml]
					[advances_to]
					advances_to=null
					[/advances_to]
				[/filter_wml]
			[/not]
		[/and]

	[/have_unit]

[do]

[modify_unit]
	[filter]
	x={X}
	y={Y}
	[/filter]

	[object]
	name=levelup1
	silent=yes
		[effect]
		apply_to=experience
		increase=10
		[/effect]
	[/object]

[/modify_unit]

[/do]
[/while]

[modify_unit]
	[filter]
	x={X}
	y={Y}
	[/filter]

	[object]
	name=cleanup
	silent=yes
		[effect]
		apply_to=experience
		set=0
		[/effect]
	[/object]

[/modify_unit]

[/event]

#enddef
User avatar
Pentarctagon
Project Manager
Posts: 5564
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Filtering units with no advancements (while - have_unit)

Post by Pentarctagon »

Wouldn't it be easier to check for when the unit's xp ends up at a lower value after you gave them more xp? Or to give the unit only as much xp as it needs to level up all at once rather than looping?
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Araja
Posts: 718
Joined: May 28th, 2009, 9:56 am
Location: Bath, England

Re: Filtering units with no advancements (while - have_unit)

Post by Araja »

The latter suggestion would be ideal, but I tried that in the past and it seems as though effect WML doesn't accept variables as valid input. Or at the least, it does so in some alien way that's unlike other languages I've dealt with.

I assumed it would be possible to save something in a container, then use a value from that container in place of standard input, something like this;

Code: Select all

[store_unit]
[filter]
x={X}
y={Y}
[/filter]
variable=xpvar
[/store_unit]

[modify_unit]
	[filter]
	x={X}
	y={Y}
	[/filter]

	[object]
	name=levelup
	silent=yes
		[effect]
		apply_to=experience
		increase=xpvar.max_experience
		[/effect]
	[/object]
[/modify_unit]
But, it evidently doesn't work out that way. I must be missing some key aspect, but to my great frustration I never found out what that aspect is. The do-while loop was a Plan B, intended to be a no-variables solution to the problem.
User avatar
Pentarctagon
Project Manager
Posts: 5564
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Filtering units with no advancements (while - have_unit)

Post by Pentarctagon »

Does increase=$xpvar.max_experience work?
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Araja
Posts: 718
Joined: May 28th, 2009, 9:56 am
Location: Bath, England

Re: Filtering units with no advancements (while - have_unit)

Post by Araja »

Oh my god, that does actually work perfectly. That was all I was missing all these times I've tried? I just needed to add a $ sign?... :doh:

Despite my frequent visits to the WML reference pages, I'd never picked up on that critical piece of information.

Well, thank you for that, and for not just laughing at me :p
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Filtering units with no advancements (while - have_unit)

Post by gfgtdf »

Pentarctagon wrote:Does increase=$xpvar.max_experience work?
Note that this code might advance a unit 2 times in case that the xp requirement for the level 3 advancemnt is less than the one for the level 2 advancement, so you need max_exp - current_exp.

Also it should be noted out that [object] is not the best way to apply these temporary changes (current experience) since it will put the useless [object] in the units wml.
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
The_Gnat
Posts: 2217
Joined: October 10th, 2016, 3:06 am
Contact:

Re: Filtering units with no advancements (while - have_unit)

Post by The_Gnat »

Hello, i might just be talking to soon, but perhaps wouldn't it be easier to just modify units like this:

Code: Select all

      [modify_unit]
          [filter]
	      id=$unit.id
          [/filter]
	    advances_to=""
      [/modify_unit]
I remember reading somewhere that this advances the unit. This doesn't help with your level 2 filter problem, but once you have filtered them out it seems like it would be easier than continually adding xp.

@devs - please correct me if i am mistaken in this idea :)
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Filtering units with no advancements (while - have_unit)

Post by Sapient »

Here is some sample code which shows how you can filter for units that have an advancement, then advance them until they no longer have an advancement:

Code: Select all

        [store_unit]
            [filter]
                [not]
                    [filter_wml]
                        advances_to="$empty"
                    [/filter_wml]
                    [or]
                        [filter_wml]
                            advances_to="$this_unit.type"
                        [/filter_wml]
                    [/or]
                    [or]
                        [filter_wml]
                            advances_to="null"
                        [/filter_wml]
                    [/or]
                [/not]
            [/filter]
            variable=tolevel
        [/store_unit]
        {FOREACH tolevel i}
        [while]
            [have_unit]
                id=$tolevel[$i].id
                [not]
                    [filter_wml]
                        advances_to="$empty"
                    [/filter_wml]
                    [or]
                        [filter_wml]
                            advances_to="$this_unit.type"
                        [/filter_wml]
                    [/or]
                    [or]
                        [filter_wml]
                            advances_to="null"
                        [/filter_wml]
                    [/or]
                [/not]
            [/have_unit]
            [do]
                {ADVANCE_UNIT id=$tolevel[$i].id ""}
            [/do]
        [/while]
        {NEXT i}
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Filtering units with no advancements (while - have_unit)

Post by Sapient »

Update: I have added a check for advances_to=null in my code above
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
Post Reply