// Town 55: Castle L2
begintownscript;

variables;

short choice,i,j,k,warn;
string dlgstr;

body;

beginstate INIT_STATE;
	set_crime_tolerance(2);
	
	change_blocked(14,25,1);
	change_blocked(15,25,1);
	change_blocked(16,25,1);
	change_blocked(17,25,1);
	change_blocked(18,25,1);
	change_blocked(14,24,1);
	change_blocked(15,24,1);
	change_blocked(16,24,1);
	change_blocked(17,24,1);
	change_blocked(18,24,1);
	change_blocked(14,23,1);
	change_blocked(15,23,1);
	change_blocked(16,23,1);
	change_blocked(17,23,1);
	change_blocked(18,23,1);
	
	change_blocked(23,43,1);
	change_blocked(23,44,1);
	change_blocked(23,45,1);
	change_blocked(24,43,1);
	change_blocked(24,44,1);
	change_blocked(24,45,1);
	change_blocked(25,43,1);
	change_blocked(25,44,1);
	change_blocked(25,45,1);
	
	change_blocked(18,43,1);
	
	change_blocked(36,42,1);
	change_blocked(36,43,1);
	change_blocked(36,44,1);
	
	change_blocked(38,21,1);
	change_blocked(39,21,1);
	change_blocked(40,21,1);
	
	change_blocked(21,9,1);
	change_blocked(21,10,1);
	change_blocked(21,11,1);
	change_blocked(21,12,1);
	
	change_blocked(8,36,1);
	change_blocked(8,37,1);
	change_blocked(8,38,1);
	
	char_give_item(9,484); // All-Gloves
	add_range_to_group(19,24,1);
	char_give_item(19,231); // Energy Elixir
	char_give_item(20,231); // Energy Elixir
	char_give_item(21,231); // Energy Elixir
	
	if (get_flag(55,0) == 0) {
		force_instant_terrain_redraw();
		message_dialog("You ascend the stairs and find yourself in the winding halls of the upper level of the castle of Nathaganth, the Lord of All Evil.","The halls are dark and cold, a stark juxtaposition against the oppressive heat of the lower level.");
		set_flag(55,0,1);
		}
	
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
if ((get_flag(55,3) > 0) && (get_flag(55,3) <= 3)) {
	inc_flag(55,4,1);
	
	if (get_flag(55,4) > 35)
		if (warn) {
			message_dialog("You hear bootsteps approaching very rapidly. You'd better get a move on, or else you'll have to fight the full array of Nathaganth's guards, which would probably be fatal.","");
			warn = 0;
			}
	
	if (get_flag(55,4) > 50) {
		message_dialog("The guards of Nathaganth's castle finally catch up to you. You are met by a host of magic-users, spearmen, swordsmen, demons, and vengeful spirits, led by the dark god of battle, Rashank the Mighty himself!","Your death is slow and painful.");
		kill_char(1000,2,0);
		}
	}
	
if (is_combat() == 0)
	end();
if (character_in_party(105) < 0)
	end();
	
	if (get_ran(1,1,3) == 1) {
		i = get_ran(1,1,7);
		if (i == 1) {
			heal_char(1000,get_ran(10,10,30));
			put_effect_on_char(0,4,1,2);
			put_effect_on_char(1,4,1,2);
			put_effect_on_char(2,4,1,2);
			put_effect_on_char(3,4,1,2);
			put_effect_on_char(4,4,1,2);
			print_str_color("Phaedra heals the party!",2);
			run_animation_sound(24);
			}
		j = get_ran(1,0,3);
		if (i == 2) {
			heal_char(j,get_ran(10,30,50));
			put_effect_on_char(j,4,1,2);
			clear_buffer();
			append_string("Phaedra heals ");
			append_char_name(j);
			append_string("!");
			get_buffer_text(dlgstr);
			print_str_color(dlgstr,2);
			run_animation_sound(24);
			}
		if (i >= 3) {
			clear_buffer();
			append_string("Phaedra blesses ");
			append_char_name(j);
			append_string("!");
			get_buffer_text(dlgstr);
			print_str_color(dlgstr,2);
			}
		if (i == 3) {
			set_char_status(j,5,20,0,1); // Magic Resistant
			put_effect_on_char(j,4,1,2);
			run_animation_sound(4);
			}
		if (i == 4) {
			set_char_status(j,4,5,0,1); // Invulnerable
			put_effect_on_char(j,8,1,2);
			run_animation_sound(25);
			}
		if (i == 5) {
			set_char_status(j,16,10,0,1); // Divinely Touched
			put_effect_on_char(j,7,1,2);
			run_animation_sound(60);
			}
		if (i == 6) {
			set_char_status(j,17,20,0,1); // Resistant
			put_effect_on_char(j,10,1,2);
			run_animation_sound(166);
			}
		if (i == 7) {
			set_char_status(j,22,20,0,1); // Regenerating
			put_effect_on_char(j,5,1,2);
			run_animation_sound(24);
			}
		}
