// TOWN SCRIPT
//    Town 11: Fresan

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

begintownscript;

variables;
short a,b,c,d,e,f,g,h,i,j,k,r1,choice;
body;

beginstate INIT_STATE;
	set_name(6,"Daeghrefn");
	set_name(7,"Anfal");			
	set_name(8,"Weder");		
	set_name(9,"Hemming");
	set_name(10,"Beanstan");			
	set_name(11,"Eadgils");			
	set_name(12,"Ecgwela");		
	set_name(13,"Fitela");	
	set_state_continue(22);
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
// This state is called every turn the party is in this town.
break;

beginstate 10;
	if (get_terrain(38,6) == 234){
	set_terrain(38,6,0);
	message_dialog("The bookshelf slides aside as you push it.","");
	}
break;

beginstate 12;
	block_entry(1);
	print_str("Blocked.");
break;

beginstate 14;
	if (get_skill_total(42) >= 10)
		set_state_continue(16);
	set_state_continue(15);
break;

beginstate 15;
	message_dialog("You find nothing of interest.","Maybe you need to study more of Arcane Lore");
break;

beginstate 16;
	message_dialog("On one of the scrolls, you find a description of a powerful priestly ritual that deals a stunning blow to any group of enemies.","You have found Divine Retribution.");
	i = 0;
	while (i < 6) {
		if (char_ok(i)) {
			if (get_spell_level(i,1,17) <= 1)
					change_spell_level(i,1,17,1);
			else 
				print_named_str(i,"cannot learn any more of this spell.");
			}
		i = i + 1;
}
break;

beginstate 17;
	if (get_skill_total(42) >= 12)
		set_state_continue(18);
	set_state_continue(15);
break;

beginstate 18;
	message_dialog("On the bookshelf you find an interesting alchemical recipe for regenerating your spell energy.","");
	if (has_recipe(9) == 0){
		give_recipe(9);
		print_str_color("You learn the recipe for Energy Elixir",2);
		}
	if (has_recipe(9) == 1){
		print_str_color("You already know the recipe for Energy Elixir",2);
		}
break;

beginstate 19;
	if (get_skill_total(42) >= 16)
		set_state_continue(20);
	set_state_continue(15);
break;

beginstate 20;
	message_dialog("On one of the scrolls, you locate a spell allowing you to call forth a powerful servant from the regions of hell.","You have learned Arcane Summon.");
	i = 0;
	while (i < 6) {
		if (char_ok(i)) {
			if (get_spell_level(i,0,17) <= 1)
					change_spell_level(i,0,17,1);
			else 
				print_named_str(i,"cannot learn any more of this spell.");
			}
		i = i + 1;
}
break;

beginstate 21;
	message_dialog("You notice a curious, leather-bound book stashed away between the many records. Reading through it briefly, you recognize some of the glyphs as instructions for a potent protective incantation.","You have learned Radiant Shield.");
	i = 0;
	while (i < 6) {
		if (char_ok(i)) {
			if (get_spell_level(i,1,12) <= 2)
					change_spell_level(i,1,12,1);
			else 
				print_named_str(i,"cannot learn any more of this spell.");
			}
		i = i + 1;
}
break;

beginstate 22;
	if (get_flag(11,9) != 255) {
	run_scenario_script(52);
	set_state_continue(23);
	}
break;

beginstate 23;
	a = (256 * get_flag(11,20)) + get_flag(11,10);
	b = (256 * get_flag(11,21)) + get_flag(11,11);
	c = (256 * get_flag(11,22)) + get_flag(11,12);
	d = (256 * get_flag(11,23)) + get_flag(11,13);
	e = (256 * get_flag(11,24)) + get_flag(11,14);
	f = (256 * get_flag(11,25)) + get_flag(11,15);
	g = (256 * get_flag(11,26)) + get_flag(11,16);
	h = (256 * get_flag(11,27)) + get_flag(11,17);
	i = (256 * get_flag(11,28)) + get_flag(11,18);
	j = (256 * get_flag(11,29)) + get_flag(11,19);

	//Fresan, Beanstan's Items
	add_item_to_shop(26,a,1);
	add_item_to_shop(26,b,1);
	add_item_to_shop(26,c,1);
	add_item_to_shop(26,d,1);
	add_item_to_shop(26,e,1);
	add_item_to_shop(26,f,1);
	add_item_to_shop(26,g,1);
	add_item_to_shop(26,h,1);
	add_item_to_shop(26,i,1);
	add_item_to_shop(26,j,1);

	set_flag(11,9,255);
break;

