fire event in teleport

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
hermestrismi
Posts: 632
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

fire event in teleport

Post by hermestrismi »

Hi,
Is there a way to fire an event when a unit teleport?
something like:

Code: Select all

[event]
name=teleport 
...
[/event]
User avatar
Ravana
Forum Moderator
Posts: 3064
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: fire event in teleport

Post by Ravana »

Add [fire_event]name=teleport[/fire_event] after you teleport units. If you mean ability then enter hex should work.
User avatar
hermestrismi
Posts: 632
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: fire event in teleport

Post by hermestrismi »

I mean fire event AFTER the specified unit teleport (precisely harm a specific unit whenever it use teleportation)
white_haired_uncle
Posts: 1282
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: fire event in teleport

Post by white_haired_uncle »

Have you looked at unit_placed? It seems to be a bit of a last restort, but you should be able to make it work.

[I assume you mean fire event after unit uses the teleport ability, not [teleport] ]
Speak softly, and carry Doombringer.
User avatar
hermestrismi
Posts: 632
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: fire event in teleport

Post by hermestrismi »

white_haired_uncle wrote: May 7th, 2024, 3:17 pm Have you looked at unit_placed? It seems to be a bit of a last restort, but you should be able to make it work.

[I assume you mean fire event after unit uses the teleport ability, not [teleport] ]
yes. as I mentioned before, I am trying to make an event that fire after a unit uses the teleport ability.
a workaround came to mind is to store both the exit hex and the entered hex then comparing the distance (+- the unit movement points)...
but:
1. I don't know if this will work flawlessly,
2. I need a way to calculate distance instead of using x and y alone ,
3. it is not a beautiful way anyway.
I locked also to the teleport related files in wml and lua but no idea came to mind...
User avatar
Spannerbag
Posts: 553
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: fire event in teleport

Post by Spannerbag »

Hi,
Your post intrigued me so I had a bit of a play, building on the suggestion by Ravana.

This code isn't perfect, it might need [allow_undo] or maybe [cancel_action] but from my limited testing it does seem to trap teleports and filter out non-teleport movement.
Anyway, it should at least give you a starting point.

I assumed you'd be using {ABILITY_TELEPORT}.
I also assumed you'd want to avoid lua if possible.

The [filter] for which unit(s) are considered teleport-capable (ability_id_active=teleport) can be changed (e.g. to type=Silver Mage) depending on circumstance.

Here's my 'orrible hack code:

Code: Select all

# --- DEBUG: SETUP TEST CONDITIONS ---
  [event]
    name=side 1 turn 1
    [terrain]
      x=8,8,10,11,12
      y=8,9, 7,11,11
      terrain=*^Vh
      layer=overlay
    [/terrain]
    [capture_village]
      side=1
      x=8,8,10,11
      y=8,9, 7,11
    [/capture_village]
{GENERIC_UNIT 1 (Silver Mage) 7 7}
{DEBUG_MSG (_"Done")}
  [/event]


# --- TELEPORT DETECT EVENTS (DEBUG VERSIONS) ---
# Teleport setup
# Setup when teleport capable unit enters village
# (i.e. do not yet know if unit will actually teleport or not)
  [event]
    name=enter_hex
    first_time_only=no
    [filter]
      x,y=$x1,$y1
      ability_id_active=teleport		# Always active; excludes units without {ABILITY_TELEPORT}
      [filter_location]
        terrain=*^V*				# Unit entered a village
      [/filter_location]
    [/filter]
    [filter_condition]
      {VARIABLE_CONDITIONAL unit.variables.tpt_x numerical_equals 0}
    [/filter_condition]
    [modify_unit]
      [filter]
        id=$unit.id
      [/filter]
      {VARIABLE tpt_x $x1}
      {VARIABLE tpt_y $y1}
    [/modify_unit]
    {DEBUG_MSG (_"Enter hex fired, unit stored x,y")}
  [/event]
#
# Teleport teardown (unit moved to adjacent hex)
# Unit did not teleport
  [event]
    name=exit_hex
    first_time_only=no
    [filter]
      x,y=$x1,$y1
      ability_id_active=teleport		# Always active; excludes units without {ABILITY_TELEPORT}
    [/filter]
    [filter_condition]
      {VARIABLE_CONDITIONAL unit.variables.tpt_x greater_than 0}
      [have_location]
        x,y=$x2,$y2
        [filter_adjacent_location]		# Unit moved 1 hex (i.e. did not teleport, includes adjacent village)
          x,y=$x1,$y1
        [/filter_adjacent_location]
      [/have_location]
    [/filter_condition]
    [modify_unit]
      [filter]
        id=$unit.id
      [/filter]
      {CLEAR_VARIABLE tpt_x,tpt_y}
    [/modify_unit]
    {DEBUG_MSG (_"Exit hex no_teleport fired, cleared unit stored x,y")}
  [/event]
