Bug with CLEAR_FOG and UNCLEAR_FOG macros
Moderator: Forum Moderators
Forum rules
Before reporting issues in this section, you must read the following topic:
Before reporting issues in this section, you must read the following topic:
Bug with CLEAR_FOG and UNCLEAR_FOG macros
I recently played with the CLEAR_FOG macro as defined in interface-utils.cfg, and found that using it multiple times in a row does not work the way one would expect.
The first CLEAR_FOG always works fine. It creates an invisible Fog Clearer unit with no interface icons on the map.
Each additional CLEAR_FOG, however, while it still creates an invisible unit, creates some interface glutter (see screenshot).
To find out why, I inspected the macros and found that the CLEAR_FOG macro gives a unique ID to each Fog Clearer. The reason is that the Fog Clearer, while invisible on its own, creates the aforementioned interface glutter, so it is hidden via a [hide_unit] tag. Given that the [hide_unit] tag only takes x- and y-coordinates, and given that there is no guarantee that the Fog Clearer (being a reguler unit itself) is actually placed on the user-specified coordinates, working with unique IDs seems about the right way to solve this issue.
Either way, the ID (a variable called fog_clearer_i) starts with 1 if no Fog Clearer yet exists, but is supposed to be incremented by 1 if already defined. This, however, cannot work as the ID is cleared again when the macro finishes. Here the relevant code snippet:
The macro would work the expected way if that last piece of code that clears the ID was removed. The reason is that [hide_unit] will always hide the first Fog Clearer because the units are given identical IDs.
Incidentally, the code for UNCLEAR_FOG does not clear the ID, even though it really should.
To sum up, the correct code for CLEAR_FOG and UNCLEAR_FOG should be:
Operating System: Windows XP
Version of Wesnoth: 1.8.3 (English)
The first CLEAR_FOG always works fine. It creates an invisible Fog Clearer unit with no interface icons on the map.
Each additional CLEAR_FOG, however, while it still creates an invisible unit, creates some interface glutter (see screenshot).
To find out why, I inspected the macros and found that the CLEAR_FOG macro gives a unique ID to each Fog Clearer. The reason is that the Fog Clearer, while invisible on its own, creates the aforementioned interface glutter, so it is hidden via a [hide_unit] tag. Given that the [hide_unit] tag only takes x- and y-coordinates, and given that there is no guarantee that the Fog Clearer (being a reguler unit itself) is actually placed on the user-specified coordinates, working with unique IDs seems about the right way to solve this issue.
Either way, the ID (a variable called fog_clearer_i) starts with 1 if no Fog Clearer yet exists, but is supposed to be incremented by 1 if already defined. This, however, cannot work as the ID is cleared again when the macro finishes. Here the relevant code snippet:
Code: Select all
# Here the ID is either created or incremented:
[if]
[have_unit]
type=Fog Clearer
[/have_unit]
[then]
[set_variable]
name=fog_clearer_i
add=1
[/set_variable]
[/then]
[else]
[set_variable]
name=fog_clearer_i
value=1
[/set_variable]
[/else]
[/if]
# Here the ID is cleared again - makes no sense:
[clear_variable]
name=fog_clearer_i
[/clear_variable]
Incidentally, the code for UNCLEAR_FOG does not clear the ID, even though it really should.
To sum up, the correct code for CLEAR_FOG and UNCLEAR_FOG should be:
Code: Select all
#define CLEAR_FOG SIDE X Y RADIUS
# Clears fog in a specific area for the given side. This is to be used only
# for the duration of an event: always use {UNCLEAR_FOG} (below) before the
# event exits, or otherwise you'll be left with fake fog clearer units on
# the map.
[if]
[have_unit]
type=Fog Clearer
[/have_unit]
[then]
[set_variable]
name=fog_clearer_i
add=1
[/set_variable]
[/then]
[else]
[set_variable]
name=fog_clearer_i
value=1
[/set_variable]
[/else]
[/if]
[unit]
side={SIDE}
type=Fog Clearer
id=fog_clearer_$fog_clearer_i
x,y={X},{Y}
max_moves={RADIUS}
[/unit]
[store_unit]
[filter]
id=fog_clearer_$fog_clearer_i
[/filter]
kill=no
variable=new_fog_clearer
[/store_unit]
[hide_unit]
x,y=$new_fog_clearer.x,$new_fog_clearer.y
[/hide_unit]
[redraw]
side={SIDE}
[/redraw]
[clear_variable]
name=new_fog_clearer
[/clear_variable]
#enddef
#define UNCLEAR_FOG
# Restores fog that was temporarily cleared (check CLEAR_FOG above). Only
# one UNCLEAR_FOG is required no matter how many times CLEAR_FOG was used.
[kill]
type=Fog Clearer
animate=no
fire_event=no
[/kill]
[clear_variable]
name=fog_clearer_i
[/clear_variable]
#enddef
Version of Wesnoth: 1.8.3 (English)
- Attachments
-
- Three Fog Clearers. The white squares were added to show their locations. The first Fog Clearer is entirely invisible, the second and third though were not hidden from the interface due to the buggy macro.
- FogClearer.png (233.52 KiB) Viewed 1387 times
The Cost Of Living (1.8): main thread, feedback thread
Re: Bug with CLEAR_FOG and UNCLEAR_FOG macros
Yeah, I was grappling with this on Sunday. Documented here: http://www.wesnoth.org/forum/viewtopic.php?f=21&t=31004
“It is written in my life-blood, such as that is, thick or thin; and I can no other.” - J.R.R. Tolkien
My campaign: Swamplings - Four centuries before the founding of Wesnoth, the first wolf rider emerges from a tribe of lowly swamp goblins.
My campaign: Swamplings - Four centuries before the founding of Wesnoth, the first wolf rider emerges from a tribe of lowly swamp goblins.
Re: Bug with CLEAR_FOG and UNCLEAR_FOG macros

I've always quite hated how hacky that macro is, but unfortunately we still don't have a better way of clearing fog...I'll fix the bug and try to clean it up a bit more.
Re: Bug with CLEAR_FOG and UNCLEAR_FOG macros
Unfortunately, [hide_unit] doesn't support more than one target, selected by a (x, y) specification (not even a complete StandardUnitFilter). Hence you get visible fog clearers if you have a hidden unit already, which is the case when the first fog clearer is created.
Patches welcome!
Patches welcome!

Author of the unofficial UtBS sequels Invasion from the Unknown and After the Storm.
Re: Bug with CLEAR_FOG and UNCLEAR_FOG macros
silene fixed [hide_unit] and [unhide_unit] to accept a SUF in trunk already, and the macro has been accordingly simplified. The fix suggested here by blob and in another thread has been applied to 1.8.