// brigdoor.txt - Modified door.txt, written for the doors in t0, Lower
//	Holds. Adjusts the dialog boxes in the BLOCK_MOVE_STATE.

// 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 the flag is at zero,
//	the special dialog box shows, otherwise it's the default. Flag
//	is set to 1 when the door is opened.
//   4 - Town script run when the door is successfully opened.
//   5 - Which door in the brig.
//	1 - starting door
//	2 - SE door (with zombie)
//	3 - NE door (with Capt. Samantha)
//	4 - NW door (with zombie)
//	5 - torture chamber
//	6 - Chase's quarters (no lockpick option)
//	7 - Second door to armory (no lockpick option)
//	8 - First door to armory (need Chase's key)

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;
	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;
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_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;
			}
		
		if ((get_memory_cell(1) > 0) && (i_am_locked > 0)) {
			if (has_special_item(get_memory_cell(1))) {
				print_str_color("You have the key which unlocks this door.",2);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
				i_am_locked = 0;
				}
			} 
		if (i_am_locked) {
			reset_dialog();
			//starting door dialog
			if((get_memory_cell(5) == 1) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)) {
				add_dialog_str(0,"Unsurprisingly, this door is locked. Your would-be rescuers seem less than benevolent now.",0);
				add_dialog_str(1,"Still, the lock looks old and a little rusty. You might've gotten lucky this time. You can try to bash down the door, or you can try to pick the lock (if you have a lockpick).",0);
				}

			//SE zombie door dialog
			else if((get_memory_cell(5) == 2) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0))
				add_dialog_str(0,"This door is locked, and you don't have the key that opens it. You can try and bash the door down, or you can try to pick the lock (if you have a lockpick).",0);

			//NE door dialog (capt. Samantha)
			else if((get_memory_cell(5) == 3) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)) {
				add_dialog_str(0,"You can hear someone or something moving around on the other side of this door. However, the noise from the ship and the ocean makes it impossible to tell what it is.",0);
				add_dialog_str(1,"Regardless, the door is locked, and you don't have the key that opens it. You can try and bash the door down, or you can try to pick the lock (if you have a lockpick).",0);
				}

			//NW zombie door dialog
			else if((get_memory_cell(5) == 4) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0))
				add_dialog_str(0,"This door is locked, and you don't have the key that opens it. You can try and bash the door down, or you can try to pick the lock (if you have a lockpick).",0);

			//torture chamber dialog
			else if((get_memory_cell(5) == 5) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)) {
				add_dialog_str(0,"The frame of this door looks slightly splintered, as if someone has tried to bash it down before. The lock also looks like it has been damaged in the past.",0);
				add_dialog_str(1,"Still, the door is locked, and you don't have the key that opens it. You can try and bash the door down, or you can try to pick the lock (if you have a lockpick).",0);
				}

			//Chase's quarters dialog
			else if((get_memory_cell(5) == 6) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)) {
				add_dialog_str(0,"This door looks odd, in that you can't see any hinges. Or a lock. Or a doorknob. It almost seems like a section of wall that was painted differently.",0);
				add_dialog_str(1,"You're not sure how to open it, but you know the standard adventurer procedure. When there's no lock, just try to bash it down.",0);
				}

			//stuck armory door dialog
			else if((get_memory_cell(5) == 7) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0)) {
				add_dialog_str(0,"You try to open this door, but it's stuck. Not locked, just jammed enough that you're not sure if you can open it.",0);
				add_dialog_str(1,"Still, being an adventurer, you can always try to bash it down by using your strongest character. What do you do?",0);
				}

			//armory door dialog (need key from Chase)
			else if((get_memory_cell(5) == 8) && (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0))
				add_dialog_str(0,"This door is locked, and you don't have the key that opens it. You can try and bash the door down, or you can try to pick the lock (if you have a lockpick).",0);

			//normal dialog
			else if (get_memory_cell(1) > 0)
				add_dialog_str(0,"This door is locked, and you don't have the right key. You can have your strongest character try to bash it down (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and a lockpick).",0);
				else add_dialog_str(0,"This door is locked. You can have your strongest character try to bash it down (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and a lockpick).",0);
			add_dialog_choice(0,"Leave the door alone.");
			add_dialog_choice(1,"Try to bash it down.");
			if((get_memory_cell(5) != 6) && (get_memory_cell(5) != 7))
			add_dialog_choice(2,"Try to pick the lock.");
			choice = run_dialog(0);
			
			if (choice == 1)
				end();
			if (choice == 2) {
				if (run_bash_door(get_physical_strength()) == FALSE)
					door_opened = 0;
				}
			if (choice == 3) {
				if (run_pick_lock(get_mechanism_difficulty()) == FALSE) 
					door_opened = 0;
					else if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
						award_party_xp(BASE_TRAP_XP,1 + 2 * get_mechanism_difficulty());
					
				}
			}
			
		if (door_opened) {
			print_str("You open the door.");
			if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0)) {
				if (get_memory_cell(4) != 0)
					run_town_script(get_memory_cell(4));
				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;
	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;
