Campaign/Forgotten Legacy/General Utils

From The Battle for Wesnoth Wiki
# Thank to zookeeper for this file within his RotL campaign

#define PUT_IMG IMG X Y
	[item]
		x={X}
		y={Y}
		image={IMG}
	[/item]
#enddef


# Shortcut for IF statements testing the value of a variable. Need to write
# [then] and [else] tags manually, though.
#
# I usually use it like this:
#
# {IF some_variable equals yes (
#     [then]
#         ...
#     [/then]
# )}

#define IF VAR OP VAL CONTENTS
    [if]
        [variable]
            name={VAR}
            {OP}={VAL}
        [/variable]

        {CONTENTS}
    [/if]
#enddef


# A variation of the above, testing [have_unit] instead of [variable].

#define IF_HAVEUNIT FILTER CONTENTS
    [if]
        [have_unit]
            {FILTER}
        [/have_unit]

        {CONTENTS}
    [/if]
#enddef


# A shortcut for [while] testing the value of a variable. Use just like the IF
# above.

#define WHILE VAR OP VAL CONTENTS
    [while]
        [variable]
            name={VAR}
            {OP}={VAL}
        [/variable]

        [do]
            {CONTENTS}
        [/do]
    [/while]
#enddef


# A variation of the above, testing for [have_unit] instead of [variable].

#define WHILE_HAVEUNIT FILTER CONTENTS
    [while]
        [have_unit]
            {FILTER}
        [/have_unit]

        [do]
            {CONTENTS}
        [/do]
    [/while]
#enddef


# Sets VAR to FROM, and repeats CONTENTS until VAR equals TO, adding 1 to VAR
# every cycle. Quite similar to REPEAT below, this just makes it simpler to
# iterate through a dynamic range. If you just need to repeat the same set of
# commands, use REPEAT below.
#
# Example that spawns a row of four skeletons: ((4,5), (5,5), (6,5), (7,5))
#
# {ITERATE 4 8 i (
#     [unit]
#         type=Skeleton
#         x=$i
#         y=5
#     [/unit]
# )}

#define ITERATE FROM TO VAR CONTENTS
    {VARIABLE {VAR} {FROM}}

    {WHILE {VAR} not_equals {TO} (
        {CONTENTS}

        {VARIABLE_OP {VAR} add 1}
    )}
#enddef


# Used in conjunction with TIMES (below) to repeat something. You cannot nest
# multiple {REPEAT}'s, because every {REPEAT} uses the same variable for
# iterating. I might fix this sometime, though. Use this to repeat the same set
# of commands, and ITERATE above to iterate through a range of values.
#
# Example that recalls 4 spearmen for side 1:
#
# {REPEAT}
#     [recall]
#         type=Spearman
#         side=1
#     [/recall]
# {TIMES 4}

#define REPEAT
    {VARIABLE REPEAT_i 0}
    {VARIABLE REPEAT_repeats 1}

    [while]
        [variable]
            name=REPEAT_i
            less_than=$REPEAT_repeats
        [/variable]

        [do]
#enddef

#define TIMES REPEATS
            {VARIABLE REPEAT_repeats {REPEATS}}
            {VARIABLE_OP REPEAT_i add 1}
        [/do]
    [/while]

    {CLEAR_VARIABLE REPEAT_i}
    {CLEAR_VARIABLE REPEAT_repeats}
#enddef


# Shorter way of writing a [teleport].

#define TELEPORT FILTER X Y
    [teleport]
        [filter]
            {FILTER}
        [/filter]

        x,y={X},{Y}
    [/teleport]
#enddef


# Tests if a given unit stands on a given type of terrain.

#define IF_UNIT_ON_TERRAIN FILTER LETTER
#enddef


# Alters a unit variable (such as unit.x, unit.type, unit.side)
#
# Example that flips all spearmen to side 2:
# {MODIFY_UNIT type=Spearman side 2}

#define MODIFY_UNIT FILTER VAR VALUE
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        variable=MODIFY_UNIT_store
        kill=yes
    [/store_unit]

    {FOREACH MODIFY_UNIT_store MODIFY_UNIT_i}
        [set_variable]
            name=MODIFY_UNIT_store[$MODIFY_UNIT_i].{VAR}
            value={VALUE}
        [/set_variable]

        [unstore_unit]
            variable=MODIFY_UNIT_store[$MODIFY_UNIT_i]
            find_vacant=no
        [/unstore_unit]
    {NEXT MODIFY_UNIT_i}

    {CLEAR_VARIABLE MODIFY_UNIT_store}
#enddef


# Modifies a side. What [modify_side] can do, this can.

#define MODIFY_SIDE SIDE VAR VALUE
    [modify_side]
        side={SIDE}
        {VAR}={VALUE}
    [/modify_side]
