
// **********************************************************
//
// NationRelation.c
//
// this whole thing overhauled by KAM ^_^
//
// **********************************************************


int nCurScroll;
ref PChar;
int PCharNation;
string PCharName;

void InitInterface(string iniName)
{
	string tempstring;
	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");

	PChar = GetMainCharacter();
	PCharNation = sti(PChar.Nation); // KK
	if (CheckAttribute(PChar,"firstname"))
	{
		PCharName = PChar.firstname;
	}
	else
	{
		PCharName = PChar.name;
	}

	GameInterface.title = "titleKAMNations";

	FillScroll();
	SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);
	CreateExitString();//MAXIMUS: standard exit-string for exit-button

	CreateString(true,"NationName","",FONT_NORMAL,COLOR_NORMAL,320,130,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"RelationsWith","",FONT_SMALL,COLOR_NORMAL,457,204,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"RelationsWithMainChr","",FONT_SMALL,COLOR_NORMAL,150,204,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"MainChrNation",PCharName + " - " +LanguageConvertString(tmpLangFileID,"'s current colours") + ":",FONT_SMALL,COLOR_NORMAL,35,365,SCRIPT_ALIGN_LEFT,1.0);
	CreateString(true,"RaiseColours",LanguageConvertString(tmpLangFileID,"Raise") + ":",FONT_SMALL,COLOR_NORMAL,265,365,SCRIPT_ALIGN_LEFT,1.0);
	CreateString(true,"KnownAsTitle",PCharName + " " + LanguageConvertString(tmpLangFileID,"is known as") + ":",FONT_SMALL,COLOR_NORMAL,35,421,SCRIPT_ALIGN_LEFT,1.0);
	CreateString(true,"KnownAs",XI_ConvertString(GetRankNameDirect(GetMainCharacter(), PIRATE,GetRankFromPoints(GetScore(GetMainCharacter())))),FONT_NORMAL,COLOR_NORMAL,190,416,SCRIPT_ALIGN_LEFT,1.0);

// KK -->
	if (ENABLE_FLAGS == 0) {
		CreateString(true,"ActualRelation","",FONT_NORMAL,COLOR_NORMAL,150,305,SCRIPT_ALIGN_CENTER,1.0);
	} else {
		CreateString(true, "ActualRelation", "", FONT_NORMAL, COLOR_NORMAL, 150, 295, SCRIPT_ALIGN_CENTER, 1.0);
		CreateString(true, "FlagRelation", "", FONT_SMALL, COLOR_NORMAL, 150, 325, SCRIPT_ALIGN_CENTER, 1.0);
		SendMessage(&GameInterface, "lsls", MSG_INTERFACE_MSG_TO_NODE, "NEUTRALCOLOURS_BUTTON", 0, XI_ConvertString("Personal"));
	}

	SetSelectable("RAISECOLOURS_BUTTON", bChangeNation);
	SetSelectable("NEUTRALCOLOURS_BUTTON", bChangeNation);
	if (ALLOW_NATION_CHANGE != 0 && PCharNation == PERSONAL_NATION) SetSelectable("NEUTRALCOLOURS_BUTTON", false);
	if (ENABLE_FLAGS != 0 && PCharNation == PERSONAL_NATION) { SetSelectable("NEUTRALCOLOURS_BUTTON", false); } // TIH cannot change to same personal flag Nov2'06
// <-- KK

	if (DEBUG == 1) { SetNodeUsing("UNKNOWN_BUTTON",true); SendMessage(&GameInterface,"lsls",MSG_INTERFACE_MSG_TO_NODE,"UNKNOWN_BUTTON",0,XI_ConvertString("Unknown")); SetNodeUsing("CATALONIA_BUTTON",true); SendMessage(&GameInterface,"lsls",MSG_INTERFACE_MSG_TO_NODE,"CATALONIA_BUTTON",0,XI_ConvertString("Catalan")); }

	KAM_UpdateDisplay();

