
void OSL_WriteGameOption()
{
	object gopt;
	PrepareDefaultOption(&gopt);
	GetRealOptions(&gopt);
	SaveSavedOptions(&gopt);
}

void OSL_ReadGameOption()
{
	object gopt;

	PrepareDefaultOption(&gopt);
	ReadSavedOptions(&gopt);
	SetCurentOptions(&gopt);
}

void PrepareDefaultOption(ref optref)
{
	optref.volume.music = 0.5;
	optref.volume.sound = 0.5;
	optref.volume.dialog = 0.5;
	optref.gameoption.musicscheme = 0; // KK
	optref.cameramode.follow_on = true;
	optref.arcademode.arcade_on = true;
// added by MAXIMUS -->
	optref.gameoption.newinterface_on = false;
	optref.gameoption.newicons_on = false;

	optref.gameoption.interfacerus_on = false;
	optref.gameoption.interfacefra_on = false;
	optref.gameoption.interfaceger_on = false;
	optref.gameoption.interfacespa_on = false;
	optref.gameoption.interfacepol_on = false; // KK

	optref.gameoption.animation_on = false;
	GetBuildSettingsData(optref);
// added by MAXIMUS <--
	optref.gameoption.btlinterface_on = false;// ccc
}

void GetRealOptions(ref optref)
{
	float ftmp1,ftmp2,ftmp3;

	ftmp1 = stf(optref.volume.sound);
	ftmp2 = stf(optref.volume.music);
	ftmp3 = stf(optref.volume.dialog);
	SendMessage(&sound,"leee", MSG_SOUND_GET_MASTER_VOLUME, &ftmp1, &ftmp2,	&ftmp3 );
	optref.volume.sound = ftmp1;
	optref.volume.music = ftmp2;
	optref.volume.dialog = ftmp3;
	optref.gameoption.musicscheme = music_scheme; // KK
	UpdateSound(); // KK

// added by MAXIMUS -->
	optref.gameoption.newinterface_on = bNewInterface;
	optref.gameoption.newicons_on = bNewIcons;

	optref.gameoption.interfacerus_on = bInterfaceRus;
	optref.gameoption.interfacefra_on = bInterfaceFra;
	optref.gameoption.interfaceger_on = bInterfaceGer;
	optref.gameoption.interfacespa_on = bInterfaceSpa;
	optref.gameoption.interfacepol_on = bInterfacePol; // KK

	optref.gameoption.animation_on = bAnimation;
	GetBuildSettingsData(optref);
// added by MAXIMUS <--
	optref.gameoption.btlinterface_on = bRealBattleInterface;// ccc

	optref.arcademode.arcade_on = bArcadeMode;

	optref.cameramode.follow_on = !locCameraEnableSpecialMode;

	if( CheckAttribute(&InterfaceStates,"InvertCameras") ) {
		optref.cameramode.InvertCameras = sti(InterfaceStates.InvertCameras);
	} else {
		optref.cameramode.InvertCameras = false;
	}

	GetControlsOptions(optref);

	// mouse
	if( CheckAttribute(&InterfaceStates,"mouse.x_sens") ) {
		optref.mouse.x_sensitivity = InterfaceStates.mouse.x_sens;
	} else {
		optref.mouse.x_sensitivity = 0.5;
	}
	if( CheckAttribute(&InterfaceStates,"mouse.y_sens") ) {
		optref.mouse.y_sensitivity = InterfaceStates.mouse.y_sens;
	} else {
		optref.mouse.y_sensitivity = 0.5;
	}
	// video colors
	if( CheckAttribute(&InterfaceStates,"video.contrast") ) {
		optref.video.contrast = InterfaceStates.video.contrast;
	} else {
		optref.video.contrast = 1.0;
	}
	if( CheckAttribute(&InterfaceStates,"video.gamma") ) {
		optref.video.gamma = InterfaceStates.video.gamma;
	} else {
		optref.video.gamma = 1.0;
	}
	if( CheckAttribute(&InterfaceStates,"video.brightness") ) {
		optref.video.brightness = stf(InterfaceStates.video.brightness); // LDH - eliminate garbage at end of options file
	} else {
		optref.video.brightness = 0.0;
	}
}

