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(50,0) == 1) && (get_flag(1,26) == 0)) {
		force_instant_terrain_redraw();
		pause(3);
		activate_hidden_group(2);
		reset_dialog();
		add_dialog_str(0,"Something doesn't feel right. As you climb the stairs, you start to feel the same chill in the air that filled the catacombs. It's also very, very quiet.",0);
		run_dialog(1);
		set_flag(1,26,1);
		}

	if(get_flag(1,0) == 0) {
		reset_dialog();
		add_dialog_str(0,"Now that you get up close to the Cathedral of Winds, you have to admit that you're a bit disappointed. The stone has been worn smooth by the elements, and the building clearly has not aged well.",0);
		add_dialog_str(1,"Far more impressive than the building are the gardens. The walkways are immaculately clean, and the hedges look like they were trimmed recently. And the obelisk you find yourself standing in front of... well, it looks old.",0);
		add_dialog_str(2,"Regardless, you'll find out more about your quest inside.",0);
		run_dialog(1);
		set_flag(1,0,1);
		}

	set_name(6,"Father Richardson");
	set_level(6,35);
	set_char_dialogue_pic(6,1949,0);
	set_name(7,"Norbert");
	set_name(9,"Alyse");
	set_level(9,20);
	set_name(8,"Ethan");
	set_level(8,20);
	set_name(15,"Paula");
	set_level(15,20);

	if(get_flag(50,0) > 0) {
		activate_hidden_group(3);
		set_name(16,"Felix");
		set_level(16,20);
		relocate_character(9,30,31);
		set_character_facing(9,0);
		set_mobility(9,0);
		set_creature_memory_cell(9,3,49);
		relocate_character(8,34,32);
		set_character_facing(8,0);
		set_mobility(8,0);
		set_creature_memory_cell(8,3,58);
		if((get_flag(11,14) == 0) && (get_flag(11,15) == 0)) {
			activate_hidden_group(1);
			relocate_character(14,41,36);
			set_name(14,"Grimsley");
			set_level(14,20);
			if(get_flag(11,6) == 1)
				set_attitude(14,10);
			}
		activate_hidden_group(2);
		if(get_flag(4,1) == 0)
			relocate_character(13,28,31);
		}

	if((get_flag(50,0) == 1) && (get_flag(50,1) == 0)) {
		if((get_flag(3,10) == 1) && (get_flag(5,6) == 1)) {
			force_instant_terrain_redraw();
			pause(3);
			reset_dialog();
			add_dialog_str(0,"Now that you're closer to the sanctuary, it seems that the ghosts have stopped following you.",0);
			run_dialog(1);
			set_flag(3,10,0);
			}
		else if((get_flag(3,10) == 1) && (get_flag(4,3) == 1)) {
			force_instant_terrain_redraw();
			pause(3);
			reset_dialog();
			add_dialog_str(0,"Now that you're closer to the sanctuary, it seems that the ghost has stopped following you.",0);
			run_dialog(1);
			set_flag(3,10,0);
			}


		}

break;


beginstate 1; //EXIT_STATE

break;


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

break;

beginstate 10; //stairs up, west set

	if(get_flag(1,4) == 0) {
		message_dialog("Hm... you should probably go speak with Father Richardson before you go and explore the rest of the cathedral.","Perhaps you should look for the sanctuary.");
		block_entry(1);
		end();
		}

	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(3,29,42);
	else
		block_entry(1);

break;

beginstate 11; //stairs up, east set

	if(get_flag(1,4) == 0) {
		message_dialog("Hm... you should probably go speak with Father Richardson before you go and explore the rest of the cathedral.","Perhaps you should look for the sanctuary.");
		block_entry(1);
		end();
		}

	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(3,35,42);
	else
		block_entry(1);

break;

beginstate 12; //stairs down to catacombs

	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(8,28,32);
	else
		block_entry(1);

break;

