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) >= 4) || (get_flag(4,1) == 0))
		set_terrain(39,35,0);

	if((get_flag(50,0) > 0) && (get_flag(50,1) == 0)) {
		activate_hidden_group(1);
		add_range_to_group(6,8,1);
		if(get_flag(4,4) == 2)
			erase_char(1001);
		if(get_flag(4,7) >= 3) {
			set_attitude(9,10);
			if(get_flag(4,12) != 1)
				activate_hidden_group(4);
			}
		if(get_flag(4,8) == 2)
			erase_char(9);
		if(get_flag(4,2) == 1)
			erase_char(13);
		}

	if((get_flag(4,0) == 0) && (get_flag(50,0) > 0)) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"As you climb higher up the stairs (and farther from the sanctuary), you feel the temperature of the air plummet. Definitely not a good sign.",0);
		add_dialog_str(1,"To make matters worse, you can feel your thoughts beginning to cloud again. It's going to be difficult to maintain your concentration up here.",0);
		run_dialog(1);
		set_flag(4,0,1);
		}

break;


beginstate 1; //EXIT_STATE

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

	if(char_ok(11) == 1) {
		reset_dialog();
		if(party_can_see_loc(char_loc_x(11), char_loc_y(11)) == 1)
			add_dialog_str(0,"The demon utters a foul curse as the last of its body becomes insubstantial and disappears. All that it leaves behind is a foul sulfur odor and a few scorch-marks.",0);
		else
			add_dialog_str(0,"You hear the demon utter a loud, vile curse. Then, silence.",0);
		run_dialog(1);
		erase_char(11);
		}

	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(21) == 1)
		set_flag(60,0,get_health(21));
	if(char_ok(16) == 1)
		set_flag(60,7,get_health(16));
	if(char_ok(14) == 1)
		set_flag(60,8,get_health(14));
	if(char_ok(17) == 1)
		set_flag(60,6,get_health(17));
	if(char_ok(15) == 1)
		set_flag(60,5,get_health(15));
	if(char_ok(20) == 1)
		set_flag(60,2,get_health(20));
	if(char_ok(18) == 1)
		set_flag(60,4,get_health(18));
	if(char_ok(19) == 1)
		set_flag(60,3,get_health(19));
	if(char_ok(22) == 1)
		set_flag(60,1,get_health(22));


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((char_ok(13) == 0) && (get_flag(4,2) == 0)) {
			message_dialog("Now that the ghost is gone, you can see what you can do with the unconscious guard.","");
			set_flag(4,2,1);
			}
		}

	if((get_flag(50,0) > 0) && (get_flag(50,1) == 0)) {
		if((char_ok(12) == 0) && (char_ok(9) == 1) && (get_flag(4,7) == 3) && (get_flag(4,2) == 0)) {
			if(get_flag(4,8) > 0)
				message_dialog("Now that you've disrupted the guard, it's time to deal with the turbo-charged priest.","");
			else
				message_dialog("Now that you've disrupted the guard, it's time to deal with the priest. Perhaps you can dispose of him before he finishes chanting.","");
			set_flag(4,2,1);
			}
		}

	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(3);
			set_character_pose(10,14);
			run_animation_sound(52);
			play_sound(-165);
			pause(1);
			set_character_pose(10,13);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(10,12);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(10,11);
			force_instant_terrain_redraw();
			pause(1);
			set_character_pose(10,0);
			force_instant_terrain_redraw();
			pause(1);
			set_name(10,"Empire Sniper");
			print_str_color("The archer reappears!",2);
			set_flag(4,3,2);
			change_char_health(10,(get_max_health(10) * -1));
			change_char_health(10,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(14);
				else {
					put_boom_on_char(14,5,0);
					change_char_health(14,(get_max_health(14) * -1));
					change_char_health(14,get_flag(60,8));
					}
				if(get_flag(5,2) == 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,5));
					}
				if(get_flag(5,13) == 2)
					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,0));
					}
				if((get_flag(5,7) == 1) || (get_flag(50,3) == 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,1));
					}
				if(get_flag(5,8) == 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,6));
					}
				if(get_flag(5,9) == 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,7));
					}
				if(get_flag(5,10) == 1)
					erase_char(19);
				else {
					put_boom_on_char(19,5,0);
					change_char_health(19,(get_max_health(19) * -1));
					change_char_health(19,get_flag(60,3));
					}
				if(get_flag(5,11) == 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,4));
					}
				if(get_flag(5,12) == 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,2));
					}
				add_range_to_group(14,22,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(3,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(3,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(5,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(5,35,42);
	else
		block_entry(1);

break;

beginstate 20; //archer ambush

	if(get_flag(4,3) == 0) {
		reset_dialog();
		add_dialog_str(0,"You hear a breeze behind you.",0);
		run_dialog(1);
		pause(3);

		force_view_center(32,39);
		force_instant_terrain_redraw();
		pause(2);

		put_boom_on_space(32,39,5,0);
		activate_hidden_group(3);
		set_name(10,"Empire Sniper");
		run_animation_sound(52);

		pause(3);
		reset_dialog();
		add_dialog_str(0,"The ghost of a battle-scarred Empire archer appears. Without saying a word, he pulls a translucent blue arrow from thin air and nocks it to a softly-glowing bow.",0);
		run_dialog(1);

		pause(3);
		force_view_center(char_loc_x(lead_char), char_loc_y(lead_char));
		force_instant_terrain_redraw();

		set_flag(4,3,2);
		}

break;

beginstate 30; //entering the office w/ Jack in it

	if((get_flag(4,1) == 0) || (get_flag(4,1) > 1))
		end();

	force_instant_terrain_redraw();
	pause(3);

	reset_dialog();
	add_dialog_str(0,"This room appears to be an office of sorts. Nothing about it seems out of the ordinary, except for the unconscious soldier lying in the middle of the floor.",0);
	if((get_flag(50,0) > 0) && (get_flag(50,1) == 0)) {
		add_dialog_str(1,"Also, there's the ghost of a soldier standing over him. The spirit draws a translucent blade and charges at you, unsurprisingly.",0);
		add_dialog_str(2,"This is probably the Jack that you were told about. You should probably check if he's still alive, once the ghost is gone.",0);
		}
	else
		add_dialog_str(2,"This is probably the Jack that you were told about. You should probably check if he's still alive.",0);
	run_dialog(1);

	set_flag(4,1,2);

break;

beginstate 31; //searching the body

	if((get_flag(4,1) > 3) || (get_flag(4,1) == 0))
		end();

	reset_dialog();
	if(get_flag(4,1) < 3) {
		add_dialog_str(0,"This poor soldier looks like he's been roughed up quite a bit. His armor has long, thin sections torn out of it, and his arms are covered with bloody cuts.",0);
		add_dialog_str(1,"You don't even want to think about how the ghosts inflicted these wounds on the man. Thankfully, he's still breathing, if shallowly.",0);
		add_dialog_str(2,"Even with his armor on, you should be able to carry him back outside to his friend.",0);
		set_flag(4,1,3);
		}
	if(char_ok(13) == 1) {
		add_dialog_str(3,"Regrettably, as long as the angry spirit is in the room, you won't have a chance to do anything with the body.",0);
		run_dialog(1);
		end();
		}
	add_dialog_str(3,"Do you take the body with you?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	if(choice == 2) {
		set_terrain(39,35,0);
		reward_give(448);
		set_flag(4,1,4);
		}


break;


beginstate 40; //door to classroom B

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

	force_instant_terrain_redraw();
	pause(2);

	reset_dialog();
	if(get_flag(50,1) == 0) {
		add_dialog_str(0,"As the door swung open, you expected that the chanting would stop. It didn't. But nothing moves to attack you, so you relax.",0);
		if(get_flag(4,5) == 0)
			add_dialog_str(1,"It looks like this room was used as a classroom before the ghosts moved in. You can only guess what the ghosts want to use it for.",0);
		}
	else
		add_dialog_str(1,"It looks like this room was used as a classroom before the ghosts moved in. You can only guess what the ghosts wanted to use it for.",0);
	run_dialog(1);
	set_flag(4,4,1);

break;

beginstate 41; //entering classroom B

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

	play_sound(-49);
	march_party(26,31);
	force_view_center(26,31);
	block_entry(1);
	force_instant_terrain_redraw();
	pause(3);

	reset_dialog();
	add_dialog_str(0,"Now you can get a better look at what's going on in here, and maybe figure out why nothing has attacked you yet.",0);
	add_dialog_str(1,"The ghost of a mage stands by a rough summoning platform, which probably once served as a teaching aid. Now, you're not certain you want to know its use.",0);
	add_dialog_str(2,"As you cautiously step into the room, the chanting stops.",0);
	run_dialog(1);

	pause(3);
	force_view_center(27,27);
	set_character_pose(6,2);
	text_bubble_on_char(6,"Come forth!");
	force_instant_terrain_redraw();

	pause(5);
	erase_text_bubbles();
	force_instant_terrain_redraw();

	put_effect_on_char(6,2,8,1);
	run_animation_sound(165);

	set_character_pose(6,1);
	put_boom_on_space(27,27,0,0);
	run_animation_sound(62);
	activate_hidden_group(2);
	force_instant_terrain_redraw();

	pause(5);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	add_dialog_str(0,"As the mage's voice dies away, a demon is left standing next to him. However, something seems to be wrong with it.",0);
	add_dialog_str(1,"Most demons you've seen in your travels have a superior air about them, but this one looks panicked. It also looks as if it's been injured, as parts of it appear to be missing.",0);
	run_dialog(1);

	pause(3);
	put_sparkles_on_char(11,7,10);
	run_animation_sound(42);
	pause(2);
	message_dialog("Then you see the problem. The demon's body is slightly insubstantial, as if it wasn't completely drawn into this plane. It probably has something to do with the building's wards.","Whatever the cause, the demon seems angry about it.");
	pause(3);

	play_sound(119);
	set_character_pose(11,1);
	set_character_facing(11,6);
	force_instant_terrain_redraw();
	put_effect_on_char(6,11,1,2);
	run_animation_sound(118);
	pause(2);
	set_character_pose(11,0);
	play_sound(-40);
	set_character_pose(6,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(6,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(6,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(6,14);
	force_instant_terrain_redraw();
	pause(1);
	erase_char(6);
	force_instant_terrain_redraw();
	pause(1);
	message_dialog("The demon rounds on the mage, disrupting him with a flick of its claw.","The other ghosts move to attack, but the demon whirls around again.");
	pause(3);
	set_character_pose(11,1);
	set_character_facing(11,4);
	force_instant_terrain_redraw();
	play_sound(-119);

	put_jagged_zap(27,27,25,27,2);
	put_boom_on_char(7,3,0);
	put_jagged_zap(27,27,28,29,2);
	put_boom_on_char(8,3,0);
	run_animation_sound(54);
	pause(3);

	play_sound(-40);
	set_character_pose(7,11);
	set_character_pose(8,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(7,12);
	set_character_pose(8,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(7,13);
	set_character_pose(8,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(7,14);
	set_character_pose(8,14);
	force_instant_terrain_redraw();
	pause(1);
	erase_char(7);
	erase_char(8);
	set_character_pose(11,0);
	force_instant_terrain_redraw();
	pause(3);

	reset_dialog();
	add_dialog_str(0,"Well, if nothing else, at least that's one mage and two grunts out of the way. But for now, you have to deal with the mage's failed attempt at summoning.",0);
	add_dialog_str(1,"The demon may be slowly fading away on its own, but it seems angry enough to try and take you down before it's gone. It bellows and rounds on you.",0);
	run_dialog(1);
	play_sound(119);
	force_view_center(char_loc_x(lead_char), char_loc_y(lead_char));
	force_instant_terrain_redraw();
	pause(2);

	set_flag(4,4,2);
	inc_flag(50,3,1);

break;


beginstate 50; //entering the archives

	if(get_flag(4,6) >= 2)
		end();

	reset_dialog();
	if(get_flag(4,6) == 1)
		add_dialog_str(0,"The archives are a lot more crowded than you would've thought. The church must keep a lot of records.",0);
	else
		add_dialog_str(0,"Judging from the massive bookshelves that dominate this chamber, you're willing to bet that these are the church's archives.",0);
	run_dialog(1);

	set_flag(4,6,2);

break;

beginstate 51; //searching bookshelves (boring stuff)

	reset_dialog();
	add_dialog_str(0,"The books on this shelf are full of parish records. They don't make for terribly interesting reading.",0);
	run_dialog(1);

break;

beginstate 52; //bookshelves (burnt burial records)

	if(get_flag(4,9) > 0)
		end();

	reset_dialog();

	add_dialog_str(0,"Odd. On this shelf, you find a thick tome whose spine appears to be a bit burnt.",0);
	add_dialog_str(1,"Predictably, most of the pages are scorched as well. But from the bits you can make out, the book seems to be a record of the many burials the church has performed.",0);
	add_dialog_str(2,"You're willing to bet that this has something to do with the angry ghosts, but you can't think what.",0);
	run_dialog(1);
	set_flag(4,9,1);

break;

beginstate 53; //bookshelves (interesting thing #2)

	if(get_flag(4,10) == 1) {
		reset_dialog();
		add_dialog_str(0,"The books on this shelf are full of parish records. They don't make for terribly interesting reading.",0);
		run_dialog(1);
		end();
		}

	reset_dialog();
	add_dialog_str(0,"Searching this shelf, you happen upon a scroll tube. Regrettably, the tube is empty.",0);
	run_dialog(1);

	set_flag(4,10,1);

break;

beginstate 54; //the desk

	//letter attempting to bribe for a record change (divorce?)
	//the money's there, as well as the unchanged record - clever

	if(get_flag(4,11) > 1)
		end();

	reset_dialog();
	if(get_flag(4,11) == 1) {
		add_dialog_str(0,"The scroll is still here. What do you do with it?",0);
		}
	else {
		add_dialog_str(0,"This desk is mostly given over to scattered blank pieces of paper and assorted pens. However, your eye is caught by an ordinary-looking scroll with a complicated seal on it.",0);
		add_dialog_str(1,"What do you do with it?",0);
		set_flag(4,11,1);
		}
	add_dialog_choice(0,"Leave it alone.");
	add_dialog_choice(1,"Read it.");
	choice = run_dialog(1);
	if(choice == 2) {
		reset_dialog();
		add_dialog_str(0,"You unroll the scroll, only to find a short letter rolled up inside it. You read the letter first:",0);
		add_dialog_str(1,"_I hope this is sufficient payment for what I've asked of you. Just sign the paper, and I'll be on my way._",15);
		add_dialog_str(2,"At first glance, the scroll seems to be a marriage certificate of sorts. But upon closer examination, you find that it is actually an official certificate of divorce.",0);
		add_dialog_str(3,"Divorce is still fairly uncommon in most Empire towns, and it still carries a significant social stigma. Thus, marriages tend to take ungodly amounts of strain before either partner chooses divorce.",0);
		add_dialog_str(4,"Somewhat alarming is how similar the signatures for the husband and wife appear. Your best guess is that one forged the other's signature, but there's no way to know. And the space for a priest's signature is blank.",0);
		add_dialog_str(5,"So, if nothing else, the priest didn't seem to give in to the bribe. That said, there's no trace of any coins in the desk, so the priest probably took the bribe. Clever.",0);
		run_dialog(1);
		set_flag(4,11,2);
		}


break;




beginstate 60; //open door to classroom A

	if((get_flag(4,7) > 1) || (get_flag(50,1) != 0))
		end();

	force_instant_terrain_redraw();
	pause(2);

	reset_dialog();
	add_dialog_str(0,"As the door swings open, you brace for an attack. But the chanting continues, and nothing moves to attack you.",0);
	if(get_flag(4,7) == 0)
		add_dialog_str(1,"It looks like this room was used as a classroom before the ghosts moved in. You can only guess what the ghosts want to use it for.",0);
	run_dialog(1);
	set_flag(4,7,2);

break;

beginstate 61; //entering the room w/ the priest

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

	play_sound(-49);
	march_party(38,31);
	force_view_center(38,31);
	block_entry(1);
	force_instant_terrain_redraw();
	pause(3);

	reset_dialog();
	if(get_flag(11,6) == 1)
		add_dialog_str(0,"You're a little surprised to find just one ghost in this room. It looks like a priest, but it definitely isn't the same one you found on the ground floor.",0);
	else if (get_flag(11,6) == 2)
		add_dialog_str(0,"You're a little surprised to find just one ghost in this room. It looks like a priest, but it definitely isn't Grimsley.",0);
	else
		add_dialog_str(0,"You're a little surprised to find just one ghost in this room. However, it looks like a priest, so you might want to exercise some caution.",0);
	add_dialog_str(1,"It has its back to you, so it might not have seen you yet. Either way, it seems to be preoccupied with chanting over a set of stone tablets.",0);
	run_dialog(1);

	pause(3);

	put_boom_on_space(38,30,5,0);
	activate_hidden_group(4);
	set_character_pose(12,14);
	run_animation_sound(52);
	play_sound(-165);
	pause(1);
	set_character_pose(12,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(12,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(12,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(12,0);
	force_instant_terrain_redraw();
	pause(2);

	message_dialog("Then, accompanied by a gust of cold wind, a ghost swoops up through the floor. It lands in front of you with a wicked-looking, bluish halberd drawn.","The priest continues chanting.");
	set_attitude(9,10);

	set_flag(4,7,3);

break;

beginstate 62; //the tablets he was looking at

	if(is_combat() == 1) {
		message_dialog("You can't read the tablets in combat mode.","");
		end();
		}

	if(get_flag(4,14) == 1) {
		message_dialog("You scan the tablets, but don't learn anything new.","");
		end();
		}

	if(get_highest_skill(42) < 8) {
		reset_dialog();
		add_dialog_str(0,"These tablets are covered in a rough-looking ancient script. Sadly, the text is beyond your comprehension.",0);
		run_dialog(1);
		end();
		}
	else {
		reset_dialog();
		add_dialog_str(0,"These tablets are covered in a rough-looking ancient script. Thankfully, you are proficient enough in ancient languages to read them.",0);
		add_dialog_str(1,"They describe a set of spells that turn even the weakest, most humble slave into a powerful warrior. By studying them for a moment, you increase your proficiency with the spells Haste and War Blessing.",0);
		run_dialog(1);
		set_flag(4,14,1);
		pc_counter = 0;
		while (pc_counter < 4) {
			if(char_ok(pc_counter) == 1) {
				if(get_stat(pc_counter,11) >= 3)
					change_spell_level(pc_counter, 0,4,1);
				if(get_stat(pc_counter,12) >= 1)
					change_spell_level(pc_counter, 1,2,2);
				}
			pc_counter = pc_counter + 1;
			}
		}


break;