// View plain text
// beam_rot.txt -  A wheel or lever that rotates one or two beam projectors. When it is 
// looked at or stepped onto, asks if the party wants to turn it. 

// Memory Cells - 
//   0,1 - The x,y coordinates of a beam projector. If both are left at 0, doesn't
//     do anything.
//   2,3 - The x,y coordinates of a second beam projector. If both are left at 0, doesn't
//     do anything.
//   4,5 -  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.
//   6,7 -  Directions to rotate the first and second projector, respectively. 
//     0 = counterclockwise
//     1 = clockwise

beginterrainscript; 

variables;

short choice;
body;

beginstate INIT_STATE;		
break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
	reset_dialog_preset_options(8);
	choice = run_dialog(0);
	if (choice == 1)
		end();
	
	flip_terrain(my_loc_x(),my_loc_y());
	play_sound(99);
	play_sound(79);
	
	if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0)){ 
		if(get_memory_cell(6) == 0){
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				print_str_color("One beam projector rotates counterclockwise.",2);
			else
				print_str_color("The beam projector rotates counterclockwise.",2);
			if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 380)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 381);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 381)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 382);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 382)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 383);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 383)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 380);
		}
		else{
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				print_str_color("One beam projector rotates clockwise.",2);
			else
				print_str_color("The beam projector rotates clockwise.",2);
			if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 381)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 380);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 382)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 381);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 383)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 382);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 380)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 383);
		}
	}
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)){ 
		if(get_memory_cell(7) == 0){
			if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0))
				print_str_color("The other beam projector rotates counterclockwise.",2);
			else
				print_str_color("The beam projector rotates counterclockwise.",2);
			if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 380)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 381);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 381)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 382);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 382)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 383);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 383)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 380);
		}
		else{
			if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0))
				print_str_color("The other beam projector rotates clockwise.",2);
			else
				print_str_color("The beam projector rotates clockwise.",2);
			if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 381)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 380);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 382)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 381);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 383)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 382);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 380)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 383);
		}
	}
	
	if ((get_memory_cell(4) > 0) || (get_memory_cell(5) > 0)) {
		if (get_flag(get_memory_cell(4),get_memory_cell(5)) == 0)
			set_flag(get_memory_cell(4),get_memory_cell(5),1);
		else
			set_flag(get_memory_cell(4),get_memory_cell(5),0);
	}
	
break;

beginstate BLOCK_MOVE_STATE;
	reset_dialog_preset_options(8);
	choice = run_dialog(0);
	if (choice == 1)
		end();
	
	flip_terrain(my_loc_x(),my_loc_y());
	play_sound(99);
	play_sound(79);
	
	if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0)){ 
		if(get_memory_cell(6) == 0){
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				print_str_color("One beam projector rotates counterclockwise.",2);
			else
				print_str_color("The beam projector rotates counterclockwise.",2);
			if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 380)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 381);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 381)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 382);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 382)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 383);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 383)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 380);
		}
		else{
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				print_str_color("One beam projector rotates clockwise.",2);
			else
				print_str_color("The beam projector rotates clockwise.",2);
			if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 381)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 380);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 382)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 381);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 383)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 382);
			else if(get_terrain(get_memory_cell(0), get_memory_cell(1)) == 380)
				set_terrain(get_memory_cell(0), get_memory_cell(1), 383);
		}
	}
	if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)){ 
		if(get_memory_cell(7) == 0){
			if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0))
				print_str_color("The other beam projector rotates counterclockwise.",2);
			else
				print_str_color("The beam projector rotates counterclockwise.",2);
			if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 380)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 381);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 381)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 382);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 382)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 383);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 383)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 380);
		}
		else{
			if ((get_memory_cell(0) > 0) || (get_memory_cell(1) > 0))
				print_str_color("The other beam projector rotates clockwise.",2);
			else
				print_str_color("The beam projector rotates clockwise.",2);
			if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 381)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 380);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 382)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 381);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 383)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 382);
			else if(get_terrain(get_memory_cell(2), get_memory_cell(3)) == 380)
				set_terrain(get_memory_cell(2), get_memory_cell(3), 383);
		}
	}
	
	if ((get_memory_cell(4) > 0) || (get_memory_cell(5) > 0)) {
		if (get_flag(get_memory_cell(4),get_memory_cell(5)) == 0)
			set_flag(get_memory_cell(4),get_memory_cell(5),1);
		else
			set_flag(get_memory_cell(4),get_memory_cell(5),0);
	}
break