// 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,i,j,k,l,func_return = 0,next_state;
string dlgstr;

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;
	
	init_special_item(0,"Soul crystal","This is a magical device - a small crystal which can hold the images of monsters and then be used to recreate copies of those monsters via the spells Capture Soul and Recall Soul.");
	init_special_item(1,"Crystal Key","This is a small crystal key. You found it in the depths of the Lava Lizard Cave in the northwestern part of the Lava Ocean.");
	init_special_item(2,"Gold-thread tapestry","This is a beautifully woven tapestry with gold thread and the ancient slith design on it. You found it in the ruined slith city at the bottom of the Slime Pit.");
	init_special_item(3,"Tree","This is a tree that you recovered from Manikoss, south of the camp. Kass wants it.");
	init_special_item(4,"Iron Key","This is a large iron key. You recovered it from the body of the large doomguard in Kathaneth.");
	init_special_item(5,"Brass Key","This is a brass key. You took it from a guard in the barbarian fort near Thanopolen.");
	init_special_item(6,"Copper Key","This is a small copper key. Ithik gave it to you in the Barbarian Fort near the Peninsula Camp.");
	init_special_item(7,"Battle Plans","These are the battle plans of the evil army in The Second Level of the afterlife. Leksandross, elsewhere in the same area, wants them.");
	init_special_item(8,"Bronze Key","This is a small bronze key. Sasha gave it to you in the Temple of Sothana. It opens the doors to the room of the Chief Priest, Zektoth.");
	init_special_item(9,"Silver Key","This is a small silver key. You found it on the body of Zektoth under the Temple of Sothana.");

	init_special_abil(0,"Spellbook/Spell Info",12);
	init_special_abil(1,"Set Cut Scene Speed",10);
	init_special_abil(2,"Party Location",19);
	init_special_abil(3,"Tip of the Day",20);
	
	init_quest(0,"Scout south","Pithoss in Camp told you that Legare wants you to scout the river south. Find another stopping place for the expedition.");
	init_quest(1,"Clear path to Thassaka","Danoss in Neoss wants you to clear a path through the monsters and barbarians to Thassaka.");
	init_quest(2,"Investigate Thassaka","Kass in the Beach told you to make contact with Thassaka and find out if it is still intact. Return to him to explain the situation.");
	init_quest(3,"Kill barbarian archmage","Khalthanas in Thassaka wants you to kill a barbarian archmage who is on an island east of Thassaka in a river of lava.");
	init_quest(4,"Scout ahead","Pithoss in Thassaka wants you to continue traveling south and find both a good stopping place for the expedition and the way to proceed from there.");
	init_quest(5,"Explore cave","Kass in the Island Camp wants you to explore the route out that you found in the lava lizard cave west of the island. Keep scouting until you've found another good stopping point for the expedition.");
	init_quest(6,"Explore vahnatai lab","Kass in the Island Camp wants you to explore the vahnatai lab in the east and find a way out of the Lava Ocean. Report your findings when you're done.");
	init_quest(7,"Fetch a tapestry","Kass in the Camp wants you to find a gold-threaded tapestry in a slime pit to the east and return it to him.");
	init_quest(8,"Fetch two trees","Kass in the Camp wants you to find two trees in a cave to the south and return them to him.");
	init_quest(9,"Scout south","Kass in the Camp wants you to scout south and find another good stopping place for the expedition.");
	init_quest(10,"Recover Legare","Pithoss in the Peninsula Camp wants you to climb the mountain to the west and bring back Legare.");
	init_quest(11,"Recover Ethass","Pithoss in the Peninsula Camp wants you to head south and find Ethass, who is currently possessed by the Goddess. Bring her back to the camp if possible.");
	init_quest(12,"Go to new camp","Pithoss in the Peninsula Camp wants you to head south down the river and reach a good landing spot for the expedition. There is no need to report back, as the expedition will be right behind you.");
	init_quest(13,"Save Legare's soul","Pithoss in the Avernoss Camp wants you to journey to the land of the dead via the island in the middle of Lake Avernoss and save Legare's soul from Nathaganth.");
	init_quest(14,"Reach the homeland","Pithoss in the Avernoss Camp wants you to finish the journey: reach Danatha, west of Lake Avernoss.");
	init_quest(15,"Explore south cave","Kass in the Island Camp wants you to explore small, hidden cave in the south and find a way out of the Lava Ocean. Report your findings when you're done.");
	init_quest(16,"Find Chief Priest","Sasha in the Temple of Sothana wants you to find the Chief Priest, Zektoth, and deal with him. If he is evil, kill him.");
	
	if ((get_flag(204,3) == 1) && (get_terrain(35,12) == 396))
		set_terrain(35,12,458);
		
	// Tip of the Day
	// If inactive, shut everything down.
	if (get_flag(290,4) == 0)
		end();
	
	// Choose a tip.
	i = get_ran(1,1,10);
	
	// Now send to the Tip state.
	set_state_continue(60);
	
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;
	// Boats, horses.
	create_boat(1,10,18,15,0);
	
	create_horse(0,47,62,56,1);
	
	// Initialize a few shops.
	
	// Shop 0 - Silthokh in Vasskolis
	add_item_to_shop(0,57,2); // Steel Longsword
	add_item_to_shop(0,67,2); // Steel Spear
	add_item_to_shop(0,41,1); // Iron Plate Mail
	add_item_to_shop(0,42,1); // Steel Plate Mail
	add_item_to_shop(0,114,4); // Boots
	add_item_to_shop(0,132,4); // Iron Large Shield
	add_item_to_shop(0,91,2); // Ash Bow
	add_item_to_shop(0,96,1); // Ash Crossbow
	add_item_to_shop(0,315,2); // Blessed Sling
	add_item_to_shop(0,174,1); // Lockpicks
	add_item_to_shop(0,87,5); // Steel Javelin
	add_item_to_shop(0,101,10); // Iron Arrows
	add_item_to_shop(0,102,10); // Steel Arrows
	add_item_to_shop(0,106,10); // Iron Bolts
	add_item_to_shop(0,107,10); // Steel Bolts
	add_item_to_shop(0,282,1); // Wand of Bolts
	add_item_to_shop(0,283,1); // Wand of Ice
	add_item_to_shop(0,228,8); // Healing Elixir
	add_item_to_shop(0,229,9); // Curing Elixir
	add_item_to_shop(0,230,7); // Strength Elixir
	add_item_to_shop(0,231,8); // Energy Elixir
	add_item_to_shop(0,214,6); // Healing Herbs
	
	// Shop 1 - Silthokh in first Camp
	add_item_to_shop(1,174,1); // Lockpicks
	add_item_to_shop(1,91,1); // Ash Bow
	add_item_to_shop(1,96,2); // Ash Crossbow
	add_item_to_shop(1,315,1); // Blessed Sling
	add_item_to_shop(1,86,9); // Iron Javelin
	add_item_to_shop(1,101,9); // Iron Arrows
	add_item_to_shop(1,106,9); // Iron Bolts
	add_item_to_shop(1,282,1); // Wand of Bolts
	add_item_to_shop(1,283,1); // Wand of Ice
	add_item_to_shop(1,233,5); // Haste Elixir
	add_item_to_shop(1,235,4); // Resistance Elixir
	add_item_to_shop(1,231,8); // Energy Elixir
	add_item_to_shop(1,239,3); // Heroic Brew
	add_item_to_shop(1,215,6); // Spiritual Herbs
	add_item_to_shop(1,216,5); // Energetic Herbs
	
	// Shop 2 - Thassaka forge
	add_item_to_shop(2,43,4); // Blessed Plate Mail
	add_item_to_shop(2,63,3); // Blessed Greatsword
	add_item_to_shop(2,58,2); // Blessed Longsword
	add_item_to_shop(2,48,2); // Blessed Dagger
	add_item_to_shop(2,134,4); // Blessed Large Shield
	add_item_to_shop(2,93,2); // Blessed Bow
	add_item_to_shop(2,98,2); // Blessed Crossbow
	add_item_to_shop(2,315,2); // Blessed Sling
	add_item_to_shop(2,88,10); // Blessed Javelin
	add_item_to_shop(2,103,9); // Blessed Arrows
	add_item_to_shop(2,108,9); // Blessed Bolts
	
	// Shop 3 - Faskoss's mage spells
	add_item_to_shop(3,2000,3); // Bolt of Fire
	add_item_to_shop(3,2001,3); // Light
	add_item_to_shop(3,2002,3); // Call Beast
	add_item_to_shop(3,2003,3); // Spray Acid
	add_item_to_shop(3,2004,3); // Haste
	add_item_to_shop(3,2005,3); // Slow
	add_item_to_shop(3,2006,3); // Ice Lances
	add_item_to_shop(3,2007,3); // Unlock Doors
	add_item_to_shop(3,2008,3); // Create Illusions
	add_item_to_shop(3,2009,3); // Far Sight
	add_item_to_shop(3,2010,3); // Lightning Spray
	add_item_to_shop(3,2011,3); // Capture Mind
	add_item_to_shop(3,2012,3); // Simulacrum
	add_item_to_shop(3,2013,3); // Dispel Barrier
	add_item_to_shop(3,2014,3); // Summon Aid
	add_item_to_shop(3,2015,3); // Forcecage
	add_item_to_shop(3,2016,3); // Fireblast
	add_item_to_shop(3,2017,3); // Arcane Summon
	add_item_to_shop(3,2018,3); // Arcane Shield
	add_item_to_shop(3,2019,3); // Arcane Blow
	
	// Shop 4 - Faskoss's priest spells
	add_item_to_shop(4,3000,3); // Healing
	add_item_to_shop(4,3001,3); // Curing
	add_item_to_shop(4,3002,3); // War Blessing
	add_item_to_shop(4,3003,3); // Terror
	add_item_to_shop(4,3004,3); // Repel Spirit
	add_item_to_shop(4,3005,3); // Smite
	add_item_to_shop(4,3006,3); // Summon Shade
	add_item_to_shop(4,3007,3); // Enduring Barrier
	add_item_to_shop(4,3008,3); // Unshackle Mind
	add_item_to_shop(4,3009,3); // Move Mountains
	add_item_to_shop(4,3010,3); // Mass Healing
	add_item_to_shop(4,3011,3); // Mass Curing
	add_item_to_shop(4,3012,3); // Radiant Shield
	add_item_to_shop(4,3013,3); // Divine Fire
	add_item_to_shop(4,3014,3); // Control Foes
	add_item_to_shop(4,3015,3); // Cloud of Blades
	add_item_to_shop(4,3016,3); // Return Life
	add_item_to_shop(4,3017,3); // Divine Retribution
	add_item_to_shop(4,3018,3); // Divine Restoration
	add_item_to_shop(4,3019,3); // Divine Host
	
	// Shop 5 - Sakna's utility items in Thassaka
	add_item_to_shop(5,57,2); // Steel Longsword
	add_item_to_shop(5,92,1); // Yew Bow
	add_item_to_shop(5,93,1); // Blessed Bow
	add_item_to_shop(5,315,1); // Blessed Sling
	add_item_to_shop(5,101,3); // Iron Arrows
	add_item_to_shop(5,102,3); // Steel Arrows
	add_item_to_shop(5,175,2); // Fine Lockpicks
	add_item_to_shop(5,171,5); // Torch
	add_item_to_shop(5,172,3); // Lamp
	add_item_to_shop(5,109,6); // Rock
	add_item_to_shop(5,217,2); // Graymold
	add_item_to_shop(5,327,1); // Unshackling Crystal
	add_item_to_shop(5,146,3); // Gloves
	add_item_to_shop(5,391,1); // Rope
	
	// Shop 12 - Pistokha's food in Thassaka
	add_item_to_shop(12,4,5); // Bread
	add_item_to_shop(12,5,5); // Mushrooms
	add_item_to_shop(12,6,4); // Greens
	add_item_to_shop(12,397,2); // Pears
	add_item_to_shop(12,398,2); // Apples
	add_item_to_shop(12,450,2); // Grapes
	add_item_to_shop(12,7,3); // Steak
	add_item_to_shop(12,8,6); // Dried Meat
	add_item_to_shop(12,9,4); // Weird Meat
	add_item_to_shop(12,400,3); // Meat on Spit
	add_item_to_shop(12,399,1); // Cake
	add_item_to_shop(12,393,5); // Cheap Wine
	add_item_to_shop(12,415,5); // Bottle of Ale
	
	// Shop 13 - Silthokh in Thassaka
	add_item_to_shop(13,315,1); // Blessed Sling
	add_item_to_shop(13,86,9); // Iron Javelin
	add_item_to_shop(13,101,9); // Iron Arrows
	add_item_to_shop(13,106,9); // Iron Bolts
	add_item_to_shop(13,228,5); // Healing Elixir
	add_item_to_shop(13,223,6); // Energy Potion
	add_item_to_shop(13,231,4); // Energy Elixir
	add_item_to_shop(13,234,2); // Invulnerability Elixir
	
	// Shop 6 - Silthokh in Lava Ocean Camp
	add_item_to_shop(6,175,1); // Fine Lockpicks
	add_item_to_shop(6,92,1); // Yew Bow
	add_item_to_shop(6,97,1); // Yew Crossbow
	add_item_to_shop(6,315,1); // Blessed Sling
	add_item_to_shop(6,87,12); // Steel Javelin
	add_item_to_shop(6,101,8); // Iron Arrows
	add_item_to_shop(6,102,9); // Steel Arrows
	add_item_to_shop(6,106,8); // Iron Bolts
	add_item_to_shop(6,107,7); // Steel Bolts
	add_item_to_shop(6,284,1); // Wand of Slowing
	add_item_to_shop(6,285,1); // Wand of Lightning
	add_item_to_shop(6,228,4); // Healing Elixir
	add_item_to_shop(6,231,7); // Energy Elixir
	add_item_to_shop(6,234,3); // Invulnerability Elixir
	add_item_to_shop(6,214,7); // Healing Herbs
	add_item_to_shop(6,215,6); // Spiritual Herbs
	add_item_to_shop(6,218,8); // Toadstools
	add_item_to_shop(6,219,5); // Mandrake
	
	// Shop 7 - Silthokh's mage spells in Lava Ocean (and future) Camps
	add_item_to_shop(7,2000,5); // Bolt of Fire
	add_item_to_shop(7,2001,5); // Light
	add_item_to_shop(7,2002,5); // Call Beast
	add_item_to_shop(7,2003,5); // Spray Acid
	add_item_to_shop(7,2004,5); // Haste
	add_item_to_shop(7,2005,5); // Slow
	add_item_to_shop(7,2006,5); // Ice Lances
	add_item_to_shop(7,2007,5); // Unlock Doors
	add_item_to_shop(7,2008,5); // Create Illusions
	add_item_to_shop(7,2009,5); // Far Sight
	add_item_to_shop(7,2010,5); // Lightning Spray
	add_item_to_shop(7,2011,5); // Capture Mind
	add_item_to_shop(7,2012,5); // Simulacrum
	add_item_to_shop(7,2013,5); // Dispel Barrier
	add_item_to_shop(7,2014,5); // Summon Aid
	add_item_to_shop(7,2015,5); // Forcecage
	add_item_to_shop(7,2016,5); // Fireblast
	add_item_to_shop(7,2017,5); // Arcane Summon
	add_item_to_shop(7,2018,5); // Arcane Shield
	add_item_to_shop(7,2019,5); // Arcane Blow
	
	// Shop 8 - Silthokh's priest spells in Lava Ocean (and future) Camps
	add_item_to_shop(8,3000,5); // Healing
	add_item_to_shop(8,3001,5); // Curing
	add_item_to_shop(8,3002,5); // War Blessing
	add_item_to_shop(8,3003,5); // Terror
	add_item_to_shop(8,3004,5); // Repel Spirit
	add_item_to_shop(8,3005,5); // Smite
	add_item_to_shop(8,3006,5); // Summon Shade
	add_item_to_shop(8,3007,5); // Enduring Barrier
	add_item_to_shop(8,3008,5); // Unshackle Mind
	add_item_to_shop(8,3009,5); // Move Mountains
	add_item_to_shop(8,3010,5); // Mass Healing
	add_item_to_shop(8,3011,5); // Mass Curing
	add_item_to_shop(8,3012,5); // Radiant Shield
	add_item_to_shop(8,3013,5); // Divine Fire
	add_item_to_shop(8,3014,5); // Control Foes
	add_item_to_shop(8,3015,5); // Cloud of Blades
	add_item_to_shop(8,3016,5); // Return Life
	add_item_to_shop(8,3017,5); // Divine Retribution
	add_item_to_shop(8,3018,5); // Divine Restoration
	add_item_to_shop(8,3019,5); // Divine Host
	
	// Shop 9 - Silthokh in Strange Cave
	add_item_to_shop(9,174,1); // Lockpicks
	add_item_to_shop(9,91,1); // Ash Bow
	add_item_to_shop(9,96,1); // Ash Crossbow
	add_item_to_shop(9,315,1); // Blessed Sling
	add_item_to_shop(9,86,5); // Iron Javelin
	add_item_to_shop(9,101,12); // Iron Arrows
	add_item_to_shop(9,102,9); // Steel Arrows
	add_item_to_shop(9,106,15); // Iron Bolts
	add_item_to_shop(9,178,4); // Fine First Aid Kit
	add_item_to_shop(9,286,1); // Wand of Fire
	add_item_to_shop(9,287,1); // Wand of Acid
	add_item_to_shop(9,230,4); // Strength Elixir
	add_item_to_shop(9,231,7); // Energy Elixir
	add_item_to_shop(9,238,3); // Protection Brew
	add_item_to_shop(9,215,6); // Spiritual Herbs
	add_item_to_shop(9,216,7); // Energetic Herbs
	add_item_to_shop(9,217,6); // Graymold
	add_item_to_shop(9,219,5); // Mandrake
	
	// Shop 10 - Silthokh in Peninsula Camp
	add_item_to_shop(10,174,1); // Lockpicks
	add_item_to_shop(10,91,1); // Ash Bow
	add_item_to_shop(10,96,1); // Ash Crossbow
	add_item_to_shop(10,315,1); // Blessed Sling
	add_item_to_shop(10,86,8); // Iron Javelin
	add_item_to_shop(10,101,9); // Iron Arrows
	add_item_to_shop(10,106,9); // Iron Bolts
	add_item_to_shop(10,177,3); // First Aid Kit
	add_item_to_shop(10,286,2); // Wand of Forcecage
	add_item_to_shop(10,287,4); // Wand of Arcana
	add_item_to_shop(10,228,4); // Healing Elixir
	add_item_to_shop(10,231,8); // Energy Elixir
	add_item_to_shop(10,233,7); // Haste Elixir
	add_item_to_shop(10,236,2); // Knowledge Brew
	add_item_to_shop(10,214,8); // Healing Herbs
	add_item_to_shop(10,215,7); // Spiritual Herbs
	add_item_to_shop(10,216,8); // Energetic Herbs
	add_item_to_shop(10,217,6); // Graymold
	add_item_to_shop(10,218,5); // Toadstools
	add_item_to_shop(10,219,4); // Mandrake
	
	// Shop 11 - Silthokh in Avernoss Camp
	add_item_to_shop(11,325,2); // Blessed Bracer
	add_item_to_shop(11,301,1); // Enchanted Lockpicks
	add_item_to_shop(11,93,1); // Blessed Bow
	add_item_to_shop(11,98,1); // Blessed Crossbow
	add_item_to_shop(11,315,1); // Blessed Sling
	add_item_to_shop(11,87,14); // Steel Javelin
	add_item_to_shop(11,102,12); // Steel Arrows
	add_item_to_shop(11,107,12); // Steel Bolts
	add_item_to_shop(11,284,2); // Wand of Slowing
	add_item_to_shop(11,285,2); // Wand of Lightning
	add_item_to_shop(11,287,5); // Wand of Arcana
	add_item_to_shop(11,228,7); // Healing Elixir
	add_item_to_shop(11,231,9); // Energy Elixir
	add_item_to_shop(11,233,4); // Haste Elixir
	add_item_to_shop(11,239,4); // Heroic Brew
	add_item_to_shop(11,214,7); // Healing Herbs
	add_item_to_shop(11,215,7); // Spiritual Herbs
	add_item_to_shop(11,216,6); // Energetic Herbs
	add_item_to_shop(11,217,5); // Graymold
	add_item_to_shop(11,218,5); // Toadstools
	add_item_to_shop(11,219,4); // Mandrake
	
	// Shop 12 above because it's in Thassaka
	// Same with Shop 13
	
	// Shop 14 - Silthokh's recipes
	i = 0;
	while (i <= 16) {
		add_item_to_shop(14,4000 + i,1);
		i = i + 1;
		}
	
	// set appropriate flags
	set_flag(290,0,3);
	set_flag(290,1,species_in_party(0) + (species_in_party(2) * 2) + (species_in_party(3) * 4));
	set_flag(290,4,1);
	i = 0;
	while (i <= 3) {
		inc_flag(291,get_species(i),1);
		set_char_trait(i,22,1);
		change_custom_abil_uses(i,0,1);
		change_custom_abil_uses(i,1,1);
		change_custom_abil_uses(i,2,1);
		change_custom_abil_uses(i,3,1);
		i = i + 1;
		}
	
	i = 0;
	if (char_ok(0))
		i = i + get_level(0);
	if (char_ok(1))
		i = i + get_level(1);
	if (char_ok(2))
		i = i + get_level(2);
	if (char_ok(3))
		i = i + get_level(3);
	i = i / party_size();
	if (i == 250)
		message_dialog("Kelandon says: A god party is HIGHLY NOT RECOMMENDED for this scenario. God parties may trigger bugs that I didn't bother to fix because normal parties can't trigger them.","");
		
