begintownscript;

variables;
short choice = 0;
short pc_counter = 0;
short lead_char = 0;
short spire = 0;
short corrupted = 0;
short controlc = 0;
short startx, starty = 0;
short crystuser = 0;
string crystal;

body;

beginstate 0; //INIT_STATE
	set_crime_tolerance(2);
	if(get_flag(4,1) == 0)
		set_terrain(35,47,0);

	spire = get_flag(6,3);
	set_flag(6,3,0);


	if(spire == 1)
		set_state_continue(20);
	else if(spire == 2)
		set_state_continue(21);
	else if(spire == 3)
		set_state_continue(22);

break;


beginstate 1; //EXIT_STATE


	if(char_ok(8) == 1)
		set_flag(6,8,get_health(8));
	if(char_ok(9) == 1)
		set_flag(6,7,get_health(9));
	if(char_ok(7) == 1)
		set_flag(6,9,get_health(7));

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; //trapdoor down, central

	reset_dialog();
	add_dialog_str(0,"There's a trapdoor here, with a ladder underneath it. 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(5,32,38);
	else
		block_entry(1);

break;

beginstate 11; //trapdoor down, west

	reset_dialog();
	add_dialog_str(0,"There's a trapdoor here, with a ladder underneath it. 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(5,25,30);
	else
		block_entry(1);

break;

beginstate 12; //trapdoor down, east

	reset_dialog();
	add_dialog_str(0,"There's a trapdoor here, with a ladder underneath it. 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(5,39,30);
	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(7,2,1);
		move_to_new_town(7,24,32);
		}
	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(7,2,2);
		move_to_new_town(7,40,32);
		}
	else
		block_entry(1);

break;


beginstate 20; // west spire entrance state

	if((get_flag(6,0) != 0) && (get_flag(50,1) == 0)) {
		if(get_flag(6,4) < 1) {
			activate_hidden_group(1);
			change_char_health(9,(get_max_health(9) * -1));
			change_char_health(9,get_flag(6,7));
			}
		if(get_flag(6,6) < 1) {
			set_terrain(23,30,0);
			activate_hidden_group(2);
			change_char_health(8,(get_max_health(8) * -1));
			change_char_health(8,get_flag(6,8));
			put_stain_on_space(23,30,6);
			}
		else {
			set_terrain(23,30,0);
			put_stain_on_space(23,30,6);
			}
		force_instant_terrain_redraw();
		}

	if(get_flag(6,0) == 0) {
		if(get_flag(50,1) == 0)
			activate_hidden_group(1);
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"You emerge from the trapdoor in a fairly cluttered storage room. Bookshelves, sacks, boxes... there's a little bit of everything in here, including a fairly generic-looking statue of a warrior.",0);
		add_dialog_str(1," ",0);
		if(get_flag(50,1) == 0)
			add_dialog_str(2,"However, the ghost in the room catches your eye first. Nothing seems odd about it, though does seem fairly angry.",0);
		else {
			add_dialog_str(2,"Thankfully, there are no ghosts that you can see.",0);
			set_flag(6,0,1);
			run_dialog(1);
			end();
			}
		run_dialog(1);

		pause(3);
		set_character_pose(9,2);
		force_instant_terrain_redraw();
		pause(1);
		text_bubble_on_char(9,"Arise!");
		force_instant_terrain_redraw();
		pause(10);
		set_character_pose(9,1);
		force_instant_terrain_redraw();
		put_jagged_zap(25,34,23,30,3);
		set_terrain(23,30,0);
		activate_hidden_group(2);
		put_boom_on_char(8,7,0);
		force_instant_terrain_redraw();
		run_animation_sound(43);

		pause(3);
		erase_text_bubbles();
		set_character_pose(9,0);
		force_instant_terrain_redraw();
		reset_dialog();
		add_dialog_str(0,"And then, for some reason, the statue begins to move, sending flecks of stone flying everywhere. Needless to say, you're getting a very bad feeling about this.",0);
		run_dialog(1);

		pause(3);
		play_sound(-108);
		set_character_pose(8,1);
		set_character_facing(8,2);
		put_stain_on_space(23,30,6);
		force_instant_terrain_redraw();
		pause(1);
		set_character_facing(8,4);
		force_instant_terrain_redraw();
		pause(1);
		set_character_facing(8,6);
		force_instant_terrain_redraw();
		pause(1);
		set_character_facing(8,4);
		force_instant_terrain_redraw();
		pause(1);
		set_character_facing(8,2);
		force_instant_terrain_redraw();
		pause(1);
		set_character_facing(8,0);
		set_character_pose(8,0);
		force_instant_terrain_redraw();
		pause(1);

		set_flag(6,0,1);
		}


