begintownscript;

variables;
short choice = 0;

body;

beginstate 0; //INIT_STATE
	if(get_flag(4,0) != 1) {
		reset_dialog();
		add_dialog_str(0,"You descend into what is probably still part of the chitrach nest. You're not certain, though.",0);
		add_dialog_str(1,"Either way, your path seems to be pretty clear. That voice, whatever it is, is in control.",0);
		run_dialog(1);
		set_flag(4,0,1);
		}

break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE
	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
	if((get_flag(4,6) == 1) && (get_flag(4,7) == 0)) {
		set_attitude(6,11);
		reset_dialog();
		add_dialog_str(0,"Elrilok cackles,",0);
		add_dialog_str(1,"_What would play time be without friends?_",20);
		add_dialog_str(2,"You shudder.",0);
		run_dialog(1);
		pause(3);
		play_sound(119);
		activate_hidden_group(2);
		add_range_to_group(7,10,1);
		force_instant_terrain_redraw();
		relocate_character(6,20,23);
		put_effect_on_space(20,23,5,3,1);
		run_animation_sound(75);
		pause(1);
		set_terrain(19,23,125);
		force_instant_terrain_redraw();
		pause(1);
		set_flag(4,7,1);
		}
	if(get_flag(4,8) == 1) {
		message_dialog("Thankfully, you're able to move again. Elrilok doesn't seem to care.","");
		set_mobility(1000,1);
		set_flag(4,8,0);
		}
	if((char_ok(6) == 1) && (get_flag(4,7) >= 1) && (get_ran(1,1,4) == 4)) {
		put_sparkles_on_char(6,9,7);
		run_animation_sound(165);
		pause(3);
		reset_dialog();
		add_dialog_str(0,"_Whoops, I skipped you!_ Elrilok says joyfully. It makes your skin crawl, which is even more unpleasant when you find that you can't move again.",0);
		run_dialog(1);
		set_mobility(1000,0);
		set_flag(4,8,1);
		}
	if((get_flag(4,7) == 1) && (num_chars_in_group(1) == 0) && (char_ok(6) == 1)) {
		reset_dialog();
		add_dialog_str(0,"Elrilok seems to have finally noticed that you killed his _friends,_ but he doesn't seem too bothered by it.",0);
		if(party_size() == 1)
			add_dialog_str(1,"_You broke my friends! Now I get to break your soul!_",20);
		else
			add_dialog_str(1,"_You broke my friends! Now I get to break your souls!_",20);
		run_dialog(1);
		set_char_status(6,3,30,1,0);
		set_char_status(6,9,30,1,0);
		set_flag(4,7,2);
		}
break;

beginstate 8; //block the bottomless pit
	block_entry(1);
break;

beginstate 9; //slope back up
	if(is_combat() == 1) {
		block_entry(1);
		end();
		}
	choice = 0;
	reset_dialog();
	add_dialog_str(0,"The tunnel slopes up sharply here. Do you continue moving forward?",0);
	add_dialog_choice(0,"Yes.");
	add_dialog_choice(1,"No.");
	choice = run_dialog(1);
	if(choice == 2)
		block_entry(1);
	else
		move_to_new_town(3,9,24);
break;

beginstate 15; //seeing the mist barrier
	if((get_flag(4,9) == 1) && (get_flag(4,11) == 0) && (get_flag(4,3) == 1)) {
		reset_dialog();
		add_dialog_str(0,"Thankfully, the mist barrier has fallen, and you can see if this passage yields an exit.",0);
		run_dialog(1);
		set_flag(4,11,1);
		}
	if((get_flag(4,3) != 1) && (get_flag(4,9) == 0)) {
		reset_dialog();
		add_dialog_str(0,"Again, the voice has chosen for you. A wall of mist blocks your way.",0);
		run_dialog(1);
		set_flag(4,3,1);
		}
break;

beginstate 20; //go open the door
	if(get_flag(4,1) == 1)
		end();
	play_sound(75);
	reset_dialog();
	add_dialog_str(0,"The voice comes back into your head, softer this time. You're almost glad to hear it.",0);
	if(party_size() > 1)
		add_dialog_str(1,"_That's it. Now come to the door and open it. Your journey is near its end, children._",20);
	else
		add_dialog_str(1,"_That's it. Now come to the door and open it. Your journey is near its end, child._",20);
	run_dialog(1);
	set_flag(4,1,1);
break;

