FloodWML

From The Battle for Wesnoth Wiki

FloodWML works for 1.9 and above

Usage

You can find sources at the bottom of the page. It's too long to include here.

NOTE: these macros are performing lots of complex calculations thus they are quite slow. They can make your scenario lag and thus create an unwelcome user experience, especially on older machines. To make sure you've minimized the negative effects, please read the Optimization section below.

Create flood

{CREATE_FLOOD "1,1;2,3" "ne,n" "Gg>Ww;Gs>Wwt" awesome_flood}

Where "1,1;2,3" is a semicolon separated list of coordinate pairs from which the flood starts, "ne,n" is a comma separated list of directions in which the flood will spread, "Gg>Ww;Gs>Wwt" is a semicolon separated list of transformations where a>b means that the hexes with the terrain code a will be converted to b, and finally awesome_flood is the variable that stores the current state of the flood (you might better think about it as the flood's id).

Spread flood

{SPREAD_FLOOD awesome_flood}

Where awesome_flood is the flood's id :). This will spread the flood by one hex. If a hex with a unit is flooded, an event named 'submerged' will be fired. Example

#kill all sunken spearmen
[event]
  name=submerged
  [filter]
    type=Spearman
  [/filter]

  [kill]
    id=$unit.id
    animate=yes
  [/kill]
[/event]

Modify flood data

{SET_FLOOD_DIRECTIONS "se,s" awesome_flood}
{SET_FLOOD_TERRAIN_TABLE "Gg>Wwg;Gs>Ww" awesome_flood}
{SET_FLOOD_SOURCE "3,3;5,4;6,6" awesome_flood}

Note: all of these macros replace current data in awesome_flood, so old information is lost. This is especially important for SET_FLOOD_SOURCE because it alters the list of current hexes from the first wave of the flood. When you call it, the original flood will stop and a new one will start from "3,3;5,4;6,6" or whatever you specify.

Optimization

When to do it?

The part consuming most of the macro's run time is choosing the appropriate terrain type to convert. If you have a wide-spreading flood, this will have to be done for many hexes. Consequently, if you have a big flood with a big terrain table, it's likely that you'll have to optimize it.

Ranking terrain codes

When searching for the matching terrain code for a specific hex, the algorithm will iterate through the terrain table's elements in the order you give them, and quit the iteration when the appropriate string is found. If you put the more abundant terrains to the front, you can achieve radical performance improvements.

Switching terrain table

Sometimes it's hard to decide which code to put first because different ones are common in different stages of the flood. For example, an avalanche starts at a mountain, where there are mostly hills, but it reaches the plains where it finds more flat terrain. In this case, it's best to completely replace the terrain table with the SET_FLOOD_TERRAIN_TABLE macro, so you can give higher rankings to the new common terrains.

Tips & Tricks

Two-wave flood

It's not unimaginable that you want to make your flood heterogeneous - for example, shallow water on the edges and deep water in the middle. You can't do this with one flood - but you can with two! You set up the first one just as normal, and the second one in a way that it converts shallow water to deep (in this example). You start to spread the second with a delay so the first wave will remain ahead.

Avoiding obstacles

Imagine you have a little stream which spreads straight south and can transform green grass to shallow water. So what happens if it accidentally meets a mountain? It stops. Well, we don't want that - but neither do we want to convert the mountain to anything else. So we can add to the terrain table:

Mm>Mm

This won't change the mountain's type (more precisely, it will be changed from Mm to Mm), and the flood won't regard it as an obstacle anymore. Our stream will continue flowing on the other side of the mountain hex.

Source Code

#define INVERT_DIRECTIONS DIRECTIONS OUT_VAR
   #a macro to change directions to their opposites (n->s, sw->ne, etc)
   #DIRECTIONS is a comma-separated list of directions
   #OUT_VAR is the variable to write the output in.

[set_variables]
   name=TMP_inverted_directions
   [split]
      list={DIRECTIONS}
      separator=","
      key=direction
      remove_empty=yes
   [/split]
[/set_variables]

{FOREACH TMP_inverted_directions TMP_ID_i}
        [set_variable]
                name=TMP_inverted_directions[$TMP_ID_i].direction
                value="-" + $TMP_inverted_directions[$TMP_ID_i].direction
        [/set_variable]
{NEXT TMP_ID_i}

{CLEAR_VARIABLE {OUT_VAR}}

[set_variable]
	name={OUT_VAR}
	[join]
		variable=TMP_inverted_directions
		separator=","
		key=direction
	[/join]
[/set_variable]

{CLEAR_VARIABLE TMP_inverted_directions}
#enddef

#define STORE_FLOODABLE_LOCATIONS SOURCE DIRECTIONS TERRAINS OUT_VAR
   #stores hexes of type TERRAINS whose neighbour in one of DIRECTIONS directions can be found in SOURCE
   #SOURCE an array of locations
   #DIRECTIONS is a list of directions
   #TERRAINS comma-separated list of terrain codes
   #OUT_VAR the variable to write the output in
   #
   #NOTE: this macro is in fact used to store the hexes of type TERRAINS which are neighbours of SOURCE in
   #certain directions, but we achieve that by inverting those directions outside of this macro. See INVERT_DIRECTIONS.

[store_locations]
   variable={OUT_VAR}

   terrain={TERRAINS}

   [filter_adjacent_location]
      adjacent={DIRECTIONS}
	  find_in={SOURCE}
   [/filter_adjacent_location]
[/store_locations]
#enddef


#define SPLIT_PAIRS PAIRS SEP1 SEP2 OUT_VAR
[set_variables]
   name=TMP_pairs
   [split]
      list={PAIRS}
      key=pair
      separator={SEP1}
      remove_empty=true
   [/split]
[/set_variables]

{FOREACH TMP_pairs TMP_SP_i}
   [set_variables]
      name=TMP_splitted
      [split]
         list=$TMP_pairs[$TMP_SP_i].pair
         separator={SEP2}
         key=value
      [/split]
   [/set_variables]

   [set_variable]
      name={OUT_VAR}[$TMP_SP_i].value1
      value=$TMP_splitted[0].value
   [/set_variable]
   [set_variable]
      name={OUT_VAR}[$TMP_SP_i].value2
      value=$TMP_splitted[1].value
   [/set_variable]
{NEXT TMP_SP_i}

{CLEAR_VARIABLE TMP_pairs}
{CLEAR_VARIABLE TMP_splitted}
#enddef

#define FLOOD_SINGLE_HEX X Y TERRAIN_TABLE
[store_locations]
   variable=TMP_current_hex
   x={X}
   y={Y}
[/store_locations]

{FOREACH {TERRAIN_TABLE} FSH_i}
   [if]
      [variable]
         name=TMP_current_hex.terrain
         equals=${TERRAIN_TABLE}[$FSH_i].value1
      [/variable]
      [then]
         {MODIFY_TERRAIN ${TERRAIN_TABLE}[$FSH_i].value2 {X} {Y}}
         [set_variable]
            name=FSH_i
            value=${TERRAIN_TABLE}.length
         [/set_variable]
      [/then]
   [/if]
{NEXT FSH_i}


{CLEAR_VARIABLE TMP_current_hex}
#enddef

#define SET_FLOOD_TERRAIN_TABLE TERRAIN_TABLE DATA_VAR
{SPLIT_PAIRS {TERRAIN_TABLE} ";" ">" {DATA_VAR}.terrain_table}
[set_variable]
   name={DATA_VAR}.floodable_terrains
   [join]
      variable={DATA_VAR}.terrain_table
      separator=","
      key=value1
   [/join]
[/set_variable]
#enddef

#define SET_FLOOD_DIRECTIONS DIRECTIONS DATA_VAR
{INVERT_DIRECTIONS {DIRECTIONS} {DATA_VAR}.directions}
#enddef

#define SET_FLOOD_SOURCE SOURCE DATA_VAR
{SPLIT_PAIRS {SOURCE} ";" "," TMP_source_coordinates}
[set_variable]
   name=TMP_fields_x
   [join]
      variable=TMP_source_coordinates
      key=value1
      separator=","
   [/join]
[/set_variable]
[set_variable]
   name=TMP_fields_y
   [join]
      variable=TMP_source_coordinates
      key=value2
      separator=","
   [/join]
[/set_variable]

[store_locations]
	variable={DATA_VAR}.fields
	x=$TMP_fields_x
	y=$TMP_fields_y
[/store_locations]
#enddef

#define CREATE_FLOOD SOURCE DIRECTIONS TERRAIN_TABLE DATA_VAR
[clear_variable]
   name={DATA_VAR}
[/clear_variable]

{SET_FLOOD_SOURCE {SOURCE} {DATA_VAR}}
{SET_FLOOD_TERRAIN_TABLE {TERRAIN_TABLE} {DATA_VAR}}
{SET_FLOOD_DIRECTIONS {DIRECTIONS} {DATA_VAR}}
#enddef

#define SPREAD_FLOOD DATA_VAR
{STORE_FLOODABLE_LOCATIONS {DATA_VAR}.fields ${DATA_VAR}.directions ${DATA_VAR}.floodable_terrains {DATA_VAR}.fields}

{FOREACH {DATA_VAR}.fields TMP_SF_i}
   {FLOOD_SINGLE_HEX ${DATA_VAR}.fields[$TMP_SF_i].x ${DATA_VAR}.fields[$TMP_SF_i].y {DATA_VAR}.terrain_table}
{NEXT TMP_SF_i}
[redraw][/redraw]

[store_unit]
	variable=TMP_submerged_units
	[filter]
		[filter_location]
			find_in={DATA_VAR}.fields
		[/filter_location]
	[/filter]
[/store_unit]

{FOREACH TMP_submerged_units TMP_SF_i}
	[fire_event]
		[primary_unit]
			id=$TMP_submerged_units[$TMP_SF_i].id
		[/primary_unit]
		name=submerged
	[/fire_event]
{NEXT TMP_SF_i}

{CLEAR_VARIABLE TMP_submerged_units}

#enddef

See Also

This page was last edited on 11 April 2013, at 03:15.