#enddef


# Shortcut for [store_unit]. Does not remove the unit from play.

#define SAVE_UNIT FILTER VAR
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable={VAR}
    [/store_unit]
#enddef


# Shortcut for [store_unit]. Removes the unit from play.

#define STORE_UNIT FILTER VAR
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=yes
        variable={VAR}
    [/store_unit]
#enddef


# Shortcut for [unstore_unit].

#define UNSTORE_UNIT VAR
    [unstore_unit]
        variable={VAR}
        find_vacant=yes
    [/unstore_unit]
#enddef

# Stores a variable attribute of a unit to the given variable.
#
# Example that flips all orcs to whatever side James is on:
#
# {STORE_UNIT_VAR description=James side side_of_James}
# {MODIFY_UNIT race=orc side $side_of_James}

#define STORE_UNIT_VAR FILTER VAR TO_VAR
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        kill=no
        variable=STORE_UNIT_VAR_store
    [/store_unit]

    {VARIABLE_OP {TO_VAR} format $STORE_UNIT_VAR_store.{VAR}}

    {CLEAR_VARIABLE STORE_UNIT_VAR_store}
#enddef


#define UNIT_OVERLAY FILTER IMAGE
    {SAVE_UNIT FILTER UNIT_OVERLAY_unit}

    [unit_overlay]
        x=$UNIT_OVERLAY_unit.x
        y=$UNIT_OVERLAY_unit.y
        image={IMAGE}
    [/unit_overlay]

    {CLEAR_VARIABLE UNIT_OVERLAY_unit}
#enddef


#define REMOVE_UNIT_OVERLAY FILTER IMAGE
    {SAVE_UNIT FILTER REMOVE_UNIT_OVERLAY_unit}

    [remove_unit_overlay]
        x=$REMOVE_UNIT_OVERLAY_unit.x
        y=$REMOVE_UNIT_OVERLAY_unit.y
        image={IMAGE}
    [/remove_unit_overlay]

    {CLEAR_VARIABLE REMOVE_UNIT_OVERLAY_unit}
#enddef


#define REDRAW
    [redraw][/redraw]
#enddef


#define DELAY TIME
    [delay]
        time={TIME}
    [/delay]
#enddef


# Shakes the screen a little bit.

#define SHORT_RUMBLE
    [scroll]
        x,y=32,32
    [/scroll]

    [scroll]
        x,y=-48,-48
    [/scroll]

    [scroll]
        x,y=24,24
    [/scroll]

    [scroll]
        x,y=-12,-12
    [/scroll]

    [scroll]
        x,y=6,6
    [/scroll]

    [scroll]
        x,y=-2,-2
    [/scroll]
#enddef


# Shakes the screen a bit more.

#define LONG_RUMBLE
    [scroll]
        x,y=32,32
    [/scroll]

    [scroll]
        x,y=-64,-64
    [/scroll]

    [scroll]
        x,y=48,48
    [/scroll]

    [scroll]
        x,y=-32,-32
    [/scroll]

    [scroll]
        x,y=32,32
    [/scroll]

    [scroll]
        x,y=-16,-16
    [/scroll]

    [scroll]
        x,y=16,16
    [/scroll]

    [scroll]
        x,y=-8,-8
    [/scroll]

    [scroll]
        x,y=4,4
    [/scroll]
#enddef


# Flashes the screen a bit. RGB sets the colour: (255,255,255) makes the flash
# white, (255,0,0) red, etc.

#define FLASH RGB
    {VARIABLE FLASH_delay 5}

    {REPEAT}
        [colour_adjust]
            red,green,blue={RGB}
        [/colour_adjust]

        [redraw][/redraw]

        [colour_adjust]
            red,green,blue=0,0,0
        [/colour_adjust]

        [redraw][/redraw]

        [delay]
            time=$FLASH_delay
        [/delay]
    {TIMES 2}
#enddef


# Macro for the event that changes a sleeping gryphon into a normal gryphon when
# attacked.

#define GRYPHON_WAKEUP_EVENT
    [event]
        name=attack
        first_time_only=no

        [filter_second]
            type=Sleeping Gryphon
        [/filter_second]

        {MODIFY_UNIT (x,y=$x2,$y2) type Gryphon}
    [/event]
#enddef


# Used by FADE_TO_BLACK (below).

#define FADE_STEP LIGHTNESS DELAY
    [colour_adjust]
        red,green,blue={LIGHTNESS},{LIGHTNESS},{LIGHTNESS}
    [/colour_adjust]

    [redraw][/redraw]
#enddef


# Fades the screen to black using [colour_adjust].

