begintownscript;

variables;
short choice;

body;

beginstate 0; //INIT_STATE

	//initial dialog
	if(get_flag(1,0) == 0) {
		force_instant_terrain_redraw();
		pause(3);
	
		reset_dialog();
		add_dialog_str(0,"You descend into the basement for the first time, and the rest of your hair starts to stand on end. The air itself feels thick with the unmistakeable crunch of magical energy.",0);
		add_dialog_str(1,"You can hear a loud thrumming down the hallway on your right.",0);
		run_dialog(1);
		set_flag(1,0,1);
	}
	
	//regular setup
	set_crime_tolerance(1);

	//Martha setup
	
	//stabilizing the portal
	if((get_flag(0,3) == 2) && (get_flag(10,0) == 0)) {
		activate_hidden_group(1);
		set_char_dialogue_pic(8,510,0);
		relocate_character(8,20,14);
		}
	//finding you unconscious after trip 1
	else if((get_flag(10,0) == 2) && (get_flag(0,3) < 3)) {
		activate_hidden_group(1);
		set_char_dialogue_pic(8,510,0);
		relocate_character(8,18,20);
		set_character_facing(8,4);
		}
	//after returning from trip 1
	else if((get_flag(10,0) == 2) && (get_flag(0,3) == 3)) {
		activate_hidden_group(1);
		set_char_dialogue_pic(8,510,0);
		relocate_character(8,20,14);
		}
	//after returning from trip 2
	else if(get_flag(10,0) == 4) {
		activate_hidden_group(1);
		set_char_dialogue_pic(8,510,0);
		if(get_flag(0,3) == 4)
			relocate_character(8,20,14);
		}
	//after returning from trip 3
	else if(get_flag(10,0) == 6) {
		activate_hidden_group(1);
		set_char_dialogue_pic(8,510,0);
		relocate_character(8,20,14);
		}
	
	//if arriving from the past...
	
	//first trip back
	if((get_flag(1,0) == 1) && (get_flag(10,0) == 2)) {
		force_view_center(23,47);
		relocate_character(0,18,21);
		relocate_character(1,17,22);
		relocate_character(2,18,23);
		relocate_character(3,19,22);
		set_character_pose(1000,14); //party is passed out
		force_instant_terrain_redraw();
		
		pause(20);
		
		reset_dialog();
		add_dialog_str(0,"You slowly regain consciousness, with a pounding headache to remind you that you're not dead yet.",0);
		run_dialog(1);
		
		//re-center the view
		pause(10);
		force_view_center(18,21);
		force_instant_terrain_redraw();
		pause(10);
		
		reset_dialog();
		add_dialog_str(0,"You eventually open your eyes, to find Martha standing over you.",0);
		add_dialog_str(1,"_Alright, I assume things did not go as planned?_",15);
		run_dialog(1);
		
		pause(5);
		set_character_pose(1000,0); //stand back up
		force_instant_terrain_redraw();
		
		set_flag(1,6,0);
		set_flag(1,0,2);
		}
	//second trip back
	if((get_flag(1,0) == 2) && (get_flag(10,0) == 4)) {
		force_view_center(23,47);
		relocate_character(0,37,32);
		relocate_character(1,37,33);
		relocate_character(2,38,33);
		relocate_character(3,39,33);
		set_character_pose(1000,14); //party is passed out
		force_instant_terrain_redraw();
		
		pause(20);

		reset_dialog();
		add_dialog_str(0,"You slowly regain consciousness... as before, everything hurts.",0);
		run_dialog(1);
		
		//re-center the view
		pause(10);
		force_view_center(37,32);
		force_instant_terrain_redraw();
		pause(10);
		
		reset_dialog();
		add_dialog_str(0,"Eventually, you open your eyes, and find yourself in a storage room. It looks like you landed in a pile of what used to be vegetables.",0);
		add_dialog_str(1,"You swear, as you brush flattened onions off of yourself.",0);
		run_dialog(1);
		
		pause(5);
		set_character_pose(1000,0); //stand back up
		force_instant_terrain_redraw();
		
		set_flag(1,6,0);
		set_flag(1,0,3);
		}
	//third trip back
	if((get_flag(1,0) == 3) && (get_flag(10,0) == 6)) {
		force_view_center(23,47);
		relocate_character(0,28,25);
		relocate_character(1,29,25);
		relocate_character(2,30,25);
		relocate_character(3,30,24);
		set_character_pose(1000,14); //party is passed out
		force_instant_terrain_redraw();
		
		pause(20);
		
		reset_dialog();
		add_dialog_str(0,"You slowly regain consciousness, and you find yourself at the bottom of a different set of stairs.",0);
		run_dialog(1);
		
		//re-center the view
		pause(10);
		force_view_center(28,25);
		force_instant_terrain_redraw();
		pause(10);
		
		reset_dialog();
		add_dialog_str(0,"You can only assume that you're back in the present. As for why, you can only guess.",0);
		add_dialog_str(1,"Regardless, you should probably find Martha.",0);
		run_dialog(1);
		
		pause(5);
		set_character_pose(1000,0); //stand back up
		set_character_pose(8,14); //Martha is passed out 
		force_instant_terrain_redraw();    
		
		set_flag(1,6,0);
		set_flag(1,0,4);
		}
		
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;
	
	//put down fields in the portal chamber
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	put_field_on_space(17 + get_ran(1,1,5), 7 + get_ran(1,1,5), get_ran(1,1,5));
	force_instant_terrain_redraw();
	
