#textdomain wesnoth
# Utility macros for manipulating map terrain and overlays.

# These don't depend on any other macros.  Please don't change this.
# ! in comments is used in generating HTML documentation, ignore it otherwise.

#define MODIFY_TERRAIN TERRAIN X_SPAN Y_SPAN
    # Changes the terrain at a given list of coordinates
    #
    # For example, we could make 14,15 and 14,16 grassland:
    #! {MODIFY_TERRAIN Gg (14,14) (15,16)}
    [terrain]
        terrain={TERRAIN}
        x={X_SPAN}
        y={Y_SPAN}
    [/terrain]
#enddef

#define MODIFY_TERRAIN_MASK X Y MASK_VALUE RULES_WML
    # Changes the terrain for a given area
    [terrain_mask]
        x={X}
        y={Y}
        mask={MASK_VALUE}
        {RULES_WML}
    [/terrain_mask]
#enddef

#define SCATTER_IMAGE FILTER NUMBER IMAGE
    # Place NUMBER copies of the IMAGE on map hexes matching FILTER.
    #
    # This call will scatter 20 copies of a pine-tree graphic over grassland:
    #! {SCATTER_IMAGE (terrain=Gg) 20 scenery/pine1.png}
    [store_locations]
        {FILTER}
        variable=random_placement_locations
    [/store_locations]

    {VARIABLE REPEAT_i 0}

    [while]
        [variable]
            name=REPEAT_i
            less_than={NUMBER}
        [/variable]
        [variable]
            name=random_placement_locations.length
            greater_than=0
        [/variable]

        [do]
            [set_variable]
                name=random_subscript
                rand="0..$($random_placement_locations.length - 1)"
            [/set_variable]

            [item]
                image={IMAGE}
                x,y=$random_placement_locations[$random_subscript].x,$random_placement_locations[$random_subscript].y
            [/item]

            {CLEAR_VARIABLE random_placement_locations[$random_subscript]}
            {VARIABLE_OP REPEAT_i add 1}
        [/do]
    [/while]

    {CLEAR_VARIABLE REPEAT_i}
    {CLEAR_VARIABLE random_subscript}
    {CLEAR_VARIABLE random_placement_locations}
#enddef

#define SCATTER_EMBELLISHMENTS TERRAINLIST EMBELLISHMENT_NAME PERCENTAGE
    # Adds the given embellishment to the given percentage of the given terrain
    # on the map.
    #
    # For example, this will add flowers to 5% of all grassland:
    #! {SCATTER_EMBELLISHMENTS G* ^Efm 5}
    [store_locations]
        terrain={TERRAINLIST}
        variable=terrain_variation_locations
    [/store_locations]

    {VARIABLE terrain_variations_to_place {PERCENTAGE}}

    # TODO: change back to 100.0 once it no longer causes a crash
    {VARIABLE_OP terrain_variations_to_place divide 100.0}

    {VARIABLE_OP terrain_variations_to_place multiply $terrain_variation_locations.length}
    {VARIABLE_OP terrain_variations_to_place round ceil}

    [while]
        [variable]
            name=terrain_variations_to_place
            greater_than=0
        [/variable]

        [do]
            {VARIABLE_OP terrain_variation_i rand "0..$($terrain_variation_locations.length - 1)"}

            [terrain]
                x,y=$terrain_variation_locations[$terrain_variation_i].x,$terrain_variation_locations[$terrain_variation_i].y
                terrain={EMBELLISHMENT_NAME}
                layer=overlay
            [/terrain]

            {VARIABLE_OP terrain_variations_to_place sub 1}

            {CLEAR_VARIABLE terrain_variation_locations[$terrain_variation_i]}
        [/do]
    [/while]

    {CLEAR_VARIABLE terrain_variation_locations,terrain_variations_to_place,terrain_variation_i}
#enddef