break;

beginstate 10;
	reset_dialog_preset_options(5);
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(56,17,27);
break;

beginstate 11;
	reset_dialog_preset_options(5);
	if (get_flag(55,3) > 0)
		add_dialog_str(1,"You can hear loud shouting down this way. If you go down, you will encounter a very large force of angry residents.",0);
	add_dialog_choice(1,"Descend the stairs.");
	if (run_dialog(1) == 2) {
		if (get_flag(55,3) == 0)
			move_to_new_town(54,39,8);
		else {
			message_dialog("Halfway down the stairs, you are met by a host of magic-users, spearmen, swordsmen, demons, and vengeful spirits, led by the dark god of battle, Rashank the Mighty himself!","Your death is slow and painful.");
			kill_char(1000,2,0);
			}
		}
	else
		block_entry(1);
break;

beginstate 12;
	reset_dialog();
	add_dialog_str(0,"On the other side of this door, you can see Legare! What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Enter and free him.");
	if (run_dialog(1) == 1) {
		block_entry(1);
		end();
		}
	
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	
	text_bubble_on_char(i,"Legare!");
	march_party(32,19);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);
		
	march_party(33,19);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);
		
	march_party(34,18);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 3);
	
	text_bubble_on_char(i,"");
	if (party_size() == 1)
		text_bubble_on_char(i,"I've come to rescue you!");
	else
		text_bubble_on_char(i,"We've come to rescue you!");
	
	text_bubble_on_char(i,"");
	force_instant_terrain_redraw();
	
	if (character_in_party(105))
		message_dialog("You quickly unchain him. _How can we leave this place?_ he asks.","_That's where I come in,_ Phaedra says, stepping forward. She pulls out a small, glowing magic ball, and she recites a short chant. The strange, magical language fills the room with energy. Then, suddenly, you transport!");
	else
		message_dialog("You quickly unchain him. _How can we leave this place?_ he asks. You consider for a moment, and then you remember Phaedra's words.","You pull out the small, glowing magic ball that you got from Phaedra, and you read a short chant on the piece of paper she gave you. The strange, magical language fills the room with energy. Then, suddenly, you transport!");
	
	put_boom_on_char(0,2,0);
	put_boom_on_char(1,2,0);
	put_boom_on_char(2,2,0);
	put_boom_on_char(3,2,0);
	put_boom_on_char(4,2,0);
	put_boom_on_char(6,2,0);
	run_animation_sound(10);
	
	march_party(1,1);
	march_party(2,1);
	march_party(3,1);
	march_party(4,1);
	march_party(5,1);
	relocate_character(6,6,1);
	force_instant_terrain_redraw();
	pause(get_flag(290,0) * 1);
	
	set_flag(51,1,1);
	move_to_new_town(46,19,16);
	
break;

beginstate 13;
	reset_dialog_preset_options(3);
	add_dialog_str(1,"This one appears to have the strength only to take one person. What do you do?",0);
	if (run_dialog(1) == 2)
		try_to_split_party(8,42,1);
break;

beginstate 14;
	reset_dialog_preset_options(3);
	if (run_dialog(1) == 2)
		reunite_party();
break;

beginstate 15;
if (is_combat()) {
	block_entry(1);
	print_str_color("You can't enter this portal in combat mode.",1);
	end();
	}
	
	reset_dialog_preset_options(3);
	add_dialog_str(1,"This one appears to have the strength only to take one person. What do you do?",0);
	if (run_dialog(1) == 2)
		if (try_to_split_party(18,39,1)) 
			if (get_flag(55,1) == 0) {
				message_dialog("When you step through this portal, you feel very weak. In fact, you cannot move!","Worse still, a deranged lunatic lunges at your immobile form from nowhere!");
				i = 0;
				while (char_status(i) != 1)
					{i = i + 1; }
				set_char_status(i,11,10,1,0);
				}
break;

beginstate 16;
	reset_dialog();
	add_dialog_str(0,"Your attacker with the powerful magical zap falls, fatally wounded, unable to cast his powerful death spell ever again.",0);
	add_dialog_str(1,"A strange thing happens then: his body disperses, its energies suffusing the room, and you feel yourself absorbing his power, but peculiarly altered, so that now you have the ability to cast a spell of as powerfully a blessing as his was a curse.",0);
	add_dialog_str(2,"(You learn Divine Warrior at level 3.)",0);
	set_flag(293,5,3);
	set_flag(293,0,run_dialog(1));
