     //\\ INTERFACE MOD BY MAXIMUS //\\

//\\ INTERFACE FOR OFFICER STATUS WINDOW //\\

#define MAX_CHARACTER_SKILL		10

string sPlayerName;
string oldCurNode;

int nPlayerMoney;
int nPlayerLand;
int nNextExperience;
int nCurrentExperience;
int nFreeSkillPoints;
int nCurrentType;

int nLeadership;
int nFencing;
int nSailing;
int nAccuracy;
int nCannons;
int nGrappling;
int nRepair;
int nDefence;
int nCommerce;
int nSneak;

bool bBeParty;
int curOfficer;
ref xi_refMainChar;
ref xi_refCharacter;
ref xi_refNPchar;

int oldIndex = -1;

int ListMax;
ref MainChar;

ref PChar;

ref NPchar;

void InitInterface_R(string iniName,ref pCharacter)
{
	int i;
	MainChar = GetMainCharacter();
	FillCharactersList(pCharacter);

	bool  OfficerReturnFromPerk = false;

	xi_refMainChar = GetMainCharacter();
	xi_refCharacter = pCharacter;

	if (sti(pCharacter.index) != GetMainCharacterIndex())
	{
		if(CheckAttribute(MainChar,"CharacterInterface_ViewEncOfficer"))
		{
			GameInterface.View_Enc_Officer = "1";
			FillCharactersList_EncOfficerOnly(pCharacter);
		}
	}

	AddCharacterExp(xi_refCharacter,0);

//MAXIMUS interface MOD
	if(!CheckAttribute(GameInterface,"title"))
	{  
		GameInterface.title = true;

		if(CheckAttribute(xi_refCharacter,"quest.officertype") && CheckAttribute(GameInterface, "View_Enc_Officer"))
		{
			switch(xi_refCharacter.quest.officertype)
			{
				case "boatswain":
					GameInterface.title = "titleBoatswain";
				break;
				case "cannoneer": 
					GameInterface.title = "titleCannoneer";
				break;
				case "navigator": 
					GameInterface.title = "titleNavigator";
				break;
				case "first mate": 
					GameInterface.title = "titleFirstmate";
				break;
				case "quartermaster": 
					GameInterface.title = "titleQmaster";
				break;
				case "carpenter": 
					GameInterface.title = "titleCarpenter";
				break;
				case "doctor": 
					GameInterface.title = "titleDoctor";
				break;
				case "deck fighter": 
					GameInterface.title = "titleDeckfighter";
				break;
			}
		}
	}
	SetSelectable("EXIT_BUTTON",true);
//MAXIMUS interface MOD

    GameInterface.SkillChanger.current = 0;

    GameInterface.StatusLine.EXP.Max = CalculateExperienceFromRank(sti(xi_refCharacter.rank)+1);
    GameInterface.StatusLine.EXP.Min = CalculateExperienceFromRank(sti(xi_refCharacter.rank));
    GameInterface.StatusLine.EXP.Value = xi_refCharacter.Experience;

	if( CheckAttribute(&InterfaceStack,"CharacterView_pos") ) GameInterface.SkillChanger.current = InterfaceStack.CharacterView_pos;
    SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);
	CreateExitString();//MAXIMUS: standard exit-string for exit-button

	for(i=1;i<4;i++)	{if( GetOfficersIndex(pCharacter,i)>=0 || GetCompanionIndex(pCharacter,i)>=0 ) break;}
	if(i==4)
	{
		bBeParty = true;
	}
	else
	{
		bBeParty = true;
	}


//	If(Checkattribute(GameInterface, "View_Enc_Officer"))
//	{
		bBeParty = false;
		SetNodeUsing("LEFTCHANGE_CHARACTER",false);
		SetNodeUsing("RIGHTCHANGE_CHARACTER",false);
	    SetNodeUsing("B_SKILLUP",false);