#
# Teleport capture (unit moved to non-adjacent hex)
# Unit did teleport
  [event]
    name=enter_hex
    first_time_only=no
    [filter]
      x,y=$x1,$y1
      ability_id_active=teleport		# Always active; excludes units without {ABILITY_TELEPORT}
    [/filter]
    [filter_condition]
      {VARIABLE_CONDITIONAL unit.variables.tpt_x greater_than 0}
      [have_location]
        x,y=$x2,$y2
        [filter_adjacent_location]		# Unit moved >1 hex (i.e. did teleport)
          x,y=$x1,$y1
          count=0
        [/filter_adjacent_location]
      [/have_location]
    [/filter_condition]
#    [modify_unit]
#      [filter]
#        id=$unit.id
#      [/filter]
#      {CLEAR_VARIABLE tpt_x,tpt_y}
#    [/modify_unit]
    {DEBUG_MSG (_"Enter hex ***teleport*** fired")}
  [/event]
As I must repeat I have only minuimally tested this with a Silver Mage and a Spearman, in particular this will also affect/process AI controlled units which may or may not be the desired behaviour.
Anyway, hope it helps, good luck... and if you do get it working I'd appreciate a copy of your polished code, I might also find a use for it. :)

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
hermestrismi
Posts: 632
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: fire event in teleport

Post by hermestrismi »

Spannerbag wrote: May 8th, 2024, 1:47 pm Hi,
Your post intrigued me so I had a bit of a play, building on the suggestion by Ravana.

This code isn't perfect, it might need [allow_undo] or maybe [cancel_action] but from my limited testing it does seem to trap teleports and filter out non-teleport movement.
Anyway, it should at least give you a starting point.

I assumed you'd be using {ABILITY_TELEPORT}.
I also assumed you'd want to avoid lua if possible.

The [filter] for which unit(s) are considered teleport-capable (ability_id_active=teleport) can be changed (e.g. to type=Silver Mage) depending on circumstance.

Here's my 'orrible hack code:

Code: Select all

# --- DEBUG: SETUP TEST CONDITIONS ---
  [event]
    name=side 1 turn 1
    [terrain]
      x=8,8,10,11,12
      y=8,9, 7,11,11
      terrain=*^Vh
      layer=overlay
    [/terrain]
    [capture_village]
      side=1
      x=8,8,10,11
      y=8,9, 7,11
    [/capture_village]
{GENERIC_UNIT 1 (Silver Mage) 7 7}
{DEBUG_MSG (_"Done")}
  [/event]


# --- TELEPORT DETECT EVENTS (DEBUG VERSIONS) ---
# Teleport setup
# Setup when teleport capable unit enters village
# (i.e. do not yet know if unit will actually teleport or not)
  [event]
    name=enter_hex
    first_time_only=no
    [filter]
      x,y=$x1,$y1
      ability_id_active=teleport		# Always active; excludes units without {ABILITY_TELEPORT}
      [filter_location]
        terrain=*^V*				# Unit entered a village
      [/filter_location]
    [/filter]
    [filter_condition]
      {VARIABLE_CONDITIONAL unit.variables.tpt_x numerical_equals 0}
    [/filter_condition]
    [modify_unit]
      [filter]
        id=$unit.id
      [/filter]
      {VARIABLE tpt_x $x1}
      {VARIABLE tpt_y $y1}
    [/modify_unit]
    {DEBUG_MSG (_"Enter hex fired, unit stored x,y")}
  [/event]
#
# Teleport teardown (unit moved to adjacent hex)
# Unit did not teleport
  [event]
    name=exit_hex
    first_time_only=no
    [filter]
      x,y=$x1,$y1
      ability_id_active=teleport		# Always active; excludes units without {ABILITY_TELEPORT}
    [/filter]
    [filter_condition]
      {VARIABLE_CONDITIONAL unit.variables.tpt_x greater_than 0}
      [have_location]
        x,y=$x2,$y2
        [filter_adjacent_location]		# Unit moved 1 hex (i.e. did not teleport, includes adjacent village)
          x,y=$x1,$y1
        [/filter_adjacent_location]
      [/have_location]
    [/filter_condition]
    [modify_unit]
      [filter]
        id=$unit.id
      [/filter]
      {CLEAR_VARIABLE tpt_x,tpt_y}
    [/modify_unit]
    {DEBUG_MSG (_"Exit hex no_teleport fired, cleared unit stored x,y")}
  [/event]
#
# Teleport capture (unit moved to non-adjacent hex)
# Unit did teleport
  [event]
    name=enter_hex
    first_time_only=no
    [filter]
      x,y=$x1,$y1
      ability_id_active=teleport		# Always active; excludes units without {ABILITY_TELEPORT}
    [/filter]
    [filter_condition]
      {VARIABLE_CONDITIONAL unit.variables.tpt_x greater_than 0}
      [have_location]
        x,y=$x2,$y2
        [filter_adjacent_location]		# Unit moved >1 hex (i.e. did teleport)
          x,y=$x1,$y1
          count=0
        [/filter_adjacent_location]
      [/have_location]
    [/filter_condition]
#    [modify_unit]
#      [filter]
#        id=$unit.id
#      [/filter]
#      {CLEAR_VARIABLE tpt_x,tpt_y}
#    [/modify_unit]
    {DEBUG_MSG (_"Enter hex ***teleport*** fired")}
  [/event]
