switch not working

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
matsjoyce
Posts: 233
Joined: May 8th, 2011, 2:10 pm
Location: UK

switch not working

Post by matsjoyce »

In updating a ai force move macro I came upon this problem.

What the macro is supposed to do is move a unit to a location by moving it by the amount that it usually moves by depending where the unit is.

The macro looks like this:

Code: Select all

#define J_MOVE_AI ID
    [store_unit]
        [filter]
            id={ID}
        [/filter]
        variable=stored_ai
        kill=no
    [/store_unit]
    [switch]
        variable=stored_ai.y
        [case]
            value="8"
            [move_unit]
                id={ID}
                to_x=2
                to_y=16
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
        [/case]
        [case]
            value="7"
            [move_unit]
                id={ID}
                to_x=2
                to_y=15
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
        [/case]
        [case]
            value="6"
            [move_unit]
                id={ID}
                to_x=2
                to_y=14
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
        [/case]
        [case]
            value="5"
            [move_unit]
                id={ID}
                to_x=2
                to_y=13
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
        [/case]
        [case]
            value="4"
            [move_unit]
                id={ID}
                to_x=2
                to_y=12
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
        [/case]
        [case]
            value="3"
            [move_unit]
                id={ID}
                to_x=2
                to_y=16
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
        [/case]
        [case]
            value="2"
            [move_unit]
                id={ID}
                to_x=2
                to_y=16
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
        [/case]
        [else]
            [move_unit]
                id={ID}
                to_x=2
                to_y=17
                fire_event=yes
            [/move_unit]
            [modify_unit]
                [filter]
                    id={ID}
                [/filter]
                moves=0
            [/modify_unit]
                [set_variable]
                    name=snapped
                    value=no
                [/set_variable]
        [/else]
[/switch]
        [clear_variable]
            name=stored_ai
        [/clear_variable]
#enddef
The macro that uses it looks like this (the important bit is right at the bottom):

Code: Select all

#define J_SNAPPING_EVENTS FIRST_ID SECOND_ID AI_TENT_X AI_TENT_Y
    [event]
        name=attacker hits
        first_time_only=no
        [filter]
            id={FIRST_ID}
        [/filter]
        [filter_attack]
            name=lance
        [/filter_attack]
        [set_variable]
            name=lancea
            rand="0..2"
        [/set_variable]
        [if]
            [variable]
                name=lancea
                greater_than=0
            [/variable]
            [then]
                [object]
                    silent=yes
                    [effect]
                        apply_to=remove_attacks
                        type=pierce
                    [/effect]
                [/object]
                [message]
                    id={FIRST_ID}
                    message=_"Aw, my lance has snapped."
                [/message]
                [store_unit] #to stop the unit attack
                    [filter]
                        id={FIRST_ID}
                    [/filter]
                    variable=stored_sir_a
                    kill=yes
                [/store_unit]
                [unstore_unit]
                    variable=stored_sir_a
                [/unstore_unit]
                [clear_variable]
                    name=stored_sir_a
                [/clear_variable]
                [fire_event]
                    name=lance_snapped
                [/fire_event]
            [/then]
            [else]
            [/else]
        [/if]
    [/event]
    [event]
        name=lance_snapped # a separate event is needed so that you only get the below
        # message the first time so it doesn't get on your nerves
        [message]
            speaker=narrator
            image=wesnoth-icon.png
            message=_"To get another lance, you will have to go to your starting position"
        [/message]
    [/event]
    [event]
        name=attacker hits
        first_time_only=no
        [filter]
            id={SECOND_ID}
        [/filter]
        [filter_attack]
            name=lance
        [/filter_attack]
        [set_variable]
            name=lanceb
            rand="0..2"
        [/set_variable]
        [if]
            [variable]
                name=lanceb
                greater_than=0
            [/variable]
            [then]
                [object]
                    silent=yes
                    [effect]
                        apply_to=remove_attacks
                        type=pierce
                    [/effect]
                [/object]
                [message]
                    id={SECOND_ID}
                    message=_"Aw, my lance has snapped."
                [/message]
                [store_unit] #to stop the unit attack
                    [filter]
                        id={SECOND_ID}
                    [/filter]
                    variable=stored_sir_b
                    kill=yes
                [/store_unit]
                [unstore_unit]
                    variable=stored_sir_b
                [/unstore_unit]
                [clear_variable]
                    name=stored_sir_b
                [/clear_variable]

            [/then]
        [/if]
    [/event]
    [event]
        name=side 2 turn refresh
        first_time_only=no
        [filter_condition]
            [variable]
                name=snapped
                boolean_equals=yes
            [/variable]
        [/filter_condition]
        {J_MOVE_AI {SECOND_ID}}
    [/event]
#enddef
It's put in to the scenario like this:

Code: Select all

    {J_SNAPPING_EVENTS Sir_Miles Sir_Hector 2 17}
It use is explaned here.
Last edited by matsjoyce on September 16th, 2011, 3:26 pm, edited 1 time in total.
User avatar
beetlenaut
Developer
Posts: 2867
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: switch not working

Post by beetlenaut »