// MAXIMUS interface MOD -->
	SetSelectable("I_SHIPHOLD",!LAi_IsBoardingProcess() && !LAi_group_IsActivePlayerAlarm());
	SetSelectable("I_TRADEBOOK",!LAi_IsBoardingProcess() && !LAi_group_IsActivePlayerAlarm());
	SetSelectable("I_QUESTBOOK",!LAi_IsBoardingProcess() && !LAi_group_IsActivePlayerAlarm());
	//SetSelectable("I_SHIPLOG",!LAi_IsBoardingProcess() && !LAi_group_IsActivePlayerAlarm());
	SetSelectable("I_SHIPLOG",false);
	SetSelectable("EXIT_BUTTON",true);
// MAXIMUS interface MOD <--

	SetEventHandler("InterfaceBreak","ProcessCancelExit",0);
	SetEventHandler("frame","FrameProcess",1);
	SetEventHandler("ievnt_command","ProcessCommandExecute",0);
	SetEventHandler("exitCancel","ProcessCancelExit",0);
}

void FrameProcess()
{
	if(nCurScroll!=sti(GameInterface.islandslist.current))
	{
		nCurScroll = sti(GameInterface.islandslist.current);
		KAM_UpdateDisplay();
	}
}

void KAM_UpdateDisplay()
{
	string tempstring;
	string tempstring2;
	int tempnum;
	bool tempbool;

	GameInterface.strings.NationName = XI_ConvertString(GetNationNameByType(nCurScroll));

// KK -->
	if (ENABLE_FLAGS == 0) {
		tempbool = false;
		if (GetRMRelation(GetMainCharacter(), nCurScroll) >= REL_AMNESTY)	{	tempbool = bChangeNation;	}
	} else {
		tempbool = bChangeNation;//true;
		if (bSeaActive && bMapEnter)
			ALLOW_NATION_CHANGE = 1;
		else
			ALLOW_NATION_CHANGE = 0;
		if (!bSeaActive)
			ALLOW_NATION_CHANGE = 1;
	}
// <-- KK
	if (nCurScroll == PIRATE)				{	tempbool = bChangeNation;	}
	if (DEBUG == 1)						{	tempbool = bChangeNation;	}
	if (nCurScroll == PCharNation)				{	tempbool = false;	}
// KK -->
	if (ENABLE_FLAGS == 0) {
		if (ALLOW_NATION_CHANGE == 1) {	SetSelectable("RAISECOLOURS_BUTTON",tempbool); }
	} else {
		if ((ALLOW_NATION_CHANGE == 1) || (nCurScroll == PIRATE)) {
			SetSelectable("RAISECOLOURS_BUTTON",tempbool);
		} else {
			SetSelectable("RAISECOLOURS_BUTTON", false);
		}
	}

	GameInterface.strings.RelationsWith = XI_ConvertString("Diplomatic relations of") + XI_ConvertString("gen_" + GetNationNameByType(nCurScroll)) + XI_ConvertString("'s relations with") + ":";
	GameInterface.strings.RelationsWithMainChr = XI_ConvertString("Attitude to") + XI_ConvertString("gen_" + GetNationNameByType(nCurScroll)) + XI_ConvertString("'s attitude towards") + " " + PCharName;

	string tmprelation;
	if (ENABLE_FLAGS == 0) {
		switch(GetRMRelationName(GetMainCharacter(),nCurScroll))
		{
			case "Neutral":
				tmprelation = XI_ConvertString("Neutral");
				if(GetNationRelation2MainCharacter(nCurScroll)==RELATION_FRIEND) tmprelation = XI_ConvertString("Friendly");
			break;
			tmprelation = XI_ConvertString(GetRMRelationName(GetMainCharacter(),nCurScroll));
		}
		GameInterface.strings.ActualRelation = tmprelation + " (" + makeint(GetRMRelation(GetMainCharacter(),nCurScroll)) + ")";
		switch (PCharNation)
		{
			case NEUTRAL_NATION:	tempstring = "neutral_nation";			break;
			case UNKNOWN_NATION:	tempstring = "unknown_nation";			break;
			//default:
			tempstring = GetNationNameByType(PCharNation);	break;
		}
		CreateImage("MainChrNationFlag","SMALL_NATION_ICONS",tempstring,220,355,260,395);

		switch (GetNationRelation2MainCharacter(nCurScroll))
		{
			case RELATION_ENEMY:	tempstring = "enemy";		break;
			case RELATION_NEUTRAL:	tempstring = "neutral";		break;
			case RELATION_FRIEND:	tempstring = "friend";		break;
		}	
		CreateImage("MainChrRelations","RELATIONS",tempstring,110,225,185,300);
		SendMessage(&GameInterface,"lsls",MSG_INTERFACE_MSG_TO_NODE,"RAISECOLOURS_BUTTON",0,XI_ConvertString("adj_" + GetNationDescByType(nCurScroll)));
	} else {
		ref PChar = GetMainCharacter();
		string ARMRelName = GetActualRMRelationName(PChar, nCurScroll);
		int ANR2PChar = GetActualNationRelation2MainCharacter(nCurScroll);
		int ARMRel = GetActualRMRelation(nCurScroll);

		switch(ARMRelName)
		{
			case "Neutral":
				tmprelation = XI_ConvertString("Neutral");
				if (ANR2PChar == RELATION_FRIEND)
					tmprelation = XI_ConvertString("Friendly");
			break;
			tmprelation = XI_ConvertString(ARMRelName);
		}
		GameInterface.strings.ActualRelation = tmprelation + " (" + makeint(ARMRel) + ")";
		switch (PCharNation)
		{
			case PERSONAL_NATION:	tempstring = "PERSONAL_NATION";			break;
			case UNKNOWN_NATION:	tempstring = "unknown_nation";			break;
			//default:
			tempstring = GetNationNameByType(PCharNation);	break;
		}
		CreateImage("MainChrNationFlag","SMALL_NATION_ICONS",tempstring,220,355,260,395);

		switch (ANR2PChar)
		{
			case RELATION_ENEMY:	tempstring = "enemy";		break;
			case RELATION_NEUTRAL:	tempstring = "neutral";		break;
			case RELATION_FRIEND:	tempstring = "friend";		break;
		}
		CreateImage("MainChrRelations","RELATIONS",tempstring,110,225,185,300);


		if ((PCharNation != PERSONAL_NATION) && (bRecognized == false)) {
			if (PCharNation == nCurScroll) {
				tempstring = "Friendly";
			} else {
				switch (GetNationRelation(PCharNation, nCurScroll))
				{
					case RELATION_ENEMY:	tempstring = "Hostile";		break;
					case RELATION_FRIEND:	tempstring = "Neutral";		break;
				}
			}
		} else {
			switch (ANR2PChar)
			{
				case RELATION_ENEMY:	tempstring = "Hostile";		break;
				case RELATION_NEUTRAL:	tempstring = "Neutral";		break;
			}
			if (ARMRel > 0)
				tempstring = "Friendly";
		}
		GameInterface.strings.FlagRelation = XI_ConvertString("Relation based on flag") + ": " + XI_ConvertString(tempstring);

		SendMessage(&GameInterface,"lsls",MSG_INTERFACE_MSG_TO_NODE,"RAISECOLOURS_BUTTON",0,XI_ConvertString("adj_" + GetNationDescByType(nCurScroll)));
	}
// <-- KK


	string IntRel_FlagImages[5];
	string IntRel_RelImages[5];
	int tempnum2 = 0;

	SetNodeUsing("NATIONS_MOUNT",true);
	SetNodeUsing("NATIONLINE",true);
	for (tempnum = 0; tempnum < MAX_NATIONS; tempnum++)
	{
		if (tempnum == nCurScroll) { continue; }

		IntRel_FlagImages[tempnum2] = GetNationNameByType(tempnum);

		switch(GetNationRelation(nCurScroll,tempnum))
		{
			case RELATION_ENEMY:	IntRel_RelImages[tempnum2] = "enemy";		break;
			case RELATION_NEUTRAL:	IntRel_RelImages[tempnum2] = "neutral";		break;
			case RELATION_FRIEND:	IntRel_RelImages[tempnum2] = "friend";		break;
		}

		tempnum2++;
	}

	CreateImage("IntRel_Flag1","SMALL_NATION_ICONS",IntRel_FlagImages[0],310,235,350,277)
	CreateImage("IntRel_Rel1","RELATIONS",IntRel_RelImages[0],355,235,395,277)

	CreateImage("IntRel_Flag2","SMALL_NATION_ICONS",IntRel_FlagImages[1],310,285,350,327)
	CreateImage("IntRel_Rel2","RELATIONS",IntRel_RelImages[1],355,285,395,327)

	CreateImage("IntRel_Flag3","SMALL_NATION_ICONS",IntRel_FlagImages[2],415,235,455,277)
	CreateImage("IntRel_Rel3","RELATIONS",IntRel_RelImages[2],460,235,500,277)

	CreateImage("IntRel_Flag4","SMALL_NATION_ICONS",IntRel_FlagImages[3],415,285,455,327)
	CreateImage("IntRel_Rel4","RELATIONS",IntRel_RelImages[3],460,285,500,327)

	CreateImage("IntRel_Flag5","SMALL_NATION_ICONS",IntRel_FlagImages[4],520,235,560,277)
	CreateImage("IntRel_Rel5","RELATIONS",IntRel_RelImages[4],565,235,605,277)

}