void SetCurentOptions(ref optref)
{
// changed by MAXIMUS -->
	float ftmp1,ftmp2,ftmp3;

	ftmp1 = stf(optref.volume.sound);
	ftmp2 = stf(optref.volume.music);
	ftmp3 = stf(optref.volume.dialog);
	SendMessage(&sound,"lfff", MSG_SOUND_SET_MASTER_VOLUME,	&ftmp1, &ftmp2, &ftmp3);
// changed by MAXIMUS <--
// KK -->
	music_scheme = sti(optref.gameoption.musicscheme);
	if (music_scheme > 0 && CheckDirectory("RESOURCE\Sounds\MUSIC\\" + GetMusicSchemeResDir(), "*.ogg") == 0) {
		music_scheme = 0;
		optref.gameoption.musicscheme = music_scheme;
		SaveSavedOptions(optref);
	}
// <-- KK

// added by MAXIMUS -->
	bNewInterface = sti(optref.gameoption.newinterface_on);
	bNewIcons = sti(optref.gameoption.newicons_on);

	bInterfaceRus = sti(optref.gameoption.interfacerus_on);
	bInterfaceFra = sti(optref.gameoption.interfacefra_on);
	bInterfaceGer = sti(optref.gameoption.interfaceger_on);
	bInterfaceSpa = sti(optref.gameoption.interfacespa_on);
	bInterfacePol = sti(optref.gameoption.interfacepol_on); // KK
// KK -->
	if (bInterfaceRus) {
		if (FindLocalLanguage("Russian")) {
			LanguageSetLanguage("Russian");
		} else {
			LanguageSetLanguage("English");
			bInterfaceRus = false;
			optref.gameoption.interfacerus_on = false;
			SaveSavedOptions(optref);
		}
	}
	if (bInterfaceFra) {
		if (FindLocalLanguage("French")) {
			LanguageSetLanguage("French");
		} else {
			LanguageSetLanguage("English");
			bInterfaceFra = false;
			optref.gameoption.interfacefra_on = false;
			SaveSavedOptions(optref);
		}
	}
	if (bInterfaceGer) {
		if (FindLocalLanguage("German")) {
			LanguageSetLanguage("German");
		} else {
			LanguageSetLanguage("English");
			bInterfaceGer = false;
			optref.gameoption.interfaceger_on = false;
			SaveSavedOptions(optref);
		}
	}
	if (bInterfaceSpa) {
		if (FindLocalLanguage("Spanish")) {
			LanguageSetLanguage("Spanish");
		} else {
			LanguageSetLanguage("English");
			bInterfaceSpa = false;
			optref.gameoption.interfacespa_on = false;
			SaveSavedOptions(optref);
		}
	}
	if (bInterfacePol) {
		if (FindLocalLanguage("Polish")) {
			LanguageSetLanguage("Polish");
		} else {
			LanguageSetLanguage("English");
			bInterfacePol = false;
			optref.gameoption.interfacepol_on = false;
			SaveSavedOptions(optref);
		}
	}
// <-- KK

	bAnimation = sti(optref.gameoption.animation_on);
	UpdateBuildSettingsData(optref);
// added by MAXIMUS <--
	bRealBattleInterface = sti(optref.gameoption.btlinterface_on);// ccc

	bArcadeMode = sti(optref.arcademode.arcade_on);
	locCameraEnableSpecialMode = !sti(optref.cameramode.follow_on);

	// mouse
	if( CheckAttribute(optref,"cameramode.InvertCameras") ) {
		InterfaceStates.InvertCameras = optref.cameramode.InvertCameras;
	} else {
		InterfaceStates.InvertCameras = false;
	}
	if( CheckAttribute(optref,"mouse.x_sensitivity") ) {
		InterfaceStates.mouse.x_sens = optref.mouse.x_sensitivity;
	} else {
		InterfaceStates.mouse.x_sens = 0.5;
	}
	if( CheckAttribute(optref,"mouse.y_sensitivity") ) {
		InterfaceStates.mouse.y_sens = optref.mouse.y_sensitivity;
	} else {
		InterfaceStates.mouse.y_sens = 0.5;
	}
	// video colors
	if( CheckAttribute(optref,"video.contrast") ) {
		InterfaceStates.video.contrast = optref.video.contrast;
	} else {
		InterfaceStates.video.contrast = 1.0;
	}
	if( CheckAttribute(optref,"video.gamma") ) {
		InterfaceStates.video.gamma = optref.video.gamma;
	} else {
		InterfaceStates.video.gamma = 1.0;
	}
	if( CheckAttribute(optref,"video.brightness") ) {
		InterfaceStates.video.brightness = optref.video.brightness;
	} else {
		InterfaceStates.video.brightness = 0.0;
	}
	XI_SetColorCorrection(stf(InterfaceStates.video.contrast),stf(InterfaceStates.video.gamma),stf(InterfaceStates.video.brightness));

	aref arControls;
	makearef(arControls,optref.controls);
	RestoreKeysFromOptions(arControls);

	if( GetTargetPlatform()=="pc" )
	{
		ControlsMakeInvert();
		SetRealMouseSensitivity();
	}
}

void ReadSavedOptions(ref gopt)
{
	string optionsPath = "options";
	if(CheckAttribute(&PlayerProfile,"name") && PlayerProfile.name!=DEFAULT_NAME)
	{
		if(FindFile(DEFAULT_PATH,"*",PlayerProfile.name+"_options")!="") optionsPath = DEFAULT_PATH+"\"+PlayerProfile.name+"_options";
	}
	SendMessage(&GameInterface, "lsa", MSG_INTERFACE_LOADOPTIONS, optionsPath, gopt);
}

void SaveSavedOptions(ref gopt)
{
	string optionsPath = "options";
	if(CheckAttribute(&PlayerProfile,"name") && PlayerProfile.name!=DEFAULT_NAME) optionsPath = DEFAULT_PATH+"\"+PlayerProfile.name+"_options";
	SendMessage(&GameInterface, "lsa", MSG_INTERFACE_SAVEOPTIONS, optionsPath, gopt);
	SendMessage(&GameInterface, "lsa", MSG_INTERFACE_SAVEOPTIONS, "options", gopt);//default
}

void GetControlsOptions(ref optref)
{
	int ng,qg, nc,qc;
	aref arIn, arOut, arGrp, arCntrl;
	string grName, cntrlName;

	makearef(arOut,objControlsState.keygroups);
	optref.controls = true;
	makearef(arIn,optref.controls);

	qg = GetAttributesNum(arOut);
	for(ng=0; ng<qg; ng++)
	{
		arGrp = GetAttributeN(arOut,ng);
		grName = GetAttributeName(arGrp);
		if( CheckAttribute(&objControlsState,"grouplist."+grName) && sti(objControlsState.grouplist.(grName))==true )
		{
			qc = GetAttributesNum(arGrp);
			for(nc=0; nc<qc; nc++)
			{
				arCntrl = GetAttributeN(arGrp,nc);
				if( CheckAttribute(arCntrl,"remapping") && sti(arCntrl.remapping)==true )
				{
					cntrlName = GetAttributeName(arCntrl);
					arIn.(grName).(cntrlName) = GetAttributeValue(arCntrl);
					arIn.(grName).(cntrlName).state = arCntrl.state;
				}
			}
		}
	}
}
