// Town 11: Thassaka
begintownscript;

variables;

short choice,i,j,k,l;
string dlgstr;

body;

beginstate INIT_STATE;
	set_crime_tolerance(2);
	
	set_name(13,"Pistokha");
	set_name(14,"Khalthanas");
	set_name(18,"Advisor");
	set_name(19,"Advisor");
	set_name(34,"Deross");
	set_name(35,"Sakna");
	set_name(36,"Faskoss");
	set_name(41,"Takoss");
	set_name(42,"Scribe");
	set_name(43,"Scribe");
	
	if (get_flag(290,7))
		make_town_hostile();
	
	if (get_flag(11,4) == 1) {
		activate_hidden_group(1);
	
		set_name(30,"Ithik"); // type 112
		set_level(30,60);
		set_name(31,"Talas"); // type 112
		set_char_dialogue_pic(31,582,0);
		set_name(25,"Silthokh"); // type 111
		set_char_dialogue_pic(32,585,0); // Phaedra
		set_name(33,"Ethass"); // type 111
		set_char_dialogue_pic(33,584,0);
		set_name(29,"Machrone"); // type 78
		set_name(26,"Pithoss"); // type 112
		set_char_dialogue_pic(26,580,0);
		set_name(28,"Kass"); // type 36
		set_char_dialogue_pic(28,583,0);
		set_name(27,"Legare"); // type 119
		
		change_blocked(34,13,1);
		change_blocked(35,13,1);
		change_blocked(36,13,1);
		}
	
if (get_flag(11,3) == 0) {
	force_instant_terrain_redraw();
	message_dialog("You approach the back docks of the slith city, looking at the banners that cover the walls and the guards that stand by. You approach causes a small alarm to go up in the city, and, not wanting to cause problems, you wait as they get organized.","After a short while, an important-looking slith comes out of the gates and speaks to you. He speaks the ancient slith language with a strange accent.");
	
	relocate_character(14,53,29);
	set_character_facing(14,6);
	
	text_bubble_on_char(14,"My name is Khalthanas.");
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 5);
	
	text_bubble_on_char(14,"");
	text_bubble_on_char(14,"I am the king of Thassaka.");
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 5);
	
	text_bubble_on_char(14,"");
	force_instant_terrain_redraw();
	
	reset_dialog();
	if ((species_in_party(0)) || (species_in_party(2)))
		add_dialog_str(0,"He looks at you with confusion on his face. It must be because he's never seen a non-slith humanoid. _Who... and what... are you?_",0);
	else
		add_dialog_str(0,"_You are not from these lands, but you are clearly not like the barbarians that menace us. Who are you?_",0);
	add_dialog_str(1,"You quickly fill him in, explaining where you're from and why you've come to Thassaka. You begin to describe the expedition that is coming behind you when he cuts you off in horror.",0);
	add_dialog_str(2,"_Darklings! The children of Thsss have returned? We do not need yet another enemy!_ He looks very upset, so you hurriedly explain that you intend no harm. You briefly describe Bahssikava and the repentance.",0);
	add_dialog_str(3,"He looks somewhat reassured but still a little skeptical. _You can enter the city,_ he says, _but do not bring your friends here yet. Come speak with me among my advisors, and we will decide what to do._ He walks back inside the city walls.",0);
	set_flag(11,3,run_dialog(1));
	
	relocate_character(14,7,56);
	set_character_facing(14,0);
	}

if (get_flag(11,6) != 0)
	end();
