begintownscript;

variables;
short choice = 0;
short pc_counter = 0;
short lead_char = 0;

body;

beginstate 0; //INIT_STATE
	set_crime_tolerance(2);

	if(get_flag(4,1) == 0)
		set_terrain(35,47,0);


	if((get_flag(50,1) != 0) && (get_flag(5,15) == 0))
		set_state_continue(80);

	if(get_flag(5,0) == 0) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"The temperature is getting downright icy this high up, and it's getting even harder to think.",0);
		run_dialog(1);
		set_flag(5,0,1);
		}

	



	if((get_flag(50,0) > 0) && (get_flag(50,1) == 0)) {
		if((get_flag(5,3) == 1) && (get_flag(5,1) == 0)) {
			activate_hidden_group(7);
			change_char_health(20,(get_max_health(20) * -1));
			change_char_health(20,get_flag(60,8));
			}
		if((get_flag(5,4) == 1) && (get_flag(5,2) == 0)) {
			activate_hidden_group(6);
			change_char_health(21,(get_max_health(21) * -1));
			change_char_health(21,get_flag(60,5));
			}
		}

break;


beginstate 1; //EXIT_STATE

	if(get_flag(4,3) == 2)
		set_flag(4,3,1);

	if(get_flag(5,6) == 2)
		set_flag(5,6,1);

	if(char_ok(19) == 1)
		set_flag(4,13,get_health(19));

	if(char_ok(6) == 1)
		set_flag(60,0,get_health(6));
	if(char_ok(7) == 1)
		set_flag(60,7,get_health(7));
	if(char_ok(8) == 1)
		set_flag(60,8,get_health(8));
	if(char_ok(9) == 1)
		set_flag(60,6,get_health(9));
	if(char_ok(10) == 1)
		set_flag(60,5,get_health(10));
	if(char_ok(15) == 1)
		set_flag(60,2,get_health(15));
	if(char_ok(16) == 1)
		set_flag(60,4,get_health(16));
	if(char_ok(17) == 1)
		set_flag(60,3,get_health(17));
	if(char_ok(18) == 1)
		set_flag(60,1,get_health(18));

	if(char_ok(21) == 1)
		set_flag(60,5,get_health(21));
	if(char_ok(20) == 1)
		set_flag(60,8,get_health(20));

break;


beginstate 2; //START_STATE
	pc_counter = 0;
	while(char_ok(pc_counter) == 0) {
		pc_counter = pc_counter + 1;
		}
	lead_char = pc_counter;

	if((get_flag(50,0) > 0) && (get_flag(50,1) == 0)) {
		if(get_flag(4,3) == 1) {
			force_instant_terrain_redraw();
			put_boom_on_space(32,39,5,0);
			activate_hidden_group(5);
			set_character_pose(19,14);
			run_animation_sound(52);
			play_sound(-165);
			pause(1);
			set_character_pose(19,13);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(19,12);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(19,11);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(19,0);
			force_instant_terrain_redraw();
			pause(1);
			set_name(19,"Empire Sniper");
			print_str_color("The archer reappears!",2);
			set_flag(4,3,2);
			change_char_health(19,(get_max_health(19) * -1));
			change_char_health(19,get_flag(4,13));
			}
		if((get_flag(5,6) < 3) && (get_flag(5,6) > 0)) {
			if(get_flag(5,6) == 1) {
				activate_hidden_group(2);
				if(get_flag(5,1) == 1)
					erase_char(8);
				else {
					put_boom_on_char(8,5,0);
					change_char_health(8,(get_max_health(8) * -1));
					change_char_health(8,get_flag(60,8));
					}
				if(get_flag(5,2) == 1)
					erase_char(10);
				else {
					put_boom_on_char(10,5,0);
					change_char_health(10,(get_max_health(10) * -1));
					change_char_health(10,get_flag(60,5));
					}
				if(get_flag(5,13) == 2)
					erase_char(6);
				else {
					put_boom_on_char(6,5,0);
					change_char_health(6,(get_max_health(6) * -1));
					change_char_health(6,get_flag(60,0));
					}
				if((get_flag(5,7) == 1) || (get_flag(50,3) == 1))
					erase_char(18);
				else {
					put_boom_on_char(18,5,0);
					change_char_health(18,(get_max_health(18) * -1));
					change_char_health(18,get_flag(60,1));
					}
				if(get_flag(5,8) == 1)
					erase_char(9);
				else {
					put_boom_on_char(9,5,0);
					change_char_health(9,(get_max_health(9) * -1));
					change_char_health(9,get_flag(60,6));
					}
				if(get_flag(5,9) == 1)
					erase_char(7);
				else {
					put_boom_on_char(7,5,0);
					change_char_health(7,(get_max_health(7) * -1));
					change_char_health(7,get_flag(60,7));
					}
				if(get_flag(5,10) == 1)
					erase_char(17);
				else {
					put_boom_on_char(17,5,0);
					change_char_health(17,(get_max_health(17) * -1));
					change_char_health(17,get_flag(60,3));
					}
				if(get_flag(5,11) == 1)
					erase_char(16);
				else {
					put_boom_on_char(16,5,0);
					change_char_health(16,(get_max_health(16) * -1));
					change_char_health(16,get_flag(60,4));
					}
				if(get_flag(5,12) == 1)
					erase_char(15);
				else {
					put_boom_on_char(15,5,0);
					change_char_health(15,(get_max_health(15) * -1));
					change_char_health(15,get_flag(60,2));
					}
				add_range_to_group(6,10,3);
				add_range_to_group(15,18,3);
				set_character_pose(1003,14);
				run_animation_sound(52);
				play_sound(-165);
				pause(1);
				set_character_pose(1003,13);
				force_instant_terrain_redraw();
				pause(1);
				set_character_pose(1003,12);
				force_instant_terrain_redraw();
				pause(1);
				set_character_pose(1003,11);
				force_instant_terrain_redraw();
				pause(1);
				set_character_pose(1003,0);
				force_instant_terrain_redraw();
				set_flag(5,6,2);
				if(num_chars_in_group(3) == 1)
					print_str_color("A ghost has followed you!",2);
				else if(num_chars_in_group(3) > 1)
					print_str_color("Some of the ghosts have followed you!",2);
			}
			if(num_chars_in_group(3) == 0) {
				reset_dialog();
				add_dialog_str(0,"As the last of the ghosts fades out, you breathe a sigh of relief. With any luck, you won't be followed any more.",0);
				run_dialog(1);
				set_flag(5,6,3);
				}
			}
		}

break;

