// Town 51: Dark Cave
begintownscript;

variables;

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

body;

beginstate INIT_STATE;
	set_crime_tolerance(2);
	
	change_blocked(23,22,1);
	change_blocked(24,22,1);
	change_blocked(25,22,1);
	
	set_name(6,"Velthkhogroz");
	add_range_to_group(6,13,1);
	char_give_item(23,262); // Light Cloak
	
	if (get_flag(51,0) == 0) {
		force_instant_terrain_redraw();
		set_flag(51,0,1);
		message_dialog("There is a small niche in the cave wall here, and a portal stands inside it. As you approach this portal, you suddenly hear a voice in your head.","_Whoever enters here faces my wrath. I do not wish to have visitors. Go away!_");
		}
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
break;

beginstate 10;
	reset_dialog_preset_options(3);
	add_dialog_str(1,"This portal looks unusually small and shaky, as though it can only take one person.",0);
	if (run_dialog(1) == 2) {
		if (get_flag(51,2) == 0) {
			if (try_to_split_party(6,38,1) == 0)
				end();
			put_field_on_space(1,34,6);
			message_dialog("You come out on the other side of the portal, momentarily disoriented. Looking around, you notice that there's a small patch of flame growing rapidly in the corner. It's kind of pretty. But then you take a closer look.","It's quickfire! This is a trap!");
			}
		if (get_flag(51,2) == 1) {
			if (try_to_split_party(1,17,1) == 0)
				end();
			message_dialog("When you pass through the portal again, you arrive in a very, very dark room. There is an oppressive shadow hovering over this place.","There are also many spikes in the ground, making it difficult to walk around.");
			}
		if (get_flag(51,2) == 2) {
			if (try_to_split_party(31,41,1) == 0)
				end();
			message_dialog("On the other side of this portal this time, you find a small room with two paths. One, heavily trapped, leads to another portal, presumably the one back to the entrance. The other leads to a room with no visible exits and a lot of monsters.","");
			}
		if (get_flag(51,2) == 3) {
			if (try_to_split_party(24,33,1) == 0)
				end();
			if (get_flag(51,8) == 0)
				message_dialog("Having passed through the portal and arrived in the appropriate place, finally, you get to see your mysterious host. It is a dragon! And an old one, at that. He sits, watching you, clearly uneasy at your presence.","");
			set_flag(51,8,1);
			}
		}
	block_entry(1);
break;

beginstate 11;
	reunite_party();
	if (get_flag(51,2) == 0)
		message_dialog("As you pass through this portal, you hear the same voice in your head again. _I repeat, do not enter the portal. I do not wish to see you. I will again distort your connection and send you where you do not wish to be. Please leave._","You arrive on the other side, where you began.");
	if (get_flag(51,2) == 1)
		message_dialog("As this portal takes you back to the niche where you entered, you hear the same voice yet again. _I grow tired of this intrusion. Cease disturbing me. If you do not, you will suffer!_","");
	if (get_flag(51,2) == 2)
		message_dialog("Upon entering this portal to return to the niche again, you hear the same voice, this time resigned. _Very well, I will reward your persistence. You may meet with me, but do not bother me for long._","");
	if (get_flag(51,2) < 3)
		inc_flag(51,2,1);
break;

