// doorpower.txt 
// by Thralni
// v 1.0.0

// This script locks or unlocks a door if a certain flag is set to respectively 0 or 1.

// Memory Cells - 
//   0,1 - codes for a SDF. If this SDF is 0, de door is locked. otherwise, if 1, the door
//		is open.

beginterrainscript; 

variables;
	short i_am_open = 0;
	short i_am_locked = 0;
	short door_opened;
	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;

		door_opened = 1;

		if (get_flag(get_memory_cell(0),get_memory_cell(1)) == 0) {
						message_dialog("This door is locked. A faint light radiates from all sides of the door. As long as don't find a way to cut the power off in this place, this door will remain locked.","");
						block_entry(1);
						end();
						}
			
		if (door_opened) {
			print_str("You open the door.");
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
				set_flag(get_memory_cell(2),get_memory_cell(3),1);
			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;
