// Town 31: Mine
begintownscript;

variables;

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

body;

beginstate INIT_STATE;
	set_crime_tolerance(2);
	
	if (get_flag(31,4) == 4) {
		set_terrain(43,18,138);
		set_terrain(8,1,138);
		set_terrain(20,33,138);
		set_terrain(9,24,0);
		set_terrain(9,25,0);
		set_terrain(9,26,0);
		}
	
	if (get_flag(29,3) == 0) {
		set_terrain(36,22,453);
		set_terrain(36,23,453);
		set_terrain(36,24,453);
		set_terrain(36,25,453);
		set_terrain(36,26,453);
		}
	
	if (get_flag(31,0) == 0) {
		force_instant_terrain_redraw();
		set_flag(31,0,1);
		message_dialog("The air has suddenly become unbearably still. The passage before you is narrow and tense, still like the nephil hunter, stalking its prey, just before it pounces.","You may be sensing something, although you can't quite name it just yet.");
		}
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
if (get_flag(31,4) == 3) {
	i = i + 1;
	if (i == 3) {
		damage_char(1000,get_ran(5,20,50),3);
		print_str_color("The curse strikes you with its power!",2);
		}
	}
break;

beginstate 10;
	if (get_flag(31,1) == 0) {
		set_flag(31,1,1);
		message_dialog("You are certain of it now. There is a presence in these mines, one that does not at all feel natural. It moves constantly, evading your senses, so that you cannot discern what it is.","The sign on the east wall indicates that this place belongs to Anthala, who urges all to leave in no uncertain terms.");
		}
break;

beginstate 11;
	if (get_flag(31,2) == 0) {
		set_flag(31,2,1);
		message_dialog("The tunnel this way has collapsed. You cannot tell how recently it did, but it was long ago enough that the dust has settled.","");
		}
break;

beginstate 12;
	reset_dialog();
	add_dialog_str(0,"You find a diary here. It is written in the modern tongue of the sliths, and it is open to the final entry, which you can still read. It is dated from just under a hundred years ago.",0);
	add_dialog_str(1,"_Where is my goddess Althanazaria? Has she abandoned me? I have always been faithful, but lately, when I pray, I do not hear her soothing voice answering. I hear nothing, or worse, I hear distant mocking laughter, the sound of demons.",0);
	add_dialog_str(2,"_The mine has failed. The curse is too strong for any of us. Miners die every day, every hour. We cannot proceed. The mine must be abandoned. I will leave a warning at the entrance so that none will enter here again.",0);
	add_dialog_str(3,"_I believe still, but I cannot hear her. Where has she gone?_",0);
	run_dialog(1);
break;

beginstate 13;
if (get_flag(31,3) == 0) {
	reset_dialog();
	add_dialog_str(0,"In this desk, you find many papers involving business transactions here in the mind. Shipping receipts to a city named Danatha, mining inventories, correspondences with blacksmiths... and a tiny 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(292,7) > 0) {
				set_flag(31,3,1);
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast Sleep Foe in the most efficient fashion possible. You read over it carefully.",0);
				set_flag(292,7,3); // flag for spell
				}
			else
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast a complex spell, Sleep Foe, 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 Sleep Foe again, but you don't learn anything new from it.","");
	}
break;

beginstate 14;
if (get_flag(31,4) > 0)
	end();
if (char_ok(6))
	end();
	
	activate_hidden_group(1);
	message_dialog("The curse is here! You feel a malevolent presence in this very room. You feel it, but you can't see it anywhere.","You feel yourself getting chills.");
break;

beginstate 15;
if (get_flag(31,4) > 0)
	message_dialog("You approach this evil altar, but it is not active. The presence is not anywhere near here.","");
break;

beginstate 16;
if (get_flag(31,5) == 0) {
	set_flag(31,5,1);
	message_dialog("As you head down this tunnel, you feel the presence in the mines very near. You suddenly realize, with a sinking feeling, that this presence is not the result of a trap but of a curse.","Malevolent curses tend to be much harder to remove than traps. Curses linger, feeding on their own hatred, for centuries. And whatever the result of this curse was, it's in front of you now.");
	}
break;

beginstate 17;
if (get_flag(31,4) != 1)
	end();
if (char_ok(7))
	end();
if (get_flag(31,7) == 1)
	end();
	
	set_flag(31,7,1);
	activate_hidden_group(2);
	message_dialog("You feel it again suddenly. The presence that has been haunting the mines is right here.","");
break;

beginstate 18;
if (get_flag(31,4) != 2)
	end();
if (char_ok(8))
	end();
