// DEFINES
#define MUSIC_CHANGE_TIME  3000
#define MUSIC_SILENCE_TIME 180000.0
#define SOUNDS_FADE_TIME   200

// KK -->
object registered_music_schemes;
int music_scheme = 0;
// <-- KK

#event_handler ("LoadSceneSound", "LoadSceneSound")
// PLAY
int Play3DSound(string name, float x, float y, float z)
{
	InitSound();
	//Trace("Play3DSound : "+name);
	return SendMessage(Sound,"lsllllllfff",MSG_SOUND_PLAY, Sound_GetName(name), SOUND_WAV_3D, VOLUME_FX, false, false, false, 0, x, y, z); // KK
}

int Play3DSoundCached(string name, float x, float y, float z)
{
	InitSound();
	//Trace("Play3DSoundCached : "+name);
	return SendMessage(Sound,"lsllllllfff",MSG_SOUND_PLAY, Sound_GetName(name), SOUND_WAV_3D, VOLUME_FX, false, false, true, 0, x, y, z); // KK
}

int Play3DSoundComplex(string name, float x, float y, float z, bool bLooped, bool bCached)
{
	InitSound();
	return SendMessage(Sound,"lsllllllfff",MSG_SOUND_PLAY, Sound_GetName(name), SOUND_WAV_3D, VOLUME_FX, false, bLooped, bCached, 0, x, y, z); // KK
}

int PlayStereoSound(string name)
{
	InitSound();
	//Trace("PlayStereoSound : "+name);
	return SendMessage(Sound,"lslllll",MSG_SOUND_PLAY, Sound_GetName(name), SOUND_WAV_STEREO, VOLUME_FX, false, false, false); // KK
}

int PlayStereoSoundLooped(string name)
{
	InitSound();
	//Trace("PlayStereoSoundLooped : "+name);
	return SendMessage(Sound,"lsllll",MSG_SOUND_PLAY, Sound_GetName(name), SOUND_WAV_STEREO, VOLUME_FX, false, true, false); // KK
}

int PlayStereoSoundLooped_JustCache(string name)
{
	InitSound();
	//Trace("PlayStereoSoundLooped : "+name);
	return SendMessage(Sound,"lslllll",MSG_SOUND_PLAY, Sound_GetName(name), SOUND_WAV_STEREO, VOLUME_FX, true, true, false); // KK
}

int PlayStereoOGG(string name)
{
	//if(PGMUS && HasSubStr(name,"pgmus")) name = name + "_PGMUS"; // NK so PGMUS doesn't require two ini files. 04-09-17. 04-11-03 fixed.
	// redo musicscheme detection 05-04-29
	// KK if(!HasSubStr(name, GetMusicScheme())) name += GetMusicScheme();
	// NK <--
	InitSound();
	//Trace("PlayStereoSound : "+name);
// KK -->
	aref MusicRef; makearef(MusicRef, Music_Alias[FindMusic(name)].files);
	int num = GetAttributesNum(MusicRef);
	string lnode = "l" + (rand(num - 1) + 1);
	string music_name = MusicRef.(lnode).name;
	return SendMessage(Sound,"lslllll",MSG_SOUND_PLAY, music_name, SOUND_MP3_STEREO, VOLUME_FX, false, false, false);
// <-- KK
}
//MAXIMUS -->
void PlayLocatorSound(ref loc, string group, string name, string snd, float up)
{
	if(group=="" || name=="" || snd=="") return;
	if(FindLoadedLocation()==-1) return;
	if(!CheckAttribute(loc,"locators."+group+"."+name)) return;

	if(FindSounds(snd)>=0)
	{
		int sidx = FindSounds(snd);
		float ftmp1, ftmp2, ftmp3;
		object optref;

		if(CheckAttribute(Sounds_Alias[sidx], "Volume"))
		{
			if(stf(Sounds_Alias[sidx].Volume)>1.0) Sounds_Alias[sidx].Volume = "1.0";

			SendMessage(&sound,"leee", MSG_SOUND_GET_MASTER_VOLUME, &ftmp1, &ftmp2,	&ftmp3);
			optref.volume.sound = stf(Sounds_Alias[sidx].Volume);
			optref.volume.music = ftmp2;
			optref.volume.dialog = 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);
		}
	}

	float x, y, z;
	x = stf(loc.locators.(group).(name).x);
	y = stf(loc.locators.(group).(name).y)+up;
	z = stf(loc.locators.(group).(name).z);
	Play3DSound(snd, x, y, z);
}
//MAXIMUS <--
// OTHER METHODS
void StopSound(int id,int fade)
{
	InitSound();
	//Trace("StopSound : "+id);
	SendMessage(Sound,"lll",MSG_SOUND_STOP,id,fade);
}

