void ProcessDialogEvent()
{
	ref NPChar;
	aref Link, NextDiag;

	DeleteAttribute(&Dialog,"Links");
	// RM -->
	int iNation;
	string sNation;
	// RM <--
	string stemp = ""; // KK

	makeref(NPChar,CharacterRef);
	makearef(Link, Dialog.Links);
	makearef(NextDiag, NPChar.Dialog);

	ref PChar;
	PChar = GetMainCharacter();
	int iTest;

	// PG -->
	if(GetMusicScheme() == "PGMUS") { // KK
		iNation = sti(NPChar.nation);
		sNation = iNation;
		if(musicName != "music_govreward")
		{
// KK -->
			if (ENABLE_FLAGS == 0) {
				if(GetRMRelation(PChar,iNation) >= REL_AMNESTY) SetMusicNoPause("music_govfriend");
				else SetMusicNoPause("music_govenemy");
			} else {
				if(GetActualRMRelation(iNation) >= REL_AMNESTY) SetMusicNoPause("music_govfriend");
				else SetMusicNoPause("music_govenemy");
			}
// <-- KK
		}
	}
	// PG <--
	
	switch(Dialog.CurrentNode)
	{
		// -----------------------------------Диалог первый - первая встреча
		case "First time":
			Dialog.defAni = "dialog_stay1";
			Dialog.defCam = "1";
			Dialog.defSnd = "dialogs\0\017";
			Dialog.defLinkAni = "dialog_1";
			Dialog.defLinkCam = "1";
			Dialog.defLinkSnd = "dialogs\woman\024";
			Dialog.ani = "dialog_stay2";
			Dialog.cam = "1";
			Dialog.snd = "voice\REGR\REGR001";
			
			dialog.text = DLG_TEXT[0];
			link.l1 = DLG_TEXT[1];
			link.l1.go = "talk_1";

			NextDiag.TempNode = "second time";
		break;

		case "talk_1":
			Dialog.snd = "voice\REGR\REGR002";
			dialog.text = DLG_TEXT[2];
			link.l1 = DLG_TEXT[3] + GetMyFullName(PChar) + DLG_TEXT[4] + pchar.ship.name + DLG_TEXT[5];
			link.l1.go = "talk_2";
		break;

		case "talk_2":
			Dialog.snd = "voice\REGR\REGR003";
			dialog.text = DLG_TEXT[6];
			link.l1 = DLG_TEXT[7];
			link.l1.go = "quests";
			link.l2 = DLG_TEXT[8];
			link.l2.go = "talk_3";
		break;

		case "talk_3":
			Dialog.snd = "voice\REGR\REGR004";
			Preprocessor_Add("addr", GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false)); // KK
			dialog.text = DLG_TEXT[9];
			link.l1 = DLG_TEXT[10];
			link.l1.go = "exit";
		break;

		case "second time":
			iNation = sti(NPChar.nation);
			Dialog.snd = "voice\REGR\REGR005";
			Preprocessor_Add("addr", GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false)); // KK
			dialog.text = DLG_TEXT[11];
			link.l1 = DLG_TEXT[12];
			link.l1.go = "quests";
			// RM -->
			iNation = sti(NPChar.nation);
			Preprocessor_Add("gov_nation", XI_ConvertString(GetNationDescByType(iNation))); // KK