break;

// This state is called every tick wherever the party is in the scenario.
// You can use the set_state
beginstate START_STATE;
	if (get_flag(290,5) == 0)
		end();
	
	i = 0;
	while ((char_ok(i) == 0) && (i <= 3))
		{i = i + 1; }
	if (i == 4)
		end();
	if (get_char_status(i,25) == 0) {
		set_flag(290,5,0);
		print_str_color("You land safely.",2);
		}
break;

// Place your own states below. Give each a number at least 10.
beginstate 10; // cut scene speed
	change_custom_abil_uses(who_used_custom_abil(),1,1);
	
	reset_dialog();
	add_dialog_str(0,"How fast would you like the cut scenes to go?",0);
	add_dialog_choice(0,"Slow speed.");
	add_dialog_choice(1,"Normal speed.");
	add_dialog_choice(2,"Faster!");
	choice = run_dialog(1);
	if (choice == 1)
		set_flag(290,0,4);
	if (choice == 2)
		set_flag(290,0,3);
	if (choice != 3) {
		end();
		}
	
	reset_dialog();
	add_dialog_str(0,"How fast would you like the cut scenes to go?",0);
	add_dialog_choice(0,"High speed.");
	add_dialog_choice(1,"Very high speed.");
	if (run_dialog(1) == 1)
		set_flag(290,0,2);
	else
		set_flag(290,0,1);
break;

beginstate 11; // seeing the barbarian sliths for the first time
if (get_flag(290,2) == 0) {
	reset_dialog();
	add_dialog_str(0,"You encounter some strange-looking sliths. At least, they're lizard-like humanoids who carry two-tined spears; they must be sliths. However, they are larger, hunched over, and more muscular than normal sliths. They look more primitive.",0);
	add_dialog_str(1,"They hiss in brutish tones that vaguely resemble the ancient slith language that you've learned, but you can't understand them, and you feel certain that they are not even speaking a full language, just the equivalent of pack wolves barking.",0);
	add_dialog_str(2,"This is deeply disturbing. If the sliths of this area all are like this, the homeland may contain nothing worth seeing at all.",0);
	set_flag(290,2,run_dialog(1));
	}
break;

