// File: sound.agc
// Created: 20-06-05


#constant SOUND3D_SCALE		 			1.0
#constant SOUND3D_DOPPLER	 				1
#constant SOUND3D_DOPPLER_INTENSITY		0.1

type Sound3d_def
	rootsnd as integer
	insnum 	as integer
	X 		as float
	Y 		as float
	Z 		as float
	lastdis 	as float
	play 	as integer
	initvol 	as float
	nearmul 	as float
	rate 	as float
	doppler 	as integer
endtype

// 3d Sound Library
// ================
function Create3dSound(snd, X as float, Y as float, Z as float, initvol as float, nearmul as float,playing,rate as float,doppler)
	pos as integer
	ins as integer
	
	if Sound3d.length=-1
		dim Sound3d[] as Sound3d_def
	endif

	Sound3d.length=Sound3d.length+1
	pos = Sound3d.length

	ins = PlaySound(snd, initvol, playing)
	SetSoundInstanceRate(ins,rate)

	Sound3d[pos].rootsnd=snd
	Sound3d[pos].insnum = ins
	
	Sound3d[pos].X = X
	Sound3d[pos].Y = Y
	Sound3d[pos].Z = Z
	Sound3d[pos].play = playing	
	Sound3d[pos].initvol = initvol
	Sound3d[pos].nearmul = nearmul
	Sound3d[pos].rate = rate
	Sound3d[pos].doppler = doppler
	
endfunction pos

function Set3dSoundPosition(pos as integer,X as float,Y as float,Z as float)
	Sound3d[pos].X = X
	Sound3d[pos].Y = Y
	Sound3d[pos].Z = Z

endfunction

function Set3dSoundVolume(pos as integer,initvol as float,nearmul as float)
	Sound3d[pos].initvol = initvol
	Sound3d[pos].nearmul = nearmul
endfunction

function Set3dSoundRate(pos as integer,rate as float)
	Sound3d[pos].rate = rate
endfunction

function Play3dSound(pos,playing)
	rate as float
	snd as integer
	ins as integer
	Sound3d[pos].play = playing	
	snd = Sound3d[pos].rootsnd
	rate = Sound3d[pos].rate
	ins = PlaySound(snd,10.0,playing)
	SetSoundInstanceRate( ins, rate ) 
	Sound3d[pos].insnum = ins
	
endfunction

function Stop3dSound(pos)
	StopSoundInstance(Sound3d[pos].insnum)
	Sound3d.remove(pos)
endfunction

function Update3dSound()
	camx as float
	camy as float
	camz as float
	pos  as integer
	ins  as integer
	x	 as float
	y	 as float
	z	 as float
	dis	 as float
	initvol as float
	nearmul as float
	vol as float
	bal as float
	absrate as float
	rate as float
	
	CamX = GetCameraX(1)
	CamY = GetCameraY(1)
	CamZ = GetCameraZ(1)

	for pos =0 to Sound3d.length
		
		ins = Sound3d[pos].insnum
		
	
		if GetSoundInstancePlaying(ins)=1
			X = Sound3d[pos].X
			Y = Sound3d[pos].Y
			Z = Sound3d[pos].Z			
			dis = Get3dDistance(CamX,CamY,CamZ,X,Y,Z)
			initvol = Sound3d[pos].initvol
			nearmul = Sound3d[pos].nearmul
			
			vol = initvol-(((dis/10.0)/SOUND3D_SCALE)^2)*(1/nearmul)
			if vol>0.0
						
				bal = sin(atan2(CamX-X,CamZ-Z)-GetCameraAngleY(1)+180.0)
				SetSoundInstanceVolume(ins,vol)
				SetSoundInstanceBalance(ins,bal)
				
				if SOUND3D_DOPPLER = 1 and Sound3d[pos].doppler = 1
					
					absrate = ((dis-Sound3d[pos].lastdis)*SOUND3D_DOPPLER_INTENSITY)
					if absrate<-SOUND3D_DOPPLER_INTENSITY then absrate=-SOUND3D_DOPPLER_INTENSITY
					if absrate>SOUND3D_DOPPLER_INTENSITY then absrate=SOUND3D_DOPPLER_INTENSITY
					rate = Sound3d[pos].rate-absrate		
					SetSoundInstanceRate(ins,(rate+GetSoundInstanceRate(ins))/2)
					
				else	
					rate = Sound3d[pos].rate		
					SetSoundInstanceRate(ins,rate)
				endif
				
			else
				SetSoundInstanceVolume(ins,0.0)
			endif
			
			Sound3d[pos].lastdis = dis
			
		else
			Sound3d.remove(pos)
		endif


	next pos

endfunction

// math library
function CurveValue(Destination as float, Current as float, Smooth as float)
	Current = Current + ((Destination-Current)/Smooth)
endfunction Current

function Get3dDistance(SX as float,SY as float,SZ as float,EX as float,EY as float,EZ as float)
	dis as float
	dis = sqrt((SX-EX)^2+(SY-EY)^2+(SZ-EZ)^2)
endfunction dis