beginstate 12;
if (get_flag(51,3) == 0) {
	reset_dialog();
	add_dialog_str(0,"You find the body of a slith, or, more accurately, the skeleton of one. Some of its armor is still intact, but not much, and its possessions are all destroyed beyond repair. However, it has a spellbook that appears to be partly still legible.",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 15.
		if (get_skill_total(42) >= 15) {
			set_flag(51,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,6) == 0) // flag for spell
				add_dialog_str(1,"The book describes an interesting new spell, one that greatly confuses the mind of your enemy, making it less able to think clearly. (You learn a level of Destroy Mind.)",0);
			else
				add_dialog_str(1,"It's a spellbook describing how to cast Destroy Mind more efficiently. You read over it carefully.",0);
			inc_flag(292,6,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 Destroy Mind again, but you don't learn anything new from it.","");
break;

beginstate 13;
	block_entry(1);
	reset_dialog_preset_options(2);
	if (run_dialog(1) == 2) {
		flip_terrain(17,16);
		play_sound(-94);
		swap_terrain(7,17,167,0);
		}
break;

beginstate 14;
	block_entry(1);
	reset_dialog_preset_options(2);
	if (run_dialog(1) == 2) {
		flip_terrain(8,14);
		play_sound(-94);
		swap_terrain(14,9,167,0);
		}
break;

beginstate 15;
	block_entry(1);
	reset_dialog_preset_options(2);
	if (run_dialog(1) == 2) {
		flip_terrain(17,4);
		play_sound(-94);
		swap_terrain(9,12,167,0);
		}
break;

beginstate 16;
	block_entry(1);
	reset_dialog_preset_options(2);
	if (run_dialog(1) == 2) {
		flip_terrain(12,16);
		play_sound(-94);
		flip_terrain(17,8);
		}
break;

beginstate 17;
	block_entry(1);
	reset_dialog_preset_options(2);
	if (run_dialog(1) == 2) {
		flip_terrain(35,36);
		play_sound(-94);
		flip_terrain(34,35);
		}
break;

beginstate 18;
if (get_flag(51,4) == 0) {
	reset_dialog();
	add_dialog_str(0,"The dragon growls as you approach these runes. _Do not go back into my treasure chamber,_ he says. _You must not go any farther._",0);
	add_dialog_str(1,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Keep going.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else {
		make_town_hostile();
		set_flag(51,4,1);
		alter_stat(1000,31,5);
		message_dialog("The dragon roars and attacks. You feel very weak. This may have been a bad idea.","(You have just received a powerful Dread Curse.)");
		}
	}
break;

beginstate 19;
if (get_flag(51,5) == 0) {
	set_flag(51,5,1);
	message_dialog("For how protective he is of it, there's surprisingly little treasure in the dragon's back room. You notice very little of value here.","");
	}
break;

beginstate 20;
if (get_flag(51,6) == 0) {
	reset_dialog();
	add_dialog_str(0,"On this pedestal, you find a tiny spellbook, already open and stained with a few red and a few clear stains -- blood and water, you presume. It describes a single, complex spell.",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 1.
		if (get_skill_total(42) >= 1) {
			set_flag(51,6,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,15) == 0) // flag for spell
				add_dialog_str(1,"The book describes an interesting new spell, one that inflicts terrible damage from powerful magic on an enemy. (You learn a level of Destruction.)",0);
			else
				add_dialog_str(1,"It's a spellbook describing how to cast Destruction more efficiently. You read over it carefully.",0);
			inc_flag(292,15,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 Destruction again, but you don't learn anything new from it.","");
break;

beginstate 21;
if (get_flag(51,7))
	end();

	reset_dialog();
	add_dialog_str(0,"You think that you have detected a trap. Your character with the highest Tool Use skill can attempt to disarm it. However, the trap seems impossibly difficult.",0);
	add_dialog_choice(0,"Leave it alone.");
	add_dialog_choice(1,"Try to disarm the trap.");
	if (run_dialog(0) == 1)
		block_entry(1);
	else {
		message_dialog("You fail to disarm the trap, and it explodes! Also, the dragon roars at you and attacks!","");
		damage_char(1000,get_ran(50,1,10),4);
		kill_char(1000,2,1);
		make_town_hostile();
		}
break;

beginstate 22;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		teleport_party(6,38,0);
break;

beginstate 23;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		teleport_party(1,17,0);
break;

beginstate 24;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		teleport_party(31,41,0);
break;

beginstate 25;
	put_object_on_space(42,19,-1);
break;