#define FADE_TO_BLACK
    # {FADE_STEP -16 5}
    {FADE_STEP -32 5}
    # {FADE_STEP -48 5}
    {FADE_STEP -64 5}
    # {FADE_STEP -80 5}
    {FADE_STEP -96 5}
    # {FADE_STEP -112 5}
    {FADE_STEP -128 5}
    # {FADE_STEP -144 5}
    {FADE_STEP -160 5}
    # {FADE_STEP -176 5}
    {FADE_STEP -192 5}
    # {FADE_STEP -208 5}
    {FADE_STEP -224 5}
    # {FADE_STEP -240 5}
    {FADE_STEP -55 5}
#enddef


# A dummy side is needed for highlights and dummy units. This macro defines a
# dummy side to which all dummies belong to. So, any scenario where the macro
# HIGHLIGHT_HEX or a regular dummy unit (id=dummy) is used requires this macro
# to be used. SIDENUM defines the side number of the dummy side (can be
# any valid side number).
#
# colour=8 (white) is because when highlighting with a dummy unit, a unit halo
# of any other colour would be visible.

#define DUMMY_SIDE SIDENUM
    [side]
        side={SIDENUM}
        team_name=DUMMY_SIDE_dummies
        colour=8
        no_leader=yes
    [/side]

    [event]
        name=prestart

        {VARIABLE DUMMY_SIDE_NUM {SIDENUM}}
    [/event]
#enddef


# Highlights a hex for a moment by flashing it. Uses [hide_unit], so no units
# should be hidden when this macro is used.
#
# It would be easy to make it work without [hide_unit], though.

#define HIGHLIGHT_HEX X Y
    [scroll_to]
        x,y={X},{Y}
    [/scroll_to]

    [if]
        [have_unit]
            x,y={X},{Y}
        [/have_unit]

        [then]
            {REPEAT}
                [unit_overlay]
                    x,y={X},{Y}
                    image=terrain/highlight.png
                [/unit_overlay]

                [redraw][/redraw]

                [delay]
                    time=50
                [/delay]

                [remove_unit_overlay]
                    x,y={X},{Y}
                    image=terrain/highlight.png
                [/remove_unit_overlay]

                [redraw][/redraw]

                [delay]
                    time=50
                [/delay]
            {TIMES 3}
        [/then]

        [else]
            [unit]
                side=$DUMMY_SIDE_NUM
                type=highlight_dummy
                x,y={X},{Y}
            [/unit]

            {REPEAT}
                [hide_unit]
                    x,y={X},{Y}
                [/hide_unit]

                [redraw][/redraw]

                [delay]
                    time=50
                [/delay]

                [unhide_unit][/unhide_unit]

                [redraw][/redraw]

                [delay]
                    time=50
                [/delay]
            {TIMES 3}

            [kill]
                x,y={X},{Y}
                animate=no
            [/kill]
        [/else]
    [/if]
#enddef



# Includes STUFF only on easy difficulty

#define ON_EASY STUFF
#ifdef EASY
{STUFF}
#endif
#enddef


# Includes STUFF only on normal difficulty

#define ON_NORMAL STUFF
#ifdef NORMAL
{STUFF}
#endif
#enddef


# Includes STUFF only on hard difficulty

#define ON_HARD STUFF
#ifdef HARD
{STUFF}
#endif
#enddef


# Moves a unit from its current location to the given location along a
# relatively straight line displaying the movement just like [move_unit_fake]
# does.
#
# Note that setting the destination on an existing unit does not kill either
# one, but causes the unit to move to the nearest vacant hex instead.

#define MOVE_UNIT FILTER TO_X TO_Y
    [store_unit]
        [filter]
            {FILTER}
        [/filter]

        variable=FL_temp
        kill=no
    [/store_unit]

    [scroll_to]
        x=$FL_temp.x
        y=$FL_temp.y
    [/scroll_to]

    [hide_unit]
        x=$FL_temp.x
        y=$FL_temp.y
    [/hide_unit]

    {VARIABLE_OP x_coords format ("$FL_temp.x|,{TO_X}")}
    {VARIABLE_OP y_coords format ("$FL_temp.y|,{TO_Y}")}

    [move_unit_fake]
        type=$FL_temp.type
        x=$x_coords
        y=$y_coords
    [/move_unit_fake]

    [teleport]
        [filter]
            {FILTER}
        [/filter]

        x,y={TO_X},{TO_Y}
    [/teleport]

    [unhide_unit][/unhide_unit]

    [redraw][/redraw]
#enddef

#define VICTORY_SMALL_BONUS
	[endlevel]
		[gold]
			amount=($turns-$turn)*25
		[/gold]
		result=victory
		bonus=no
	[/endlevel]		
#enddef
This page was last edited on 23 March 2008, at 12:36.