// doorwheeltext.txt - This is the script that handles any doorwheel that displays a message
// when it is opened.

// 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 - This gives the relevant terrain script an identifying number which can be retrieved by 
//    get_memory_cell(7);



beginterrainscript; 

variables;
	short i_am_locked = 0;
	short choice;
	short c;
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(current_town());
				
		if ((get_memory_cell(1) > 0) && (i_am_locked > 0)) {
			if (has_special_item(get_memory_cell(1))) {
		set_state_continue(current_town());
				}
			} 
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;
break;

beginstate 30;
	if (get_memory_cell(7) == 4)
	message_dialog("The wheel controls the massive, rusty, iron portcullis to the west.","As you turn it, the gate moves with a loud, shrill squeal. It needs some oil.");
	if (get_memory_cell(7) == 3)
	message_dialog("The wheel controls the massive, rusty, iron portcullis to the east.","As you turn it, the gate slides smoothly in its stone grooves.");
	set_state_continue(3);
break;

beginstate 39;
	if (get_memory_cell(7) == 0) { // this script increases the flag by 2
		if ((get_flag(39,0) == 0) || (get_flag(39,0) == 8)){
				inc_flag(39,0,2);
				set_state_continue(139);				
			}
		if ((get_flag(39,0) == 2) || (get_flag(39,0) == 10))		
	message_dialog("The wheel is still stuck. You can't move it anymore.","");
	}
	
	if (get_memory_cell(7) == 2) { // this script increases the flag by 8
		if ((get_flag(39,0) == 0) || (get_flag(39,0) == 2)){
				inc_flag(39,0,8);
				set_state_continue(139);				
			}
		if ((get_flag(39,0) == 8) || (get_flag(39,0) == 10))		
	message_dialog("The wheel is still stuck. You can't move it anymore.","");
	}
	
	if (get_memory_cell(7) == 8) {
	message_dialog("You hear the distant sounds of gates opening. Or closing. You can't tell from here.","");
	flip_terrain(42,3);
	flip_terrain(29,27);
	}
	
break;


beginstate 139;
	if (get_flag(39,0) == 10)
		set_terrain(29,42,32);	
	message_dialog("With great effort, you manage to turn the wheel a few feet. You feel chains shifting and sliding through holes underneath your feet.","Then the wheel gets stuck. You can't budge it.");
	
break;


