Filtering units with no advancements (while - have_unit)

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

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.

Filtering units with no advancements (while - have_unit)

Postby Araja » October 10th, 2017, 12:03 pm

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

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

Postby Pentarctagon » October 10th, 2017, 12:38 pm

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
Pentarctagon
Forum Administrator
 
Posts: 2921
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

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

Postby Araja » October 10th, 2017, 7:49 pm

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

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

Postby Pentarctagon » October 10th, 2017, 11:21 pm

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
Pentarctagon
Forum Administrator
 
Posts: 2921
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)


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

Postby Araja » October 11th, 2017, 9:26 am

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

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

Postby gfgtdf » October 14th, 2017, 12:20 am

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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

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

Postby The_Gnat » October 14th, 2017, 3:53 am

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
The_Gnat
 
Posts: 1257
Joined: October 10th, 2016, 3:06 am

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

Postby Sapient » October 14th, 2017, 9:53 am

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]
                [/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]
                [/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
Developer
 
Posts: 4359
Joined: November 26th, 2005, 7:41 am


Return to WML Workshop

Who is online

Users browsing this forum: No registered users and 2 guests