beginstate 15; //the fountain

	if(get_flag(1,1) != 0)
		end();

	reset_dialog();
	if(get_flag(0,19) == 0) {
		add_dialog_str(0,"This fountain doesn't quite seem normal. It probably has something to do with the fact that the stone from which it was hewn. It actually glows.",0);
		add_dialog_str(1,"You can actually feel the energy radiating off of it. It feels very positive, almost holy.",0);
		}
	else
		add_dialog_str(0,"This fountain looks and feels very similar to the one you saw in Stratton. Perhaps both were built by the same architect.",0);
	run_dialog(1);

	set_flag(1,1,1);

break;


beginstate 20; //the herb patch

	if(get_flag(1,29) == 1)
		end();

	if(get_flag(1,27) == 2) {
		reset_dialog();
		add_dialog_str(0,"These must be the herbs that Paula was talking about. And like she said, they don't look ripe yet.",0);
		run_dialog(1);
		set_flag(1,29,1);
		end();
		}

	reset_dialog();
	add_dialog_str(0,"This appears to be a small herb garden. Sadly, none of the plants are more than a couple of inches high, making it very unlikely that any of them are ripe for your uses.",0);
	run_dialog(1);
	set_flag(1,29,1);


break;


beginstate 25; //main entrance dialog

	if((has_item(448) == 1) && (get_flag(4,1) < 5)) {
		pause(3);
		reset_dialog();
		add_dialog_str(0,"The second you step out of the cathedral, the guard you met earlier runs up to you, clearly having noticed that you're carrying his friend.",0);
		add_dialog_str(1,"He takes the unconscious guard from you and takes him off the walkway, setting him down on the grass.",0);
		add_dialog_str(2,"_Thank you so much!_ the guard says. _We'll get him out of here as soon as we can._",0);
		run_dialog(1);
		toggle_quest(5,0);
		award_party_xp(200,20);
		take_item(448);
		activate_hidden_group(15);
		set_character_pose(21,14);
		force_instant_terrain_redraw();
		set_flag(4,1,5);
		}

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

	reset_dialog();
	add_dialog_str(0,"As you approach the entrance to the cathedral, you're struck by how plain the doors look. There's really nothing special about them, which is a little odd.",0);
	add_dialog_str(1,"Then, you think to look up, and remember that this building's power doesn't come from it being opulently-designed. It's just huge.",0);
	run_dialog(1);
	set_flag(1,3,1);

break;

beginstate 26; //main entrance, door trigger

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

	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	add_dialog_str(0,"A very cold breeze blows out of the building. But then again, it is the Cathedral of Winds, so you brush aside the creepy feeling it gives you.",0);
	run_dialog(1);
	set_flag(1,3,2);

break;

beginstate 27; //main entrance, stepping inside

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

	reset_dialog();
	add_dialog_str(0,"Stepping into the cathedral for the first time, you are awed by the scale of everything. Huge staircases ascend on either side of you, and immense arches rise up to support the ceiling.",0);
	add_dialog_str(1,"As with the gardens, the interior of the building is immaculately clean, with surprisingly nice carpets and tiles.",0);
	run_dialog(1);
	set_flag(1,3,3);

break;

beginstate 29;  //keep party out of the rest of the cathedral 
		//before talking with Father Richardson

	if(get_flag(1,4) == 0) {
		block_entry(1);
		message_dialog("Hm... you should probably go speak with Father Richardson before you go and explore the rest of the cathedral.","Perhaps you should look for the sanctuary.");
		}

break;

