
native string LanguageGetLanguage();
native int LanguageOpenFile(string sFileName);
native void LanguageCloseFile(int idLngFile);
native string LanguageConvertString(int userLngFileID, string inStr);
native void LanguageSetLanguage(string sLanguageName);
native string XI_ConvertString(string inStr);
native int GlobalLngFileID();
native string LanguageGetFaderPic(string faderPicName);

native void XI_SetColorCorrection(float fContrast, float fGamma, float fBrightness);
native void XI_SetMouseSensitivity( float fMouseXSens, float fMouseYSens );
native void XI_ControlMakeInvert(string sControlName, int bInvertState);

#libriary "script_interface_functions"

void CreateImage(string AttrName,string imgListName,string imgName,int left,int top,int right,int bottom)
{
	GameInterface.pictures.(AttrName).tex = imgListName;
    GameInterface.pictures.(AttrName).pic = imgName;
    SendMessage(&GameInterface,"lslllll",MSG_INTERFACE_PLACE_IMAGE,AttrName,true,left,top,right,bottom);
}

void CreateAbsoluteImage(string AttrName,string imgListName,string imgName,int left,int top,int right,int bottom)
{
	GameInterface.pictures.(AttrName).tex = imgListName;
    GameInterface.pictures.(AttrName).pic = imgName;
    SendMessage(&GameInterface,"lslllll",MSG_INTERFACE_PLACE_IMAGE,AttrName,false,left,top,right,bottom);
}

void SetPictureBlind(string picName,bool blindFlag,int minCol,int maxCol)
{
	SendMessage(&GameInterface,"lslll",MSG_INTERFACE_SET_BLIND,picName,blindFlag,minCol,maxCol);
}

void CreateString(int enable,string strName, string strData, string fontName, int color, int x, int y, int alignment, float scale)
{
    GameInterface.strings.(strName) = strData;
    SendMessage(&GameInterface,"lssllllf",MSG_INTERFACE_CREATE_STRING, strName, fontName, color, x,y,alignment,scale);
    if(enable==FALSE)
    {
        SendMessage(&GameInterface,"ls", MSG_INTERFACE_DISABLE_STRING, strName);
    }
}

void DisableString(string strName)
{
    SendMessage(&GameInterface,"ls", MSG_INTERFACE_DISABLE_STRING, strName);
}

void EnableString(string strName)
{
    SendMessage(&GameInterface,"ls", MSG_INTERFACE_ENABLE_STRING, strName);
}

void ChangeStringColor(string strName, int newCol)
{
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_CHANGE_STR_COLOR,strName,newCol);
}

void SetNodeUsing(string nodeName,int bUsing)
{
    SendMessage(&GameInterface,"lsl", MSG_INTERFACE_SET_NODE_USING, nodeName, bUsing);
}

void SetCurrentNode(string nodeName)
{
    SendMessage(&GameInterface,"ls", MSG_INTERFACE_NEW_CURRENT_NODE,nodeName);
}
string GetCurrentNode()
{
	string retVal;
	SendMessage(&GameInterface,"le", MSG_INTERFACE_GET_CURRENT_NODE,&retVal);
	return retVal;
}

void ShowHelpString(string helpString)
{
    SendMessage(&GameInterface,"ls", MSG_INTERFACE_SETHELPSTRING, helpString);
}

string MakeMoneyShow(int moneyQuantity,string msign,string deliver)
{
	string moneyStr;
	int i,j;
	moneyStr="";
	while(moneyQuantity>999)
	{
		i = moneyQuantity/1000;
		j = moneyQuantity-i*1000;
		if(j>99)
		{
			moneyStr = deliver + j + moneyStr;
		}
		else
		{
			if(j>9)
			{
				moneyStr = deliver+"0" + j + moneyStr;
			}
			else
			{
				moneyStr = deliver+"00" + j + moneyStr;
			}
		}
		moneyQuantity = i;
	}
	moneyStr = msign+moneyQuantity+moneyStr;
	return moneyStr;
}

// NK -->
string ShowMoney(int money)
{
	return MakeMoneyShow(money, MONEY_SIGN, MONEY_DELIVER);
}