void ResumeSound(int id,int fade)
{
	InitSound();
	//Trace("ResumeSound : "+id);
	SendMessage(Sound,"lll",MSG_SOUND_RESUME,id,fade);
}

void ReleaseSound(int id)
{
	InitSound();
	//Trace("ReleaseSound : "+id);
	SendMessage(Sound,"ll",MSG_SOUND_RELEASE,id);
}

// SOUND SCHEMES
void ResetSoundScheme()
{
	InitSound();
	Trace("ResetSoundScheme");
	SendMessage(Sound,"l",MSG_SOUND_SCHEME_RESET);
}

void SetSoundScheme(string schemeName)
{
	InitSound();
	Trace("SetSoundScheme: "+schemeName);
	SendMessage(Sound,"ls",MSG_SOUND_SCHEME_SET,schemeName);
}

void AddSoundScheme(string schemeName)
{
	InitSound();
	Trace("AddSoundScheme: "+schemeName);
	SendMessage(Sound,"ls",MSG_SOUND_SCHEME_ADD,schemeName);
}

void SetWeatherScheme(string scheme)
{
	if (Whr_IsStorm())
	{
		AddSoundScheme(scheme+"_storm");
		PlayStereoSoundLooped("rain_storm");
	}
	else 
	{ 
		if (Whr_IsRain())
		{
			AddSoundScheme(scheme+"_rain");
			PlayStereoSoundLooped("rain");
		}
		else
		{
			AddSoundScheme(scheme);
		}
	}
}

void SetTimeScheme(string scheme)
{
	if (Whr_IsNight())
	{
		AddSoundScheme(scheme+"_night");
	}
	else
	{
		AddSoundScheme(scheme+"_day");
	}
}

