begintownscript;

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

body;

beginstate 0; //INIT_STATE
	set_crime_tolerance(2);
	spire = get_flag(7,2);
	set_flag(7,2,0);

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

break;


beginstate 1; //EXIT_STATE

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

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, 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) {
		set_flag(6,3,1);
		move_to_new_town(6,24,32);
		}
	else
		block_entry(1);

break;

beginstate 11; //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) {
		set_flag(6,3,2);
		move_to_new_town(6,40,32);
		}
	else
		block_entry(1);

break;


beginstate 20; //west spire entrance state

	if(get_flag(7,3) == 1) {
		force_instant_terrain_redraw();
		pause(3);
		activate_hidden_group(4);
		set_character_pose(7,14);
		put_boom_on_char(7,5,0);
		run_animation_sound(52);
		play_sound(-165);
		pause(1);
		set_character_pose(7,13);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(7,12);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(7,11);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(7,0);
		force_instant_terrain_redraw();
		pause(3);
		print_str_color("The soldier reappears!",2);
		change_char_health(7,(get_max_health(7) * -1));
		change_char_health(7,get_flag(7,7));
		}
	if(get_flag(7,3) > 0) {
		set_floor(27,31,132);
		set_floor(26,31,132);
		set_floor(25,32,132);
		set_floor(25,33,132);
		set_floor(26,33,132);
		set_floor(27,33,132);
		}

	if(get_flag(7,0) == 0) {
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		if(get_flag(7,0) == 1)
			add_dialog_str(0,"This room looks pretty much identical to the one on the east side of the building, except with a bookshelf. There's even a box with similar carvings on it.",0);
		else
			add_dialog_str(0,"This room looks to be empty of anything other than junk, much to your relief. The only really interesting feature is an ornately carved wooden box.",0);
		add_dialog_str(1,"With any luck, there's a control crystal in it.",0);
		run_dialog(1);
		set_flag(7,0,1);
		}

break;


beginstate 21; //east spire entrance state

	if(get_flag(7,4) == 1) {
		force_instant_terrain_redraw();
		pause(3);
		activate_hidden_group(4);
		set_character_pose(6,14);
		put_boom_on_char(6,5,0);
		run_animation_sound(52);
		play_sound(-165);
		pause(1);
		set_character_pose(6,13);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(6,12);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(6,11);
		force_instant_terrain_redraw();
		pause(1);
		set_character_pose(6,0);
		force_instant_terrain_redraw();
		pause(3);
		print_str_color("The soldier reappears!",2);
		change_char_health(6,(get_max_health(6) * -1));
		change_char_health(6,get_flag(7,8));
		}
	if(get_flag(7,4) > 0) {
		set_floor(27,31,132);
		set_floor(26,31,132);
		set_floor(25,32,132);
		set_floor(25,33,132);
		set_floor(26,33,132);
		set_floor(27,33,132);
		}

	if(get_flag(7,1) == 0) {
		force_instant_terrain_redraw();
		reset_dialog();
		if(get_flag(7,0) == 1)
			add_dialog_str(0,"This room looks pretty much identical to the one on the west side of the building, except with no bookshelf. There's even a box with similar carvings on it.",0);
		else
			add_dialog_str(0,"This room looks to be empty of anything other than junk, much to your relief. The only really interesting feature is an ornately carved wooden box.",0);
		add_dialog_str(1,"With any luck, there's a control crystal in it.",0);
		run_dialog(1);
		set_flag(7,1,1);
		}

break;