void KAM_SetColours(int ReqdNation)
{
	int i;

// KK -->
	PChar.nation = ReqdNation;
	if (ENABLE_FLAGS == 1) {
		HoistFlag(ReqdNation);
		if (IsEntity(&BattleInterface)) SetRelationsForFlag(ReqdNation);
	}
// <-- KK
	for(i=1; i<4; i++)
	{
		if(GetCompanionIndex(PChar, i) >= 0 && GetRemovable(GetCharacter(GetCompanionIndex(PChar, i))))
		{
			Characters[GetCompanionIndex(PChar, i)].nation = PChar.nation;
		}
	}
	PCharNation = sti(Pchar.nation); // KK
	KAM_UpdateDisplay();
	SetCurrentNode("ISLANDSLIST");			// added after build 11 by KAM

// KK -->
	if (ENABLE_FLAGS == 0) {
		if (ReqdNation == NEUTRAL_NATION && CheckAttribute(PChar,"Viper_SailsTypeDesign") && sti(PChar.Viper_SailsTypeDesign) == 1)
		{
			pchar.Viper_SailsType = "default";
			DeleteAttribute(PChar,"Viper_SailsTypeEmblemedFile");
			DeleteAttribute(PChar,"Viper_SailsTypeNormalFile");
			DeleteAttribute(PChar,"Viper_SailsTypeType");
			DeleteAttribute(PChar,"Viper_SailsTypeColour");
			DeleteAttribute(PChar,"Viper_SailsTypeDesign");
			DeleteAttribute(PChar,"Viper_SailsTypeNumber");
		}
	} else {
		if (ReqdNation == PERSONAL_NATION && CheckAttribute(PChar,"Viper_SailsTypeDesign") && sti(PChar.Viper_SailsTypeDesign) == 1)
		{
			pchar.Viper_SailsType = "default";
			DeleteAttribute(PChar,"Viper_SailsTypeEmblemedFile");
			DeleteAttribute(PChar,"Viper_SailsTypeNormalFile");
			DeleteAttribute(PChar,"Viper_SailsTypeType");
			DeleteAttribute(PChar,"Viper_SailsTypeColour");
			DeleteAttribute(PChar,"Viper_SailsTypeDesign");
			DeleteAttribute(PChar,"Viper_SailsTypeNumber");
		}
	}
// <-- KK
}