//	}


	SetNodeUsing("B_ABILITIES",true);

    CreateString(true,"CurrentExperience","",FONT_NORMAL,COLOR_NORMAL,175,379,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"NextExperience","",FONT_NORMAL,COLOR_NORMAL,175,410,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"Money","",FONT_NORMAL,COLOR_MONEY,175,441,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"Land","",FONT_NORMAL,COLOR_NORMAL,175,472,SCRIPT_ALIGN_CENTER,1.0); // GreatZen
	CreateString(true,"CharacterName","",FONT_NORMAL,COLOR_NORMAL,156,50,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"Reputation","",FONT_NORMAL,COLOR_NORMAL,156,82,SCRIPT_ALIGN_CENTER,1.0);
	CreateString(true,"FreeAbilitiesPoints","",FONT_BOLD_NUMBERS,COLOR_NORMAL,444,396,SCRIPT_ALIGN_RIGHT,1.0);
	// MAXIMUS [new skill-change] -->
	CreateString(!AUTO_SKILL_SYSTEM,"FreeSkillPoints","",FONT_BOLD_NUMBERS,COLOR_NORMAL,610,396,SCRIPT_ALIGN_RIGHT,1.0);
	CreateString(!AUTO_SKILL_SYSTEM,"FreeSkill",XI_ConvertString("Free Skill Points"),FONT_NORMAL,COLOR_NORMAL,460,401,SCRIPT_ALIGN_LEFT,1.0);
	if(!AUTO_SKILL_SYSTEM) { CreateImage("FREESKILL","ICONS","black rectangle",454,396,616,428); }
	// MAXIMUS [new skill-change] -->
	CreateString(true,"Rank","",FONT_BOLD_NUMBERS,COLOR_NORMAL,268,324,SCRIPT_ALIGN_RIGHT,1.0);
	CreateString(true,"Salary","",FONT_NORMAL,COLOR_MONEY,521,471,SCRIPT_ALIGN_CENTER,1.0); // NK

    CreateString(true,SKILL_LEADERSHIP,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,46,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_FENCING,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,80,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_SAILING,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,114,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_ACCURACY,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,148,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_CANNONS,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,182,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_GRAPPLING,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,216,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_REPAIR,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,250,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_DEFENCE,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,284,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_COMMERCE,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,318,SCRIPT_ALIGN_RIGHT,1.0);
    CreateString(true,SKILL_SNEAK,"",FONT_BOLD_NUMBERS,COLOR_NORMAL,587,352,SCRIPT_ALIGN_RIGHT,1.0);

// added by MAXIMUS [new skill-change] -->
	if(AUTO_SKILL_SYSTEM)
	{
		int curY = 54;
		for(int c = 0; c < 10; c++)
		{		
			string curSkill = GetSkillName(c);
			if(CheckAttribute(xi_refCharacter,"skill."+curSkill))
			{
				string skillPercent = curSkill+"_Percent";
				CreateString(true,skillPercent,"",FONT_NORMAL,COLOR_MONEY,475,curY,SCRIPT_ALIGN_LEFT,0.8);
				if(CheckAttribute(xi_refCharacter,"skill."+curSkill+".bonus") && stf(xi_refCharacter.skill.(curSkill).bonus)>1.0 && sti(xi_refCharacter.skill.(curSkill))<10) { CreateImage("Bonus"+curSkill,"icons","complete",590,curY-10,620,curY+20); }
				curY = curY + 34;
			}
		}
	}
	SetCurrentNode("SKILLCHANGER");
// added by MAXIMUS [new skill-change] <--

    CreateString(true,"MaxHP","",FONT_NORMAL,COLOR_NORMAL,66,331,SCRIPT_ALIGN_LEFT,1.0);
	if(!Checkattribute(GameInterface, "View_Enc_Officer"))
	{
		MainChar.AnyCharacterShow = true;
		CreateStringCheckCase(true,"fakeTitle",xi_refCharacter.name + " " + xi_refCharacter.lastname,"interface_title",COLOR_NORMAL,320,5,SCRIPT_ALIGN_CENTER,1.0,true);
	}

	SetVariable();

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

void SetNextParty(bool bRight)
{
	string tempstring;
	int tempidx;

	if (bRight)
	{
		curOfficer++;
		if (curOfficer > ListMax) { curOfficer = 1; }
	}
	else
	{
		curOfficer--;
		if (curOfficer < 1) { curOfficer = ListMax; }
	}

	tempstring = "member" + curOfficer;
	tempidx = sti(MainChar.tempCharactersList.(tempstring).idx);
	xi_refCharacter = GetCharacter(tempidx);
	AddCharacterExp(xi_refCharacter,0);
}



