// SCENARIO SCRIPT

// This is the special script for your scenario, where you will
// write special encounters that can happen anywhere in the scenario.
// The states LOAD_SCEN_STATE, START_SCEN_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

beginscenarioscript;

variables;
short a,c,e,i,j,k,r,t,choice;

body;

beginstate LOAD_SCEN_STATE;
// This state called whenever this scenario is loaded, including when
// a save file is loaded.
	init_special_item(0,"Merchant Letter","A small, folded message, carried by the merchant caravan from Hatherond to Hengest. You are to deliver it to agent Rauling.");
	init_special_item(1,"Steel Key","A small and well-crafted key made of steel.");
	init_special_item(2,"Brass Key","A long, sturdy key made of brass. It looks less for a door than for some sort of large mechanical construct.");
	init_special_item(3,"Captain's Log","This contains some rather messy handwriting (or at least you think so) in the language of the Seawyrm, totally alien to you.");
	init_special_item(4,"Obsidian Orb","A simple sphere made of igneous rock.");
	init_special_item(5,"Red Book","A small bound book, it contains a short account of the history of the isle of Ylmir.");
	init_special_item(6,"Docks Keys","These keys open the gates to the docks at Skane.");
	init_special_item(46,"Contract","Freawaru the commander of the Falcon realm armies gave you this scrap of paper in Hengest.");

//*** Quests ***
	init_quest(0,"Meet Agent Rauling","The party is meant to meet an agent of the Seawyrm by the name of Rauling, he is hiding somewhere in the city of Hatherond.");
	init_quest(1,"Retrieve the Message","Agent Rauling in Hatherond wants you to go northeast to Ravenswood forest.  There you are to obtain a message from Aeschere to Falconmaster Scyld.");
	init_quest(2,"Investigate Aeschere, Finn","This time Agent Rauling in Hatherond wants you to investigate the homes of Aeschere and Finn, in an attempt to learn anything more.");
	init_quest(3,"Kill Aeschere","Dracan (the Seawyrm leader?), who lives on the upper floor of Ingusi, demands that you go to Hatherond and kill Aeschere.");
	init_quest(4,"Kill Aeschere","Now, Dracan (the Seawyrm leader?), who lives on the upper floor of Ingusi, demands that you follow Aeschere to Hetware and kill him there.");		
	init_quest(5,"Warn Sweon","Falconmaster Scyld wants you to go to Sweon, warn the guards there and resolve a messy problem with a Seawyrm outpost.");		
	init_quest(6,"Enter the Mist Realms","Now the Falconmaster has asked you to go enter the mysterious Realms of Mist and retrieve a magical orb of obsidian.");		

	init_special_abil(0,"Identify Spell",62);
break;

beginstate START_SCEN_STATE;
// This state called whenever this scenario is started, and only
// when it's started.

//The Shops

//Hatherond, Yse's Dusts
	add_item_to_shop(0,435,3);
	add_item_to_shop(0,436,5);
	add_item_to_shop(0,437,17);
	add_item_to_shop(0,438,11);

//Hatherond, Herebeald's Armors
	add_item_to_shop(1,25,13);
	add_item_to_shop(1,26,12);
	add_item_to_shop(1,30,6);
	add_item_to_shop(1,35,6);
	add_item_to_shop(1,40,4);

//Hatherond, Offa's Weaponry
	add_item_to_shop(2,46,10);
	add_item_to_shop(2,51,5);
	add_item_to_shop(2,55,6);
	add_item_to_shop(2,56,5);
	add_item_to_shop(2,60,2);
	add_item_to_shop(2,61,2);
	add_item_to_shop(2,175,5);
	add_item_to_shop(2,220,2);
	add_item_to_shop(2,245,2);
	add_item_to_shop(2,458,20);
	add_item_to_shop(2,459,12);

//Hatherond, Ingorn's Bows and Arrows
	add_item_to_shop(3,90,8);
	add_item_to_shop(3,91,20);
	add_item_to_shop(3,100,100);
	add_item_to_shop(3,101,100);

//Hatherond, Minor Conjurations
	add_item_to_shop(4,2000,3);
	add_item_to_shop(4,2001,3);
	add_item_to_shop(4,2002,3);
	add_item_to_shop(4,2003,3);
	add_item_to_shop(4,2004,3);
	add_item_to_shop(4,2005,3);
	add_item_to_shop(4,2006,2);
	add_item_to_shop(4,2007,2);
	add_item_to_shop(4,2008,2);
	add_item_to_shop(4,2009,2);

//Outdoor Section 0,0: Herb Camp
	add_item_to_shop(5,214,50);

//Outdoor Section 0,2: Farming Supplies
	add_item_to_shop(6,114,8);
	add_item_to_shop(6,115,8);
	add_item_to_shop(6,174,5);

//Outdoor Section 1,2: Merchant Caravan 
	add_item_to_shop(7,32,10);
	add_item_to_shop(7,37,10);
	add_item_to_shop(7,41,6);
	add_item_to_shop(7,42,2);

//Outdoor Section 1,2: Burton's Formulas 
	add_item_to_shop(8,4007,1);
	add_item_to_shop(8,4008,1);
	add_item_to_shop(8,4009,1);

