// Town 30: Fort Alora
begintownscript;

variables;

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

body;

beginstate INIT_STATE;
	set_crime_tolerance(2);
	
	put_object_on_space(24,38,5);
	put_object_on_space(17,43,6);
	
	add_range_to_group(35,39,1);
	
	change_blocked(26,35,1);
	change_blocked(26,37,1);
	change_blocked(26,41,1);
	change_blocked(27,40,1);
	change_blocked(27,34,1);
	change_blocked(28,31,1);
	change_blocked(28,33,1);
	change_blocked(28,35,1);
	change_blocked(28,39,1);
	change_blocked(29,40,1);
	change_blocked(30,41,1);
	change_blocked(31,38,1);
	change_blocked(32,40,1);
	change_blocked(34,32,1);
	change_blocked(35,39,1);
	change_blocked(35,37,1);
	change_blocked(35,35,1);
	change_blocked(36,40,1);
	change_blocked(37,41,1);
	change_blocked(37,39,1);
	change_blocked(37,33,1);
	change_blocked(37,31,1);
	change_blocked(30,34,1);
	change_blocked(31,34,1);
	change_blocked(32,34,1);
	change_blocked(33,34,1);
	
if (get_flag(30,4) == 0) {
	change_blocked(50,15,1);
	change_blocked(51,15,1);
	change_blocked(52,15,1);
	change_blocked(53,15,1);
	change_blocked(54,15,1);
	}
	
	// Set the gates properly.
	if (get_flag(30,1) > 1) {
		set_terrain(31,55,28);
		set_terrain(32,55,28);
		set_terrain(33,55,28);
		if (get_flag(30,1) == 2) {
			set_terrain(54,31,33);
			set_terrain(54,32,33);
			set_terrain(54,33,33);
			}
		else {
			set_terrain(31,23,32);
			set_terrain(32,23,32);
			set_terrain(33,23,30);
			}
		}
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
if (get_flag(30,4) != 1)
	end();
	
	i = 48;
	while (i <= 56) {
		j = 6;
		while (j <= 15) {
			put_field_on_space(i,j,5);
			j = j + 1;
			}
		i = i + 1;
		}
	i = 51;
	while (i <= 53) {
		j = 16;
		while (j <= 19) {
			put_field_on_space(i,j,5);
			j = j + 1;
			}
		i = i + 1;
		}
break;

beginstate 10; // Nagas appear!
	activate_hidden_group(2);
	put_boom_on_char(8,2,0);
	put_boom_on_char(9,2,0);
	run_animation_sound(10);
break;

beginstate 11; // Avatars appear!
	activate_hidden_group(3);
	put_boom_on_char(10,2,0);
	put_boom_on_char(11,2,0);
	put_boom_on_char(12,2,0);
	put_boom_on_char(13,2,0);
	run_animation_sound(10);
break;

beginstate 12; // Demons appear!
	activate_hidden_group(4);
	put_boom_on_char(14,2,0);
	put_boom_on_char(15,2,0);
	put_boom_on_char(16,2,0);
	put_boom_on_char(17,2,0);
	run_animation_sound(10);
break;

beginstate 13; // Cold damage!
	put_effect_on_char (0,11,1,2);
	put_effect_on_char (1,11,1,2);
	put_effect_on_char (2,11,1,2);
	put_effect_on_char (3,11,1,2);
	run_animation_sound(75);
	damage_char(1000,get_ran(5,30,50),5);
break;

beginstate 14; // Acid damage!
	put_effect_on_char (0,13,1,2);
	put_effect_on_char (1,13,1,2);
	put_effect_on_char (2,13,1,2);
	put_effect_on_char (3,13,1,2);
	run_animation_sound(42);
	damage_char(1000,get_ran(5,20,40),6);
break;

beginstate 15; // Blade wall everywhere!
	i = 26;
	while (i <= 37) {
		j = 31;
		while (j <= 41) {
			put_field_on_space(i,j,5);
			j = j + 1;
			}
		i = i + 1;
		}
break;