void FillCharactersList(ref ViewingCharacter)
{
	int tempnum1;
	int tempnum2;
	int tempnum3;
	int tempnum4;
	int tempnum5;
	ref tempchar;
	string tempstring1;
	string tempstring2;
	string tempstring3;

	If (CheckAttribute(MainChar,"tempCharactersList"))
	{
		DeleteAttribute(MainChar,"tempCharactersList");
		if (TRACELOG == 1) { Trace("deleted existing list"); }
	}

	ListMax = 1;



	// ******* STEP 1/5: ADD MAIN CHARACTER *******

	MainChar.tempCharactersList.member1.idx = sti(MainChar.index);
	if (TRACELOG == 1) { Trace("added mainchar"); }
	ListMax++;



	// ******* STEP 2/5: ADD OFFICERS *******

	for (tempnum1 = 1; tempnum1 < 4; tempnum1++)
	{
		tempnum2 = GetOfficersIndex(MainChar,tempnum1);
		if (tempnum2 >= 0)
		{
			tempstring1 = "member" + ListMax;
			MainChar.tempCharactersList.(tempstring1).idx = tempnum2;
			if (TRACELOG == 1) { Trace("added list member " + ListMax + ": idx " + tempnum2 + " type 1"); }
			ListMax++;
		}
	}



	// ******* STEP 3/5: ADD COMPANIONS AND THEIR OFFICERS *******

	for (tempnum1 = 1; tempnum1 < 4; tempnum1++)
	{
		tempnum2 = GetCompanionIndex(MainChar,tempnum1);
		if (tempnum2 >= 0)
		{
			tempstring1 = "member" + ListMax;
			MainChar.tempCharactersList.(tempstring1).idx = tempnum2;
			if (TRACELOG == 1) { Trace("added list member " + ListMax + ": idx " + tempnum2 + " type 2"); }
			ListMax++;

			tempchar = GetCharacter(tempnum2);
			for (tempnum3 = 1; tempnum3 < 4; tempnum3++)
			{
				tempnum4 = GetOfficersIndex(tempchar,tempnum3);
				if (TRACELOG == 1) { Trace("checking officer " + tempnum3 + ", idx " + tempnum4 + ", of companion " + tempnum1 + ", idx " + tempnum2); }
				if (tempnum4 >= 0)
				{
					tempstring1 = "member" + ListMax;
					MainChar.tempCharactersList.(tempstring1).idx = tempnum4;
					if (TRACELOG == 1) { Trace("added list member " + ListMax + ": idx " + tempnum4 + " type 3"); }
					ListMax++;
				}
			}
		}
	}



	// ******* STEP 4/5: ADD NON-OFFICER PASSENGERS *******

	for (tempnum1 = 0; tempnum1 < GetPassengersQuantity(MainChar); tempnum1++)
	{
		tempnum2 = GetPassenger(MainChar,tempnum1);
		if (tempnum2 >= 0)
		{
			tempchar = GetCharacter(tempnum2);
			if (IsOfficer(tempchar) == false)
			{
				tempstring1 = "member" + ListMax;
				MainChar.tempCharactersList.(tempstring1).idx = tempnum2;
				if (TRACELOG == 1) { Trace("added list member " + ListMax + ": idx " + tempnum2 + " type 4"); }
				ListMax++;
			}
		}
	}



	// ******* STEP 5/5: ADD CAPTAINS ON SHORE LEAVE AND THEIR OFFICERS *******

	for (tempnum1 = 0; tempnum1 < 8; tempnum1++)
	{
		tempstring1 = "port" + tempnum1;
		for (tempnum2 = 1; tempnum2 < 5; tempnum2++)
		{
			tempstring2 = "slot" + tempnum2;
			if (sti(MainChar.ShipBerthing.(tempstring1).(tempstring2).status) == 2)
			{
				tempnum3 = sti(MainChar.ShipBerthing.(tempstring1).(tempstring2).captainindex);
				tempstring3 = "member" + ListMax;
				MainChar.tempCharactersList.(tempstring3).idx = tempnum3;
				if (TRACELOG == 1) { Trace("added list member " + ListMax + ": idx " + tempnum3 + " type 5"); }
				ListMax++;

				tempchar = GetCharacter(tempnum3);
				for (tempnum4 = 1; tempnum4 < 4; tempnum4++)
				{
					tempnum5 = GetOfficersIndex(tempchar,tempnum4);
					if (TRACELOG == 1) { Trace("checking officer " + tempnum4 + ", idx " + tempnum5 + ", of shore leave captain idx " + tempnum3); }
					if (tempnum5 >= 0)
					{
						tempstring1 = "member" + ListMax;
						MainChar.tempCharactersList.(tempstring1).idx = tempnum5;
						if (TRACELOG == 1) { Trace("added list member " + ListMax + ": idx " + tempnum5 + " type 6"); }
						ListMax++;
					}
				}
			}
		}
	}



	ListMax--;
	if (TRACELOG == 1) { Trace("list total: " + ListMax); }
	curOfficer = 1;
	xi_refCharacter = GetMainCharacter();

	tempnum3 = sti(ViewingCharacter.index);
	for (tempnum1 = 1; tempnum1 <= ListMax; tempnum1++)
	{
		tempstring1 = "member" + tempnum1;
		tempnum2 = sti(MainChar.tempCharactersList.(tempstring1).idx);
		if (tempnum2 == tempnum3)
		{
			curOfficer = tempnum1;
			xi_refCharacter = GetCharacter(tempnum2);
		}
	}

	AddCharacterExp(xi_refCharacter,0);
}



