// Town 8: Machinery
begintownscript;

variables;

short dummy,choice,i,j,tick;
string dlgstr,secdlgstr;

body;

beginstate INIT_STATE;
turn_off_training(1);

	tick = get_current_tick();

	// to open first door
	put_object_on_space(27,45,5);
	
	// to turn into halls
	put_object_on_space(31,43,5);
	put_object_on_space(30,37,6);
	
	// to open doors in halls
	put_object_on_space(25,36,6);
	put_object_on_space(37,36,6);
	
	// first puzzle
	put_object_on_space(20,32,6);
	put_object_on_space(28,22,6);
	put_object_on_space(27,22,6);
	put_object_on_space(22,20,6);
	
	// second puzzle
	put_object_on_space(36,32,5);
	put_object_on_space(39,25,5);
	put_object_on_space(35,25,6);
	
	// third puzzle
	put_object_on_space(33,16,5);
	put_object_on_space(31,7,6);
	put_object_on_space(37,10,6);
	put_object_on_space(32,16,5);
	put_object_on_space(33,15,6);

if (get_flag(8,0) == 1)
	end();
	
	force_instant_terrain_redraw();
	message_dialog("You descend down the stairs into a new level, very deep beneath Bahssikava. You can hear machinery working all around you, even though no one has lived in these caves or maintained the machines in decades.","This place doesn't know that Bahssikava is dead, though. It is still functioning, and presumably so are all of its safeguards and defenses.");
	set_flag(8,0,1);
	
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
	if (tick_difference(tick,get_current_tick()) >= 4)
		{flip_terrain(30,46);
		tick = get_current_tick(); }
break;

beginstate 10;
if (get_flag(8,1))
	end();
	
	reset_dialog();
	add_dialog_str(0,"The vahnatai style of the decor is a bit befuddling. Was there some sort of connection between the ancient Bahssikavans and the vahnatai?",0);
	add_dialog_str(1,"The idea doesn't make much sense, because the vahnatai with whom Avernum has interacted were already Resting by the time Bahssikava was founded.",0);
	if (get_flag(250,2))
		add_dialog_str(2,"You also find it hard to believe that the fully functioning mind crystal that you found upstairs was not influenced by the vahnatai, but anything is possible.",0);
	add_dialog_str(3,"Still, the architectural style of this room is identical to that of the vahnatai, for whatever that's worth.",0);
	set_flag(8,1,run_dialog(1));
break;

beginstate 11;
	flip_terrain(46,42);
	play_sound(94);
break;

beginstate 12;
	reset_dialog();
	add_dialog_str(0,"The stairs lead back up. Do you wish to climb them?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Ascend.");
	choice = run_dialog(1);
	if (choice == 2)
		{set_flag(8,11,1);
		move_to_new_town(7,8,62); }
break;

beginstate 13;
if (get_flag(8,2))
	end();
	
	reset_dialog();
	clear_buffer();
	add_dialog_str(0,"The technology here is amazing. These beam devices are vahnatai inventions that the humans have only recently begun to use. That sliths, of all people, built these devices, and built them well enough to last, is astonishing.",0);
	if (get_flag(250,0) < 4)
		add_dialog_str(1,"You have always thought of sliths as being slow-witted, unintelligent, violent creatures. Evidently that is not necessarily in their nature. This is a bit of a shock.",0);
	if (get_flag(250,0) == 4) 
		{if (get_flag(251,3) == 1)
			append_string("As a slith");
		else
			append_string("As sliths");
		append_string(", you have always had the feeling that you were slow-witted and violent by nature, and also unable to change. As you discover more about these Bahssikavans, you feel more sure that you have always been wrong.");
		get_buffer_text(dlgstr);
		add_dialog_str(1,dlgstr,0);
		}
	if (get_flag(250,0) > 4)
		{if (get_flag(251,3) == 1)
			add_dialog_str(1,"Since you have a slith in your party, you know that sliths deal with prejudice on a daily basis, and they almost come to believe the stereotypes. Seeing that sliths are not necessarily violent and slow-witted must be encouraging to your comrade.",0);
		else
			add_dialog_str(1,"Since you have sliths in your party, you know that sliths deal with prejudice on a daily basis, and they almost come to believe the stereotypes. Seeing that sliths are not necessarily violent and slow-witted must be encouraging to your comrades.",0);
		}
	add_dialog_str(2,"One thing is sure: the secrets of Bahssikava must not remain secrets. If everyone in Avernum knew what sliths were capable of, people would treat them differently.",0);
	set_flag(8,2,run_dialog(1));
