begintownscript;

variables;
short choice = 0;

body;

beginstate 0; //INIT_STATE
	if(get_flag(3,0) == 0) {
		relocate_character(0,39,11);
		relocate_character(1,39,10);
		relocate_character(2,40,11);
		relocate_character(3,40,10);
		force_view_center(39,11);
		force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"Your first thought is that it's awfully dark down here. And cold. The air feels so thick that it's almost hard to breathe. Your light no longer penetrates the darkness.",0);
		add_dialog_str(1,"Luckily, there's a fairly large clump of crystal above you, so you can at least see for now.",0);
		run_dialog(1);
		set_flag(3,0,1);
		set_state_continue(10);
		}
	if(get_flag(3,2) == 1) {
		set_terrain(5,19,394);
		set_terrain(5,18,394);
		set_terrain(5,17,394);
		set_terrain(5,16,394);
		force_instant_terrain_redraw();
		}
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(3,0) == 1) && (char_ok(7) == 0)) {
		reset_dialog();
		add_dialog_str(0,"Somewhat similar to the mist, the thing just kind of collapses and disappears. A wisp of smoke marks where it stood.",0);
		run_dialog(1);
		set_flag(3,0,2);
		}
	if(get_flag(3,0) == 2) {
		if(get_ran(1,1,15) == 1) {
			if(get_ran(1,1,2) == 1)
				play_sound(-119); //demon
			else
				play_sound(-121); //chitrach
			}
		}
break;

