// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessable from anywhere in the scenario. it also
// contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers greater than
// or equal to 10.

beginscenarioscript;

variables;
short choice = 0;

body;

// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.
beginstate LOAD_SCEN_STATE;

	//Initialize the only quest
	init_quest(0,"A Stitch in Time","Martha, a very intimidating researcher, has conscripted you to save the fabric of time-space... or something like that. Anyway, you're supposed to go through a portal downstairs.");

	//...update quest for the second trip
	if(get_flag(0,3) == 3)
		init_quest(0,"A Stitch in Time","Since negotiating with Ethalia worked so well, Martha has decided that it's better to head her off at the pass by killing the demon before Ethalia can stop.");
	//...update for the third trip
	if(get_flag(0,3) == 4)
		init_quest(0,"A Stitch in Time","Martha has another plan. This time, you need to go back through the portal, negotiate with a younger version of Ethalia, and get her to delay or avoid the meeting with the demon.");
	//...update for the fourth trip
	if(get_flag(0,3) == 5)
		init_quest(0,"A Stitch in Time","Finally, a plan you can understand. Martha wants you to jump back in the portal, and simply disrupt the summoning as you see fit. Which means you'll probably get to fight something or someone.");

	break;

// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.
beginstate START_SCEN_STATE;
	// Initialize a few shops.
	
	// Shop 0 - armor
	add_item_to_shop(0,25,25);
	add_item_to_shop(0,26,20);
	add_item_to_shop(0,30,20);
	add_item_to_shop(0,31,10);
	add_item_to_shop(0,35,10);
	add_item_to_shop(0,121,15);
	add_item_to_shop(0,122,15);
	add_item_to_shop(0,126,3);
	add_item_to_shop(0,131,2);
	add_item_to_shop(0,111,15);
	add_item_to_shop(0,16,10);
	add_item_to_shop(0,136,10);
	add_item_to_shop(0,137,10);
	add_item_to_shop(0,141,5);
	
	// Shop 1 - weapons
	add_item_to_shop(1,45,25);
	add_item_to_shop(1,46,20);
	add_item_to_shop(1,50,15);
	add_item_to_shop(1,51,10);
	add_item_to_shop(1,55,5);
	add_item_to_shop(1,65,20);
	add_item_to_shop(1,66,10);
	add_item_to_shop(1,70,5);
	
	// Shop 2 - tools
	add_item_to_shop(2,170,500);
	add_item_to_shop(2,171,500);
	add_item_to_shop(2,172,30);
	add_item_to_shop(2,174,500);
	add_item_to_shop(2,177,500);
	
	// Shop 3 - missiles
	add_item_to_shop(3,85,500);
	add_item_to_shop(3,86,10);
	add_item_to_shop(3,90,10);
	add_item_to_shop(3,95,3);
	add_item_to_shop(3,100,20);
	add_item_to_shop(3,101,10);
	add_item_to_shop(3,105,10);

	// Shop 4 - potions
	add_item_to_shop(4,220,4);
	add_item_to_shop(4,221,3);
	add_item_to_shop(4,222,3);
	add_item_to_shop(4,223,2);

	// Shop 5 - food
	add_item_to_shop(5,4,500);
	add_item_to_shop(5,6,500);
	add_item_to_shop(5,7,500);
break;

// This state is called every tick wherever the party is in the scenario.
// You can use the set_state
beginstate START_STATE;

break;


beginstate 10; //arcane scroll
	message_dialog("You start to read the scroll, until you realize that you don't really know where to start. The scroll is filled with highly specific, highly esoteric arcane scrawlings.","You can't make heads or tails of it.");
break;
beginstate 11; //papyrus covered in notes
	message_dialog("You can't make any sense of the writing on this papyrus, but it's probably important to someone's research. To you, however, it's worthless.","");
break;


