// v1.0.2
// magicdoor.txt
// Xoid (xoid@hotmail.com)

// Version History:
//   v1.0.2 - Version by Xoid. There are bug fixes galore.
//   v1.0.1 - Version by Xoid. Forked off from v1.0.1 of door.txt
//   v1.0.0 - Original version by Jeff Vogel.

// This is a derivative of the basic script that powers all doors. This script
// is backwards compatible with the door.txt that ships with BoA. The default
// settings for the script are for a basic, unlocked door. To make it locked,
// you will need to change the memory cells.

// Memory Cells:
//   0 - Lock difficulty. This is the tool use skill needed to unlock the door.
//     Lockpicks help. If set to a really high number (like 200), it cannot be
//     unlocked by normal means. If left at 0, the door will be left unlocked.

//   1 - Special item needed. If left at 0, no special item helps unlock the
//     door. Otherwise, if the party has this special item, the door is
//     automatically unlocked.

// 2,3 - SDF coordinates. If these are left at 0 and 0, these memory cells are
//     ignored. Otherwise, the SDF is set to 1 when the door is unlocked. If the
//     SDF is non-zero, then when the party enters the zone, the door is
//     automatically unlocked.

//   4 - Door strength. This is the strength skill needed to bash down the door.
//     If set to a really high number (like 200), it cannot be bashed down by
//     normal means. If left at 0, Door strength will default to memory cell 0.
//
//     [Making this script backwards compatible with v1.0.0 of magicdoor.txt]

//   5 - Magic strength. This is the level of the unlock doors spell needed to
//     remove the magical part of the lock. If set to a really high number
//     (like 10), the magical part of the lock cannot be removed by normal
//     means. If left at 0, Door strength will default to memory cell 0.
//
//     [Making this script backwards compatible with v1.0.0 of magicdoor.txt]

beginterrainscript;

variables;
	int choice,cur_terrain,short door_opened;
	int i_am_locked = 0;
	int i_am_open = 0;
	int magic_removed = 0;
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;

	// CPeters - These next 2 if statements are some of my additions.
	if (get_memory_cell(4) == 0)
		set_memory_cell(4,get_memory_cell(0));
	if (get_memory_cell(5) == 0)
		set_memory_cell(5,get_memory_cell(0));

	if (get_memory_cell(0) > 0) {
		i_am_locked = 1;
		set_mechanism_difficulty(get_memory_cell(0));
		// CPeters - Another of my changes.
		set_physical_strength(get_memory_cell(4));

		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 ((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) {
			if (magic_removed == 0) {
				message_dialog("This door is locked. You try to examine the lock, but it is surrounded by a faint field of energy. You can't reach it until you remove the enchantment with a spell of your own.","");
				end();
			}

			reset_dialog();
			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.");
			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))
				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;
	// CPeters - Most of this sections is re-written using the a mix 'n'
	// match technique of my own devise.  Plain english?  I'm lazy.
	if ((i_am_open > 0) || (i_am_locked == 0))
		print_str_color("Unlock Doors: The spell doesn't affect unlocked doors.",2);
	else if ((magic_removed > 0) && (i_am_locked == 1)) {
		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);
		}
	}
	else {
		// CPeters - minor alteration.  get_memory_cell(0) is now get_memory_cell(5)
		if (get_unlock_spell_strength() >= (get_memory_cell(5))) {
			print_str_color("Unlock Doors: The magical part of the lock has been removed.",2);
			print_str_color("  (It can now be picked normally.)",2);
			magic_removed = 1;
			play_sound(89);
		}
		else 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;