As I must repeat I have only minuimally tested this with a Silver Mage and a Spearman, in particular this will also affect/process AI controlled units which may or may not be the desired behaviour.
Anyway, hope it helps, good luck... and if you do get it working I'd appreciate a copy of your polished code, I might also find a use for it. :)

Cheers!
-- Spannerbag
Thank you very much.
this seems to be more logic and straight than my approach. I will test it as soon as possible and I will give you my result.
User avatar
Spannerbag
Posts: 553
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: fire event in teleport

Post by Spannerbag »

hermestrismi wrote: May 8th, 2024, 3:55 pm Thank you very much.
this seems to be more logic and straight than my approach. I will test it as soon as possible and I will give you my result.
Glad to have been able to help! :D

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2273
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: fire event in teleport

Post by Celtic_Minstrel »

hermestrismi wrote: May 7th, 2024, 4:02 pm
white_haired_uncle wrote: May 7th, 2024, 3:17 pm Have you looked at unit_placed? It seems to be a bit of a last restort, but you should be able to make it work.

[I assume you mean fire event after unit uses the teleport ability, not [teleport] ]
yes. as I mentioned before, I am trying to make an event that fire after a unit uses the teleport ability.
a workaround came to mind is to store both the exit hex and the entered hex then comparing the distance (+- the unit movement points)...
but:
1. I don't know if this will work flawlessly,
2. I need a way to calculate distance instead of using x and y alone ,
3. it is not a beautiful way anyway.
I locked also to the teleport related files in wml and lua but no idea came to mind...
…what? This is not a workaround. This is the most logical way to do it. A teleport ability is just a regular move where the hexes moved between are not adjacent, so enter/exit hex is the obvious way to detect a teleport. And as Spannerbag showed, it certainly works.

My only complaint about Spannerbag's code is that it only catches the default teleport ability and will not detect any custom teleport abilities that you define. A starting point for fixing this would be to use ability_type_active=teleport instead of ability_id_active=teleport. That's not sufficient however, as his code also relies on setting a flag when entering a teleportable terrain. I think there should be some way of doing it without setting a flag, but if you only care about default teleport, then his solution seems quite reasonable.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Spannerbag
Posts: 553
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: fire event in teleport

Post by Spannerbag »

Celtic_Minstrel wrote: May 10th, 2024, 2:00 pm My only complaint about Spannerbag's code is that it only catches the default teleport ability and will not detect any custom teleport abilities that you define. A starting point for fixing this would be to use ability_type_active=teleport instead of ability_id_active=teleport. That's not sufficient however, as his code also relies on setting a flag when entering a teleportable terrain. I think there should be some way of doing it without setting a flag, but if you only care about default teleport, then his solution seems quite reasonable.
Just to be clear it was one of the assumptions in my post:
I assumed you'd be using {ABILITY_TELEPORT}.
But yes, I take your point that generalising it to any teleport would be trivial. :)

I agree with you regarding setting a flag and have thought about it a bit more.
I think this much simplified code might be the basis of a more elegant solution, in my very quick and minimal testing it seemed to work.
But it does need much more robust testing, unfortunately I'm not in a position to do that just now. :(

Code: Select all

# Teleport capture (unit moved to non-adjacent hex)
# Unit did teleport
  [event]
    name=enter_hex
    first_time_only=no
    [filter]
      x,y=$x1,$y1
    [/filter]
    [filter_condition]
      [have_location]
        x,y=$x2,$y2
        [filter_adjacent_location]		# Unit moved >1 hex (i.e. did teleport)
          x,y=$x1,$y1
          count=0
        [/filter_adjacent_location]
      [/have_location]
    [/filter_condition]
    {DEBUG_MSG (_"Enter hex ***teleport*** fired")}
  [/event]
Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Ravana
Forum Moderator
Posts: 3064
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: fire event in teleport

Post by Ravana »

[filter] x,y=$x1,$y1 [/filter]

Is always passing, so you can just remove that.
User avatar
hermestrismi
Posts: 632
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: fire event in teleport

Post by hermestrismi »

you guys are just wizards. unfortunately, I didn't have time to test the suggestion but I think that it is very logical so now we can built an ability like 'weakned teleportation' that give a penalty for using teleportation or to make it like a first AMLA before another AMLA (a regular teleport ability).
I hope I will be able to test it soon.
User avatar
Spannerbag
Posts: 553
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: fire event in teleport

Post by Spannerbag »

Ravana wrote: May 10th, 2024, 10:39 pm [filter] x,y=$x1,$y1 [/filter]

Is always passing, so you can just remove that.
Less is more so even better!
Must remember that, thanks.

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Spannerbag
Posts: 553
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: fire event in teleport

Post by Spannerbag »

hermestrismi wrote: May 10th, 2024, 11:03 pm you guys are just wizards. unfortunately, I didn't have time to test the suggestion...
Just as well, the latest version is about 1/3rd the size of the first attempt! :)

Good luck with your idea!

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply