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(3,0) == 0) && (get_flag(50,0) > 0)) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"As you climb higher up into the cathedral, you begin to feel a bit colder, similar to when you were in the catacombs.",0);
		add_dialog_str(1,"Thankfully, the oppressive atmosphere isn't as intense as it was down there. It's probably because you're still fairly close to the sanctuary.",0);
		run_dialog(1);
		set_flag(3,0,1);
		}


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

	if(get_flag(50,0) > 0) {
		activate_hidden_group(1);
		force_instant_terrain_redraw();
		set_name(10,"Ethan");
		set_level(10,20);
		set_name(11,"Alyse");
		set_level(11,20);
		set_name(8,"Felix");
		set_level(8,20);
		}
	if((get_flag(50,0) > 0) && (get_flag(50,1) != 1)) {
		activate_hidden_group(2);
		set_name(18,"Private Roberts");
		force_instant_terrain_redraw();
		}
	else if(get_flag(50,0) == 0)
		activate_hidden_group(3);
	set_name(6,"Father Richardson");
	set_level(6,35);
	set_name(7,"Norbert");
	set_name(9,"Brother Silas");
	set_level(9,25);

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(25) == 1)
		set_flag(60,0,get_health(25));
	if(char_ok(22) == 1)
		set_flag(60,7,get_health(22));
	if(char_ok(21) == 1)
		set_flag(60,8,get_health(21));
	if(char_ok(23) == 1)
		set_flag(60,6,get_health(23));
	if(char_ok(24) == 1)
		set_flag(60,5,get_health(24));
	if(char_ok(28) == 1)
		set_flag(60,2,get_health(28));
	if(char_ok(26) == 1)
		set_flag(60,4,get_health(26));
	if(char_ok(27) == 1)
		set_flag(60,3,get_health(27));
	if(char_ok(20) == 1)
		set_flag(60,1,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(8);
				if(get_flag(5,1) == 1)
					erase_char(21);
				else {
					put_boom_on_char(21,5,0);
					change_char_health(21,(get_max_health(21) * -1));
					change_char_health(21,get_flag(60,8));
					}
				if(get_flag(5,2) == 1)
					erase_char(24);
				else {
					put_boom_on_char(24,5,0);
					change_char_health(24,(get_max_health(24) * -1));
					change_char_health(24,get_flag(60,5));
					}
				if(get_flag(5,13) == 2)
					erase_char(25);
				else {
					put_boom_on_char(25,5,0);
					change_char_health(25,(get_max_health(25) * -1));
					change_char_health(25,get_flag(60,0));
					}
				if((get_flag(5,7) == 1) || (get_flag(50,3) == 1))
					erase_char(20);
				else {
					put_boom_on_char(20,5,0);
					change_char_health(20,(get_max_health(20) * -1));
					change_char_health(20,get_flag(60,1));
					}
				if(get_flag(5,8) == 1)
					erase_char(23);
				else {
					put_boom_on_char(23,5,0);
					change_char_health(23,(get_max_health(23) * -1));
					change_char_health(23,get_flag(60,6));
					}
				if(get_flag(5,9) == 1)
					erase_char(22);
				else {
					put_boom_on_char(22,5,0);
					change_char_health(22,(get_max_health(22) * -1));
					change_char_health(22,get_flag(60,7));
					}
				if(get_flag(5,10) == 1)
					erase_char(27);
				else {
					put_boom_on_char(27,5,0);
					change_char_health(27,(get_max_health(27) * -1));
					change_char_health(27,get_flag(60,3));
					}
				if(get_flag(5,11) == 1)
					erase_char(26);
				else {
					put_boom_on_char(26,5,0);
					change_char_health(26,(get_max_health(26) * -1));
					change_char_health(26,get_flag(60,4));
					}
				if(get_flag(5,12) == 1)
					erase_char(28);
				else {
					put_boom_on_char(28,5,0);
					change_char_health(28,(get_max_health(28) * -1));
					change_char_health(28,get_flag(60,2));
					}
				add_range_to_group(20,28,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) {
		set_flag(3,10,1);
		move_to_new_town(1,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) {
		set_flag(3,10,1);
		move_to_new_town(1,35,38);
		}
	else
		block_entry(1);

break;

beginstate 12; //stairs up, west set

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

break;

beginstate 13; //stairs up, east set

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

break;


beginstate 20; //enter the balcony

	if(get_flag(3,1) > 0)
		end();

	reset_dialog();
	add_dialog_str(0,"A row of comfortable-looking seats adorn this balcony, as well as a polished rail. From here, you can almost see the entire sanctuary.",0);
	run_dialog(1);
	set_flag(3,1,1);


break;

beginstate 25; //walk into a cell

	if(get_flag(3,2) > 1)
		end();

	if(get_flag(3,2) == 1) {
		set_flag(3,2,2);
		end();
		}

	reset_dialog();
	add_dialog_str(0,"This tiny room appears to be a meditation cell of sorts, judging from the sparse furnishings.",0);
	run_dialog(1);
	set_flag(3,2,2);


break;

beginstate 30; //walk into Silas' cell

	if(get_flag(3,3) > 0)
		end();

	reset_dialog();
	if(get_flag(3,2) == 0) {
		add_dialog_str(0,"This tiny room appears to be a meditation cell of sorts, judging from the sparse furnishings and the anxious-looking priest crouched in the corner.",0);
		set_flag(3,2,2);
		}
	else {
		add_dialog_str(0,"Upon entering this cell, you're more than a little surprised to find an anxious-looking priest crouched in the corner.",0);
		}
	if(get_flag(1,7) == 1)
		add_dialog_str(1,"You're willing to bet that this is the Brother Silas that Father Richardson spoke of.",0);
	else if(get_flag(50,0) > 0)
		add_dialog_str(1,"Whoever it is, you can't help but wonder why they're still up here, and why the ghosts haven't killed him yet.",0);
	else if(get_flag(50,1) > 0)
		add_dialog_str(1,"Whoever it is, you can't help but wonder why they're still up here, and why the ghosts didn't kill him.",0);
	run_dialog(1);
	set_flag(3,3,1);


break;

beginstate 35; //walk into Roberts' cell

	if((get_flag(3,6) == 1) && (get_flag(50,1) == 2)) {
		reset_dialog();
		add_dialog_str(0,"You're a little surprised to see the ghost still lingering in this room.",0);
		run_dialog(1);
		set_flag(3,6,2);
		end();
		}


	if(get_flag(3,6) > 0)
		end();


	if((get_flag(50,0) == 0) || (get_flag(50,1) == 1)) {
		if(get_flag(3,2) < 2) {
			reset_dialog();
			add_dialog_str(0,"This tiny room appears to be a meditation cell of sorts, judging from the sparse furnishings.",0);
			run_dialog(1);
			set_flag(3,2,2);
			end();
			}
		}
	else {
		reset_dialog();
		if(get_flag(3,2) < 2) {
			add_dialog_str(0,"This tiny room appears to be a meditation cell of sorts, judging from the sparse furnishings.",0);
			set_flag(3,2,2);
			}
		add_dialog_str(1,"As you enter the cell, you're shocked to find one of the dead soldiers. The ghost looks equally shocked.",0);
		add_dialog_str(2,"Before you can react, the ghost throws its hands up in the air and shouts _Wait! I'm a friend!_",0);
		run_dialog(1);
		set_flag(3,6,1);
		}

break;