void SetSchemeForLocation(ref loc)
{
	//GO <---
	int rainSoundID;
	int windSoundID;
	int waterSoundID;
	int water2SoundID;
	int shipSoundID;
	int ship2SoundID;
	//GO --->

	if(CheckAttribute(loc,"type")) 
	{
		ResetSoundScheme();
		int logined = 0;
		if(LAi_numloginedcharacters>1)
		{
			for(int login=0; login<LAi_numloginedcharacters; login++)
			{
				if(LAi_loginedcharacters[login]<0 || GetMainCharacterIndex()==LAi_loginedcharacters[login]) continue;
				if(!IsOfficer(GetCharacter(LAi_loginedcharacters[login]))) { logined++; }
			}
		}
		switch (loc.type)
		{
		case "port":
			SetSoundScheme("port");
			SetTimeScheme("land");
			SetWeatherScheme("seashore_weather");
			//a simple virtual sailor change -->
			int location_or_nation;
			if(CheckAttribute(loc,"townsack"))
				location_or_nation=GettownNation(loc.townsack);
			else
				location_or_nation=GetLocationNation(loc);
// KK -->
			switch(location_or_nation) {
				case ENGLAND: SetMusicAlarm("music_eng_town"); break;//changed by MAXIMUS -->
				case FRANCE: SetMusicAlarm("music_fra_town"); break;
				case SPAIN: SetMusicAlarm("music_spa_town"); break;
				case PIRATE: SetMusicAlarm("music_pir_town"); break;
				case HOLLAND: SetMusicAlarm("music_hol_town"); break;
				case PORTUGAL: SetMusicAlarm("music_gorod_2"); break;//changed by MAXIMUS <--
			}
			//SetMusicAlarm("music_gorod");
// <-- KK
			//a simple virtual sailor change <-- 
			break;
		case "town":
			SetSoundScheme("town");
			SetTimeScheme("land");
			SetWeatherScheme("land");
			//a simple virtual sailor change -->
			if(CheckAttribute(loc,"townsack"))
				location_or_nation=GettownNation(loc.townsack);
			else
				location_or_nation=GetLocationNation(loc);
// KK -->
			switch(location_or_nation)
			{
				case ENGLAND: SetMusicAlarm("music_eng_town"); break;//changed by MAXIMUS -->
				case FRANCE: SetMusicAlarm("music_fra_town"); break;
				case SPAIN: SetMusicAlarm("music_spa_town"); break;
				case PIRATE: SetMusicAlarm("music_pir_town"); break;
				case HOLLAND: SetMusicAlarm("music_hol_town"); break;
				case PORTUGAL: SetMusicAlarm("music_gorod_2"); break;//changed by MAXIMUS <--
			}
			//SetMusicAlarm("music_gorod");
// <-- KK
			//a simple virtual sailor change <-- 
			break;
		case "jungle":
			SetSoundScheme("jungle");
			SetTimeScheme("land");
			SetWeatherScheme("land");
			SetMusicAlarm("music_jungle");
			break;
		case "KR_jungle":
			SetSoundScheme("jungle");
			SetTimeScheme("land");
			SetWeatherScheme("land");
			SetMusicAlarm("music_jungle");
			break;
		case "KR_jungle1":
			SetSoundScheme("jungle");
			SetTimeScheme("land");
			SetWeatherScheme("land");
			SetMusicAlarm("music_jungle");
			break;
		case "KhaelRoa":
			SetSoundScheme("cave");
			SetMusicAlarm("music_coridor");
			break;
		case "seashore"://changed by MAXIMUS -[for some locations, such as QC]->
			if(CheckAttribute(loc,"townsack")) location_or_nation = GettownNation(loc.townsack);
			else location_or_nation = -1;
			if(location_or_nation==PIRATE)
			{
				SetSoundScheme("port");
				SetTimeScheme("land");
				SetWeatherScheme("seashore_weather");
				SetMusicAlarm("music_pir_town");
			}
			else
			{
				ResetSoundScheme();
				SetTimeScheme("land");
				SetWeatherScheme("seashore_weather");
				SetMusicAlarm("music_jungle");
			}
			break;//changed by MAXIMUS <-[for some locations, such as QC]-
		case "cave":
			SetSoundScheme("cave");
			SetMusicAlarm("music_coridor");
			break;
		case "inca_temple":
			SetSoundScheme("inca_temple");
			SetMusicAlarm("music_coridor");
			break;
		case "house":
			SetSoundScheme("house");
			SetMusicAlarm("music_gorod");
			break;
		case "tavern":
			SetSoundScheme("tavern");
			//a simple virtual sailor change -->
			if (CheckAttribute(loc,"townsack"))
				location_or_nation=GettownNation(loc.townsack);
			else
				location_or_nation=GetLocationNation(loc);
// KK -->
			switch (location_or_nation) {
				case ENGLAND: SetMusicAlarm("music_tavern"); break;
				case FRANCE: SetMusicAlarm("music_tavern"); break;
				case SPAIN: SetMusicAlarm("music_tavern"); break;
				case PIRATE: ref Pchar = GetMainCharacter(); if(sti(Pchar.nation) != PIRATE) SetMusicAlarm("music_tavern_louche"); else SetMusicAlarm("music_tavern_pirate"); break;
				case HOLLAND: SetMusicAlarm("music_tavern"); break;
				case PORTUGAL: SetMusicAlarm("music_tavern"); break;
			}
			//SetMusicAlarm("music_tavern");
// <-- KK
			//a simple virtual sailor change <--
			break;
		case "shop":
			SetSoundScheme("shop");
			SetMusicAlarm("music_shop");
			break;
		case "residence":
			SetSoundScheme("residence");
			//a simple virtual sailor change -->
			if(CheckAttribute(loc,"townsack"))
				location_or_nation=GettownNation(loc.townsack);
			else
				location_or_nation=GetLocationNation(loc);
// KK -->
			switch(location_or_nation) {
				case ENGLAND: SetMusicAlarm("music_english_governor"); break;//changed by MAXIMUS -->
				case FRANCE: SetMusicAlarm("music_french_governor"); break;
				case SPAIN: SetMusicAlarm("music_spanish_governor"); break;
				case PIRATE: SetMusicAlarm("music_gorod_2"); break;
				case HOLLAND: SetMusicAlarm("music_dutch_governor"); break;
				case PORTUGAL: SetMusicAlarm("music_portugal_governor"); break;//changed by MAXIMUS <--
			}
			//SetMusicAlarm("music_gubernator");
// <-- KK
			//a simple virtual sailor change <--
			break;
		case "church":
			SetSoundScheme("church");
			SetMusicAlarm("music_church");
			break;
		case "jail":
			SetSoundScheme("jail");
			SetMusicAlarm("music_coridor");
			PlayStereoSoundLooped("AMBIENT\JAIL\JailAmb.wav");
			break;
		case "deck":
			SetSoundScheme("deck");
			SetMusicAlarm("music_gorod");
			break;
		case "dungeon":				//ccc -->
			SetSoundScheme("inca_temple");
			SetMusicAlarm("music_coridor");
			break;
			// ccc <--
// added by MAXIMUS -->
		case "brothel":
			SetSoundScheme("brothel");
			SetMusicAlarm("music_brothel");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\razgovor_001.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\razg_002.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\razg_003.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\razg_004.wav");
			PlayStereoSoundLooped("brothel");
			break;
		case "brothel_room":	
			SetSoundScheme("room");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\muhi_001.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\vorkovanie_001.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\vorkovanie_002.wav");
			break;
		case "opium_room":	
			SetSoundScheme("room");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\muhi_001.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\muhi_002.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\muhi_003.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\razgovor_001.wav");
			PlayStereoSoundLooped("AMBIENT\BROTHEL\silent_talks.wav");
			break;
		case "treasure_cave":
			SetSoundScheme("cave");
			SetMusicAlarm("music_treasure_cave");
			break;
		case "own_deck":
			SetSoundScheme("own_deck");
			if(Whr_IsStorm())
			{
			    if(HasSubStr(loc.id,"ShipDeck"))
				{
					rainSoundID = PlayStereoSoundLooped_JustCache("rain_storm");
					SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, rainSoundID, 0.5);
					SendMessage(Sound,"lll",MSG_SOUND_RESUME, rainSoundID, 0);
				}
			    windSoundID = PlayStereoSoundLooped_JustCache("water_sea_storm");
			    SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, windSoundID, 0.45);
			    SendMessage(Sound,"lll",MSG_SOUND_RESUME, windSoundID, 0);
			}
			else 
			{ 
			    if(Whr_IsRain())
			    {
			        if(HasSubStr(loc.id,"ShipDeck"))
					{
						rainSoundID = PlayStereoSoundLooped("rain");
						SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, rainSoundID, 0.5);
						SendMessage(Sound,"lll",MSG_SOUND_RESUME, rainSoundID, 0);
					}
			        waterSoundID = PlayStereoSoundLooped_JustCache("water_sea");
			        SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, waterSoundID, 0.6);
			        SendMessage(Sound,"lll",MSG_SOUND_RESUME, waterSoundID, 0);
			    }
			    else
			    {
			        water2SoundID = PlayStereoSoundLooped_JustCache("water_sea");
			        SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, water2SoundID, 0.35);
			        SendMessage(Sound,"lll",MSG_SOUND_RESUME, water2SoundID, 0);
			    }
			}
			if(HasSubStr(loc.id,"ShipDeck"))
			{
				if(bSeaActive && !bCanEnterToLand)
				{
					shipSoundID = PlayStereoSoundLooped_JustCache("squeak_sea");
					SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, shipSoundID, 0.45);
					SendMessage(Sound,"lll",MSG_SOUND_RESUME, shipSoundID, 0);

					ship2SoundID = PlayStereoSoundLooped_JustCache("sails_ambient");
					SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, ship2SoundID, 0.45);
					SendMessage(Sound,"lll",MSG_SOUND_RESUME, ship2SoundID, 0);
				}
				if(bCanEnterToLand)
				{
					shipSoundID = PlayStereoSoundLooped_JustCache("squeak_sea");
					SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, shipSoundID, 0.35);
					SendMessage(Sound,"lll",MSG_SOUND_RESUME, shipSoundID, 0);

					if(!Whr_IsStorm() && !Whr_IsRain() && !Whr_IsNight())
					{
						ship2SoundID = PlayStereoSoundLooped_JustCache("NATURE\seagulls2.wav");
						SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, ship2SoundID, 0.45);
						SendMessage(Sound,"lll",MSG_SOUND_RESUME, ship2SoundID, 0);
					}
				}
			}
			if(Whr_IsNight()) SetMusicAlarm("music_night_sailing");
			else SetMusicAlarm("music_spokplavanie");
			break;
		case "ship":
			SetSoundScheme("ship");
			if(Whr_IsStorm())
			{
				if(HasSubStr(loc.id,"ShipDeck"))
				{
					rainSoundID = PlayStereoSoundLooped_JustCache("rain_storm");
					SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, rainSoundID, 0.5);
					SendMessage(Sound,"lll",MSG_SOUND_RESUME, rainSoundID, 0);
				}
			    windSoundID = PlayStereoSoundLooped_JustCache("water_sea_storm");
			    SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, windSoundID, 0.45);
			    SendMessage(Sound,"lll",MSG_SOUND_RESUME, windSoundID, 0);
			}
			else 
			{
			    if(Whr_IsRain())
			    {
			        if(HasSubStr(loc.id,"ShipDeck"))
					{
						rainSoundID = PlayStereoSoundLooped("rain");
						SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, rainSoundID, 0.5);
						SendMessage(Sound,"lll",MSG_SOUND_RESUME, rainSoundID, 0);
					}
			        waterSoundID = PlayStereoSoundLooped_JustCache("water_sea");
			        SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, waterSoundID, 0.6);
			        SendMessage(Sound,"lll",MSG_SOUND_RESUME, waterSoundID, 0);
			    }
			    else
			    {
			        water2SoundID = PlayStereoSoundLooped_JustCache("water_sea");
			        SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, water2SoundID, 0.35);
			        SendMessage(Sound,"lll",MSG_SOUND_RESUME, water2SoundID, 0);
			    }
			}
			if(HasSubStr(loc.id,"ShipDeck")) break;
			if(Whr_IsNight()) SetMusicAlarm("music_night_sailing");
			else
			{
			    if(logined>=2)
				{
					PlayStereoSoundLooped("shipyard");
					PlayStereoSoundLooped("AMBIENT\SHIPYARD\Fon_Out_01.wav");
				}
				SetMusicAlarm("music_spokplavanie");
			}
			break;
		case "crew":
			if(!Whr_IsNight() && logined>=2) SetSoundScheme("tavern");
			else SetSoundScheme("crew");
			if (Whr_IsStorm())
			{
			    windSoundID = PlayStereoSoundLooped_JustCache("water_sea_storm");
			    SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, windSoundID, 0.45);
			    SendMessage(Sound,"lll",MSG_SOUND_RESUME, windSoundID, 0);
			}
			else 
			{ 
			    if (Whr_IsRain())
			    {
			        waterSoundID = PlayStereoSoundLooped_JustCache("water_sea");
			        SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, waterSoundID, 0.6);
			        SendMessage(Sound,"lll",MSG_SOUND_RESUME, waterSoundID, 0);
			    }
			    else
			    {
			        water2SoundID = PlayStereoSoundLooped_JustCache("water_sea");
			        SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, water2SoundID, 0.35);
			        SendMessage(Sound,"lll",MSG_SOUND_RESUME, water2SoundID, 0);
			    }
			}
			if(Whr_IsNight()) SetMusicAlarm("music_night_sailing");
			else SetMusicAlarm("music_spokplavanie");
			break;