beginstate 9; //slope back up
	if(is_combat() == 1) {
		block_entry(1);
		end();
		}
	if(get_flag(3,0) == 1) {
		block_entry(1);
		message_dialog("You should probably take care of the crawling thing before you turn your back to leave.","");
		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(2,7,22);
break;

beginstate 10; //chitrach cutscene, deep crawler intro
	pause(10);
	play_sound(121);
	reset_dialog();
	add_dialog_str(0,"Unless you're mistaken, that's the sound of a distressed chitrach.",0);
	run_dialog(1);
	pause(7);
	relocate_character(6,37,16);
	force_instant_terrain_redraw();
	pause(3);
	relocate_character(6,38,15);
	force_instant_terrain_redraw();
	pause(3);
	relocate_character(6,38,14);
	force_instant_terrain_redraw();
	pause(3);
	relocate_character(6,38,13);
	force_instant_terrain_redraw();
	pause(5);
	reset_dialog();
	add_dialog_str(0,"You didn't think that chitrach larvae could sigh, but this one just did. It looks relieved to be in the light again, even though it's standing next to possibly the biggest threat to its life expectancy.",0);
	add_dialog_str(1,"Then you hear something else.",0);
	run_dialog(1);
	pause(5);
	play_sound(119);
	relocate_character(7,37,16);
	force_instant_terrain_redraw();
	pause(2);
	relocate_character(7,38,15);
	force_instant_terrain_redraw();
	pause(2);
	relocate_character(7,38,14);
	force_instant_terrain_redraw();
	pause(3);
	play_sound(-87);
	set_character_pose(7,1);
	force_instant_terrain_redraw();
	pause(2);
	kill_char(6,4,0);
	pause(3);
	set_character_pose(7,0);
	force_instant_terrain_redraw();
	play_sound(56);
	pause(5);
	reset_dialog();
	add_dialog_str(0,"That... thing. That thing just ate a chitrach. Nothing eats chitrachs. Nothing.",0);
	add_dialog_str(1,"And that thing... the edges of its form are blurred. The air around it seems dark and warped, as if it's a part of the darkness itself.",0);
	add_dialog_str(2,"But the claws are real enough. It bellows and lunges at you.",0);
	run_dialog(1);
	play_sound(-119);
	pause(5);

break;

beginstate 15; //chittering sound is back
	if(get_flag(3,1) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"The familar sound of chitrachs returns. The chittering sound is far better than the deathly silence you heard earlier, and it makes you wonder where they are.",0);
	add_dialog_str(1,"Of course, this far into the nest, you'd expect to see most of the chitrach brood. Or, the way things have been going, their remains.",0);
	add_dialog_str(2,"Every so often, you hear the sound of a dying chitrach echoing along the passage. It's also getting a bit damper.",0);
	run_dialog(1);
	set_flag(3,1,1);
break;

beginstate 20; //block off the normal-looking exit
	if(get_flag(3,2) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"The exit ahead is pretty well-lit. Even though it leads further down, it gives you a bit of hope that you might escape this nightmarish tunnel.",0);
	add_dialog_str(1,"And then the voice returns.",0);
	add_dialog_str(2,"_Who would dare take my children from me? You will take the road less traveled..._",20);
	run_dialog(1);
	pause(5);
	put_effect_on_space(5,19,5,5,1);
	put_effect_on_space(5,18,5,5,1);
	put_effect_on_space(5,17,5,5,1);
	put_effect_on_space(5,16,5,5,1);
	set_terrain(5,19,394);
	set_terrain(5,18,394);
	set_terrain(5,17,394);
	set_terrain(5,16,394);
	run_animation_sound(75);
	force_instant_terrain_redraw();
	set_flag(3,2,1);
	pause(5);
	reset_dialog();
	add_dialog_str(0,"And just like that, your path is yet again chosen for you.",0);
	run_dialog(1);
break;

beginstate 25; //side exit
	if(get_flag(3,3) == 1)
		end();
	set_flag(3,3,1);
	reset_dialog();
	add_dialog_str(0,"There's a small tunnel leading down off to the side here. It's not as prominent as the others that have led you through the chitrach nest, but it's there.",0);
	if(get_flag(3,2) == 1)
		add_dialog_str(1,"And with the other exit blocked, it seems to be the only way to proceed.",0);
	run_dialog(1);
break;

beginstate 30; //leaving via side exit
	if(is_combat() == 1) {
		block_entry(1);
		end();
		}
	choice = 0;
	reset_dialog();
	add_dialog_str(0,"The tunnel slopes down 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(4,40,21);
break;

beginstate 35; //chittering louder, heading down to trapped bugs
	if(get_flag(3,4) > 0)
		end();
	reset_dialog();
	add_dialog_str(0,"The chittering sound is getting louder. Maybe a few bugs are still alive.",0);
	add_dialog_str(1,"You never thought you'd live to see the day when you'd be glad to see chitrachs alive, but this might lead you to an exit from this hellhole.",0);
	run_dialog(1);
	set_flag(3,4,1);
break;

beginstate 36; //found trapped bugs
	if(get_flag(3,4) > 1)
		end();
	reset_dialog();
	add_dialog_str(0,"Your heart sinks again. This isn't an exit, this is merely where those... things were keeping the last living chitrachs. Like some sort of twisted pantry.",0);
	add_dialog_str(1,"The bugs don't move to attack you. The dead husk of a chitrach in the center of the chamber is more than likely responsible for that.",0);
	add_dialog_str(2,"You could free them, but they look to be paralyzed with fear. You could kill them, but it wouldn't really matter anyway. The bottom line is that this is a dead end.",0);
	run_dialog(1);
	set_flag(3,4,2);
break;

beginstate 40; //nursery
	if(get_flag(3,5) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"You can barely see through the darkness in this chamber, but it looks like it was originally a nursery of sorts for the bugs. Perhaps they are a bit smarter than originally thought.",0);
	add_dialog_str(1,"Or, more than likely, this is just instinctual to them. Either way, most of these ones are dead, so it makes little difference.",0);
	run_dialog(1);
	set_flag(3,5,1);
break;

beginstate 45; //queen's chamber
	if(get_flag(3,6) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"Every chitrach nest has a queen. You've heard tales from other adventurers about their massive chambers, where they will sit and be fed by the other bugs.",0);
	add_dialog_str(1,"Even in the pitch-black darkness, you can tell that this is the queen's chamber. The unusually large pieces of chitrach husk beneath your feet give that much away.",0);
	run_dialog(1);
	set_flag(3,6,1);
break;