You never explained your problem. "Doesn't work" is not enough. Describe what it's supposed to do, and what it actually does. However, the switch and cases are unnecessary, so you can do away with them, and maybe that will fix what's broken. You can do:

Code: Select all

to_y="$($stored_ai.y+8)"
...and just write the repeated section once, because that should work for all the cases (though I didn't test it to make sure).
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
matsjoyce
Posts: 233
Joined: May 8th, 2011, 2:10 pm
Location: UK

Re: switch not working

Post by matsjoyce »

What it's supposed to do is make an ai unit move normally to a destination it would not normally move to. If you want to see the map it's the jousting map in my add-on Jousting.
User avatar
beetlenaut
Developer
Posts: 2867
Joined: December 8th, 2007, 3:21 am
Location: Washington State
Contact:

Re: switch not working

Post by beetlenaut »

...and I suppose we are to assume that it doesn't do anything at all?

I looked through it more carefully, and see a potential problem. The macro is called if the variable "snapped" is set to "yes", but the only place I can see where that can happen is in the macro, and only in the [else] block too. Either you didn't post all the code here, or the macro will never be called.
Campaigns: Dead Water,
The Founding of Borstep,
Secrets of the Ancients,
and WML Guide
User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: switch not working

Post by battlestar »

If you're looking to assign units a destination and have them move naturally towards it, I had the similar problem and:
Bob_The_Mighty wrote:battlestar, you can assign unit.goto_x and unit.goto_y to each unit that is travelling.
And that made it work.
LUA: Llama Under Apprenticeship
Hell faction: completed
User avatar
matsjoyce
Posts: 233
Joined: May 8th, 2011, 2:10 pm
Location: UK

Re: switch not working

Post by matsjoyce »

Thanks battlestar, I've got it working now.

Another question: I'm trying to make a terrain that heals like a village but gives defence like a normal flat tile.
Here's the code:

Code: Select all

#textdomain wesnoth-j

[terrain_type]
    symbol_image=grass/green
    id=healing_grass
    name= _ "Healing grass"
    string=^Vmz
    aliasof=Gg
    heals=8
    editor_group=J
[/terrain_type]
[terrain_graphics]
    map="
, *
* , *
, 1
* , *
, *"
    [tile]
        pos=1
        type=*^Vmz
        set_flag="base"
    [/tile]
    [image]
        name=grass/green1
        base=90,146
    [/image]
[/terrain_graphics]
#{OVERLAY    *^Vmz           grass/green1} this doesn't seem to make any difference
And the _main.cfg section:

Code: Select all

#ifdef EDITOR
[binary_path]
    path="data/add-ons/Jousting"
[/binary_path]

[editor_group]
    id=J
    name= _ "Jousting"
    icon="group_custom"
[/editor_group]
{~add-ons/Jousting/utils/terrain/terrain.cfg}
#endif
And it dosen't seem to work. It appears in the editor but if I try to load the map this happens:
Screenshot132.png
Any ideas?
blueknight1758
Posts: 252
Joined: March 30th, 2011, 8:38 pm
Location: The UK

Re: switch not working

Post by blueknight1758 »

I think (this is just a think) that If you put alias of (whatever the string of a flat is) and put the heals=yes tag in it would work.
(Please note I'm not great at this and the 'heals' tag might be something else look in the terrain definitions under villages you should be able to see.)
User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: switch not working

Post by battlestar »

With WML you can try [heal_unit] with [filter_location]'s terrain=
LUA: Llama Under Apprenticeship
Hell faction: completed
User avatar
Alarantalara
Art Contributor
Posts: 789
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: switch not working

Post by Alarantalara »

You need to include the terrain in the campaign as well. So copy the binary path (if not already present) and {~add-ons/Jousting/utils/terrain/terrain.cfg} into the part of your _main.cfg that lies within the #ifdef <CAMPAIGN_NAME>.

Edit: since I guess you may have based your entry off of my tutorial on such subjects, I've also edited in an additional note to make it clear that the terrain files must also be included like any other WML.
User avatar
matsjoyce
Posts: 233
Joined: May 8th, 2011, 2:10 pm
Location: UK

Re: switch not working

Post by matsjoyce »

Thanks got it working :lol2: . Two other questions:
  1. When the ai moves of it, it heals 10 not 8.
  2. How could you stop the ai staying on it until it has full health?
User avatar
Alarantalara
Art Contributor
Posts: 789
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: switch not working

Post by Alarantalara »

Assuming you're still using movement code much like the earlier code you have in this thread, it's because the AI didn't actually move there so it counts as resting.
You may be able to just set "resting" to no in the same way you set moves to 0 to solve the problem.

There are lots of ways to get the AI to move off of the healing area.
For instance:
Change the terrain so it's gone, removing the incentive to stay after enough healing. Replace it whenever your existing move to heal area code runs.
Write something using FormulaAI/LuaAI that completely controls the AI's actions (basically write rules of when to heal and when to attack - they could also replace your WML movement back fixing the first problem at the same time since the AI would be moving the unit)
Write more WML to move it off. (The AI should attack if its adjacent to the enemy even if it can't move)

Of these, the second offers the most power/control. You may find the other two easier to write, though.
Post Reply