begintownscript;

variables;
short bmessage,gone,pcs,speaker_pcs,twondx,others_ok;
string custom_m,custom_m2,custom_m3;

body;

beginstate INIT_STATE;

	if(get_flag(99,4) == 1 && get_flag(26,0) < 23){
		if(get_flag(99,3) == 0)
			split_off_one_char(24,4,0,0);
		if(get_flag(99,3) == 1)
			split_off_one_char(43,24,0,0);
		if(get_flag(99,3) == 2)
			split_off_one_char(4,24,0,0);
		if(get_flag(99,3) == 3)
			split_off_one_char(24,43,0,0);
		add_char_to_party(7);
		if(get_flag(99,3) == 0){
			march_party(24,5);
			force_view_center(24,5);
			set_character_facing(4,4);
		}
		if(get_flag(99,3) == 1){
			march_party(42,24);
			force_view_center(42,24);
			set_character_facing(4,2);
		}
		if(get_flag(99,3) == 2){
			march_party(5,24);
			force_view_center(5,24);
			set_character_facing(4,6);
		}
		if(get_flag(99,3) == 3){
			march_party(24,42);
			force_view_center(23,42);
			set_character_facing(4,0);
		}
		force_instant_terrain_redraw();
		block_entry(1);
	}
	else{
		erase_char(7);
	}


	if(get_flag(34,0) == 0){
		reset_dialog();
		add_dialog_str(0,"This city has clearly seen better days.",0);
		add_dialog_str(1,"The plan for Ivalice likely was for it to have been a boom town, thriving on ore and lumber.",0);
		add_dialog_str(2,"Alas, it has since fallen on considerably worse times.  The apartments that were already completed still stand, and the areas where more were planned are visible.",0);
		add_dialog_str(3,"Bodies are strewn about the entrance, mostly but not entirely decomposed.  Blood from the battle is still scattered about.  It's a grisly scene.",0);
		if(get_flag(99,0) == 1)
			add_dialog_str(4,"The nephils need to be stopped, so that this sort of tragedy does not ever repeat itself.",0);
		if(get_flag(99,0) == 2)
			add_dialog_str(4,"This city will be avenged with the blood of cats!",0);
		bmessage = run_dialog(1);
		set_flag(34,0,1);
	}

	if(get_flag(26,0) == 15){
		reset_dialog();
		add_dialog_str(0,"Dervish Malachai, now well and truly exhausted from blood loss, is beginning to slow down.",0);
		clear_buffer();
		append_string("_");
		append_char_name(0);
		append_string("...  The administrative building in the northwest corner of the city...  It has a bed.  Let's go there, then we can discuss things more leisurely._");
		get_buffer_text(custom_m);
		add_dialog_str(1,custom_m,0);
		add_dialog_str(2,"_And really...  Thanks._",0);
		if(get_flag(99,0) == 1)
			add_dialog_choice(0,"Don't mention it.");
		if(get_flag(99,0) == 2)
			add_dialog_choice(0,"You're my superior; think nothing of it.");
		bmessage = run_dialog(1);

		set_flag(26,0,16);
	}

	if(get_flag(26,0) == 23){
		erase_char(6);
		split_off_one_char(14,13,0,0);
		relocate_character(0,14,13);
		force_view_center(14,13);
		force_instant_terrain_redraw();
		pause(6);

		reset_dialog();
		add_dialog_str(0,"Malachai!  Oh no, he's gone!  Has he recovered?  Furthermore, how much time has passed since you were asleep?  Why didn't he wake you?  He even went through the trouble of putting you back in bed...",0);
		add_dialog_str(1,"Calm down...  Maybe he's outside making breakfast or keeping guard.  Maybe he's okay.  Don't jump to conclusions.",0);
		bmessage = run_dialog(1);
		block_entry(1);
		set_flag(26,0,24);
	}

break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
break;

beginstate 10;

	if(get_flag(26,0) == 16){
		message_dialog("_This was supposed to be the Mayor's office, and also his house.  Through that next threshold is a meeting hall.  The mayor's bedroom is the first door to your right as you're walking in._","");
		set_flag(26,0,17);
	}

break;

beginstate 11;

	if(get_flag(26,0) == 17){
		gone = remove_char_from_party(601);
		gone = remove_char_from_party(403);
		message_dialog("As you enter the room, Malachai walks over to the bed and falls on it haphazardly, not even bothering to take off his armor.  _Nghh...  That feels good._  He lies prone, letting his caked blood fall out of his armor and onto the bed.","_Come...  Have a seat.  Now that we have a respite, let us discuss._");
		set_flag(26,0,18);
		activate_hidden_group(1);
		relocate_character(6,14,13);
		set_character_pose(6,14);
	}

