begintownscript;

variables;
short choice;
short bubble1 = 0;

body;

beginstate 0; //INIT_STATE

	//first-time setup
	if(get_flag(0,0) == 0) {
		relocate_character(0,23,6);
		relocate_character(1,24,6);
		relocate_character(2,24,5);
		relocate_character(3,23,5);
		set_character_facing(1000,4);
		force_view_center(23,6);
		force_instant_terrain_redraw();
		
		pause(10);
		
		set_flag(0,0,1);
		}

	//regular setup
	set_crime_tolerance(1);
	
	set_char_dialogue_pic(6,510,0);
	//if you haven't got the quest, make sure Martha is there
	if(get_flag(0,3) == 0) {
		set_creature_memory_cell(6,1,1);
		relocate_character(6,23,20);
		force_instant_terrain_redraw();
		}
	//if you've already got the quest, let Martha move
	if(get_flag(0,3) == 1) {
		set_creature_memory_cell(6,1,0);
		force_instant_terrain_redraw();
		}

break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE
	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
	
	//Clear text bubbles
	erase_text_bubbles();
	force_instant_terrain_redraw();
	
	//Martha wandering the archives
	if(get_flag(0,3) == 1) {
		bubble1 = get_ran(1,1,12);
		if(bubble1 == 10)
			text_bubble_on_char(6,"I swear I left it over here...");
		if(bubble1 == 11)
			text_bubble_on_char(6,"Aha! That's it!");
		if(bubble1 == 12)
			text_bubble_on_char(6,"Now where was that?");
		force_instant_terrain_redraw();
	}
	
break;

beginstate 5; //steps
	if(get_flag(0,3) == 0) {
		message_dialog("The mage glares angrily at you as you head towards the stairs. Perhaps you should speak with her first.","");
		block_entry(1);
		end();
		}

	move_to_new_town(1,23,29);
break;

beginstate 6; //blocking lower steps
	block_entry(1);
break;


beginstate 10; //opening the door
	if(get_flag(0,1) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"As you move to open the door, it flings open of its own accord. A group of panicked mages rushes out, clutching piles of paper, worn books, and at least one live rat.",0);
	add_dialog_str(1,"A few forgotten papers flutter to the ground, and one last apprentice skitters past you.",0);
	add_dialog_str(2,"As the dust settles, you hear a woman's voice call from inside.",0);
	add_dialog_str(3,"_Fine! Run, you gutless bastards!_",15);
	run_dialog(1);
	set_flag(0,1,1);
	
	//scatter some papyrus sheets on the ground
	put_item_on_spot(24,8,446);
	put_item_on_spot(22,7,446);
	put_item_on_spot(23,6,446);
	put_item_on_spot(23,6,446);
	put_item_on_spot(23,3,446);
	put_item_on_spot(24,4,446);
	put_item_on_spot(25,6,446);
	force_instant_terrain_redraw();
break;

beginstate 15; //entering the lab proper
	if(get_flag(0,2) == 1)
		end();
		
	reset_dialog();
	add_dialog_str(0,"As you enter the building, you can feel the hairs on the back of your neck stand up. The familiar feeling of concentrated magic washes over you.",0);
	add_dialog_str(1,"It's clear that this place is some sort of arcane facility, but you have no idea what it's for, or why it would clear out so quickly. It's awfully ornate, though.",0);
	add_dialog_str(2,"Your attention is drawn to the signs of a hasty exodus: cast-off belongings, trampled glassware, and piles upon piles of papers. And one mage storming angrily around the room.",0);
	add_dialog_str(3,"She doesn't seem to have noticed you yet.",0);
	run_dialog(1);
	set_flag(0,2,1);
break;

beginstate 20; //scrying room control panel
	message_dialog("This panel is covered in buttons and knobs. There's also a small display which reads _LOCKED_.","You press a button at random, and nothing happens.");
break;

beginstate 21; //scrying room
	if(get_flag(0,4) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"You readily recognize this room as a scrying pool, one of the most useful innovations in the history of magic use. The setup here is rather elaborate, and appears to use a very large amount of energy.",0);
	add_dialog_str(1,"And of course, you don't know how to go about turning it on.",0);
	run_dialog(1);
	set_flag(0,4,1);
break;

beginstate 25; //alchemy lab
	if(get_flag(0,5) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"The smell alone tells you that this room is an alchemical lab of sorts. Flasks line the tables, and raw reagents litter the floor. Like everywhere else, it looks like people left in a hurry.",0);
	add_dialog_str(1,"It appears that someone left something brewing.",0);
	run_dialog(1);
	set_flag(0,5,1);
break;

beginstate 26; //something brewing (specobj trigger)
	reset_dialog();
	add_dialog_str(0,"There's still something brewing in this cauldron. It smells like a cross between freshly-cut roses and elk vomit.",0);
	add_dialog_str(1,"It also seems to be moving around on its own. Off-putting, to say the least.",0);
	run_dialog(1);
break;

beginstate 30; //archives
	if(get_flag(0,6) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"This room is unmistakably the library. Rows of bookshelves stuffed with tomes and scrolls stretch out ahead of you, though many were clearly looted as the building emptied out.",0);
	add_dialog_str(1,"A simple glance at a dropped scroll tells you that most of these papers will be beyond your understanding.",0);
	if((get_flag(0,3) == 1) && (party_can_see_loc(char_loc_x(6),char_loc_y(6)) == 1))
		add_dialog_str(2,"You can see Martha rummaging around the shelves, juggling several scrolls while attempting to read them all at once.",0);
	else if((get_flag(0,3) == 1) && (party_can_see_loc(char_loc_x(6),char_loc_y(6)) != 1))
		add_dialog_str(2,"You can hear someone rummaging around in the shelves.",0);
	run_dialog(1);
	set_flag(0,6,1);
break;

beginstate 31; //searching the shelves (specobj trigger)
	message_dialog("You sort through some of the stuff on the shelves, but can't find anything that looks useful. Or coherent, for that matter. Most of it is highly specific to other people's work.","");
break;