break;

beginstate 14;
	message_dialog("There is a control panel here, but you don't know how to work it. You leave it alone, knowing that you are more likely to do something destructive than something useful if you use it.","");
break;

beginstate 15;
if (get_flag(8,4))
	end();
	
	reset_dialog();
	add_dialog_str(0,"As you round this corner, your jaw drops.",0);
	add_dialog_str(1,"It seems that every time you think you've learned the extent of the abilities of the Bahssikavans, they surprise you again. In front of you, you can see an elaborate system of machinery that is still completely functional.",0);
	add_dialog_str(2,"The machinery sends energy into power sources, which power beam projectors. Those beam projecters fire beams into mirrors, which focus the beams and redirect them into new sources. Those sources then power two more projectors.",0);
	add_dialog_str(3,"Those projectors then fire beams into two large crystals, which appears to be the point of all this. The crystals appear to redirect the energy into the ground somehow.",0);
	add_dialog_str(4,"If you understand this setup right, this is the source of power for the whole area. If you damaged these crystals, you would shut off all of the power (including to all of the traps).",0);
	add_dialog_str(5,"Of course, the crystals would also explode and bring the entire cave down around you.",0);
	set_flag(8,4,run_dialog(1));
break;

beginstate 16;
	block_entry(1);
	message_dialog("This area is hot. Very hot. You can't even get close to the crystals without burning yourself badly, so you don't go any farther.","");
break;

beginstate 17;
if (get_flag(8,5))
	end();
	
	message_dialog("Well, now you know why the southernmost power source is not lit. This projector, which would power it, is broken. If you want to get that door open, you'll need some other way to fire up that power source.","");
	set_flag(8,5,1);
break;

beginstate 18;
	message_dialog("As you come close to these runes, you notice the red rune glowing dangerously. It looks extremely unstable, likely to explode and kill someone.","You probably shouldn't walk here.");
break;

beginstate 19;
	message_dialog("You have come to a locked door. Like all the others, it requires power to open, but unfortunately, its power source is shattered. It appears that there is nothing you can do to open this door.","");
break;

beginstate 20;
if (get_flag(8,6))
	end();
	
	message_dialog("Curioser and curioser. In this box you find two cloaks clearly in the style of the vahnatai, presumably worn as additional protection for working on the machinery here.","It seems undeniable that the Bahssikavans knew vahnatai at some point in their history.");
	set_flag(8,6,1);
break;

beginstate 21;
if (get_flag(8,7))
	end();
	
	message_dialog("You have finally managed to open the doors.","");
	award_party_xp(200,get_level(0) + 10);
	
	reset_dialog();
	clear_buffer();
	append_string("You step through, still thinking over what you have seen. The Bahssikavans were truly advanced.");
	if (get_flag(8,4))
		append_string(" They had machinery as complex as anything the Empire has ever built.");
	if (get_flag(8,5))
		append_string(" They had contact with the vahnatai and understood some of their magic.");
	get_buffer_text(dlgstr);
	add_dialog_str(0,dlgstr,0);
	if (get_flag(250,0) < 4)
		add_dialog_str(1,"You have not known many sliths in your life. There are not many left in Avernum and none in the Empire. Sliths tend not to mingle with humans. But you cannot deny that the Bahssikavans were worthy of respect.",0);
	 if (get_flag(250,0) == 4)
	 	add_dialog_str(1,"For the first time in your life, you can feel pride in your heritage. You are not a Darkling. Your spiritual ancestors were these Bahssikavans, even if your hereditary ones were not.",0);
	if (get_flag(250,0) > 4)
		add_dialog_str(1,"Despite your multi-racial party, you have not known many sliths in your life. There are not many left in Avernum and none in the Empire. Sliths tend not to mingle with humans. But you cannot deny that the Bahssikavans were worthy of respect.",0);
	add_dialog_str(2,"But your thoughts are interrupted by the astonishing visual in front of you. Towering over you are two mountainous statues of robed sliths, presumably priests.",0);
	clear_buffer();
	append_string("Presumably that means that you are approaching an altar or shrine of some kind.");
	if ((get_flag(3,9)) || (has_special_item(1) > 0))
		{append_string(" Maybe you will find one of those metal triangles here.");
		if (has_special_item(1) == 4)
			append_string(" You sure hope so, because you don't know where else to look.");
		}
	get_buffer_text(dlgstr);
	add_dialog_str(3,dlgstr,0);
	set_flag(8,7,run_dialog(1));