break;

beginstate 12;

	if(get_flag(26,0) == 18){
		message_dialog("You should probably speak with Dervish Malachai.  You have no need to leave this place.","");
		block_entry(1);
	}

break;

beginstate 13;

	if(get_flag(26,0) == 18){

		relocate_character(0,14,14);
		force_instant_terrain_redraw();
		pause(5);

		reset_dialog();
		add_dialog_str(0,"Malachai takes his helmet off and rests his head on his pillow, turning his face towards you.  He facial features droop into a less-uptight position, while his eyes stroll towards your face, absorbing its nuances.",0);
		clear_buffer();
		append_string("_You know, ");
		append_char_name(0);
		append_string(", you're a lot different from the other troops I have back at Fort Ivalice...  I know nothing about you._");
		get_buffer_text(custom_m);
		add_dialog_str(1,custom_m,0);
		add_dialog_str(2,"_I haven't told this to anyone yet, but every soldier who works under me was hand-picked in Kalzas at my behest.  Everyone who Marched to Ivalice with me was competent, but moreover, they all have had traumatic pasts._",0);
		add_dialog_str(3,"_Everyone there suffers...  They suffer in a way that I wish I could take away.  But I can't._",0);
		if(get_flag(99,0) == 1)
			add_dialog_choice(0,"What was your past like?");
		if(get_flag(99,0) == 2)
			add_dialog_choice(0,"Why do you do it?");
		bmessage = run_dialog(1);

		reset_dialog();
		if(get_flag(99,0) == 1){
			add_dialog_str(0,"_My past?  I...  was rich.  Both parents were alive, both parents loved me very much, and I felt as if I was blessed by a deity._",0);
		}
		if(get_flag(99,0) == 2){
			add_dialog_str(0,"_Why?  I...  was born rich.  Both parents were alive, both parents loved me very much, and I felt as if I was blessed by a deity._",0);
		}
		add_dialog_str(1,"_Unfortunately, I became enchanted by money, and never understood my parents' love...  And then they died.  Old age._",0);
		add_dialog_str(2,"_My father was a military man.  Full honors, decoration almost as heavy as his armor, et cetera...  He held firmly to tradition.  And so I enlisted, and learned the value of camaraderie.  Then, Stewart I decided to invade Valorim._",0);
		add_dialog_str(3,"_People, friends...  They all died.  Nephils were slaughtered.  I was told by Foottracer, 'Death is natural path towards progress...'  But I would not accept that.  I still don't._",0);
		add_dialog_str(4,"_There is no room for pain and misery in this world...  And yet I have caused so much.  I am just trying to repent.  Please try and understand._",0);
		add_dialog_choice(0,"...");
		bmessage = run_dialog(1);

		reset_dialog();
		clear_buffer();
		append_string("_");
		append_char_name(0);
		append_string(", if you don't mind me asking, where did you grow up?_");
		get_buffer_text(custom_m);
		add_dialog_str(0,custom_m,0);
		add_dialog_choice(0,"House Aaron.");
		bmessage = run_dialog(1);

		reset_dialog();
		add_dialog_str(0,"He looks surprised.  _House Aaron...  The one from Northern Valorim, right?_",0);
		add_dialog_str(1,"You tell him that he is, indeed, correct.",0);
		add_dialog_str(2,"_That's impossible, though!  The master of the house, Brigadier General Aaron, and his wife both died of a bizarre disease, as did all their relations.  Their daughter died of the disease, and their younger child..._",0);
		add_dialog_str(3,"_Their other child ran away and was assumed to have died._",0);
		add_dialog_str(4,"_Why didn't you go back?  You would have riches beyond your wildest imagining, and you would probably get a few promotions._",0);
		if(get_flag(99,0) == 1)
			add_dialog_choice(0,"I would have to remember.");
		if(get_flag(99,0) == 2)
			add_dialog_choice(0,"None of your business.");
		bmessage = run_dialog(1);

		reset_dialog();
		add_dialog_str(0,"_I'm sorry...  I'm so, so sorry.  I do not understand the pain you suffered, and for that, I apologize._",0);
		add_dialog_str(1,"An eternity passes.",0);
		add_dialog_str(2,"_Your family...  Tell me about them._",0);
		add_dialog_choice(0,"They were nice...");
		bmessage = run_dialog(1);

		block_entry(1);
		set_ticks_forward(1000);
		force_instant_terrain_redraw();

		move_to_new_town(5,12,14);
	}