void FillCharactersList_EncOfficerOnly(ref tempEncOfficer)
{
	If (CheckAttribute(MainChar,"tempCharactersList"))
	{
		DeleteAttribute(MainChar,"tempCharactersList");
		if (TRACELOG == 1) { Trace("deleted existing list"); }
	}

	ListMax = 1;
	curOfficer = 1;
	
	MainChar.tempCharactersList.member1.idx = sti(tempEncOfficer.index);
	xi_refCharacter = GetCharacter(sti(MainChar.tempCharactersList.member1.idx));
	AddCharacterExp(xi_refCharacter,0);

	if (TRACELOG == 1) { Trace("added enc officer id " + sti(MainChar.tempCharactersList.member1.idx) + ". list total: " + ListMax); }
}

void SetVariable()
{
    sPlayerName = xi_refCharacter.name;

    if(!CheckAttribute(xi_refCharacter,"Money")) { xi_refCharacter.Money = GetRandCharMoney(xi_refCharacter, 10); }
	nPlayerMoney = sti(xi_refCharacter.Money);
    if(xi_refCharacter.id == xi_refMainChar.id) nPlayerMoney = sti(xi_refCharacter.wealth); // NK
    nPlayerLand = GetCharacterLand(xi_refCharacter); // RM
    nCurrentExperience = sti(xi_refCharacter.Experience);
	
    nNextExperience = CalculateExperienceFromRank(sti(xi_refCharacter.rank)+1);
    nFreeSkillPoints = sti(xi_refCharacter.Skill.FreeSkill);
	int nFreeAbilities = 0;
	if( CheckAttribute(xi_refCharacter,"perks.FreePoints") )
		nFreeAbilities = sti(xi_refCharacter.perks.FreePoints);

    nLeadership = sti(xi_refCharacter.Skill.Leadership);
    nFencing = sti(xi_refCharacter.Skill.Fencing);
    nSailing = sti(xi_refCharacter.Skill.Sailing);
    nAccuracy = sti(xi_refCharacter.Skill.Accuracy);
    nCannons = sti(xi_refCharacter.Skill.Cannons);
    nGrappling = sti(xi_refCharacter.Skill.Grappling);
    nRepair = sti(xi_refCharacter.Skill.Repair);
    nDefence = sti(xi_refCharacter.Skill.Defence);
    nCommerce = sti(xi_refCharacter.Skill.Commerce);
    nSneak = sti(xi_refCharacter.Skill.Sneak);

// added by MAXIMUS [new skill-change] -->
	if(AUTO_SKILL_SYSTEM)
	{
		for(int c = 0; c < 10; c++)
		{		
			string curSkill = GetSkillName(c);
			if(CheckAttribute(xi_refCharacter,"skill."+curSkill))
			{
				string skillPercent = curSkill+"_Percent";
				if(CheckAttribute(xi_refCharacter,"Experience."+curSkill)) GameInterface.strings.(skillPercent) = makeint(sti(xi_refCharacter.Experience.(curSkill))/500)+"%";
				else GameInterface.strings.(skillPercent) = 0+"%";
				if(sti(xi_refCharacter.skill.(curSkill))==10) DisableString(skillPercent);
				else EnableString(skillPercent);
			}
		}
	}
// added by MAXIMUS [new skill-change] <--

    GameInterface.StatusLine.EXP.Max = nNextExperience;
    GameInterface.StatusLine.EXP.Min = CalculateExperienceFromRank(sti(xi_refCharacter.rank));
    GameInterface.StatusLine.EXP.Value = nCurrentExperience;

    GameInterface.strings.CurrentExperience = nCurrentExperience;
    GameInterface.strings.NextExperience = nNextExperience;
    GameInterface.strings.Money = MakeMoneyShow(nPlayerMoney,MONEY_SIGN,MONEY_DELIVER);
	GameInterface.strings.Land = MakeMoneyShow(nPlayerLand,MONEY_SIGN,MONEY_DELIVER) + " " + XI_ConvertString("acr") + ".";
    GameInterface.strings.CharacterName =   stringRet(  CheckAttribute(xi_refCharacter,"name"),
                                                        xi_refCharacter.name + " ",
                                                        "Anonymous"
                                                     ) + 
                                            stringRet(  CheckAttribute(xi_refCharacter,"lastname"),
                                                        xi_refCharacter.lastname + " ",
                                                        "Coward"
                                                     );
    GameInterface.strings.Reputation = GetTranslatedReputationForSex(xi_refCharacter, GetReputationName(GetCharacterReputation(xi_refCharacter)));
	GameInterface.strings.FreeAbilitiesPoints = nFreeAbilities;
    GameInterface.strings.FreeSkillPoints = nFreeSkillPoints;
    GameInterface.strings.Rank = xi_refCharacter.rank;
	if(CheckAttribute(xi_refCharacter,"quest.officerprice") && bAllies(xi_refCharacter))
	{
		GameInterface.strings.Salary = XI_ConvertString("MonthlySalary") + ": " + makeint(sti(xi_refCharacter.quest.officerprice) * OFF_PRICE_SCALAR * CalcOffScalar() * SALARY_MULTIPLIER);//JA 26Nov06 used to have this * 10?? 
	}
	else { GameInterface.strings.Salary = XI_ConvertString(GetNationNameByType(sti(xi_refCharacter.nation))); }

	GameInterface.strings.MaxHP = MakeInt(LAi_GetCharacterHP(xi_refCharacter)) + "/" + MakeInt(LAi_GetCharacterMaxHP(xi_refCharacter));

	SetSkillShow(SKILL_LEADERSHIP,nLeadership);
	SetSkillShow(SKILL_FENCING,nFencing);
	SetSkillShow(SKILL_SAILING,nSailing);
	SetSkillShow(SKILL_ACCURACY,nAccuracy);
	SetSkillShow(SKILL_CANNONS,nCannons);
	SetSkillShow(SKILL_GRAPPLING,nGrappling);
	SetSkillShow(SKILL_REPAIR,nRepair);
	SetSkillShow(SKILL_DEFENCE,nDefence);
	SetSkillShow(SKILL_COMMERCE,nCommerce);
	SetSkillShow(SKILL_SNEAK,nSneak);

	SetNewPicture("FACEPICT", "interfaces\portraits\256\face_" + xi_refCharacter.FaceId + ".tga");

//	SetNewPicture("TYPEPICT", "");
	SetNodeUsing("TYPEPICT",xi_refCharacter.id!=xi_refMainChar.id);
//	if( xi_refCharacter.id == xi_refMainChar.id ) {
//		SetNewPicture("TYPEPICT", "");
//	} else {
		if ( IsCompanion(xi_refCharacter) ) SetNewPicture("TYPEPICT", "interfaces\blank_ship.tga");
		if ( IsOfficer(xi_refCharacter) )
		{
			if(CheckAttribute(xi_refCharacter,"quest.officertype"))
			{
				switch (xi_refCharacter.quest.officertype)
				{
					case "boatswain": SetNewPicture("TYPEPICT", "interfaces\order.tga"); break;
					case "deck fighter": SetNewPicture("TYPEPICT", "interfaces\fighter.tga"); break;
					case "fighter": SetNewPicture("TYPEPICT", "interfaces\fighter.tga"); break;
					case "cannoneer": SetNewPicture("TYPEPICT", "interfaces\gunner.tga"); break;
					case "quartermaster": SetNewPicture("TYPEPICT", "interfaces\quartermaster.tga"); break;
					case "navigator": SetNewPicture("TYPEPICT", "interfaces\navigator.tga"); break;
					case "first mate": SetNewPicture("TYPEPICT", "interfaces\officer.tga"); break;
					case "carpenter": SetNewPicture("TYPEPICT", "interfaces\carpenter.tga"); break;
					case "doctor": SetNewPicture("TYPEPICT", "interfaces\doctor.tga"); break;	
				}
			}
		}
		if ( IsOfficerCompanion(xi_refCharacter) ) SetNewPicture("TYPEPICT", "interfaces\kam_isofficercompanion.tga");
		if ( IsOnShoreLeave(xi_refCharacter) ) SetNewPicture("TYPEPICT", "interfaces\kam_isonshoreleave.tga");
		if ( IsOfficerOnShoreLeave(xi_refCharacter) ) SetNewPicture("TYPEPICT", "interfaces\kam_isofficeronshoreleave.tga");
//	}

	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_MSG_TO_NODE,"exp",0);
	oldIndex = -1;
}

