[set_menu_item]
	id=use_item
	description=_ "Use item"
	[show_if]
		[variable]
			name=scenario_number
			equals=1
		[/variable]
	[/show_if]
	[filter_location]
		[filter]
			side=1
			[wml_filter]
				[variables]
					isholdingitem="1"
				[/variables]
			[/wml_filter]
		[/filter]			
	[/filter_location]
	[command]
		{VARIABLE_OP is_item_used value no}
		[store_unit]
			variable=item_user
			[filter]
				x=$x1
				y=$y1
				side=1
			[/filter]
		[/store_unit]
		[fire_event]
			name=unlock_door
			[primary_unit]
				x=$x1
				y=$y1
			[/primary_unit]
		[/fire_event]
		[fire_event]
			name=use_torch
			[primary_unit]
				x=$x1
				y=$y1
			[/primary_unit]
		[/fire_event]
		[fire_event]
			name=make_torch
			[primary_unit]
				x=$x1
				y=$y1
			[/primary_unit]
		[/fire_event]
		[if]
			[variable]
				name=is_item_used
				equals=no
			[/variable]
		[then]
			[message]
				x=$x1
				y=$y1
				message=_ "I don't know how I can use the $item_user.variables.theitem|."
			[/message]
		[/then]
		[/if]
	[/command]
[/set_menu_item]

[event]
	name=unlock_door
	first_time_only=no
	[filter]
		side=1
		[filter_wml]
			[variables]
				theitem="wire"
			[/variables]
		[/filter_wml]
		[filter_location]
			x=24,33,27,25
			y=25,5,17,15
		[/filter_location]
	[/filter]

	[message]
		x=$x1
		y=$y1
		message=_ "This door can't be opened with the wire only."
	[/message]
	
	{VARIABLE_OP is_item_used value yes}
[/event]

[event]
	name=unlock_door
	first_time_only=no
	[filter]
		side=1
		[filter_wml]
			[variables]
				theitem="key"
			[/variables]
		[/filter_wml]
		[filter_location]
			x=24,33,27,25
			y=25,5,17,15
		[/filter_location]
	[/filter]
	
	[if]
		[variable]
			name=door_closed_$x1|_$y1
			equals=yes
		[/variable]
	[then]
		[store_locations]
			variable=door_loc
			x=$x1
			y=$y1
			radius=1
		[/store_locations]
		{FOREACH door_loc i}
			[if]
				[variable]
					name=door_loc[$i].terrain
					equals="xx"
				[/variable]
			[then]
				[terrain]
					x=$door_loc[$i].x
					y=$door_loc[$i].y
					terrain=Re
				[/terrain]
				[removeitem]
					x=$door_loc[$i].x
					y=$door_loc[$i].y
				[/removeitem]
			[/then]
			[/if]
		{NEXT i}
		[sound]
			name=open-chest.wav
		[/sound]
		{VARIABLE_OP door_closed_$x1_$y1 value no}
	[/then]
	[/if]
	
	{VARIABLE_OP is_item_used value yes}
[/event]