beginstate 12; // begin special spells madness o god
	change_custom_abil_uses(who_used_custom_abil(),0,1);
	
	// Separate between getting info and writing a spellbook.
	// If the former, send to a different state.
	reset_dialog();
	add_dialog_str(0,"Do you want to write a spellbook or get info on a special spell?",0);
	add_dialog_choice(0,"Neither. I changed my mind.");
	add_dialog_choice(1,"Write a spellbook.");
	add_dialog_choice(2,"Get info on a special spell.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	if (choice == 3) {
		reset_dialog();
		add_dialog_str(0,"Do you want info on a hostile spell or a blessing spell?",0);
		add_dialog_choice(0,"A hostile spell.");
		add_dialog_choice(1,"A blessing spell.");
		if (run_dialog(1) == 1)
			set_state_continue(40);
		else
			set_state_continue(49);
		}
	
	// If the party has no special spells, give a message and end.
	if ((get_flag(292,0) == 0) && (get_flag(293,0) == 0)) {
		message_dialog("The party has not learned special spells yet. There is no need to write a spellbook.","");
		end();
		}
	
	// Can't write spellbooks in combat, because that would make no sense in RP
	// terms.
	if (is_combat()) {
		message_dialog("You can't write a spellbook in the middle of combat. It takes more care and concentration than you can devote while running around like that.","");
		end();
		}
	
	// Separate between targeted and untargeted spells.
	reset_dialog();
	add_dialog_str(0,"What kind of spellbook would you like to write? (You can only have one copy of each kind of spellbook at a time.)",0);
	add_dialog_choice(0,"No spellbook. I changed my mind.");
	if (char_has_item(who_used_custom_abil(),497) == 0)
		if (get_flag(292,0) > 0)
			add_dialog_choice(1,"A spellbook for hostile spells.");
	if (char_has_item(who_used_custom_abil(),498) == 0)
		if (get_flag(293,0) > 0)
			add_dialog_choice(2,"A spellbook for blessing spells.");
	choice = run_dialog(1);
	// To make up for BoA's bug, set the choice to the proper number.
	if ((char_has_item(who_used_custom_abil(),497) != 0) || (get_flag(292,0) == 0))
		if (choice == 2)
			choice = 3;
	
	if (choice == 1)
		end();
	if (choice == 2) // hostile spellbook
		char_give_item(who_used_custom_abil(),497);
	if (choice == 3) // blessing spellbook
		char_give_item(who_used_custom_abil(),498);
	
	message_dialog("You write out a short description of the unusual spells that you have learned, storing them away in your spellbook for future reference.","");
		
break;

beginstate 13; // using hostile special spells - oh the horror
	// Can't cast spells if dumbfounded
	if (get_char_status(who_used_custom_item(),13) > 0) {
		print_str("You can't cast complex spells while dumbfounded.");
		end();
		}
	
	// Can't cast these outdoors.
	if (is_outdoor() == 1) {
		print_str("You can't cast hostile spells outdoors.");
		end();
		}
	
	// Can't cast these if in an AM-field.
	if (is_field_on_space(char_loc_x(who_used_custom_item()),char_loc_y(who_used_custom_item()),2)) {
		print_str("You can't cast spells in an anti-magic field.");
		end();
		}
	
	// Ask which spell to cast. Also list which spells are available.
	reset_dialog();
	add_dialog_str(0,"Which special spell would you like to cast? (Hit _OK_ and type in the full spell name, the spell initials, or the spell number.)",0);
	add_dialog_str(1,"Available spells:",0);
	clear_buffer();
	// Begin the listing madness. The list uses four variables.
	// k stores whether this special spell is the first in the list
	k = 0;
	if ((get_flag(292,1) > 0) && (get_stat(who_used_custom_item(),11) >= 10)) {
		k = 1;
		append_string("1 Bind Foe");
		}
	if ((get_flag(292,2) > 0) && (get_stat(who_used_custom_item(),11) >= 18)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("2 Poison");
		}
	if ((get_flag(292,3) > 0) && (get_stat(who_used_custom_item(),12) >= 18)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("3 Filth");
		}
	if ((get_flag(292,4) > 0) && (get_stat(who_used_custom_item(),12) >= 10)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("4 Capture Soul");
		}
	if ((get_flag(292,5) > 0) && (get_stat(who_used_custom_item(),12) >= 10)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("5 Recall Soul");
		}
	if ((get_flag(292,6) > 0) && (get_stat(who_used_custom_item(),11) >= 22)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("6 Destroy Mind");
		}
	if ((get_flag(292,7) > 0) && (get_stat(who_used_custom_item(),12) >= 19)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("7 Sleep Foe");
		}
	if ((get_flag(292,8) > 0) && (get_stat(who_used_custom_item(),12) >= 20)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("8 Paralyze");
		}
	if ((get_flag(292,9) > 0) && (get_stat(who_used_custom_item(),12) >= 22)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("9 Anti-Magic Field");
		}
	if ((get_flag(292,10) > 0) && (get_stat(who_used_custom_item(),12) >= 20)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("10 Sleep Cloud");
		}
	if ((get_flag(292,11) > 0) && (get_stat(who_used_custom_item(),12) >= 20)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("11 Quickfire");
		}
	if ((get_flag(292,12) > 0) && (get_stat(who_used_custom_item(),11) >= 20)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("12 Fire Field");
		}
	if ((get_flag(292,13) > 0) && (get_stat(who_used_custom_item(),11) >= 20)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("13 Ice Field");
		}
	if ((get_flag(292,14) > 0) && (get_stat(who_used_custom_item(),11) >= 25)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("14 Death Knell");
		}
	if ((get_flag(292,15) > 0) && (get_stat(who_used_custom_item(),11) >= 21)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("15 Destruction");
		}
	if ((get_flag(292,16) > 0) && (get_stat(who_used_custom_item(),11) >= 23)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("16 Cloudkill");
		}
	if ((get_flag(292,17) > 0) && (get_stat(who_used_custom_item(),11) >= 18)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("17 Inferno");
		}
	if ((get_flag(292,18) > 0) && (get_stat(who_used_custom_item(),11) >= 19)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("18 Bonechill");
		}
	append_string(".");
	get_buffer_text(dlgstr);
	add_dialog_str(2,dlgstr,0);
	add_dialog_str(3,"If you've changed your mind, type _cancel_ in the box. To get info on any of these spells, use the spellbook ability and choose to get info.",0);
	run_dialog(1);
	
	// Not coming from an info state.
	j = 0;
	
	set_state_continue(14);
	
break;
	
beginstate 14;
	
	// Now actually get the text response. Variable i stores the spell number.
	// First it must be zeroed.
	i = 0;
	get_text_response("Which spell?");
	check_text_response_match("Bind Foe");
	if (got_text_match())
		i = 1;
	check_text_response_match("Bind");
	if (got_text_match())
		i = 1;
	check_text_response_match("BF");
	if (got_text_match())
		i = 1;
	check_text_response_match("1");
	if (got_text_match())
		i = 1;
	check_text_response_match("Poison");
	if (got_text_match())
		i = 2;
	check_text_response_match("PO");
	if (got_text_match())
		i = 2;
	check_text_response_match("2");
	if (got_text_match())
		i = 2;
	check_text_response_match("Filth");
	if (got_text_match())
		i = 3;
	check_text_response_match("F");
	if (got_text_match())
		i = 3;
	check_text_response_match("FI");
	if (got_text_match())
		i = 3;
	check_text_response_match("3");
	if (got_text_match())
		i = 3;
	check_text_response_match("Capture Soul");
	if (got_text_match())
		i = 4;
	check_text_response_match("Capture");
	if (got_text_match())
		i = 4;
	check_text_response_match("CS");
	if (got_text_match())
		i = 4;
	check_text_response_match("CA");
	if (got_text_match())
		i = 4;
	check_text_response_match("CAP");
	if (got_text_match())
		i = 4;
	check_text_response_match("4");
	if (got_text_match())
		i = 4;
	check_text_response_match("Recall Soul");
	if (got_text_match())
		i = 5;
	check_text_response_match("Recall");
	if (got_text_match())
		i = 5;
	check_text_response_match("RS");
	if (got_text_match())
		i = 5;
	check_text_response_match("5");
	if (got_text_match())
		i = 5;
	check_text_response_match("Destroy Mind");
	if (got_text_match())
		i = 6;
	check_text_response_match("Destroy");
	if (got_text_match())
		i = 6;
	check_text_response_match("DM");
	if (got_text_match())
		i = 6;
	check_text_response_match("DES");
	if (got_text_match())
		i = 6;
	check_text_response_match("6");
	if (got_text_match())
		i = 6;
	check_text_response_match("Sleep Foe");
	if (got_text_match())
		i = 7;
	check_text_response_match("SF");
	if (got_text_match())
		i = 7;
	check_text_response_match("7");
	if (got_text_match())
		i = 7;
	check_text_response_match("Paralyze");
	if (got_text_match())
		i = 8;
	check_text_response_match("P");
	if (got_text_match())
		i = 8;
	check_text_response_match("PARA");
	if (got_text_match())
		i = 8;
	check_text_response_match("8");
	if (got_text_match())
		i = 8;
	check_text_response_match("Anti-Magic Field");
	if (got_text_match())
		i = 9;
	check_text_response_match("Anti");
	if (got_text_match())
		i = 9;
	check_text_response_match("A");
	if (got_text_match())
		i = 9;
	check_text_response_match("AM");
	if (got_text_match())
		i = 9;
	check_text_response_match("AMF");
	if (got_text_match())
		i = 9;
	check_text_response_match("9");
	if (got_text_match())
		i = 9;
	check_text_response_match("Sleep Cloud");
	if (got_text_match())
		i = 10;
	check_text_response_match("SC");
	if (got_text_match())
		i = 10;
	check_text_response_match("10");
	if (got_text_match())
		i = 10;
	check_text_response_match("Quickfire");
	if (got_text_match())
		i = 11;
	check_text_response_match("Q");
	if (got_text_match())
		i = 11;
	check_text_response_match("QU");
	if (got_text_match())
		i = 11;
	check_text_response_match("QF");
	if (got_text_match())
		i = 11;
	check_text_response_match("11");
	if (got_text_match())
		i = 11;
	check_text_response_match("Fire Field");
	if (got_text_match())
		i = 12;
	check_text_response_match("Fire");
	if (got_text_match())
		i = 12;
	check_text_response_match("FF");
	if (got_text_match())
		i = 12;
	check_text_response_match("12");
	if (got_text_match())
		i = 12;
	check_text_response_match("Ice Field");
	if (got_text_match())
		i = 13;
	check_text_response_match("Ice");
	if (got_text_match())
		i = 13;
	check_text_response_match("IF");
	if (got_text_match())
		i = 13;
	check_text_response_match("13");
	if (got_text_match())
		i = 13;
	check_text_response_match("Death Knell");
	if (got_text_match())
		i = 14;
	check_text_response_match("Death");
	if (got_text_match())
		i = 14;
	check_text_response_match("DK");
	if (got_text_match())
		i = 14;
	check_text_response_match("14");
	if (got_text_match())
		i = 14;
	check_text_response_match("Destruction");
	if (got_text_match())
		i = 15;
	check_text_response_match("D");
	if (got_text_match())
		i = 15;
	check_text_response_match("DE");
	if (got_text_match())
		i = 15;
	check_text_response_match("DES");
	if (got_text_match())
		i = 15;
	check_text_response_match("15");
	if (got_text_match())
		i = 15;
	check_text_response_match("Cloudkill");
	if (got_text_match())
		i = 16;
	check_text_response_match("C");
	if (got_text_match())
		i = 16;
	check_text_response_match("CK");
	if (got_text_match())
		i = 16;
	check_text_response_match("16");
	if (got_text_match())
		i = 16;
	check_text_response_match("Inferno");
	if (got_text_match())
		i = 17;
	check_text_response_match("I");
	if (got_text_match())
		i = 17;
	check_text_response_match("IN");
	if (got_text_match())
		i = 17;
	check_text_response_match("17");
	if (got_text_match())
		i = 17;
	check_text_response_match("Bonechill");
	if (got_text_match())
		i = 18;
	check_text_response_match("Bone");
	if (got_text_match())
		i = 18;
	check_text_response_match("BC");
	if (got_text_match())
		i = 18;
	check_text_response_match("B");
	if (got_text_match())
		i = 18;
	check_text_response_match("BO");
	if (got_text_match())
		i = 18;
	check_text_response_match("18");
	if (got_text_match())
		i = 18;
	check_text_response_match("cancel");
	if (got_text_match())
		end();
	
	// If the player entered P or SLEEP, disambiguate.
	check_text_response_match("P");
	if (got_text_match()) {
		// If they don't have one or the other of the spells, choose the right
		// one.
		if (get_flag(292,2) == 0)
			i = 8;
		if (get_flag(292,8) == 0)
			i = 2;
		
		if (i == 0) {
			reset_dialog();
			add_dialog_str(0,"By _P,_ did you mean Poison or Paralyze?",0);
			add_dialog_choice(0,"Neither. Whoops. Let me try again.");
			add_dialog_choice(1,"Poison.");
			add_dialog_choice(2,"Paralyze.");
			choice = run_dialog(1);
			if (choice == 1)
				set_state_continue(14);
			if (choice == 2)
				i = 2;
			if (choice == 1)
				i = 8;
			}
		}
	check_text_response_match("SLEEP");
	if (got_text_match()) {
		// If they don't have one or the other of the spells, choose the right
		// one.
		if (get_flag(292,7) == 0)
			i = 10;
		if (get_flag(292,10) == 0)
			i = 7;
		
		if (i == 0) {
			reset_dialog();
			add_dialog_str(0,"By _SLEEP,_ did you mean Sleep Foe or Sleep Cloud?",0);
			add_dialog_choice(0,"Neither. Whoops. Let me try again.");
			add_dialog_choice(1,"Sleep Foe.");
			add_dialog_choice(2,"Sleep Cloud.");
			choice = run_dialog(1);
			if (choice == 1)
				set_state_continue(14);
			if (choice == 2)
				i = 7;
			if (choice == 1)
				i = 10;
			}
		}
	
	check_text_response_match("info");
	if (got_text_match())
		set_state_continue(40);
	
	// Whew! If none of those things matched, then inform the player.
	if (i == 0) {
		reset_dialog();
		add_dialog_str(0,"That was not a name or proper abbreviation of any special spell. What would you like to do?",0);
		add_dialog_choice(0,"Let me try entering that again.");
		add_dialog_choice(1,"Let me see the list of special spells again.");
		add_dialog_choice(2,"Never mind. End all of this.");
		choice = run_dialog(1);
		if (choice == 1)
			set_state_continue(14);
		if (choice == 2) {
			if (j == 0)
				set_state_continue(13);
			else
				set_state_continue(40);
			}
		if (choice == 3)
			end();
		}
	
	// If the player sneakily chose a spell that he does not have, inform him.
	if (j == 0) {
		if (get_flag(292,i) == 0) {
			reset_dialog();
			add_dialog_str(0,"Unfortunately, you chose a spell that you do not have. What would you like to do?",0);
			add_dialog_choice(0,"Let me try entering that again.");
			add_dialog_choice(1,"Let me see the list of special spells again.");
			add_dialog_choice(2,"Never mind. End all of this.");
			choice = run_dialog(1);
			if (choice == 1)
				set_state_continue(14);
			if (choice == 2) 
				set_state_continue(13);
			if (choice == 3)
				end();
			}
		}
	
	// The player has successfully chosen a special spell to cast. Now cast it!
	if (j == 0)
		set_state_continue(i + 20);
	else {
		func_return = 1;
		set_state_continue(40);
		}
	
