// TOWN SCRIPT
//    Town 21: The Dacgban (L2)

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;

short a = 0,g,i,j,k,r1,t,x,y,choice;

body;

beginstate INIT_STATE;
		set_state_continue(26);
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
	g = get_current_tick() % 2;
	if (g == 0) {
	if (get_flag(121,0) == 250)
	set_state_continue(50);
	}
break;

beginstate 11;
	if (get_flag(21,1) >= 1)
		set_state_continue(42);
	set_state_continue(12);
break;

beginstate 12;
	reset_dialog();
	add_dialog_str(0,"On this pedestal is placed the captain's log. Some brief scans reveal that you can read absolutely nothing of a hurried handwriting in a language you've never learned.",0);
	add_dialog_str(1,"Still, you could take it -- someone on this isle must be able to decipher it.",0);
	add_dialog_choice(0,"OK.");
	add_dialog_choice(1,"Take.");
	choice = run_dialog(1);
	if (choice == 1)	
		set_state_continue(13);	
	if (choice == 2)
		set_state_continue(14);
break;

beginstate 13;
	block_entry(1);
	set_flag(21,1,1);
break;
	
beginstate 14;
	message_dialog("You stash away the log.",
	  "[An entry has been added to your Special Items list.]");
	change_spec_item(3,1);
	set_flag(21,1,1);
break;

beginstate 16;
	if (get_flag(21,0) >= 1){
	if (get_flag(21,1) >= 1)
		set_state_continue(19);
	}
break;