if (get_flag(11,4) == 0)
	end();
	
	// Khalthanad performance time
	activate_hidden_group(2);
	activate_hidden_group(3);
	erase_char(63);
	erase_char(64);
	erase_char(65);
	erase_char(66);
	
	i = 45;
	while (i <= 62) {
		if (i <= 53)
			relocate_character(i,22,27 + (i - 45));
		else
			relocate_character(i,23,27 + (i - 54));
		i = i + 1;
		}
	
	relocate_character(14,18,33);
	relocate_character(26,18,32);
	relocate_character(27,18,31);
	relocate_character(28,18,29);
	relocate_character(33,18,30);
	
	relocate_character(25,19,30);
	relocate_character(29,19,31);
	relocate_character(30,19,32);
	relocate_character(31,20,30);
	relocate_character(32,20,31);
	relocate_character(16,20,32);
	relocate_character(17,21,30);
	relocate_character(15,21,31);
	relocate_character(35,21,32);
	
	force_instant_terrain_redraw();
	
	message_dialog("All of Thassaka seems quiet right now. You hear the sound of a crowd near the middle of town, occasionally making noise, cheering. Something must be going on.","");
	
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
if (get_crime_level() == 0)
	end();
if (get_flag(290,7) > 0)
	end();
	
	print_str_color("You have made the expedition angry with you.",1);
	make_town_hostile();
	set_flag(290,7,1);
break;

beginstate 10;
	print_str_color("You can't board this boat. It's not yours.",1);
break;

beginstate 11;
if (char_ok(26))
	message_dialog("Pithoss blocks your way through this door. _Legare is inside,_ he says, _and he is resting. You cannot disturb him now._","");
else
	message_dialog("This door is locked very firmly. You cannot get it open.","");
break;

beginstate 12;
	message_dialog("This is a catalog of all the writings in the library. It is a vast store, indeed, and you are sure that it represents a who's-who of slith homeland writers.","However, you don't have enough time to look through each work, and you're not looking for anything in particular.");
break;

beginstate 13; // Scripting fun!
if (get_flag(11,1) == 0)
	end();
if (get_flag(11,5) >= 2)
	end();
	
	// Give two messages depending on whether the party has done this before.
	reset_dialog();

if (get_flag(11,5) == 0) {
	add_dialog_str(0,"You step on this mat, and suddenly you realize that several sliths are watching you. One, Sakna the merchant, comes over to speak with you.",0);
	add_dialog_str(1,"_Are you a javelin-thrower?_ he asks. _Have you the skill? This is our training spot, the place where we practice our throwing skills; it is one of the favorite pastimes of this city. Do you wish to test your skill?_",0);
	add_dialog_str(2,"What do you say?",0);
	}