break;

beginstate 17;
	set_flag(55,1,1);
	message_dialog("As your would-be killer dies, you shake your head. Sometimes it's better to defeat an attacker without even raising a hand. Faith in the right gods can do that.","You feel your limbs begin to loosen up.");
	set_char_status(i,11,0 - get_char_status(i,11),1,0);
break;

beginstate 18;
	if (get_flag(55,3) < 1) {
		message_dialog("You hear a large commotion down the stairs behind you. Judging by the loud shouting, the dead guards have been found, and forces are mustering to come up and find the intruders.","You don't have a lot of time before they'll get enough people together that they think that they can handle you. You'd better get a move on.");
		set_flag(55,3,1);
		change_blocked(36,42,0);
		change_blocked(36,43,0);
		change_blocked(36,44,0);
		set_flag(55,4,0);
		warn = 1;
		}	
break;

beginstate 19;
	if (get_flag(55,3) < 2) {
		message_dialog("As you ascend these stairs, you hear the noise behind you getting a little closer. Your pursuers are on this level.","It would not do to backtrack too far at this point.");
		set_flag(55,3,2);
		change_blocked(38,21,0);
		change_blocked(39,21,0);
		change_blocked(40,21,0);
		set_flag(55,4,0);
		warn = 1;
		}	
break;

beginstate 20;
	if (get_flag(55,3) < 3) {
		message_dialog("As you continue to climb, you can hear more guards joining in pursuit. There are far too many for you to fight, if sound is any indication. You had best keep moving and not dawdle.","");
		set_flag(55,3,3);
		change_blocked(21,9,0);
		change_blocked(21,10,0);
		change_blocked(21,11,0);
		change_blocked(21,12,0);
		set_flag(55,4,0);
		warn = 1;
		}	
break;

beginstate 21;
	if (get_flag(55,3) < 4) {
		message_dialog("As you step up to the top of these stairs, you suddenly hear the guards behind you calling for a halt. They completely stop and wait quietly, but you don't hear them leave, so they must still be there.","For whatever reason, they are no longer coming after you immediately, but they're still behind you, so you can't turn around.");
		set_flag(55,3,4);
		change_blocked(8,36,0);
		change_blocked(8,37,0);
		change_blocked(8,38,0);
		}	
break;

beginstate 22;
	if (get_flag(55,3) > 1) {
		message_dialog("As you head back this direction towards the shouting guards, you are met by a host of magic-users, spearmen, swordsmen, demons, and vengeful spirits, led by the dark god of battle, Rashank the Mighty himself!","Your death is slow and painful.");
		kill_char(1000,2,0);
		}
break;

beginstate 23;
	if (get_flag(55,3) > 2) {
		message_dialog("As you head back this direction towards the shouting guards, you are met by a host of magic-users, spearmen, swordsmen, demons, and vengeful spirits, led by the dark god of battle, Rashank the Mighty himself!","Your death is slow and painful.");
		kill_char(1000,2,0);
		}
break;

beginstate 24;
	if (get_flag(55,3) > 3) {
		message_dialog("As you head back this direction towards the shouting guards, you are met by a host of magic-users, spearmen, swordsmen, demons, and vengeful spirits, led by the dark god of battle, Rashank the Mighty himself!","Your death is slow and painful.");
		kill_char(1000,2,0);
		}
break;

beginstate 25;
	message_dialog("When this monstrous woman finally dies, you notice on her body a very nice pair of gloves. They look magical.","Now that you have a moment to survey the room, you glance at the books on the shelves, but none of them look immediately useful to you, and you don't have time to read them in detail.");
break;

beginstate 26;
	if (get_flag(55,5) == 0) {
		message_dialog("Back this way, you can hear several vahnatai arguing loudly about something.","");
		set_flag(55,5,1);
		}
break;

beginstate 27;
if (get_flag(55,2) == 1)
	end();
	
	reset_dialog();
	add_dialog_str(0,"This rune radiates magical energy. It is primed to do something. What do you wish to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Step on the rune and hope for the best.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else {
		reset_dialog();
		add_dialog_str(0,"When you step on this rune, you feel magical energy flowing through you, and a soft voice whispers the secrets of the most powerfully destructive spell into your ear.",0);
		if (get_flag(292,15) == 0)
			add_dialog_str(1,"Unfortunately, you are completely unfamiliar with the spell, Death Knell, and you can't understand what it describes. Sighing, you back away.",0);
		else {
			add_dialog_str(1,"It is describing the most efficient way to cast Death Knell! You pay careful attention, and at the end, you understand the spell in great detail.",0);
			set_flag(55,2,1);
			set_flag(292,14,3);
			}
		run_dialog(1);
		}
			
break;