// moved here from ship.c and updated for KNB. 05-04-10
string GetCannonName(int ct)
{
	if(ct==CANNON_TYPE_NONECANNON)	return XI_ConvertString("NoneCannons");
	ref cref = GetCannonByType(ct);
	string cname, retVal;
	// KNB -->
	if(USE_REAL_CANNONS)
	{
		switch(sti(cref.type))
		{
			case CANNON_NAME_CARRONADE: cname = XI_ConvertString("Carronade"); break;
			cname = XI_ConvertString("Long Gun");
		}
	}
	// KNB <--
	else
	{
		if(cref.type==CANNON_NAME_CULVERINE)	cname = XI_ConvertString("Culverine");
		else	cname = XI_ConvertString("Cannon");
	}
	retVal = XI_ConvertString("caliber"+cref.caliber) + " " + cname;
	return retVal;
}

// NK <--
//MAXIMUS -->
void ChangePicture(string nodeName, string pictureFileName, int bUsing)
{
	if(bUsing) { SetNewPicture(nodeName, pictureFileName); }
}
//MAXIMUS <--
void SetNewPicture(string _NodeName,string _NewPictureFileName)
{
	SendMessage(&GameInterface,"lslls", MSG_INTERFACE_MSG_TO_NODE,_NodeName,2, false, _NewPictureFileName);
}
void SetNewVideoPicture(string _NodeName,string _NewVideoPicFileName)
{
	SendMessage(&GameInterface,"lslls", MSG_INTERFACE_MSG_TO_NODE,_NodeName,2, true, _NewVideoPicFileName);
}

void SetSelectable(string _NodeName,int _Selectable)
{
	SendMessage(&GameInterface,"lsl", MSG_INTERFACE_SET_SELECTABLE, _NodeName, _Selectable);
	if(_NodeName=="EXIT_BUTTON")//MAXIMUS: for standard exit-button -->
	{
		if(!_Selectable) ChangeStringColor("EXIT", COLOR_DESELECT);
		else ChangeStringColor("EXIT", COLOR_NORMAL);
	}//MAXIMUS <--
}
int GetSelectable(string _NodeName)
{
	return SendMessage(&GameInterface,"ls", MSG_INTERFACE_GET_SELECTABLE, _NodeName);
}

void SetClickable(string _NodeName, bool ClickStatus)
{
	SendMessage(&GameInterface,"lslll", MSG_INTERFACE_MSG_TO_NODE,_NodeName,-1,1, ClickStatus);
}
bool GetClickable(string _NodeName)
{
	return SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,_NodeName,-1,2);
}

void DelFromScroll(string nodeName,int pnum)
{
	int qn = sti(GameInterface.(nodeName).ListSize)-1;
	aref inar,outar;
	string attrName;
	for(int i=pnum; i<qn; i++)
	{
		attrName = "pic"+(i+1);
		makearef(inar,GameInterface.(nodeName).(attrName));
		attrName = "pic"+(i+2);
		makearef(outar,GameInterface.(nodeName).(attrName));
		CopyAttributes(inar,outar);
	}
	attrName = "pic"+(i+1);
	DeleteAttribute(&GameInterface,nodeName+"."+attrName);
	if(qn>=0) GameInterface.(nodeName).ListSize = qn;
	SendMessage(&GameInterface,"lsl", MSG_INTERFACE_DEL_SCROLLIMAGE, nodeName, pnum);
}

void AddToScroll(string nodeName,int pnum)
{
	SendMessage(&GameInterface,"lsl", MSG_INTERFACE_ADD_SCROLLIMAGE, nodeName, pnum);
}

// KK -->
/*void XI_SetQuestTitles(string nodName,aref questInfo,int topNum)
{
	SendMessage(&GameInterface,"lsal",MSG_INTERFACE_SET_TITLE_STRINGS,nodName,questInfo,topNum);
}*/
// <-- KK

// PS -->
string GetShipString(int stype)
{
	if(stype < 0) return "";
	if(stype >= SHIP_TYPES_QUANTITY_WITH_FORT) return ""; //also checks for st == SHIP_NOTUSED
	return ShipsTypes[stype].SName;
}

string GetShipPicName(int stype)
{
	if(stype == SHIP_NOTUSED) return SHIP_NOTUSED_TYPE_NAME;
	return ShipsTypes[stype].BigPic;
}

int GetShipTexture(int stype)
{
	if(stype < 0) return -1;
	if(stype >= SHIP_TYPES_QUANTITY_WITH_FORT) return -1; //also checks for st == SHIP_NOTUSED
	/*if(shipType=="Galeoth1") return 1;
	if(shipType=="Galeoth2") return 1;
	return 0;*/
	return sti(ShipsTypes[stype].BigPicTex);
}