// KK -->
			Link.l3 = DLG_TEXT[98];
			Link.l3.go = "international relations";
			if (ENABLE_FLAGS == 0) {
				if(GetRMRelation(PChar, iNation) <= REL_WAR)
				{
					Link.l4 = DLG_TEXT[97];
					Link.l4.go = "amnesty";
				}
				else
				{
					if(GetRMRelation(PChar,iNation) < REL_NEUTRAL)
					{
						Link.l4 = DLG_TEXT[99];
						Link.l4.go = "LoM";
					}
					else
					{
						if(SetupMR(&PChar, &NPChar))
						{
							dialog.text = GenerateMR(PChar, NPChar);
						}
						if(PromoteCanBe(PChar, iNation) && GetRMRelation(PChar, iNation) > RequiredNextRank(PChar, iNation))
						{
							if(GetMusicScheme() == "PGMUS") SetMusicNoPause("music_govreward"); // PG // KK
							Link.l4 = DLG_TEXT[80];
							Link.l4.go = "promote";
						}
						Link.l6 = PCharRepPhrase(DLG_TEXT[103] + XI_ConvertString(GetNationNameByType(iNation)) + DLG_TEXT[104], DLG_TEXT[105] + XI_ConvertString(GetNationNameByType(iNation)) + ".");
						Link.l6.go = "leave";
					}
				}
			} else {
				if(GetActualRMRelation(iNation) <= REL_WAR)
				{
					Link.l4 = DLG_TEXT[97];
					Link.l4.go = "amnesty";
				}
				else
				{
					if (GetActualRMRelation(iNation) < REL_NEUTRAL || !HaveLetterOfMarque(iNation))
					{
						Link.l4 = DLG_TEXT[99];
						Link.l4.go = "LoM";
					}
					else
					{
						if(SetupMR(&PChar, &NPChar))
						{
							dialog.text = GenerateMR(PChar, NPChar);
						}
						if(PromoteCanBe(PChar, iNation) && GetActualRMRelation(iNation) > RequiredNextRank(PChar, iNation))
						{
							if(GetMusicScheme() == "PGMUS") SetMusicNoPause("music_govreward"); // PG
							Link.l4 = DLG_TEXT[80];
							Link.l4.go = "promote";
						}
						Link.l6 = PCharRepPhrase(DLG_TEXT[103] + XI_ConvertString(GetNationNameByType(iNation)) + DLG_TEXT[104], DLG_TEXT[105] + XI_ConvertString(GetNationNameByType(iNation)) + ".");
						Link.l6.go = "leave";
					}
				}
			}
// <-- KK
			link.l5 = DLG_TEXT[13];
			link.l5.go = "exit";
		break;

		case "LoM":
			iNation = sti(NPChar.nation);
			dialog.text = DLG_TEXT[106] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[107] + makeint(PChar.rank)*LOM_COST + DLG_TEXT[108];
			Link.l1 = DLG_TEXT[109];
			Link.l1.go = "exit";
			if(GetCharacterMoney(PChar) > makeint(PChar.rank)*LOM_COST)
			{
				Link.l2 = DLG_TEXT[110];
				Link.l2.go = "LoMc";
			}
		break;

		case "LoMc":
// KK -->
			stemp = "";
			iNation = sti(NPChar.nation);
			stemp = GetHostileNations(iNation);
			if (stemp != "")
				stemp = DLG_TEXT[50] + DLG_TEXT[51] + DLG_TEXT[53] + DLG_TEXT[54] + stemp + DLG_TEXT[56] + DLG_TEXT[58];
			else
				stemp = DLG_TEXT[50] + DLG_TEXT[51] + DLG_TEXT[52] + DLG_TEXT[53] + DLG_TEXT[55] + DLG_TEXT[57] + DLG_TEXT[58];
			AddMoneyToCharacter(&PChar, makeint(PChar.rank)*-LOM_COST);
			PlayStereoSound("INTERFACE\took_item.wav");
			SetRMRelation(&PChar, iNation, REL_NEUTRAL);
			if (ENABLE_FLAGS == 1) {
				SetActualRMRelation(iNation, REL_NEUTRAL);
				ReceiveLetterOfMarque(iNation);
			}
			SetRank(PChar, iNation, GetOldRank(PChar, iNation));
			if(sti(PChar.reputation) >= REP_COMEBACKMIN && GetRank(PChar, iNation)) stemp += DLG_TEXT[110] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[111] + XI_ConvertString(GetRankName(PChar, iNation));
			dialog.Text = stemp;
			Link.l1 = DLG_TEXT[59];
			Link.l1.go = "exit";
