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

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

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

	ref PChar;
	PChar = GetMainCharacter();

	int cDays = Rand(1);
	if(CheckQuestAttribute("ChangeHoverPos2", "completed"))
	{
		pchar.quest.ChangeHoverPos1.win_condition.l1 = "Timer";
		pchar.quest.ChangeHoverPos1.win_condition.l1.date.day = GetAddingDataDay(0, cDays, 0);
		pchar.quest.ChangeHoverPos1.win_condition.l1.date.month = GetAddingDataMonth(0, cDays, 0);
		pchar.quest.ChangeHoverPos1.win_condition.l1.date.year = GetAddingDataYear(0, cDays, 0);
		pchar.quest.ChangeHoverPos1.win_condition = "ChangeHoverPos";
		pchar.quest.ChangeHoverPos1_progress = "begin";
		pchar.quest.ChangeHoverPos1.idx = GetCharacterIndex(NPChar);
	}
	if(CheckQuestAttribute("ChangeHoverPos1", "completed"))
	{
		pchar.quest.ChangeHoverPos2.win_condition.l1 = "Timer";
		pchar.quest.ChangeHoverPos2.win_condition.l1.date.day = GetAddingDataDay(0, cDays, 0);
		pchar.quest.ChangeHoverPos2.win_condition.l1.date.month = GetAddingDataMonth(0, cDays, 0);
		pchar.quest.ChangeHoverPos2.win_condition.l1.date.year = GetAddingDataYear(0, cDays, 0);
		pchar.quest.ChangeHoverPos2.win_condition = "ChangeHoverPos";
		pchar.quest.ChangeHoverPos2_progress = "begin";
		pchar.quest.ChangeHoverPos2.idx = GetCharacterIndex(NPChar);
	}

	// 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";
			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";
			dialog.text = DLG_TEXT[11];
			link.l1 = DLG_TEXT[12];
			link.l1.go = "quests";
			// RM -->
			iNation = sti(NPChar.nation);
// KK -->
			if (ENABLE_FLAGS == 0) {
				if(GetRMRelation(PChar, iNation) <= REL_WAR)
				{
					Link.l3 = DLG_TEXT[88];
					Link.l3.go = "amnesty";
				}
				else
				{
					if(GetRMRelation(PChar,iNation) < REL_NEUTRAL)
					{
						Link.l3 = DLG_TEXT[89];
						Link.l3.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.l3 = DLG_TEXT[72];
							Link.l3.go = "promote";
						}
						Link.l5 = PCharRepPhrase(DLG_TEXT[93] + XI_ConvertString(GetNationNameByType(iNation)) + DLG_TEXT[94], DLG_TEXT[95] + XI_ConvertString(GetNationNameByType(iNation)) + ".");
						Link.l5.go = "leave";
					}
				}
			} else {
				if(GetActualRMRelation(iNation) <= REL_WAR)
				{
					Link.l3 = DLG_TEXT[88];
					Link.l3.go = "amnesty";
				}
				else
				{
					if ((GetActualRMRelation(iNation) < REL_NEUTRAL) || (!HaveLetterOfMarque(iNation)))
					{
						Link.l3 = DLG_TEXT[89];
						Link.l3.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.l3 = DLG_TEXT[72];
							Link.l3.go = "promote";
						}
						Link.l5 = PCharRepPhrase(DLG_TEXT[93] + XI_ConvertString(GetNationNameByType(iNation)) + DLG_TEXT[94], DLG_TEXT[95] + XI_ConvertString(GetNationNameByType(iNation)) + ".");
						Link.l5.go = "leave";
					}
				}
			}
// <-- KK
			link.l4 = DLG_TEXT[13];
			link.l4.go = "exit";
		break;

		case "LoM":
			iNation = sti(NPChar.nation);
			dialog.text = DLG_TEXT[96] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[97] + makeint(PChar.rank)*LOM_COST + DLG_TEXT[98];
			Link.l1 = DLG_TEXT[99];
			Link.l1.go = "exit";
			if(GetCharacterMoney(PChar) > makeint(PChar.rank)*LOM_COST)
			{
				Link.l2 = DLG_TEXT[100];
				Link.l2.go = "LoMc";
			}
		break;
		
		case "LoMc":
			iNation = sti(NPChar.nation);
			AddMoneyToCharacter(&PChar, makeint(PChar.rank)*-LOM_COST);
			SetRMRelation(&PChar, iNation, REL_NEUTRAL);