else {
	add_dialog_str(0,"When you return, Sakna notices again. _Do you wish to try out your javelin skills now?_",0);
	}
	add_dialog_choice(0,"No, not right now. (Leave.)");
	add_dialog_choice(1,"Yes, I'll give it a shot.");
	
	// If the party opts out, end it all.
	if (run_dialog(1) == 1) {
		message_dialog("Sakna and the others look disappointed, but they let you go. _Perhaps some other time,_ he says.","");
		end();
		}
	
	block_entry(1);
	
	message_dialog("Sakna hands you three javelins. _Just try to hit the targets,_ he says. _You get two tries from each of the three mats. Two hits out of three targets is a win._","");
	
	// Find the character with the highest Thrown Missiles + Sharpshooter skill
	// i is an index, j is the highest stat so far, and k is the character with
	// that stat
	i = 0;
	j = 0;
	k = 10;
	while (i < 4) {
		if (char_ok(i))
			if (get_stat(i,7) + get_stat(i,30) > j) {
				j = get_stat(i,7) + get_stat(i,30);
				k = i;
				}
		i = i + 1;
		}
	
	// If no one has any javelin skill, just take char 0.
	if (k == 10)
		k = 0;
	
	// Set up party locations
	relocate_character(0,23,53);
	relocate_character(1,23,52);
	relocate_character(2,23,51);
	relocate_character(3,23,50);
	set_character_facing(0,2);
	set_character_facing(1,2);
	set_character_facing(2,2);
	set_character_facing(3,2);
	
	relocate_character(k,21,54);
	force_view_center(21,54);
	
	activate_hidden_group(2);
	relocate_character(35,18,48);
	set_character_facing(35,4);
	
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);
	
	message_dialog("A crowd rapidly gathers. You set up your place on the first mat.","When everything is ready, you throw the first javelin.");
	
	// Now i is current success or failure and j denotes total successes.
	// l indicates how many times you've tried
	
	// Throw the javelins. This is nuts, I know.
	//
	// The idea is that you have two chances on each target and have to hit two
	// of three targets to win.
	//
	// To determine if you hit, add your Thrown Missiles and Sharpshooter to get
	// your total skill. Then roll two dice with as many sides as your skill. 16
	// and up is a hit. 15 and down is a miss.
	
	i = 0;
	j = 0;
	l = 0;
	while (l <= 5) {
		set_character_pose(k,1);
		
		// Hit?
		if (get_ran(2,1,get_stat(k,7) + get_stat(k,30)) > 15) {
			put_effect_on_space(15,54 - ((l/2) * 2),9,1,2);
			run_animation_sound(118);
			i = 1;
			}
		else {
			text_bubble_on_char(63 + ((l + 1) / 2),"*thunk*");
			force_instant_terrain_redraw();
			play_sound(91);
			text_bubble_on_char(63 + ((l + 1) / 2),"");
			force_instant_terrain_redraw();
			i = 0;
			}
			
		set_character_pose(k,0);
		force_instant_terrain_redraw();
			
		// If hit, give a message. If not, give a different message.
		// Either way, keep track.
		if (i == 0) {
			clear_buffer();
			append_string("You miss your ");
			if ((l % 2) == 0)
				append_string("first");
			else
				append_string("second");
			append_string(" try on target ");
			append_number((l / 2) + 1);
			append_string(".");
			if ((l % 2) == 0)
				append_string(" You try again.");
			else
				if (l != 5)
					append_string(" You move on to the next target.");
			get_buffer_text(dlgstr);
			message_dialog(dlgstr,"");
			l = l + 1;
			if ((l == 2) || (l == 4)) {
				relocate_character(k,char_loc_x(k),char_loc_y(k) - 2);
				force_view_center(char_loc_x(k),char_loc_y(k));
				force_instant_terrain_redraw();
				}
			}
		else {
			if (l < 4)
				message_dialog("You hit the target! Feeling pleased with your success, you move to the next one.","");
			else
				message_dialog("You hit the last target! The crowd cheers!","");
			j = j + 1;
			if ((l % 2) == 0)
				l = l + 2;
			else
				l = l + 1;
			if (l <= 5) {
				relocate_character(k,char_loc_x(k),char_loc_y(k) - 2);
				force_view_center(char_loc_x(k),char_loc_y(k));
				force_instant_terrain_redraw();
				}
			}
		}
	
	if (j >= 2) {
		set_flag(11,5,3);
		message_dialog("You hit more than one of the targets, which means you've successfully completed the test! The crowd buzzes with excitement; you've demonstrated great skill with the favorite sport of the city.","You may find yourself more popular now.");
		award_party_xp(30,60);
		}
	else {
		set_flag(11,5,2);
		message_dialog("You could not hit two of the targets, which means you've failed the test. Despite that, you hear some positive murmurs among the crowd. You've attempted to partake in the favorite sport of the city, and even if you weren't good, you tried.","You may find yourself a little more popular now.");
		award_party_xp(10,60);
		}
	
	relocate_character(35,9,42);
	set_character_facing(35,0);
	
	march_party(21,53);
	march_party(21,52);
	march_party(21,51);
	march_party(21,50);
	
	i = 45;
	while (i <= 66) {
		erase_char(i);
		i = i + 1;
		}
	
	force_instant_terrain_redraw();
	
break;

beginstate 14;
if (get_flag(11,6) != 0)
	end();
if (get_flag(11,4) == 0)
	end();
	
	block_entry(1);
	
	message_dialog("You can hear the sound of a loud slith voice speaking rhythmically, melodically, as if in a strange sort of meter. You stop mid-step and draw closer, following the sound.","There are many sliths in your way. You walk around to get a better view.");

	march_party(29,29);
	force_view_center(29,29);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(28,29);
	force_view_center(28,29);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(27,29);
	force_view_center(27,29);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(26,28);
	force_view_center(26,28);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(25,27);
	force_view_center(25,27);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(24,26);
	force_view_center(24,26);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(23,25);
	force_view_center(23,25);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	while (char_loc_x(i) > 15) {
		march_party(char_loc_x(i) - 1,char_loc_y(i));
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(get_flag(290,0) * 1);
		}

	set_state_continue(17);