//Outdoor Section 2,2: Village Rations 
	add_item_to_shop(9,4,500);
	add_item_to_shop(9,5,500);
	add_item_to_shop(9,6,500);
	add_item_to_shop(9,7,500);
	add_item_to_shop(9,8,500);
	add_item_to_shop(9,9,500);
	add_item_to_shop(9,11,500);
	add_item_to_shop(9,12,500);

//Ingusi, Minor Conjurations
	add_item_to_shop(10,2000,4);
	add_item_to_shop(10,2001,4);
	add_item_to_shop(10,2002,4);
	add_item_to_shop(10,2003,4);
	add_item_to_shop(10,2004,4);
	add_item_to_shop(10,2005,4);
	add_item_to_shop(10,2006,4);
	add_item_to_shop(10,2007,3);
	add_item_to_shop(10,2008,3);
	add_item_to_shop(10,2009,3);
	add_item_to_shop(10,2010,3);
	add_item_to_shop(10,2011,3);
	add_item_to_shop(10,2012,2);
	add_item_to_shop(10,2013,2);
	add_item_to_shop(10,2014,2);
	add_item_to_shop(10,2015,2);

//Ingusi, Woll's Grasses 
	add_item_to_shop(11,215,73);
	add_item_to_shop(11,216,35);

//Hetware, Earthly Rituals
	add_item_to_shop(12,3000,4);
	add_item_to_shop(12,3001,4);
	add_item_to_shop(12,3002,4);
	add_item_to_shop(12,3003,4);
	add_item_to_shop(12,3004,4);
	add_item_to_shop(12,3005,4);
	add_item_to_shop(12,3006,4);
	add_item_to_shop(12,3007,3);
	add_item_to_shop(12,3008,3);
	add_item_to_shop(12,3009,3);
	add_item_to_shop(12,3010,3);
	add_item_to_shop(12,3011,3);
	add_item_to_shop(12,3012,3);

//Hetware, Thrytho's Brews
	add_item_to_shop(13,220,30);
	add_item_to_shop(13,221,30);
	add_item_to_shop(13,222,30);
	add_item_to_shop(13,223,20);
	add_item_to_shop(13,225,20);

//Hetware, Archery Supplies
	add_item_to_shop(14,85,300);
	add_item_to_shop(14,86,100);
	add_item_to_shop(14,90,60);
	add_item_to_shop(14,91,50);
	add_item_to_shop(14,92,50);
	add_item_to_shop(14,100,89);
	add_item_to_shop(14,101,70);
	add_item_to_shop(14,102,29);
	add_item_to_shop(14,103,15);

//Hengest, Heorot's Wines
	add_item_to_shop(15,166,500);
	add_item_to_shop(15,393,500);
	add_item_to_shop(15,414,500);
	add_item_to_shop(15,415,500);

//Fresan, Ecgwela's Items
	add_item_to_shop(16,28,1);
	add_item_to_shop(16,42,3);
	add_item_to_shop(16,57,2);
	add_item_to_shop(16,63,5);
	add_item_to_shop(16,66,1);
	add_item_to_shop(16,252,1);
	add_item_to_shop(16,419,5);	

//Fresan, Fitela's Assortment
	add_item_to_shop(17,302,11);
	add_item_to_shop(17,303,6);
	add_item_to_shop(17,304,2);
	add_item_to_shop(17,305,5);
	add_item_to_shop(17,306,12);

//Fresan, Skane's Weaponry 
	add_item_to_shop(18,46,55);
	add_item_to_shop(18,51,16);
	add_item_to_shop(18,60,16);
	add_item_to_shop(18,66,11);
	add_item_to_shop(18,75,23);
	add_item_to_shop(18,458,10);
	add_item_to_shop(18,459,12);

//Fresan, Weder's Wares
	add_item_to_shop(20,435,55);
	add_item_to_shop(20,436,55);
	add_item_to_shop(20,437,55);

//Sweon, Garmund 
	add_item_to_shop(21,2015,3);

//Under Hengest, Helsaeg 
	add_item_to_shop(22,5019,3);
	add_item_to_shop(22,5020,3);
	add_item_to_shop(22,5021,3);

//Ingusi, Sado
	add_item_to_shop(23,5019,3);

//Skane, Salerne 
	add_item_to_shop(24,5019,3);
	add_item_to_shop(24,5020,3);
	add_item_to_shop(24,5021,3);

//Padan, Ghazal's Herbalism
	add_item_to_shop(25,230,55);
	add_item_to_shop(25,231,55);
	add_item_to_shop(25,233,55);
	
	create_boat(0,11,5,23,1);
	create_boat(1,11,6,26,1);
	create_boat(2,13,15,56,0);
	create_boat(3,13,13,58,0);
	create_boat(4,27,9,56,0);
	create_boat(5,7,15,22,1);
	create_boat(6,7,16,20,1);
	create_boat(7,7,20,20,1);	
	create_horse(0,10,13,47,1);
	create_horse(1,10,15,50,1);
	create_horse(2,12,32,12,1);
	create_horse(3,12,30,15,1);
	reward_give(448);	
	toggle_quest(0,1);

	i = 0;
	while (i < 4) {
			change_custom_abil_uses(i,0,1);
		i = i + 1;
		}