beginstate 10; //stairs down, west set

	reset_dialog();
	add_dialog_str(0,"This set of stairs leads down. Do you descend?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	
	if(choice == 2)
		move_to_new_town(4,29,38);
	else
		block_entry(1);

break;

beginstate 11; //stairs down, east set

	reset_dialog();
	add_dialog_str(0,"This set of stairs leads down. Do you descend?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	
	if(choice == 2)
		move_to_new_town(4,35,38);
	else
		block_entry(1);

break;

beginstate 12; //ladder up, central

	reset_dialog();
	add_dialog_str(0,"There's a ladder here, leading up to a trapdoor in the ceiling. Do you climb it?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	
	if(choice == 2) {
		set_flag(6,3,3);
		move_to_new_town(6,32,38);
		}
	else
		block_entry(1);

break;

beginstate 13; //ladder up, west

	reset_dialog();
	add_dialog_str(0,"There's a ladder here, leading up to a trapdoor in the ceiling. Do you climb it?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	
	if(choice == 2) {
		set_flag(6,3,1);
		move_to_new_town(6,25,30);
		}
	else
		block_entry(1);

break;

beginstate 14; //ladder up, east

	reset_dialog();
	add_dialog_str(0,"There's a ladder here, leading up to a trapdoor in the ceiling. Do you climb it?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);

	if(choice == 2) {
		set_flag(6,3,2);
		move_to_new_town(6,39,30);
		}
	else
		block_entry(1);

break;

beginstate 20; //west ambush

	if((get_flag(5,4) == 1) || (get_flag(5,2) == 1) || (get_flag(50,1) != 0))
		end();

	reset_dialog();
	add_dialog_str(0,"You feel a gust of icy air blowing from the west.",0);
	run_dialog(1);
	pause(3);

	put_boom_on_space(21,32,5,0);
	activate_hidden_group(6);
	set_character_pose(21,14);
	run_animation_sound(52);
	play_sound(-165);
	pause(1);
	set_character_pose(21,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(21,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(21,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(21,0);
	force_instant_terrain_redraw();
	pause(5);

	set_flag(5,4,1);

break;

beginstate 21; //east ambush

	if((get_flag(5,3) == 1) || (get_flag(5,1) == 1) || (get_flag(50,1) != 0))
		end();

	reset_dialog();
	add_dialog_str(0,"You feel a gust of icy air blowing from the east.",0);
	run_dialog(1);
	pause(3);

	put_boom_on_space(43,32,5,0);
	activate_hidden_group(7);
	set_character_pose(20,14);
	run_animation_sound(52);
	play_sound(-165);
	pause(1);
	set_character_pose(20,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(20,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(20,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(20,0);
	force_instant_terrain_redraw();
	pause(5);

	set_flag(5,3,1);
break;


beginstate 30; //west dormitory

	if((get_flag(50,1) != 0) && ((get_flag(5,5) < 2) || (get_flag(5,5) == 3)))
		set_flag(5,5,2);

	if((get_flag(5,5) == 2) || (get_flag(5,5) == 4))
		end();

	reset_dialog();
	if(get_flag(5,5) == 3) {
		add_dialog_str(0,"This room looks pretty much identical to the one at the east end of the floor.",0);
		set_flag(5,5,4);
		}
	else {
		if(get_flag(5,5) == 1)
			add_dialog_str(0,"Judging from the sign, this should be a dormitory, probably for novice priests. You probably could've guessed this from the uniform and spartan furnishings alone.",0);
		else
			add_dialog_str(0,"Judging from the uniform and spartan furnishings, this is probably a dormitory. The church probably houses novice priests here.",0);
		set_flag(5,5,2);
		}
	add_dialog_str(1,"Thankfully, there don't seem to be any ghosts lurking in here.",0);
	run_dialog(1);

break;

beginstate 31; //east dormitory

	if((get_flag(5,5) == 3) || (get_flag(5,5) == 4))
		end();

	reset_dialog();
	if(get_flag(5,5) == 2) {
		add_dialog_str(0,"This room looks pretty much identical to the one at the west end of the floor.",0);
		set_flag(5,5,4);
		}
	else {
		add_dialog_str(0,"Judging from the uniform and spartan furnishings, this is probably a dormitory. The church probably houses novice priests here.",0);
		set_flag(5,5,3);
		}
	add_dialog_str(1,"Thankfully, there don't seem to be any ghosts lurking in here.",0);
	run_dialog(1);

break;


beginstate 40; //try the door, ghosts appear

	reset_dialog();
	add_dialog_str(0,"As you reach for the door, you hear an all too familiar sound behind you, accompanied by a blast of air so cold that it shocks your lungs.",0);
	run_dialog(1);

	activate_hidden_group(2);
	add_range_to_group(6,10,3);
	add_range_to_group(15,18,3);
	put_boom_on_char(6,5,0);
	put_boom_on_char(7,5,0);
	if(get_flag(5,1) == 1)
		erase_char(8);
	else
		put_boom_on_char(8,5,0);
	put_boom_on_char(9,5,0);
	if(get_flag(5,2) == 1)
		erase_char(10);
	else
		put_boom_on_char(10,5,0);
	put_boom_on_char(15,5,0);
	put_boom_on_char(16,5,0);
	put_boom_on_char(17,5,0);
	if((get_flag(50,3) < 2) && (get_flag(5,7) == 0))
		put_boom_on_char(18,5,0);
	else
		erase_char(18);
	set_character_pose(1003,14);
	run_animation_sound(52);
	play_sound(-165);
	pause(1);
	set_character_pose(1003,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(1003,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(1003,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(1003,0);
	force_instant_terrain_redraw();

	pause(3);

	set_flag(5,13,1);
	reset_dialog();
	add_dialog_str(0,"The captain appears, along with a significant group of underlings. This time, however, there's something different about the captain.",0);
	add_dialog_str(1,"His form appears a bit more solid, and it practically radiates angry energy. His halberd also seems to be glowing.",0);
	add_dialog_str(2,"He smiles at you and raises the halberd. _Excuse me,_ he says. _You're not going anywhere._",0);
	run_dialog(1);
	pause(3);

	put_boom_on_space(32,39,3,0);
	put_straight_zap(32,43,32,39,6);
	set_character_pose(6,1);
	run_animation_sound(112);
	pause(2);
	set_character_pose(6,0);
	force_instant_terrain_redraw();
	pause(3);

	reset_dialog();
	add_dialog_str(0,"The captain fires a bolt of energy from his halberd past you, straight at the door. It hits home, covering the door with a thick layer of ice. You definitely won't be able to open it like that.",0);
	add_dialog_str(1,"Of course, with a group of angry ghosts bearing down on you, the door is the least of your worries.",0);
	run_dialog(1);
	set_flag(5,6,2);

break;



beginstate 80; //teh cutscene

	set_ticks_forward(1000);

	flip_terrain(27,36);
	force_view_center(23,34);
	set_character_facing(1000,4);
	set_character_pose(1000,14);
	relocate_character(0,23,34);
	relocate_character(1,22,34);
	relocate_character(2,24,34);
	relocate_character(3,23,33);
	activate_hidden_group(15);
	force_instant_terrain_redraw();

	pause(3);

	reset_dialog();
	if(get_flag(5,5) == 2)
		add_dialog_str(0,"You awake in the dormitory you found on the fourth floor. A priest stands nearby, clearly sent to make sure you were still alive and coherent.",0);
	else if(get_flag(5,5) == 4)
		add_dialog_str(0,"You awake in one of the dormitories you found on the fourth floor. A priest stands nearby, clearly sent to make sure you were still alive and coherent.",0);
	else
		add_dialog_str(0,"You awake in what seems to be a dormitory. A priest stands nearby, clearly sent to make sure you were still alive and coherent.",0);
	if(get_flag(11,11) == 1)
		add_dialog_str(1,"As you carefully stand up again, you're relieved to see that the priest is Ethan. He clears his throat.",0);
	else
		add_dialog_str(1,"As you carefully stand up again, the priest clears his throat.",0);
	set_character_pose(1000,0);
	force_instant_terrain_redraw();
	run_dialog(1);
	set_flag(5,15,1);
	pause(3);

	reset_dialog();
	add_dialog_str(0,"_Good to see that you're conscious again. We were rather worried when we heard the generator start up again and you didn't come back down... it's a good thing that Norbert went to check on you._",0);
	if(get_flag(50,1) == 1) {
		add_dialog_str(1,"_Anyway, the generator seems to have worked, and we haven't heard or felt any of the ghosts since it kicked back in. Of course, we'd like to thank you for your work, and Father Richardson has a reward for you downstairs._",0);
		toggle_quest(7,0);
		}
	else if(get_flag(50,1) == 2) {
		add_dialog_str(1,"_Anyway, the generator seems to have been tampered with, as a few ghosts are still hanging around the cathedral. The dead soldiers have vanished, and we don't know where they went._",0);
		add_dialog_str(2,"_We know this can't have been easy for you, so Father Richardson has decided to compensate you for your efforts. Speak to him to get it._",0);
		toggle_quest(9,0);
		}
	add_dialog_str(3,"He bows, then leaves you alone in the room.",0);
	run_dialog(1);
	if(get_flag(50,1) == 1)
		toggle_quest(7,0);
	if(get_flag(50,1) == 1)
		toggle_quest(9,0);

	set_character_facing(22,6);
	force_instant_terrain_redraw();
	pause(1);
	play_sound(-49);
	relocate_character(22,24,36);
	force_instant_terrain_redraw();
	pause(4);
	play_sound(-50);
	relocate_character(22,25,36);
	force_instant_terrain_redraw();
	pause(4);
	play_sound(-49);
	relocate_character(22,26,36);
	force_instant_terrain_redraw();
	pause(4);
	play_sound(-50);
	relocate_character(22,27,36);
	force_instant_terrain_redraw();
	pause(4);
	set_character_facing(22,4);
	force_instant_terrain_redraw();
	pause(1);
	play_sound(-49);	
	relocate_character(22,27,37);
	force_instant_terrain_redraw();
	pause(4);
	play_sound(-50);
	relocate_character(22,27,38);
	force_instant_terrain_redraw();
	pause(4);
	erase_char(22);
	force_view_center(char_loc_x(lead_char), char_loc_y(lead_char));
	force_instant_terrain_redraw();
	

break;


beginstate 81; //leaving the cutscene room

	if(get_flag(5,15) != 1)
		end();

	reset_dialog();
	add_dialog_str(0,"Stepping back out into the hallway, you're relieved to hear nothing. No paranoid murmurs, no deranged chanting... the cathedral should finally be quiet again.",0);
	if(get_flag(50,1) == 2)
		add_dialog_str(1,"Of course, that's assuming that all of the ghosts left.",0);
	run_dialog(1);
	set_flag(5,15,2);

break;