// Town 16: Under Khar-Grav
begintownscript;

variables;

short choice,dummy,i,j,when_on;
string dlgstr;

body;

beginstate INIT_STATE;
	change_blocked(42,3,1);
	change_blocked(42,4,1);
	change_blocked(42,5,1);
	change_blocked(42,6,1);
	change_blocked(42,7,1);
	change_blocked(42,8,1);
	change_blocked(42,9,1);
	change_blocked(42,10,1);

	set_flag(16,11,1); // because the southeast puzzle doesn't work, sadly

	put_object_on_space(22,39,6);
	put_object_on_space(27,42,5);
	put_object_on_space(26,32,5);
	put_object_on_space(34,39,6);
	put_object_on_space(45,46,5);
	put_object_on_space(37,42,6);

	if (get_flag(16,11) == 1)
		set_terrain(39,26,388);
	if (get_flag(16,9) == 1)
		set_terrain(21,1,388);
	if (get_flag(16,10) == 1)
		set_terrain(4,18,388);
	
	when_on = 0;

if (get_flag(14,11) == 1)
	flip_terrain(21,27);

if (get_flag(16,2) == 1)
	{set_terrain(12,33,30);
	set_terrain(12,35,30); }

if (get_flag(16,0) != 0)
	end();
	
	force_instant_terrain_redraw();
	message_dialog("You descend the ladder to a floor beneath the demon strongholds. The sounds of heavy machinery are louder here than they were above, and you can sense magical energy all around you.","If one could strike a solid, definitive blow against Nolagh-Khar anywhere, this would be the place where it would be possible.");
	set_flag(16,0,1);
break;

beginstate EXIT_STATE; // keep track of solved puzzles
	if (get_terrain(39,26) == 388)
		set_flag(16,11,1);
	else
		set_flag(16,11,0);
		
	if (get_terrain(21,1) == 388)
		set_flag(16,9,1);
	else
		set_flag(16,9,0);
		
	if (get_terrain(4,18) == 388)
		set_flag(16,10,1);
	else
		set_flag(16,10,0);
		
	if ((get_flag(16,11) == 1) && (get_flag(16,9) == 1) && (get_flag(16,10) == 1))
	set_flag(16,12,1);
break;

beginstate START_STATE;
	if (get_terrain(37,33) == 388)
		end();
		
	if (tick_difference(when_on,get_current_tick()) > 2)
		{set_terrain(37,33,388);
		set_terrain(37,37,388);
		print_str("The beam projectors flash off!"); }
break;

beginstate 10;
if ((get_flag(250,4) == 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 wish to climb it and ascend to a higher level?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice == 2)
		move_to_new_town(15,48,29);
break;

beginstate 11;
	flip_terrain(12,31);
	flip_terrain(12,37);
break;

beginstate 12;
	flip_terrain(12,31);
	flip_terrain(12,33);
	flip_terrain(12,35);
break;

beginstate 13;
	flip_terrain(12,31);
	flip_terrain(12,35);
break;

beginstate 14;
	flip_terrain(12,33);
	flip_terrain(12,37);
break;

beginstate 15;
if (get_flag(16,1) != 0)
	end();
	
	message_dialog("You see before you a strange configuration of gates and runes. It appears that Nolagh-Khar, rather than using a simple door lock, has protected his machines with a gate combination lock.","Evidently he is counting on the stupidity of his underlings. He never expected an adventuring party to come through and assault him.");
	set_flag(16,1,1);
break;

beginstate 16;
	set_flag(16,2,1);
break;

beginstate 17;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		{teleport_party(45,3,0);
		if (get_flag(16,11) == 1)
			set_terrain(39,3,388);
		else
			set_terrain(39,3,384);
		if (get_flag(16,9) == 1)
			set_terrain(33,1,388);
		else
			set_terrain(33,1,384);
		if (get_flag(16,10) == 1)
			set_terrain(34,3,388);
		else
			set_terrain(34,3,384);
		if ((get_flag(16,11) == 1) && (get_flag(16,9) == 1) && (get_flag(16,10) == 1))
			set_terrain(36,1,388);
		else
			set_terrain(36,1,384);
		}
	block_entry(1);
break;

beginstate 18;
	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)
		dummy = try_to_split_party(2,36,1);
	block_entry(1);
break;

beginstate 19;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		teleport_party(2,1,0);
	block_entry(1);
break;

beginstate 20;
// Sadly, I couldn't get the room in the southeast to work. It was my most
// ambitious beam projector puzzle yet, but BoA's laser code isn't robust enough
// to handle it. I may redo it for the second round of beta.
//	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)
//		dummy = try_to_split_party(23,32,1);
//	block_entry(1);
	message_dialog("This portal is inactive. It won't take you anywhere.","");
break;

beginstate 21;
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	set_flag(16,4,i);
	
if (get_flag(16,3) != 0)
	end();
	
	message_dialog("This room is exceedingly hot. The lava in the middle of it is hotter than most lava, and if you stepped on it, you'd die. Presumably it isn't a problem for demonkind, but it is a significant obstacle to you.","For some strange reason, there is an enchantment on this room. It may be possible for you to get across this lava, despite its incredible heat. Still, it appears that it would be quite easy to get trapped out there if you took a wrong step.");
	set_flag(16,3,1);
break;