string GetShipTextureName(int stype)
{
	if(stype < 0) return "";
	if(stype >= SHIP_TYPES_QUANTITY_WITH_FORT) return ""; //also checks for st == SHIP_NOTUSED
	/*if(shipType=="Galeoth1") return "SHIPS1";
	if(shipType=="Galeoth2") return "SHIPS1";
	return "SHIPS16";*/
	return ShipsTypes[stype].BigPicTexName;
}
// PS <--

string GetFaceGroupName(int charIdx)
{
	if(charIdx<0) {return "EMPTYFACE";}
	return "FACE128_"+Characters[charIdx].FaceID;
}

string GetFacePictureName(int charIdx)
{
	if(charIdx<0) return "emptyface";
	return "face";
}

// NK -->
int GetFacePicture(int idx)
{
	if(idx < 0) return -1; // NK 04-11
    // Scheffnow - bfd
	//return Characters[idx].faceID;
	if(CheckAttribute(Characters[idx],"faceID")) return sti(Characters[idx].faceID);
}

string GetFaceGroupNameFromFace(int Idx)
{
	if(Idx<0) {return "EMPTYFACE";}
	return "FACE128_"+Idx;
}

// NK <--

void SetFormatedText(string sNodeName, string sText)
{
	SendMessage(&GameInterface,"lss",MSG_INTERFACE_SET_FORMATEDTEXT,sNodeName,sText);
}

//NK/Inez
string FloatToString(float fl,int nDigAfterPoint)
{
	float fmul = pow(10.0,nDigAfterPoint);
	fl = fl + 0.5/fmul;
	int p1 = makeint(fl);
	int p2 = makeint((fl-p1)*fmul);
     string minusstr = ""; if(fl < 0) minusstr = "-"; 
	return minusstr + abs(p1)+"."+abs(p2);
}

int GetStringWidth(string str, string fontID, float fScale)
{
	return SendMessage(&GameInterface, "lssf", MSG_INTERFACE_GET_STRWIDTH, str, fontID, fScale);
}

string GetItemPictureName(string itemName)
{
	aref arItm;
	if( Items_FindItem(itemName,&arItm)>=0 )
	{
		if( CheckAttribute(arItm,"picIndex") )
		{
			return	"itm"+sti(arItm.picIndex);
		}
	}
	return "cannon back";
}

string GetItemTextureName(string itemName)
{
	aref arItm;
	if( Items_FindItem(itemName,&arItm)>=0 )
	{
		if( CheckAttribute(arItm,"picTexture") )
		{
			return	arItm.picTexture;
		}
	}
	return "ICONS";
}

int GetItemPictureTexture(string accessString, string itemName)
{
	aref arItm,arGroup;
	makearef(arGroup,GameInterface.(accessString));
	string sTexName = "ICONS";

	if( Items_FindItem(itemName,&arItm)>=0 )
	{	if( CheckAttribute(arItm,"picTexture") )
		{	sTexName = arItm.picTexture;
		}
	}
	int q = GetAttributesNum(arGroup);
	for(int i=0; i<q; i++)
	{	if( GetAttributeValue(GetAttributeN(arGroup,i)) == sTexName )
		{	return i;
		}
	}
	return -1;
}

void FillImagesGroupForItems(aref arImgGrp)
{
	int i,n,q;
	string stmp;
	object objtmp;
	for(i=0; i<ITEMS_QUANTITY; i++)
	{
		if( CheckAttribute( &Items[i], "picTexture" ) && Items[i].picTexture!="" )
		{
			stmp = Items[i].picTexture;
			objtmp.(stmp) = true;
		}
	}
	q = GetAttributesNum(&objtmp);
	n = GetAttributesNum(arImgGrp)+1;
	for(i=0; i<q; i++)
	{
		stmp = "t"+n;
		arImgGrp.(stmp) = GetAttributeName(GetAttributeN(&objtmp,i));
		n++;
	}
}

string GetFacePicName(ref chref)
{
	return "face";
}

void FillFaceList(string strAccess, ref chref, int fillCode)
{
	aref ar;
	makearef(ar, GameInterface.(strAccess));

	int n, cn, q;

	AddFaceGroup(strAccess,"FACE128_"+chref.faceID);

	if(fillCode==0) // officers
	{
		for(n=1; n<4; n++)
		{
			cn = GetOfficersIndex(chref,n);
			if( cn>=0 ) {
				AddFaceGroup(strAccess,"FACE128_"+Characters[cn].faceID);
			}
		}
	}

	if(fillCode==1) // companions
	{
		for(n=1; n<4; n++)
		{
			cn = GetCompanionIndex(chref,n);
			if( cn>=0 ) {
				AddFaceGroup(strAccess,"FACE128_"+Characters[cn].faceID);
			}
		}
	}

	if(fillCode==2) // passengers
	{
		q = GetPassengersQuantity(chref);
		for(n=0; n<q; n++)
		{
			cn = GetPassenger(chref,n);
			if( cn>=0 ) {
				AddFaceGroup(strAccess,"FACE128_"+Characters[cn].faceID);
			}
		}
	}
}