void ProcessCancelExit()
{
	interfaceResultCommand = RC_INTERFACE_OFFICER_EXIT;

	DeleteAttribute(MainChar,"tempCharactersList");
	if(CheckAttribute(MainChar,"CharacterInterface_ViewEncOfficer")) { DeleteAttribute (MainChar,"CharacterInterface_ViewEncOfficer"); }


	if (CheckAttribute(GameInterface,"View_Enc_Officer")) {DeleteAttribute(GameInterface, "View_Enc_Officer") ; CharInterfaceExit(true);}


	else
	{
		if( CheckAttribute(&InterfaceStates,"InstantExit") && sti(InterfaceStates.InstantExit)==true )
		{
			CharInterfaceExit(true);
		}
		else
		{
			CharInterfaceExit(true);
        }
    }

}

void CharInterfaceExit(bool flag)
{
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
    DelEventHandler("InterfaceCancel","ProcessCancelExit");
    DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("ievnt_command","ProcessCommandExecute");
	DelEventHandler("frame","ProcessFrame");

	WriteNewData();
    EndCancelInterface(flag);
	if(flag) { if(CheckAttribute(GetMainCharacter(),"AnyCharacterShow")) { DeleteAttribute(GetMainCharacter(),"AnyCharacterShow"); } }
}

