// doorwheel.txt - This is the basic script that handles any doorwheel that  requires a
// special item to open.

// Memory Cells - 
//   0 - Lock level. If left at 0, doorwheel is unlocked. 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 wheel. Otherwise,
//     if that party has this special item, the doorwheel 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 doorwheel is unlocked. If the flag is non-zero,
//     then when the party looks at the doorwheel it will become unlocked.
//    4 - Strength needed to turn wheel.
//    5,6 - Coordinates of gate to be opened.
//   7 - Call a state in the town script, this is my personal alteration, Ishad Nha.


beginterrainscript; 

variables;
	short i_am_locked = 0;
	short choice;
body;

beginstate INIT_STATE;
	if (get_memory_cell(0) > 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 BLOCK_MOVE_STATE;
		set_state_continue(SEARCH_STATE);
break;

beginstate SEARCH_STATE;
	reset_dialog();
		add_dialog_str(0,"There is a large, cavewood wheel here, with a long length of iron chain wrapped around it several times. The chain then goes from the wheel into a hole in the ground.",0);
	add_dialog_choice(0,"Leave the wheel alone.");
	add_dialog_choice(1,"Try to turn it.");
	choice = run_dialog(0);
	if (choice == 1)
		end();
	
	if (get_skill_total(0) < get_memory_cell(4)) {
		message_dialog("You try to turn the wheel, but it is too large and too stuck. You aren't strong enough to budge it.","");
		end();
		}

		if (get_memory_cell(1) == 0) 
				set_state_continue(3);
				
		if ((get_memory_cell(1) > 0) && (i_am_locked > 0)) {
			if (has_special_item(get_memory_cell(1))) {
				set_state_continue(3);
				}
			} 
	if (get_memory_cell(7) > 9)
		run_town_script(get_memory_cell(7));
			
break;

beginstate 3;
				play_sound(99);
				print_str_color("You hear the sounds of grinding gears and the chain",2);
				print_str_color("  scraping over stone.",2);
				if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
					set_flag(get_memory_cell(2),get_memory_cell(3),1);
				if ((get_memory_cell(5) > 0) || (get_memory_cell(6) > 0))
					flip_terrain(get_memory_cell(5),get_memory_cell(6));
					
				i_am_locked = 0;
					if (get_memory_cell(7) > 9)
		run_town_script(get_memory_cell(7));

break;