// spectownlocks.txt - This script handles doors and other terrain types that are only
// unlocked with the aid of a special item.

// Memory Cells - 
//   0 - Lock level. If left at 0, door is unlocked. Otherwise, the strength/tool
//     use needed to get it open. If this is set to a really high number (say, 200),
//     it can't be unlocked by normal means
//   1 - Key needed. If left at 0, no special item helps unlock the door. Otherwise,
//     if that party has this special item, the door automatically unlocks.
//   2,3 - Coordinates for a stuff done flag. If these are 0 and 0, ignored. Otherwise,
//     the stuff done flag is set to 1 when the door is unlocked. If the flag is non-zero,
//     than when the party enters this zone, the door will become unlocked.
//   4 - This gives the relevant script an identifying number which can be retrieved by 
//    get_memory_cell(4);

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();
	if (((cur_terrain >= 14) && (cur_terrain <= 17)) ||
	  ((cur_terrain >= 50) && (cur_terrain <= 53)))
	  	i_am_open = 1;
	  	else i_am_open = 0;

	if (get_memory_cell(0) > 0) {
		i_am_locked = 1;
		set_mechanism_difficulty(get_memory_cell(0));
		set_physical_strength(get_memory_cell(0));
		
		if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
			if (get_sdf(get_memory_cell(2),get_memory_cell(3)) > 0)
				i_am_locked = 0;
			}
		}

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 (i_am_open == 0) {
		block_entry(1);
		door_opened = 1;
			if (current_town() > 3)
			set_state_continue(current_town());
			else
			set_state_continue(90 + current_town());
		} 
break;

beginstate UNLOCK_SPELL_STATE;
	if ((i_am_open > 0) || (i_am_locked == 0))
		print_str_color("Unlock Doors: The spell doesn't affect unlocked doors.",2);
		else {
			if (get_unlock_spell_strength() >= get_mechanism_difficulty() * 2) {
				print_str_color("Unlock Doors: The spell unlocks a door.",2);
				i_am_locked = 0;
				play_sound(9);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
				}
				else {
					print_str_color("Unlock Doors: The spell isn't strong enough to affect the door.",2);
					print_str_color("  (This spell usually only affects doors with magical protection.)",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;

beginstate 3; // door being opened
			flip_terrain(my_loc_x(),my_loc_y());
			i_am_open = 1;
			i_am_locked = 0;
			play_sound(-58);
break;

beginstate 10;
	if (has_special_item(32) == 0)
	message_dialog("There is a huge stone door here, with a large bronze padlock.","You attempt to use all of your skills and resources to open the lock, with no success.");

	else {
	message_dialog("There is a huge stone door here, held shut with a large bronze padlock.","Fortunately, your bronze key opens it easily.");
	set_state_continue(3);
			}
break;

beginstate 20;
	if (has_special_item(37) == 0)
	message_dialog("You start to turn the wheel, but you soon find that you can't move it more than a few inches. You soon find out why ... there's a small gold padlock blocking the mechanism.You try to pick the lock. Then you try to break it off.","After an hour of effort, you have to give up. The lock must be enchanted. It's too strong for you to open it, and you don't have the key.");

	else {
	message_dialog("You start to turn the wheel, only to find that the mechanism is blocked by a small gold padlock. Fortunately, you have the key to the mechanism.","You turn the wheel around three times, as it turns, you hear the sound of stone scraping against stone to the west. As you leave, the padlock springs back onto the wheel, under its own power, and locks itself.");
	set_terrain(13,7,0);
	set_terrain(4,42,55);	
			}
break;

beginstate 28;
	if (has_special_item(36) > 0) {
		message_dialog("There is a heavy, stone door set into this wall.  It is held shut by a large, iron padlock. Fortunately, one of your keys opens it.","");
			set_state_continue(3);
		}		
		else
	message_dialog("There is a heavy, stone door set into this wall.  It is held shut by a large, iron padlock. It looks like you will have a very difficult time getting it open. None of your keys fit.","");
break;


beginstate 30;
	if (has_special_item(34) == 0)
	message_dialog("There is a rather ordinary looking door here, with a very large and stubborn iron lock.","Unfortunately, none of your keys fit.");

	else {
	message_dialog("There is a rather ordinary looking door here, with a very large and stubborn iron lock.","You try your keys and find that the onyx one unlocks it!");
	set_state_continue(3);
			}
break;

beginstate 34;
	if (has_special_item(35) > 0) {
		message_dialog("There is an enormous, locked door here. You try the key you took from Pyrog immediately. It opens the lock.","");
			set_state_continue(3);
		}		
		else
	message_dialog("There is an enormous, locked door here with a dragon carved on it. You cannot get the lock open.","");
break;

beginstate 38;
	if (has_special_item(33) == 0)
	message_dialog("Before you is a huge, granite door with a marble keyhole set into its center. Some runes set into its surface are a rather impressive array of magical wards.","");

	else {
	message_dialog("Before you is a huge, granite door with a marble keyhole set into its center. You place the stone key into the lock and turn it. With a light clicking noise, the door becomes unlocked.","");
	set_state_continue(3);
			}
break;


beginstate 83;

	if (get_memory_cell(4) == 0) {
	if (has_special_item(37) == 0)
	message_dialog("You start to turn the wheel, but you soon find that you can't move it more than a few inches. You soon find out why ... there's a small gold padlock blocking the mechanism.You try to pick the lock. Then you try to break it off.","After an hour of effort, you have to give up. The lock must be enchanted. It's too strong for you to open it, and you don't have the key.");

	else {
	message_dialog("You start to turn the wheel, only to find that the mechanism is blocked by a small gold padlock. Fortunately, you have the key to the mechanism.","You turn the wheel around three times, as it turns, you hear the sound of stone scraping against stone to the west. As you leave, the padlock springs back onto the wheel, under its own power, and locks itself.");
	set_terrain(13,7,0);
	set_terrain(4,42,55);	
			}
	}

	if (get_memory_cell(4) == 1) {
	if (has_special_item(36) > 0) {
		message_dialog("There is a heavy, stone door set into this wall.  It is held shut by a large, iron padlock. Fortunately, one of your keys opens it.","");
			set_state_continue(3);
		}		
		else
	message_dialog("There is a heavy, stone door set into this wall.  It is held shut by a large, iron padlock. It looks like you will have a very difficult time getting it open. None of your keys fit.","");
	}
break;