beginstate 16; // Ice wall everywhere!
	i = 26;
	while (i <= 37) {
		j = 31;
		while (j <= 41) {
			put_field_on_space(i,j,4);
			j = j + 1;
			}
		i = i + 1;
		}
break;

beginstate 17; // Fire wall everywhere!
	i = 26;
	while (i <= 37) {
		j = 31;
		while (j <= 41) {
			put_field_on_space(i,j,1);
			j = j + 1;
			}
		i = i + 1;
		}
break;

beginstate 18; // Sleep cloud everywhere!
	i = 26;
	while (i <= 37) {
		j = 31;
		while (j <= 41) {
			put_field_on_space(i,j,0);
			j = j + 1;
			}
		i = i + 1;
		}
break;

beginstate 19; // messing with the gates
	if (get_flag(30,1) == 0)
		set_flag(30,1,1);
		
	reset_dialog();
	add_dialog_str(0,"Here, there is a large wheel that can rotate only partway around. Turning the wheel moves a spoke into one of three slots: a high, a center, and a low. It is connected to the ground, but what it does is not obvious from the setup.",0);
	if (get_flag(30,1) == 1)
		add_dialog_str(1,"The spoke is currently in the lowest position.",0);
	if (get_flag(30,1) == 2)
		add_dialog_str(1,"The spoke is currently in the center position.",0);
	if (get_flag(30,1) == 3)
		add_dialog_str(1,"The spoke is currently in the highest position.",0);
	add_dialog_str(2,"What do you do?",0);
	if (get_flag(30,1) == 1)
		add_dialog_choice(0,"Leave the spoke in the lowest position.");
	else
		add_dialog_choice(0,"Turn the spoke to the lowest position.");
	if (get_flag(30,1) == 2)
		add_dialog_choice(1,"Leave the spoke in the center position.");
	else
		add_dialog_choice(1,"Turn the spoke to the center position.");
	if (get_flag(30,1) == 3)
		add_dialog_choice(2,"Leave the spoke in the highest position.");
	else
		add_dialog_choice(2,"Turn the spoke to the highest position.");
	choice = run_dialog(1);
	if (choice == 1) {
		if (get_flag(30,1) == 1)
			end();
		
		set_flag(30,1,1);
		set_terrain(31,55,32); // open
		set_terrain(32,55,32);
		set_terrain(33,55,32);
		set_terrain(54,31,29); // closed
		set_terrain(54,32,29);
		set_terrain(54,33,29);
		set_terrain(31,23,28); // closed
		set_terrain(32,23,28);
		set_terrain(33,23,28);
		play_sound(99);
		print_str_color("You hear the sounds of chains and grinding gears.",2);
		}
	if (choice == 2) {
		if (get_flag(30,1) == 2)
			end();
		
		set_flag(30,1,2);
		set_terrain(31,55,28); // closed
		set_terrain(32,55,28);
		set_terrain(33,55,28);
		set_terrain(54,31,33); // open
		set_terrain(54,32,33);
		set_terrain(54,33,33);
		set_terrain(31,23,28); // closed
		set_terrain(32,23,28);
		set_terrain(33,23,28);
		play_sound(99);
		print_str_color("You hear the sounds of chains and grinding gears.",2);
		}
	if (choice == 3) {
		if (get_flag(30,1) == 3)
			end();
		
		set_flag(30,1,3);
		set_terrain(31,55,28); // closed
		set_terrain(32,55,28);
		set_terrain(33,55,28);
		set_terrain(54,31,29); // closed
		set_terrain(54,32,29);
		set_terrain(54,33,29);
		set_terrain(31,23,32); // open
		set_terrain(32,23,32);
		set_terrain(33,23,32);
		play_sound(99);
		print_str_color("You hear the sounds of chains and grinding gears.",2);
		}
break;

beginstate 20;
if (get_flag(30,2) == 0) {
	set_flag(30,2,1);
	activate_hidden_group(1);
	put_boom_on_char(6,2,0);
	put_boom_on_char(7,2,0);
	run_animation_sound(10);
	}
break;