[event]
	name=unlock_door
	first_time_only=no
	[filter]
		[filter_location]
			x=30,36,32,28,27,34,32,29
			y=26,20,17,13,20,24,7,4
		[/filter_location]
		side=1
		[and]
			[filter_wml]
				[variables]
					theitem="wire"
				[/variables]						
			[/filter_wml]
			[or]
				[filter_wml]
					[variables]
						theitem="key"
					[/variables]
				[/filter_wml]
			[/or]
		[/and]
	[/filter]
					
	# set new terrain at the door location
	[store_locations]
		variable=door_loc
		x=$x1
		y=$y1
		radius=1
	[/store_locations]
	{FOREACH door_loc i}
		[if]
			[variable]
				name=door_loc[$i].terrain
				equals="xx"
			[/variable]
		[then]
			[terrain]
				x=$door_loc[$i].x
				y=$door_loc[$i].y
				terrain=Re
			[/terrain]
			[removeitem]
				x=$door_loc[$i].x
				y=$door_loc[$i].y
			[/removeitem]
		[/then]
		[/if]
	{NEXT i}
	[sound]
	name=open-chest.wav
	[/sound]
	{VARIABLE_OP door_closed_$x1|_$y1 value no}
	
	[if]
		[variable]
			name=door_closed_34_24
			equals=no
		[/variable]
	[then]
		{CLEAR_FOG 1 36 26 1}
		[store_unit]
			variable=prison_unit
			[filter]
				side=6
				[filter_location]
					x=36-37
					y=26
				[/filter_location]
			[/filter]
		[/store_unit]
		[message]
			x=34
			y=24
			message=_ "Who and who, I am glad to see you here too. I'll need all the possible help to get away from this prison."
		[/message]
		[message]
			x=36
			y=26
			message=_ "Carac, we'll do what we can. After all, none of us are insane enough to want to stay in the prison..."
		[/message]
		{UNCLEAR_FOG}
		{VARIABLE_OP door_closed_34_24 value done}
	[/then]
	[/if]
	
	[if]
		[variable]
			name=door_closed_30_26
			equals=no
		[/variable]
	[then]
		{CLEAR_FOG 1 30 29 1}
		[store_unit]
			variable=prison_unit
			[filter]
				side=6
				[filter_location]
					x=29-31
					y=30
				[/filter_location]
			[/filter]
		[/store_unit]
		[message]
			x=30
			y=26
			message=_ "Great to see you two, who and who! Let's escape from this prison."
		[/message]
		[message]
			x=29
			y=30
			message=_ "Oh Carac, we own you one. We will have to be very careful in dealing with the guards. There is a large number of them. I had a glimpse before they locked me in."
		[/message]
		[message]
			x=30
			y=26
			message=_ "I think we should try and release all of us before the majority of the guards notice us. That means, we have to be quick..."
		[/message]
		{UNCLEAR_FOG}
		{VARIABLE_OP door_closed_30_26 value done}
	[/then]
	[/if]
	
	[if]
		[variable]
			name=door_closed_32_17
			equals=no
		[/variable]
	[then]
		{CLEAR_FOG 1 35 16 2}
		[store_unit]
			variable=prison_unit
			[filter]
				side=6
				[filter_location]
					x=36
					y=15
				[/filter_location]
			[/filter]
		[/store_unit]
		[message]
			x=32
			y=17
			message=_ "Come along. Let's get out of this prison!"
		[/message]
		[message]
			x=36
			y=15
			message=_ "Great. I don't want to die just yet."
		[/message]
		{UNCLEAR_FOG}
		{VARIABLE_OP door_closed_32_17 value done}
	[/then]
	[/if]
	
	[if]
		[variable]
			name=door_closed_28_13
			equals=no
		[/variable]
	[then]
		{CLEAR_FOG 1 30 12 1}
		[store_unit]
			variable=prison_unit
			[filter]
				side=6
				[filter_location]
					x=30-31
					y=11-12
				[/filter_location]
			[/filter]
		[/store_unit]
		[message]
			x=28
			y=13
			message=_ "We are getting out of this prison. Follow us."
		[/message]
		[message]
			x=30
			y=11
			message=_ "We are glad to do so..."
		[/message]
		{UNCLEAR_FOG}
		{VARIABLE_OP door_closed_28_13 value done}
	[/then]
	[/if]
	
	{FOREACH prison_unit i}
		{VARIABLE_OP prison_unit[$i].side value 1}
		{VARIABLE prison_unit[$i].variables.isholdingitem 0}
		[unstore_unit]
			variable=prison_unit[$i]
		[/unstore_unit]
	{NEXT i}
	{CLEAR_VARIABLE prison_unit}
	
	{VARIABLE_OP is_item_used value yes}
[/event]

[event]
	name=use_torch
	first_time_only=no
	[filter]
		side=1
		[filter_wml]
			[variables]
				isholdingitem="1"
				theitem="torch"
			[/variables]
		[/filter_wml]
		[filter_location]
			x=23
			y=19
		[/filter_location]
	[/filter]			
	
	[store_locations]
		variable=south_wall
		x=23
		y=20
	[/store_locations]
	[if]
		[variable]
			name=south_wall.terrain
			equals=Xu
		[/variable]
		[variable]
			name=is_torch_usable
			equals=yes
		[/variable]
	[then]
		[sound]
			name=sling-big-miss.ogg
		[/sound]				
		{MODIFY_TERRAIN Uu 23 20}
	[/then]
	[/if]
	
	{VARIABLE_OP is_item_used value yes}
