// Lever v1.1
// lever.txt (variant of my Search-Step, which is based on Jeff Vogel's specobj)
// by Kelandon (tomwatts@berkeley.edu)
// This script is backwards compatible with v1.0, no memory cell change
// necessary.
//
// A script for levers.
//
// NOTE: This script keeps track of the lever's position when the town is
// initially entered. If you flip the terrain after that, this script will think
// the lever is in its second position. Set memory cell 1 to 1 for this not to
// matter.
//
// Memory Cells - 
//  0 - Number of a state in the town script. This is the state that is called 
//		when the lever is used.
//	1 - When the state is called. If 0, whenever flipped. If 1, only when the
//		lever moves from its unflipped initial position. If 2, calls a different
//		state when flipped back from the second position.
//	2 - If cell 1 is 2, which town state to call when the lever is in the second
//		position.


beginterrainscript; 

variables;
short choice,first_position;
body;

beginstate INIT_STATE;
	first_position = get_terrain(my_loc_x(),my_loc_y());
break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
	reset_dialog_preset_options(2); // Standard lever dialog
	choice = run_dialog(1); // Run it.
	if (choice == 2) // If the party pulls the lever,
		{flip_terrain(my_loc_x(),my_loc_y()); // flip the lever.
		play_sound(-94); // Play the lever sound.
		// Now, depending on the behavior defined in cell 1, maybe call a state.
		if (get_terrain(my_loc_x(),my_loc_y()) == first_position)
			{if (get_memory_cell(1) == 0)
				run_town_script(get_memory_cell(0));
			if (get_memory_cell(1) == 1)
				end();
			if (get_memory_cell(1) == 2)
				run_town_script(get_memory_cell(2));
			}
		else
			run_town_script(get_memory_cell(0));
		}
break;
	
beginstate STEP_INTO_SPOT_STATE;
	reset_dialog_preset_options(2); // Standard lever dialog
	choice = run_dialog(1); // Run it.
	if (choice == 2) // If the party pulls the lever,
		{flip_terrain(my_loc_x(),my_loc_y()); // flip the lever.
		play_sound(-94); // Play the lever sound.
		// Now, depending on the behavior defined in cell 1, maybe call a state.
		if (get_terrain(my_loc_x(),my_loc_y()) == first_position)
			{if (get_memory_cell(1) == 0)
				run_town_script(get_memory_cell(0));
			if (get_memory_cell(1) == 1)
				end();
			if (get_memory_cell(1) == 2)
				run_town_script(get_memory_cell(2));
			}
		else
			run_town_script(get_memory_cell(0));
		}
break;