begintownscript;

variables;
short choice;

body;

beginstate 0; //INIT_STATE
	enable_add_chars(0); //no new people on this boat
	if(get_flag(0,0) == 0) {
		force_view_center(44,3); //look at the darkness!
		force_instant_terrain_redraw();
		reset_dialog();
		add_dialog_str(0,"As you near the ship, you remember why you've never heard of adventurers swimming. Your gear is doing a good job of dragging you down, and it's difficult just to tread water.",0);
		add_dialog_str(1,"When you reach the side of the boat and don't find a rope, you start to panic. Just when you think you're going to lose consciousness, you feel someone or something lifting you out of the water.",0);
		add_dialog_str(2,"Relieved, you pass out.",0);
		run_dialog(1);

		pause(15);

		force_view_center(22,41);
		set_character_pose(1000,14); //party is laying down
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"You finally come to, soaking wet and plastered to the floor of a dark and moldy cabin. It's times like this that really make you glad to be an adventurer.",0);
		add_dialog_str(1,"Looking out your cabin's window, all you can see is a dark and stormy ocean. The ship is moving somewhere, judging from the amount of sea-spray coming in through the window.",0);
		add_dialog_str(2,"So far, you're wishing that you had stayed on the island. At least it was warm there.",0);
		run_dialog(1);
		set_flag(0,0,1);
		pause(5);
		set_character_pose(1000,0); //party stands up
		set_flag(0,11,4); //set up timer for Chase's door
		}
	//set up Capt. Samantha if not in party
	if(character_in_party(14) == -1) {
		activate_hidden_group(6);
		set_char_dialogue_pic(14,514,0);
		set_name(14,"Captain Samantha");
		}
	//remove corpses from central chamber if encounter has already
	// been triggered
	if(get_flag(0,3) == 1) {
		set_terrain(29,30,0);
		set_terrain(28,33,279);
		set_terrain(26,31,279);
		force_instant_terrain_redraw();
		}
	//remove corpses from W torture chamber if encounter has already
	// been triggered
	if(get_flag(0,4) == 1) {
		set_terrain(21,32,0);
		set_terrain(20,31,0);
		set_terrain(21,31,0);
		set_terrain(20,33,0);
		force_instant_terrain_redraw();
		}
	//same thing for NW zombie chamber
	if(get_flag(0,14) == 1) {
		set_terrain(20,37,0);
		force_instant_terrain_redraw();
		}
	//same thing for SE zombie chamber
	if(get_flag(0,15) == 1) {
		set_terrain(28,39,0);
		force_instant_terrain_redraw();
		}

	//drunk ghost encounter (center cargo chamber)
	if(get_flag(0,27) == 1) {
		if(get_flag(0,28) == 0) {
			reset_dialog();
			add_dialog_str(0,"As you descend to what looks like the lowest cargo hold, you hear something drunkenly snicker off in the darkness. You can't see anything.",0);
			add_dialog_str(1,"This is not good.",0);
			run_dialog(1);
			set_flag(0,28,1);
			}
		activate_hidden_group(15);
		add_range_to_group(21,26,2);
		set_flag(0,27,1);
		}

	//dialog for captain's storage
	if(get_flag(1,26) == 1) {
		force_instant_terrain_redraw();
		pause(2);
		reset_dialog();
		add_dialog_str(0,"You enter what appears to be a storeroom, one which hasn't been opened in a long time. As with everywhere else on the ship, the smell of mildew is overpowering.",0);
		run_dialog(1);
		set_flag(1,26,2);
		}

break;

beginstate 1; //EXIT_STATE
	//make sure both brig barriers fade, so there's no awkward
	// entrance with the party walking 'through' one
	if(get_flag(0,16) == 0)
		set_flag(0,16,1);
	if(get_flag(0,17) == 0)
		set_flag(0,17,1);
	//if the drunken invisible ghost fight has been triggered,
	// un-trigger it.
	if(get_flag(0,27) == 1)
		set_flag(0,27,0);

break;