if (get_flag(31,8) == 1)
	end();
	
	set_flag(31,8,1);
	activate_hidden_group(3);
	message_dialog("You feel it again suddenly. The presence that has been haunting the mines is right here.","");
break;

beginstate 19;
if (get_flag(31,4) != 1)
	message_dialog("You approach this evil altar, but it is not active. The presence is not anywhere near here.","");
break;

beginstate 20;
if (get_flag(31,4) < 2)
	message_dialog("You approach this evil altar, but it is not active. The presence is not anywhere near here.","");
if (get_flag(31,4) == 3)
	message_dialog("This evil altar still hums with power. It must be the center of the curse, and the destruction of the presence has not ended it.","");
break;

beginstate 21;
	inc_flag(31,4,1);
	if (get_flag(31,4) < 3) {
		if (get_flag(31,4) == 1) {
			message_dialog("Very suddenly, the presence fades. It is not gone; you can still feel it, distantly. However, it has moved to a different part of the mines.","It left behind a beautiful ring, though.");
			put_item_on_spot(char_loc_x(6),char_loc_y(6),494);
			}
		else
			message_dialog("Once again, the presence fades. It is still not gone. It is just somewhere else in the mines.","You cannot feel it growing any weaker. If anything, battling it seems to be making it stronger, angrier, and more determined. But what else can you do?");
		erase_char(5 + get_flag(31,4));
		end();
		}
		
	reset_dialog();
	add_dialog_str(0,"You strike a final blow, and the presence collapses. Its semi-material body has taken so much punishment that it can no longer hold together. It bursts, and a thin layer of malice and hatred covers everything, now.",0);
	add_dialog_str(1,"For a moment, you remember how the presence cast its terribly damaging spell. You remember how the energies gathered, and you think you understand what it was doing. You think you could duplicate it. (You learn a level of Destruction.)",0);
	add_dialog_str(2,"But then your eyes blur, reddening and watering. The presence is defeated, but the curse is not over. Why? You turn to the nearby evil altar, which is now humming with magical power. You must destroy it. You must.",0);
	inc_flag(292,15,run_dialog(1));
	set_flag(292,0,1);
	i = 0;
break;

beginstate 22;
if (get_flag(31,4) < 3)
	end();
	
	set_flag(31,4,4);
	
	message_dialog("You begin the Ritual of Sanctification on this evil altar, home of the curse on these mines. It resists mightily, but you continue the ritual, driving out whatever it is that is haunting these mines.","");
	
	put_jagged_zap(43,18,1,3,2);
	put_jagged_zap(43,18,30,3,2);
	put_jagged_zap(43,18,45,3,2);
	put_jagged_zap(43,18,45,30,2);
	put_jagged_zap(43,18,35,37,2);
	put_jagged_zap(43,18,15,36,2);
	run_animation_sound(102);
	
	put_jagged_zap(43,18,10,10,2);
	put_jagged_zap(43,18,25,5,2);
	put_jagged_zap(43,18,38,3,2);
	put_jagged_zap(43,18,46,28,2);
	put_jagged_zap(43,18,40,37,2);
	put_jagged_zap(43,18,20,36,2);
	run_animation_sound(102);
	
	put_jagged_zap(43,18,10,20,2);
	put_jagged_zap(43,18,15,5,2);
	put_jagged_zap(43,18,29,3,2);
	put_jagged_zap(43,18,46,5,2);
	put_jagged_zap(43,18,45,30,2);
	put_jagged_zap(43,18,30,36,2);
	run_animation_sound(102);
	
	set_terrain(43,18,138);
	set_terrain(8,1,138);
	set_terrain(20,33,138);
	set_terrain(9,24,0);
	set_terrain(9,25,0);
	set_terrain(9,26,0);
	put_boom_on_space(43,18,1,0);
	run_animation_sound(152);
	
	reset_dialog();
	add_dialog_str(0,"And finally, it works! You stand, panting, looking at the rubble where the evil altar used to be. The dark feeling of a malevolent presence is lifted from this area, and you feel certain that now you can pass through unharmed.",0);
	add_dialog_str(1,"Suddenly you remember something: you're supposed to be looking for Legare. How could he have come this way, with this sort of obstacle blocking the path? It seems impossible. But where else could he have gone?",0);
	add_dialog_str(2,"You must press on. There is no reason to believe that he went anywhere but further up this mountain, in whatever way he may have done it.",0);
	run_dialog(1);
break;

beginstate 23;
if (get_flag(31,4) == 4)
	end();
if (get_flag(31,6) == 0) {
	set_flag(31,6,1);
	message_dialog("The passage this direction is blocked by magic barriers that are too strong for you to dispel. You feel certain that these barriers are connected to the presence in the mines, the oppressive, humid presence.","");
	}
break;