break;

beginstate 21; // east spire entrance state

	if((get_flag(6,5) == 0) && (get_flag(50,1) == 0)) {
		activate_hidden_group(3);
		if(get_flag(6,1) == 1) {
			change_char_health(7,(get_max_health(7) * -1));
			change_char_health(7,get_flag(6,9));
			}
		}

	force_instant_terrain_redraw();

	if(get_flag(6,1) == 0) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"The first thing you notice about this room is the scent of slightly stale clothing, probably due to the piles of robes and other clothing heaped about the tables.",0);
		if(get_flag(50,1) == 0) {
			add_dialog_str(1,"There's also a few ancient-looking mops and brooms, and a very vicious-looking ghost staring directly at you. The soldier draws a jagged longsword and lets loose a massive yell, one aimed directly at you.",0);
		run_dialog(1);
		play_sound(-48);
			}
		else {
			add_dialog_str(1,"There's also a few ancient-looking mops and brooms. But thankfully, no ghosts.",0);
			run_dialog(1);
			}
		set_flag(6,1,1);
		}

break;

beginstate 22; // south spire entrance state

	if((get_flag(50,1) != 0) && (get_flag(6,2) == 1)) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		if(get_flag(50,1) == 1)
			add_dialog_str(0,"Returning to the generator chamber, you can feel the intense disruption energy the machine radiates as a prickly feeling on your skin. To any undead, this would be lethal.",0);
		else {
			add_dialog_str(0,"As you return to the generator room, you can feel the intense bursts of energy the machine radiates.",0);
			add_dialog_str(1,"You wonder how long it'll be until the priests realize what the machine is actually doing.",0);
			}
		run_dialog(1);
		set_flag(6,2,2);
		}

	if(get_flag(6,2) == 0) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"So this is it. The source of this cathedral's problem is a complex-looking machine positioned in the center of a very precise and very permanent magic circle. Two braziers similar to what you saw in the catacombs light the chamber.",0);
		add_dialog_str(1,"Thankfully, it doesn't look like any ghosts are here to interfere with your plans. But it's not all that surprising given the huge amounts of magic you feel in the room.",0);
		if((has_item(446) == 0) && (has_item(447) == 0))
			add_dialog_str(2,"Of course, without a control crystal, you doubt that there's much you can do here.",0);
		run_dialog(1);
		set_flag(6,2,1);
		}

break;



