// Town 39: Kathaneth
begintownscript;

variables;

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

body;

beginstate INIT_STATE;
	set_crime_tolerance(2);
	
	if (get_flag(39,1)) {
		flip_terrain(40,17);
		flip_terrain(40,18);
		flip_terrain(40,19);
		flip_terrain(44,17);
		flip_terrain(44,18);
		flip_terrain(44,19);
		}
	
	if (get_flag(39,0) == 0) {
		force_instant_terrain_redraw();
		set_flag(39,0,1);
		message_dialog("You enter what appears to be an abandoned barbarian fort. The sign on the wall says, _Kathaneth._ You'd guess that it wasn't abandoned too long ago, because not much has fallen apart yet. That is, except the gates, which are the rubble at your feet.","Even though the fort has no barbarians in it anymore, it is not empty: you can hear things moving around in the buildings.");
		}
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
break;

beginstate 10;
	block_entry(1);
	reset_dialog_preset_options(2);
	if (run_dialog(1) == 2) {
		if (has_special_item(4)) {
			flip_terrain(40,17);
			flip_terrain(40,18);
			flip_terrain(40,19);
			flip_terrain(44,17);
			flip_terrain(44,18);
			flip_terrain(44,19);
			if (get_terrain(40,18) == 63)
				set_flag(39,1,0);
			else
				set_flag(39,1,1);
			play_sound(99);
			}
		else
			message_dialog("This lever is stuck. You look closely, and it is locked in place with an iron lock. You can only hope that the key is nearby.","");
		}
break;

beginstate 11;
	message_dialog("As the rakshasa dies, you think carefully about the deadly spell that it cast. You think you understand it. You understand it well enough to cast it!","(You have learned a level of Cloudkill.)");
	inc_flag(292,16,1);
	set_flag(292,0,1);
break;

beginstate 12;
if (get_flag(39,2) == 0) {
	reset_dialog();
	add_dialog_str(0,"In this desk, you find all sorts of papers, most involving the god Kelora-Nak. However, one thing seems interesting: a small spellbook!",0);
	add_dialog_str(1,"Reading strange spellbooks can be dangerous: they can carry curses or other terrible effects. However, they can also have great spells recorded within.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Leave it alone.");
	add_dialog_choice(1,"Attempt to read it.");
	if (run_dialog(1) == 2) {
		reset_dialog();
		// Requires Arcane Lore of 25.
		if (get_skill_total(42) >= 25) {
			add_dialog_str(0,"The book is written in a strange rune script, but fortunately, due to your knowledge of Arcane Lore, you can read it.",0);
			// Requires some skill in the spell already.
			if (get_flag(293,1) > 0) {
				set_flag(39,2,1);
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast Sanctuary in the most efficient fashion possible. You read over it carefully.",0);
				set_flag(293,1,3); // flag for spell
				}
			else
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast a complex spell, Sanctuary, in a very efficient manner. Unfortunately, you are completely unfamiliar with the spell, so it is too hard for you to understand.",0);
			run_dialog(1);
			}
		else
			message_dialog("The book is written in a strange rune script, and unfortunately, you can't read it. Perhaps if you knew more about Arcane Lore, you would be able to.","");
		}
	}
else
	message_dialog("You find the book on Sanctuary again, but you don't learn anything new from it.","");
break;

beginstate 13;
if (get_flag(39,3) == 0) {
	reset_dialog();
	add_dialog_str(0,"Most of the books on these shelves are still legible but completely useless; they concern superfluous topics or are works of sordid fiction. But amongst this garbage, you find a spellbook!",0);
	add_dialog_str(1,"Reading strange spellbooks can be dangerous: they can carry curses or other terrible effects. However, they can also have great spells recorded within.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Leave it alone.");
	add_dialog_choice(1,"Attempt to read it.");
	if (run_dialog(1) == 2) {
		reset_dialog();
		// Requires Arcane Lore of 20.
		if (get_skill_total(42) >= 20) {
			set_flag(39,3,1);
			add_dialog_str(0,"The book is written in a strange rune script, but fortunately, due to your knowledge of Arcane Lore, you can read it.",0);
			if (get_flag(292,2) == 0) // flag for spell
				add_dialog_str(1,"The book describes an interesting new spell, one that deadens an enemy's muscles. (You learn a level of Paralyze.)",0);
			else
				add_dialog_str(1,"It's a spellbook describing how to cast Paralyze more efficiently. You read over it carefully.",0);
			inc_flag(292,2,run_dialog(1)); // flag for spell
			set_flag(292,0,1);
			}
		else
			message_dialog("The book is written in a strange rune script, and unfortunately, you can't read it. Perhaps if you knew more about Arcane Lore, you would be able to.","");
		}
	}
else
	message_dialog("You find the book on Paralyze again, but you don't learn anything new from it.","");
break;

beginstate 14;
	reset_dialog();
	add_dialog_str(0,"There is a note in this desk. It is  hard to read but not illegible. Also, the language is archaic, but the letter cannot be very old, since it has not rotted much. It is written in an awkward, personal hand: this was not written by a scribe.",0);
	add_dialog_str(1,"_O Anthalak, Lord of the Misbegotten, Worker of Disfigurement, Master of the Haunting, with this writing, I invoke thee, most especially as the Haunter. Hear my prayer and respond.",0);
	add_dialog_str(2,"_A shade has come to haunt the mines upon which we depend for sustenance, and it has driven out our prey. Without the hunted, we hunters may soon perish.",0);
	add_dialog_str(3,"_O Lord Anthalak, banish this shade to the dark realms whence it came, and let our prey return to its cave, so that we may prosper again, or if not, grant us some other land, just as bountiful, where we may hunt and raid also.",0);
	add_dialog_str(4,"_Grant us your blessing, dark lord, and let these things be done. It is written._",0);
	run_dialog(1);
break;

beginstate 15;
if (get_flag(39,4) != 0)
	end();
	
	set_flag(39,4,1);
	message_dialog("As the doomguard dies, you notice a small iron key on its body. You grab it.","");
	change_spec_item(4,1);
break;