// ***********************************************
// * Script: Lever                               *
// * Author: Kelandon                            *
// ***********************************************

// Lever v1.0.0
// lever.txt (variant of my Search-Step)
// by Kelandon (tomwatts@berkeley.edu)
// A script for levers.
// - Modified (heavily) as needed for Amnesia.

// Memory Cells - 
//  0 - Number of a state in the town script. This is the state that is called 
//	when the item is used.
//  --- added cells ---
//  1,2 - SDF containing where to store value of lever prior to calling town script.
//        Could be val or -val.
//  3 - Value of lever prior to multiplying by -1 or 1 depending on lever state.

beginterrainscript; 

variables;

short choice, minus, adder;

body;

beginstate INIT_STATE;
break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
	set_state_continue(10);
break;
        
beginstate STEP_INTO_SPOT_STATE;
	block_entry(1);
	set_state_continue(10);
break;

beginstate 10;
	reset_dialog_preset_options(2);
	choice = run_dialog(1);
	if (choice == 2) {
		flip_terrain(my_loc_x(), my_loc_y());
		play_sound(-94);
		if (get_terrain(my_loc_x(), my_loc_y()) == 374)
			minus = -1;
		else
			minus = 1;
		if (get_memory_cell(0) > 0) {
			// set which lever position this is
			if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0)) {
				adder = get_memory_cell(3) * minus;
				set_flag(get_memory_cell(1), get_memory_cell(2), adder);
			}
			run_town_script(get_memory_cell(0));
		}
	}
break;
