// doorflag.txt 
// by Thralni
// v 1.0.0

// This script locks or unlocks a door if a certain flag is set to respectively 0 or a value of your choice.
// The door will open when the flag is equal to the value of the cell.

// Memory Cells - 
//	0,1 - codes for a SDF. If this SDF is not equal to memory cell 2, the door is locked.
//	2 - codes for the value of a SDF flag which will open the door.

beginterrainscript; 

variables;
	short i_am_open = 0;
	short i_am_locked = 0;
	short choice;
body;

beginstate INIT_STATE;
break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
	if (i_am_open == 1) {
		print_str_color("You close the door.",2);
		flip_terrain(my_loc_x(),my_loc_y());
		i_am_open = 0;
		play_sound(-59);
		}
break;

beginstate BLOCK_MOVE_STATE;

		if (get_flag(get_memory_cell(0),get_memory_cell(1)) != get_memory_cell(2)) {
				message_dialog("This door is locked. It doesn't have a lock, so lockpicking is out of the question. It also doesn't seem to be protected by a spell. You won't be able to open the door just now.","");
				block_entry(1);
				end();
				}
			
		if (get_flag(get_memory_cell(0),get_memory_cell(1)) == get_memory_cell(2)) {
				print_str("You open the door.");
				flip_terrain(my_loc_x(),my_loc_y());
				i_am_open = 1;
				i_am_locked = 0;
				play_sound(-58);
				}
break;

beginstate UNLOCK_SPELL_STATE;
break;

beginstate DISPEL_BARRIER_STATE;
	print_str_color("A dispel barrier spell won't work against this door.",2);
break;
