// v1.0.0
// containers.txt
// by Xoid (xoid@hotmail.com)

// Version History:
//   v1.0.0 - Original version.  It is a derivative from door.txt.

// The default settings for the script are for a basic, unlocked container. To
// make it locked, you will need to change the memory cells.

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

//   1 - Special item needed. If left at 0, no special item helps unlock the
//     container. Otherwise, if that party has this special item, the container
//     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 container is unlocked.
//     If the SDF is non-zero, then when the party enters the zone, the
//     container is automatically unlocked.

//   4 - Lock strength. This is the strength skill needed to break the lock. If
//     set to a really high number (like 200), it cannot be broken by normal
//     means. If left at 0, lock strength will default to memory cell 0.

beginterrainscript;

variables;
	int c1,choice,cur_terrain,door_opened;
	int i_am_locked = 0;
	int i_am_open = 0;
	string s1;
body;

beginstate INIT_STATE;
	cur_terrain = terrain_in_this_spot();
	if ((cur_terrain >= 448) && (cur_terrain <= 458))
	  	i_am_open = 1;
	else i_am_open = 0;

	if (get_memory_cell(4) == 0)
		set_memory_cell(4,get_memory_cell(0));

	if (get_memory_cell(0) > 0) {
		i_am_locked = 1;
		set_mechanism_difficulty(get_memory_cell(0));
		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) {
		if (item_of_class_on_spot(my_loc_x(),my_loc_y(),-1) == FALSE) {
			print_str_color("You close the container.",2);
			flip_terrain(my_loc_x(),my_loc_y());
			i_am_open = 0;
			play_sound(-59);
		}
	}
	else {
		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 container.",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();
			if (get_memory_cell(1) > 0)
				add_dialog_str(0,"This container is locked, and you don't have the right key. You can have your strongest character try to break the lock (which requires high STR) or have your most skilled character try to pick the lock (which requires high TUS and a lockpick).",0);
			else add_dialog_str(0,"This container is locked. You can have your strongest character try to break the lock (which requires high STR) or have your most skilled character try to pick the lock (which requires high TUS and a lockpick).",0);

			add_dialog_choice(0,"Leave the door alone.");
			add_dialog_choice(1,"Try to break the lock.");
			add_dialog_choice(2,"Try to pick the lock.");
			choice = run_dialog(0);

			if (choice == 1)
				end();
			if (choice == 2) {
				clear_buffer();
				c1 = char_with_highest_skill(0);
				append_string("Lock Breaking: ");
				append_char_name(c1);
				if (get_physical_strength() > get_highest_skill(0)) {
					door_opened = 0;
					append_string(" fails. (Strength: ");
					get_buffer_text(s1);
					play_sound(-76);
					print_big_str(s1,get_memory_cell(4),")");
					damage_char(c1,get_ran(1,1,6),4);
				}
				else {
					append_string(" breaks the lock. (Strength: ");
					get_buffer_text(s1);
					print_big_str(s1,get_memory_cell(4),")");
				}
			}
			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 container.");
			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(-107);
		}
	}
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 containers.",2);
	else {
		if (get_unlock_spell_strength() >= get_mechanism_difficulty() * 2) {
			print_str_color("Unlock Doors: The spell unlocks a container.",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 container.",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 container.",2);
	}
break;