// t8door.txt (modification on powereddoor.txt)
// by Kelandon
// Can only be put on closed doors
// Door closes and locks if four terrains are not active power sources

// Memory Cells - 
//	0,1 - coordinates of first power source
//	2,3 - coordinates of second power source
//	4,5 - coordinates of third power source
//	6,7 - coordinates of fourth power source

beginterrainscript; 

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

beginstate INIT_STATE;
	
	cur_terrain = terrain_in_this_spot();
	i_am_open = 0;
	i_am_locked = 1;
		
	break;

beginstate START_STATE;
	if (i_am_open == 0)
		end();
	if (((get_terrain(get_memory_cell(0),get_memory_cell(1)) != 384) && (get_terrain(get_memory_cell(2),get_memory_cell(3)) != 384)) && ((get_terrain(get_memory_cell(4),get_memory_cell(5)) != 384) && (get_terrain(get_memory_cell(6),get_memory_cell(7)) != 384)))
		{flip_terrain(my_loc_x(),my_loc_y());
		i_am_locked = 1;
		i_am_open = 0;
		play_sound(-59);
		}
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 (i_am_open == 0) {
		block_entry(1);
		
		door_opened = 1;
		
		if (((get_terrain(get_memory_cell(0),get_memory_cell(1)) == 384) && (get_terrain(get_memory_cell(2),get_memory_cell(3)) == 384)) && ((get_terrain(get_memory_cell(4),get_memory_cell(5)) == 384) && (get_terrain(get_memory_cell(6),get_memory_cell(7)) == 384)))
			i_am_locked = 0;
			
		if (i_am_locked) 
			{message_dialog("This door is locked, and you won't be able to get it open without providing it some power.","");
			door_opened = 0;
			}
			
		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;
	print_str_color("Unlock Doors: The spell isn't strong enough to affect the door.",2);
break;

beginstate DISPEL_BARRIER_STATE;
	if ((i_am_open == 0) && (i_am_locked)) {
		print_str_color("Dispel Barrier: The spell fails to affect a locked door.",2);
		}
break;