beginstate 30; //approach west box

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

	force_instant_terrain_redraw();
	pause(3);
	activate_hidden_group(4);
	set_character_pose(7,14);
	put_boom_on_char(7,5,0);
	run_animation_sound(52);
	play_sound(-165);
	pause(1);
	set_character_pose(7,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(7,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(7,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(7,0);
	force_instant_terrain_redraw();
	pause(3);
	set_floor(27,31,132);
	set_floor(26,31,132);
	set_floor(25,32,132);
	set_floor(25,33,132);
	set_floor(26,33,132);
	set_floor(27,33,132);

	reset_dialog();
	if(get_flag(7,4) == 0)
		add_dialog_str(0,"The shade of a soldier appears, snarling and waving a sharp blade. Somehow, you're not surprised... this seemed too easy.",0);
	else
		add_dialog_str(0,"A shade very similar to the one from the other spire appears. However, this one seems to be right-handed.",0);
	run_dialog(1);
	set_flag(7,3,1);

break;

beginstate 31; //west box

	if(get_flag(7,5) == 2)
		end();

	if(char_ok(7) == 1) {
		reset_dialog();
		add_dialog_str(0,"You try to get at the box, but the presence of the angry ghost is preventing that. You should probably deal with that first.",0);
		run_dialog(1);
		end();
		}

	reset_dialog();
	if(get_flag(7,6) >= 1) {
		add_dialog_str(0,"This box is practically identical to the one you found in the other spire, with an identical crystal inside it. You can't help but wonder why the priests chose to spread out their crystals over two spires.",0);
		add_dialog_str(1,"Anyway, the crystal is here. Do you take it?",0);
		}
	else if(get_flag(7,5) == 0) {
		add_dialog_str(0,"This box is very ornately carved, and you can practically feel magic emanating from it.",0);
		add_dialog_str(1,"Inside, you find a complex-looking greenish crystal, probably one of the control crystals Felix told you about. Do you take the crystal?",0);
		set_flag(7,5,1);
		}
	else
		add_dialog_str(1,"The crystal is still inside the box. Do you take it?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);

	if(choice == 2) {
		reward_give(446);
		message_dialog("You carefully remove the crystal and pocket it. It feels warm to the touch, and you think you see swirling patterns inside it.","");
		set_flag(7,5,2);
		}


break;


beginstate 32; //killing the west guard

	set_flag(7,3,2);
	reset_dialog();
	add_dialog_str(0,"As you strike the final blow, the guard howls, _The Empire will-_",0);
	add_dialog_str(1,"That's as far as he gets. Now, you're free to sift through the contents of this room.",0);
	run_dialog(1);

break;


beginstate 40; //approach east box

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

	force_instant_terrain_redraw();
	pause(3);
	activate_hidden_group(3);
	set_character_pose(6,14);
	put_boom_on_char(6,5,0);
	run_animation_sound(52);
	play_sound(-165);
	pause(1);
	set_character_pose(6,13);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(6,12);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(6,11);
	force_instant_terrain_redraw();
	pause(1);
	set_character_pose(6,0);
	force_instant_terrain_redraw();
	pause(3);
	set_floor(37,31,132);
	set_floor(38,31,132);
	set_floor(39,31,132);
	set_floor(39,32,132);
	set_floor(39,33,132);
	set_floor(37,33,132);

	reset_dialog();
	if(get_flag(7,3) == 0)
		add_dialog_str(0,"The shade of a soldier appears, snarling and waving a sharp blade. Somehow, you're not surprised... this seemed too easy.",0);
	else
		add_dialog_str(0,"A shade very similar to the one from the other spire appears. However, this one seems to be left-handed.",0);
	run_dialog(1);
	set_flag(7,4,1);

break;

beginstate 41; //east box

	if(get_flag(7,6) == 2)
		end();

	if(char_ok(6) == 1) {
		reset_dialog();
		add_dialog_str(0,"You try to get at the box, but the presence of the angry ghost is preventing that. You should probably deal with that first.",0);
		run_dialog(1);
		end();
		}

	reset_dialog();
	if(get_flag(7,5) >= 1) {
		add_dialog_str(0,"This box is practically identical to the one you found in the other spire, with an identical crystal inside it. You can't help but wonder why the priests chose to spread out their crystals over two spires.",0);
		add_dialog_str(1,"Anyway, the crystal is here. Do you take it?",0);
		}
	else if(get_flag(7,6) == 0) {
		add_dialog_str(0,"This box is very ornately carved, and you can practically feel magic emanating from it.",0);
		add_dialog_str(1,"Inside, you find a complex-looking greenish crystal, probably one of the control crystals Felix told you about. Do you take the crystal?",0);
		set_flag(7,6,1);
		}
	else
		add_dialog_str(1,"The crystal is still inside the box. Do you take it?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);

	if(choice == 2) {
		reward_give(446);
		message_dialog("You carefully remove the crystal and pocket it. It feels warm to the touch, and you think you see swirling patterns inside it.","");
		set_flag(7,6,2);
		}


break;

beginstate 42; //killing the east guard

	set_flag(7,4,2);
	reset_dialog();
	add_dialog_str(0,"As you strike the final blow, the guard howls, _The worms will-_",0);
	add_dialog_str(1,"That's as far as he gets. Now, you're free to sift through the contents of this room.",0);
	run_dialog(1);

break;