[/event]

[event]
	name=make_torch
	first_time_only=no
	[filter]
		side=1
		[filter_wml]
			[variables]
				isholdingitem="1"
				theitem="wooden_pieces"
			[/variables]
		[/filter_wml]
	[/filter]			
	{VARIABLE go_ahead no}
	[if]
		[variable]
			name=p_$x1|_$y1|_s_0
			greater_than=0
		[/variable]
		[and]
			[variable]
				name=p_$x1|_$y1|_s_1_type
				equals=burnable
			[/variable]
			[or]
				[variable]
					name=p_$x1|_$y1|_s_2_type
					equals=burnable
				[/variable]
			[/or]
			[or]
				[variable]
					name=p_$x1|_$y1|_s_3_type
					equals=burnable
				[/variable]
			[/or]
			[or]
				[variable]
					name=p_$x1|_$y1|_s_4_type
					equals=burnable
				[/variable]
			[/or]
			[or]
				[variable]
					name=p_$x1|_$y1|_s_5_type
					equals=burnable
				[/variable]
			[/or]
		[/and]
	[then]
		[message]
			x=$x1
			y=$y1
			message=_ "This will use the wooden pieces and one of the burnable crops. Shall I go ahead?"
			[option]
				message=_ "Yes, go ahead."
				[command]
					{VARIABLE_OP go_ahead value yes}
				[/command]
			[/option]
			[option]
				message=_ "No, forget about it."
				[command]
				[/command]
			[/option]
		[/message]
		[if]
			[variable]
				name=go_ahead
				equals=yes
			[/variable]
		[then]
			[store_unit]
				variable=torch_unit
				[filter]
					x=$x1
					y=$y1
					side=1
				[/filter]
			[/store_unit]
			[removeitem]
				x=$x1
				y=$y1
			[/removeitem]
			[remove_unit_overlay]
				x=$x1
				y=$y1
				image=$torch_unit.variables.theitemfile
			[/remove_unit_overlay]
			
			# we want to store a torch_unit which does not have the image overlay, hence the double store_unit
			[store_unit]
				variable=torch_unit
				[filter]
					x=$x1
					y=$y1
					side=1
				[/filter]
			[/store_unit]
			{VARIABLE_OP torch_unit.variables.theitem format torch}
			{VARIABLE_OP torch_unit.variables.isholdingitem value 1}
			{VARIABLE_OP torch_unit.variables.theitemname format "Torch"}
			{VARIABLE_OP torch_unit.variables.theitemfile format "items/torch.png"}
			{VARIABLE_OP torch_unit.variables.theitemtype format weapon}
			[unstore_unit]
				variable=torch_unit
			[/unstore_unit]
			[unit_overlay]
				x=$x1
				y=$y1
				image=items/torch.png
			[/unit_overlay]
			[sound]
				name=torch-miss.ogg
			[/sound]
			
			{VARIABLE_OP p_$x1|_$y1|_s_0 add -1}
			
			{VARIABLE i 1}
			[while]
				[variable]
					name=p_$x1|_$y1|_s_$i|_type
					not_equals=burnable
				[/variable]
			[do]
				{VARIABLE_OP i add 1}
			[/do]
			[/while]
			{VARIABLE_OP p_$x1|_$y1|_s_$i value 0}
			{CLEAR_VARIABLE i}
			
			{VARIABLE Repeat_i 1}
			[while]
				[variable]
					name=Repeat_i
					less_than=6
				[/variable]

				[do]
					[if]
						[variable]
							name=p_$x1|_$y1|_s_$Repeat_i
							not_equals=0
						[/variable]
					[then]
						{VARIABLE_OP pos_i_file to_variable p_$x1|_$y1|_s_$Repeat_i|_file}
						[item]
							x=$x1
							y=$y1
							image=$pos_i_file
						[/item]
					[/then]
					[/if]
					{VARIABLE_OP Repeat_i add 1}
				[/do]
			[/while]
			{CLEAR_VARIABLE Repeat_i}
			{CLEAR_VARIABLE pos_i_file}
		[/then]
		[/if]
	[/then]
	[/if]
	
	{VARIABLE_OP is_item_used value yes}
[/event]
