begintownscript;

variables;
short choice, door_win, staygo;
short pc_counter = 0;
short spawn_countdown = 2;

body;

beginstate 0; //INIT_STATE

	enable_add_chars(0); //no new chars on the boat

	if(get_flag(2,0) == 0) {
		relocate_character(0,24,32);
		relocate_character(1,24,33);
		relocate_character(2,25,33);
		relocate_character(3,25,32);
		relocate_character(4,26,32);
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"You finally emerge onto the deck of the ship, and you are greeted by flashes of lightning and thunder. The ship is clearly in the middle of a very strong storm, though no rain is falling yet.",0);
		add_dialog_str(1,"Between the thunder and the sea-swells, it's getting hard to hear. This doesn't stop you, however, from noticing the deckhands shouting incoherently.",0);
		add_dialog_str(2,"As the ghosts draw translucent daggers, you notice the bones that seem to litter the deck. This is not a good thing.",0);
		run_dialog(1);
		set_flag(2,0,1);
		}

	//spawn helmsman, if necessary
	if((get_flag(2,4) == 1) && (get_flag(1,23) == 0)) {
		activate_hidden_group(2);
		force_instant_terrain_redraw();
		set_char_dialogue_pic(6,515,0);
		}

	//fix cargo door, if open
	if(get_flag(2,1) == 1) {
		set_floor(23,30,192);
		set_floor(23,31,192);
		set_floor(26,30,192);
		set_floor(26,31,192);
		set_floor(24,30,71);
		set_floor(25,30,71);
		set_height(24,30,8);
		set_height(24,31,8);
		set_height(25,30,8);
		set_height(25,31,8);
		set_terrain(24,31,295);
		set_terrain(25,31,295);
		force_instant_terrain_redraw();
		}
break;

beginstate 1; //EXIT_STATE

	//de-activate the horde if activated
	if(get_flag(2,2) == 1)
		set_flag(2,2,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;

	//maintain skeleton horde
	if((get_flag(2,2) == 1) && (num_chars_in_group(1) < 11)) {
		if(get_flag(2,3) == 0) {
			message_dialog("As you reduce some skeletons to their constituent bones, you begin to think this will be easy. However, a rattling sound soon catches your attention.","The bones are quickly reassembling themselves into full skeletons.");
			set_flag(2,3,1);
			}
		if(num_chars_in_group(1) < 10)
			print_str("More skeletons rise up from the deck!");
		else
			print_str("Another skeleton rises up from the deck!");
		play_sound(-40); //killing an undead (but in reverse!)
		activate_hidden_group(1);
		force_instant_terrain_redraw();
		add_range_to_group(10,23,1); //all the skeletons
		}
	//check and see if the skeletons should disappear
	if(get_flag(2,2) == 1) {
		staygo = 1;
		pc_counter = 0;
		while((pc_counter < 6) && (staygo == 1)) {
			if((char_ok(pc_counter) == 1) && ((char_loc_y(pc_counter) <= 26) && (char_loc_y(pc_counter) > 13)))
				staygo = 0;
			pc_counter = pc_counter + 1;
			}
		if(staygo == 1) {
			message_dialog("Now that you've gotten away from the piles of bones, the skeletons fall apart and clatter to the deck harmlessly.","");
			play_sound(-40); //undead dying
			set_character_pose(1001,11);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(1001,12);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(1001,13);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(1001,14);
			force_instant_terrain_redraw();
			pause(1);
			erase_char(1001);
			force_instant_terrain_redraw();
			set_flag(2,2,0);
			}
		}
	//check and see if they're cheating
	if((get_flag(2,2) == 0) && (get_flag(2,5) == 1)) {
		staygo = 1;
		pc_counter = 0;
		while((pc_counter < 6) && (staygo == 1)) {
			if((char_ok(pc_counter) == 1) && ((char_loc_y(pc_counter) > 26) || (char_loc_y(pc_counter) < 13)))
				staygo = 0;
			pc_counter = pc_counter + 1;
			}
		if(staygo == 0) {
			message_dialog("You try to sneak through the field of bones, but the bones soon reassemble themselves into full skeletons.","");
			play_sound(-40); //undead dying
			activate_hidden_group(1);
			add_range_to_group(10,23,1); //all the skeletons
			force_instant_terrain_redraw();
			set_flag(2,2,1);
			}
		}

	//counter to re-spawn Helmsman after killing the captain
	if(get_flag(2,8) == 1) {
		message_dialog("Without even acknowledging you, the robed figure simply disappears from the deck of the ship, making you wonder if it was ever really here.","");
		erase_char(13);
		force_instant_terrain_redraw();
		set_flag(2,8,2);
		}
	if((get_flag(1,23) > 0) && (get_flag(2,8) == 0)) {
		if(spawn_countdown == 0) {
			//spawn the helmsman
			reset_dialog();
			add_dialog_str(0,"It's not as if you really know, what with the apparent language barrier and all, but you feel as if you just took down the spirit-in-charge on this boat.",0);
			add_dialog_str(1,"And now, as you stumble around on the deck, you can't help but think that you might yet get home.",0);
			add_dialog_str(2,"No sooner does this thought cross your mind than a gale-force wind rips across the deck, scattering loose bones everywhere.",0);
			run_dialog(1);
			put_boom_on_space(25,17,3,0);
			set_flag(2,8,1);
			run_animation_sound(162); //weird chime
			activate_hidden_group(3);
			set_char_dialogue_pic(13,515,0);
			force_instant_terrain_redraw();
			}
		if(spawn_countdown > 0)
			spawn_countdown = spawn_countdown - 1;
		}
break;

//level transitions

beginstate 10; //below-deck stairs, center
	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,25,36);
	else
		block_entry(1);