break;

beginstate 22;
if (get_flag(8,8))
	end();
	
	reset_dialog();
	add_dialog_str(0,"Inside this chest, you find a small metal triangle! What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Take it.");
	choice = run_dialog(1);
	if (choice == 2)
		{change_spec_item(1,1);
		if (has_special_item(1) > 1)
			print_big_str("You now have ",has_special_item(1)," metal triangles.");
		else
			print_str("You now have 1 metal triangle.");
		set_flag(8,8,1);
		}
break;

beginstate 23;
	reset_dialog();
	add_dialog_str(0,"There is a small, simple pool of purple water here. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Drink some.");
	add_dialog_choice(2,"Wash yourself.");
	choice = run_dialog(1);
	if (choice == 2)
		message_dialog("The water tastes bitter. You can't tell if anything happens.","");
	if (choice == 3)
		{message_dialog("The water is cold and soothing. You can't tell if anything happens, though.","");
		set_flag(8,9,1); }
break;

beginstate 24;
	reset_dialog();
	add_dialog_str(0,"There is a small, simple pool of blue water here. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Drink some.");
	add_dialog_choice(2,"Wash yourself.");
	choice = run_dialog(1);
	if (choice == 2)
		message_dialog("The water tastes bitter. You can't tell if anything happens.","");
	if (choice == 3)
		{message_dialog("The water is cold and soothing. You can't tell if anything happens, though.","");
		if (get_flag(8,9) == 1)
			set_flag(8,9,2); 
		}
break;

beginstate 25;
	reset_dialog();
	add_dialog_str(0,"There is a small, simple pool of light blue water here. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Drink some.");
	add_dialog_choice(2,"Wash yourself.");
	choice = run_dialog(1);
	if (choice == 2)
		message_dialog("The water tastes bitter. You can't tell if anything happens.","");
	if (choice == 3)
		{message_dialog("The water is cold and soothing. You can't tell if anything happens, though.","");
		if (get_flag(8,9) == 2)
			set_flag(8,9,3); 
		}
break;

beginstate 26;
if (get_flag(8,10))
	end();
	
	if (get_flag(8,9) < 3)
		{block_entry(1);
		message_dialog("A soft, sibilant voice speaks in your mind. _First you must come from the darkness into the light._","You can't step up onto the platform.");
		}
	else
		{message_dialog("A soft, sibilant voice speaks in your mind. _From the darkness to the light you have come, and you may now approach the altars._","");
		set_flag(8,10,1); }
break;

beginstate 27; // reset first puzzle
		// move characters to safe location
		relocate_character(0,24,32);
		relocate_character(1,25,32);
		relocate_character(2,25,31);
		relocate_character(3,24,31);
		
		// wipe clean rectangle with corners (14,19) and (28,32)
		j = 19;
		while (j <= 32)
			{i = 14;
			while (i <= 28)
				{put_object_on_space(i,j,-1);
				i = i + 1;
				}
			j = j + 1;
			}
			
		put_object_on_space(20,32,6);
		put_object_on_space(28,22,6);
		put_object_on_space(27,22,6);
		put_object_on_space(22,20,6);
		
		set_terrain(27,32,380);
		set_terrain(25,29,380);
		set_terrain(20,29,380);
		set_terrain(18,26,380);
		set_terrain(14,32,380);
		
		set_terrain(17,27,382);
		
		set_terrain(25,27,383);
		set_terrain(25,26,383);
		set_terrain(25,25,383);
		set_terrain(25,24,383);
		set_terrain(26,21,383);
		set_terrain(21,29,383);
		set_terrain(18,29,383);
		set_terrain(18,23,383);
		
		set_terrain(26,29,388);
		set_terrain(17,29,388);
		set_terrain(17,26,388);
		set_terrain(25,21,388);
		set_terrain(20,20,388);		