beginstate 20; //the ivory wand
	//if you're not in the final fight AND targeting the demon or Ethalia
	if(((who_is_custom_item_target() != 10) && (who_is_custom_item_target() != 9)) || (current_town() != 2) || (get_flag(10,0) != 7)) {
		put_effect_on_char(who_used_custom_item(),5,3,1);
		run_animation_sound(61); //boing
		message_dialog("The wand smokes gently, but doesn't do anything. Strange.","");
		}
	//if you are in the final fight
	else {
		//if you target Ethalia
		if(who_is_custom_item_target() == 9) {
			reset_dialog();
			add_dialog_str(0,"Ethalia's eyes grow wide as you point the wand at her. You can feel the syllables of a spell rising to your lips, and it would be very easy to just let it fly.",0);
			add_dialog_str(1,"_No, fool! You can't undo this! Can't you see I've come too far to just let it slip away?_",15);
			add_dialog_str(2,"Ethalia sounds very desperate, and you have no idea what will happen if you complete the spell. Do you?",0);
			add_dialog_choice(0,"No.");
			add_dialog_choice(1,"Yes.");
			choice = run_dialog(1);
			if(choice == 1) {
				message_dialog("You lower the wand. For a moment, you glimpse relief in Ethalia's eyes, before it is replaced by cold fury.","She turns her attention back to the demon, and the wand emits a puff of smoke.");
				end();
				}
			pause(5);
			set_flag(2,9,1);
			set_attitude(10,4);
			put_jagged_zap(char_loc_x(who_used_custom_item()), char_loc_y(who_used_custom_item()), char_loc_x(9), char_loc_y(9), 4);
			put_boom_on_char(9,3,0);
			run_animation_sound(102); //big zap
			pause(2);
			
			erase_char(9);
			reset_dialog();
			add_dialog_str(0,"The spell strikes Ethalia in the chest, and she freezes. Her face softens, and the aura of energy around her begins to subside.",0);
			add_dialog_str(1,"_I... have failed._",15);
			add_dialog_str(2,"You see her body begin to fade away, and she turns to look at her younger self. Her voice barely rises above a whisper.",0);
			add_dialog_str(3,"_I have failed you._",15);
			add_dialog_str(4,"Then she is gone. The wand, its magic spent, crumbles to dust.",0);
			force_instant_terrain_redraw();
			run_dialog(1);
			take_all_of_item(447);
			award_party_xp(250,15);
			}
		//hitting the demon
		else if(who_is_custom_item_target() == 10) {
			reset_dialog();
			add_dialog_str(0,"The demon's eyes (all of them) grow wide as you point the wand at it. You can feel the syllables of a spell rising to your lips, and it would be very easy to just let it fly.",0);
			add_dialog_str(1,"_No! Still your hand, lest you bring destruction upon us all!_",15);
			add_dialog_str(2,"The demon sounds nervous, and you have no idea what will happen if you complete the spell. Do you?",0);
			add_dialog_choice(0,"No.");
			add_dialog_choice(1,"Yes.");
			choice = run_dialog(1);
			if(choice == 1) {
				message_dialog("You lower the wand. For a moment, you glimpse relief in the demon's eyes, before it is replaced by blazing fury.","It turns its attention back to Ethalia, and the wand emits a puff of smoke.");
				end();
				}
			pause(5);
			set_flag(2,9,2);
			set_attitude(9,4);
			put_jagged_zap(char_loc_x(who_used_custom_item()), char_loc_y(who_used_custom_item()), char_loc_x(10), char_loc_y(10), 4);
			put_boom_on_char(10,3,0);
			run_animation_sound(102); //big zap
			pause(2);
			
			erase_char(10);
			reset_dialog();
			add_dialog_str(0,"The spell strikes the demon in the chest, and it freezes. Its features contort into a furious and horrifying knot. It screams something in a language you can't understand.",0);
			add_dialog_str(1,"You see its body begin to fade away, and it turns to look at the younger Ethalia. Its voice trembles with rage.",0);
			add_dialog_str(2,"_Traitorous wretch! I swear on the name of my master that---_",15);
			add_dialog_str(3,"Then it is gone. The wand, its magic spent, crumbles to dust.",0);
			force_instant_terrain_redraw();
			run_dialog(1);
			take_all_of_item(447);
			award_party_xp(250,15);
			}
		}
break;