break;

beginstate 14;

	if(get_flag(26,0) == 24){
		message_dialog("He's not here either...!  But where could he have gone?  And why did he go?","Maybe he thought you would be okay, and he went back to Fort Ivalice.  At any rate, that's probably the best place to start.");
		set_flag(26,0,25);

		if(get_flag(99,4) == 1){
			set_flag(99,4,0);

			reunite_party();
			relocate_character(0,13,20);
			pcs = 1;
			while(pcs <= 3){
				if(char_ok(pcs) == TRUE){
					relocate_character(pcs,(pcs + 12),24);
					set_character_facing(pcs,4);
				}
				pcs = (pcs + 1);
			}
			force_view_center(13,20);
			force_instant_terrain_redraw();
			pause(10);

			reset_dialog();
			if(party_size() == 2){
				clear_buffer();
				pcs = 1;
				while(char_ok(pcs) == FALSE){
					pcs = (pcs + 1);
				}
				append_char_name(pcs);
				append_string("!  But why is your partner here, waiting for you?");
				get_buffer_text(custom_m);
				add_dialog_str(0,custom_m,0);

				clear_buffer();
				append_string("_Hey, ");
				append_char_name(0);
				append_string(".  Funny thing, meeting you like this...  Thing is, I was sent from Ivalice to check on how the raid had went.  We thought you'd have returned by then.");
				get_buffer_text(custom_m2);
				add_dialog_str(1,custom_m2,0);

				add_dialog_str(2,"_I saw the Dervish climbing up the mountain, though.  He looked, well...  Less confident than I expected, considering that he was still alive.  I thought he had lost, especially considering how injured he was._",0);
				add_dialog_str(3,"_Still, he told me that the raid was a success, and the nephils had been dispatched...  He said that I would find you here, but that I should let you rest.  So that's what I did._",0);
				add_dialog_str(4,"_Let's get back to Fort Ivalice now, okay?_",0);
			}
			if(party_size() > 2){

				clear_buffer();
				append_string("_");
				pcs = 1;
				while(pcs <= 3){
					if(char_ok(pcs) == TRUE){
						others_ok = (pcs + 1);
						twondx = 0;
						while(others_ok < 4 && twondx < 2){
	if(char_ok(others_ok) == 1){
		twondx = (twondx + 1);
	}
	others_ok = (others_ok + 1);
						}
						if(twondx == 1){
	append_char_name(pcs);
	append_string(", and ");
						}
						if(twondx > 1){
	append_char_name(pcs);
	append_string(", ");
						}
						if(twondx == 0){
	append_char_name(pcs);
	append_string("!");
						}
					}
					pcs = (pcs + 1);
				}
				append_string("  But why are your partners here, waiting for you?");
				get_buffer_text(custom_m);
				add_dialog_str(0,custom_m,0);

				clear_buffer();
				while(speaker_pcs == 0){
					pcs = random_party_member();
					if(pcs > 0){
						speaker_pcs = pcs;
					}
				}
				append_char_name(speaker_pcs);
				append_string(" begins to speak for the group.");
				get_buffer_text(custom_m2);
				add_dialog_str(1,custom_m2,0);

				clear_buffer();
				append_string("_Hey, ");
				append_char_name(0);
				append_string(".  Funny thing, meeting you like this...  Thing is, we were sent from Ivalice to check on how the raid had went.  We thought you'd have returned by then.");
				get_buffer_text(custom_m2);
				add_dialog_str(2,custom_m2,0);

				add_dialog_str(3,"_We saw the Dervish climbing up the mountain, though.  He looked, well...  Less confident than we expected, considering that he was still alive.  We thought he had lost, especially considering how injured he was._",0);
				add_dialog_str(4,"_Still, he told us that the raid was a success, and the nephils had been dispatched...  He said that we would find you here, but that we should let you rest.  So that's what we did._",0);
				add_dialog_str(5,"_Let's get back to Fort Ivalice now, okay?_",0);
			}
			bmessage = run_dialog(1);
		}
	}

break;


beginstate 20;

	set_flag(99,3,0);

break;

beginstate 21;

	set_flag(99,3,1);

break;

beginstate 22;

	set_flag(99,3,2);

break;

beginstate 23;

	set_flag(99,3,3);

break;