beginstate 30; //entering the sanctuary

	if((get_flag(50,0) == 1) && (get_flag(11,2) == 0)) {
		reset_dialog();
		add_dialog_str(0,"Much to your relief, the air feels warmer and cleaner in here than in the rest of the cathedral. It's probably due to the presence of Father Richardson.",0);
		add_dialog_str(1,"Interestingly enough, it looks like all the priests have taken shelter in here. The irony of a cathedral full of priests hiding from a few angry ghosts is almost too much for you to bear.",0);
		add_dialog_str(2,"In any case, you hope that Father Richardson can shed some light on what has happened.",0);
		run_dialog(1);
		set_flag(11,2,1);
		}

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

	reset_dialog();
	add_dialog_str(0,"As you step through this doorway, you're almost floored by the sheer power of the room. The architect clearly knew what he was doing.",0);
	add_dialog_str(1,"The smooth stone walls rise up to a gracefully-arched ceiling, from which dozens of lanterns hang. There are also balconies built into the east and west walls of the room.",0);
	add_dialog_str(2,"From here, you can just see a raised platform at the north end of the room, with a large stained glass window set above it. A robed figure is seated on the platform, and it beckons you forward.",0);
	if(get_flag(1,3) <= 3)
		add_dialog_str(3,"This is almost definitely the sanctuary.",0);
	run_dialog(1);
	set_flag(1,3,5);

break;

beginstate 40; //kitchen dialog

	//keep party out of the rest of the cathedral 
	//before talking with Father Richardson
	if(get_flag(1,4) == 0) {
		block_entry(1);
		message_dialog("Hm... you should probably go speak with Father Richardson before you go and explore the rest of the cathedral.","Perhaps you should look for the sanctuary.");
		end();
		}

	if(get_flag(1,18) > 1)
		end();

	reset_dialog();
	if(get_flag(1,18) == 1)
		add_dialog_str(0,"Nothing's cooking in the kitchen right now, which is a bit of a disappointment. There's nothing adventurers like more than a free meal.",0);
	else
		add_dialog_str(1,"This appears to be a kitchen. Nothing's cooking right now, which is a bit of a disappointment. There's nothing adventurers like more than a free meal.",0);
	run_dialog(1);
	set_flag(1,18,2);


break;

beginstate 50; //cloakroom dialog

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

	reset_dialog();
	if(get_flag(1,11) == 0)
		add_dialog_str(0,"This seems to be a cloak room. It probably gets a lot more use when the cathedral is holding services, but for now it is mostly empty.",0);
	else
		add_dialog_str(0,"This cloak room probably gets a lot more use when the cathedral is holding services, but for now it is mostly empty.",0);
	run_dialog(1);
	set_flag(1,11,2);

break;

beginstate 55; //small shrine dialog

	

	if(get_flag(1,12) != 0) {
		if((get_flag(11,6) == 0) && (get_flag(50,0) == 1) && (get_flag(50,1) == 0))
			set_state_continue(80);
		end();
		}

	reset_dialog();
	add_dialog_str(0,"This room contains a small shrine, with several offerings set next to it. The heavy smell of incense quickly fills your lungs.",0);
	run_dialog(1);
	set_flag(1,12,1);

break;

beginstate 60; //statues dialog

	if(get_flag(1,13) != 0)
		end();

	reset_dialog();
	add_dialog_str(0,"This area seems to be a monument of sorts, with a few expertly-carved statues set beside a pool of water.",0);
	run_dialog(1);
	set_flag(1,13,1);

break;

beginstate 61; //nw statue

	reset_dialog();
	add_dialog_str(0,"This statue depicts a woman who looks to be half-warrior, half-priestess. In a word, she looks fierce. The plaque on the statue reads:",0);
	add_dialog_str(1,"Mother Eleanor Lichter, 385-444. Founder of the Church of the Winds. May her spirit ride the winds forever.",10);
	run_dialog(1);

break;

beginstate 62; //ne statue

	reset_dialog();
	add_dialog_str(0,"This statue depicts a man gazing thoughtfully into the distance. He appears to be a moderately wealthy merchant.",0);
	add_dialog_str(1,"The plaque on the statue reads:",0);
	add_dialog_str(2,"Robert Paulsen, 484-520. Architect of the cathedral. May his life's work be remembered forever.",10);
	if((get_flag(8,12) == 1) && (get_flag(11,10) < 2))
		add_dialog_str(3,"This must be the dead architect that you met in the catacombs. He'd probably want to know about this.",0);
	if(get_flag(11,10) == 0)
		set_flag(11,10,1);
	run_dialog(1);