beginstate 40; //approach the machine

	if(get_flag(50,1) != 0) {
		reset_dialog();
		if(get_flag(50,1) == 1)
			add_dialog_str(0,"The field generator seems to be working properly again. But with what happened before, you don't really feel comfortable approaching it again.",0);
		else
			add_dialog_str(0,"The field generator seems to be _working_ now. But with what happened before, you don't really feel comfortable approaching it again.",0);
		run_dialog(1);
		block_entry(1);
		end();
		}

	corrupted = has_item(447);
	controlc = has_item(446);

	if(get_flag(6,10) == 1) {
		reset_dialog();
		if((controlc == 0) && (corrupted == 0)) {
			add_dialog_str(0,"The machine is still here, occasionally discharging a bit of energy in bright blue arcs. However, since you don't have a crystal, you can't do much with it.",0);
			run_dialog(1);
			block_entry(1);
			end();
			}
		add_dialog_str(0,"The machine is still here, occasionally discharging a bit of energy in bright blue arcs. What do you do?",0);
		add_dialog_choice(0,"Nothing.");
		if(controlc == 1)
			add_dialog_choice(1,"Insert a new control crystal.");
		if(corrupted == 1)
			add_dialog_choice(2,"Insert the corrupted crystal.");
		choice = run_dialog(1);
		if(choice == 1)
			end();
		else if(choice == 2) {
			if(controlc == 0)
				set_state_continue(60);
			else
				set_state_continue(50);
			}
		else if(choice == 3)
			set_state_continue(60);
		}

	if(get_flag(6,10) == 0) {
		set_flag(6,10,1);
		reset_dialog();
		add_dialog_str(0,"Up this close, you can feel the raw magical energy being radiated by the machine. It's a little disorienting, but you expect that it's worse for the ghosts.",0);
		add_dialog_str(1,"Every so often, a small bolt of bright blue energy arcs from the machine to the floor. You're a little worried about how this will affect you when you try to fix the machine.",0);
		if((controlc == 0) && (corrupted == 0)) {
			add_dialog_str(2,"With magical machinery this complex and powerful, it'd be suicide to try and operate it improperly. Since you lack a fresh crystal for the machine, you decide to back off.",0);
			run_dialog(1);
			block_entry(1);
			end();
			}
		if(get_flag(11,9) == 0)
			add_dialog_str(2,"Now is your chance to finally lay these ghosts to rest. What do you do?",0);
		else
			add_dialog_str(2,"Now is your chance to finally lay these ghosts to rest... or to free them. What do you do?",0);
		add_dialog_choice(0,"Nothing");
		if(controlc == 1)
			add_dialog_choice(1,"Insert a new control crystal.");
		if(corrupted == 1)
			add_dialog_choice(2,"Insert the corrupted crystal.");
		choice = run_dialog(1);
		if(choice == 1) {
			block_entry(1);
			end();
			}
		else if(choice == 2) {
			if(controlc == 0)
				set_state_continue(60);
			else
				set_state_continue(50);
			}
		else if(choice == 3)
			set_state_continue(60);
		}

break;