void WriteNewData()
{
    xi_refCharacter.Skill.FreeSkill = nFreeSkillPoints;
    xi_refCharacter.Skill.Leadership = nLeadership;
    xi_refCharacter.Skill.Fencing = nFencing;
    xi_refCharacter.Skill.Sailing = nSailing;
    xi_refCharacter.Skill.Accuracy = nAccuracy;
    xi_refCharacter.Skill.Cannons = nCannons;
    xi_refCharacter.Skill.Grappling = nGrappling;
    xi_refCharacter.Skill.Repair = nRepair;
    xi_refCharacter.Skill.Defence = nDefence;
    xi_refCharacter.Skill.Commerce = nCommerce;
    xi_refCharacter.Skill.Sneak = nSneak;
}


void IncreaseSkill()
{
    if(nFreeSkillPoints>0)
    {
        switch( sti(GameInterface.SkillChanger.current) )
        {
        case 1:
			if(nLeadership==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nLeadership<MAX_CHARACTER_SKILL)
			{
				nLeadership++;
				SetSkillShow(SKILL_LEADERSHIP,nLeadership);
				SetNodeUsing("B_SKILLUP",nLeadership<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 2:
			if(nFencing==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nFencing<MAX_CHARACTER_SKILL)
			{
				nFencing++;
				SetSkillShow(SKILL_FENCING,nFencing);
				SetNodeUsing("B_SKILLUP",nFencing<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 3:
			if(nSailing==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nSailing<MAX_CHARACTER_SKILL)
			{
				nSailing++;
				SetSkillShow(SKILL_SAILING,nSailing);
				SetNodeUsing("B_SKILLUP",nSailing<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 4:
			if(nAccuracy==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nAccuracy<MAX_CHARACTER_SKILL)
			{
				nAccuracy++;
				SetSkillShow(SKILL_ACCURACY,nAccuracy);
				SetNodeUsing("B_SKILLUP",nAccuracy<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 5:
			if(nCannons==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nCannons<MAX_CHARACTER_SKILL)
			{
				nCannons++;
				SetSkillShow(SKILL_CANNONS,nCannons);
				SetNodeUsing("B_SKILLUP",nCannons<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 6:
			if(nGrappling==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nGrappling<MAX_CHARACTER_SKILL)
			{
				nGrappling++;
				SetSkillShow(SKILL_GRAPPLING,nGrappling);
				SetNodeUsing("B_SKILLUP",nGrappling<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 7:
			if(nRepair==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nRepair<MAX_CHARACTER_SKILL)
			{
				nRepair++;
				SetSkillShow(SKILL_REPAIR,nRepair);
				SetNodeUsing("B_SKILLUP",nRepair<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 8:
			if(nDefence==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nDefence<MAX_CHARACTER_SKILL)
			{
				nDefence++;
				SetSkillShow(SKILL_DEFENCE,nDefence);
				SetNodeUsing("B_SKILLUP",nDefence<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 9:
			if(nCommerce==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nCommerce<MAX_CHARACTER_SKILL)
			{
				nCommerce++;
				SetSkillShow(SKILL_COMMERCE,nCommerce);
				SetNodeUsing("B_SKILLUP",nCommerce<MAX_CHARACTER_SKILL);
			}
			else return;
        break;

        case 10:
			if(nSneak==0 && !ENABLE_SKILLUP_IFZERO)	{return;}
			if(nSneak<MAX_CHARACTER_SKILL)
			{
				nSneak++;
				SetSkillShow(SKILL_SNEAK,nSneak);
				SetNodeUsing("B_SKILLUP",nSneak<MAX_CHARACTER_SKILL);
			}
			else return;
        break;
        }
        nFreeSkillPoints--;
		GameInterface.strings.FreeSkillPoints = nFreeSkillPoints;
    }
}

int SetAlphaIntoColor(int col, int alpha)
{
	int newCol = and(col,16777215) + shl(alpha,24);
	return newCol;
}

int GetAlphaFromSkill(int nskill)
{
	switch(nskill)
	{
	case 0:		return 55;	break;
	case 1:		return 75;	break;
	case 2:		return 95;	break;
	case 3:		return 115;	break;
	case 4:		return 135;	break;
	case 5:		return 155;	break;
	case 6:		return 175;	break;
	case 7:		return 195;	break;
	case 8:		return 215;	break;
	case 9:		return 235;	break;
	case 10:	return 255;	break;
	}
	return 0;
}

void SetSkillShow(string skillName, int skillVal)
{
	xi_refCharacter.Skill.(skillName) = skillVal;
	GameInterface.strings.(skillName) = skillVal + " - " + GetCharacterSkill(&xi_refCharacter,skillName);
	int color = COLOR_NORMAL;
	if(skillVal==SKILL_MAX)	color = COLOR_MONEY;
	ChangeStringColor(skillName,SetAlphaIntoColor(color,GetAlphaFromSkill(skillVal)));
}

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

	int curChanger = 11;
	if(AUTO_SKILL_SYSTEM) { curChanger = 1; }

	int x;ref tmpch;string tmpstr;

	switch(nodName)
	{
	case "LEFTCHANGE_CHARACTER":
		if(comName=="click")
		{
			if(bBeParty)
			{
				WriteNewData();
				SetNextParty(false);
				SetVariable();
			}
		}
	break;

	case "RIGHTCHANGE_CHARACTER":
		if(comName=="click")
		{
			if(bBeParty)
			{
				WriteNewData();
				SetNextParty(true);
				SetVariable();
			}
		}
	break;

	case "SKILLCHANGER":
		if(sti(GameInterface.SkillChanger.current)==0)
		{
			if(comName=="rightstep" && bBeParty)
			{
				WriteNewData();
				SetNextParty(true);
				SetVariable();
			}
			if(comName=="leftstep" && bBeParty)
			{
				WriteNewData();
				SetNextParty(false);
				SetVariable();
			}
		}
		else
		{
			if(sti(GameInterface.SkillChanger.current)==curChanger)//11 - MAXIMUS [new skill-change]
			{
				if(comName=="activate")
				{
					GameInterface.AbilityChr = xi_refCharacter.index;
					PostEvent("LaunchIAfterFrame",1,"sl", "AbilityView", 1);
					interfaceResultCommand = RC_INTERFACE_OFFICER_EXIT;
					CharInterfaceExit(false);
					return;
				}
			}
			else
			{
				if(comName=="activate")
				{	IncreaseSkill();
				}
			}
		}

	break;

	case "B_SKILLUP":
		if(comName=="click")
		{
			if(!CheckAttribute(MainChar,"CharacterInterface_ViewEncOfficer"))
			{
				if(sti(GameInterface.SkillChanger.current)<curChanger)//11 - MAXIMUS [new skill-change]
				{
					IncreaseSkill();
					oldIndex = -1;
				}
			}
		}
	break;

	case "B_ABILITIES":
		if(comName=="click")
		{
			GameInterface.AbilityChr = xi_refCharacter.index;
			PostEvent("LaunchIAfterFrame",1,"sl", "AbilityView", 1);
			interfaceResultCommand = RC_INTERFACE_OFFICER_EXIT;
			CharInterfaceExit(false);
			return;
		}
	break;

	case "CHANGE_OUTFIT":
		if(comName=="click")
		{
			MainChar.ChangeOutfitFromCharacterScreen = xi_refCharacter.index;
			PostEvent("LaunchIAfterFrame",1,"sl", "ChangeOutfit", 1);
			interfaceResultCommand = RC_INTERFACE_OFFICER_EXIT;
			CharInterfaceExit(false);
			return;
		}
	break;
	}

}

void ProcessFrame()
{
	int curChanger = 11;
	if(AUTO_SKILL_SYSTEM) { curChanger = 1; }
	int newIndex = sti(GameInterface.SkillChanger.current);
	if(oldIndex!=newIndex)
	{
		bool bSetBtn = false;
		oldIndex = newIndex;
		if(newIndex>0)
		{
			if(nFreeSkillPoints>0)
			{
				if(ENABLE_SKILLUP_IFZERO)
				{
					switch(newIndex)
					{
					case 1:		if(nLeadership<MAX_CHARACTER_SKILL)     {bSetBtn=true;}	break;
					case 2:		if(nFencing<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 3:		if(nSailing<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 4:		if(nAccuracy<MAX_CHARACTER_SKILL)	    {bSetBtn=true;}	break;
					case 5:		if(nCannons<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 6:		if(nGrappling<MAX_CHARACTER_SKILL)	    {bSetBtn=true;}	break;
					case 7:		if(nRepair<MAX_CHARACTER_SKILL)		    {bSetBtn=true;}	break;
					case 8:		if(nDefence<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 9:		if(nCommerce<MAX_CHARACTER_SKILL)	    {bSetBtn=true;}	break;
					case 10:	if(nSneak<MAX_CHARACTER_SKILL)			{bSetBtn=true;}	break;
					}
				}else {
				switch(newIndex)
					{
					case 1:		if(nLeadership>0 && nLeadership<MAX_CHARACTER_SKILL){bSetBtn=true;}	break;
					case 2:		if(nFencing>0 && nFencing<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 3:		if(nSailing>0 && nSailing<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 4:		if(nAccuracy>0 && nAccuracy<MAX_CHARACTER_SKILL)	{bSetBtn=true;}	break;
					case 5:		if(nCannons>0 && nCannons<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 6:		if(nGrappling>0 && nGrappling<MAX_CHARACTER_SKILL)	{bSetBtn=true;}	break;
					case 7:		if(nRepair>0 && nRepair<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 8:		if(nDefence>0 && nDefence<MAX_CHARACTER_SKILL)		{bSetBtn=true;}	break;
					case 9:		if(nCommerce>0 && nCommerce<MAX_CHARACTER_SKILL)	{bSetBtn=true;}	break;
					case 10:	if(nSneak>0 && nSneak<MAX_CHARACTER_SKILL)			{bSetBtn=true;}	break;
					}
				}
			}
			if(bSetBtn)
			{
				int pic_top = 12 + 34*newIndex;
				int pic_bottom = 44 + 34*newIndex;
				SendMessage(&GameInterface,"lsllllll", MSG_INTERFACE_MSG_TO_NODE,"B_SKILLUP",0, 478,pic_top,510,pic_bottom, 0); // NK was 528,560
			}
		}
		if(Checkattribute(GameInterface, "View_Enc_Officer")) SetNodeUsing("B_ABILITIES",newIndex==curChanger);//11 - MAXIMUS [new skill-change]
		SetNodeUsing("B_SKILLUP",false);
	}
}

void EnhancedSetNextParty(bool bRight)
{
	int i,cn;
	int CmpQ = GetCompanionQuantity(xi_refMainChar)-1;
	int PsgQ = GetPassengersQuantity(xi_refMainChar);
	int PrtQ = makeint(CmpQ+PsgQ);

	if(bRight) { curOfficer++; }
	else { curOfficer--; }

	if(curOfficer>PrtQ) curOfficer=0;
	if(curOfficer<0) curOfficer=PrtQ;

	if(curOfficer<=CmpQ) { cn = GetCompanionIndex(xi_refMainChar,curOfficer); }
	else { cn = GetPassenger(xi_refMainChar,curOfficer-(CmpQ+1)); }

	if(curOfficer!=0) { xi_refCharacter = GetCharacter(cn); }
	else { xi_refCharacter = xi_refMainChar; }
	AddCharacterExp(xi_refCharacter,0);
}