beginstate 21;
if (get_flag(30,0) == 0) {
	set_flag(30,0,1);
	message_dialog("You enter an old fort (or so you judge it by the decor). The west wall names this place, in barely legible writing, Fort Alora. It is probably abandoned: if any intelligent creatures were still living here you would have been met at the door by guards.","It is not encouraging, however, that some slith decided to write at the entrance, _Kithynoth thaanoth_: Danger! Death!");
	}
break;

beginstate 22;
if (get_flag(30,3) == 0) {
	reset_dialog_preset_options(1);
	add_dialog_str(0,"In this sarcophagus, you find the thin bones of an ancient vahnatai warrior. All else has decayed completely, and his waveblade is broken in half, but a crystal wand lays by his side, gleaming pure as polished glass. What do you do?",0);
	if (run_dialog(1) == 2) {
		message_dialog("You carefully lift out the crystal wand from the sarcophagus and silently give thanks to the vahnatai who left it behind for you. Hopefully, you have not angered anyone's spirit by doing so.","");
		set_flag(30,3,reward_give(447));
		}
	}
break;

beginstate 23;
if (get_flag(30,4) == 0) {
	change_blocked(50,15,0);
	change_blocked(51,15,0);
	change_blocked(52,15,0);
	change_blocked(53,15,0);
	change_blocked(54,15,0);
	
	activate_hidden_group(6);
	put_boom_on_char(24,2,0);
	put_boom_on_char(25,2,0);
	put_boom_on_char(26,2,0);
	put_boom_on_char(27,2,0);
	set_terrain(50,20,453);
	set_terrain(51,20,453);
	set_terrain(52,20,453);
	set_terrain(53,20,453);
	run_animation_sound(10);
	reset_dialog();
	add_dialog_str(0,"As you step on these runes, you trigger a trap. In front of you appear four vahnatai! Vahnatai have not been seen by Avernites for several years, but you recognize them from descriptions you've heard.",0);
	add_dialog_str(1,"Something strikes you as odd about these vahnatai, though. Their movements are not those of a free-willed being, but they are not those of the undead either. You can't place exactly what they look like, but whatever it is, it's not normal.",0);
	add_dialog_str(2,"The air suddenly begins to feel thicker, too.",0);
	set_flag(30,4,run_dialog(1));
	}
break;

beginstate 24;
if (get_flag(30,5) == 0) {
	reset_dialog();
	add_dialog_str(0,"You step on these runes, ready for the worst, and you feel a magical presence scanning you. Your hands go to your weapons, and you feel the presence finish its examination.",0);
	add_dialog_str(1,"However, nothing happens. You glance around to make sure, but no horrible monsters have sprung up anywhere. It must approve of you approaching the sarcophagus.",0);
	add_dialog_str(2,"Perhaps the blue runes contained an automatic trap, but the green runes have an intelligent magical one. This is an encouraging prospect.",0);
	set_flag(30,5,run_dialog(1));
	}
break;