break;
beginstate 11; //below-deck trapdoor, W
	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(has_special_item(2) == 0) {
			message_dialog("You try to force it open, but it's securely-locked. Not only do you not have the key, but it seems that the lock is on the other side of the door.","");
			block_entry(1);
			end();
			}
		else {
			move_to_new_town(1,17,17);
			print_str_color("You have the key which unlocks this trapdoor.",2);
			}
		}
	else
		block_entry(1);
break;
beginstate 12; //below-deck trapdoor, E
	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(has_special_item(2) == 0) {
			message_dialog("You try to force it open, but it's securely-locked. Not only do you not have the key, but it seems that the lock is on the other side of the door.","");
			block_entry(1);
			end();
			}
		else {
			move_to_new_town(1,32,17);
			print_str_color("You have the key which unlocks this trapdoor.",2);
			}
		}
	else
		block_entry(1);
break;
beginstate 13; //captain's quarters
	if(is_combat() == 1) {
		print_str_color("Can't use these doors during combat.",1);
		block_entry(1);
		end();
		}
	if(get_flag(2,7) == 0) {
		reset_dialog();
		add_dialog_str(0,"When you approach this door, it discharges a small bolt of electricity that strikes you. It stings a little, but it's not that bad.",0);
		add_dialog_str(1,"On a hunch, you kick a loose bone over towards the door. When it hits the door, it is instantly reduced to a small pile of black dust.",0);
		add_dialog_str(2,"Might not be a good idea to use that door yet.",0);
		run_dialog(1);
		end();
		}
	reset_dialog();
	if(get_flag(2,7) == 1) {
		add_dialog_str(0,"Now that the cloaked figure has unlocked this door, you feel much safer using it. However, if the figure was telling the truth, then you'll have quite a fight ahead of you.",0);
		set_flag(2,7,2);
		}
	add_dialog_str(1,"Do you enter the door?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter.");
	choice = run_dialog(0);
	if(choice == 2) {
		if(get_flag(1,19) == 0)
			set_flag(1,19,1);
		move_to_new_town(1,24,9);
		}
	else
		block_entry(1);
break;
beginstate 14; //cargo hold stairs
	//this is basically a dummed-down shortened door.txt...
	if(get_flag(2,1) == 0) {
		reset_dialog();
		add_dialog_str(0,"A set of heavy wooden doors is set into the deck here. You try opening them, but they're locked.",0);
		add_dialog_str(1,"You could try forcing them open, or you could try picking the lock.",0);
		add_dialog_choice(0,"Leave them alone.");
		add_dialog_choice(1,"Try to force them open.");
		add_dialog_choice(2,"Try to pick the lock.");
		choice = run_dialog(1);
		if(choice == 1) {
			block_entry(1);
			end();
			}
		else if(choice == 2) {
			block_entry(1);
			door_win = 0;
			play_sound(-73); //door bash (and fire?)
			damage_char(char_with_highest_skill(0),5,4);
			if(get_highest_skill(0) >= 4)
				door_win = 1;
			else	
				print_str("You fail to break the doors in.");
			}
		else if(choice == 3) {
			block_entry(1);
			door_win = 0;
			if(get_highest_skill(15) > 4) { 
				play_sound(-9); //unlocking door sound
				door_win = 1;
				award_party_xp(50,5);
				}
			else
				print_str("You fail to pick the lock.");
			}
		if(door_win == 1) {
			play_sound(-58); //door opening
			message_dialog("You manage to get the doors open, revealing a set of stairs leading back down into the ship.","");
			set_floor(23,30,192);
			set_floor(23,31,192);
			set_floor(26,30,192);
			set_floor(26,31,192);
			set_floor(24,30,71);
			set_floor(25,30,71);
			set_height(24,30,8);
			set_height(24,31,8);
			set_height(25,30,8);
			set_height(25,31,8);
			set_terrain(24,31,295);
			set_terrain(25,31,295);
			force_instant_terrain_redraw();
			set_flag(2,1,1);
			}
		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) {
		if(get_flag(1,25) == 0)
			set_flag(1,25,1);
		move_to_new_town(1,24,29);
		}
	else
		block_entry(1);