beginstate 22;
	message_dialog("The magic in this room swirls, changing the ground right before your eyes! (The floors are reset.)","");
	set_floor(2,34,100);
	set_floor(2,33,100);
	set_floor(3,33,100);
	set_floor(3,32,100);
	set_floor(4,32,100);
	set_floor(3,31,100);
	set_floor(3,30,100);
	set_floor(4,30,100);
	set_floor(1,29,100);
	set_floor(2,29,100);
	set_floor(3,29,100);
	set_floor(1,28,100);
	set_floor(2,27,100);
	set_floor(3,27,100);
	set_floor(2,26,100);
	set_floor(3,26,100);
	set_floor(6,26,100);
	set_floor(6,27,100);
	set_floor(6,28,100);
	set_floor(5,29,100);
	set_floor(8,28,100);
	set_floor(8,29,100);
	set_floor(7,29,100);
	set_floor(8,30,100);
	set_floor(6,31,100);
	set_floor(7,31,100);
	set_floor(6,32,100);
	set_floor(6,33,100);
	set_floor(5,34,100);
	set_floor(7,34,100);
	i = 1;
	while (i <= 8)
		{set_floor(i,35,100);
		i = i + 1;
		}
break;

beginstate 23;
	reset_dialog();
	add_dialog_str(0,"In front of you is a simple control panel with a single switch. It has two positions, both with labels you can't read. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Flip the switch to the other position.");
	if (run_dialog(1) == 2)
		flip_terrain(4,18);
break;

beginstate 24;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		reunite_party();
	block_entry(1);
break;

beginstate 25;
	set_flag(16,7,1);

if (get_flag(16,6) != 0)
	end();
	
	message_dialog("You step into this room and observe immediately that these crystals are filled with magical energy. Loosely contained magical energy, you realize, seeing a large spark from one of the crystals.","If you get too close to any of them, a crystal may release some energy, with potentially devastating consequences.");
	set_flag(16,6,1);
break;

beginstate 26;
	message_dialog("One of the nearby crystals discharges!","");
	i = random_party_member();
	put_boom_on_char(i,0,0);
	run_animation_sound(5);
	kill_char(i,2,1);
break;

beginstate 27;
	set_flag(16,7,0);
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		teleport_party(9,39,0);
	block_entry(1);
break;

beginstate 28;
	reset_dialog();
	add_dialog_str(0,"In front of you is a simple control panel with a single switch. It has two positions, both with labels you can't read. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Flip the switch to the other position.");
	if (run_dialog(1) == 2)
		flip_terrain(21,1);
break;

beginstate 29;
if (get_flag(16,5) != 0)
	end();
	
	message_dialog("You unlock the simple bolt lock from this side. You can now pass this door freely.","");
	set_flag(16,5,1);
break;

beginstate 30;
	reset_dialog();
	add_dialog_str(0,"In front of you is a simple control panel with a single switch. It has two positions, both with labels you can't read. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Flip the switch to the other position.");
	if (run_dialog(1) == 2)
		flip_terrain(39,26);
break;

beginstate 31;
	set_height(44,39,1);
	set_height(44,40,1);
	set_height(44,41,1);
	set_height(44,42,1);
	set_height(44,43,1);
	print_str("The ground to the east shifts dramatically!");
break;

beginstate 32;
	set_height(44,39,5);
	set_height(44,40,5);
	set_height(44,41,5);
	set_height(44,42,5);
	set_height(44,43,5);
	print_str("The ground to the east shifts dramatically!");
break;

beginstate 33;
	set_terrain(37,33,384);
	set_terrain(37,37,384);
	when_on = get_current_tick();
	print_str("The beam projectors flash on!");
break;

beginstate 34;
if (get_flag(16,8) != 0)
	end();
	
	message_dialog("The beam projector in front of you is on a track. It looks as though it will slide south if you push it hard enough.","");
	set_flag(16,8,1);
break;

beginstate 35;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		reunite_party();
	block_entry(1);
break;

beginstate 36;
	if (get_terrain(42,34) == 380)
		{block_entry(1);
		message_dialog("You try to squeeze past the beam projector in your way, but you can't quite fit. You can move it, either, because it's too firmly planted in the ground. You can't pass as long as it is here.",""); }
break;

beginstate 37;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		teleport_party(15,39,0);
	block_entry(1);
break;

beginstate 38; // the critical terrains are (39,26), (21,1), and (4,18)
	i = 0;
	if (get_terrain(39,26) == 388)
		i = i + 1;
	if (get_terrain(21,1) == 388)
		i = i + 1;
	if (get_terrain(4,18) == 388)
		i = i + 1;
		
	reset_dialog();
	add_dialog_str(0,"From here you can see the incredible machinery that is making this whole place hum. You don't have the technical expertise to understand the ins and outs of this system, but it's pretty obvious that it's designed to channel a LOT of power somewhere.",0);
	if (i == 0)
		add_dialog_str(1,"And it's humming along perfectly. If this power supply is helping Nolagh-Khar, you probably should shut it off before you try to fight him.",0);
	if ((i > 0) && (i < 3))
		add_dialog_str(1,"It's not working at full power, though. You've disrupted a little bit of its functionality, but not enough to shut the whole thing down. Nolagh-Khar can probably still draw significant power from this.",0);
	if (i == 3)
		add_dialog_str(1,"However, it's shut off at the moment. You've managed to disrupt the whole thing pretty badly, and Nolagh-Khar should be badly weakened by your efforts.",0);
	dummy = run_dialog(1);
break;