break;

beginstate 15; // using blessing special spells
	// Can't cast spells if dumbfounded
	if (get_char_status(who_used_custom_item(),13) > 0) {
		print_str("You can't cast complex spells while dumbfounded.");
		end();
		}
	
	// Can't cast these if in an AM-field.
	if (is_field_on_space(char_loc_x(who_used_custom_item()),char_loc_y(who_used_custom_item()),2)) {
		print_str("You can't cast spells in an anti-magic field.");
		end();
		}
	
	// Ask which spell to cast. Also list which spells are available.
	reset_dialog();
	add_dialog_str(0,"Which special spell would you like to cast? (Hit _OK_ and type in the full spell name, the spell initials, or the spell number.)",0);
	clear_buffer();
	append_string("Spells available to this character: ");
	// k stores whether this special spell is the first in the list
	k = 0;
	if ((get_flag(293,1) > 0) && (get_stat(who_used_custom_item(),12) >= 21)) {
		k = 1;
		append_string("1 Sanctuary");
		}
	if ((get_flag(293,2) > 0) && (get_stat(who_used_custom_item(),12) >= 22)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("2 Protection");
		}
	if ((get_flag(293,3) > 0) && (get_stat(who_used_custom_item(),11) >= 10)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("3 Major Haste");
		}
	if ((get_flag(293,4) > 0) && (get_stat(who_used_custom_item(),12) >= 15)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("4 Safe Travel");
		}
	if ((get_flag(293,5) > 0) && (get_stat(who_used_custom_item(),12) >= 22)) {
		if (k != 0)
			append_string(", ");
		k = 1;
		append_string("5 Divine Warrior");
		}
	
	append_string(".");
	get_buffer_text(dlgstr);
	add_dialog_str(1,dlgstr,0);
	add_dialog_str(2,"(If you want info on any of these spells, type _info_ in the coming box or _cancel_ if you've changed your mind.)",0);
	run_dialog(1);
	
	// Not coming from the info state.
	j = 0;
	
	set_state_continue(16);
break;

beginstate 16;
	
	// Now actually get the text response. Variable i stores the spell number.
	// First it must be zeroed.
	i = 0;
	get_text_response("Which spell?");
	check_text_response_match("Sanctuary");
	if (got_text_match())
		i = 1;
	check_text_response_match("SAN");
	if (got_text_match())
		i = 1;
	check_text_response_match("S");
	if (got_text_match())
		i = 1;
	check_text_response_match("1");
	if (got_text_match())
		i = 1;
	check_text_response_match("Protection");
	if (got_text_match())
		i = 2;
	check_text_response_match("PRO");
	if (got_text_match())
		i = 2;
	check_text_response_match("P");
	if (got_text_match())
		i = 2;
	check_text_response_match("2");
	if (got_text_match())
		i = 2;
	check_text_response_match("Major Haste");
	if (got_text_match())
		i = 3;
	check_text_response_match("MH");
	if (got_text_match())
		i = 3;
	check_text_response_match("M");
	if (got_text_match())
		i = 3;
	check_text_response_match("MAJ");
	if (got_text_match())
		i = 3;
	check_text_response_match("Major");
	if (got_text_match())
		i = 3;
	check_text_response_match("3");
	if (got_text_match())
		i = 3;
	check_text_response_match("Safe Travel");
	if (got_text_match())
		i = 4;
	check_text_response_match("ST");
	if (got_text_match())
		i = 4;
	check_text_response_match("Safe");
	if (got_text_match())
		i = 4;
	check_text_response_match("4");
	if (got_text_match())
		i = 4;
	check_text_response_match("Divine Warrior");
	if (got_text_match())
		i = 5;
	check_text_response_match("DW");
	if (got_text_match())
		i = 5;
	check_text_response_match("DIV");
	if (got_text_match())
		i = 5;
	check_text_response_match("Divine");
	if (got_text_match())
		i = 5;
	check_text_response_match("D");
	if (got_text_match())
		i = 5;
	check_text_response_match("5");
	if (got_text_match())
		i = 5;
	check_text_response_match("cancel");
	if (got_text_match())
		end();
	
	check_text_response_match("info");
	if (got_text_match())
		set_state_continue(49);
	
	// Whew! If none of those things matched, then inform the player.
	if (i == 0) {
		reset_dialog();
		add_dialog_str(0,"That was not a name or proper abbreviation of any special spell. What would you like to do?",0);
		add_dialog_choice(0,"Let me try entering that again.");
		add_dialog_choice(1,"Let me see the list of special spells again.");
		add_dialog_choice(2,"Never mind. End all of this.");
		choice = run_dialog(1);
		if (choice == 1)
			set_state_continue(16);
		if (choice == 2) {
			if (j == 0)
				set_state_continue(15);
			else
				set_state_continue(49);
			}
		if (choice == 3)
			end();
		}
	
	// If the player sneakily chose a spell that he does not have, inform him.
	if (j == 0) {
		if (get_flag(293,i) == 0) {
			reset_dialog();
			add_dialog_str(0,"Unfortunately, you chose a spell that you do not have. What would you like to do?",0);
			add_dialog_choice(0,"Let me try entering that again.");
			add_dialog_choice(1,"Let me see the list of special spells again.");
			add_dialog_choice(2,"Never mind. End all of this.");
				choice = run_dialog(1);
			if (choice == 1)
				set_state_continue(16);
			if (choice == 2)
				set_state_continue(15);
			if (choice == 3)
				end();
			}
		}
	
	// The player has successfully chosen a special spell to cast. Now cast it!
	if (j == 0)
		set_state_continue(i + 40);
	else {
		func_return = 1;
		set_state_continue(49);
		}

break;

beginstate 19; // Party location, because Windows doesn't give this in the menu
	change_custom_abil_uses(who_used_custom_abil(),2,1);
	
if (is_outdoor() == 0) {
	i = 0;
	while (i <= 5) {
		if (char_ok(i)) {
			clear_buffer();
			append_string("Character number ");
			append_number(i);
			append_string(" is located at (");
			append_number(char_loc_x(i));
			append_string(",");
			append_number(char_loc_y(i));
			append_string(")");
			get_buffer_text(dlgstr);
			print_str(dlgstr);
			}
		i = i + 1;
		}
	}
else {
	clear_buffer();
	append_string("You are in section ");
	append_number(current_out_section());
	append_string(".");
	get_buffer_text(dlgstr);
	print_str(dlgstr);
	}
break;

beginstate 20; // Tip of the Day manipulation
	change_custom_abil_uses(who_used_custom_abil(),3,1);
	
	reset_dialog();
	add_dialog_str(0,"Would you like to see Tip of the Day?",0);
	add_dialog_choice(0,"No. Turn off Tip of the Day.");
	add_dialog_choice(1,"Yes. Show me Tip of the Day when loading the game.");
	add_dialog_choice(2,"I'd like to see a Tip of the Day right now without changing my preferences.");
	choice = run_dialog(1); 
	if (choice < 3) {
		set_flag(290,4,choice - 1);
		if (get_flag(290,4) == 0)
			message_dialog("Tip of the Day: INACTIVE.","");
		if (get_flag(290,4) == 1)
			message_dialog("Tip of the Day: ACTIVE.","");
		}
	else {
		i = get_ran(1,1,10);
		set_state_continue(60);
		}
	
break;