break;

beginstate 5; //steps
	if(get_flag(10,0) > 0) {
		message_dialog("You go to leave, but you're too frightened of what might happen. You could cease to exist or something.","You back away from the stairs.");
		block_entry(1);
		end();
		}
	move_to_new_town(0,23,21);
break;

beginstate 10; //test chamber B
	if(get_flag(1,1) == 1)
		end();
	
	play_sound(-49);
	march_party(20,15);
	block_entry(1);
	force_view_center(20,15);
	force_instant_terrain_redraw();
	pause(3);
	
	reset_dialog();
	add_dialog_str(0,"You're nearly blinded by the light coming out of this room, and almost deafened by the noise. Cascades of elemental energy crash around the room, changing from one element to another without warning.",0);
	add_dialog_str(1,"At the center, just like Martha said, is a portal, and it's nothing like you've ever seen. The portal seems out of sync with the rest of the room in a way you can't describe.",0);
	add_dialog_str(2,"There's no doubt in your mind that this is the portal causing all the trouble. You just have no idea how to go about using it without being obliterated by the swirling chaos.",0);
	run_dialog(1);
	set_flag(1,1,1);
	
	//make Martha appear
	pause(5);
	put_boom_on_space(20,14,4,0);
	run_animation_sound(54); //lightning spray
	activate_hidden_group(1);
	set_char_dialogue_pic(8,510,0);
	relocate_character(8,20,14);
	set_character_pose(8,1);
	set_character_facing(8,4);
	force_instant_terrain_redraw();
	pause(2);
	
	message_dialog("Martha appears in front of you with a clap of thunder, adding to the already oppressive noise in the room.","");

	set_character_facing(8,4);
	set_character_pose(8,0);
	force_instant_terrain_redraw();
	pause(2);
	text_bubble_on_char(8,"Oh good, you found it.");
	force_instant_terrain_redraw();
	pause(13);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	
	reset_dialog();
	add_dialog_str(0,"She glances over her shoulder at the portal, and briefly nods.",0);
	add_dialog_str(1,"_Sorry I took so long. I had to look something up so I could stabilize the portal. Didn't want you getting through the portal before then._",15);
	add_dialog_str(2,"She turns to face the portal for a moment, and then starts shouting some strange arcane syllables. You can barely hear her over the storming chaos.",0);
	add_dialog_str(3,"You wait for a moment, trying not to think about the sheer power needed to pull off this kind of madness. Then Martha turns back to you, eerily silhouetted by the portal.",0);
	add_dialog_str(4,"_There! Go, stop Ethalia!_",15);
	run_dialog(1);
	
	set_character_facing(8,0);
	set_character_pose(8,1);
	force_instant_terrain_redraw();
	set_flag(0,3,2);
	set_flag(1,6,1); //can enter portal
break;

beginstate 15; //howling winds
	if(get_flag(1,2) == 1)
		end();
	
	reset_dialog();
	add_dialog_str(0,"You feel a blast of charged air as you walk down the hall, and can't help but notice the blinding light coming from a doorway at the end.",0);
	run_dialog(1);
	set_flag(1,2,1);
