// Town 12: Central Bahssikava
begintownscript;

variables;

short choice,dummy,i;
string dlgstr;

body;

beginstate INIT_STATE;
	turn_off_training(1);

	change_blocked(40,28,1);
	change_blocked(40,29,1);
	change_blocked(41,28,1);
	change_blocked(41,29,1);
	change_blocked(42,28,1);
	change_blocked(42,29,1);
	change_blocked(43,28,1);
	change_blocked(43,29,1);

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

	force_instant_terrain_redraw();
	message_dialog("You descend a short but steep slope. Down here, you can hear chittering, not like anything you've heard so far. No lizards, dark wyrms, or slith ghosts are down here, but something rather different.","");
	set_flag(12,0,1);
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
break;

beginstate 10;
if ((get_flag(250,4) == 1) && (has_item(451) == 0))
	{message_dialog("You can't just leave Phaedra behind. You must take her with you to the guardpost immediately or she will die.","");
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"There is a small, rubble-filled slope up to the east here. Do you wish to walk up it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Walk up the passage.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice == 2)
		move_to_new_town(11,4,9);
break;

beginstate 11;
if (get_flag(12,1) != 0)
	end();
	
	message_dialog("It appears there was a massive cave-in here some time ago, like much of the rest of this area. However, unlike much of the rest of this area, this area has been dug away.","By very large insects, if you read the scratch marks correctly. Evidently there are chitrachs or their cousins ahead. And if the size of the scratches is any indication, these are not normal chitrachs.");
	set_flag(12,1,1);
break;

beginstate 12;
if (get_flag(12,2) != 0)
	end();
	
	message_dialog("Well, you've found the source of these chitrachs. There is a large hole in the ground in the middle of what appears to be a ruined ancient slith temple. Chitrachs scurry up and down the hole constantly.","The holy altar here is evidently still sanctified, and completely disapproves of this. It is zapping the chitrachs as they appear, to little effect.");
	set_flag(12,2,1);
break;

beginstate 13;
if (get_flag(12,3) != 0)
	end();
	
	message_dialog("This area is completely a mess. The cavequake must have made the ceiling of this whole area collapse. It appears that several tunnels split off from here, but only one is still clear.","");
	set_flag(12,3,1);
break;

beginstate 14;
if (get_flag(250,2) == 0)
	{message_dialog("There is a chalkboard sign here with strange cursive letters written on it. You don't know the ancient script in which it is written, so you can't read it.","");
	end(); }
	
	set_flag(12,8,1);
if (get_flag(250,2) == 1)
	message_dialog("There is a chalkboard sign here with Ancient Bahssikavan cursive letters written on it. You can make out something about _school_ and _reading,_ but you can't quite make out exactly what it is saying.","");
if (get_flag(250,2) >= 2)
	message_dialog("There is a chalkboard sign here with Ancient Bahssikavan cursive letters written on it. It says something about _CENTRAL SCHOOL_ and _READING CLASS TONIGHT._ You can't quite make it out, but you can guess.","Right before the cavequake, the school was set up for a reading class. That's why the mind crystal was prepped and ready.");
break;

beginstate 15;
if (get_flag(12,4) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"This place has the appearance of a school, and this room is a peculiar testament to the educational practices of ancient sliths. It has five mind crystals with chairs for each of them.",0);
	add_dialog_str(1,"It appears that along with conventional instruction, sliths dumped information directly into their students' minds. This seems rather strange, at least by Avernum standards.",0);
	add_dialog_str(2,"This strangeness is, of course, added to the strangeness inherent in seeing sliths using vahnatai mind crystals, especially when it doesn't appear possible that these sliths had met vahnatai before.",0);
	add_dialog_str(3,"Most of the crystals are out, inactive. However, the one in the northwest corner appears to still be lit.",0);
	set_flag(12,4,run_dialog(1));
break;

beginstate 16;
if (get_flag(12,13) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"This mind crystal appears to still be lit and therefore active. Do you wish to attempt to use it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Use it.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	else
		{add_dialog_str(0,"As you stare deep into the Mind Crystal, you start to hear strange, whispering voices in your mind. The words are barely audible, but they seem to be trying to teach you something.",0);
		add_dialog_str(1,"You concentrate, and you start to get an impression of what ability the item is trying to teach you.",0);
		add_dialog_str(2,"Ancient Slithzerikai Language",15);
		add_dialog_str(3,"Learning some of the ancient slithzerikai language will allow you to read better the cursive letters that you find around Bahssikava.",15);
		add_dialog_str(4,"This crystal can only be used once, so choose carefully whether or not to use it now.",0);
		add_dialog_choice(0,"Don't use it yet.");
		add_dialog_choice(1,"Use the crystal.");
		choice = run_dialog(1);
		if (choice == 2)
			{message_dialog("Words and images flow across the surface of the crystal, and a soft, sibilant voice whispers into your mind.","When it is done, you are not fluent in ancient slitherzerikai, but you understand it far better.");
			inc_flag(250,2,1);
			set_flag(12,13,1);
			}
		}
break;

beginstate 17;
	reset_dialog();
	add_dialog_str(0,"Searching this shelf, you find an interesting-looking book. It is red and has runes written across the cover. They look magical. Do you wish to try to read it?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	if (run_dialog(1) == 1)
		end();
		
if (get_skill_total(42) <= 30)
	message_dialog("You open the book and page through, but the words are too foreign. You can't make out any of it. Your Rune Reading skill is not high enough.","");
	else {
		reset_dialog();
		add_dialog_str(0,"The book teaches you a better way of casting Arcane Shield.",0);
		if (char_ok(0)) {
			clear_buffer();
			append_char_name(0);
			if (get_spell_level(0,0,18) > 0) {
				if (get_spell_level(0,0,18) >= 3)
					append_string(" already knows the spell at level 3 or better.");
				else {
					append_string(" learns the spell.");
					change_spell_level(0,0,18,3 - get_spell_level(0,0,18));
					}
				}
			else
				append_string(" needs to know the spell at level 1 or higher to learn from this book.");
			get_buffer_text(dlgstr);
			add_dialog_str(1,dlgstr,0);
			}
		if (char_ok(1)) {
			clear_buffer();
			append_char_name(1);
			if (get_spell_level(1,0,18) > 0) {
				if (get_spell_level(1,0,18) >= 3)
					append_string(" already knows the spell at level 3 or better.");
				else {
					append_string(" learns the spell.");
					change_spell_level(1,0,18,3 - get_spell_level(1,0,18));
					}
				}
			else
				append_string(" needs to know the spell at level 1 or higher to learn from this book.");
			get_buffer_text(dlgstr);
			add_dialog_str(2,dlgstr,0);
			}
		if (char_ok(2)) {
			clear_buffer();
			append_char_name(2);
			if (get_spell_level(2,0,18) > 0) {
				if (get_spell_level(2,0,18) >= 3)
					append_string(" already knows the spell at level 3 or better.");
				else {
					append_string(" learns the spell.");
					change_spell_level(2,0,18,3 - get_spell_level(2,0,18));
					}
				}
			else
				append_string(" needs to know the spell at level 1 or higher to learn from this book.");
			get_buffer_text(dlgstr);
			add_dialog_str(3,dlgstr,0);
			}
		if (char_ok(3)) {
			clear_buffer();
			append_char_name(3);
			if (get_spell_level(3,0,18) > 0) {
				if (get_spell_level(3,0,18) >= 3)
					append_string(" already knows the spell at level 3 or better.");
				else {
					append_string(" learns the spell.");
					change_spell_level(3,0,18,3 - get_spell_level(3,0,18));
					}
				}
			else
				append_string(" needs to know the spell at level 1 or higher to learn from this book.");
			get_buffer_text(dlgstr);
			add_dialog_str(4,dlgstr,0);
			}
		dummy = run_dialog(1);
		}
break;

beginstate 18;
if (get_flag(12,5) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"The area back here is completely collapsed, caved into a giant mess. You can't continue this way.",0);
	if (get_flag(6,7) == 1)
		{add_dialog_str(1,"You do a little bit of mental figuring, and you realize that this must be the southern end of the collapsed tunnel that you found in the northern part of Ancient Bahssikava.",0);
		add_dialog_str(2,"That means this must be part of Bahssikava, too. Now you know where the river leads, because the river is just south of here. The river must lead back into Bahssikava. You can go and tell Legare that.",0);
		add_dialog_str(3,"Of course, you can also explore a little more, if you like. Bahssikava must be home to powerful treasures, after all.",0);
		set_flag(12,6,1);
		}
	set_flag(12,5,run_dialog(1));
break;

beginstate 19;
if (get_flag(12,7) != 0)
	end();
	
	block_entry(1);
	reset_dialog();
	add_dialog_str(0,"As you walk down this narrow passageway, you hear a low, almost inaudible, disembodied hiss. _Come clossssser, clossssser, please,_ it says, its words sliding through the air like skates on an icy lake.",0);
	add_dialog_str(1,"Your skin tingles faintly. The air is cold, harsh, biting, as if all of the warm cave moisture had been squeezed out of it.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Leave quickly.");
	add_dialog_choice(1,"Approach the source of the sound.");
	choice = run_dialog(1);
	if (choice == 1)
		{message_dialog("You turn around and head the opposite direction, quickly.","");
		
		i = 0;
		while (char_ok(i) == 0)
			{i = i + 1; }
		
		march_party(31,42);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		march_party(32,42);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		march_party(33,42);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		march_party(34,42);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		march_party(35,42);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		march_party(36,42);
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		end();
		}
		
	if (get_flag(250,9)) {
		reset_dialog();
		add_dialog_str(0,"You have reached another cut scene, one in which a ghost describes what has happened to Ancient Bahssikava. Do you wish to skip it or see it?",0);
		add_dialog_choice(0,"Skip it.");
		add_dialog_choice(1,"See it.");
		if (run_dialog(1) == 1)
			set_state_continue(26);
		}
		
	march_party(29,42);
	force_view_center(char_loc_x(i),char_loc_y(i));
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	march_party(28,42);
	force_view_center(char_loc_x(i),char_loc_y(i));
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	march_party(27,42);
	force_view_center(char_loc_x(i),char_loc_y(i));
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	activate_hidden_group(1);
	put_boom_on_char(12,2,0);
	run_animation_sound(163);
	pause(3 * get_flag(250,8));
	
	i = first_group_member(0);
	text_bubble_on_char(i,"Who...?");
	force_instant_terrain_redraw();
	pause(4 * get_flag(250,8));
	
	text_bubble_on_char(i,"");
	text_bubble_on_char(i,"What...?");
	force_instant_terrain_redraw();
	pause(4 * get_flag(250,8));
	
	text_bubble_on_char(i,"");
	text_bubble_on_char(12,"My name is... was... Sakos.");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"I was a Bahssikavan.");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"I taught at the school near here.");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"Then the cavequake hit.");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"The collapse of these tunnels");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"destroyed our city.");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"In the chaos following the disaster,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"demons invaded.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"Followers of Grah-Hoth,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"they came to this world");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"at the same time as he did.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"We could not defend ourselves.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"We were defeated.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"The demons murdered all of us");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"and enslaved our souls.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"We have waited since then");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	if (party_size() > 1)
		text_bubble_on_char(12,"for those who would free us.");
	else
		text_bubble_on_char(12,"for the one who would free us.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	if (party_size() > 1)
		text_bubble_on_char(12,"If you are those for whom we wait,");
	else
		text_bubble_on_char(12,"If you are the one for whom we wait,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"first, you must find");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"the red iron key");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"that can unlock this door.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"It is up the river,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"north of the guardpost.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(12,"");
	text_bubble_on_char(12,"We anxiously await your return.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	put_boom_on_char(12,2,0);
	run_animation_sound(163);
	erase_char(12);
	text_bubble_on_char(i,"Wait!");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	text_bubble_on_char(i,"");
	force_instant_terrain_redraw();
	
	message_dialog("The ghost is gone. He clearly indicated that this area is Bahssikava, so you know for certain where the river leads. You can report that back to Kass.","A key, past the guardpost? You make a mental note of that, too. You may wish to investigate this situation further.");
	set_flag(12,6,1);
	set_flag(12,7,1);
break;

beginstate 20;
if (get_flag(250,4) == 1) // Phaedra dead
	{add_dialog_str(0,"Behind the door is a narrow, dark passage. However, you can't go this way: Phaedra needs you to bring her back to the guardpost.",0);
	dummy = run_dialog(1);
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"Behind the door is a narrow, dark passage. Do you wish to walk down it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Walk down the passage.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice == 2)
		move_to_new_town(13,24,8);
break;

beginstate 21;
if ((get_flag(12,9) != 0) || (get_flag(12,10) != 0))
	end();
	
	message_dialog("You look down this hole where the chitrachs are coming from, and you can see that it narrows considerably at a certain point. If you could drop something down there, you could stop up this hole and block any more insects from coming up.","");
	set_flag(12,9,1);
break;

beginstate 22;
	message_dialog("You push the barrel filled with stone blocks down the shaft, stuffing up the chitrach hole. It works! No more chitrachs can come up this way.","The nearby altar glows momentarily.");
	set_flag(12,10,1);
	set_flag(12,2,2);
break;

beginstate 23;
	if (get_flag(12,2) < 2)
		{block_entry(1);
		message_dialog("A powerful force prevents you from stepping up on this platform.",""); }
	if (get_flag(12,2) == 2)
		{put_item_on_spot(1,22,453);
		put_effect_on_space(1,22,4,1,2);
		run_animation_sound(25);
		message_dialog("You feel something strange in your pocket that wasn't there a moment ago. You take it out. It's a silver key! You have no idea what it's for, but surely it will be useful somewhere.","The bracelet that just appeared on the altar will probably be useful, too.");
		change_spec_item(5,1);
		set_flag(12,2,3);
		}
break;

beginstate 24;
if (get_flag(12,11) != 0)
	end();
	
	set_flag(12,11,1);
	message_dialog("You approach a building that appears to be mostly intact. Evidently the collapses that occurred elsewhere in this area spared this particular section.","");
break;

beginstate 25;
if (get_flag(12,12) != 0)
	end();
if ((get_flag(12,5) == 0) || (get_flag(12,6) == 0) || (get_flag(12,7) == 1))
	end();
	
	set_flag(12,12,1);
	message_dialog("You hear a strange whispering to the south.","");
break;

beginstate 26;
	reset_dialog();
	add_dialog_str(0,"A ghost appears before you. He tells a story about Bahssikava, about how it was attacked by demons and how the souls of the sliths who died in the attack became slaves to the demons who then took over the city.",0);
	add_dialog_str(1,"He begs for your help and says that if you want to go farther, you need to pass the door that is locked so that it can only be opened with a red iron key which can be found north up the river.",0);
	add_dialog_str(2,"He clearly indicated that this area is Bahssikava, so you know for certain where the river leads. You can report that back to Kass.",0);
	set_flag(12,6,run_dialog(1));
	set_flag(12,7,1);
break;