<?xml version="1.0" encoding="UTF-8"?>
<rmml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="L:\collaborative-nir\program\Cscl3DWS\rmml\RMML.xsd">
	<composition _name="CSoundEngine" >
		<script>
			this.sResPath='';
			this.sPrefix='';
			this.snd = null;
			this.playCommon = null;
			this.lastSound = null;
			this.hintSound = null;	// только для режима [ V ]
			this.aWrongSPlay = null;
			this.aWrongSCPlay = null;
			this.chemCloud = null;
			this.bSilenceMode = false;
			this.silenceStub = null;
			this.soundPath  = "Sounds";
		</script>
		<script><![CDATA[
			function rewindAndPlayAudio(snd)
			{
				if( snd.playing)
					snd.stop();
				snd.rewindAndPlay();
				//snd.play();
			}
			function isAudio(snd)
			{
				return (snd instanceof Audio)?true:false;
			}

			function isSpeech(snd)
			{
				return (snd instanceof Speech)?true:false;
			}

			function isSoundPlaying(snd)
			{
				return (isAudio(snd) && snd._playing) || (isSpeech(snd) && scene.chemistscene.chemist.Chemist.speech==snd)?true:false;
			}

			function silenceMode(val)
			{
				this.bSilenceMode = val;
				this.stopCurrent();
				this.showSilenceAnimation(this.bSilenceMode);
			}

			function stopCurrent()
			{
				if( this.lastSound != null)
				{
					if( isAudio(lastSound))
					{
						this.lastSound.stop();
					}
					else	if( isSpeech(lastSound))
						scene.chemistscene.chemist.Chemist.say(null);
				}

				if (snd!=null)
				{
					s = snd.target;
					if( this.isSoundPlaying(snd))
					{
						if( isAudio(snd))
						{
							snd._playing = false;
							snd = null;
							fireEvent(s+".onPlayed");
						}
						else	if( isSpeech(snd))
						{
							scene.chemistscene.chemist.Chemist.say(null);
							snd = null;
							fireEvent(s+".onSaid");
						}
					}
					else
						snd = null;
				}
			}

			function onsPlayed(id)
			{
				if( this.bSilenceMode || eval("scene.chemistscene.chemist.Chemist.aud"+id)==undefined)	return true;
				return eval("scene.chemistscene.chemist.Chemist.aud"+id).target+".onSaid";
			}

			function splay(id)
			{
				stopCurrent();
				if( this.bSilenceMode)
				{
					return true;
				}
				trace("Say sounds:"+id);
				if( eval("this.aud"+id)==undefined)
				//if( eval("scene.chemistscene.chemist.Chemist.aud"+id)==undefined)
				{
					//snd = new Speech();
					snd = new Audio();
					snd._name = "aud"+id;
					if( eval("this.aud"+id)==undefined)
					{
						if(id >= 10)	snd.src = ":" + this.sResPath + sPrefix + id + ".mp3";
						else	snd.src = ":" + this.sResPath + sPrefix + "0" + id + ".mp3";
					}
					else
					{
						var audioSrc = eval("this.aud"+id).src;
						if( audioSrc.indexOf("..\\")!=-1 || audioSrc.indexOf("../")!=-1)
							audioSrc = audioSrc.substr(3, audioSrc.length-3);
						snd.src = ":" + audioSrc;
					}
					//snd.parent = scene.chemistscene.chemist.Chemist;
					snd.parent = this;
				}
				else
				{
					//snd = eval("scene.chemistscene.chemist.Chemist.aud"+id);
					snd = eval("this.aud"+id);
				}
				/*if (scene.moduleH != undefined)
				{
					if (scene.moduleH)
					{
						var strTmp = snd.src;
						var count = 6;
						if (strTmp.toLowerCase().indexOf("sounds_w") != -1)
							count = 8;
						if (strTmp.toLowerCase().indexOf("sounds_s") != -1)
							count = 8;
						if (strTmp.toLowerCase().indexOf("sounds_ws") != -1)
							count = 9;
						var strI = strTmp.toLowerCase().indexOf("sounds");
						if (strI != -1)
						{
							var path = strTmp.substr(0,strI) + this.soundPath + strTmp.substr(strI+count, strTmp.length);
							if (resourceExists(path))
								snd.src = path;
						}
					}
				}*/
				if( this.aWrongSPlay != null)
				{
					var i=0;
					for( i=0; i<this.aWrongSPlay.length; i++)
						if( this.aWrongSPlay[i] == id)
							break;
					if( i>=this.aWrongSPlay.length)
						this.lastSound = snd;
				}
				else
					this.lastSound = snd;
				if( this.chemCloud != null)
					this.chemCloud.splay(id, snd.target+".onSaid");
				if( snd==undefined || snd==null)
					trace("Error index in splay["+id+"]");
				else
				{
					if(isAudio(snd))
					{
						rewindAndPlayAudio(snd);
						//snd.stop();
						//snd.rewindAndPlay();
						return snd.target+".onPlayed";
					}
					else if(isSpeech(snd))
					{
						scene.chemistscene.chemist.Chemist.say(snd);
						return snd.target+".onSaid";
					}
				}
				return true;
			}

			function scplay(id)
			{
				stopCurrent();
				if( this.bSilenceMode)
				{
					return true;
				}
				trace("Say common sounds:"+id);
				if( eval("this.caud"+id)==undefined)
				//if( eval("scene.chemistscene.chemist.Chemist.caud"+id)==undefined)
				{
trace("New speech");
					//snd = new Speech();
					snd = new Audio();
					snd._name = "caud"+id;
					if( eval("this.caud"+id)==undefined)
					{
						if(id >= 10)	snd.src = "rmsnd:scaud" + id + ".mp3";
						else	snd.src = "rmsnd:scaud" + "0" + id + ".mp3";
					}
					else
					{
						//snd.src = eval("this.caud"+id).src;
						snd.src = ":" + eval("this.caud"+id).src;
					}
					//snd.parent = scene.chemistscene.chemist.Chemist;
					snd.parent = this;
				}
				else
				{
					//snd = eval("scene.chemistscene.chemist.Chemist.caud"+id);
					snd = eval("this.caud"+id);
trace("No New speech="+snd);
				}
				/*if (scene.moduleH != undefined)
				{
					if (scene.moduleH)
					{
						if (this.soundPath == "Sounds")
						{
							if(id >= 10)
							{
								snd.src = "rmsnd:scaud" + id + ".mp3"
							}
							else	snd.src = "rmsnd:scaud" + "0" + id + ".mp3";
						}
						else
						{
							if(id >= 10)	snd.src = ":mmlab.chemcommon.resources.soundsh:" + this.soundPath + "\\scaud" + id + ".mp3";
							else	snd.src = ":mmlab.chemcommon.resources.soundsh:" + this.soundPath + "\\scaud0" + id + ".mp3";
						}
					}
				}*/
				if (!resourceExists(snd.src))
				{
					if(id >= 10)	snd.src = "rmsnd:scaud" + id + ".mp3";
					else	snd.src = "rmsnd:scaud" + "0" + id + ".mp3";
				}
				if( this.aWrongSCPlay != null)
				{
					var i=0;
					for( i=0; i<this.aWrongSCPlay.length; i++)
						if( this.aWrongSCPlay[i] == id)
							break;
					if( i>=this.aWrongSCPlay.length)
						this.lastSound = snd;
				}
				else
					this.lastSound = snd;
				if( this.chemCloud != null)
					this.chemCloud.scplay(id, snd.target+".onSaid");
				if(isAudio(snd))
				{
					trace("IS audio");
					//snd.stop();
					//snd.rewindAndPlay();
					rewindAndPlayAudio(snd);
					return snd.target+".onPlayed";
				}
				else if(isSpeech(snd))
				{
					trace("IS speech");
					scene.chemistscene.chemist.Chemist.say(snd);
					return snd.target+".onSaid";
				}
				return true;
			}

			function getSEName(nm)
			{
				return nm.replace(/(\.)/g,"_");
			}

			function sestop(_aud_name)
			{
				var nm = getSEName(_aud_name);
				if( eval("this."+nm) != undefined && eval("this."+nm)!=null)
					eval("this."+nm).stop();
			}

			function seplay(_aud_name, lp)
			{
				var aud = null;
				trace("Play SE sounds: "+_aud_name);
				aud = new Audio();
				aud.src = "rmsnd:SE\\" + _aud_name;
				if( lp!=undefined)
					aud.loop = lp;
				aud.parent = this;
				aud._name = getSEName(_aud_name);
				aud._playing = true;
			}

			function help(sndID,aplayCommon)
			{
				//if (playCommon != null) this.playCommon = true;
				if (aplayCommon == undefined) this.playCommon = false;
				else
					this.playCommon = aplayCommon;
				trace("Say help["+sndID+"]");
				if (sndID == -1)
				{
					trace("STOP: seq_help");
					this.seqHelp._playing = false;
				}
				else
				{
					this.id = sndID;
					this.seqHelp.rewindAndPlay();
				}
			}

			function errorAction()
			{
				if( this.id==-1)	return;
				if( this.seqHelp.currentFrame!=1)
				{
					seqHelp.currentFrame = 1;
					seqHelp.play();
				}
			}

			function setCloud(cloud)
			{
				this.chemCloud = cloud;
			}

			/* Функция используется в модулях с ограничением по слуху.
			При нажатии на горячую клавишу (по умолчанию Enter) повторяется последняя фраза
			химика. Последней фразой является звук, который был вызван командой splay(..) или scplay(..).
			Повторение звука возможно только в тот момент, когда химик не произносит других звуков. */
			function repeatLastComment()
			{
				trace("-repeatLastComment-"+lastSound);
				if( this.lastSound == null)	return;										// если не было ни одного звука - ничего не повторяем
				if( this.isSoundPlaying(this.lastSound))	return;						// если сейчас играется звук - ничего не повторяем
				if( this.snd!=null && this.isSoundPlaying(this.snd))	return;	// если сейчас играется звук - ничего не повторяем
				//
				//this.lastSound.rewindAndPlay();
				if( isAudio(lastSound))
				{
					//this.lastSound.stop();
					//this.lastSound.rewindAndPlay();
					rewindAndPlayAudio(lastSound);
				}
				else if( isSpeech(lastSound))
					scene.chemistscene.chemist.Chemist.say(lastSound);
			}

			function isSoundPlaying(snd)
			{
				if( this.snd==null) return false;
				if(isAudio(snd))
					if( this.snd._playing == true)	return true;
				else	if(isSpeech(snd))
					if( this.snd._playing == true)	return true;
				return false;
			}

			/*Функция используется в модулях с ограничением по зрению.
			При появления ярлыка с подсказкой может проигрываться звук.
			Повторение звука возможно только в тот момент, когда химик не произносит других звуков.
			*/
			function sayHintText(sname)
			{
				if( this.isSoundPlaying(snd))	return true;	// если сейчас играется звук - ничего не повторяем
				if( this.isSoundPlaying(hintSound))	return true;	// если сейчас играется звук - ничего не повторяем
				if( this.bSilenceMode)	return true;
				if( eval("scene.chemistscene.chemist.Chemist.hintSound")==undefined)
				{
					snd = new Speech();
					snd._name = "hintSound";
					snd.parent = scene.chemistscene.chemist.Chemist;
				}
				else	snd = eval("scene.chemistscene.chemist.Chemist.hintSound");
				snd.src = "rmsnd:hint\\"+sname+".mp3";
				scene.chemistscene.chemist.Chemist.say(snd);
				return snd.target+".onSaid";
			}

			function DefaultFillWrongSCPlay()
			{
				if( this.aWrongSCPlay != null)	return;
				this.aWrongSCPlay = new Array(1,3,9,12,14,15,16,17,22,24,31,32,33,35,41,42,43);
			}

			function showSilenceAnimation(val)
			{
				if( val && this.silenceStub == null)
				{
					this.silenceStub = new Image();
					this.silenceStub._name = "silenceStub";
					this.silenceStub.src = "rmgr:silenceStub.png";
					this.silenceStub.x = 32;
					this.silenceStub.y = 337;
					this.silenceStub.parent  = scene;
				}
				if(this.silenceStub!=null)
					this.silenceStub.visible = val;
			}
		]]></script>
		<sequencer _name="seqHelp" _loop="true">
			<script>
				trace("PLAYING: seq_help. ID = " + this.id);
				30000;
			</script>
			<script>
				if (this.playCommon == true)
					scene.sengine.scplay(this.id);
				else
					scene.sengine.splay(this.id);
			</script>
			<script>
				20000;
			</script>
		</sequencer>
	</composition>
</rmml>