// added by MAXIMUS <--
		}
	}
	if(!CheckAttribute(loc,"soundskip")) SetStaticSounds(loc);// MAXIMUS: strange, but StaticSounds not works in AOP locations. I'm wondering - all was OK with these locations
}

void SetStaticSounds(ref loc)
{
	if(!IsEntity(loc)) return;
	if(!CheckAttribute(loc, "locators.sound")) return;

	aref locatorGroup; makearef(locatorGroup, loc.locators.sound);
	if(GetAttributesNum(locatorGroup)>0)
	{
		for(int i=0; i<GetAttributesNum(locatorGroup); i++)
		{
			aref locator = GetAttributeN(locatorGroup, i);
			string locatorName = GetAttributeName(locator);
			string locatorType = strcut(locatorName, 0, strlen(locatorName)-3);
			if(locatorType=="nightinsects" || locatorType=="tree") continue;
			SendMessage(Sound,"lsllllllfff",MSG_SOUND_PLAY, Sound_GetName(locatorType), SOUND_WAV_3D, VOLUME_FX, false, true, true, 0, stf(locator.x), stf(locator.y), stf(locator.z));//MAXIMUS
		}
	}
}

void SetSchemeForAbordage()
{
	ResetSoundScheme();
	SetMusic("music_abordage"); // KK
	ResumeAllSounds();
	int abordageSoundID = PlayStereoSoundLooped_JustCache("abordage");
	SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, abordageSoundID, 0.5);
	SendMessage(Sound,"lll",MSG_SOUND_RESUME, abordageSoundID, 0);
}