beginstate 50; //Suppress the ghosts!!!

	crystuser = 0;
	pc_counter = 0;
	if(char_has_item(0,446) == 1)
		crystuser = 0;
	else if(char_has_item(1,446) == 1)
		crystuser = 1;
	else if(char_has_item(2,446) == 1)
		crystuser = 2;
	else if(char_has_item(3,446) == 1)
		crystuser = 3;
	reset_dialog();
	clear_buffer();
	append_char_name(crystuser);
	append_string(" steps up to the machine and deftly switches the old crystal for a fresh control crystal, and the machine roars to life...");
	get_buffer_text(crystal);
	add_dialog_str(0,crystal,0);
	clear_buffer();
	append_string("A few hairs on the back of ");
	append_char_name(crystuser);
	if(party_size() == 1)
		append_string("'s neck start to stand on end, and you start edging toward the trapdoor.");
	else
		append_string("'s neck start to stand on end, as you all begin edging toward the trapdoor.");
	get_buffer_text(crystal);
	add_dialog_str(1,crystal,0);
	force_view_center(32,39);
	relocate_character(0,10,10);
	relocate_character(1,11,10);
	relocate_character(2,12,10);
	relocate_character(3,13,10);
	if(crystuser == 0) {
		relocate_character(1,31,38);
		relocate_character(2,32,38);
		relocate_character(3,33,38);
		}
	else if(crystuser == 1) {
		relocate_character(0,31,38);
		relocate_character(2,32,38);
		relocate_character(3,33,38);
		}
	else if(crystuser == 2) {
		relocate_character(0,31,38);
		relocate_character(1,32,38);
		relocate_character(3,33,38);
		}
	else if(crystuser == 3) {
		relocate_character(0,31,38);
		relocate_character(1,32,38);
		relocate_character(2,33,38);
		}
	relocate_character(crystuser,32,39);
	set_character_facing(1000,4);
	run_dialog(1);
	force_instant_terrain_redraw();
	pause(3);
	take_item(446);
	play_sound(126);
	pause(5);
	set_terrain(32,40,384);
	put_sparkles_on_space(32,40,9,6);
	run_animation_sound(12);

	pause(2);
	put_boom_on_space(32,40,2,0);
	run_animation_sound(24);

	pause(3);
	reset_dialog();
	add_dialog_str(0,"A blinding flash of blue light fills the room, and you can feel the machine's energy building. The air is practically crackling with magic.",0);
	add_dialog_str(1,"And for some reason, you can't shake the feeling that this is a bad place to stand right now.",0);
	run_dialog(1);

	pause(3);

	put_sparkles_on_space(31,39,0,4);
	put_sparkles_on_space(32,39,0,4);
	put_sparkles_on_space(33,39,0,4);
	put_sparkles_on_space(31,40,0,4);
	put_sparkles_on_space(33,40,0,4);
	put_sparkles_on_space(31,41,0,4);
	put_sparkles_on_space(32,41,0,4);
	put_sparkles_on_space(33,41,0,4);
	run_animation_sound(54);

	put_sparkles_on_space(31,42,0,4);
	put_sparkles_on_space(32,42,0,4);
	put_sparkles_on_space(33,42,0,4);
	put_sparkles_on_space(30,38,0,4);
	put_sparkles_on_space(31,38,0,4);
	put_sparkles_on_space(32,38,0,4);
	put_sparkles_on_space(33,38,0,4);
	put_sparkles_on_space(34,38,0,4);
	put_sparkles_on_space(30,39,0,4);
	put_sparkles_on_space(30,40,0,4);
	put_sparkles_on_space(30,41,0,4);
	put_sparkles_on_space(34,39,0,4);
	put_sparkles_on_space(34,40,0,4);
	put_sparkles_on_space(34,41,0,4);
	run_animation_sound(54);

	put_sparkles_on_space(29,38,0,4);
	put_sparkles_on_space(29,39,0,4);
	put_sparkles_on_space(29,40,0,4);
	put_sparkles_on_space(29,41,0,4);
	put_sparkles_on_space(35,38,0,4);
	put_sparkles_on_space(35,39,0,4);
	put_sparkles_on_space(35,40,0,4);
	put_sparkles_on_space(35,41,0,4);
	put_sparkles_on_space(31,37,0,4);
	put_sparkles_on_space(32,37,0,4);
	put_sparkles_on_space(33,37,0,4);
	run_animation_sound(54);

	put_sparkles_on_space(28,38,0,4);
	put_sparkles_on_space(28,39,0,4);
	put_sparkles_on_space(28,40,0,4);
	put_sparkles_on_space(28,41,0,4);
	put_sparkles_on_space(36,38,0,4);
	put_sparkles_on_space(36,39,0,4);
	put_sparkles_on_space(36,40,0,4);
	put_sparkles_on_space(36,41,0,4);
	run_animation_sound(54);

	pause(3);
	reset_dialog();
	add_dialog_str(0,"As the freshly-generated field washes over you, your vision starts to fade. It slowly gets harder to think, until you forget how to keep yourself upright.",0);
	add_dialog_str(1,"You collapse on the cold floor, and the world goes black.",0);
	run_dialog(1);
	force_view_center(0,0);
	force_instant_terrain_redraw();
	play_sound(-31);

	pause(15);

	set_flag(50,1,1);
	move_to_new_town(5,23,33);

break;