beginstate 25;
if (get_flag(30,6) == 0) {
	reset_dialog();
	add_dialog_str(0,"There are many papyrus scrolls on these shelves that have decayed, and a few that remain that are on trivial topics. But in all of this mess, you find a partially intact 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 24.
		if (get_skill_total(42) >= 24) {
			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,3) > 0) {
				set_flag(30,6,1);
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast Filth in the most efficient fashion possible. You read over it carefully.",0);
				set_flag(292,3,3); // flag for spell
				}
			else
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast a complex spell, Filth, 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 Filth again, but you don't learn anything new from it.","");
break;

beginstate 26;
	reset_dialog();
	add_dialog_str(0,"There are many papyrus scrolls on these shelves that have decayed, and a few that remain that are on trivial topics. But in all of this mess, you find a partially intact 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) {
		message_dialog("You struggle to read the difficult language, which is obscure and jargon-laden. You feel yourself getting a headache, and then your stomach feels ill. The weakness spreads throughout your whole body.","You close the book, but it's too late: you feel terrible. (You have acquired a Dread Curse.)");
		alter_stat(1000,31,2);
		}
break;

beginstate 27;
	reset_dialog();
	add_dialog_str(0,"There are many papyrus scrolls on these shelves that have decayed, but you find something else, too: a book that is almost completely intact.",0);
	add_dialog_str(1,"Reading strange books can be dangerous: they can carry curses or other terrible effects. However, they can also have great secrets 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();
		add_dialog_str(0,"It is written in a strange mixture of the classical and modern tongues of the sliths. It reads as follows:",0);
		add_dialog_str(1,"_Pessoss the Meaty pursued the beautiful princess Kathana, exclaiming, 'Please, honey, come back to me! I didn't mean to do what I did with all those other women! And I didn't really mean what I said about your mother! Don't be such a syykhatatha!'_",0);
		add_dialog_str(2,"You're not quite sure what that last word means, but you're pretty sure that it's not complimentary. The story goes on in this vein for some time.",0);
		run_dialog(1);
		}
break;

beginstate 28;
	i = 0;
	if (char_ok(24))
		i = i + 1;
	if (char_ok(25))
		i = i + 1;
	if (char_ok(26))
		i = i + 1;
	if (char_ok(27))
		i = i + 1;
	if (i > 1)
		end();
	
	set_flag(30,4,2);
	set_terrain(50,20,0);
	set_terrain(51,20,0);
	set_terrain(52,20,0);
	set_terrain(53,20,0);
	put_effect_on_space(50,20,3,1,2);
	put_effect_on_space(51,20,3,1,2);
	put_effect_on_space(52,20,3,1,2);
	put_effect_on_space(53,20,3,1,2);
	run_animation();
	message_dialog("As you strike down the last of the strange vahnatai, the air becomes freer, and the trap deactivates.","One more set of runes lies between you and the sarcophagus.");
break;

beginstate 29;
if (get_flag(30,7) == 0) {
	set_flag(30,7,1);
	message_dialog("As you enter this room, you see the machinery for energy beams scattered around the room. You recognize this setup from your experiences in Bahssikava: the door in the southeast corner needs power to open.","You must redirect the beam in the north to hit the unpowered source in the southeast in order to be able to pass through that door.");
	}
break;

beginstate 30;
if (get_flag(30,9) == 0) {
	reset_dialog();
	add_dialog_str(0,"On the pedestal here is a very small spellbook. It is old and tattered, but one page is 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 24.
		if (get_skill_total(42) >= 24) {
			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,4) > 0) || (get_flag(292,5) > 0)) {
				set_flag(30,9,1);
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast Capture Soul and Recall Soul in the most efficient fashion possible. You read over it carefully.",0);
				set_flag(292,4,3); // flag for spell
				set_flag(292,5,3); // flag for spell
				}
			else
				add_dialog_str(1,"It's a very advanced spellbook describing how to cast two complex spells, Capture Soul and Recall Soul, in a very efficient manner. Unfortunately, you are completely unfamiliar with the spells, 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 Capture Soul and Recall Soul again, but you don't learn anything new from it.","");
break;

beginstate 31;
if (get_flag(30,8) == 0) {
	set_flag(30,8,1);
	message_dialog("There is a switch on this side of the door that is clearly marked as power for the door. You throw it, which should leave the door permanently powered, regardless of the status of the source on the other side.","");
	}
break;

beginstate 32;
if (get_flag(30,10) == 0) {
	set_flag(30,10,1);
	message_dialog("Presumably, once upon a time, there was something in this room. By the looks of it, it was taken by force some very long time ago. By very brutal, bloody force.","");
	}
break;

beginstate 33;
if (get_flag(30,11) == 0) {
	set_flag(30,11,1);
	message_dialog("This room is filled with lots of complex machinery, most of it intended to redirect beams at power sources, and almost all of it shattered beyond repair.","Fortunately, the one source that powers the door is still intact, meaning that you don't have to worry about any of this.");
	}
break;

beginstate 34;
	message_dialog("These gravestones list names, quite a number of names, of sliths who are buried here.","");
break;