beginstate 21; // casting Bind Foe
	// 10 MS, 4 SP, 2 + 3 * skill + B/5 levels of Webbed
	// effect 10, sound 7
	// Skill level is the value of the flag
	// B is INT + MAG + Magery
	if (get_stat(who_used_custom_item(),11) < 10) {
		print_str("Your Mage Spells is not high enough to cast Bind Foe.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 4) {
		print_str("You do not have enough energy to cast Bind Foe.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-4);
	put_effect_on_char(who_is_custom_item_target(),10,4,0);
	run_animation_sound(7);
	set_char_status(who_is_custom_item_target(),6,2 + (3 * get_flag(292,1)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) / 5),0,1);
	
break;

beginstate 22; // casting Poison
	// 18 MS, 3 SP, 10 + 15 * skill + B * 4 levels of Poison
	// Skill level is the value of the flag
	// B is INT + MAG + Magery
	if (get_stat(who_used_custom_item(),11) < 18) {
		print_str("Your Mage Spells is not high enough to cast Poison.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 3) {
		print_str("You do not have enough energy to cast Poison.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-3);
	put_effect_on_char(who_is_custom_item_target(),13,4,0);
	run_animation_sound(17);
	set_char_status(who_is_custom_item_target(),0,10 + (15 * get_flag(292,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) * 4),0,1);
	
break;

beginstate 23; // casting Filth
	// 18 PS, 5 SP, 20 + 50 * skill + B * 5 levels of Poison
	// 20 + 50 * skill + B * 5 levels of Acid
	// 20 + 50 * skill + B * 5 levels of Diseased
	if (get_stat(who_used_custom_item(),12) < 18) {
		print_str("Your Priest Spells is not high enough to cast Filth.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 5) {
		print_str("You do not have enough energy to cast Filth.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-5);
	put_effect_on_char(who_is_custom_item_target(),7,4,0);
	run_animation_sound(66);
	set_char_status(who_is_custom_item_target(),0,20 + (50 * get_flag(292,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) * 5),1,1);
	set_char_status(who_is_custom_item_target(),12,20 + (50 * get_flag(292,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) * 5),0,1);
	set_char_status(who_is_custom_item_target(),7,20 + (50 * get_flag(292,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) * 5),1,1);
	
break;

beginstate 24; // casting Capture Soul
	// PS 10, 15 SP
	// effect 4, sound 90
	if (get_stat(who_used_custom_item(),12) < 10) {
		print_str("Your Priest Spells is not high enough to cast Capture Soul.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 15) {
		print_str("You do not have enough energy to cast Capture Soul.");
		end();
		}
	
	// Level check:
	// (your level) + ((your PS + your Magery) / 4) + ((your CS skill) * 10)
	if (get_level(who_is_custom_item_target()) > ((get_level(who_used_custom_item()) - 10) + ((get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 4) + (10 * (get_flag(292,4))))) {
		print_str("Your power is not high enough to capture this creature.");
		end();
		}
	
	if (func_return == 0) {
		reset_dialog();
		add_dialog_str(0,"You must now enter which slot you want to save the creature into. Enter a numeral between 1 and 4 inclusive.",0);
		
		next_state = 24;
		set_state_continue(51);
		}
	
	func_return = 0;
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-15);
	set_flag(294,i,creature_type(who_is_custom_item_target()));
	put_effect_on_char(who_is_custom_item_target(),4,4,0);
	run_animation_sound(90);
	
break;

beginstate 25; // casting Recall Soul
	// PS 10, 20 SP
	// effect 10, sound 61
	if (get_stat(who_used_custom_item(),12) < 10) {
		print_str("Your Priest Spells is not high enough to cast Capture Soul.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 20) {
		print_str("You do not have enough energy to cast Capture Soul.");
		end();
		}
	
	if (func_return == 0) {
		reset_dialog();
		add_dialog_str(0,"You must now enter from which slot you want to recall a creature. Enter a numeral between 1 and 4 inclusive.",0);
		
		next_state = 25;
		set_state_continue(51);
		}
	
	// If char has RS at level 3, summoned creature stays until killed.
	// Otherwise it vanishes after a few turns.
	if (get_flag(292,5) >= 3)
		j = 0;
	else
		j = 2;
	
	func_return = 0;
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-20);
	place_monster(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),get_flag(294,i),j);
	
	// Make newly-placed monster friendly.
	i = 119;
	while (char_ok(i) == 0)
		{i = i - 1; }
	set_attitude(i,3);
	
	put_effect_on_char(i,10,4,0);
	run_animation_sound(61);
	
break;

beginstate 26; // casting Destroy Mind
	// 22 MS, 15 SP, 5 + 5 * skill + B/3 levels of Dumbfounded
	// L2 5 + 5 * skill + B/3 levels of Enfeebled
	// L3 1 + 3 * skill + B/7 level of Confused
	if (get_stat(who_used_custom_item(),11) < 22) {
		print_str("Your Mage Spells is not high enough to cast Destroy Mind.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 15) {
		print_str("You do not have enough energy to cast Destroy Mind.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-15);
	put_effect_on_char(who_is_custom_item_target(),7,4,0);
	run_animation_sound(65);
	set_char_status(who_is_custom_item_target(),13,5 + (5 * get_flag(292,6)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) / 3),0,1);
	if (get_flag(292,6) >= 2)
		set_char_status(who_is_custom_item_target(),21,5 + (5 * get_flag(292,6)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) / 3),0,1);
	if (get_flag(292,6) >= 3)
		set_char_status(who_is_custom_item_target(),18,1 + (3 * get_flag(292,6)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) / 7),0,1);
	
break;

beginstate 27; // casting Sleep Foe
	// 19 PS, 8 SP, 2 * skill + B/15 levels of Asleep
	if (get_stat(who_used_custom_item(),12) < 19) {
		print_str("Your Priest Spells is not high enough to cast Sleep Foe.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 8) {
		print_str("You do not have enough energy to cast Sleep Foe.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-8);
	put_sparkles_on_char(who_is_custom_item_target(),8,4);
	run_animation_sound(20);
	set_char_status(who_is_custom_item_target(),10,(2 * get_flag(292,7)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 15),0,1);
	
break;

beginstate 28; // casting Paralyze
	// 20 PS, 10 SP, 1 + 2 * skill + B/10 levels of Paralysis
	if (get_stat(who_used_custom_item(),12) < 20) {
		print_str("Your Priest Spells is not high enough to cast Paralyze.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 10) {
		print_str("You do not have enough energy to cast Paralyze.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-10);
	put_effect_on_char(who_is_custom_item_target(),5,4,2);
	run_animation_sound(90);
	set_char_status(who_is_custom_item_target(),11,1 + (2 * get_flag(292,8)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 10),0,1);
	
break;

beginstate 29; // casting Anti-Magic Field
	if (get_stat(who_used_custom_item(),12) < 22) {
		print_str("Your Priest Spells is not high enough to cast Anti-Magic Field.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 15) {
		print_str("You do not have enough energy to cast Anti-Magic Field.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-15);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) - 1,2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) - 1,2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) - 1,2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()),2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()),2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) + 1,2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) + 1,2);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) + 1,2);
	run_animation_sound(25);
	
break;

beginstate 30; // casting Sleep Cloud
	if (get_stat(who_used_custom_item(),12) < 20) {
		print_str("Your Priest Spells is not high enough to cast Sleep Cloud.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 10) {
		print_str("You do not have enough energy to cast Sleep Cloud.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-10);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) - 1,0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) - 1,0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) - 1,0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()),0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()),0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) + 1,0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) + 1,0);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) + 1,0);
	run_animation_sound(24);
	
break;

beginstate 31; // casting Quickfire
	if (get_stat(who_used_custom_item(),12) < 25) {
		print_str("Your Priest Spells is not high enough to cast Quickfire.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 30) {
		print_str("You do not have enough energy to cast Quickfire.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-30);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),6);
	run_animation_sound(169);
	
break;

beginstate 32; // casting Fire Field
	if (get_stat(who_used_custom_item(),11) < 20) {
		print_str("Your Mage Spells is not high enough to cast Fire Field.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 10) {
		print_str("You do not have enough energy to cast Fire Field.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-10);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) - 1,1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) - 1,1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) - 1,1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()),1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()),1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) + 1,1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) + 1,1);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) + 1,1);
	run_animation_sound(167);
	
break;

beginstate 33; // casting Ice Field
	if (get_stat(who_used_custom_item(),11) < 20) {
		print_str("Your Mage Spells is not high enough to cast Ice Field.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 10) {
		print_str("You do not have enough energy to cast Ice Field.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-10);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) - 1,4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) - 1,4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) - 1,4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()),4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()),4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) - 1,char_loc_y(who_is_custom_item_target()) + 1,4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()) + 1,4);
	put_field_on_space(char_loc_x(who_is_custom_item_target()) + 1,char_loc_y(who_is_custom_item_target()) + 1,4);
	run_animation_sound(52);
	
break;

beginstate 34; // casting Death Knell
	// MS 25, 30 SP, 5-100 + (6-150 * skill) + 4 * B unblockable to all visible
	if (get_stat(who_used_custom_item(),11) < 25) {
		print_str("Your Mage Spells is not high enough to cast Death Knell.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 30) {
		print_str("You do not have enough energy to cast Death Knell.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-30);
	i = 0;
	j = 0;
	while (i < 120) {
		if (char_ok(i))
			if (party_can_see_loc(char_loc_x(i),char_loc_y(i)))
				if (get_attitude(i) >= 10) {
					put_boom_on_char(i,3,0);
					run_animation_sound(65);
					damage_char(i,get_ran(5,1,25) + (get_ran(6,1,25) * get_flag(292,15)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) * 4),4);
					j = j + 1;
					}
		i = i + 1;
		}
	i = 0;
	
break;

beginstate 35; // casting Destruction
	// 21 MS, 13 SP, 9-163 + (13-275 * skill) + 8 * B
	if (get_stat(who_used_custom_item(),11) < 21) {
		print_str("Your Mage Spells is not high enough to cast Destruction.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 13) {
		print_str("You do not have enough energy to cast Destruction.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-13);
	put_jagged_zap(char_loc_x(who_used_custom_item()),char_loc_y(who_used_custom_item()),char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),2);
	put_boom_on_char(who_is_custom_item_target(),6,0);
	run_animation_sound(65);
	if (get_char_status(who_is_custom_item_target(),4)) {
		set_flag(296,0,get_char_status(who_is_custom_item_target(),4));
		set_char_status(who_is_custom_item_target(),4,0 - get_char_status(who_is_custom_item_target(),4),1,0);
		}
	else
		set_flag(296,0,0);
	damage_char(who_is_custom_item_target(),get_ran(9,1,17) + (get_ran(13,1,25) * get_flag(292,14)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) * 8),4);
	if (get_flag(296,0))
		set_char_status(who_is_custom_item_target(),4,get_flag(296,0),1,0);
	
break;

beginstate 36; // casting Cloudkill
	// 23 MS, 16 SP, 7-105 + (11-253 * skill) + 7 * B
	if (get_stat(who_used_custom_item(),11) < 23) {
		print_str("Your Mage Spells is not high enough to cast Cloudkill.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 16) {
		print_str("You do not have enough energy to cast Cloudkill.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-16);
	put_jagged_zap(char_loc_x(who_used_custom_item()),char_loc_y(who_used_custom_item()),char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),2);
	put_effect_on_char(who_is_custom_item_target(),5,4,2);
	run_animation_sound(25);
	i = 0;
	while (i < 120) {
		set_flag(296 + (i / 30),i % 30,get_char_status(i,4));
		set_char_status(i,4,0 - get_char_status(i,4),1,0);
		i = i + 1;
		}

	if (get_flag(292,16) == 1)
		i = 2; // radius
	else
		i = 3;
	damage_near_loc(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),get_ran(7,1,15) + (get_ran(11,1,23) * get_flag(292,16)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) * 7),i,4);
	i = 0;
	while (i < 120) {
		set_char_status(i,4,get_flag(296 + (i / 30),i % 30),1,0);
		i = i + 1;
		}
	
break;

beginstate 37; // casting Inferno
	// 18 MS, 10 SP, 8-64 + (10-80 * skill) + 3 * B
	if (get_stat(who_used_custom_item(),11) < 18) {
		print_str("Your Mage Spells is not high enough to cast Inferno.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 10) {
		print_str("You do not have enough energy to cast Inferno.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-10);

	if (get_flag(292,16) == 1)
		i = 2; // radius
	else
		i = 3;
	put_boom_on_char(who_is_custom_item_target(),1,1);
	put_boom_on_char(who_is_custom_item_target(),1,1);
	put_boom_on_char(who_is_custom_item_target(),1,1);
	run_animation_sound(51);
	damage_near_loc(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),get_ran(8,1,8) + (get_ran(10,1,8) * get_flag(292,17)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) * 3),i,1);
	
break;

beginstate 38; // casting Bonechill
	// 19 MS, 10 SP, 8-64 + (10-80 * skill) + 3 * B
	if (get_stat(who_used_custom_item(),11) < 19) {
		print_str("Your Mage Spells is not high enough to cast Bonechill.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 10) {
		print_str("You do not have enough energy to cast Bonechill.");
		end();
		}
		
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-10);

	if (get_flag(292,16) == 1)
		i = 2; // radius
	else
		i = 3;
	put_effect_on_char(who_is_custom_item_target(),0,4,2);
	run_animation_sound(52);
	damage_near_loc(char_loc_x(who_is_custom_item_target()),char_loc_y(who_is_custom_item_target()),get_ran(8,1,8) + (get_ran(10,1,8) * get_flag(292,17)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) * 3),i,5);
	
break;

beginstate 40; // getting info
	if (func_return == 0) {
		reset_dialog();
		add_dialog_str(0,"For which special spell would you like info? (Hit _OK_ and type in the full spell name, the spell initials, or the spell number.)",0);
		add_dialog_str(1,"Possible spell choices:",0);
		add_dialog_str(2,"1 Bind Foe, 2 Poison, 3 Filth, 4 Capture Soul, 5 Recall Soul, 6 Destroy Mind, 7 Sleep Foe, 8 Paralyze, 9 Anti-Magic Field, 10 Sleep Cloud, 11 Quickfire, 12 Fire Field, 13 Ice Field, 14 Death Knell, 15 Destruction, 16 Cloudkill, 17 Inferno, 18 Bonechill.",0);
		add_dialog_str(3,"You know the following spells:",0);
		clear_buffer();
	// Begin the listing madness. The list uses four variables.
	// i is a counter
	// j stores the number of special spells you know
	// k stores whether this special spell is the first in the list
	// l stores the highest special spell number that you know
	i = 1;
	j = 0;
	k = 0;
	l = 0;
	while (i <= 18) {
		if (get_flag(292,i) > 0) {
			j = j + 1;
			l = i;
			}
		i = i + 1;
		}
	if (get_flag(292,1) > 0) {
		k = 1;
		append_string("1 Bind Foe");
		}
	if (get_flag(292,2) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 2)
				append_string("and ");
			}
		k = 1;
		append_string("2 Poison");
		}
	if (get_flag(292,3) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 3)
				append_string("and ");
			}
		k = 1;
		append_string("3 Filth");
		}
	if (get_flag(292,4) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 4)
				append_string("and ");
			}
		k = 1;
		append_string("4 Capture Soul");
		}
	if (get_flag(292,5) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 5)
				append_string("and ");
			}
		k = 1;
		append_string("5 Recall Soul");
		}
	if (get_flag(292,6) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 6)
				append_string("and ");
			}
		k = 1;
		append_string("6 Destroy Mind");
		}
	if (get_flag(292,7) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 7)
				append_string("and (");
			}
		k = 1;
		append_string("7 Sleep Foe");
		}
	if (get_flag(292,8) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 8)
				append_string("and ");
			}
		k = 1;
		append_string("8 Paralyze");
		}
	if (get_flag(292,9) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 9)
				append_string("and ");
			}
		k = 1;
		append_string("9 Anti-Magic Field");
		}
	if (get_flag(292,10) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 10)
				append_string("and ");
			}
		k = 1;
		append_string("10 Sleep Cloud");
		}
	if (get_flag(292,11) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 11)
				append_string("and ");
			}
		k = 1;
		append_string("11 Quickfire");
		}
	if (get_flag(292,12) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 12)
				append_string("and ");
			}
		k = 1;
		append_string("12 Fire Field");
		}
	if (get_flag(292,13) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 13)
				append_string("and ");
			}
		k = 1;
		append_string("13 Ice Field");
		}
	if (get_flag(292,14) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 14)
				append_string("and ");
			}
		k = 1;
		append_string("14 Death Knell");
		}
	if (get_flag(292,15) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 15)
				append_string("and ");
			}
		k = 1;
		append_string("15 Destruction");
		}
	if (get_flag(292,16) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 16)
				append_string("and ");
			}
		k = 1;
		append_string("16 Cloudkill");
		}
	if (get_flag(292,17) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 17)
				append_string("and ");
			}
		k = 1;
		append_string("17 Inferno");
		}
	if (get_flag(292,18) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 18)
				append_string("and ");
			}
		k = 1;
		append_string("18 Bonechill");
		}
	
	if (j == 0)
		append_string("none");
		
	append_string(".");
	get_buffer_text(dlgstr);
	add_dialog_str(4,dlgstr,0);
		j = run_dialog(1);
		
		set_state_continue(14);
		}
	func_return = 0;
	
	// Now give the info.
	if (i == 1) {
		reset_dialog();
		add_dialog_str(0,"BIND FOE",0);
		add_dialog_str(1,"MS 10, 4 SP, 2 + 3 * skill + B/5 levels of Webbed",0);
		add_dialog_str(2,"Bind Foe covers its target in sticky webs that immobilize him for an extended period of time.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 10. Energy cost: 12.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 4 + 4 * skill + B/2 levels of Webbed on target.",0);
		add_dialog_str(5,"The engine recognizes the names BF, BIND, BIND FOE, and 1 for this spell.",0);
		run_dialog(1);
		}
	if (i == 2) {
		reset_dialog();
		add_dialog_str(0,"POISON",0);
		add_dialog_str(1,"MS 18, 3 SP, 10 + 15 * skill + B * 4 levels of Poison",0);
		add_dialog_str(2,"Poison fills the veins of its target with foul toxins, causing damage over an extended period of time.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 18. Energy cost: 3.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 10 + 15 * skill + B * 4 levels of Poison on target.",0);
		add_dialog_str(5,"The engine recognizes the names PO, POISON, and 2 for this spell. Note that it does NOT recognize P, as this could be either Poison or Paralyze.",0);
		run_dialog(1);
		}
	if (i == 3) {
		reset_dialog();
		add_dialog_str(0,"FILTH",0);
		add_dialog_str(1,"PS 18, 5 SP, 20 + 50 * skill + B * 5 levels of Poison; 20 + 50 * skill + B * 5 levels of Acid; 20 + 50 * skill + B * 5 levels of Diseased",0);
		add_dialog_str(2,"Filth entirely befouls the target, inflicting poison, acid, and disease on it, causing damage gradually.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 18. Energy cost: 5.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: 5 + 12 * skill + B * 3 levels of Poison, Acid, and Diseased on target.",0);
		add_dialog_str(5,"The engine recognizes the names F, FI, FILTH, and 3 for this spell.",0);
		run_dialog(1);
		}
	if (i == 4) {
		reset_dialog();
		add_dialog_str(0,"CAPTURE SOUL",0);
		add_dialog_str(1,"PS 10, 15 SP, store soul",0);
		add_dialog_str(2,"Capture Soul attempts to store of copy of the target. You can then make copies of this creature with the spell Recall Soul. You have four slots in your Soul Crystal to store such creatures.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 10. Energy cost: 15.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: stores the creature number for later recall. Note that the recalled creature will have its default stats and script, which may make it considerably less powerful.",0);
		add_dialog_str(5,"The engine recognizes the names CS, CA, CAP, CAPTURE, CAPTURE SOUL, and 4 for this spell.",0);
		run_dialog(1);
		}
	if (i == 5) {
		reset_dialog();
		add_dialog_str(0,"RECALL SOUL",0);
		add_dialog_str(1,"PS 10, 20 SP, summon soul",0);
		add_dialog_str(2,"Recall Soul creates a copy of one of the creatures stored via Capture Soul. At level 3, the creature stays until it dies, rather than disappearing after a few turns.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 10. Energy cost: 20.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: recalls a stored creature. At L1 and L2, the recalled creature is summoned, but at L3 and above, it is placed (i.e. normal). Note that the recalled creature will have its default stats and script, which may make it considerably less powerful.",0);
		add_dialog_str(5,"The engine recognizes the names RS, RECALL, RECALL SOUL, and 5 for this spell.",0);
		run_dialog(1);
		}
	if (i == 6) {
		reset_dialog();
		add_dialog_str(0,"DESTROY MIND",0);
		add_dialog_str(1,"22 MS, 15 SP, 5 + 5 * skill + B/3 levels of Dumbfounded; L2 5 + 5 * skill + B/3 levels of Enfeebled; L3 1 + 3 * skill + B/7 level of Confused",0);
		add_dialog_str(2,"Destroy Mind devastates the mind of the target, filling it with confusing thoughts and terror, making the target unable to cast spells.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 22. Energy cost: 20.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 20 SP, 5 + 5 * skill + B/3 levels of Dumbfounded on target. At L2, an equal amount of Enfeebled also. At L3, 1 + 3 * skill + B/7 level of Confused also.",0);
		add_dialog_str(5,"The engine recognizes the names DM, DES, DESTROY, DESTROY MIND, and 6 for this spell.",0);
		run_dialog(1);
		}
	if (i == 7) {
		reset_dialog();
		add_dialog_str(0,"SLEEP FOE",0);
		add_dialog_str(1,"19 PS, 8 SP, 2 * skill + B/15 levels of Asleep",0);
		add_dialog_str(2,"Sleep Foe makes the target very drowsy, potentially making it fall asleep.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 19. Energy cost: 12.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 2 * skill + B/15 levels of Asleep on target.",0);
		add_dialog_str(5,"The engine recognizes the names SF, SLEEP FOE, and 7 for this spell. Note that it does NOT recognize SLEEP, as this could be either Sleep Foe or Sleep Cloud.",0);
		run_dialog(1);
		}
	if (i == 8) {
		reset_dialog();
		add_dialog_str(0,"PARALYZE",0);
		add_dialog_str(1,"20 PS, 10 SP, 1 + 2 * skill + B/10 levels of Paralysis",0);
		add_dialog_str(2,"Paralyze makes the target's joints very stiff, rendering it unable to move.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 20. Energy cost: 20.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 1 + 2 * skill + B/10 levels of Paralysis on target.",0);
		add_dialog_str(5,"The engine recognizes the names P, PARA, PARALYZE, and 8 for this spell.",0);
		run_dialog(1);
		}
	if (i == 9) {
		reset_dialog();
		add_dialog_str(0,"ANTI-MAGIC FIELD",0);
		add_dialog_str(1,"PS 22, 15 SP, 3 by 3 field of antimagic",0);
		add_dialog_str(2,"Anti-Magic Field shrouds the target in a field that renders anyone in it unable to cast spells or be the target of spells.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 22. Energy cost: 20.");
		if (get_flag(292,i) > 0) 
			append_string(" You know this spell.");
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: creates a 3 by 3 field of antimagic centered on the target.",0);
		add_dialog_str(5,"The engine recognizes the names A, AM, AMF, ANTI, ANTI-MAGIC FIELD, and 9 for this spell.",0);
		run_dialog(1);
		}
	if (i == 10) {
		reset_dialog();
		add_dialog_str(0,"SLEEP CLOUD",0);
		add_dialog_str(1,"PS 20, 10 SP, 3 by 3 field of sleep cloud",0);
		add_dialog_str(2,"Sleep Cloud covers the target in a field that makes anyone in it feel sleepy.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 20. Energy cost: 15.");
		if (get_flag(292,i) > 0) 
			append_string(" You know this spell.");
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: creates a 3 by 3 field of sleep cloud centered on the target.",0);
		add_dialog_str(5,"The engine recognizes the names SC, SLEEP CLOUD, and 10 for this spell. Note that it does NOT recognize SLEEP, as this could be either Sleep Foe or Sleep Cloud.",0);
		run_dialog(1);
		}
	if (i == 11) {
		reset_dialog();
		add_dialog_str(0,"QUICKFIRE",0);
		add_dialog_str(1,"PS 25, 30 SP, 1 by 1 field of quickfire",0);
		add_dialog_str(2,"Quickfire places a single field of quickfire on the target. Quickfire spreads rapidly, however, so be very careful with this spell: it can cause terrible devastation.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 25. Energy cost: 50.");
		if (get_flag(292,i) > 0) 
			append_string(" You know this spell.");
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: creates a 1 by 1 field of quickfire centered on the target.",0);
		add_dialog_str(5,"The engine recognizes the names Q, QU, QF, QUICKFIRE, and 11 for this spell.",0);
		run_dialog(1);
		}
	if (i == 12) {
		reset_dialog();
		add_dialog_str(0,"FIRE FIELD",0);
		add_dialog_str(1,"MS 20, 10 SP, 3 by 3 field of fire wall",0);
		add_dialog_str(2,"Fire Field surrounds the target with a cloud of fire, doing damage to anyone inside who is vulnerable to flames.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 20. Energy cost: 10.");
		if (get_flag(292,i) > 0) 
			append_string(" You know this spell.");
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: creates a 3 by 3 field of fire wall centered on the target.",0);
		add_dialog_str(5,"The engine recognizes the names FF, FIRE, FIRE FIELD, and 12 for this spell.",0);
		run_dialog(1);
		}
	if (i == 13) {
		reset_dialog();
		add_dialog_str(0,"ICE FIELD",0);
		add_dialog_str(1,"MS 20, 10 SP, 3 by 3 field of ice wall",0);
		add_dialog_str(2,"Ice Field puts a super-chilled cloud of air around the target, damaging anyone nearby who is not immune to cold.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 20. Energy cost: 10.");
		if (get_flag(292,i) > 0) 
			append_string(" You know this spell.");
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: creates a 3 by 3 field of ice wall centered on the target.",0);
		add_dialog_str(5,"The engine recognizes the names IF, ICE, ICE FIELD, and 13 for this spell.",0);
		run_dialog(1);
		}
	if (i == 14) {
		reset_dialog();
		add_dialog_str(0,"DEATH KNELL",0);
		add_dialog_str(1,"MS 25, 30 SP, 9-163 + (13-275 * skill) + 8 * B unblockable to all visible",0);
		add_dialog_str(2,"The devastating Death Knell hits all visible targets with terrible damage.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 25. Energy cost: 50.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 9-163 + (13-275 * skill) + 8 * B points of unblockable damage on all visible hostile creatures.",0);
		add_dialog_str(5,"The engine recognizes the names DK, DEATH, DEATH KNELL, and 14 for this spell.",0);
		run_dialog(1);
		}
	if (i == 15) {
		reset_dialog();
		add_dialog_str(0,"DESTRUCTION",0);
		add_dialog_str(1,"21 MS, 13 SP, 5-65 + (10-210 * skill) + 6 * B unblockable",0);
		add_dialog_str(2,"Destruction hits a single target with an intense blast of magical energy.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 21. Energy cost: 30.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 5-65 + (10-210 * skill) + 6 * B points of unblockable damage on target.",0);
		add_dialog_str(5,"The engine recognizes the names D, DE, DES, DESTRUCTION, and 15 for this spell.",0);
		run_dialog(1);
		}
	if (i == 16) {
		reset_dialog();
		add_dialog_str(0,"CLOUDKILL",0);
		add_dialog_str(1,"23 MS, 16 SP, 7-105 + (11-253 * skill) + 7 * B unblockable on rad 2 (L2+: 3)",0);
		add_dialog_str(2,"Cloudkill drops a large cloud of terrible magical energy on the target, hurting it and anyone near it. At level 2 and above, the radius of the cloud extends to three spaces.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 23. Energy cost: 35.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 7-105 + (11-253 * skill) + 7 * B points of unblockable damage on 2 step radius around target. L2 and above: 3 step radius.",0);
		add_dialog_str(5,"The engine recognizes the names C, CK, CLOUDKILL, and 16 for this spell.",0);
		run_dialog(1);
		}
	if (i == 17) {
		reset_dialog();
		add_dialog_str(0,"INFERNO",0);
		add_dialog_str(1,"18 MS, 10 SP, 8-64 + (10-80 * skill) + 3 * B fire",0);
		add_dialog_str(2,"Inferno creates a large tower of fire around the target, hitting it and anyone near it. At level 2 and above, the radius of the fire extends to three spaces.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 18. Energy cost: 20.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 8-64 + (10-80 * skill) + 3 * B points of fire damage on 2 step radius around target. L2 and above: 3 step radius.",0);
		add_dialog_str(5,"The engine recognizes the names I, IN, INFERNO, and 17 for this spell.",0);
		run_dialog(1);
		}
	if (i == 18) {
		reset_dialog();
		add_dialog_str(0,"BONECHILL",0);
		add_dialog_str(1,"19 MS, 10 SP, 8-64 + (10-80 * skill) + 3 * B cold",0);
		add_dialog_str(2,"Bonechill immerses the target in a rapid blast of ultracold air, damaging it and anyone near it. At level 2 and above, the radius of the cold extends to three spaces.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 19. Energy cost: 20.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(292,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: inflicts 8-64 + (10-80 * skill) + 3 * B points of cold damage on 2 step radius around target. L2 and above: 3 step radius.",0);
		add_dialog_str(5,"The engine recognizes the names B, BC, BO, BONE, BONECHILL, and 18 for this spell.",0);
		run_dialog(1);
		}
	
	reset_dialog();
	add_dialog_str(0,"Would you like info on another spell?",0);
	add_dialog_choice(0,"No. I'm done.");
	add_dialog_choice(1,"Yes, I'd like to know about another hostile spell.");
	add_dialog_choice(2,"Yes, but now I'd like to know about a blessing spell.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	if (choice == 2)
		set_state_continue(40);
	if (choice == 3)
		set_state_continue(49);
	
break;

beginstate 41; // casting Sanctuary
	// PS 21, 8 SP, 3 + 3 * skill + B/6 levels of Sanctuary
	// L2 affects everyone
	// L3 3 + 3 * skill + B/6 levels of Regenerating
	// sparkle 2, sound 4
	if (get_stat(who_used_custom_item(),12) < 21) {
		print_str("Your Priest Spells is not high enough to cast Sanctuary.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 8) {
		print_str("You do not have enough energy to cast Sanctuary.");
		end();
		}
	
	if (get_flag(293,1) == 1) {
		i = 0;
		if (run_select_a_pc(0))
			i = get_selected_pc();
		else {
			if ((char_ok(4)) || (char_ok(5))) {
				reset_dialog();
				add_dialog_str(0,"Would you like to cast this spell on an extra party member?",0);
				add_dialog_choice(0,"No.");
				if (char_ok(4)) {
					clear_buffer();
					append_string("Cast it on ");
					append_char_name(4);
					append_string(".");
					get_buffer_text(dlgstr);
					add_dialog_choice(1,dlgstr);
					}
				if (char_ok(5)) {
					clear_buffer();
					append_string("Cast it on ");
					append_char_name(5);
					append_string(".");
					get_buffer_text(dlgstr);
					add_dialog_choice(2,dlgstr);
					}
				choice = run_dialog(1);
				if (choice == 2)
					i = 4;
				if (choice == 3)
					i = 5;
				}
			
			if (i == 0) {
				reset_dialog();
				add_dialog_str(0,"You failed to select a character on whom to cast the spell. Would you like to try again?",0);
				add_dialog_choice(0,"No.");
				add_dialog_choice(1,"Yes.");
				if (run_dialog(1) == 1)
					end();
				else
					set_state_continue(41);
				}
			}
		j = i;
		}
	else {
		i = 0;
		j = 119;
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-8);
	while (i <= j) {
		if (party_can_see_loc(char_loc_x(i),char_loc_y(i))) {
			if ((get_attitude(i) == 3) || (get_attitude(i) == 0)) {
				set_char_status(i,14,3 + (3 * get_flag(293,1)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 6),0,1);
				if (get_flag(293,1) >= 3)
					set_char_status(i,22,3 + (3 * get_flag(293,1)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 6),0,1);
				put_sparkles_on_char(i,2,4);
				}
			}
		i = i + 1;
		}
	run_animation_sound(4);
	
break;

beginstate 42; // casting Protection
	// PS 22, 12 SP, 10 + 10 * skill + B levels of Martyr's Shield
	// 10 + 10 * skill + B levels of Magic resistant
	// 10 + 10 * skill + B levels of Resistant
	// L3 1 + 2 * skill + B/10 levels of Invulnerable
	if (get_stat(who_used_custom_item(),12) < 22) {
		print_str("Your Priest Spells is not high enough to cast Protection.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 12) {
		print_str("You do not have enough energy to cast Protection.");
		end();
		}
	
	i = 0;
	if (run_select_a_pc(0))
		i = get_selected_pc();
	else {
		if ((char_ok(4)) || (char_ok(5))) {
			reset_dialog();
			add_dialog_str(0,"Would you like to cast this spell on an extra party member?",0);
			add_dialog_choice(0,"No.");
			if (char_ok(4)) {
				clear_buffer();
				append_string("Cast it on ");
				append_char_name(4);
				append_string(".");
				get_buffer_text(dlgstr);
				add_dialog_choice(1,dlgstr);
				}
			if (char_ok(5)) {
				clear_buffer();
				append_string("Cast it on ");
				append_char_name(5);
				append_string(".");
				get_buffer_text(dlgstr);
				add_dialog_choice(2,dlgstr);
				}
			choice = run_dialog(1);
			if (choice == 2)
				i = 4;
			if (choice == 3)
				i = 5;
			}
		
		if (i == 0) {
			reset_dialog();
			add_dialog_str(0,"You failed to select a character on whom to cast the spell. Would you like to try again?",0);
			add_dialog_choice(0,"No.");
			add_dialog_choice(1,"Yes.");
			if (run_dialog(1) == 1)
				end();
			else
				set_state_continue(42);
			}
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-12);
	set_char_status(i,15,10 + (10 * get_flag(293,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25))),0,1);
	set_char_status(i,5,10 + (10 * get_flag(293,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25))),0,1);
	set_char_status(i,17,10 + (10 * get_flag(293,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25))),0,1);
	if (get_flag(293,2) >= 3)
		set_char_status(i,4,1 + (2 * get_flag(293,2)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 10),0,1);
	put_effect_on_char(i,7,4,0);
	run_animation_sound(65);
	
