// Town 20: Mount Galthrax (L2)
begintownscript;

variables;

short choice,dummy,i,j,which_flag,golem_type,hold_loc_x,hold_loc_y;
string dlgstr;

body;

beginstate INIT_STATE;
turn_off_training(1);
set_name(28,"Experimental Lich");

char_give_item(79,464); // Helm of Runes

change_blocked(1,17,1);
change_blocked(1,18,1);
change_blocked(1,19,1);
change_blocked(2,17,1);
change_blocked(2,18,1);
change_blocked(2,19,1);

change_blocked(41,18,1);
change_blocked(42,18,1);
change_blocked(41,19,1);
change_blocked(42,19,1);

change_blocked(53,58,1);
change_blocked(53,59,1);
change_blocked(53,60,1);
change_blocked(54,58,1);
change_blocked(54,59,1);
change_blocked(54,60,1);
change_blocked(55,58,1);
change_blocked(55,59,1);
change_blocked(55,60,1);

if (get_flag(15,22) == 0)
	erase_char(12);
	
if (get_flag(20,20))
	set_terrain(52,1,138);
	
if (get_flag(20,4) == 2) {
	set_terrain(58,1,0);
	erase_char(28);
	}
	
if (get_flag(20,0) != 0)
	{force_instant_terrain_redraw();
	message_dialog("You climb down a short ladder, entering a new level. You can hear some activity around you, but not much from the nearby room. Evidently the Demon Golem's secret entrance allowed him relatively private access to the inside of the mountain.","What exactly is in the interior of this mountain is still an open question, however.");
	set_flag(20,0,1);
	}
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
if (get_flag(20,11) == 3)
	{set_flag(20,11,4);
	message_dialog("Now that the spirits are dead, you have a moment to look around the room. This room appears to be a place for storing almost-finished undead. Judging by the cages and the blood and goo, the undead can sometimes get unruly.","The ghoul in the middle of the room is not completely animated yet, apparently. It stands vacantly, doing nothing. It seems not to be a threat.");
	}

if (get_flag(20,23) == 1)
	end();
	
if (is_field_on_space(50,21,6))
	{if (get_terrain(51,21) != 47)
		{i = 0;
		while (char_ok(i) == 0)
			{i = i + 1; }
		j = 0; // If j gets set to 1, party was stupid and dies.
		if (char_loc_y(i) <= 26)
			j = 1;
		else
			if (get_terrain(52,26) != 48)
				j = 1;
		}
	if (j)
		{message_dialog("Quickfire spills out of the room and, since you foolishly didn't close the door, reaches you.","Your death is quick but painful.");
		kill_char(1000,2,0);
		}
	set_flag(20,23,1);
	message_dialog("You hear the sound of the quickfire obliterating the dead body storage room. This undead factory will be out of raw materials for a good long while.","");
	}
			
	
break;

beginstate 10;
	block_entry(1);
	message_dialog("These runes glare a bright green when you step near them. You are not permitted to cross.","");
break;