break;

beginstate 63; //se statue

	reset_dialog();
	add_dialog_str(0,"This statue depicts an elderly priest leaning on a twisted staff. The plaque on the statue reads:",0);
	add_dialog_str(1,"Matthew Culvertson, 507-563. First Father of the Cathedral.",10);
	run_dialog(1);

break;

beginstate 64; //sw statue

	reset_dialog();
	add_dialog_str(0,"This statue of an archmage bears a simple, yet cryptic inscription:",0);
	add_dialog_str(1,"To Micael, who inspired all of this.",10);
	run_dialog(1);

break;


beginstate 70; //start the guard quest

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

	force_instant_terrain_redraw();
	pause(2);

	reset_dialog();
	add_dialog_str(0,"You're a little startled to find a guard right behind this door, but it's a safe bet that the guard was startled more by you. He leaps back and clutches his chest before calming himself down.",0);
	if(party_size() == 1)
		add_dialog_str(1,"_Friend, I need your help. My friend Jack and I were attacked by ghosts when we were guarding the third floor, and they got him._",0);
	else
		add_dialog_str(1,"_Friends, I need your help. My friend Jack and I were attacked by ghosts when we were guarding the third floor, and they got him._",0);
	add_dialog_str(2,"_They just came up through the floor and grabbed him,_ the guard says, verging on hysterics.",0);
	add_dialog_str(3,"You calm him down and tell him that you'll help his friend, if only because it will shut him up. The guard turns and scurries away.",0);
	add_dialog_str(4,"_I'll be outside!_ he yells over his shoulder.",0);
	run_dialog(1);

	toggle_quest(5,1);

	erase_char(13);
	force_instant_terrain_redraw();
	set_flag(4,1,1); 

break;


beginstate 80; //surprise! ghost priest!

	if((get_flag(11,6) > 0) || (get_flag(50,0) == 0) || (get_flag(50,1) > 0))
		end();

	if(party_can_see_loc(char_loc_x(14), char_loc_y(14)) != 1)
		end();

	force_instant_terrain_redraw();
	pause(3);

	reset_dialog();
	add_dialog_str(0,"You're shocked to find a ghost this close to the sanctuary, but there's one standing facing the small altar in this room. It seems like it hasn't noticed you yet.",0);
	add_dialog_str(1,"Whatever the reason, you have a chance to surprise it. And since it looks like the ghost of a priest, surprising it would make disrupting it a lot easier.",0);
	add_dialog_str(2,"Do you attack?",0);
	add_dialog_choice(0,"Charge!");
	add_dialog_choice(1,"Wait.");
	choice = run_dialog(1);

	if(choice == 1) {
		reset_dialog();
		add_dialog_str(0,"The spirit whirls around and begins to chant. Judging from that response, it seems as though your ambush failed.",0);
		run_dialog(1);
		set_name(14,"Ghost Priest");
		set_flag(11,6,1);
		set_attitude(14,10);
		}
	else {
		reset_dialog();
		add_dialog_str(0,"You wait for what feels like an eternity. Then, the ghost turns and beckons you forward, speaking with a clear, calm voice.",0);
		if(party_size() == 1)
			add_dialog_str(1,"_Come, friend. My name is Grimsley, and I believe we have much to discuss._",0);
		else
			add_dialog_str(1,"_Come, friends. My name is Grimsley, and I believe we have much to discuss._",0);
		run_dialog(1);
		set_flag(11,6,2);
		}	

break;