void SetSchemeForSea()
{
	ResetSoundScheme();

	AddSoundScheme("sea");
	if (Whr_IsStorm())
	{
		AddSoundScheme("sea_weather_storm");
		int rainSoundID = PlayStereoSoundLooped_JustCache("rain_storm");
		SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, rainSoundID, 0.5);
		SendMessage(Sound,"lll",MSG_SOUND_RESUME, rainSoundID, 0);
		int windSoundID = PlayStereoSoundLooped_JustCache("water_sea_storm");
		SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, windSoundID, 0.45);
		SendMessage(Sound,"lll",MSG_SOUND_RESUME, windSoundID, 0);
		SetMusic("music_storm");
	}
	else 
	{ 
		SetMusic("music_spokplavanie");
		if (Whr_IsRain())
		{
			AddSoundScheme("sea_weather_rain");
			PlayStereoSoundLooped("rain");
			int waterSoundID = PlayStereoSoundLooped_JustCache("water_sea");
			SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, waterSoundID, 0.6);
			SendMessage(Sound,"lll",MSG_SOUND_RESUME, waterSoundID, 0);
		}
		else
		{
			AddSoundScheme("sea_weather");
			int water2SoundID = PlayStereoSoundLooped_JustCache("water_sea");
			SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, water2SoundID, 0.35);
			SendMessage(Sound,"lll",MSG_SOUND_RESUME, water2SoundID, 0);
		}
	}
	ResumeAllSounds();
}