beginstate 2; //START_STATE
	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
	
	//check to see if Chase's door opens
	// only happens if both undead encounters have been sprung
	if((get_flag(0,3) == 1) && (get_flag(0,4) == 1) && (get_flag(0,10) == 0)) {
		if(num_chars_in_group(1) == 0) {
			if(get_flag(0,11) > 0)
				inc_flag(0,11,-1);
			else {
			set_flag(0,10,1); //SDF for Chase's door
			play_sound(-58); //door opening
			flip_terrain(25,28);
			force_instant_terrain_redraw();
			reset_dialog();
			if(party_can_see_loc(25,28) == 0)
				add_dialog_str(0,"You hear a door slowly creak open, but you can't tell where the sound is coming from.",0);
			else
				add_dialog_str(0,"Out of the corner of your eye, you see a door open. It definitely wasn't open before.",0);
			add_dialog_str(1,"Then, you hear a voice shouting in a foreign tongue. You don't recognize it, but it sounds like swearing.",0);
			run_dialog(0);
				}
			}
		}

	//check to see if the drunken ghosts have disappeared
	if((get_flag(0,27) == 1) && (get_flag(0,28) == 1)) {
		if(num_chars_in_group(2) == 0) {
			reset_dialog();
			add_dialog_str(0,"Finally, the cargo hold falls silent. All you can hear now is the occasional creaking of timbers.",0);
			run_dialog(1);
			set_flag(0,27,2);
			award_party_xp(200,5);
			}
		else {
			put_sparkles_on_char(random_group_member(2),4,1);
			run_animation();
			print_str("You see something glimmer in the darkness, and then hear a hiccup.");
			}
		}
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(1,21,21);
	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(1,28,21);
	else
		block_entry(1);
break;
beginstate 12; //cargo hold 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(1,24,24);
	else
		block_entry(1);
break;
beginstate 13; //captain's storage ladder
	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)
		move_to_new_town(1,30,6);
	else
		block_entry(1);
break;


beginstate 20; //step out into the hallway
	if(get_flag(0,1) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"Ah, that's not good. Broken bones and bloodstains don't usually signal friendly intentions.",0);
	run_dialog(1);
	set_flag(0,1,1);
break;
beginstate 21; //torture chamber, yay
	if(get_flag(0,2) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"As you carefully walk down the hall, the only sounds you hear are the creaking of the ship's timbers, and the waves crashing into the ship.",0);
	add_dialog_str(1,"And then you see the bodies. Someone has clearly been busy down here, judging from the condition of the corpses. They look vaguely humanoid, but it's hard to tell.",0);
	run_dialog(1);
	set_flag(0,2,1);
break;
beginstate 22; //torture chamber encounter
	if(get_flag(0,3) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"Very slowly, the battered and bloodied corpses in the chamber begin to get up and shamble around. Or, in the case of the hanging skeletons, carefully detach themselves from their beams.",0);
	add_dialog_str(1,"It's a gristly sight.",0);
	run_dialog(1);
	set_terrain(29,30,0);
	set_terrain(28,33,279);
	set_terrain(26,31,279);
	activate_hidden_group(1);
	add_range_to_group(7,9,1);
	force_instant_terrain_redraw();
	set_flag(0,3,1);
break;

beginstate 25; //side chamber encounter
	if(get_flag(0,4) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"The purpose of this room seems fairly clear, judging from the frightening array of tools scattered around the floor.",0);
	add_dialog_str(1,"You're beginning to understand the fate your gracious hosts probably had in mind for you when they pulled you aboard. Hopefully, you can keep that from happening.",0); 
	add_dialog_str(2,"As before, the mangled corpses in this chamber object to your intrusion. Strongly.",0);
	run_dialog(1);
	set_terrain(21,32,0);
	set_terrain(20,31,0);
	set_terrain(21,31,0);
	set_terrain(20,33,0);
	activate_hidden_group(2);
	add_range_to_group(10,12,1);
	add_char_to_group(18,1);
	force_instant_terrain_redraw();
	set_flag(0,4,1);
break;

beginstate 30; //blocked by barriers, west side
	if((character_in_party(14) != -1) || (get_flag(0,13) == 2))
		set_state_continue(31);
	if(get_flag(0,13) > 0)
		end();
	reset_dialog();
	add_dialog_str(0,"A magical barrier blocks the stairs here. Someone must have gone to great lengths to confine you to the lower levels of the ship.",0);
	run_dialog(1);
	set_flag(0,13,1);
break;
beginstate 31; //dealing with barriers (from state 30)
	if(is_object_on_space(21,24,4) == 0)
		end();
	else {
		reset_dialog();
		if(get_flag(0,13) == 0) {
			add_dialog_str(0,"A magical barrier blocks the stairs here. Someone must have gone to great lengths to confine you to the lower levels of the ship.",0);
			set_flag(0,13,1);
			}
		if((character_in_party(14) != 1) && (get_flag(0,13) != 2)) {
			add_dialog_str(1,"Captain Samantha approaches the barrier and starts waving her arms around in a way that makes you worry for her mental well-being.",0);
			add_dialog_str(2,"_When I was brought down here, I remember one of the ghosts making these gestures. It made the barrier disappear._",15);
			add_dialog_str(3,"Fortunately, it works. You have her repeat the ridiculous gestures a few times so that you can remember them.",0);
			set_flag(0,13,2);
			}
		else if(get_flag(0,13) == 2) {
			add_dialog_str(1,"You repeat the ridiculous gestures that Captain Samantha taught you, and the barrier fades away.",0);
			}
		run_dialog(1);
		set_flag(0,16,1); //runebarrier trigger
		put_effect_on_space(21,24,8,4,1);
		run_animation_sound(112); //smite spell
		put_object_on_space(21,24,-1); //remove the barrier
		}