break;

beginstate 43; // casting Major Haste
	// MS 10, 6 SP, 2 + 1-3 * skill + B/4 levels of Haste
	if (get_stat(who_used_custom_item(),11) < 10) {
		print_str("Your Mage Spells is not high enough to cast Major Haste.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 6) {
		print_str("You do not have enough energy to cast Major Haste.");
		end();
		}
	
	if (is_outdoor()) {
		print_str("You cannot cast Major Haste outdoors.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-6);
	
	i = 0;
	while (i <= 119) {
		if (char_ok(i))
			if (party_can_see_loc(char_loc_x(i),char_loc_y(i)))
				if ((get_attitude(i) == 3) || (get_attitude(i) == 0)) {
					set_char_status(i,3,2 + (get_ran(1,1,3) * get_flag(293,3)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),11) + get_stat(who_used_custom_item(),25)) / 4),0,1);
					put_effect_on_char(i,10,4,2);
					}
		i = i + 1;
		}
	run_animation_sound(12);
	
break;

beginstate 44; // casting Safe Travel
	// PS 15, 10 SP, L1 30 + 50 * skill + B levels of Safe Travel
	// L3 30 + 50 * skill + B levels of Hovering Feet
	if (get_stat(who_used_custom_item(),12) < 15) {
		print_str("Your Priest Spells is not high enough to cast Safe Travel.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 20) {
		print_str("You do not have enough energy to cast Safe Travel.");
		end();
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-10);
	if (get_flag(293,4) < 3)
		set_char_status(1000,26,30 + (50 * get_flag(293,4)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25))),0,1);
	else
		set_char_status(1000,27,30 + (50 * get_flag(293,4)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25))),0,1);
	put_effect_on_char(0,10,4,0);
	put_effect_on_char(1,10,4,0);
	put_effect_on_char(2,10,4,0);
	put_effect_on_char(3,10,4,0);
	put_effect_on_char(4,10,4,0);
	put_effect_on_char(5,10,4,0);
	run_animation_sound(24);
	// sound 24
	
