begintownscript;

variables;
short choice, pc_counter, lead_char;
short ghostlocx;
short countera = 3;

body;

beginstate 0; //INIT_STATE
	enable_add_chars(0); //no new people on the boat

	//intro dialog
	if(get_flag(1,0) == 0) {
		reset_dialog();
		add_dialog_str(0,"You can hear voices now. You might finally be able to find out who's in charge of this horrible vessel.",0);
		if(character_in_party(14) != -1) {
			add_dialog_str(1,"Captain Samantha motions for you to be quiet.",0);
			add_dialog_str(2,"_You hear them? These are probably the ones that threw us in the brig. And it sounds like there's a lot of them._",15);
			}
		run_dialog(1);
		set_flag(1,0,1);
		set_flag(1,1,2); //timer for being discovered
		}

	//groups - mess hall group
	add_range_to_group(7,11,1); //excludes cook
	add_char_to_group(16,1);

	//fix skeletons if necessary
	if(get_flag(1,5) == 1)
		set_terrain(19,30,279);
	if(get_flag(1,6) == 1)
		set_terrain(27,34,279);
	if(get_flag(1,7) == 1)
		set_terrain(31,25,279);

	//fix cauldron if you ate the soup
	if(get_flag(1,16) == 1)
		set_terrain(20,13,248);

	//intro section for captain's chamber
	if(get_flag(1,19) == 1) {
		set_character_facing(1000,0);
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"You enter the dark and musty chamber, passing by a few rotting pots full of salt water. It smells like death, plain and simple.",0);
		add_dialog_str(1,"Suddenly, the air becomes dry as a bone, and you hear a sound like a whip crack.",0);
		run_dialog(1);
		put_object_on_space(21,8,4);
		put_object_on_space(28,8,4);
		put_object_on_space(24,10,4);
		put_object_on_space(25,10,4);
		put_sparkles_on_space(21,8,11,6);
		put_sparkles_on_space(28,8,11,6);
		put_sparkles_on_space(24,10,11,6);
		put_sparkles_on_space(25,10,11,6);
		run_animation_sound(51);
		}

	if(get_flag(1,25) == 1) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"As you descend into the rough center of the ship, you find what amounts to a storeroom, complete with hungry-looking spiders.",0);
		run_dialog(1);
		set_flag(1,25,2);
		}

break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE

	if((get_flag(1,19) == 1) && (countera > 0)) {
		countera = countera - 1;
		}
	if((get_flag(1,19) == 1) && (countera == 0)) {
		reset_dialog();
		add_dialog_str(0,"A strange mist begins to coalesce on the floor.",0);
		run_dialog(1);
		put_effect_on_space(24,5,5,5,0);
		run_animation_sound(162); //weird chime thing
		pause(5);
		set_flag(1,19,2);
		activate_hidden_group(10);
		}

	if((get_flag(1,1) == 0) && (get_flag(1,2) != 1))
		set_state_continue(20);
	if((get_flag(1,1) > 0) && (get_flag(1,2) != 1))
		inc_flag(1,1,-1);

	//message from Samantha post-Captain
	if(get_flag(1,23) == 1) {
		if(character_in_party(14) != -1) {
			reset_dialog();
			add_dialog_str(0,"Captain Samantha turns to you, a wide grin on her face.",0);
			add_dialog_str(1,"_We did it! Now we'll finally be able to get back to the Empire!_",15);
			run_dialog(1);
			}
		else
			message_dialog("With the captain dead, you only wish that Captain Samantha could've helped see it through to the end.","");
		set_flag(1,23,2);
		}

	//Checking for the lead pc each turn...
	pc_counter = 0;
	while(char_ok(pc_counter) == 0) {
		pc_counter = pc_counter + 1;
		}
	lead_char = pc_counter;
break;