beginstate 100; //leaving, for the last time

	if((get_flag(1,4) != 3) || (get_flag(11,16) == 1))
		end();

	set_flag(11,16,1);
	pause(15);
	set_character_facing(1000,0);
	force_instant_terrain_redraw();
	
	pause(2);

	reset_dialog();
	add_dialog_str(0,"As you depart the cathedral's grounds for hopefully the last time, you turn to take one last look at the cathedral.",0);
	add_dialog_str(1,"You're a little surprised to see a dervish approaching you from the cathedral, and it's none other than the leader of the building's official guards.",0);
	run_dialog(1);

	pause(5);

	activate_hidden_group(19);
	force_instant_terrain_redraw();
	pause(3);
	force_view_center(32,55);
	force_instant_terrain_redraw();
	pause(2);
	play_sound(-49);
	relocate_character(22,32,50);
	force_instant_terrain_redraw();
	pause(4);
	play_sound(-50);
	relocate_character(22,32,51);
	force_instant_terrain_redraw();
	pause(4);
	play_sound(-49);
	relocate_character(22,32,52);
	force_instant_terrain_redraw();
	pause(4);
	text_bubble_on_char(22,"Stop!");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	reset_dialog();
	add_dialog_str(0,"Now that you get a better look at the Dervish, you can tell that he is obviously very old. Long, wispy gray hair spills out from under his helmet, and his stride is a little pained.",0);
	add_dialog_str(1,"Still, he carries himself with the authority his position demands, which really is all the Empire seems to demand. He clears his throat loudly.",0);
	run_dialog(1);
	pause(3);
	text_bubble_on_char(25,"On behalf of the 35th Brigade...");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	pause(2);
	if(get_flag(50,1) == 2) {
		text_bubble_on_char(22,"Thank you for what you've done.");
		force_instant_terrain_redraw();
		pause(22);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		pause(5);
		text_bubble_on_char(22,"That is all. You're dismissed.");
		force_instant_terrain_redraw();
		pause(16);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		if(get_flag(11,7) == 0)
			add_dialog_str(0,"Well, that was odd. You can't be certain what the Dervish is talking about, but you're starting to get the feeling that you should leave before this all gets any weirder.",0);
		else
			add_dialog_str(0,"Wait, wasn't the 35th the ghosts' brigade? Perhaps you weren't the first person the ghosts tried to contact... either way, this is getting too weird for you.",0);
		add_dialog_str(1,"The Dervish watches you calmly as you go, and a cool breeze follows you up the road.",0);
		run_dialog(1);
		end();
		}
	else {
		text_bubble_on_char(22,"You're under arrest.");
		force_instant_terrain_redraw();
		pause(18);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		pause(1);
		reset_dialog();
		add_dialog_str(0,"You stand there for a moment, stunned. The rest of the cathedral's guards start to show up, slowly edging towards you. For a moment, you contemplate running or fighting.",0);
		add_dialog_str(1,"If you run, then you'll have a hard time escaping Empire patrols, unless you want to eke out an existence in the deeper forests of Aizo. Not a very good option.",0);
		add_dialog_str(2,"Fighting seems like an even worse option. Even if you win, you still have to run, and your eventual punishment would be even worse. Realizing this, you give up.",0);
		add_dialog_str(3,"You are bound and force-marched to Barton, the nearest large city.",0);
		add_dialog_choice(0,"Um...");
		run_dialog(1);

		force_view_center(0,0);
		force_instant_terrain_redraw();
		pause(5);
		block_entry(1);
		reset_dialog();
		if(party_size() == 1)
			add_dialog_str(0,"The day of the trial comes, and you find yourself standing in front of an imperial governor, wondering what will become of you.",0);
		else
			add_dialog_str(0,"The day of the trial comes, and you find yourselves standing in front of an imperial governor, wondering what will become of you.",0);
		add_dialog_str(1,"Luckily, the governor seems just as confused by the proceedings as you are. Perhaps he's inept, or perhaps the case really is full of hot air, but you are judged innocent.",0);
		add_dialog_str(2,"So you completed your quest here, and escaped unscathed. However, you will always remember the hateful look on the Dervish's face when the verdict was announced.",0);
		add_dialog_choice(0,"THE END");
		run_dialog(1);
		end_scenario(1);

		}
	

break;