break;


beginstate 21; //block entry around front mast
	block_entry(1);
break;


beginstate 30; //skeletons aplenty!
	if((get_flag(2,2) == 2) || (get_flag(1,23) >= 1))
		end();
	else if((get_flag(2,2) == 0) && (get_flag(2,5) == 0)) {
		//skeletons appear for the first time
		reset_dialog();
		add_dialog_str(0,"A chill wind picks up as you make your way across the deck, cutting straight through your armor. The wind is strong enough that it's able to shuffle loose bones around the deck.",0);
		add_dialog_str(1,"Then, for just a minute, the wind dies down, and the bones continue moving.",0);
		run_dialog(1);
		pause(4);
		play_sound(-40); //undead dying
		activate_hidden_group(1);
		add_range_to_group(10,23,1); //all the skeletons
		set_character_pose(1001,14);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(1001,13);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(1001,12);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(1001,11);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(1001,0);
		force_instant_terrain_redraw();
		pause(15);

		reset_dialog();
		add_dialog_str(0,"A freshly-assembled group of skeletons begins to shamble towards you, clutching broken weapons and shields. Their bones have been at the same time preserved and warped by the sea salt, but they don't look very sturdy.",0);
		add_dialog_str(1,"Still, they block your path along the ship.",0);
		run_dialog(1);
		set_flag(2,2,1);
		}
	else if(get_flag(2,2) == 0) {
		message_dialog("As you return to the field of bones, the skeletons rise up to greet you.","");
		play_sound(-40); //undead dying
		activate_hidden_group(1);
		add_range_to_group(10,23,1); //all the skeletons
		force_instant_terrain_redraw();
		set_flag(2,2,1);
		}
break;

beginstate 40; //reach the door, find the helmsman
	if(get_flag(2,4) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"As you reach the edge of the bone piles, you encounter a welcome sight: the ship's wheel. At the very least, this means that the ship can be steered back to a safe harbor where you can find a priest.",0);
	add_dialog_str(1,"Then, a tall cloaked figure fades into view ahead of you. It seems that the wheel is already manned.",0);
	add_dialog_str(2,"However, if you want to do anything with the wheel, you should probably work your way out of the field of bones.",0);
	run_dialog(1);
	set_flag(2,4,1);
	put_boom_on_space(24,9,3,0);
	activate_hidden_group(2);
	force_instant_terrain_redraw();
	run_animation_sound(162); //weird chime
	set_char_dialogue_pic(6,515,0);
	
	//if Capt. Samantha is lagging behind, vault her ahead of the group
	if(character_in_party(14) != -1) {
		pause(10);
		reset_dialog();
		add_dialog_str(0,"You hear a grunt from behind you, and suddenly Captain Samantha has vaulted past the skeletons onto the steps ahead of you.",0);
		add_dialog_str(1,"_Hurry, we can't hang about all day!_",15);
		run_dialog(1);
		relocate_character(character_in_party(14),28,11);
		force_instant_terrain_redraw();
		}
break;

beginstate 50; // the ship's wheel (specobj trigger)
	if(char_ok(6) == 1) {
		message_dialog("You try to reach for the wheel, but the robed figure swats your hand away.","");
		end();
		}
	else if(get_flag(2,8) == 2) {
		reset_dialog();
		add_dialog_str(0,"You reach the unoccupied wheel, and start scanning the horizon. As you expected, you have no idea where you are. A storm continues to brew over your head.",0);
		add_dialog_str(1,"With the helmsman gone and the ship's captain defeated, by Empire maritime law this ship is as good as yours. What do you do?",0);
		add_dialog_choice(0,"Leave the wheel alone.");
		add_dialog_choice(1,"Sail away.");
		choice = run_dialog(1);
		if(choice == 1)
			end();
		else {
			reset_dialog();
			add_dialog_str(0,"Since you have no idea where you actually are, you decide that just picking a direction and sticking with it should see you back to somewhere civilized.",0);
			add_dialog_str(1,"But before you can pick a random direction, an image forms in your mind. You see the ship sailing towards a sunlit island, and more importantly, a busy harbor.",0);
			add_dialog_str(2,"When the image fades, you find yourself gripping the wheel and holding the ship to a course. Never one to ignore a positive omen, you maintain that course.",0);
			add_dialog_str(3,"A day later, you leave the storm behind. The day after that, you find yourself sailing into the same dock at Woodsmuir from which you originally departed. Of course, nobody believes your story.",0);
			add_dialog_str(4,"THE END",35);
			run_dialog(1);
			end_scenario(1); //teh end1
			}
		}
break;