void SetSchemeForMap()
{
	ResetSoundScheme();
	AddSoundScheme("sea_map");
	SetMusic("music_map");
	ResumeAllSounds();

	int water2SoundID = PlayStereoSoundLooped_JustCache("water_sea");
	SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, water2SoundID, 0.05);
	SendMessage(Sound,"lll",MSG_SOUND_RESUME, water2SoundID, 0);
}

// MUSIC
int musicID = 0;
int oldMusicID = 0;
string musicName = "";
string oldMusicName = "";
// NK 04-09-09
// to set music with no pause in music
void SetMusicNoPause(string name)
{
	//bugfix 04-12-11 was not handling PGMUS correctly.
	// KKstring n2 = name;
	// KK if(name != musicName && name + GetMusicScheme() != musicName) SetMusic(n2); // 05-04-29 fix music handling
	SetMusic(name); // KK
}
// NK <--
void SetMusic(string name)
{
	//return;
	// KK if(!HasSubStr(name, GetMusicScheme())) name += GetMusicScheme(); // redo music handling 05-04-29
	InitSound();
	Trace("SETTING MUSIC: "+name);
	if (name == musicName)
	{
		SendMessage(Sound,"lll",MSG_SOUND_RESUME, musicID, SOUNDS_FADE_TIME);
		return;
	}

	//Trace("SetMusic : "+name);
	if (oldMusicID)
	{
		SendMessage(Sound,"ll",MSG_SOUND_RELEASE, oldMusicID);
	}

	if (musicID)
	{
		SendMessage(Sound,"lll",MSG_SOUND_STOP, musicID, MUSIC_CHANGE_TIME);
		oldMusicID = musicID;
	}

	int silenceTime = 20000 + MakeInt(frnd()*MUSIC_SILENCE_TIME);
// KK -->
	int midx = FindMusic(name);

	if (midx < 0) return;
	aref MusicRef; makearef(MusicRef, Music_Alias[midx].files);
	int num = GetAttributesNum(MusicRef);
	string lnode = "l" + (rand(num - 1) + 1);
	string music_name = MusicRef.(lnode).name;
	musicID = SendMessage(Sound,"lslllllll",MSG_SOUND_PLAY, music_name, SOUND_MP3_STEREO, VOLUME_MUSIC, true, true, false, MUSIC_CHANGE_TIME, silenceTime);
	if (CheckAttribute(Music_Alias[midx], "Volume"))
		SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, musicID, stf(Music_Alias[midx].Volume));
	else
		SendMessage(Sound,"llf",MSG_SOUND_SET_VOLUME, musicID, 0.55);