beginstate 11;
if ((get_flag(250,5) == 1) && (has_item(451) == 0))
	{message_dialog("You can't just leave Phaedra behind. You must take her with you to the guardpost immediately or she will die.","");
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"There is a ladder here. Do you climb up it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb up it.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(19,5,37);
break;

beginstate 12;
if (get_flag(20,1) != 0)
	end();
	
	set_flag(20,1,1);
	message_dialog("You emerge in what appears to be a food storage room. You can smell all sorts of edible items, ranging from the familiar aromas of mushrooms and lizard meat to the more exotic scents of cave cow steak and some mysterious meat that you cannot identify.","Golems don't eat, at least in your experience. They are machines. Whatever is down here must be different and must require food, and quite a lot of it, at that.");
break;

beginstate 13;
if (get_flag(20,2) != 0)
	end();
	
	set_flag(20,2,1);
	message_dialog("You come to a large open space in the mountain interior. There is a pool here, with a group of statues in the center. They appear to be four-legged horned monsters, ornately decorated like idols.","There is a crowd of undead around the pool. They each are headed in different directions, doing different tasks. Some are carrying food. Like golems, undead don't eat, though. Something else must require all the food that you saw in that storage room.");
break;

beginstate 14;
if (get_flag(20,3) == 0)
	{set_flag(20,3,1);
	message_dialog("The moment you step into this foul room, your senses begin screaming. The stench of death hangs thickly over this room like a deep black shroud, and breathing this air is like drinking the juice of a rotten apple.","Evidently this is where the undead come from. These dead bodies go through the processes in the preceding rooms until they become undead. If you are to stop the manufacture of these dark servants, you must do it here.");
	}

i = 0;
while (char_ok(i) == 0)
	{i = i + 1; }
if (char_loc_x(i) <= 50)
	end();

if (has_special_item(8))
	{reset_dialog();
	add_dialog_str(0,"You remember the Phoenix Egg that you are carrying. If you used it here, you would be able to destroy the contents of this room.",0);
	add_dialog_str(1,"Since this is the storage room for bodies before they get processed into undead, that would halt production of undead in this area, at least temporarily. However, such an action would be very dangerous.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter the room without using the Phoenix Egg.");
	add_dialog_choice(2,"Throw the Phoenix Egg and run.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice < 3)
		end();
	
	block_entry(1);
	change_spec_item(8,-1);
	put_field_on_space(45,21,6);
	force_instant_terrain_redraw();
	message_dialog("You throw the Phoenix Egg. It explodes, creating quickfire on the other side of the room!","You'd better close that door and get out of here pretty quickly.");
	}
break;

beginstate 15;
if (get_flag(250,5) == 1) // Phaedra dead
	{add_dialog_str(0,"The ground slopes sharply up here. However, you can't go this way: Phaedra needs you to bring her back to the guardpost.",0);
	dummy = run_dialog(1);
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"The ground slopes sharply up here. Do you climb up it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb up it.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(19,1,10);
break;

beginstate 16;
if (get_flag(20,4) != 1)
	end();
	
	reset_dialog();
	add_dialog_str(0,"The red rune here looks insubstantial, as if you could just rub it out by stepping on it. The bound lich watches you carefully. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Rub out the rune.");
	if (run_dialog(1) == 1)
		end();
		
	block_entry(1);
	set_flag(20,4,2);
	message_dialog("You carefully erase this rune off the floor. It is magic, which means that destroying the spell haphazardly could be dangerous, so you take every reasonable precaution.","It seems to work.");

	put_boom_on_space(58,1,1,0);
	set_terrain(58,1,0);
	run_animation_sound(51);
	
	put_boom_on_char(28,6,0);
	run_animation_sound(4);
	
	text_bubble_on_char(28,"I AM FREE!");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(28,"");
	text_bubble_on_char(28,"Thank you very much,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(28,"");
	if (party_size() != 1)
		text_bubble_on_char(28,"kind adventurers.");
	else
		text_bubble_on_char(28,"kind adventurer.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(28,"");
	text_bubble_on_char(28,"You will be...");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(28,"");
	text_bubble_on_char(28,"rewarded.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(28,"");
	force_instant_terrain_redraw();
	
	put_boom_on_char(28,6,0);
	run_animation_sound(4);
	erase_char(28);
	if (get_flag(19,13)) { // If they've already killed Dargenloth
		put_item_on_spot(56,2,462); // give them the Quickness Gauntlets.
		set_flag(19,16,1);
		}
	force_instant_terrain_redraw();
	
	message_dialog("With that, he is gone. You can only hope that you have done the right thing in freeing the twice-dead lich.","");
	
break;

beginstate 17;
if (get_flag(20,5) != 0)
	end();
	
	set_flag(20,5,1);
	activate_hidden_group(2);
	put_boom_on_char(17,6,0);
	put_boom_on_char(18,6,0);
	put_boom_on_char(19,6,0);
	put_boom_on_char(20,6,0);
	run_animation_sound(54);
break;

beginstate 18;
if (get_flag(20,6) != 0)
	end();
	
	set_flag(20,6,1);
	activate_hidden_group(1);
	put_boom_on_char(13,6,0);
	put_boom_on_char(14,6,0);
	put_boom_on_char(15,6,0);
	put_boom_on_char(16,6,0);
	run_animation_sound(54);
break;

beginstate 19;
if (get_flag(20,7) != 0)
	end();
	
	set_flag(20,7,1);
	message_dialog("The runes here gleam viciously. Presumably whatever is beyond is important and these runes guard it in some way. Stepping on them may trigger something unexpected or difficult to handle.","But then, _unexpected_ and _difficult to handle_ are your specialties.");
break;

beginstate 20;
if (get_flag(20,8) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"Through these windows you can see a breathtaking sight: the source of the river that flows through the guardpost and into Bahssikava, or at least the source of this river on this level.",0);
	add_dialog_str(1,"The river spills down through a hole in the ceiling of the cave, flowing over multiple waterfalls until it finds more steady ground at the same height as the ground on which you are standing.",0);
	add_dialog_str(2,"Surely the undead did not build this room to admire the aesthetic beauty of the falls. It must serve some purpose.",0);
	set_flag(20,8,run_dialog(1));
break;

beginstate 21;
if (get_flag(20,9) != 0)
	end();
	
	set_flag(20,9,1);
	message_dialog("In this room is a sizable crowd of undead. They appear to be in the middle of casting some sort of spell. You don't recognize the spell, but it seems powerful. The blue runes in the middle of the room look very unstable.","Since you have interrupted the spell, the undead creatures turn to you and attack.");
break;

beginstate 22;
if (get_flag(20,10) != 0)
	end();
	
	set_flag(20,10,1);
	message_dialog("You hear some very loud noises coming from the other side of this door. It may be very dangerous to go through, because you can hear the sounds of many different kinds of undead on the other side.","");
break;

beginstate 23;
	inc_flag(20,11,1);
break;

beginstate 24;
if (get_flag(20,12) != 0)
	end();
	
	set_flag(20,12,1);
	message_dialog("It's now clear what these rooms are: an undead factory. In these rooms, bodies go from simple corpses to animated undead. The entire process appears to be streamlined so that hordes of undead can be made at once.","This explains why you've seen so many of these monsters around these caves.");
break;

beginstate 25;
if (get_flag(250,5) == 1) // Phaedra dead
	{add_dialog_str(0,"You have reached some stairs. However, you can't climb them: Phaedra needs you to bring her back to the guardpost.",0);
	dummy = run_dialog(1);
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"You have reached some stairs. Do you climb them?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb them.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(19,43,14);
break;

beginstate 26;
	set_flag(20,13,1);
	message_dialog("As the fiery undead warrior falls to the ground, the nearby lich on the table hisses. It looks at you as if it wishes to speak with you.","");
break;

beginstate 27;
	inc_flag(20,14,1);
break;

beginstate 28;
	message_dialog("The horrific monster bursts into flames, consumed by its own fire. You stare at its ashes momentarily before moving on.","");
	set_flag(20,15,1);
break;

beginstate 29;
	swap_terrain(34,40,0,437);
break;

beginstate 30;
	swap_terrain(18,45,0,437);
	if (get_flag(20,16) == 0)
		{place_monster(17,45,45,0);
		place_monster(17,44,45,0);
		set_flag(20,16,1);
		}
break;

beginstate 31;
	swap_terrain(17,47,0,437);
	if (get_flag(20,17) == 0)
		{place_monster(16,48,45,0);
		place_monster(16,47,45,0);
		set_flag(20,17,1);
		}
break;

beginstate 32;
if (get_flag(20,19) != 0)
	end();
	
	set_flag(20,19,1);
	message_dialog("You can distinctly hear the sounds of something -- some things -- working behind these barriers. You step quietly; it appears that you are safe for now, but dangerous undead are nearby.","");
break;

beginstate 33;
	swap_terrain(31,40,0,437);
break;

beginstate 34;
	swap_terrain(27,38,0,437);
break;

beginstate 35;
	swap_terrain(17,50,0,437);
	if (get_flag(20,18) == 0)
		{place_monster(16,50,138,0);
		set_flag(20,18,1);
		}
break;

beginstate 36;
	swap_terrain(36,43,287,270);
	swap_terrain(37,43,287,270);
	swap_terrain(36,44,287,271);
	swap_terrain(37,44,287,271);
break;

beginstate 37;
if (get_terrain(36,43) == 270)
	{block_entry(1);
	message_dialog("The runes flash brightly as you approach. You get the feeling that if you tried to cross, they would destroy you. You cannot cross.","");
	}
break;

beginstate 38;
	swap_terrain(36,41,0,437);
break;

beginstate 39;
	swap_terrain(36,46,0,437);
break;

beginstate 40;
	message_dialog("This book contains notes from the skeleton warrior's experiments in this room. Evidently the many barriers were due to the need for extreme protection, sealing off various areas for particular experiments.","The kinds of experiments he apparently conducted here, you'd rather not think about.");
break;

beginstate 41;
if (get_flag(250,5) == 1) // Phaedra dead
	{add_dialog_str(0,"The ground slopes sharply down here. However, you can't go this way: Phaedra needs you to bring her back to the guardpost.",0);
	dummy = run_dialog(1);
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"The ground slopes sharply down here. Do you climb down it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb down it.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(21,53,61);
break;

beginstate 42;
if (has_special_item(7) > 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"You find here a page with complex instructions written on it. It looks like it refers to the device immediately west of it. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Take the page of instructions.");
	if (run_dialog(1) == 2)
		change_spec_item(7,1);
break;

beginstate 43;
	// Set the variables for the golem freeing state
	which_flag = 20;
	golem_type = 216; // Blade golem
	hold_loc_x = 52; // location of the holding cell
	hold_loc_y = 1;

	set_state_continue(44);
break;

beginstate 44;
// Golem freeing state for this town
if (get_flag(current_town(),which_flag))
	{message_dialog("You've already done all that you can do here. You leave the control panel alone.","");
	end();
	}

	reset_dialog();
	add_dialog_str(0,"You come to a control panel for this strange device.",0);
	if (has_special_item(7) > 0)
		add_dialog_str(1,"You remember the instructions page that you found and pull it out. It seems to describe how to do something with this control panel.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	if (has_special_item(7) > 0)
		add_dialog_choice(1,"Use the controls, following the instructions on the page.");
	else
		add_dialog_choice(1,"Fiddle with the controls blindly.");
	if (run_dialog(1) == 1)
		end();
		
	if (has_special_item(7) == 0)
		{message_dialog("You press some buttons here and there, but nothing happens.","");
		end();
		}
		
	message_dialog("You follow the instructions and press numerous buttons in a particular order. Nothing seems to happen at first, but then the machine activates!","");
	
	set_flag(current_town(),which_flag,1); // freed this golem
	
	set_terrain(hold_loc_x,hold_loc_y,138);
	put_boom_on_space(hold_loc_x,hold_loc_y,6,0);
	run_animation_sound(53);
		
	if (get_flag(250,7) == 1)
		message_dialog("You free this golem and give him the same lame story about not being here to help the dragon. Disappointed, he vanishes.","");
	
	if (get_flag(250,7) == 2) {
		if (golem_type == 216)
			message_dialog("You free this golem and tell him the same thing that you told the first golem that you freed. You ask if he will help you fight the dragon. He assents quite willingly.","You have gained the aid of a blade golem.");
		else
			message_dialog("You free this golem and tell him the same thing that you told the first golem that you freed. You ask if he will help you fight the dragon. He assents quite willingly.","You have gained the aid of a jewelled golem.");
	}
	
	if (get_flag(250,7) > 0)
		end();
	
	place_monster(hold_loc_x,hold_loc_y,golem_type,2);
	j = 120;
	while (char_ok(j) == 0)
		{j = j - 1; }
		
	run_scenario_script(15);
	
break;

beginstate 45;
	// Set the variables for the golem freeing state
	which_flag = 21;
	golem_type = 216; // Blade Golem
	hold_loc_x = 34; // location of the holding cell
	hold_loc_y = 59;

	set_state_continue(44);
break;

beginstate 46;
	// Set the variables for the golem freeing state
	which_flag = 22;
	golem_type = 219; // Jewelled Golem
	hold_loc_x = 4; // location of the holding cell
	hold_loc_y = 29;

	set_state_continue(44);
break;