break;

beginstate 45; // casting Divine Warrior
	// PS 22, 20 SP, 4 + 4 * skill + B/5 levels of Divinely Touched
	// L2 4 + 4 * skill + B/5 levels of Magic Resistant
	// L3 1 + 2 * skill + B/10 levels of Invulnerable
	// effect 10, sound 60
	if (get_stat(who_used_custom_item(),12) < 22) {
		print_str("Your Priest Spells is not high enough to cast Divine Warrior.");
		end();
		}
	
	if (get_energy(who_used_custom_item()) < 20) {
		print_str("You do not have enough energy to cast Divine Warrior.");
		end();
		}
	
	if ((char_ok(4)) || (char_ok(5)))
		if (get_flag(290,6) == 0) {
			reset_dialog();
			clear_buffer();
			append_string("To cast a blessing spell on ");
			if ((char_ok(4)) && (char_ok(5) == 0))
				append_char_name(4);
			if ((char_ok(4)) && (char_ok(5))) {
				append_char_name(4);
				append_string(" or ");
				append_char_name(5);
				}
			if ((char_ok(4) == 0) && (char_ok(5)))
				append_char_name(5);
			append_string(", you must hit _cancel_ and then choose from the option that subsequently appears.");
			get_buffer_text(dlgstr);
			add_dialog_str(0,dlgstr,0);
			add_dialog_choice(0,"Ok.");
			add_dialog_choice(1,"Don't show this message again.");
			if (run_dialog(1) == 2)
				set_flag(290,6,1);
			}
	
	i = 0;
	if (run_select_a_pc(0))
		i = get_selected_pc();
	else {
		if ((char_ok(4)) || (char_ok(5))) {
			reset_dialog();
			add_dialog_str(0,"Would you like to cast this spell on an extra party member?",0);
			add_dialog_choice(0,"No.");
			if (char_ok(4)) {
				clear_buffer();
				append_string("Cast it on ");
				append_char_name(4);
				append_string(".");
				get_buffer_text(dlgstr);
				add_dialog_choice(1,dlgstr);
				}
			if (char_ok(5)) {
				clear_buffer();
				append_string("Cast it on ");
				append_char_name(5);
				append_string(".");
				get_buffer_text(dlgstr);
				add_dialog_choice(2,dlgstr);
				}
			choice = run_dialog(1);
			if (choice == 2)
				i = 4;
			if (choice == 3)
				i = 5;
			}
		
		if (i == 0) {
			reset_dialog();
			add_dialog_str(0,"You failed to select a character on whom to cast the spell. Would you like to try again?",0);
			add_dialog_choice(0,"No.");
			add_dialog_choice(1,"Yes.");
			if (run_dialog(1) == 1)
				end();
			else
				set_state_continue(45);
			}
		}
	
	deduct_ap(5);
	change_char_energy(who_used_custom_item(),-20);
	set_char_status(i,16,4 + (4 * get_flag(293,5)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 5),0,1);
	if (get_flag(293,5) >= 2)
		set_char_status(i,5,4 + (4 * get_flag(293,5)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 5),0,1);
	if (get_flag(293,5) >= 3)
		set_char_status(i,4,1 + (2 * get_flag(293,5)) + ((get_stat(who_used_custom_item(),2) + get_stat(who_used_custom_item(),12) + get_stat(who_used_custom_item(),25)) / 10),0,1);
	put_effect_on_char(i,10,4,2);
	run_animation_sound(60);
	