// <-- KK
	SendMessage(Sound,"lll",MSG_SOUND_RESUME, musicID, MUSIC_CHANGE_TIME);
	
	oldMusicName = musicName;
	musicName = name;
}

void FadeOutMusic(int _time)
{
	if (musicID)
	{
		StopSound(musicID,_time);
		musicID = 0;
		musicName = "";
	}
}

// RELOAD
void PauseAllSounds()
{
	Trace("PauseAllSounds");
	SendMessage(Sound,"lll",MSG_SOUND_STOP, 0, SOUNDS_FADE_TIME);
}

void ResumeAllSounds()
{
	Trace("ResumeAllSounds");
	SendMessage(Sound,"lll",MSG_SOUND_RESUME, musicID, SOUNDS_FADE_TIME);
}

// OLD VERSIONS
int PlaySoundDist3D(string name, float x, float y, float z)
{
	return Play3DSound(name, x,y,z);
}

int PlaySoundLoc3D(string name, float x, float y, float z)
{
	return Play3DSoundCached(name, x,y,z);
}

int PlaySound3D(string name, float x, float y, float z)
{
	return Play3DSound(name, x,y,z);
}

int PlaySound(string name)
{
	return PlayStereoSound(name);
}

int PlaySoundComplex(string sSoundName, bool bSimpleCache, bool bLooped, bool bCached, int iFadeTime)
{
	return SendMessage(Sound,"lsllllll",MSG_SOUND_PLAY,VOLUME_FX,Sound_GetName(sSoundName),SOUND_WAV_3D,bSimpleCache,bLooped,bCached,iFadeTime); // KK
}

void StopMusic()
{
}

void PlayMusic(string n)
{
}
//--------------------------------------------------------------------
// Sound Section
//--------------------------------------------------------------------
object Sound;

int alarmed = 0;
int oldAlarmed = 0;
bool seaAlarmed = false;
bool oldSeaAlarmed = false;
void SetMusicAlarm(string name)
{
	if (alarmed == 0)
	{
		SetMusic(name);
	}
	else
	{
		SetMusic("music_bitva");
	}
}

void Sound_OnAllarm()
{
	Sound_OnAlarm(GetEventData());
}

void Sound_OnAlarm(bool _alarmed)
{
	alarmed = _alarmed;
	if (alarmed == oldAlarmed)
		return;

	if (alarmed != 0)
	{ //alarm on!
		SetMusic("music_bitva");
	}
	else
	{ //alarm off
		SetMusic(oldMusicName);
	}
	oldAlarmed = alarmed;
}

void Sound_OnSeaAlarm(bool _seaAlarmed)
{
	seaAlarmed = _seaAlarmed;
	if (seaAlarmed == oldSeaAlarmed)
		return;

	if (seaAlarmed)
	{ //alarm on!
		SetMusic("music_sea_battle");
	}
	else
	{ //alarm off
		SetMusic(oldMusicName);
	}
	oldSeaAlarmed = seaAlarmed;
}

void InitSound()
{
	//Trace("InitSound");
	if (!IsEntity(&Sound))
	{
		CreateEntity(&Sound, "Sound");
		SetEventHandler("eventAllarm", "Sound_OnAllarm", 0);
		//Alias_Init(); // KK
	}
	//SendMessage(Sound,"lf",MSG_SOUND_SET_MASTER_VOLUME,1.0);
}

void ResetSound()
{
	ResetSoundScheme();
	StopSound(0,0);
	ReleaseSound(0);
	musicName = "";
	oldMusicName = "";
}

void LoadSceneSound()
{
// KK -->
	if (LAi_boarding_process && LAi_group_IsActivePlayerAlarm()) {
		SetMusic("music_abordage");
		if(HasSubStr(characters[GetMainCharacterIndex()].location,"BOARDING_"))//MAXIMUS -->
		{
			if(FindFile("RESOURCE\SOUNDS\OBJECTS\ABORDAGE", "*.wav", "abordage1.wav")!="")
			{
				if(!HasSubStr(characters[GetMainCharacterIndex()].location,"NEXT") && !HasSubStr(characters[GetMainCharacterIndex()].location,"CABIN")) characters[GetMainCharacterIndex()].boardingsound = PlayStereoSoundLooped("OBJECTS\ABORDAGE\abordage1.wav");
			}
		}//MAXIMUS <--
	} else {
		int i = FindLoadedLocation();
		if (i != -1) SetSchemeForLocation(&Locations[i]);
	}
// <-- KK
}

