#textdomain wesnoth-utbs
# The file contains useful macros I use within the campaign

#!***Macro to random place unit within area***
#define RANDOM_PLACEMENT X Y RADIUS UNIT
	[store_locations]
		x,y={X},{Y}
		radius={RADIUS}
		variable=random_placement_locations
	[/store_locations]
 
	{VARIABLE_OP random_string format "0..$random_placement_locations.length"}
 
	{RANDOM $random_string}
 
	{VARIABLE_OP random_placement_x format "$random_placement_locations[$random].x"}
	{VARIABLE_OP random_placement_y format "$random_placement_locations[$random].y"}
 
	{CLEAR_VARIABLE random_placement_locations}
 
	{UNIT}
 
	[+unit]
		x,y=$random_placement_x,$random_placement_y
	[/unit]
#enddef

#***Macro that will unclutter scenarios somewhat***
#define ADD VAR AMOUNT
[set_variable]
	name={VAR}
	add={AMOUNT}
[/set_variable]
#enddef

#define STARTLOOP VAR
# Loop VAR times, counting from -VAR to -1.
[set_variable]
name={VAR}
multiply=-1
[/set_variable]

[while]
	[variable]
	name={VAR}
	less_than=0
	[/variable]
	[do] #enddef

# Same as mainline NEXT
#define ENDLOOP VAR
# Macro to end a WML clause that iterates over an array.
    [set_variable]
    name={VAR}
    add=1
    [/set_variable]
    [/do]
[/while]
{CLEAR_VARIABLE {VAR}}
#enddef

# create a unit with full upkeep
#define UNIT_ TYPE DESC USERDESC SIDE X Y
	[unit]
		type={TYPE}
		description={DESC}
		user_description={USERDESC}
		side={SIDE}
		upkeep=full
		x={X}
		y={Y}
	[/unit]
#enddef

# create a unit with no upkeep
#define FREE_UNIT TYPE DESC USERDESC SIDE X Y
	[unit]
		type={TYPE}
		description={DESC}
		user_description={USERDESC}
		side={SIDE}
		upkeep=free
		x={X}
		y={Y}
	[/unit]
#enddef

# create a unit with full upkeep and random traits

#define UNIT_T TYPE DESC USERDESC SIDE X Y
[unit]
	side={SIDE}
	type={TYPE}
	description={DESC}
	user_description={USERDESC}
	upkeep=full
	x={X}
	y={Y}
	random_traits="yes"
[/unit]
#enddef

#define INVOKE_TROLL_VARIATION FILTER
[object]
	silent=yes
	duration=forever

	[filter]
		{FILTER}
	[/filter]

	[effect]
		apply_to=variation
		name=troll
	[/effect]
[/object]

{MODIFY_UNIT {FILTER} unit_description ( _ "These undead trolls fight again in death as they did in life, except this time they smell much worse.")}

[redraw][/redraw]
#enddef


# UNIT_OVERLAY adds an overlay to a unit, taking in a standard filter
#
# Example that gives all spearmen a book:
# {UNIT_OVERLAY type=Spearman items/book1.png}

#define UNIT_OVERLAY FILTER IMG
    [store_unit]
        [filter]
            {FILTER}
        [/filter]
        variable=UNIT_OVERLAY_store
        kill=no
    [/store_unit]
    {FOREACH UNIT_OVERLAY_store UNIT_OVERLAY_i}
	{VARIABLE_OP UNIT_OVERLAY_tempx format
$UNIT_OVERLAY_store[$UNIT_OVERLAY_i].x}
	{VARIABLE_OP UNIT_OVERLAY_tempy format
$UNIT_OVERLAY_store[$UNIT_OVERLAY_i].y}
        [unit_overlay]
            x=$UNIT_OVERLAY_tempx
            y=$UNIT_OVERLAY_tempy
		image={IMG}
        [/unit_overlay]
    {NEXT UNIT_OVERLAY_i}
    {CLEAR_VARIABLE UNIT_OVERLAY_store}
#enddef

#define REMOVE_UNIT_OVERLAY FILTER IMG
    [store_unit]
        [filter]
            {FILTER}
        [/filter]
        variable=UNIT_OVERLAY_store
        kill=no
    [/store_unit]
    {FOREACH UNIT_OVERLAY_store UNIT_OVERLAY_i}
	{VARIABLE_OP UNIT_OVERLAY_tempx format
$UNIT_OVERLAY_store[$UNIT_OVERLAY_i].x}
	{VARIABLE_OP UNIT_OVERLAY_tempy format
$UNIT_OVERLAY_store[$UNIT_OVERLAY_i].y}
        [remove_unit_overlay]
            x=$UNIT_OVERLAY_tempx
            y=$UNIT_OVERLAY_tempy
		image={IMG}
        [/remove_unit_overlay]
    {NEXT UNIT_OVERLAY_i}
    {CLEAR_VARIABLE UNIT_OVERLAY_store}
#enddef

#define SET_PORTRAIT_ON_ADVANCE NAME TYPE PORTRAIT
# Create an event that hacks a character's portrait on level advance.
[event]
	name=post_advance

	[filter]
		description={NAME}
		type={TYPE}
	[/filter]

	{MODIFY_UNIT description={NAME} profile {PORTRAIT}}
[/event]
#enddef