//level transitions
beginstate 10; //west prison stairs
	if(is_combat() == 1) {
		print_str_color("Can't climb stairs during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	choice = run_dialog(0);
	if(choice == 2)
		move_to_new_town(0,21,25);
	else
		block_entry(1);
break;
beginstate 11; //east prison stairs
	if(is_combat() == 1) {
		print_str_color("Can't climb stairs during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	choice = run_dialog(0);
	if(choice == 2)
		move_to_new_town(0,28,25);
	else
		block_entry(1);
break;
beginstate 12; //cargo hold stairs, down
	if(is_combat() == 1) {
		print_str_color("Can't climb stairs during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	choice = run_dialog(0);
	if(choice == 2) {
		if(get_flag(0,27) == 0)
			set_flag(0,27,1);
		move_to_new_town(0,24,20);
		}
	else
		block_entry(1);
break;
beginstate 13; //captain's storage ladder - trapdoor
	if(is_combat() == 1) {
		print_str_color("Can't use trapdoors during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(9); //trapdoor dialog
	choice = run_dialog(0);
	if(choice == 2) {
		if(get_flag(1,26) == 0)
			set_flag(1,26,1);
		move_to_new_town(0,28,6);
		}
	else
		block_entry(1);
break;
beginstate 14; //cargo hold stairs, up
	if(is_combat() == 1) {
		print_str_color("Can't climb stairs during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	choice = run_dialog(0);
	if(choice == 2)
		move_to_new_town(2,25,32);
	else
		block_entry(1);
break;
beginstate 15; //stairs to deck
	if(is_combat() == 1) {
		print_str_color("Can't climb stairs during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	choice = run_dialog(0);
	if(choice == 2)
		move_to_new_town(2,24,33);
	else
		block_entry(1);
break;
beginstate 16; //ladder to deck, W
	if(is_combat() == 1) {
		print_str_color("Can't climb ladders during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	add_dialog_str(0,"There's a ladder here.",0);
	add_dialog_choice(1,"Climb the ladder.");
	choice = run_dialog(0);
	if(choice == 2) {
		if(has_special_item(2) == 0) {
			message_dialog("When you reach the top, you run up against a locked trapdoor. You don't have the key, and you don't like the idea of trying to pick the lock upside down at the top of a tall ladder.","");
			block_entry(1);
			end();
			}
		else {
			move_to_new_town(2,18,16);
			print_str_color("You have the key which unlocks the trapdoor at the top of this ladder.",2);
			}
		}
	else
		block_entry(1);
break;
beginstate 17; //ladder to deck, E
	if(is_combat() == 1) {
		print_str_color("Can't climb ladders during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	add_dialog_str(0,"There's a ladder here.",0);
	add_dialog_choice(1,"Climb the ladder.");
	choice = run_dialog(0);
	if(choice == 2) {
		if(has_special_item(2) == 0) {
			message_dialog("When you reach the top, you run up against a locked trapdoor. You don't have the key, and you don't like the idea of trying to pick the lock upside down at the top of a tall ladder.","");
			block_entry(1);
			end();
			}
		else {
			move_to_new_town(2,31,16);
			print_str_color("You have the key which unlocks the trapdoor at the top of this ladder.",2);
			}
		}
	else
		block_entry(1);
break;
beginstate 18; //stairs out of captain's room
	if(get_flag(1,20) == 0)
		end();
	if(is_combat() == 1) {
		print_str_color("Can't climb stairs during combat.",1);
		block_entry(1);
		end();
		}
	reset_dialog_preset_options(5); //stairs dialog
	choice = run_dialog(0);
	if(choice == 2)
		move_to_new_town(2,24,12);
	else
		block_entry(1);
break;

beginstate 20; //being discovered
	pause(5);

	//where's the ghost
	if(char_loc_x(lead_char) <= 24)
		ghostlocx = 18;
	else
		ghostlocx = 31;

	message_dialog("The corridor suddenly feels colder. For a second, you think about jumping back down the stairs, but you're not fast enough...","");
	//activate ghost and make it patrol
	activate_hidden_group(1);
	relocate_character(15,ghostlocx,24);
	force_instant_terrain_redraw();
	pause(4);
	relocate_character(15,ghostlocx,23);
	force_instant_terrain_redraw();
	pause(4);
	relocate_character(15,ghostlocx,22);
	force_instant_terrain_redraw();
	pause(4);
	relocate_character(15,ghostlocx,21);
	force_instant_terrain_redraw();
	pause(4);
	relocate_character(15,ghostlocx,20);
	force_instant_terrain_redraw();
	pause(3);
	//ghost sees you
	if(ghostlocx == 18)
		set_character_facing(15,6);
	else
		set_character_facing(15,2);
	force_instant_terrain_redraw();

	pause(4);

	message_dialog("The ghost stares straight through you for a second, as if it isn't used to looking at the living. Then it shouts something you can't understand.","Something whips the curtains aside, revealing a room full of ghosts. It seems you've been discovered.");

	//reveal the ghosts
	set_terrain(25,18,0);
	set_terrain(26,18,0);
	set_terrain(27,18,0);
	force_instant_terrain_redraw();

	set_flag(1,2,1);
break;

beginstate 25; //enter the mess hall
	if(get_flag(1,3) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"This looks surprisingly like a mess hall, something that you wouldn't expect to find on a ship with a dead crew.",0);
	if(num_chars_in_group(1) > 0)
		add_dialog_str(1,"Of course, it's even stranger that there are still some crew members in here, despite the fact that their plates and flagons are empty.",0);
	else
		add_dialog_str(1,"Of course, it's even stranger that there were still some crew members in here, despite the fact that their plates and flagons were clearly empty.",0);
	add_dialog_str(2,"It seems that some habits die hard.",0);
	run_dialog(1);
	set_flag(1,3,1);
break;

beginstate 26; //cook
	if(get_flag(1,4) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"Like almost every mess hall you've ever been in, this one has a kitchen. And a cook. And also like nearly every mess hall you've been in, the cook is not happy with your intrusion.",0);
	run_dialog(1);
	set_flag(1,4,1);
break;

beginstate 27; //soup, triggered from specobj
	if(get_flag(1,16) == 1) {
		message_dialog("Sadly, none of the nourishing ghost soup remains.","You should have saved some for later.");
		end();
		}
	reset_dialog();
	add_dialog_str(0,"You're not exactly sure why, but it seems that the chef has been working on a rather large pot of soup. It has a pale, milky color that you normally wouldn't associate with soup.",0);
	if(get_skill_total(13) > 6) //arcane lore of 6+
		add_dialog_str(1,"Also, it seems to be radiating some sort of magical energy. It doesn't feel malevolent, but it's always hard to tell with soup.",0);
	add_dialog_str(2,"There's a ladle chained to the pot. If you wanted, you could sample the soup.",0);
	add_dialog_choice(0,"Leave it alone.");
	add_dialog_choice(1,"Try some soup.");
	choice = run_dialog(1);
	if(choice == 1)
		end();
	else {
		pc_counter = 0;
		while(pc_counter < 6) {
			if(char_ok(pc_counter) == 1) {
				heal_char(pc_counter, get_max_health(pc_counter));
				restore_energy_char(pc_counter,50);
				}
			pc_counter = pc_counter + 1;
			}
		reset_dialog();
		add_dialog_str(0,"You try a ladleful of soup, and wait. At first, it doesn't taste like much of anything, and later, it still doesn't. But then a strange warm feeling starts spreading through your body.",0);
		add_dialog_str(1,"A couple more servings later, and you feel completely refreshed. Then you realize that you've managed to drain the entire pot of soup.",0);
		add_dialog_str(2,"It's a shame, because you don't even really know what it tasted like.",0);
		set_terrain(20,13,248);
		set_flag(1,16,1);
		run_dialog(1);
		}
break;

beginstate 30; //further down the hall
	if(get_flag(1,8) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"As you move along the ship, you start to wonder how good your chances are of actually getting off the ship. Even if you clear out every single monster, you can't possibly hope to guide the ship somewhere safe.",0);
	add_dialog_str(1,"You don't even know where you are, much less which direction you should go in. Of course, if that means you're stuck on the ship, you might as well keep on evicting the original owners.",0);
	run_dialog(1);
	set_flag(1,8,1);
break;

beginstate 31; //skeleton encounter, west side
	if(get_flag(1,5) == 1)
		end();
	print_str("A skeleton climbs down from its beam!");
	set_terrain(19,30,279);
	activate_hidden_group(3);
	force_instant_terrain_redraw();
	set_flag(1,5,1);
break;
beginstate 32; //skeleton encounter, southeast side
	if(get_flag(1,6) == 1)
		end();
	print_str("A skeleton climbs down from its beam!");
	set_terrain(27,34,279);
	activate_hidden_group(4);
	force_instant_terrain_redraw();
	set_flag(1,6,1);
break;
beginstate 33; //skeleton encounter, northeast side
	if(get_flag(1,7) == 1)
		end();
	print_str("A skeleton climbs down from its beam!");
	set_terrain(31,25,279);
	activate_hidden_group(2);
	force_instant_terrain_redraw();
	set_flag(1,7,1);
break;

beginstate 39; //dead corpse in guard room
	message_dialog("Unlike seemingly every other corpse on this ship, this one seems completely inert, and thus it is very unlikely to attack you.","Someone must've beaten the un-life out of it.");
break;

beginstate 40; //guards, guards!
	if(get_flag(1,11) > 0)
		end();
	reset_dialog();
	add_dialog_str(0,"By now, you're certain that the ghosts know you're here, so you try to move as quietly as possible. Then, as you round the corner, you find another set of barriers. Before you can stop yourself, you curse loudly.",0);
	add_dialog_str(1,"After a short, tense pause, the doors at the end of the hall swing open, revealing several well-armed ghosts. You frantically run through the gestures Captain Samantha taught you, but nothing happens.",0);
	add_dialog_str(2,"A tiny, annoying part of your brain wonders if an incorporeal sword will still hurt.",0);
	run_dialog(1);

	//open doors
	flip_terrain(24,39);
	flip_terrain(25,39);
	play_sound(-58); //door open
	set_flag(1,9,1); //SDF to unlock doors
	force_instant_terrain_redraw();

	set_flag(1,11,1);
break;

beginstate 45; //nw crew cabin encounter
	if(get_flag(1,12) == 1)
		end();
	if(get_ran(1,0,1 + get_skill_total(18)) > 0)
		end();
	message_dialog("With a painful creak, a nearby door opens.","");
	flip_terrain(20,33);
	set_flag(1,12,1);
	play_sound(-58);
	force_instant_terrain_redraw();
break;
beginstate 46; //sw crew cabin encounter
	if(get_flag(1,13) == 1)
		end();
	if(get_ran(1,0,1 + get_skill_total(18)) > 0)
		end();
	message_dialog("With a painful creak, a nearby door opens.","");
	flip_terrain(21,37);
	set_flag(1,13,1);
	play_sound(-58);
	force_instant_terrain_redraw();
break;
beginstate 47; //se crew cabin encounter
	if(get_flag(1,14) == 1)
		end();
	if(get_ran(1,0,1 + get_skill_total(18)) > 0)
		end();
	message_dialog("With a painful creak, a nearby door opens.","");
	flip_terrain(27,37);
	set_flag(1,14,1);
	play_sound(-58);
	force_instant_terrain_redraw();
break;
beginstate 48; //ne crew cabin encounter
	if(get_flag(1,15) == 1)
		end();
	if(get_ran(1,0,1 + get_skill_total(18)) > 0)
		end();
	message_dialog("With a painful creak, a nearby door opens.","");
	flip_terrain(28,33);
	set_flag(1,15,1);
	play_sound(-58);
	force_instant_terrain_redraw();
break;

beginstate 60; //scratched out runes heading down to cargo
	if(get_flag(1,21) == 1)
		end();
	message_dialog("Lucky for you, these runes have been scratched out, probably by the spiders. You won't have to worry about barriers here.","");
	set_flag(1,21,1);
break;

beginstate 65; //captain's quarters
	if(get_flag(1,22) == 1)
		end();
	message_dialog("Now that the barriers have fallen, you can get a good look at the captain's quarters. As you would expect, the room is in an advanced state of disrepair, and the smell of mildew hangs heavy in the air.","");
	set_flag(1,22,1);
break;

beginstate 70; //the captain's chest & key
	if(get_flag(1,24) == 1)
		end();
	message_dialog("When you open the chest, you find a key hooked to the underside of the lid. You take it.","");
	change_spec_item(2,1);
	set_flag(1,24,1);
break;

beginstate 75; //the captain's skeleton (specobj trigger)
	message_dialog("This skeleton is completely clean of flesh, and bears no distinguishing marks. You don't even see any obvious signs of foul play.","Odd.");
break;