// KK -->
string GetMusicSchemeName(int musNum)
{
	string retVal = "Standard";
	ref rms; makeref(rms, registered_music_schemes);
	string lnode = "l" + musNum;
	if (CheckAttribute(rms, lnode)) {
		if (CheckDirectory("RESOURCE\Sounds\MUSIC\\" + rms.(lnode).resdir, "*.ogg") > 0) retVal = rms.(lnode).name;
	}
	return retVal;
}

string GetMusicScheme()
{
	string retVal = "Standard";
	ref rms; makeref(rms, registered_music_schemes);
	string lnode = "l" + music_scheme;
	if (CheckAttribute(rms, lnode)) {
		if (CheckDirectory("RESOURCE\Sounds\MUSIC\\" + rms.(lnode).resdir, "*.ogg") > 0) retVal = rms.(lnode).id;
	}
	return retVal;
}

string GetMusicSchemeResDir()
{
	string retVal = "";
	ref rms; makeref(rms, registered_music_schemes);
	string lnode = "l" + music_scheme;
	if (CheckAttribute(rms, lnode + ".resdir")) retVal = rms.(lnode).resdir;
	return retVal;
}

int GetMusicSchemeCount()
{
	ref rms; makeref(rms, registered_music_schemes);
	return GetAttributesNum(rms);
}

void UpdateSound()
{
	string mid = musicName;
	ResetSound();
	Alias_Init();
	SetMusic(mid);
}

string Sound_GetName(string name)
{
	aref SoundsRef;
	int sidx = FindSounds(name);
	if (sidx >= 0) {
		makearef(SoundsRef, Sounds_Alias[sidx].files);
	} else {
		sidx = FindGreetings(name);
		if (sidx >= 0) {
			makearef(SoundsRef, Greetings_Alias[sidx].files);
		} else {
			sidx = FindTalks(name);
			if (sidx >= 0)
				makearef(SoundsRef, Talks_Alias[sidx].files);
			else
				return name;
		}
	}

	int num = GetAttributesNum(SoundsRef);
	string lnode = "l" + (rand(num - 1) + 1);
	if(!CheckAttribute(SoundsRef, lnode))// MAXIMUS: FOR TESTING -->
	{
		traceandlog("WARNING: sound "+name+" not exist, or not properly added into alias");
		return "INTERFACE\notebook.wav";
	}// MAXIMUS: FOR TESTING <--
	return SoundsRef.(lnode).name;
}

//MAXIMUS: -[Will not reset sounds and music, only a dialog. Also a greeting will be played as 3D]->
void PlayGreeting(ref refChar)
{
	InitSound();
	int gidx = FindGreetings(refChar.greeting);
	if(gidx < 0) return;// -1;

	float ftmp1,ftmp2,ftmp3,x,y,z;
	object optref;

	GetCharacterPos(refChar,&x,&y,&z);

	if(CheckAttribute(Greetings_Alias[gidx], "Volume"))
	{
		if(stf(Greetings_Alias[gidx].Volume)>1.0) Greetings_Alias[gidx].Volume = "1.0";

		SendMessage(&sound,"leee", MSG_SOUND_GET_MASTER_VOLUME, &ftmp1, &ftmp2,	&ftmp3);
		optref.volume.sound = ftmp1;
		optref.volume.music = ftmp2;
		optref.volume.dialog = stf(Greetings_Alias[gidx].Volume);

		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);
	}
	SendMessage(Sound,"lsllllllfff",MSG_SOUND_PLAY, Sound_GetName(refChar.greeting), SOUND_WAV_3D, VOLUME_SPEECH, false, false, false, 0, x, y, z);

//	int greetingID = SendMessage(Sound,"lslllll",MSG_SOUND_PLAY, Sound_GetName(refChar.greeting), SOUND_WAV_STEREO, VOLUME_SPEECH, false, false, false);
//	if (CheckAttribute(Greetings_Alias[gidx], "Volume")) SendMessage(Sound, "llf", MSG_SOUND_SET_VOLUME, greetingID, stf(Greetings_Alias[gidx].Volume));

//	return greetingID;
}
// <-- KK
//MAXIMUS: <-[Will not reset sounds and music, only a dialog. Also a greeting will be played as 3D]-