break;

beginstate 49; // getting info on blessing spells
	if (func_return == 0) {
		reset_dialog();
		add_dialog_str(0,"For which special spell would you like info? (Hit _OK_ and type in the full spell name, the spell initials, or the spell number.)",0);
		add_dialog_str(1,"Possible spell choices: 1 Sanctuary, 2 Protection, 3 Major Haste, 4 Safe Travel, 5 Divine Warrior.",0);clear_buffer();
	append_string("You know these spells: ");
	// Begin the listing madness. The list uses four variables.
	// i is a counter
	// j stores the number of special spells you know
	// k stores whether this special spell is the first in the list
	// l stores the highest special spell number that you know
	i = 1;
	j = 0;
	k = 0;
	l = 0;
	while (i <= 5) {
		if (get_flag(293,i) > 0) {
			j = j + 1;
			l = i;
			}
		i = i + 1;
		}
	if (get_flag(293,1) > 0) {
		k = 1;
		append_string("1 Sanctuary");
		}
	if (get_flag(293,2) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 2)
				append_string("and ");
			}
		k = 1;
		append_string("2 Protection");
		}
	if (get_flag(293,3) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 3)
				append_string("and ");
			}
		k = 1;
		append_string("3 Major Haste");
		}
	if (get_flag(293,4) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 4)
				append_string("and ");
			}
		k = 1;
		append_string("4 Safe Travel");
		}
	if (get_flag(293,5) > 0) {
		if (k != 0) {
			if (j != 2)
				append_string(",");
			append_string(" ");
			if (l == 5)
				append_string("and ");
			}
		k = 1;
		append_string("5 Divine Warrior");
		}
	
	if (j == 0)
		append_string("none");
	
	append_string(".");
	get_buffer_text(dlgstr);
	add_dialog_str(2,dlgstr,0);
		j = run_dialog(1);
		
		set_state_continue(16);
		}
	func_return = 0;
	
	// Now give the info.
	if (i == 1) {
		reset_dialog();
		add_dialog_str(0,"SANCTUARY",0);
		add_dialog_str(1,"PS 21, 8 SP, 3 + 3 * skill + B/6 levels of Sanctuary; L2 affects everyone; L3 3 + 3 * skill + B/6 levels of Regenerating",0);
		add_dialog_str(2,"Hides a member of the party, who will be hit in melee by enemies much less. Attacking something voids your Sanctuary. At level 2, the spell hides the whole party, and at level 3, it also provides gradual healing.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 21. Energy cost: 15.");
		if (get_flag(292,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(293,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: gives 3 + 3 * skill + B/6 levels of Sanctuary to target.",0);
		add_dialog_str(5,"The engine recognizes the names S, SAN, SANCTUARY, and 1 for this spell.",0);
		run_dialog(1);
		}
	if (i == 2) {
		reset_dialog();
		add_dialog_str(0,"PROTECTION",0);
		add_dialog_str(1,"PS 22, 12 SP, 10 + 10 * skill + B levels of Martyr's Shield; 10 + 10 * skill + B levels of Magic Resistant; 10 + 10 * skill + B levels of Resistant; L3 1 + 2 * skill + B/10 levels of Invulnerable",0);
		add_dialog_str(2,"Protection provides strong and wide-ranging resistance to melee, magic, and elemental attacks. At level 3, it also grants brief invulnerability.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 22. Energy cost: 20.");
		if (get_flag(293,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(293,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: grants 10 + 10 * skill + B levels of Martyr's Shield, Magic Resistant, and Resistant to the target. At L3, also grants 1 + 2 * skill + B/10 levels of Invulnerable to the target.",0);
		add_dialog_str(5,"The engine recognizes the names P, PRO, PROTECTION, and 2 for this spell.",0);
		run_dialog(1);
		}
	if (i == 3) {
		reset_dialog();
		add_dialog_str(0,"MAJOR HASTE",0);
		add_dialog_str(1,"MS 10, 6 SP, 2 + 1-3 * skill + B/4 levels of Haste to all visible allies",0);
		add_dialog_str(2,"Major Haste doubles for a short time the number of action points available in combat for all friendly characters visible to the party.",0);
		clear_buffer();
		append_string("Minimum Mage Spells to cast: 10. Energy cost: 6.");
		if (get_flag(293,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(293,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: confers 2 + 1-3 * skill + B/4 levels of Haste to all visible allies.",0);
		add_dialog_str(5,"The engine recognizes the names M, MH, MAJ, MAJOR, MAJOR HASTE, and 3 for this spell.",0);
		run_dialog(1);
		}
	if (i == 4) {
		reset_dialog();
		add_dialog_str(0,"SAFE TRAVEL",0);
		add_dialog_str(1,"PS 10, 20 SP, L1/L2 only 30 + 50 * skill + B levels of Safe Travel; L3 30 + 50 * skill + B levels of Hovering Feet",0);
		add_dialog_str(2,"Safe Travel enables your group to walk over swamps without harm. At level 3, it also protects from lava.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 15. Energy cost: 20.");
		if (get_flag(293,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(293,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: at L1 and L2 only, provides 30 + 50 * skill + B levels of Safe Travel to the whole party. At L3 and above, provides instead 30 + 50 * skill + B levels of Hovering Feet to the whole party.",0);
		add_dialog_str(5,"The engine recognizes the names ST, SAFE, SAFE TRAVEL, and 4 for this spell. (Note that S is Sanctuary, though.)",0);
		run_dialog(1);
		}
	if (i == 5) {
		reset_dialog();
		add_dialog_str(0,"DIVINE WARRIOR",0);
		add_dialog_str(1,"PS 22, 20 SP, 4 + 4 * skill + B/5 levels of Divinely Touched; L2 4 + 4 * skill + B/5 levels of Magic Resistant; L3 1 + 2 * skill + B/10 levels of Invulnerable",0);
		add_dialog_str(2,"Divine Warrior calls down the protection and blessing of the gods, which makes the target a mighty warrior for a short time, better in every way. At level 2, it grants partial magic resistance, and at level 3, it grants brief invulnerability.",0);
		clear_buffer();
		append_string("Minimum Priest Spells to cast: 22. Energy cost: 25.");
		if (get_flag(293,i) > 0) {
			append_string(" You know this spell at level ");
			append_number(get_flag(293,i));
			append_string(".");
			}
		else
			append_string(" You do not know this spell.");
		get_buffer_text(dlgstr);
		add_dialog_str(3,dlgstr,0);
		add_dialog_str(4,"Effect: grants 4 + 4 * skill + B/5 levels of Divinely Touched to the target. At L2, also provides an equal level of Magic Resistant. At L3, also provides 1 + 2 * skill + B/10 levels of Invulnerable.",0);
		add_dialog_str(5,"The engine recognizes the names D, DW, DIV, DIVINE, DIVINE WARRIOR, and 5 for this spell.",0);
		run_dialog(1);
		}
	
	reset_dialog();
	add_dialog_str(0,"Would you like info on another spell?",0);
	add_dialog_choice(0,"No. I'm done.");
	add_dialog_choice(1,"Yes, but now I'd like to know about a hostile spell.");
	add_dialog_choice(2,"Yes, I'd like to know about another blessing spell.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	if (choice == 2)
		set_state_continue(40);
	if (choice == 3)
		set_state_continue(49);
		
break;

beginstate 50; // numeric input for Capture Soul and Recall Soul
	i = 0;
	get_text_response("Which slot?");
	check_text_response_match("1");
	if (got_text_match())
		i = 1;
	check_text_response_match("2");
	if (got_text_match())
		i = 2;
	check_text_response_match("3");
	if (got_text_match())
		i = 3;
	check_text_response_match("4");
	if (got_text_match())
		i = 4;
	
	if (i == 0) {
		reset_dialog();
		add_dialog_str(0,"Unfortunately, that was not a numeral between 1 and 4 inclusive. Remember, enter a number: 1, 2, 3, or 4.",0);
		add_dialog_str(1,"What do you want to do now?",0);
		add_dialog_choice(0,"Try again.");
		add_dialog_choice(1,"Cancel all of this.");
		if (run_dialog(1) == 1)
			set_state_continue(50);
		else
			end();
		}
	
	func_return = 1;
	set_state_continue(next_state);
		
break;

beginstate 51; // state to set up the dialogs for CS and RS
	// This state was supposed to do more than it does now.
	//
	// Specifically, I made a list of all the monsters in the scenario, and I
	// used it to display the name of the stored monster in each of the dialog
	// strings. However, I ran up against the 750-strings-in-a-script limit, so
	// I had to delete it.
	//
	// Damn it.
	
i = 1;
while (i <= 4) {
	clear_buffer();
	append_string("Slot ");
	append_number(i);
	append_string(": ");
	
	if (get_flag(294,i) == 0)
		append_string("Empty");
	else
		append_string("Filled");
		
	append_string(".");
	get_buffer_text(dlgstr);
	add_dialog_str(i,dlgstr,0);
	i = i + 1;
	}
	
	run_dialog(1);
	set_state_continue(50);
	
break;

beginstate 60; // Tip of the Day! Begun in LOAD_SCEN_STATE or state 20.
	
	// Now display the tip.
	reset_dialog();
	add_dialog_str(0,"TIP OF THE DAY",0);
	if (i == 1)
		add_dialog_str(1,"Read the readme! It has vital information about playing this scenario! You can find it in the scenario folder. It is the file named READ ME.txt.",0);
	if (i == 2)
		add_dialog_str(1,"If you're stuck somewhere, check out the hints and walkthrough in the scenario folder. They are in the file named HINTS.txt. If you need more help, ask via the Spiderweb message boards or e-mail me.",0);
	if (i == 3)
		add_dialog_str(1,"Check out your custom special abilities! They are important and sometimes very useful. View them by pressing _u_ or hitting the Special Ability button.",0);
	if (i == 4)
		add_dialog_str(1,"In order to use some of the special spells, you will need to train your Mage Spells or Priest Spells skills above 17, which is the highest you need for normal spells. Check the readme for details.",0);
	if (i == 5)
		add_dialog_str(1,"Unlike in some BoA scenarios, in this scenario, every skill will count for something (except First Aid, I guess). Don't neglect your Tool Use, Nature Lore, or Arcane Lore!",0);
	if (i == 6)
		add_dialog_str(1,"Most monsters in Exodus have a _distractibility_ factor: if you attack them, they might acquire the attacking character as a target instead of whoever they currently have. Useful when you want a monster to attack someone else.",0);
	if (i == 7)
		add_dialog_str(1,"There are a number of secret passages in Exodus. They are never necessary to complete the game, but they often conceal useful bonuses.",0);
	if (i == 8)
		add_dialog_str(1,"Doors work a little differently in this scenario. Unlock Doors works on non-magical doors. Also, some doors are magically locked and cannot be unlocked without Unlock Doors or a magical lockpick. Lockpicking has been slightly randomized, too.",0);
	if (i == 9)
		add_dialog_str(1,"In order to use special spells, you must create a spellbook. Do this by using your special abilities. Press _u_ or hit the Special Ability button. To use the Hostile Spellbook, you must be in combat mode.",0);
	if (i == 10)
		add_dialog_str(1,"In this scenario, you're likely to encounter a BoA error: a file corruption caused a too-high skill that is being manually reset to 0. Then, on your stats sheet, your health will appear as 0. I can't do anything about this, but it has no in-game effect.",0);
	add_dialog_str(2,"(You may deactivate Tip of the Day via your special abilities.)",0);
	add_dialog_choice(1,"See next Tip of the Day.");
	if (run_dialog(1) == 2) {
		i = i + 1;
		if (i == 11)
			i = 1;
		set_state_continue(60);
		}
break;

beginstate 70;
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
if (in_boat() >= 0) {
	print_str_color("You cannot fly while in a boat.",1);
	end();
	}
		
if (party_has_status(25) == 0) {
	set_char_status(1000,25,6,1,0);
	print_str_color("You lift up into the air!",2);
	set_flag(290,5,1);
	}
else
	print_str_color("You must land first.",1);
break;