void ProcessCommandExecute()
{
	string comName = GetEventData();
	string nodName = GetEventData();

// MAXIMUS interface MOD -->

	int x;ref tmpch;string tmpstr;
	if(nodName=="I_CHARACTER")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
	if(nodName=="I_SHIP")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
	if(nodName=="I_SHIPHOLD")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
	if(nodName=="I_PASSENGER")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
	if(nodName=="I_QUESTBOOK")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
	if(nodName=="I_TRADEBOOK")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
	if(nodName=="I_SHIPLOG")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
	if(nodName=="I_ITEMS")
	{
		if(comName=="activate" || comName=="click")
		{
			PostEvent("LaunchIAfterFrame",1,"sl", nodName, 2);
			InterfaceStack.SelectMenu_node = nodName;
			interfaceResultCommand = RC_INTERFACE_NATIONRELATION_EXIT;
			EndCancelInterface(false);
		}
	}
// MAXIMUS interface MOD <--

	if(comName=="activate" || comName=="click")
	{
		if (nodName == "RAISECOLOURS_BUTTON")
		{
// KK -->
			if (ENABLE_FLAGS == 1)
				SetSelectable("NEUTRALCOLOURS_BUTTON", true);
// <-- KK
			KAM_SetColours(nCurScroll);
		}
		if (nodName == "NEUTRALCOLOURS_BUTTON")
		{
// KK -->
			if (ENABLE_FLAGS == 0) {
				KAM_SetColours(NEUTRAL_NATION);
			} else {
				SetSelectable("NEUTRALCOLOURS_BUTTON", false);
				KAM_SetColours(PERSONAL_NATION);
			}
// <-- KK
		}
		if (nodName == "UNKNOWN_BUTTON")
		{
			KAM_SetColours(UNKNOWN_NATION);
		}
		if (nodName == "CATALONIA_BUTTON")
		{
			KAM_SetColours(CATALONIA);
		}
	}
}