break;

beginstate 28; // reset second puzzle
		// move characters to safe location
		relocate_character(0,38,32);
		relocate_character(1,38,31);
		relocate_character(2,37,31);
		relocate_character(3,37,32);
		
		j = 17;
		while (j <= 32)
			{i = 32;
			while (i <= 39)
				{put_object_on_space(i,j,-1);
				i = i + 1;
				}
			j = j + 1;
			}
		put_object_on_space(35,16,-1);
		
		put_object_on_space(36,32,5);
		put_object_on_space(39,25,5);
		put_object_on_space(35,25,6);
		
		set_terrain(32,32,380);
		set_terrain(36,27,380);
		set_terrain(37,21,380);
		
		set_terrain(32,24,383);
		set_terrain(32,21,383);
		set_terrain(32,19,383);
		
		set_terrain(36,28,388);
		set_terrain(32,25,388);
		set_terrain(32,22,388);
		set_terrain(32,20,388);
		set_terrain(36,21,388);
		set_terrain(32,17,388);
break;

beginstate 29; // reset third puzzle
	if (get_terrain(39,8) == 380)
			{message_dialog("You can't quite reach the lever here. The beam is blocking your way.","");
			end(); }
			
	reset_dialog_preset_options(2);
	choice = run_dialog(1);
	if (choice == 2)
		{
		// move characters to safe location
		relocate_character(0,38,7);
		relocate_character(1,38,8);
		relocate_character(2,37,8);
		relocate_character(3,36,8);
		
		j = 6;
		while (j <= 8)
			{i = 27;
			while (i <= 31)
				{put_object_on_space(i,j,-1);
				i = i + 1;
				}
			j = j + 1;
			}
		j = 6;
		while (j <= 15)
			{i = 32;
			while (i <= 39)
				{put_object_on_space(i,j,-1);
				i = i + 1;
				}
			j = j + 1;
			}
		put_object_on_space(32,16,-1);
		put_object_on_space(33,16,-1);
		put_object_on_space(34,16,-1);
		put_object_on_space(36,16,-1);
		put_object_on_space(37,16,-1);
		put_object_on_space(38,16,-1);
		put_object_on_space(39,16,-1);
		
		put_object_on_space(33,16,5);
		put_object_on_space(31,7,6);
		put_object_on_space(37,10,6);
		put_object_on_space(32,16,6);
		put_object_on_space(32,15,6);
		
		set_terrain(33,10,380);
		
		set_terrain(34,15,381);
		set_terrain(35,7,381);
		
		set_terrain(34,11,382);
		set_terrain(35,11,382);
		set_terrain(38,11,382);
		
		set_terrain(32,14,383);
		set_terrain(32,11,383);
		set_terrain(32,9,383);
		
		set_terrain(39,11,388);
		set_terrain(32,13,388);
		set_terrain(32,10,388);
		set_terrain(30,8,388);
		set_terrain(36,7,388);
		set_terrain(37,7,388);
		}
break;

beginstate 30;
		j = 39;
		while (j <= 46)
			{i = 15;
			while (i <= 28)
				{put_object_on_space(i,j,-1);
				i = i + 1;
				}
			j = j + 1;
			}
			
		put_object_on_space(27,45,5);
			
		set_terrain(15,46,380);
		set_terrain(24,46,380);
		set_terrain(21,39,382);
break;

beginstate 31;
	play_sound(152);
	play_sound(152);
	flip_terrain(46,42);
	set_terrain(37,8,137);
	set_terrain(39,8,137);
break;

beginstate 32;
if (get_flag(8,3))
	end();
	
	message_dialog("A number of the power sources and beam projectors in this room are broken. It appears that the years since Ancient Bahssikava was abandoned have not been kind to this area. Some things do not work quite right anymore.","The door in front of you, for example, appears to need power in order to open. It is not getting that power right now. Enough equipment is here that you should be able to fix that, though.");
	set_flag(8,3,1);
break;

beginstate 33;
	i = 0;
	while ((char_loc_y(i) != 7) && (i < 6))
		{i = i + 1; }
	
	if (i == 6)
		end();
	
	message_dialog("The rune explodes violently, killing one of the members of your party!","That was pretty stupid.");
	
	kill_char(i,2,0);
break;