// KK -->
			if (ENABLE_FLAGS == 1)
			{
				SetActualRMRelation(iNation, REL_NEUTRAL);
				ReceiveLetterOfMarque(iNation);
			}
// <-- KK
			string st = DLG_TEXT[50];
			SetRank(PChar, iNation, GetOldRank(PChar, iNation));
			if(sti(PChar.reputation) >= REP_COMEBACKMIN && GetRank(PChar, iNation)) st += DLG_TEXT[101] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[102] + XI_ConvertString(GetRankName(PChar, iNation));
			dialog.Text = st;
			Link.l1 = DLG_TEXT[51];
			Link.l1.go = "exit";
		break;
		
		case "amnesty":
			iNation = sti(NPChar.nation);
			sNation = iNation;
			dialog.text = DLG_TEXT[103] + XI_ConvertString(GetNationNameByType(iNation)) + ", " + GetMyAddressForm(NPChar, PChar, ADDR_TITLE, false, false) + DLG_TEXT[104] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[105];
			Link.l1 = DLG_TEXT[106];
			Link.l1.go = "exit";
			if(sti(pchar.reputation) >= 40)
			{
				pchar.nations.(sNation).Amnesty = RMAmnestyCost(PChar, iNation);
				dialog.text = DLG_TEXT[107] + XI_ConvertString(GetNationNameByType(iNation)) + DLG_TEXT[108] + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[109] + ShowMoney(-sti(pchar.nations.(sNation).Amnesty)) + DLG_TEXT[98];
				if(makeint(pchar.money) >= 0-makeint(pchar.nations.(sNation).Amnesty))
				{
					Link.l2 = DLG_TEXT[110];
					Link.l2.go = "Amnestyc";
				}
			}
		break;
		
		case "Amnestyc":
			iNation = sti(NPChar.nation);
			sNation = iNation;
			dialog.text = DLG_TEXT[111] + GetMyFullName(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[112];
			Link.l1.go = "exit";
		break;
		
		case "promote":
			iNation = sti(NPChar.nation);
			NPChar.templand = GetCharacterLand(PChar);
			Promote(&PChar, &NPChar, iNation);
			dialog.text = DLG_TEXT[73] + XI_ConvertString(GetRankName(PChar, iNation)) + DLG_TEXT[113] + (GetCharacterLand(PChar) - sti(NPChar.templand)) + DLG_TEXT[114] + XI_ConvertString(GetNationRoyalByType(iNation)) + "."; // PB: Might fix a dialog display error. Please check!
			DeleteAttribute(NPChar,"templand");
			Link.l1 = DLG_TEXT[74];
			Link.l1.go = "exit";
		break;
			
		case "leave":
			iNation = sti(NPChar.nation);
			dialog.text = DLG_TEXT[115] + PChar.name + "? " + XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[116];
			Link.l1 = DLG_TEXT[117];
			Link.l1.go = "leavec";
			if(sti(PChar.reputation) < REP_LEAVEMIN + 5)
			{
				dialog.text = XI_ConvertString(GetNationRoyalByType(iNation)) + DLG_TEXT[118] + PChar.name + ".";
				Link.l1 = "Tough.";
			}
			Link.l2 = DLG_TEXT[119];
			Link.l2.go = "exit";
		break;
		
		case "leavec":
			ChangeCharacterReputation(&PChar, -5);
			iNation = sti(NPChar.nation);
// KK -->
			if (ENABLE_FLAGS == 1)
				LooseLetterOfMarque(iNation);
// <-- KK
			dialog.text = DLG_TEXT[120];
			Link.l1 = DLG_TEXT[106];
			Link.l1.go = "exit";
			if(LeaveService(&PChar, iNation, false))
			{
				dialog.text = DLG_TEXT[121] + XI_ConvertString(DEFAULT_TITLE) + " " + PChar.lastname ".";
				Link.l1 = DLG_TEXT[122];
				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":
			Dialog.snd = "voice\REGR\REGR006";
			dialog.text = DLG_TEXT[14];
// boal -->
			if (!CheckAttribute(pchar, "quest.generate_kill_quest") && GetNationRelation2MainCharacter(GetCurrentLocationNation()) != RELATION_ENEMY)
			{
				dialog.text = DLG_TEXT[77];
				link.l1 = DLG_TEXT[78];
				link.l1.go = "kill_pirate";
			}
			if (CheckQuestAttribute("generate_kill_quest", "completed") && pchar.quest.generate_kill_quest.town==GetCurrentTownID())
			{
				dialog.text = DLG_TEXT[79];
				link.l1 = DLG_TEXT[80];
				link.l1.go = "kill_pirate_completed";
			}
			if (CheckQuestAttribute("Santiago", "payergov"))
			{
				dialog.snd = "Voice\REGR\REGR007";
				dialog.Text = DLG_TEXT[124];
				link.l1 = DLG_TEXT[125];
				link.l1.go = "gov_1";
			}
			if (CheckQuestAttribute("Santiago", "paspaye") && makeint(pchar.money) >= 10000)
			{
				link.l2 = DLG_TEXT[137];
				link.l2.go = "gov_5";
			}

			link.l99  = DLG_TEXT[19];
			link.l99.go = "exit";
		break;
		
		case "kill_pirate":
			GeneratePirateQuest(NPChar);
			int quest_pirate_idx = GetCharacterIndex("Quest pirate"); // KK
			iNation = sti(characters[quest_pirate_idx].nation);
			dialog.text = DLG_TEXT[81] + XI_ConvertString("sw_"+GetNationDescByType(iNation)) + " " + DLG_TEXT[123] + GetShipDescribe(characters[quest_pirate_idx].id, false, false, false, true) + DLG_TEXT[83] + XI_ConvertString(pchar.quest.generate_kill_quest.destination) + DLG_TEXT[84] + pchar.quest.generate_kill_quest.money + DLG_TEXT[85]; // KK
			link.l1 = DLG_TEXT[86];
			link.l1.go = "kill_pirate_agree";
			link.l2 = DLG_TEXT[87];
			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 "gov_1":
			dialog.snd = "Voice\REGR\REGR008";
			dialog.text = DLG_TEXT[126];
			link.l1 = DLG_TEXT[138];
			link.l1.go = "gov_1_1";
		break;

		case "gov_1_1":
			dialog.snd = "Voice\REGR\REGR008";
			dialog.text = DLG_TEXT[139];
			link.l1 = DLG_TEXT[127];
			link.l1.go = "gov_2";
		break;

		case "gov_2":
			dialog.snd = "Voice\REGR\REGR008";
			dialog.text = DLG_TEXT[128];
			link.l1 = DLG_TEXT[129];
			link.l1.go = "gov_3";
		break;

		case "gov_3":
			dialog.snd = "Voice\REGR\REGR008";
			dialog.text = DLG_TEXT[130];
			link.l1 = DLG_TEXT[131];
			link.l1.go = "gov_4";
		break;

		case "gov_4":
			dialog.snd = "Voice\REGR\REGR008";
			dialog.text = DLG_TEXT[132];
			if (makeint(pchar.money >= 10000))
			{
				link.l1 = DLG_TEXT[133];
				link.l1.go = "gov_5";
			}
			link.l2 = DLG_TEXT[134];
			link.l2.go = "exit";
			pchar.quest.santiago = "paspaye";
		break;

		case "gov_5":
			PlayStereoSound("INTERFACE\took_item.wav");
			AddMoneyToCharacter(pchar, -10000);
			dialog.snd = "Voice\CLLA\CLLA006";
			dialog.text = DLG_TEXT[135];
			link.l1 = DLG_TEXT[136];
			link.l1.go = "exit";
			pchar.quest.santiago = "preparation";
			AddDialogExitQuest("txikilibre");
			AddQuestRecord("Santiago", 6);
		break;

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