void FillScroll()
{
	int i;
	string attributename1;
	string attributename2;

	GameInterface.islandslist.ImagesGroup.t1 = "NATIONS_1";
	GameInterface.islandslist.ImagesGroup.t2 = "NATIONS_2";

	GameInterface.islandslist.pic1.img1 = "England";
	GameInterface.islandslist.pic1.tex1 = 1;

	GameInterface.islandslist.pic2.img1 = "France";
	GameInterface.islandslist.pic2.tex1 = 1;

	GameInterface.islandslist.pic3.img1 = "Spain";
	GameInterface.islandslist.pic3.tex1 = 0;

	GameInterface.islandslist.pic4.img1 = "Pirate";
	GameInterface.islandslist.pic4.tex1 = 1;

	GameInterface.islandslist.pic5.img1 = "Holland";
	GameInterface.islandslist.pic5.tex1 = 1;

	GameInterface.islandslist.pic6.img1 = "Portugal";
	GameInterface.islandslist.pic6.tex1 = 0;

	GameInterface.islandslist.ListSize = 6;

	nCurScroll = PCharNation;
	if (nCurScroll < 0 || nCurScroll >= sti(GameInterface.islandslist.ListSize)) { nCurScroll = 0; }
	GameInterface.islandslist.current = nCurScroll;
}

void ProcessCancelExit()
{
	IDoExit(RC_INTERFACE_NATIONRELATION_EXIT);
}

void IDoExit(int exitCode)
{
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
	DelEventHandler("frame","FrameProcess");
	DelEventHandler("exitCancel","ProcessCancelExit");

    interfaceResultCommand = exitCode;
// MAXIMUS interface MOD -->
	EndCancelInterface(true);/*
	if( CheckAttribute(&InterfaceStates,"InstantExit") && sti(InterfaceStates.InstantExit)==true )
	{
		EndCancelInterface(true);
	}
	else
	{
		PostEvent("LaunchIAfterFrame",1,"sl", "I_SELECTMENU", 1);
		EndCancelInterface(false);
	}*/
// MAXIMUS interface MOD <--
	if (bSeaActive) RefreshBattleInterface(); // TIH helps to refresh minimap colors if relation changed Aug27'06
}