beginstate 19;
	if (get_flag(121,0) == 250)
		end();
	reset_dialog();
	add_dialog_str(0,"The captain's room behind you erupts in a blast of water! Someone must have drilled through the ship's hull. The bay's fury is pouring frantically into the lower deck. Duty, it seems, has prompted this suicidal attack.",0);
	add_dialog_str(1,"The ship shakes and tosses violently, threatening to drown you with it. There's only one exit, and it's at the north end of the lower deck.",0);
	add_dialog_str(2,"You had better hurry.",0);
	add_dialog_str(5,"[It is advisable to save your game now.]",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	set_flag(121,0,250);
	erase_char(6);
	a = get_current_tick();
	set_floor(16,40,58);
	set_floor(17,40,58);
	set_floor(18,40,58);		
	set_flag(21,8,0);
break;

beginstate 21;
	if (get_flag(21,7) == 250)
		end();
	message_dialog("The ship shudders and turns, sinking rapidly. You had better make it to the top soon.","");
	set_flag(21,7,250);
break;

beginstate 23;
	message_dialog("Blasts of water fill in from everywhere. You don't make it another two steps before being overwhelmed.","");
	set_state_continue(24);
break;

beginstate 24;
	i = 0;
	while (i < 6) {
		if (char_ok(i)) {
			kill_char(i,2,0);
			}
		i = i + 1;
		}
break;

beginstate 25;
	end_scenario(1);
break;

beginstate 26;
	if (get_flag(21,9) == 250)
		end();
	message_dialog("The lower deck of the ship is mostly quiet. Evidently, most of the sailors are on shore now.","");
	set_flag(21,9,250);
break;

beginstate 27;
	if (get_flag(21,0) >= 1)
		set_state_continue(28);
break;

beginstate 28;
	if (get_flag(21,1) >= 1)
		set_state_continue(29);
break;

beginstate 29;
	if (get_flag(21,2) == 250)
		end();
	set_flag(21,2,250);
	message_dialog("There is a mad scramble ahead as those who are still in the lower deck attempt to escape. Your hopes of mingling with the crowd is quickly dashed, however.",
	  "Hostility is ahead, water behind. There's one way out, and not much time left.");
	activate_hidden_group(1);
break;

beginstate 30;
	if (get_flag(21,3) == 1)
		set_state_continue(32);
break;

beginstate 32;
	message_dialog("As you rummage about in the storeroom, you spot a moving shadow. Looking carefully, you see a small, slight sailor. He sees you. As quickly as he can, he bolts for it, making it out of the room before you can catch him.","");
	set_flag(21,3,2);
break;

beginstate 33;
	if (get_flag(21,3) == 2)
		set_state_continue(34);
break;

beginstate 34;
	message_dialog("Searching carefully behind every pillar, you finally find the sailor that eluded you in the storeroom. A brief interrogation reveals that his name is Terr. You drill him further, but all he says is _Wac! Wac!_ over and over again.",
	  "Your attempts to make him explain are cut short abruptly. An arrow whizzes by your ear and pierces his throat.");
	set_flag(21,3,3);
	set_state_continue(35);
break;

beginstate 35;
	activate_hidden_group(2);
break;

beginstate 36;
	if (get_terrain(18,37) == 10)
		set_state_continue(37);
break;

beginstate 37;
	reset_dialog();
	add_dialog_str(0,"The locking mechanism on this door is remarkably complex as compared to everything else you've seen. Comprised of a series of small levers and wheels, it takes you a while to even figure out how to make it respond.",0);
	add_dialog_str(1,"It appears to be a combination lock in four parts, each capable of spelling out a letter of the alphabet in some code. As to what that four-letter code would be, however, there are no clues.",0);
	add_dialog_str(2,"Do you try a combination?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	if (choice == 1)
		set_state_continue(38);
	if (choice == 2)
		set_state_continue(39);
break;

beginstate 38;
	message_dialog("Nothing happens.","");
	block_entry(1);
	set_state_continue(51);
break;

beginstate 39;
	get_text_response("You Respond:");
		check_text_response_match("wac");
		 if (got_text_match())
			set_state_continue(40);
		check_text_response_match("uuac");
		 if (got_text_match())
			set_state_continue(40);
		check_text_response_match("vvac");
		 if (got_text_match())
			set_state_continue(40);
	set_state_continue(38);
break;

beginstate 40;
	message_dialog("There is a click.","");	
	set_terrain(18,37,14);
break;

beginstate 42;
	message_dialog("There is nothing of interest here.","");
break;

beginstate 43;
	if (get_flag(21,0) >= 1)
	end();
	message_dialog("The seated man holds out a hand, waving you back.",
	  "_Before, hear I wish to say._");
	block_entry(1);
break;


beginstate 46;
	set_flag(21,0,1);
break;

beginstate 47;
	if (get_flag(21,0) >= 1)
		set_state_continue(48);
	set_state_continue(56);
break;

beginstate 48;
	if (get_flag(21,1) >= 1)
		set_state_continue(49);
	set_state_continue(56);
break;

beginstate 49;
	block_entry(1);
	reset_dialog_preset_options(5);
	choice = run_dialog(0);
	if (choice == 1) 	
	end();
	if (choice == 2) {
		move_to_new_town(22,20,11);
		end();
		}
break;

beginstate 50;
	x = 0;
while (x < 48) {
	y = 0;
	while (y < 48) {
	if (get_floor(x,y) == 58) {	
	move_item_on_spot(x,y,-1,-1);			
	if (get_floor(x - 1,y - 1) != 58)
	set_floor(x - 1,y - 1,58);
	if (get_floor(x,y - 1) != 58)	
	set_floor(x,y - 1,58);
	if (get_floor(x + 1,y - 1) != 58)	
	set_floor(x + 1,y - 1,58);
	}
		y = y + 1;
	}
	x = x + 1;
	}
	
	set_state_continue(58);
break;

beginstate 51;
	if (character_in_party(1702) >= 0) {
	if (get_flag(95,0) == 250)
		end();
	message_dialog("_I know the answer._ Fernow smirks. _But I'm not going to tell you. I like seeing people puzzle over things._","");
	set_flag(95,0,250);
	}
break;

beginstate 56;
	block_entry(1);
	reset_dialog_preset_options(5);
	choice = run_dialog(0);
	if (choice == 2) {
		move_to_new_town(20,20,10);
		end();
		}
break;
	
beginstate 58;
	if (get_current_tick()  == a + 4)
			set_state_continue(21);
	else set_state_continue(59);			
break;
			
beginstate 59;
	if (get_current_tick()  == a + 8)
			set_state_continue(23);			
break;