void AddFaceGroup(string strAccess, string groupName)
{
	if( FindFaceGroupNum(strAccess,groupName)<0 )
	{
		aref ar;
		makearef(ar, GameInterface.(strAccess));
		string attrName = "t"+GetAttributesNum(ar);
		ar.(attrName) = groupName;
	}
}

int FindFaceGroupNum(string strAccess, string groupName)
{
	int n, q;
	aref aRoot, aCur;
	makearef(aRoot, GameInterface.(strAccess));

	q = GetAttributesNum(aRoot);
	for(n=0; n<q; n++)
	{
		aCur = GetAttributeN(aRoot,n);
		if( GetAttributeValue(aCur) == groupName ) return n;
	}

	return -1;
}

string GlobalStringConvert(string strID)
{
	return LanguageConvertString( GlobalLngFileID(), strID );
}

// added by MAXIMUS - changes strings to uppercase for using INTERFACE_TITLE with any strings in interfaces -->
void CreateStringCheckCase(int enable, string strName, string strData, string strFont, int color, int x, int y, int alignment, float scale, bool bTranslated)
{
	string strAlfa = "";
	for(int g=0; g<strlen(strData); g++)
	{
		if(strFont=="interface_title")
		{
			if(bTranslated)
			{
				strAlfa += toupper(strcut(strData,g,g)); // KK
			}
			else strAlfa += touppereng(strcut(strData,g,g)); // KK
		}
		else strAlfa += strcut(strData,g,g);
	}
	CreateString(enable,strName,strAlfa,strFont,color,x,y,alignment,scale);
}
// added by MAXIMUS - changes strings to uppercase for using INTERFACE_TITLE with any strings in interfaces <--

// KK -->
void SetMainCharacterName(string newName)
{
	CharFirstName = "";
	CharOldFirstName = "";
	CharMiddleName = "";
	CharOldMiddleName = "";
	CharLastName = "";
	CharOldLastName = "";

	string part1 = "";
	string part2 = "";
	if (DivideString(newName, " ", &part1, &part2)) {
		CharFirstName = part1;
		CharOldFirstName = part1;
	} else {
		CharLastName = part1;
		CharOldLastName = part1;
		return;
	}
	newName = part2;
	while (DivideString(newName, " ", &part1, &part2)) {
		if (part1 == "de" || part1 == "van" || part1 == "da" || part1 == "la" || part1 == "di" || part1 == "o" || part1 == "von") break;
		if (CharMiddleName == "")
			CharMiddleName = part1;
		else
			CharMiddleName = CharMiddleName + " " + part1;
		CharOldMiddleName = CharMiddleName;
		newName = part2;
	}
	CharLastName = newName;
	CharOldLastName = newName;
}

void SetCharacterName(ref chr, string newName)
{
	chr.name = "";
	chr.old.name = "";
	chr.middlename = "";
	chr.old.middlename = "";
	chr.lastname = "";
	chr.old.lastname = "";

	string part1 = "";
	string part2 = "";
	if (DivideString(newName, " ", &part1, &part2)) {
		chr.name = part1;
		chr.old.name = part1;
	} else {
		DeleteAttribute(chr, "middlename");
		DeleteAttribute(chr, "old.middlename");
		CharLastName = part1;
		CharOldLastName = part1;
		return;
	}
	newName = part2;
	while (DivideString(newName, " ", &part1, &part2)) {
		if (part1 == "de" || part1 == "van" || part1 == "da" || part1 == "la" || part1 == "di" || part1 == "o" || part1 == "von") break;
		if (chr.middlename == "")
			chr.middlename = part1;
		else
			chr.middlename = chr.middlename + " " + part1;
		chr.old.middlename = chr.middlename;
		newName = part2;
	}
	chr.lastname = newName;
	chr.old.lastname = newName;
	if (chr.middlename == "") {
		DeleteAttribute(chr, "middlename");
		DeleteAttribute(chr, "old.middlename");
	}
}
// <-- KK