break;

beginstate 15;
if (get_flag(11,6) != 0)
	end();
if (get_flag(11,4) == 0)
	end();
	
	block_entry(1);
	
	message_dialog("You can hear the sound of a loud slith voice speaking rhythmically, melodically, as if in a strange sort of meter. You stop mid-step and draw closer, following the sound.","");

	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	
	march_party(char_loc_x(i),char_loc_y(i) - 1);
	force_view_center(char_loc_x(i),char_loc_y(i));
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);
	
	while (char_loc_x(i) < 9) {
		march_party(char_loc_x(i) + 1,char_loc_y(i));
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		pause(get_flag(290,0) * 1);
		}

	if (char_loc_y(i) > 32)
		march_party(10,char_loc_y(i) + 1);
	else
		march_party(10,char_loc_y(i) - 1);
	force_view_center(10,char_loc_y(i));
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(11,char_loc_y(i));
	force_view_center(11,char_loc_y(i));
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(12,char_loc_y(i));
	force_view_center(12,char_loc_y(i));
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(13,char_loc_y(i));
	force_view_center(13,char_loc_y(i));
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(14,char_loc_y(i));
	force_view_center(14,char_loc_y(i));
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	march_party(15,char_loc_y(i));
	force_view_center(15,char_loc_y(i));
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);

	set_state_continue(17);

break;

beginstate 17;
	set_flag(11,6,1);
	
	force_view_center(16,31);
	set_total_visibility(1);
	force_instant_terrain_redraw();
	
	reset_dialog();
	add_dialog_str(0,"As you drew near the altar, you picked up some snatches of the verse. The speaker is clearly reciting in some sort of meter, but it's odd: you can't quite place it.",0);
	add_dialog_str(1,"More oddly, you can't identify the language. It sounds like the ancient slith language that you learned in Bahssikava and Vasskolis, but it isn't. Or at least... that's it! It's an archaic form. But the ancient language is already hundreds of years old!",0);
	add_dialog_str(2,"You're listening to a slith poem that must descend from a time before the exile of the sliths, perhaps even before the rise of the surface Empire, before any human history. The memory of these sliths is long.",0);
	run_dialog(1);
	
	text_bubble_on_char(67,"Kaaloohessem koot");
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 5);
	
	text_bubble_on_char(67,"");
	text_bubble_on_char(67,"belleekh theifaa moni kaanaes");
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 5);
	
	text_bubble_on_char(67,"");
	force_instant_terrain_redraw();
	
	reset_dialog();
	add_dialog_str(0,"_Of the noble story and battles, goddess, sing in me,_ the poet recites, his voice drifting up and down like a musical scale, guided by the rhythm of the hexameter line. This is the Khalthanad, the founding poem of the slith empire.",0);
	add_dialog_str(1,"As you listen, Khalthas begins his life as a prince of a small city-state in distant lands, the city of Iliointh. He grows up, and as a young man becomes king. Then he is betrayed by his demon-aided brother, Siisyfoss, and exiled from the land.",0);
	add_dialog_str(2,"For many long years, he wanders, aimless, until he finds a mountain home of a great dragon, Mahanyakshetra, with which he fights a terrible battle. With immense strength, he slays the beast.",0);
	add_dialog_str(3,"After his victory, a goddess appears to him, and she reveals to him his magnificent destiny and teaches him the virtues that he must uphold throughout the rest of his life. She sends him on his way, with her blessings, to the nearest city.",0);
	add_dialog_str(4,"There, he finds the evil lord Thantaloss, whom he overthrows and slays, and he begins his empire. Short wars with neighboring kingdoms later, he rules over all of Khitaloss, the great cave in the center of the slith homeland.",0);
	add_dialog_str(5,"He has several more adventures before his end, and when he finally dies, the rulership passes in an orderly fashion to his successor, the great Teveross.",0);
	run_dialog(1);
	
	message_dialog("The poem lasts for hours, and how many you simply cannot tell. The crowd must have heard this story dozens of times in full and hundreds in summary, but they shout and cheer at every victory of Khalthas and nearly weep at every loss.","Likewise, you, transfixed, cannot help but join in for as long as the poem lasts, and when it ends, you speak at length with the others nearby about the story.");
	
	set_total_visibility(0);
	
	i = 45;
	while (i <= 62) {
		erase_char(i);
		i = i + 1;
		}
	
	erase_char(67);
	
	relocate_character(14,7,56);
	relocate_character(26,36,13);
	relocate_character(27,36,15);
	relocate_character(28,35,15);
	relocate_character(33,35,14);
	
	relocate_character(25,14,39);
	relocate_character(29,33,44);
	relocate_character(30,9,18);
	relocate_character(31,13,13);
	relocate_character(32,20,10);
	relocate_character(16,29,54);
	relocate_character(17,31,54);
	relocate_character(15,32,55);
	relocate_character(35,9,42);
	
	march_party(27,48);
	march_party(28,48);
	march_party(29,48);
	march_party(30,48);
	
	force_view_center(10,56);
	force_instant_terrain_redraw();
	
	message_dialog("Once you are finally done, you return to your quarters and sleep, dreaming of slith legends and heroes. When you wake, it is time to speak with someone about the next stage of your journey.","");
	
	force_view_center(30,48);
	force_instant_terrain_redraw();
	