// <-- KK
		break;

		case "amnesty":
			iNation = sti(NPChar.nation);
			sNation = iNation;
			dialog.text = DLG_TEXT[113] + XI_ConvertString(GetNationNameByType(iNation)) + ", " + GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false) + DLG_TEXT[114] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[115];
			Link.l1 = DLG_TEXT[116];
			Link.l1.go = "exit";
			if(sti(pchar.reputation) >= 40)
			{
				pchar.nations.(sNation).Amnesty = RMAmnestyCost(PChar, iNation);
				dialog.text = FirstLetter(GetMyRespectfullyName(PChar)) + "! " + DLG_TEXT[117] + XI_ConvertString(GetNationNameByType(iNation)) + DLG_TEXT[118] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[119] + ShowMoney(-sti(pchar.nations.(sNation).Amnesty)) + DLG_TEXT[108];
				if(makeint(pchar.money) >= 0-makeint(pchar.nations.(sNation).Amnesty))
				{
					Link.l2 = DLG_TEXT[120];
					Link.l2.go = "Amnestyc";
				}
			}
		break;

		case "Amnestyc":
			iNation = sti(NPChar.nation);
			sNation = iNation;
			dialog.text = DLG_TEXT[121] + GetMyRespectfullyName(PChar) + ".";
			PlayStereoSound("INTERFACE\took_item.wav");
			AddMoneyToCharacter(pchar, sti(pchar.nations.(sNation).Amnesty));
			DeleteAttribute(pchar,("nations."+sNation+".Amnesty"));
			RMAmnesty(&Pchar, iNation);
			Link.l1 = DLG_TEXT[122];
			Link.l1.go = "exit";
		break;

		case "promote":
			iNation = sti(NPChar.nation);
			NPChar.templand = GetCharacterLand(PChar);
			Promote(&PChar, &NPChar, iNation);
			dialog.text = DLG_TEXT[81] + XI_ConvertString(GetRankName(PChar, iNation)) + DLG_TEXT[123] + (GetCharacterLand(PChar) - sti(NPChar.templand)) + DLG_TEXT[124] + XI_ConvertString(GetNationRoyalByType(iNation)) + "."; // PB: Fixes a dialog display error
			DeleteAttribute(NPChar,"templand");
			Link.l1 = DLG_TEXT[82];
			Link.l1.go = "exit";
		break;

		case "leave":
			iNation = sti(NPChar.nation);
			dialog.text = DLG_TEXT[125] + PChar.name + "? " + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[126];
			Link.l1 = DLG_TEXT[127];
			Link.l1.go = "leavec";
			if(sti(PChar.reputation) < REP_LEAVEMIN + 5)
			{
				dialog.text = XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[128] + PChar.name + ".");
				Link.l1 = DLG_TEXT[134];
			}
			Link.l2 = DLG_TEXT[129];
			Link.l2.go = "exit";
		break;

		case "leavec":
			ChangeCharacterReputation(&PChar, -5);
			iNation = sti(NPChar.nation);
			if (ENABLE_FLAGS == 1) LooseLetterOfMarque(iNation); // KK
			dialog.text = DLG_TEXT[130];
			Link.l1 = DLG_TEXT[116];
			Link.l1.go = "exit";
			if(LeaveService(&PChar, iNation, false))
			{
				dialog.text = DLG_TEXT[131] + XI_ConvertString(DEFAULT_TITLE) + " " + PChar.lastname ".";
				Link.l1 = DLG_TEXT[132];
				Link.l1.go = "leavebad";
			}
		break;

		case "leavebad":
			iNation = sti(NPChar.nation);
			NextDiag.CurrentNode = NextDiag.TempNode;
			DialogExit();
			ref sld;
			string loc;
			float dist;
			GetNearLocator( "goto", &dist, &loc, 0);
			sld = LAi_CreateFantomCharacter(false, 0, true, true, true, LAi_SoldierModel(iNation), "goto", loc);
			LAi_group_MoveCharacter(sld, GetSoldiersName(iNation)+"_SOLDIERS");

			sld = LAi_CreateFantomCharacter(false, 0, true, true, true, LAi_SoldierModel(iNation), "goto", loc);
			LAi_group_MoveCharacter(sld, GetSoldiersName(iNation)+"_SOLDIERS");

			LAi_group_SetRelation(GetSoldiersName(iNation)+"_SOLDIERS", LAI_GROUP_PLAYER, LAI_GROUP_ENEMY);
			LAi_group_FightGroups(GetSoldiersName(iNation)+"_SOLDIERS", LAI_GROUP_PLAYER, true);
		// RM <--
		break;

		case "quests":
			itest = 0;
			Dialog.snd = "voice\REGR\REGR006";
			Preprocessor_Add("addr", GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false)); // KK
			dialog.text = DLG_TEXT[14];
			int linkNum = 1;
			string curLink = "l";
			if (CheckQuestAttribute("ANIMISTS", "begin") && iTest < QUEST_COUNTER)
			{
				curLink = "l";
				linkNum = sti(linkNum+1);
				curLink = curLink+linkNum;
				link.(curLink) = DLG_TEXT[15];
				Link.(curLink).go = "ANIMISTS_1";
				iTest = iTest + 1;
			}
			if (CheckQuestAttribute("ANIMISTS", "completed") && npchar.quest.ANIMISTS !="completed" && iTest < QUEST_COUNTER)
			{
				curLink = "l";
				linkNum = sti(linkNum+1);
				curLink = curLink+linkNum;
				link.(curLink) = DLG_TEXT[16];
				link.(curLink).go = "ANIMISTS_completed";
			}
			if (characters[GetCharacterIndex("Toff Oremans")].quest.daughter == "help_begin" && iTest < QUEST_COUNTER)
			{
				curLink = "l";
				linkNum = sti(linkNum+1);
				curLink = curLink+linkNum;
				link.(curLink) = DLG_TEXT[17];
				link.(curLink).go = "daughter_1";
			}
			if (characters[GetCharacterIndex("Toff Oremans")].quest.daughter == "good_2_complete" || characters[GetCharacterIndex("Toff Oremans")].quest.daughter == "almost_complete")
			{
				curLink = "l";
				linkNum = sti(linkNum+1);
				curLink = curLink+linkNum;
				link.(curLink) = DLG_TEXT[18];
				link.(curLink).go = "daughter_5";
			}