break;

beginstate 20; //entering the portal
	block_entry(1); //can't get too close now

	//if you don't know what you're doing
	if(get_flag(1,6) == 0) {
		message_dialog("There is no way you're going through the portal before you know what you're supposed to do. Or if the portal's even safe.","You back away slowly.");
		end();
		}

	//first time through
	if((get_flag(0,3) == 2) && (get_flag(10,0) == 0)) {
		reset_dialog();
		add_dialog_str(0,"Do you step into the portal?",0);
		add_dialog_choice(0,"Not yet.");
		add_dialog_choice(1,"Yes.");
		choice = run_dialog(1);
		if(choice == 2) {
			reset_dialog();
			add_dialog_str(0,"You step into the portal, and the storm of elemental magic overwhelms you. Your vision goes black as the sound of howling wind batters your ears...",0);
			run_dialog(1);
			play_sound(-10);
			force_view_center(18,47);
			force_instant_terrain_redraw();
			set_flag(10,0,1);
			move_to_new_town(2,30,12);
			}
		}
	//second time through
	else if((get_flag(0,3) == 3) && (get_flag(10,0) == 2)) {
		reset_dialog();
		add_dialog_str(0,"Do you step back into the portal?",0);
		add_dialog_choice(0,"Not yet.");
		add_dialog_choice(1,"Yes.");
		choice = run_dialog(1);
		if(choice == 2) {
			reset_dialog();
			add_dialog_str(0,"You step into the portal, and this time you feel like you're ready for it. Then you immediately black out.",0);
			run_dialog(1);
			play_sound(-10);
			force_view_center(18,47);
			force_instant_terrain_redraw();
			set_flag(10,0,3);
			move_to_new_town(2,19,25);
			}
		}
	//third time through
	else if((get_flag(0,3) == 4) && (get_flag(10,0) == 4)) {
		reset_dialog();
		add_dialog_str(0,"Do you step back into the portal?",0);
		add_dialog_choice(0,"Not yet.");
		add_dialog_choice(1,"Yes.");
		choice = run_dialog(1);
		if(choice == 2) {
			reset_dialog();
			add_dialog_str(0,"You step into the portal, and this time you don't even try to keep conscious. You black out instantly.",0);
			run_dialog(1);
			play_sound(-10);
			force_view_center(18,47);
			force_instant_terrain_redraw();
			set_flag(10,0,5);
			move_to_new_town(2,23,32);
			}
		}
	//fourth time through
	else if((get_flag(0,3) == 5) && (get_flag(10,0) == 6)) {
		reset_dialog();
		add_dialog_str(0,"Do you step back into the portal?",0);
		add_dialog_choice(0,"Not yet.");
		add_dialog_choice(1,"Yes.");
		choice = run_dialog(1);
		if(choice == 2) {
			reset_dialog();
			add_dialog_str(0,"You step into the portal, and amazingly, you don't black out this time. On the other hand, this means you have to look at the blinding and bewildering swirl of magic that makes this kind of portal work.",0);
			run_dialog(1);
			play_sound(-10);
			force_view_center(18,47);
			force_instant_terrain_redraw();
			set_flag(10,0,7);
			move_to_new_town(2,18,7);
			}
		}		
break;

beginstate 25; //locked storeroom door
	if((get_flag(1,4) == 1) || (get_flag(10,0) < 4))
		end();
	
	reset_dialog();
	add_dialog_str(0,"You start to panic as you realize that the door to this room is locked. Even from the inside, the mechanism on the door appears to be more complex than you could ever hope to deal with.",0);
	add_dialog_str(1,"Out of sheer frustration, you pound on the door. Barely a minute goes by before you hear a key in the lock, and the door opens.",0);
	run_dialog(1);
	
	pause(5);
	flip_terrain(32,31);
	play_sound(9);
	play_sound(-58);
	relocate_character(8,31,31);
	set_character_facing(8,6);
	force_instant_terrain_redraw();
	
	pause(10);
	reset_dialog();
	add_dialog_str(0,"Martha looks irritated and tired, but she manages a faint smile.",0);
	add_dialog_str(1,"_Well, at least you're alive._",15);
	run_dialog(1);
	
	set_flag(1,4,1);
break;