// Exodus Door v1.0.0
// by Kelandon (tomwatts@berkeley.edu)
// door.txt - This is the basic script that powers all doors. If you are 
// working on a scenario that contains doors, DO NOT REMOVE THIS SCRIPT.
//
// If you place a door, this script is automatically attached to it. The
// default settings for the script are for a basic, unlocked door. To make it
// locked, you will need to change the memory cells.
//
// If you set memory cell 0 to 200, then this door really is impenetrable.
//
// The magically locked business is separate from physically locked now.
//
// 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 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 - Magical lock level. If left at 0, door is not magically locked.
//		Otherwise, this is the unlock spell strength required to unlock the
//		door.

beginterrainscript; 

variables;
	short i,j
	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(4) > 0)
		i_am_locked = 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;
		}
		
	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) {
			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();
			
			// Bashing the door
			if (choice == 2) {
				if (get_physical_strength() == 200) {
					door_opened = 0;
					message_dialog("You push on this door, but you quickly realize that you are having no effect whatsoever. This door cannot be bashed down.","");
					end();
					}
				if (get_memory_cell(4) > 0) {
					message_dialog("This door is magically locked. You slam into it repeatedly, but nothing happens. You will not be able to bash it down.","");
					end();
					}
				if (run_bash_door(get_physical_strength() - 3 + (get_ran(2,1,6) / 2)) == FALSE)
					door_opened = 0;
				}
				
			// Picking the lock.
			if (choice == 3) {
				if (get_memory_cell(4) > 0) {
					// Require magic lockpicks
					if (has_item_of_class(1,0) == 0) {
						message_dialog("This door is magically locked. You play with the lock for a while, but it will not budge without magical assistance. Your lockpicks are of no use here.","");
						end();
						}
					}
				if (get_mechanism_difficulty() == 200) {
					door_opened = 0;
					message_dialog("You fiddle with the lock on this door, but you quickly realize that you are having no effect whatsoever. This door's lock cannot be picked.","");
					}
				if (run_pick_lock(get_mechanism_difficulty() - 3 + (get_ran(2,1,6) / 2)) == 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;
	if ((i_am_open > 0) || (i_am_locked == 0)) {
		print_str_color("Unlock Doors: The spell doesn't affect unlocked doors.",2);
		end();
		}
	
	// If magically locked, spell strength has to be greater than equal to lock
	// strength.
	if (get_memory_cell(4))
			if (get_unlock_spell_strength() >= get_memory_cell(4))
				i_am_locked = 0;
				
	// If normally locked, spell strength has to be 50% greater than lock
	// strength.
	else
		if (get_unlock_spell_strength() >= get_mechanism_difficulty() * 3 / 2)
			i_am_locked = 0;
	
	// Inform the player whether the spell was strong enough or not. Do other
	// appropriate things, if necessary.
	if (i_am_locked)
		print_str_color("Unlock Doors: The spell isn't strong enough to affect the door.",2);
	else {
		print_str_color("Unlock Doors: The spell unlocks a door.",2);
		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);
		}
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;
