#include "TEXT\DIALOGS\MainHero_dialog.h"
void ProcessDialogEvent()
{
	ref NPChar;
	aref Link, NextDiag;

	DeleteAttribute(&Dialog,"Links");

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

    ref chr;
    float  fTemp;
    bool bOk;
    
	switch(Dialog.CurrentNode)
	{
        case "Exit":
			NextDiag.CurrentNode = NextDiag.TempNode;
			DialogExit_Self();
		break;
		
		case "First time":
	      	NextDiag.TempNode = "First time";

	        Dialog.Text = DLG_TEXT_BASE[0];
			Link.l1 = DLG_TEXT_BASE[1];
			Link.l1.go = "exit";
 			//===> Квесты мэра, базар после боевки на палубе
			if (CheckAttribute(pchar, "quest.DestroyPirate_PirateIsOut"))
			{
				dialog.text = DLG_TEXT_BASE[2];
				Link.l1 = "...";
    			Link.l1.go = "exit";
				pchar.quest.Munity = "Deads";
			}
			// ==> Английская линейка, квест №8: в поселении буканьеров
   	    	if (pchar.questTemp.State == "ToMansfield_GoOn")
    		{
    		    dialog.text = DLG_TEXT_BASE[3];
    			link.l1 = "...";
    			link.l1.go = "exit";
				Pchar.quest.ToMansfield_OutFromFort.win_condition.l1 = "location";
				Pchar.quest.ToMansfield_OutFromFort.win_condition.l1.location = "LaVega_port";
				Pchar.quest.ToMansfield_OutFromFort.win_condition = "ToMansfield_OutFromFort";
    		}
            // ==> Исп. линейка, квест №4: в спальне донны Анны
            if (pchar.questTemp.State == "Sp4Detection_toMirderPlace")
    		{
    		    dialog.text = DLG_TEXT_BASE[4];
    			link.l1 = "...";
    			link.l1.go = "exit";
                pchar.questTemp.State = "Sp4Detection_FoundLetter";
                AddQuestRecord("Spa_Line_4_MirderDetection", "2");
    		}
            // ==> Французская линейка, квест №2: в таверне Тортуги
   	    	if (pchar.questTemp.State == "Fr2Letter_SeekProblems")
    		{
    		    dialog.text = DLG_TEXT_BASE[5];
    			link.l1 = "...";
    			link.l1.go = "exit";
				pchar.questTemp.Out = 1;
				SaveCurrentQuestDateParam("questTemp");
    		}
			// ==> Изабелла, нашли мертвую служанку
   	    	if (pchar.RomanticQuest == "SeekIsabella" && pchar.location == "Beliz_ExitTown")
    		{
    		    dialog.text = DLG_TEXT_BASE[6];
    			link.l1 = "...";
    			link.l1.go = "exit";
				pchar.RomanticQuest = "FoundServantGirl";
				characters[GetCharacterIndex("ServantGirl")].LifeDay = 0;
    		}
			// ==> Квест Аскольда, обнаружение входа в пещеру к гробнице
			if (pchar.questTemp.Ascold == "Ascold_NotEnterFoundGrave")
			{
				dialog.text = DLG_TEXT_BASE[7];
				link.l1 = "";
				link.l1.go = "exit";
			}
			//===> Квест Аскольда, попытка вскрыть лампу.
			if (pchar.questTemp.Azzy == "Azzy_IHaveLamp" && locations[reload_location_index].type == "shipyard")
			{
				dialog.text = DLG_TEXT_BASE[8];
				Link.l1 = DLG_TEXT_BASE[9];
    			Link.l1.go = "FreedomAzzy";
			}
			//===> шебека Синяя Птица
			if (pchar.questTemp.BlueBird == "seenCarriers")
			{
				dialog.text = DLG_TEXT_BASE[10];
				Link.l1 = "...";
    			Link.l1.go = "exit";
				AddDialogExitQuestFunction("BlueBird_endCaveDialog");
			}
		break;
		// boal -->
		case "TalkSelf_Main":
	   		NextDiag.TempNode = "First time";
//navy --> after duel in tavern
			if (CheckAttribute(pchar, "questTemp.LocationClone"))
			{
				Dialog.Text = RandPhraseSimple(DLG_TEXT_BASE[11], DLG_TEXT_BASE[12]);
				link.l1 = "...";
				link.l1.go = "exit";
				DeleteAttribute(pchar, "questTemp.LocationClone");
				break;
			}
//navy <--
	        Dialog.Text = RandPhraseSimple(DLG_TEXT_BASE[13], DLG_TEXT_BASE[14]);
	        if (!bDisableMapEnter && pchar.location == Get_My_Cabin())
	        {
	            if (makeint(environment.time) >= 22.0 || makeint(environment.time) < 10.0)
	    		{
	    			link.l1 = DLG_TEXT_BASE[15];
	    			link.l1.go = "TalkSelf_room_day";
	    		}
	    		else
	    		{
	    			link.l1 = DLG_TEXT_BASE[16];
	    			link.l1.go = "TalkSelf_room_night";
	    			link.l2 = DLG_TEXT_BASE[17];
					link.l2.go = "TalkSelf_room_day";
	    		}
			}
			else 
			{   //исп.линейка, квест №6, возможность переночевать в оплаченной комнате
				if (CheckAttribute(pchar, "questTemp.State.Open") && pchar.location == "Tortuga_tavern_upstairs")
				{
					if (makeint(environment.time) >= 22.0 || makeint(environment.time) < 10.0)
	    			{
	    				link.l1 = DLG_TEXT_BASE[18];
	    				link.l1.go = "TalkSelf_room_day";
	    			}
	    			else
	    			{
	    				link.l1 = DLG_TEXT_BASE[19];
	    				link.l1.go = "TalkSelf_room_night";
	    				link.l2 = DLG_TEXT_BASE[20];
						link.l2.go = "TalkSelf_room_day";
	    			}
				}
			}
	        /*if (CheckAttribute(pchar, "Quest.Loans") || CheckAttribute(pchar, "Quest.Deposits"))
	        {
	    		Link.l4 = DLG_TEXT_BASE[21];
	    		Link.l4.go = "TalkSelf_CashBook";
			}*/
	        if (!bDisableMapEnter)//боя в море нет
	        {
	            if (GetCargoGoods(pchar, GOOD_SLAVES) > 0)
	            {
	                Link.l5 = DLG_TEXT_BASE[22];
	                Link.l5.go = "TalkSelf_SlavesToCrew";
	            }
               Link.l6 = "Let's change the Rum ration...";
	       Link.l6.go = "TalkSelf_ChangeRumRation";


		Link.l7 = "Who am I looking for?";
  		Link.l7.go = "QueryCharacter";

	        }
	        // захват города
	        if (GetCharIDXForTownAttack(pchar.location) != -1)
	        {
	            Link.l8 = DLG_TEXT_BASE[23];
	    		Link.l8.go = "TalkSelf_TownAttack";
	        }
			Link.l10 = RandPhraseSimple(DLG_TEXT_BASE[24], DLG_TEXT_BASE[25]);
			Link.l10.go = "exit";
		break;

		case "TalkSelf_ChangeRumRation":
					Dialog.Text = "Now, how much to give em?";
	            Link.l1 = "Set the crew on half rations!";
	        		Link.l1.go = "TalkSelf_ChangeRumRation_half";
	            Link.l2 = "Set the crew on normal rations.";
	        		Link.l2.go = "TalkSelf_ChangeRumRation_norm";
	            Link.l3 = "Set the crew on double rations!";
	        		Link.l3.go = "TalkSelf_ChangeRumRation_double";
	        		Link.l4 = "On second thoughts, lets not.";
	        		Link.l4.go = "TalkSelf_Main";
		break;
		
		case "TalkSelf_ChangeRumRation_half":
					Dialog.Text = "Ok, half it is...";
					pchar.CrewPerRum = 50;
					Link.l1 = "Now, what else...";
	        		Link.l1.go = "TalkSelf_Main";
		break;

		case "TalkSelf_ChangeRumRation_norm":
					Dialog.Text = "Normal rations it is.";
					pchar.CrewPerRum = 40;
					Link.l1 = "Now, what else...";
	        		Link.l1.go = "TalkSelf_Main";
		break;

		case "TalkSelf_ChangeRumRation_double":
					Dialog.Text = "Ok, double rum!";
					pchar.CrewPerRum = 35;
					Link.l1 = "Now, what else...";
	        		Link.l1.go = "TalkSelf_Main";
		break;


		case "QueryCharacter":
    			dialog.text = "Who is this guy? Do I know their first name or their last name?";
    			Link.l1.edit = 1;
    			Link.l1.go = "QueryCharacter1";
  		break;
  		case "QueryCharacter1":
    			dialog.text = "";
    			int iCount = 0;
    			string strInput = dialogEditStrings[1];
    		for(i=0;i<MAX_CHARACTERS;i++){
      			ref rRoot = GetCharacter(i);
      			if(rRoot.id == "0")
        		continue;
      		if(rRoot.id == strInput || rRoot.name == strInput || rRoot.lastname == strInput ){
        		if(iCount > 0) dialog.text = dialog.text + "\n";
        	dialog.text = dialog.text+"# "+rRoot.name+"."+rRoot.lastname+" 0.id "+rRoot.id+" 1.index "+rRoot.index+" 2.sex "+rRoot.sex+" 3.nation "+rRoot.nation;
        	dialog.text = dialog.text+" 4.location "+rRoot.location+" { 0.group "+rRoot.location.group+" 1.locator "+rRoot.location.locator;
        	dialog.text = dialog.text+" } 5.ship { 0.type "+rRoot.ship.type+" 1.name "+rRoot.ship.name;
        	dialog.text = dialog.text+" } 6.city "+rRoot.city+" 7.cityshore "+rRoot.cityshore;
        	dialog.text = dialog.text+" 8.quest "+rRoot.quest+" { 0.targetcity "+rRoot.quest.targetcity+" 1.targetshore "+rRoot.quest.targetshore+" } 9.quest_date "+rRoot.quest_date+" # ";
        		iCount ++;
      		}
    	}
    	if(iCount == 0)
      		dialog.text = "Character " + strInput + " not found.";
    		Link.l1 = "Done";
    		Link.l1.go = "exit";
    		Link.l2 = "Seems like there is another one in my mind...";
    		Link.l2.go = "QueryCharacter";

		break;

		
		case "TalkSelf_room_night":
			NextDiag.CurrentNode = NextDiag.TempNode;
			//AddDialogExitQuestFunction("TavernWaitDate_Night");
			TavernWaitDate("wait_night");
			DialogExit_Self();
		break;

		case "TalkSelf_room_day":
			NextDiag.CurrentNode = NextDiag.TempNode;
			//AddDialogExitQuestFunction("TavernWaitDate_Day");
			TavernWaitDate("wait_day");
			DialogExit_Self();
		break;
		//  захват города
		case "TalkSelf_TownAttack":
            bOk = (GetPartyCrewQuantity(Pchar, true) >= 500) || bBettaTestMode;
			if (sti(Pchar.Ship.Type) != SHIP_NOTUSED && bOk)
	        {
	            chr = GetCharacter(GetCharIDXForTownAttack(pchar.location));
	            if (chr.from_sea == Pchar.location.from_sea)
	            {
					Dialog.Text = DLG_TEXT_BASE[26];
	    			Link.l1 = "...";
	    			Link.l1.go = "exit";
	            }
	            else
	            {
	                if (pchar.location == "Panama_ExitTown")
					{
						Dialog.Text = DLG_TEXT_BASE[27];
	    				Link.l1 = "...";
	    				Link.l1.go = "exit";
					}
					else
					{
						Dialog.Text = DLG_TEXT_BASE[28] + GetCityName(chr.City) + ".";
		    			Link.l1 = DLG_TEXT_BASE[29];
		    			Link.l1.go = "TalkSelf_TownAttack_Start";
		    			Link.l2 = DLG_TEXT_BASE[30];
		    			Link.l2.go = "exit";
					}
	    		}
	        }
	        else
	        {
	            Dialog.Text = DLG_TEXT_BASE[31];
	    		Link.l1 = "...";
	    		Link.l1.go = "exit";
			}
	    break;

		case "TalkSelf_TownAttack_Start":
	        NextDiag.CurrentNode = NextDiag.TempNode;

		    Pchar.GenQuestFort.fortCharacterIdx = GetCharIDXForTownAttack(pchar.location);
		    DeleteQuestAttribute("Union_with_Escadra");
	        chr = GetCharacter(sti(Pchar.GenQuestFort.fortCharacterIdx));
			if (isCityHasFort(chr.City))
			{
	        	SetLocationCapturedState(chr.from_sea, true);
	        }
	        else
	        {
	            SetLocationCapturedState(chr.Default, true); // для сухопутных
	        }
	        AddDialogExitQuest("Capture_Forts");
	        Ship_NationAgressive(chr, chr);
	        DialogExit_Self();
	    break;
	    // Зачислить рабов в команду  -->
		case "TalkSelf_SlavesToCrew":
	        if (GetCurCrewEscadr() >= GetMaxCrewAble())
	        {
	            Dialog.Text = DLG_TEXT_BASE[32];
	            Link.l1 = DLG_TEXT_BASE[33];
	    		Link.l1.go = "exit";
	        }
	        else
	        {
	            if (GetCrewQuantity(pchar) >= GetMaxCrewQuantity(pchar))
	            {
	                Dialog.Text = DLG_TEXT_BASE[34];
	                Link.l1 = DLG_TEXT_BASE[35];
	        		Link.l1.go = "exit";
	            }
	            else
	            {
	                if (GetFreeCrewQuantity(pchar) <= GetCargoGoods(pchar, GOOD_SLAVES))
	                {
	                    pchar.GenQuest.SlavesToCrew = GetFreeCrewQuantity(pchar);
	                }
	                else
	                {
	                    pchar.GenQuest.SlavesToCrew = GetCargoGoods(pchar, GOOD_SLAVES);
	                }
	                Dialog.Text = DLG_TEXT_BASE[36] + pchar.GenQuest.SlavesToCrew+ DLG_TEXT_BASE[37];
	                Link.l1 = DLG_TEXT_BASE[38];
	        		Link.l1.go = "TalkSelf_SlavesToCrew_1";
	        		Link.l2 = DLG_TEXT_BASE[39];
	        		Link.l2.go = "exit";
	    		}
			}
		break;

		case "TalkSelf_SlavesToCrew_1":
	        if (CheckOfficersPerk(pchar, "IronWill"))
	        {
	            AddCrewMorale(pchar, -makeint(sti(pchar.GenQuest.SlavesToCrew) / 5.0))
	        }
	        else
	        {
	            AddCrewMorale(pchar, -makeint(sti(pchar.GenQuest.SlavesToCrew) / 3.0))
	        }
	        // падение опыта -->
	        fTemp =  stf(GetCrewQuantity(pchar) + sti(pchar.GenQuest.SlavesToCrew));
	        pchar.Ship.Crew.Exp.Sailors   = (stf(pchar.Ship.Crew.Exp.Sailors)*GetCrewQuantity(pchar) + 
			                                        stf(pchar.Ship.Crew.Exp.Sailors)*0.3*sti(pchar.GenQuest.SlavesToCrew)) / fTemp;
			pchar.Ship.Crew.Exp.Cannoners   = (stf(pchar.Ship.Crew.Exp.Cannoners)*GetCrewQuantity(pchar) + 
			                                        stf(pchar.Ship.Crew.Exp.Cannoners)*0.3*sti(pchar.GenQuest.SlavesToCrew)) / fTemp;
			pchar.Ship.Crew.Exp.Soldiers   = (stf(pchar.Ship.Crew.Exp.Soldiers)*GetCrewQuantity(pchar) + 
			                                        stf(pchar.Ship.Crew.Exp.Soldiers)*0.3*sti(pchar.GenQuest.SlavesToCrew)) / fTemp;
			// падение опыта <-- 
			pchar.Ship.Crew.Quantity = sti(pchar.Ship.Crew.Quantity) + sti(pchar.GenQuest.SlavesToCrew); 
	        RemoveCharacterGoodsSelf(pchar, GOOD_SLAVES, sti(pchar.GenQuest.SlavesToCrew));       
			                            
	        NextDiag.CurrentNode = NextDiag.TempNode;
			DialogExit_Self();
		break;
		// Зачислить рабов в команду  <--
		// boal <--

//navy CONTRABAND METRO -->
		case "GenTravel_Main":
			NextDiag.TempNode = "First time";
			dialog.Text = DLG_TEXT_BASE[40];
			Link.l1 = DLG_TEXT_BASE[41];
			Link.l1.go = "exit";

			chr = CharacterFromID("Abracham_Gray");
			chr.location = "none";
			SeaExchangeCharactersShips(PChar, chr, false, false);
			SetCrewQuantity(PChar, 0);

			//грузим ГГ куда нужно...
			SetAnyReloadToLocation(pchar.GenQuest.contraTravel.destination.loc, pchar.GenQuest.contraTravel.destination.group, pchar.GenQuest.contraTravel.destination.locator, "", 0, 0, 0, 0);
			AddDialogExitQuest("AnyReloadToLocation");
            chrDisableReloadToLocation = false;
            
			CloseQuestHeader("Gen_ContrabandTravel");
			setCharacterShipLocation(PChar, pchar.GenQuest.contraTravel.destination.loc);
			setWDMPointXZ(pchar.GenQuest.contraTravel.destination.loc);

			//трем аттрибуты
			DeleteAttribute(PChar, "GenQuest.contraTravel");
			break;
//navy CONTRABAND METRO <--

		// Квест Аскольда, освобождение Аззи.
		case "FreedomAzzy":
			chr = GetCharacter(NPC_GenerateCharacter("Azzy", "Azzy", "man", "man", 22, PIRATE, -1, false));
			chr.name = DLG_TEXT_BASE[42];
			chr.lastname = DLG_TEXT_BASE[43];
			chr.Dialog.Filename = "Quest\Azzy_dialog.c";
			GiveItem2Character(chr, "blade28");// сабля Моргана
			chr.equip.blade = "blade28";
			chr.rank 	= 100;
			chr.reputation = "0";
			SetSelfSkill(chr, 100, 100, 100, 100, 100);
			SetShipSkill(chr, 100, 100, 100, 100, 100, 100, 100, 100, 100);
			LAi_SetImmortal(chr, true);
			LAi_SetActorType(chr);
			LAi_SetLoginTime(chr, 0.0, 24.0);
			LAi_SetHP(chr, 10000.0, 10000.0);
			chr.greeting = "Gr_Azzy";
			ChangeCharacterAddressGroup(chr, pchar.location, "officers", "reload1_1");
			DoQuestCheckDelay("Ascold_AzzyIsFree", 4.0);
			pchar.questTemp.Azzy = "Azzy_Freedom";
			DialogExit();
		break;	
	}
}

void  DialogExit_Self()
{
    DialogExit();
	locCameraSleep(false); //boal
}