beginstate 25; //open the door
	if(get_flag(4,9) == 1)
		set_state_continue(41);
	if(get_flag(4,2) > 1)
		end();
	reset_dialog();
	if(get_flag(4,2) == 0) {
		add_dialog_str(0,"You approach the door, or at least what you imagine must be the door. There's really nothing else down here.",0);
		add_dialog_str(1,"Well, it comes down to this. Even though there's not much left to do, you still have a choice. Do you open the door?",0);
		set_flag(4,2,1);
		}
	else
		add_dialog_str(0,"Do you open the door?",0);
	add_dialog_choice(0,"No, not yet.");
	add_dialog_choice(1,"Yes, bring it on.");
	choice = run_dialog(1);
	if(choice == 1) {
		block_entry(1);
		end();
		}
	else {
		set_flag(4,2,2);
		march_party(29,23);
		force_instant_terrain_redraw();
		pause(5);
		block_entry(1);
		reset_dialog();
		add_dialog_str(0,"As you carefully step closer to the _door,_ you wonder.",0);
		add_dialog_str(1,"Why. Just... why.",0);
		add_dialog_str(2,"Not knowing what else to do, you press your hand up against the wall. You can hear the voice chanting something incoherent from the other side. At least you'll finally get to see the face of your torturer.",0);
		run_dialog(1);
		play_sound(-62);
		pause(10);
		message_dialog("The stone heats up under your hand.","");
		pause(3);
		play_sound(-45);
		pause(2);
		play_sound(-45);
		pause(2);
		play_sound(-45);
		set_terrain(28,23,0);
		force_instant_terrain_redraw();
		pause(5);
		message_dialog("And just like that, the wall slides into the ground, revealing a larger chamber.","Not knowing what else to do, you enter.");
		pause(5);
		march_party(28,23);
		play_sound(-49);
		force_view_center(28,23);
		force_instant_terrain_redraw();
		pause(3);
		march_party(27,23);
		play_sound(-49);
		force_view_center(27,23);
		force_instant_terrain_redraw();
		pause(3);
		march_party(26,23);
		play_sound(-49);
		force_view_center(26,23);
		force_instant_terrain_redraw();
		pause(3);
		march_party(25,23);
		play_sound(-49);
		force_view_center(25,23);
		force_instant_terrain_redraw();
		pause(3);
		march_party(24,23);
		play_sound(-49);
		force_view_center(24,23);
		force_instant_terrain_redraw();
		pause(10);
		play_sound(-45);
		pause(2);
		play_sound(-45);
		pause(2);
		play_sound(-45);
		set_terrain(28,23,41);
		force_instant_terrain_redraw();
		pause(5);
		message_dialog("The wall seals itself behind you, unsurprisingly.","");
		}
break;

beginstate 30; //teh cutscene
	if(get_flag(4,4) == 1)
		end();
	set_flag(4,4,1);
	activate_hidden_group(1);
	set_char_dialogue_pic(6,501,0);
	set_name(6,"Elrilok");
	force_instant_terrain_redraw();
	pause(15);
	reset_dialog();
	add_dialog_str(0,"The voice stops its chanting, and becomes even softer. Even more menacing. It practically drips evil.",0);
	add_dialog_str(1,"_I hear a knock. Who has come to visit poor old Elrilok?_",20);
	run_dialog(1);
	pause(5);
	put_effect_on_space(19,23,5,3,1);
	run_animation_sound(75);
	pause(1);
	set_terrain(19,23,422);
	force_instant_terrain_redraw();
	pause(1);
	put_effect_on_space(19,23,5,3,1);
	run_animation_sound(75);
	pause(1);
	set_terrain(19,23,423);
	force_instant_terrain_redraw();
	pause(1);
	put_effect_on_space(19,23,5,3,1);
	run_animation_sound(75);
	pause(1);
	set_terrain(19,23,424);
	force_instant_terrain_redraw();
	pause(1);
	put_effect_on_space(19,23,5,3,1);
	run_animation_sound(75);
	pause(1);
	set_terrain(19,23,425);
	force_instant_terrain_redraw();
	pause(1);
	put_effect_on_space(19,23,5,3,1);
	run_animation_sound(75);
	pause(1);
	set_terrain(19,23,426);
	force_instant_terrain_redraw();
	pause(2);
	reset_dialog();
	add_dialog_str(0,"It's... again, you're not sure. But upon seeing it, you feel like you'll never be happy again. Your mind aches just looking at the robed form.",0);
	add_dialog_str(1,"You're just glad you can't see the face.",0);
	add_dialog_str(2,"_Ah, my children. Come, speak._",20);
	add_dialog_str(3,"You would run, but your legs have stopped responding to your mind's demands.",0);
	run_dialog(1);
	set_mobility(1000,0);
break;

beginstate 40; //try and go back
	if(get_flag(4,9) != 1)
		end();
	reset_dialog();
	add_dialog_str(0,"With Elrilok defeated (or at least gone for the moment), you see no need to backtrack.",0);
	add_dialog_str(1,"After all, there's a lot of chitrachs back there, dead or alive. And Elrilok's _friends_ eat chitrachs. So it seems like a wise idea to stay away from large numbers of the bugs for now.",0);
	run_dialog(1);
	block_entry(1);
break;

beginstate 41; //leaving chamber dialog
	if(get_flag(4,10) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"As you step out into the (relatively) fresh air of the tunnel, your task is clear. You need to find a way out. And now it's on your own terms.",0);
	run_dialog(1);
	set_flag(4,10,1);
break;

beginstate 50; //normalness
	if(get_flag(4,13) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"This passage looks reassuringly... well, normal. No dead chitrachs, no slime... and perhaps even better, the crystal deposits in the ceiling are getting denser again.",0);
	run_dialog(1);
	set_flag(4,13,1);
break;

beginstate 55; //upslope!
	if(get_flag(4,12) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"There's a slope leading upward. Finally, something in this damned cave goes the way you want it to! Maybe this will finally lead you out of here!",0);
	run_dialog(1);
	set_flag(4,12,1);
break;

beginstate 60; //change location upon exit
	reset_dialog();
	add_dialog_str(0,"You sprint up the passage, and are greeted by the familiar glow of the Triad's algae! Granted, you have no idea where you are, but it's better than that hellhole!",0);
	run_dialog(1);
	change_outdoor_location(1,0,22,38);
break;