// boal -->
			if (!CheckAttribute(pchar, "quest.generate_kill_quest") && GetNationRelation2MainCharacter(GetCurrentLocationNation()) != RELATION_ENEMY)
			{
				curLink = "l";
				linkNum = sti(linkNum+1);
				curLink = curLink+linkNum;
				dialog.text = DLG_TEXT[85];
				link.(curLink) = DLG_TEXT[86];
				link.(curLink).go = "kill_pirate";
			}
			if (CheckQuestAttribute("generate_kill_quest", "completed") && pchar.quest.generate_kill_quest.town==GetCurrentTownID())
			{
				curLink = "l";
				linkNum = sti(linkNum+1);
				curLink = curLink+linkNum;
				dialog.text = DLG_TEXT[87];
				link.(curLink) = DLG_TEXT[88];
				link.(curLink).go = "kill_pirate_completed";
			}
			curLink = "l";
			linkNum = sti(linkNum+1);
			curLink = curLink+linkNum;
			link.(curLink)  = DLG_TEXT[19];
			link.(curLink).go = "exit";
			Preprocessor_Remove("addr"); // KK
		break;
		
		case "kill_pirate":
			GeneratePirateQuest(NPChar); // KK
			int quest_pirate_idx = GetCharacterIndex("Quest pirate"); // KK
			iNation = sti(characters[quest_pirate_idx].nation); // KK
			dialog.text = DLG_TEXT[89] + XI_ConvertString("sw_" + GetNationDescByType(iNation)) + DLG_TEXT[90] + characters[quest_pirate_idx].ship.name + DLG_TEXT[92] + XI_ConvertString(pchar.quest.generate_kill_quest.destination)я + DLG_TEXT[93] + pchar.quest.generate_kill_quest.money + DLG_TEXT[94]; // KK
			link.l1 = DLG_TEXT[95];
			link.l1.go = "kill_pirate_agree";
			link.l2 = DLG_TEXT[96];
			link.l2.go = "kill_pirate_refused";
		break;

		case "kill_pirate_agree":
			DialogExit();
			NextDiag.CurrentNode = NextDiag.TempNode;
			AddDialogExitQuest("kill_pirate_agree");
		break;

		case "kill_pirate_refused":
			DialogExit();
			NextDiag.CurrentNode = NextDiag.TempNode;
			AddDialogExitQuest("kill_pirate_refused");
		break;

		case "kill_pirate_completed":
			if(AUTO_SKILL_SYSTEM)
			{
				AddPartyExpChar(pchar, "Leadership", 1500);
				AddPartyExpChar(pchar, "Sneak", 15);
			}
			else { AddPartyExp(pchar, 1500); }
		    PlayStereoSound("INTERFACE\took_item.wav");
			AddMoneyToCharacter(pchar, makeint(pchar.quest.generate_kill_quest.money));
			AddDialogExitQuest("kill_pirate_refused_2");
			ChangeCharacterReputation(pchar, 1);
			DialogExit();
			NextDiag.CurrentNode = NextDiag.TempNode;
			pchar.quest.generate_kill_quest = "wait_timer";
		break;