break;

beginstate 32; //blocked by barriers, east side
	if((character_in_party(14) != -1) || (get_flag(0,13) == 2))
		set_state_continue(33);
	if(get_flag(0,13) > 0)
		end();
	reset_dialog();
	add_dialog_str(0,"A magical barrier blocks the stairs here. Someone must have gone to great lengths to confine you to the lower levels of the ship.",0);
	run_dialog(1);
	set_flag(0,13,1);
break;
beginstate 33; //dealing with barriers (from state 32)
	if(is_object_on_space(28,24,4) == 0)
		end();
	else {
		reset_dialog();
		if(get_flag(0,13) == 0) {
			add_dialog_str(0,"A magical barrier blocks the stairs here. Someone must have gone to great lengths to confine you to the lower levels of the ship.",0);
			set_flag(0,13,1);
			}
		if((character_in_party(14) != 1) && (get_flag(0,13) != 2)) {
			add_dialog_str(1,"Captain Samantha approaches the barrier and starts waving her arms around in a way that makes you worry for her mental well-being.",0);
			add_dialog_str(2,"_When I was brought down here, I remember one of the ghosts making these gestures. It made the barrier disappear._",15);
			add_dialog_str(3,"Fortunately, it works. You have her repeat the ridiculous gestures a few times so that you can remember them.",0);
			set_flag(0,13,2);
			}
		else if(get_flag(0,13) == 2) {
			add_dialog_str(1,"Fortunately, you remember the ridiculous motions Captain Samantha taught you. You run through them again, and the barrier fades away.",0);
			}
		run_dialog(1);
		set_flag(0,17,1); //runebarrier trigger
		put_effect_on_space(28,24,8,4,1);
		run_animation_sound(112); //smite spell
		put_object_on_space(28,24,-1); //remove the barrier
		}
break;


beginstate 35; //NW zombie encounter
	if(get_flag(0,14) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"This room contains a corpse, and little else. Probably another victim of your hosts.",0);
	add_dialog_str(1,"The corpse then stands up and looks at you hungrily.",0);
	run_dialog(1);
	activate_hidden_group(4);
	set_terrain(20,37,0);
	force_instant_terrain_redraw();
	set_flag(0,14,1);
break;
beginstate 40; //SE zombie encounter
	if(get_flag(0,15) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"This room contains a corpse, and little else. Probably another victim of your hosts.",0);
	add_dialog_str(1,"The corpse then stands up and looks at you hungrily.",0);
	run_dialog(1);
	activate_hidden_group(3);
	set_terrain(28,39,0);
	force_instant_terrain_redraw();
	set_flag(0,15,1);
break;

beginstate 49; //sign for storeroom
	if(get_flag(0,23) == 0)
		set_flag(0,23,1);
break;

beginstate 50; //storeroom
	if(get_flag(0,23) == 2)
		end();
	reset_dialog();
	if(get_flag(0,23) == 1)
		add_dialog_str(0,"You enter the storeroom for the ship, expecting to find food, supplies, something useful. Instead, you find rats. A lot of rats.",0);
	else
		add_dialog_str(0,"This room looks like it once served as a storeroom for the denizens of the ship. Now, it makes a nice home for rats.",0);
	add_dialog_str(1,"They look hungry, and you're probably the first fresh meat they've seen in a long time.",0);
	run_dialog(1);
	set_flag(0,23,2);
break;

beginstate 55; //corpse in Chase's room
	message_dialog("Unlike every other corpse on this ship, this one seems completely inert, and thus it is very unlikely to attack you.","The ghost in charge must've even beaten the un-life out of it.");
break;

beginstate 60; //skeleton in Chase's room
	message_dialog("Whoever this used to be, they probably got trapped in here by the jammed door.","Looks like they've been here for a while.");
break;

beginstate 70; //un-dispellable barriers (door trigger)
	if(get_flag(0,29) == 1)
		end();
	if(get_flag(0,13) < 2)
		end();
	force_instant_terrain_redraw();
	pause(3);
	message_dialog("Faced with another barrier, you try the ridiculous gestures that Captain Samantha showed you. However, all you succeed in doing is embarrassing yourself.","It seems that not all the barriers on this ship will be that easy to deal with.");
	set_flag(0,29,1);
break;