break;

beginstate 18;
if ((get_flag(16,1) == 1) && (get_flag(14,0) == 1)) {
	reset_dialog();
	add_dialog_str(0,"Are you ready to approach Pithoss and tell him what you found in the ocean of lava?",0);
	add_dialog_choice(0,"No. (Leave.)");
	add_dialog_choice(1,"Yes. (Approach him.)");
	if (run_dialog(1) == 1)
		block_entry(1);
	else {
		reset_dialog();
		add_dialog_str(0,"_Greetings,_ Pithoss says as you approach. You describe the path you've found, the island of safety in the ocean of lava and the probable exit point that Talas can surely clear.",0);
		add_dialog_str(1,"_Good,_ he says. He goes inside and speaks briefly with Legare and Kass, and then he returns. _I will tell the others to prepare to leave. You should get some rest. We will go in a few hours._",0);
		add_dialog_str(2,"You return to your room and drift to sleep.",0);
		run_dialog(1);
		
		toggle_quest(4,0);
		award_party_xp(50,60);
		
		force_view_center(28,57);
		force_instant_terrain_redraw();
		
		reset_dialog();
		add_dialog_str(0,"All that you have experienced so far tumbles through your dreams. Bahssikava, Vasskolis, the battle in the middle of the dry river, the ruins of Nakhtha, the living city of Thassaka, and now this ocean of lava.",0);
		add_dialog_str(1,"The epic Khalthanad, the unknown Goddess, Phaedra's mysterious pregnancy, the strange connection between the vahnatai and the sliths, even the current status of the center of the homeland -- so many questions remain unanswered.",0);
		add_dialog_str(2,"You can only hope that during the next leg of your journey, some of these issues will finally begin to be resolved.",0);
		add_dialog_str(3,"When you wake, you travel with the expedition to the small island of tranquility in the burning ocean.",0);
		run_dialog(1);
		
		set_flag(14,1,1);
		change_outdoor_location(0,5,30,28);
		move_to_new_town(15,10,29);
		}
	}
break;

beginstate 19;
if (get_flag(14,1) == 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"Are you ready to leave Thassaka and move on to the Lava Ocean?",0);
	add_dialog_choice(0,"No. I am not finished here.");
	add_dialog_choice(1,"Yes. Onward!");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(15,10,28);
break;

beginstate 20;
	message_dialog("These are mind crystals, powerful artifacts usually associated with the vahnatai that speak directly into the mind of the person sitting in the chair and teach things. They are the fastest means of learning things ever devised.","However, these are completely dark. They must not be charged up with any information right now.");
break;