// boal <--

		case "ANIMISTS_completed":
			Dialog.snd = "voice\REGR\REGR007";
			Preprocessor_Add("addr", GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false)); // KK
			dialog.text = DLG_TEXT[20];
			link.l1 = DLG_TEXT[21];
			link.l1.go = "exit";
			npchar.quest.ANIMISTS = "completed";
			if(AUTO_SKILL_SYSTEM)
			{
				AddPartyExpChar(pchar, "Leadership", 2800);
				AddPartyExpChar(pchar, "Sneak", 28);
			}
			else { AddPartyExp(pchar, 2800); }
		break;

		case "daughter_1":
			Dialog.snd = "voice\REGR\REGR008";
			dialog.text = DLG_TEXT[22];
			Preprocessor_Add("Bernardo Gamboa", GetCharacterFullName("Bernardo Gamboa")); // KK
			Preprocessor_Add("addr", GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false)); // KK
			link.l1 = DLG_TEXT[23];
			link.l1.go = "daughter_2";
		break;

		case "daughter_2":
			Dialog.snd = "voice\REGR\REGR009";
			dialog.text = DLG_TEXT[26];
			link.l1 = DLG_TEXT[27];
			link.l1.go = "daughter_3";
		break;

		case "daughter_3":
			Dialog.snd = "voice\REGR\REGR010";
			dialog.text = DLG_TEXT[28];
			link.l1 = DLG_TEXT[29];
			link.l1.go = "daughter_4";
		break;

		case "daughter_4":
			Dialog.snd = "voice\REGR\REGR011";
			Preprocessor_Add("addr", GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false)); // KK
			dialog.text = DLG_TEXT[30];
			link.l1 = DLG_TEXT[31];
			link.l1.go = "exit";
			characters[GetCharacterIndex("Toff Oremans")].quest.daughter = "governor";
			AddQuestRecord("daughter", 2);
		break;

		case "daughter_5":
			Dialog.snd = "voice\REGR\REGR012";
			dialog.text = DLG_TEXT[32];
			link.l1 = DLG_TEXT[33];
			link.l1.go = "daughter_6";
		break;

		case "daughter_6":
			Dialog.snd = "voice\REGR\REGR013";
			dialog.text = DLG_TEXT[34];
			link.l1 = DLG_TEXT[35];
			link.l1.go = "daughter_7";
		break;

		case "daughter_7":
			Dialog.snd = "voice\REGR\REGR014";
			dialog.text = DLG_TEXT[36];
			link.l1 = DLG_TEXT[37];
			link.l1.go = "exit";
			AddQuestRecord("daughter", 5);
			if (characters[GetCharacterIndex("Toff Oremans")].quest.daughter == "almost_complete")
			{
				closeQuestHeader("daughter");
				characters[GetCharacterIndex("Toff Oremans")].quest.daughter = "almost_2_complete";
			}
			characters[GetCharacterIndex("Toff Oremans")].quest.daughter = "bad_2_complete";
			if(AUTO_SKILL_SYSTEM)
			{
				AddPartyExpChar(pchar, "Leadership", 4500);
				AddPartyExpChar(pchar, "Sneak", 45);
			}
			else { AddPartyExp(pchar, 4500); }
			PlayStereoSound("INTERFACE\took_item.wav");
			AddMoneyToCharacter(pchar, 850);
		break;

		case "ANIMISTS_1":
			Dialog.snd = "voice\REGR\REGR015";
			dialog.text = DLG_TEXT[38];
			link.l1 = DLG_TEXT[39];
			link.l1.go = "ANIMISTS_2";
		break;

		case "ANIMISTS_2":
			Dialog.snd = "voice\REGR\REGR016";
			dialog.text = DLG_TEXT[40];
			link.l1 = DLG_TEXT[41];
			link.l1.go = "ANIMISTS_3";
		break;

		case "ANIMISTS_3":
			Dialog.snd = "voice\REGR\REGR017";
			dialog.text = DLG_TEXT[42];
			link.l1 = DLG_TEXT[43];
			link.l1.go = "ANIMISTS_4";
		break;

		case "ANIMISTS_4":
			Dialog.snd = "voice\REGR\REGR018";
			dialog.text = DLG_TEXT[44];
			link.l1 = DLG_TEXT[45];
			link.l1.go = "ANIMISTS_5";
			link.l2 = DLG_TEXT[46];
			link.l2.go = "ANIMISTS_denied";
			pchar.quest.ANIMISTS = "denied";
			pchar.quest.exit_from_douwesen_townhall.win_condition.l1 = "location";
			pchar.quest.exit_from_douwesen_townhall.win_condition.l1.location = "Douwesen_town";
			pchar.quest.exit_from_douwesen_townhall.win_condition = "exit_from_douwesen_townhall_complete";
		break;

		case "ANIMISTS_5":
			Dialog.snd = "voice\REGR\REGR019";
			dialog.text = DLG_TEXT[47];
			link.l1 = DLG_TEXT[48];
			link.l1.go = "exit";
			pchar.quest.ANIMISTS = "to_douwesen_tavern";
			AddQuestRecord("ANIMISTS", 3);
			ChangeCharacterAddressGroup(&characters[GetCharacterIndex("Gheraed Drabbe")], "Douwesen_tavern", "sit", "sit5");
		break;

		case "ANIMISTS_denied":
			AddQuestRecord("ANIMISTS", 2);
			CloseQuestHeader("ANIMISTS");
			DialogExit();
			NextDiag.CurrentNode = NextDiag.TempNode;
		break;

		case "Exit":
			DialogExit();
			NextDiag.CurrentNode = NextDiag.TempNode;
		break;

// KK -->
		case "international relations":
			stemp = "";
			iNation = sti(NPChar.nation);
			stemp = GetHostileNations(iNation);
			if (stemp != "")
				stemp = DLG_TEXT[51] + DLG_TEXT[53] + DLG_TEXT[54] + stemp + DLG_TEXT[56] + DLG_TEXT[58];
			else
				stemp = DLG_TEXT[51] + DLG_TEXT[52] + DLG_TEXT[53] + DLG_TEXT[55] + DLG_TEXT[57] + DLG_TEXT[58];
			dialog.Text = stemp;
			Link.l1 = DLG_TEXT[75];
			Link.l1.go = "exit";
		break;
// <-- KK
	}
}
