// flaglever.txt
// version 1.0.0
// by Thralni

// You can alway use this script freely, but put my name (Thralni) in the acknowledgements.

// flaglever.txt - Completely changed version of doorlever.txt. Similair to my selfmade walllever.txt.
// With this version you have to ALWAYS set a SDF (otherwise it wouldn't work), which gets set to 1 when you pull the lever. Also, a terrain at a certain
// spot turns into a terrain of your choice. This script is needed when you would want to pull a lever, but a certain thing has to be done first.
// The terrain changes back to its original terrain when pulled a second time (forexample: you pull the lever,
// and the terrain on coordinates 45,67 (previously a plant) turns into a statue. You pull it  again, and the statue becomes what it was before: a plant).

// Memory Cells: 
//   0,1 - The coordinates of a SDF. If that SDF is 1, script is run, if it is 0 does nothing.
//   2,3 - The x,y coordinates of a terrain this lever replaces by a terrain number defined in memory cell 4. If both are left at 0, doesn't
//     do anything.
//	 4   - the terrain of location (memory cells 0 and 1) to change in.
//   5   - a sound to be played
//	 6,7 -  Coordinates for a stuff done flag. If these are 0 and 0, ignored. Otherwise,
//     when the lever is pulled, if the stuff done flag is 0, it becomes 1, and if the flag
//     is non-zero, it becomes 0.
//     If a flag is assigned here and is set to when, when the party enters this town later,
//     this script will open the gates.

// NOTE: be sure to not use flags 299,0 - 299,29. These are used by this script!

beginterrainscript; 

variables;

short choice,terr,flag,x,value1,value2;
	
body;

beginstate INIT_STATE;
break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
break;

beginstate STEP_INTO_SPOT_STATE;
reset_dialog();
add_dialog_str(0,"This lever is big and made of steel. would you like to pull it and see what it does?",0);
add_dialog_choice(0,"Leave it");
add_dialog_choice(1,"Pull and see what happens");
choice = run_dialog(0);

if (choice == 1)
	end();

if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0))
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) == 1) {
			
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0) || (get_memory_cell(4) > 0)) {
				terr = get_terrain(get_memory_cell(2),get_memory_cell(3));
				
				flip_terrain(my_loc_x(),my_loc_y());
				set_flag(299,28,(terr / 256));
				set_flag(299,29,(terr % 256));
				
				x = 0;
				
				while (x == 1) {
					if (flag >= 27)
							flag = 0;
					set_flag(299,flag,terr);
					flag = (flag + 1);
					x = 1;
					}
				
				if ((get_memory_cell(6) > 0) || (get_memory_cell(7) > 0)) {
						if (get_flag(get_memory_cell(6),get_memory_cell(7)) == 0) {
								set_terrain(get_memory_cell(2),get_memory_cell(3),get_memory_cell(4));
								play_sound(get_memory_cell(5));
								set_flag(get_memory_cell(6),get_memory_cell(7),1);
								end();
								}
							value1 = (get_flag(299,28) * 256);
							value2 = get_flag(299,29);
							terr = (value1 + value2);
							set_terrain(get_memory_cell(2),get_memory_cell(3),terr);
							play_sound(get_memory_cell(5));
							set_flag(get_memory_cell(6),get_memory_cell(7),0);
							end();
							}
						}
				}

	message_dialog("Apparently, you didn't do the right thing to be able to pull this lever. It stays tight in its position.","");
break;