beginstate 60; //Free the ghosts!!!

	crystuser = 0;
	pc_counter = 0;
	if(char_has_item(0,447) == 1)
		crystuser = 0;
	else if(char_has_item(1,447) == 1)
		crystuser = 1;
	else if(char_has_item(2,447) == 1)
		crystuser = 2;
	else if(char_has_item(3,447) == 1)
		crystuser = 3;
	reset_dialog();
	clear_buffer();
	append_char_name(crystuser);
	append_string(" steps up to the machine and deftly switches the old crystal for a fresh control crystal, and the machine roars to life...");	get_buffer_text(crystal);
	add_dialog_str(0,crystal,0);
	if(party_size() == 1)
		add_dialog_str(1,"You just hope that Grimsley's work is precise enough to not blow you up.",0);
	else
		add_dialog_str(1,"The rest of you silently edge towards the trapdoor, hoping that Grimsley's work was precise enough.",0);
	relocate_character(crystuser,32,39);
	force_view_center(32,39);
	if(crystuser == 0) {
		relocate_character(1,31,38);
		relocate_character(2,32,38);
		relocate_character(3,33,38);
		}
	else if(crystuser == 1) {
		relocate_character(0,31,38);
		relocate_character(2,32,38);
		relocate_character(3,33,38);
		}
	else if(crystuser == 2) {
		relocate_character(0,31,38);
		relocate_character(1,32,38);
		relocate_character(3,33,38);
		}
	else if(crystuser == 3) {
		relocate_character(0,31,38);
		relocate_character(1,32,38);
		relocate_character(2,33,38);
		}
	set_character_facing(1000,4);
	run_dialog(1);
	force_instant_terrain_redraw();
	pause(3);
	take_item(446);
	play_sound(126);
	pause(5);
	set_terrain(32,40,384);
	put_sparkles_on_space(32,40,9,6);
	run_animation_sound(12);

	pause(2);
	put_boom_on_space(32,40,0,0);
	run_animation_sound(25);

	pause(3);
	reset_dialog();
	add_dialog_str(0,"A blinding flash of red light fills the room, and you can feel the machine's energy building. The air is practically crackling with magic.",0);
	add_dialog_str(1,"And for some reason, you can't shake the feeling that this is a bad place to stand right now.",0);
	run_dialog(1);

	pause(3);

	put_sparkles_on_space(31,39,6,4);
	put_sparkles_on_space(32,39,6,4);
	put_sparkles_on_space(33,39,6,4);
	put_sparkles_on_space(31,40,6,4);
	put_sparkles_on_space(33,40,6,4);
	put_sparkles_on_space(31,41,6,4);
	put_sparkles_on_space(32,41,6,4);
	put_sparkles_on_space(33,41,6,4);
	run_animation_sound(54);

	put_sparkles_on_space(31,42,6,4);
	put_sparkles_on_space(32,42,6,4);
	put_sparkles_on_space(33,42,6,4);
	put_sparkles_on_space(30,38,6,4);
	put_sparkles_on_space(31,38,6,4);
	put_sparkles_on_space(32,38,6,4);
	put_sparkles_on_space(33,38,6,4);
	put_sparkles_on_space(34,38,6,4);
	put_sparkles_on_space(30,39,6,4);
	put_sparkles_on_space(30,40,6,4);
	put_sparkles_on_space(30,41,6,4);
	put_sparkles_on_space(34,39,6,4);
	put_sparkles_on_space(34,40,6,4);
	put_sparkles_on_space(34,41,6,4);
	run_animation_sound(54);

	put_sparkles_on_space(29,38,6,4);
	put_sparkles_on_space(29,39,6,4);
	put_sparkles_on_space(29,40,6,4);
	put_sparkles_on_space(29,41,6,4);
	put_sparkles_on_space(35,38,6,4);
	put_sparkles_on_space(35,39,6,4);
	put_sparkles_on_space(35,40,6,4);
	put_sparkles_on_space(35,41,6,4);
	put_sparkles_on_space(31,37,6,4);
	put_sparkles_on_space(32,37,6,4);
	put_sparkles_on_space(33,37,6,4);
	run_animation_sound(54);

	put_sparkles_on_space(28,38,6,4);
	put_sparkles_on_space(28,39,6,4);
	put_sparkles_on_space(28,40,6,4);
	put_sparkles_on_space(28,41,6,4);
	put_sparkles_on_space(36,38,6,4);
	put_sparkles_on_space(36,39,6,4);
	put_sparkles_on_space(36,40,6,4);
	put_sparkles_on_space(36,41,6,4);
	run_animation_sound(54);

	pause(3);
	reset_dialog();
	add_dialog_str(0,"As the product of Grimsley's experiment washes over you, your vision starts to fade. It slowly gets harder to think, until you forget how to keep yourself upright.",0);
	add_dialog_str(1,"You collapse on the cold floor, and the world goes black.",0);
	run_dialog(1);
	force_view_center(0,0);
	force_instant_terrain_redraw();
	play_sound(-31);

	pause(15);

	set_flag(50,1,2);
	move_to_new_town(5,23,33);


break;