break;

beginstate START_STATE;
	r = get_current_tick() % 5000;
//	print_big_str("The time is now ",r," minutes.");
	if (r == 0) {
//	print_str_color("A new day is starting again.", 3);
	i = 9; 
	while (i < 30) {
	 set_flag(11,i,0);
		i = i + 1;
	}
		i = 9; 
	while (i < 30) {
	 set_flag(111,i,0);
		i = i + 1;
	}
}

	if (get_current_tick() % 5 == 0) {
		if(is_town() == 1) {
//	print_str_color("The party regains health and energy.", 3);
	i = 0;
while (i < 6) {
	change_char_energy(i,1);
	change_char_health(i,1);
i = i + 1;
}
}
}
break;

beginstate 10;
	reset_dialog();
	add_dialog_str(0,"FALCONMASTER SCYLD:",0);
	add_dialog_str(1,"     I trust that you have received the earlier dispatch, and have reviewed it thoroughly. Raiders and privateers are harrying our commerce, and we suspect that even the western pirates have ties to the Seawyrm Clan.",0);
	add_dialog_str(2,"     The situation grows worse, as the Seawyrm trespasses upon our lands each day. It seems necessary that we form an alliance to repulse this threat for the long term, instead of maintaining our current impassive strategy.",0);
	add_dialog_str(3,"     Your response is eagerly awaited.",0);
	add_dialog_str(4,"          - AESCHERE",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
break;

beginstate 11;
	set_flag(211,2,0);
break;

beginstate 12;
	set_flag(211,3,1);
break;

beginstate 13;
	reset_dialog();
	add_dialog_str(0,"Ships are waiting for you in the harbor, ready to carry you back over the waters. You could forsake the mission now. However scarce, there will be new opportunities.",0);
	add_dialog_str(1,"Do you wish to leave this land behind?",0);
	add_dialog_choice(0,"No.");
	add_dialog_choice(1,"Yes.");
	choice = run_dialog(1);
	if (choice == 1)
	block_entry(1);
	if (choice == 2) {
	reset_dialog();
	add_dialog_str(0,"You board the next ship, and make your departure. Taking one last look at the expansive plains, the thick green forests behind, and above all the majestic mountains of the north, you bid the island a silent farewell.",0);
	add_dialog_str(1," * * *",0);
	add_dialog_str(2,"_There will be time to murder and create, and time for all the works and days of hands that lift and drop a question on your plate; time for you and time for me, and time yet for a hundred indecisions, and for a hundred visions and revisions...",0);
	add_dialog_str(3,"Before the taking of a toast and tea._",0);
	add_dialog_str(4,"- T. S. Eliot",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
	end_scenario(1);
	}
break;

beginstate 17;
	if (get_flag(20,6) >= 1)
		set_state_continue(19);
	set_state_continue(18);
break;

beginstate 18;
	message_dialog("You can't read the log at all.","");
break;

beginstate 19;
	message_dialog("Unferth's translation gives you some idea of what the log is about. It appears that the log is merely a simple record of what took place each day, with no special events or orders written in them.","");
break;

beginstate 20;
	reset_dialog();
	add_dialog_str(0,"- The History of Ylmir -",0);
	add_dialog_str(1,"It is said that the hand of an Outer Power was in the shaping of Ylmir. Its mountains and glens, its rivers and fens - all crafted with perfection. So it has been, as the land slopes gently downward from the snow-crowned Northrange.",0);
	add_dialog_str(2,"The legends of the bringing of civilization to the isle have been muddled over the decades, five since. The Falconers came over the Northrange, bringing with them steel and steeds, and conquered the native tribes until they fell under the Falcon's sway.",0);
	add_dialog_str(3,"The power of the Falcon has diminished, and now the southwest is held by the Wolf clan, named for the fierce Ursags that once prowled the land, but are no more. Inhabited by the natives that remain, their society is less rigid, and less dominating.",0);
	add_dialog_str(4,"Eastward had always been a wilder land, with rumors of demons inhabiting the swamp-isles. But the red fleets of the Seawyrm came two decades ago, and through force of iron they claimed their place at the Whale's Cape.",0);
	add_dialog_str(5,"An uneasy truce exists between the Seawyrm and the two elder clans that has led to much distrust. Of the newcomers little is known, coming with only whispers of their past. For better or for worse, the truths are still hidden beyond the Mirrors.",0);
	add_dialog_choice(0,"OK.");
	choice = run_dialog(1);
break;

beginstate 21;
	if (get_flag(97,4) >= 1)
		set_state_continue(23);
break;

beginstate 22;
	message_dialog("Ephon seems impatient. _We should be getting on with the quest to defeat the swamp monster, not dawdling like this._","");
	set_state_continue(27);
break;

beginstate 23;
	if (get_ran(1,1,100) < 34)
		set_state_continue(22);
	set_state_continue(24);
break;

beginstate 24;
	if (get_ran(1,1,100) < 51)
		set_state_continue(25);
	set_state_continue(26);
break;

beginstate 25;
	message_dialog("Ephon sighs in exasperation. _I would be fighting Grendel now, but no. I'm stuck with a crowd of do-nothingers._","");
	set_state_continue(27);
break;

beginstate 26;
	message_dialog("Ephon shakes his head. _Each day, the swamp monster tears apart another soul. We cannot tarry._","");
	set_state_continue(27);
break;

beginstate 27;
// OBSOLETE NODE: Timers are no longer used. Manually write scenario timers in the 
// scenario script.
//6,000 moves till scenario state 30 below.
break;

beginstate 30;
	if (character_in_party(2802) >= 0) 
		set_state_continue(21);
break;


beginstate 32;
	e = get_ran(6,1,6);
	print_str_color("The barrier shocks you!",1);		
	damage_char(1000,e,3);
	block_entry(1);
break;


beginstate 36;
	reset_dialog();
	if ((has_special_item(0) == 0) && (has_special_item(3) == 0) && (has_special_item(5) == 0)) {
	print_str_color("You lack any texts to read.",1);		
	end();
	}
	
	add_dialog_str(0,"Which text would you like to read? Available are:",0);
	if (has_special_item(0)){
	add_dialog_str(1,"1 Merchant's Letter: A small, folded message, carried by the merchant caravan from Hatherond to Hengest. You are to deliver it to agent Rauling",0);	
	}
	if (has_special_item(3)){
	add_dialog_str(2,"2 Captain's Log: This contains some rather messy handwriting (or at least you think so) in the language of the Seawyrm, totally alien to you.",0);	
	}
	if (has_special_item(5)){
	add_dialog_str(3,"3 Red Book: A small bound book, it contains a short account of the history of the isle of Ylmir.",0);	
	}
run_dialog(1);

	get_text_response("Choose from 1,2 or 3.");

	check_text_response_match("1");
	 if (got_text_match()) {
	set_state_continue(10);
	}
		
	check_text_response_match("2");
	 if (got_text_match()) {
	set_state_continue(17);
	}
	
	check_text_response_match("3");
	 if (got_text_match()) {
	set_state_continue(20);
	}
break;


beginstate 52;
t = current_town();
i = 10; 
while (i < 20) {
 if (get_flag(t,i) != 0) {
 print_str_color("All links to the Demiplane are already open.",4);
 end();
} 
 else {
 
  if (get_flag(t,i) == 0) 
 a = get_ran(1,1,1000);

if ((a >= 1) && (a <= 4))
 set_flag(t,i,27);

if ((a >= 5) && (a <= 8))
 set_flag(t,i,28);

if ((a >= 9) && (a <= 10))
 set_flag(t,i,32);

if ((a >= 11) && (a <= 16))
 set_flag(t,i,33);

if ((a >= 17) && (a <= 18))
 set_flag(t,i,37);

if ((a >= 19) && (a <= 22))
 set_flag(t,i,38);

if ((a >= 23) && (a <= 24))
 set_flag(t,i,42);

if ((a >= 25) && (a <= 30))
 set_flag(t,i,43);

if ((a >= 31) && (a <= 32))
 set_flag(t,i,47);

if ((a >= 33) && (a <= 38))
 set_flag(t,i,48);

if ((a >= 39) && (a <= 40))
 set_flag(t,i,52);

if ((a >= 41) && (a <= 46))
 set_flag(t,i,53);

if ((a >= 47) && (a <= 48))
 set_flag(t,i,57);

if ((a >= 49) && (a <= 54))
 set_flag(t,i,58);

if ((a >= 55) && (a <= 56))
 set_flag(t,i,62);

if ((a >= 57) && (a <= 62))
 set_flag(t,i,63);

if ((a >= 63) && (a <= 64))
 set_flag(t,i,67);

if ((a >= 65) && (a <= 70))
 set_flag(t,i,68);

if ((a >= 71) && (a <= 72))
 set_flag(t,i,72);

if ((a >= 73) && (a <= 78))
 set_flag(t,i,73);

if ((a >= 79) && (a <= 80))
 set_flag(t,i,77);

if ((a >= 81) && (a <= 86))
 set_flag(t,i,78);

if ((a >= 87) && (a <= 90))
 set_flag(t,i,80);

if ((a >= 91) && (a <= 92))
 set_flag(t,i,87);

if ((a >= 93) && (a <= 98))
 set_flag(t,i,88);

if ((a >= 99) && (a <= 102))
 set_flag(t,i,92);

if ((a >= 103) && (a <= 108))
 set_flag(t,i,93);

if ((a >= 109) && (a <= 112))
 set_flag(t,i,97);

if ((a >= 113) && (a <= 118))
 set_flag(t,i,98);

if ((a >= 119) && (a <= 120))
 set_flag(t,i,102);

if ((a >= 121) && (a <= 126))
 set_flag(t,i,103);

if ((a >= 127) && (a <= 128))
 set_flag(t,i,107);

if ((a >= 129) && (a <= 134))
 set_flag(t,i,108);

if ((a >= 135) && (a <= 138))
 set_flag(t,i,112);

if ((a >= 139) && (a <= 142))
 set_flag(t,i,115);

if ((a >= 143) && (a <= 148))
 set_flag(t,i,117);

if ((a >= 149) && (a <= 154))
 set_flag(t,i,119);

if ((a >= 155) && (a <= 156))
 set_flag(t,i,123);

if ((a >= 157) && (a <= 162))
 set_flag(t,i,124);

if ((a >= 163) && (a <= 164))
 set_flag(t,i,128);

if ((a >= 165) && (a <= 170))
 set_flag(t,i,129);

if ((a >= 171) && (a <= 172))
 set_flag(t,i,133);

if ((a >= 173) && (a <= 178))
 set_flag(t,i,134);

if ((a >= 179) && (a <= 182))
 set_flag(t,i,138);

if ((a >= 183) && (a <= 186))
 set_flag(t,i,139);

if ((a >= 187) && (a <= 188))
 set_flag(t,i,143);

if ((a >= 189) && (a <= 194))
 set_flag(t,i,144);

if ((a >= 195) && (a <= 198))
 set_flag(t,i,147);

if ((a >= 199) && (a <= 202))
 set_flag(t,i,167);

if ((a >= 203) && (a <= 206))
 set_flag(t,i,175);

if ((a >= 207) && (a <= 210))
 set_flag(t,i,176);

if ((a >= 211) && (a <= 214))
 set_flag(t,i,178);

if ((a >= 215) && (a <= 218))
 set_flag(t,i,220);

if ((a >= 219) && (a <= 222))
 set_flag(t,i,221);

if ((a >= 223) && (a <= 226))
 set_flag(t,i,222);

if ((a >= 227) && (a <= 230))
 set_flag(t,i,223);

if ((a >= 231) && (a <= 234))
 set_flag(t,i,225);

if ((a >= 235) && (a <= 238))
 set_flag(t,i,226);

if ((a >= 239) && (a <= 242))
 set_flag(t,i,227);

if ((a >= 243) && (a <= 246))
 set_flag(t,i,228);

if ((a >= 247) && (a <= 250))
 set_flag(t,i,229);

if ((a >= 251) && (a <= 254))
 set_flag(t,i,230);

if ((a >= 255) && (a <= 258))
 set_flag(t,i,231);

if ((a >= 259) && (a <= 262))
 set_flag(t,i,233);

if ((a >= 263) && (a <= 266))
 set_flag(t,i,234);

if ((a >= 267) && (a <= 270))
 set_flag(t,i,235);

if ((a >= 271) && (a <= 274))
 set_flag(t,i,236);

if ((a >= 275) && (a <= 278))
 set_flag(t,i,237);

if ((a >= 279) && (a <= 282))
 set_flag(t,i,238);

if ((a >= 283) && (a <= 286))
 set_flag(t,i,239);

if ((a >= 287) && (a <= 290))
 set_flag(t,i,240);

if ((a >= 291) && (a <= 294))
 set_flag(t,i,241);

if ((a >= 295) && (a <= 298))
 set_flag(t,i,244);

if ((a >= 299) && (a <= 302))
 set_flag(t,i,245);

if ((a >= 303) && (a <= 306))
 set_flag(t,i,246);

if ((a >= 307) && (a <= 310))
 set_flag(t,i,247);

if ((a >= 311) && (a <= 314))
 set_flag(t,i,248);

if ((a >= 315) && (a <= 318))
 set_flag(t,i,249);

if ((a >= 319) && (a <= 322))
 set_flag(t,i,250);

if ((a >= 323) && (a <= 326))
 set_flag(t,i,251);

if ((a >= 327) && (a <= 330))
 set_flag(t,i,252);

if ((a >= 331) && (a <= 334))
 set_flag(t,i,253);

if ((a >= 335) && (a <= 338))
 set_flag(t,i,254);

if ((a >= 339) && (a <= 342))
 set_flag(t,i,255);

if ((a >= 343) && (a <= 346)) {
 set_flag(t,i,0);
 set_flag(t,i + 10,1);
 }

if ((a >= 347) && (a <= 350)) {
 set_flag(t,i,1);
 set_flag(t,i + 10,1);
 }

if ((a >= 351) && (a <= 354)) {
 set_flag(t,i,2);
 set_flag(t,i + 10,1);
 }

if ((a >= 355) && (a <= 358)) {
 set_flag(t,i,3);
 set_flag(t,i + 10,1);
 }

if ((a >= 359) && (a <= 362)) {
 set_flag(t,i,4);
 set_flag(t,i + 10,1);
 }

if ((a >= 363) && (a <= 366)) {
 set_flag(t,i,5);
 set_flag(t,i + 10,1);
 }

if ((a >= 367) && (a <= 370)) {
 set_flag(t,i,14);
 set_flag(t,i + 10,1);
 }

if ((a >= 371) && (a <= 374)) {
 set_flag(t,i,15);
 set_flag(t,i + 10,1);
 }

if ((a >= 375) && (a <= 378)) {
 set_flag(t,i,16);
 set_flag(t,i + 10,1);
 }

if ((a >= 379) && (a <= 382)) {
 set_flag(t,i,17);
 set_flag(t,i + 10,1);
 }

if ((a >= 383) && (a <= 386)) {
 set_flag(t,i,18);
 set_flag(t,i + 10,1);
 }

if ((a >= 387) && (a <= 390)) {
 set_flag(t,i,19);
 set_flag(t,i + 10,1);
 }

if ((a >= 391) && (a <= 394)) {
 set_flag(t,i,20);
 set_flag(t,i + 10,1);
 }

if ((a >= 395) && (a <= 398)) {
 set_flag(t,i,21);
 set_flag(t,i + 10,1);
 }

if ((a >= 399) && (a <= 402)) {
 set_flag(t,i,22);
 set_flag(t,i + 10,1);
 }

if ((a >= 403) && (a <= 406)) {
 set_flag(t,i,23);
 set_flag(t,i + 10,1);
 }

if ((a >= 407) && (a <= 410)) {
 set_flag(t,i,24);
 set_flag(t,i + 10,1);
 }

if ((a >= 411) && (a <= 414)) {
 set_flag(t,i,25);
 set_flag(t,i + 10,1);
 }

if ((a >= 415) && (a <= 418)) {
 set_flag(t,i,26);
 set_flag(t,i + 10,1);
 }

if ((a >= 419) && (a <= 422)) {
 set_flag(t,i,27);
 set_flag(t,i + 10,1);
 }

if ((a >= 423) && (a <= 426)) {
 set_flag(t,i,28);
 set_flag(t,i + 10,1);
 }

if ((a >= 427) && (a <= 430)) {
 set_flag(t,i,29);
 set_flag(t,i + 10,1);
 }

if ((a >= 431) && (a <= 434)) {
 set_flag(t,i,30);
 set_flag(t,i + 10,1);
 }

if ((a >= 435) && (a <= 438)) {
 set_flag(t,i,31);
 set_flag(t,i + 10,1);
 }

if ((a >= 439) && (a <= 442)) {
 set_flag(t,i,32);
 set_flag(t,i + 10,1);
 }

if ((a >= 443) && (a <= 446)) {
 set_flag(t,i,33);
 set_flag(t,i + 10,1);
 }

if ((a >= 447) && (a <= 450)) {
 set_flag(t,i,34);
 set_flag(t,i + 10,1);
 }

if ((a >= 451) && (a <= 454)) {
 set_flag(t,i,35);
 set_flag(t,i + 10,1);
 }

if ((a >= 455) && (a <= 458)) {
 set_flag(t,i,36);
 set_flag(t,i + 10,1);
 }

if ((a >= 459) && (a <= 462)) {
 set_flag(t,i,37);
 set_flag(t,i + 10,1);
 }

if ((a >= 463) && (a <= 466)) {
 set_flag(t,i,38);
 set_flag(t,i + 10,1);
 }

if ((a >= 467) && (a <= 470)) {
 set_flag(t,i,39);
 set_flag(t,i + 10,1);
 }

if ((a >= 471) && (a <= 474)) {
 set_flag(t,i,40);
 set_flag(t,i + 10,1);
 }

if ((a >= 475) && (a <= 478)) {
 set_flag(t,i,41);
 set_flag(t,i + 10,1);
 }

if ((a >= 479) && (a <= 482)) {
 set_flag(t,i,42);
 set_flag(t,i + 10,1);
 }

if ((a >= 483) && (a <= 486)) {
 set_flag(t,i,43);
 set_flag(t,i + 10,1);
 }

if ((a >= 487) && (a <= 490)) {
 set_flag(t,i,44);
 set_flag(t,i + 10,1);
 }

if ((a >= 491) && (a <= 494)) {
 set_flag(t,i,46);
 set_flag(t,i + 10,1);
 }

if ((a >= 495) && (a <= 498)) {
 set_flag(t,i,47);
 set_flag(t,i + 10,1);
 }

if ((a >= 499) && (a <= 502)) {
 set_flag(t,i,48);
 set_flag(t,i + 10,1);
 }

if ((a >= 503) && (a <= 506)) {
 set_flag(t,i,49);
 set_flag(t,i + 10,1);
 }

if ((a >= 507) && (a <= 510)) {
 set_flag(t,i,50);
 set_flag(t,i + 10,1);
 }

if ((a >= 511) && (a <= 514)) {
 set_flag(t,i,51);
 set_flag(t,i + 10,1);
 }

if ((a >= 515) && (a <= 518)) {
 set_flag(t,i,52);
 set_flag(t,i + 10,1);
 }

if ((a >= 519) && (a <= 522)) {
 set_flag(t,i,53);
 set_flag(t,i + 10,1);
 }

if ((a >= 523) && (a <= 526)) {
 set_flag(t,i,54);
 set_flag(t,i + 10,1);
 }

if ((a >= 527) && (a <= 532)) {
 set_flag(t,i,59);
 set_flag(t,i + 10,1);
 }

if ((a >= 533) && (a <= 536)) {
 set_flag(t,i,66);
 set_flag(t,i + 10,1);
 }

if ((a >= 537) && (a <= 542)) {
 set_flag(t,i,69);
 set_flag(t,i + 10,1);
 }

if ((a >= 543) && (a <= 546)) {
 set_flag(t,i,70);
 set_flag(t,i + 10,1);
 }

if ((a >= 547) && (a <= 550)) {
 set_flag(t,i,71);
 set_flag(t,i + 10,1);
 }

if ((a >= 551) && (a <= 554)) {
 set_flag(t,i,72);
 set_flag(t,i + 10,1);
 }

if ((a >= 555) && (a <= 558)) {
 set_flag(t,i,73);
 set_flag(t,i + 10,1);
 }

if ((a >= 559) && (a <= 562)) {
 set_flag(t,i,74);
 set_flag(t,i + 10,1);
 }

if ((a >= 563) && (a <= 566)) {
 set_flag(t,i,75);
 set_flag(t,i + 10,1);
 }

if ((a >= 567) && (a <= 570)) {
 set_flag(t,i,76);
 set_flag(t,i + 10,1);
 }

if ((a >= 571) && (a <= 574)) {
 set_flag(t,i,77);
 set_flag(t,i + 10,1);
 }

if ((a >= 575) && (a <= 578)) {
 set_flag(t,i,79);
 set_flag(t,i + 10,1);
 }

if ((a >= 579) && (a <= 582)) {
 set_flag(t,i,80);
 set_flag(t,i + 10,1);
 }

if ((a >= 583) && (a <= 586)) {
 set_flag(t,i,81);
 set_flag(t,i + 10,1);
 }

if ((a >= 587) && (a <= 590)) {
 set_flag(t,i,82);
 set_flag(t,i + 10,1);
 }

if ((a >= 591) && (a <= 594)) {
 set_flag(t,i,83);
 set_flag(t,i + 10,1);
 }

if ((a >= 595) && (a <= 598)) {
 set_flag(t,i,84);
 set_flag(t,i + 10,1);
 }

if ((a >= 599) && (a <= 602)) {
 set_flag(t,i,85);
 set_flag(t,i + 10,1);
 }

if ((a >= 603) && (a <= 606)) {
 set_flag(t,i,86);
 set_flag(t,i + 10,1);
 }

if ((a >= 607) && (a <= 610)) {
 set_flag(t,i,87);
 set_flag(t,i + 10,1);
 }

if ((a >= 611) && (a <= 614)) {
 set_flag(t,i,88);
 set_flag(t,i + 10,1);
 }

if ((a >= 615) && (a <= 618)) {
 set_flag(t,i,89);
 set_flag(t,i + 10,1);
 }

if ((a >= 619) && (a <= 622)) {
 set_flag(t,i,93);
 set_flag(t,i + 10,1);
 }

if ((a >= 623) && (a <= 626)) {
 set_flag(t,i,94);
 set_flag(t,i + 10,1);
 }

if ((a >= 627) && (a <= 630)) {
 set_flag(t,i,96);
 set_flag(t,i + 10,1);
 }

if ((a >= 631) && (a <= 634)) {
 set_flag(t,i,97);
 set_flag(t,i + 10,1);
 }

if ((a >= 635) && (a <= 638)) {
 set_flag(t,i,98);
 set_flag(t,i + 10,1);
 }

if ((a >= 639) && (a <= 642)) {
 set_flag(t,i,99);
 set_flag(t,i + 10,1);
 }

if ((a >= 643) && (a <= 646)) {
 set_flag(t,i,100);
 set_flag(t,i + 10,1);
 }

if ((a >= 647) && (a <= 650)) {
 set_flag(t,i,101);
 set_flag(t,i + 10,1);
 }

if ((a >= 651) && (a <= 654)) {
 set_flag(t,i,102);
 set_flag(t,i + 10,1);
 }

if ((a >= 655) && (a <= 658)) {
 set_flag(t,i,103);
 set_flag(t,i + 10,1);
 }

if ((a >= 659) && (a <= 662)) {
 set_flag(t,i,104);
 set_flag(t,i + 10,1);
 }

if ((a >= 663) && (a <= 666)) {
 set_flag(t,i,105);
 set_flag(t,i + 10,1);
 }

if ((a >= 667) && (a <= 670)) {
 set_flag(t,i,106);
 set_flag(t,i + 10,1);
 }

if ((a >= 671) && (a <= 674)) {
 set_flag(t,i,108);
 set_flag(t,i + 10,1);
 }

if ((a >= 675) && (a <= 678)) {
 set_flag(t,i,109);
 set_flag(t,i + 10,1);
 }

if ((a >= 679) && (a <= 682)) {
 set_flag(t,i,111);
 set_flag(t,i + 10,1);
 }

if ((a >= 683) && (a <= 686)) {
 set_flag(t,i,112);
 set_flag(t,i + 10,1);
 }

if ((a >= 687) && (a <= 690)) {
 set_flag(t,i,113);
 set_flag(t,i + 10,1);
 }

if ((a >= 691) && (a <= 694)) {
 set_flag(t,i,114);
 set_flag(t,i + 10,1);
 }

if ((a >= 695) && (a <= 698)) {
 set_flag(t,i,115);
 set_flag(t,i + 10,1);
 }

if ((a >= 699) && (a <= 702)) {
 set_flag(t,i,124);
 set_flag(t,i + 10,1);
 }

if ((a >= 703) && (a <= 706)) {
 set_flag(t,i,125);
 set_flag(t,i + 10,1);
 }

if ((a >= 707) && (a <= 710)) {
 set_flag(t,i,128);
 set_flag(t,i + 10,1);
 }

if ((a >= 711) && (a <= 714)) {
 set_flag(t,i,129);
 set_flag(t,i + 10,1);
 }

if ((a >= 715) && (a <= 718)) {
 set_flag(t,i,131);
 set_flag(t,i + 10,1);
 }

if ((a >= 719) && (a <= 722)) {
 set_flag(t,i,134);
 set_flag(t,i + 10,1);
 }

if ((a >= 723) && (a <= 726)) {
 set_flag(t,i,139);
 set_flag(t,i + 10,1);
 }

if ((a >= 727) && (a <= 730)) {
 set_flag(t,i,140);
 set_flag(t,i + 10,1);
 }

if ((a >= 731) && (a <= 734)) {
 set_flag(t,i,154);
 set_flag(t,i + 10,1);
 }

if ((a >= 735) && (a <= 738)) {
 set_flag(t,i,162);
 set_flag(t,i + 10,1);
 }

if ((a >= 739) && (a <= 742)) {
 set_flag(t,i,164);
 set_flag(t,i + 10,1);
 }

if ((a >= 743) && (a <= 746)) {
 set_flag(t,i,165);
 set_flag(t,i + 10,1);
 }

if ((a >= 747) && (a <= 750)) {
 set_flag(t,i,166);
 set_flag(t,i + 10,1);
 }

if ((a >= 751) && (a <= 754)) {
 set_flag(t,i,167);
 set_flag(t,i + 10,1);
 }

if ((a >= 755) && (a <= 758)) {
 set_flag(t,i,168);
 set_flag(t,i + 10,1);
 }

if ((a >= 759) && (a <= 762)) {
 set_flag(t,i,169);
 set_flag(t,i + 10,1);
 }

if ((a >= 763) && (a <= 766)) {
 set_flag(t,i,170);
 set_flag(t,i + 10,1);
 }

if ((a >= 767) && (a <= 770)) {
 set_flag(t,i,171);
 set_flag(t,i + 10,1);
 }

if ((a >= 771) && (a <= 774)) {
 set_flag(t,i,172);
 set_flag(t,i + 10,1);
 }

if ((a >= 775) && (a <= 778)) {
 set_flag(t,i,173);
 set_flag(t,i + 10,1);
 }

if ((a >= 779) && (a <= 782)) {
 set_flag(t,i,174);
 set_flag(t,i + 10,1);
 }

if ((a >= 783) && (a <= 786)) {
 set_flag(t,i,175);
 set_flag(t,i + 10,1);
 }

if ((a >= 787) && (a <= 790)) {
 set_flag(t,i,176);
 set_flag(t,i + 10,1);
 }

if ((a >= 791) && (a <= 794)) {
 set_flag(t,i,177);
 set_flag(t,i + 10,1);
 }

if ((a >= 795) && (a <= 798)) {
 set_flag(t,i,178);
 set_flag(t,i + 10,1);
 }

if ((a >= 799) && (a <= 802)) {
 set_flag(t,i,179);
 set_flag(t,i + 10,1);
 }

if ((a >= 803) && (a <= 806)) {
 set_flag(t,i,181);
 set_flag(t,i + 10,1);
 }

if ((a >= 807) && (a <= 810)) {
 set_flag(t,i,182);
 set_flag(t,i + 10,1);
 }

if ((a >= 811) && (a <= 814)) {
 set_flag(t,i,184);
 set_flag(t,i + 10,1);
 }

if ((a >= 815) && (a <= 818)) {
 set_flag(t,i,185);
 set_flag(t,i + 10,1);
 }

if ((a >= 819) && (a <= 822)) {
 set_flag(t,i,186);
 set_flag(t,i + 10,1);
 }

if ((a >= 823) && (a <= 826)) {
 set_flag(t,i,187);
 set_flag(t,i + 10,1);
 }

if ((a >= 827) && (a <= 830)) {
 set_flag(t,i,188);
 set_flag(t,i + 10,1);
 }

if ((a >= 831) && (a <= 1000)) {
 set_flag(t,i,0);
}

//	print_big_str("Number of item is ",(256 * get_flag(t,i + 10)) + get_flag(t,i),".");

}
i = i + 1;
}
break;

beginstate 62;
	c = who_used_custom_abil();
	change_custom_abil_uses(c,0,1);
	if	(get_energy (c) < 30) 
		print_str_color("You lack the required magical energy.",1);	
	if	(get_stat(c,11) < 3) 
		print_str_color("You lack the required knowledge of Mage spells.",1);	

	if	((get_energy (c) >= 30) && (get_stat(c,11) >= 3)) {
		change_char_energy(c,-30);
		print_str_color("You call upon higher powers for insight.",2);	
		begin_talk_mode(190);
	}
break;

