void() DynamicMusic;

// FrikaC's player dependant sound code
// so different players hear different music
void(entity reciever, float vol, float attenuation, float soundnum, vector where, float chan) music =
{
	msg_entity = reciever;
	local float h;
	
	WriteByte(1, 6); // SVC_SOUND
	WriteByte(1, 3); // Mask vol & attenuation
	h = floor (vol * 255);
	WriteByte(1, h);
	WriteByte(1, attenuation * 64);
	WriteShort(1, chan); // world + chan
        WriteByte(1, soundnum);
	WriteCoord(1, where_x);
	WriteCoord(1, where_y);
	WriteCoord(1, where_z);
};

void() musicPrecache =
{
        precache_sound ("music/idle.ogg");     // sound 1
        precache_sound ("music/spotted.ogg");  // sound 2
        precache_sound ("music/dead.ogg");     // sound 3

        precache_sound ("music/idle2.ogg");     // sound 4
        precache_sound ("music/idle3.ogg");     // sound 5
        precache_sound ("music/fadeout.ogg");     // sound 6

	precache_sound ("talk/talk1.wav"); // 7
	precache_sound ("talk/talk2.wav"); // 8
	precache_sound ("talk/talk3.wav"); // 9
	precache_sound ("talk/talk4.wav"); // 10
	precache_sound ("talk/talk5.wav"); // 11
	precache_sound ("talk/talk6.wav"); // 12
	precache_sound ("talk/talk7.wav"); // 13
	precache_sound ("talk/talk8.wav"); // 14
	precache_sound ("talk/talk9.wav"); // 15
	precache_sound ("talk/talk10.wav"); // 16
	precache_sound ("talk/talk11.wav"); // 17
	precache_sound ("talk/talk12.wav"); // 18
	precache_sound ("talk/talk13.wav"); // 19
	precache_sound ("talk/talk14.wav"); // 20
	precache_sound ("talk/talk15.wav"); // 21
	precache_sound ("talk/talk16.wav"); // 22
	precache_sound ("talk/talk17.wav"); // 23
	precache_sound ("talk/talk18.wav"); // 24
	precache_sound ("talk/talk19.wav"); // 25
	precache_sound ("talk/talk20.wav"); // 26
	precache_sound ("talk/talk21.wav"); // 27
	precache_sound ("talk/talk22.wav"); // 28
	precache_sound ("talk/talk23.wav"); // 29
	precache_sound ("talk/talk24.wav"); // 30
	precache_sound ("talk/talk25.wav"); // 31
	precache_sound ("talk/talk26.wav"); // 32
	precache_sound ("talk/talk27.wav"); // 33
	precache_sound ("talk/talk28.wav"); // 34
	precache_sound ("talk/talk29.wav"); // 35
	precache_sound ("talk/talk30.wav"); // 36
	precache_sound ("talk/talk31.wav"); // 37
	precache_sound ("talk/talk32.wav"); // 38
	precache_sound ("talk/talk33.wav"); // 39
	precache_sound ("talk/talk34.wav"); // 40
	precache_sound ("talk/talk35.wav"); // 41
	precache_sound ("talk/talk36.wav"); // 42
	precache_sound ("talk/talk37.wav"); // 43
	precache_sound ("talk/talk38.wav"); // 44
	precache_sound ("talk/talk39.wav"); // 45
	precache_sound ("talk/talk40.wav"); // 46
	precache_sound ("talk/talk41.wav"); // 47
	precache_sound ("talk/talk42.wav"); // 48
	precache_sound ("talk/talk43.wav"); // 49
	precache_sound ("talk/talk44.wav"); // 50
	precache_sound ("talk/talk45.wav"); // 51
	precache_sound ("talk/talk46.wav"); // 52

	precache_sound ("player/teeth.wav"); // 53
	precache_sound ("player/heart.wav"); // 54
};

void() playMusicIdle =
{
	local float r;
	r = random();
	if (r < 0.35)
        	music(self, 0.8, ATTN_NONE, 1, self.origin, CHAN_MUSIC);
	else if (r < 0.70)
        	music(self, 0.8, ATTN_NONE, 4, self.origin, CHAN_MUSIC);
	else
        	music(self, 0.8, ATTN_NONE, 5, self.origin, CHAN_MUSIC);
        self.musicstate = idleplaying;
};

void() playMusicFade =
{
        music(self, 1, ATTN_NONE, 6, self.origin, CHAN_MUSIC);
        self.musicstate = fadeplaying;
};

void() playMusicFight =
{
        music(self, 1, ATTN_NONE, 2, self.origin, CHAN_MUSIC);
        self.musicstate = fightplaying;
};

void() playMusicDead =
{
        music(self, 1, ATTN_NONE, 3, self.origin, CHAN_MUSIC);
        self.musicstate = deadplaying;
};

void() fightMusicPlay =
{
        if (self.musicstate == fightplaying)
                return;
        else
                self.musicstate = fight;
};

void() fadeMusicPlay =
{
        if (self.musicstate == fadeplaying)
                return;
        else
                self.musicstate = fade;
};

void() fightCountdown =
{
        local string fighttime;

        if (self.owner.fight_time <= 0)
        {
                self.owner.fight_time = 0;
                self.owner.fightCounting = FALSE;
                remove(self);
		return;
        }

        self.owner.fightCounting = TRUE;
        self.owner.fight_time = self.owner.fight_time - 1;
        self.nextthink = time + 0.75;
        self.think = fightCountdown;
};


void() fightCountSpawn =
{
        local entity fight_count;

        fight_count = spawn();
        fight_count.owner = self;
        fight_count.nextthink = time + 0.1;
        fight_count.think = fightCountdown;
};


//
// Fading out music
//

void() fadeCountdown =
{
        local string fadetime;

        if (self.owner.fade_time <= 0)
        {
                self.owner.fade_time = 0;
                self.owner.fadeCounting = FALSE;
                remove(self);
		return;
        }

        self.owner.fadeCounting = TRUE;
        self.owner.fade_time = self.owner.fade_time - 1;
        self.nextthink = time + 0.75;
        self.think = fadeCountdown;
};

void() fadeCountSpawn =
{
        local entity fight_count;

        fight_count = spawn();
        fight_count.owner = self;
        fight_count.nextthink = time + 0.1;
        fight_count.think = fadeCountdown;
	self.fade_time = 8;
};

void() DynamicMusic =
{
        if (self.musicstate == fade)
        {
                playMusicFade();
                self.musicstate = fadeplaying;
                return;
        }
        else if (self.musicstate == idle)
        {
                playMusicIdle();
                self.musicstate = idleplaying;
                return;
        }
        else if (self.musicstate == fight)
        {
                playMusicFight();
                self.musicstate = fightplaying;
                return;
        }
        else if (self.musicstate == dead)
        {
                playMusicDead();
                self.musicstate = deadplaying;
                return;
        }
        else if (self.musicstate == idleplaying)
        {
                return;
        }
        else if (self.musicstate == fightplaying)
        {
                if (self.fight_time <= 0)
                {
			if (self.fade_counting == FALSE)
			{
				self.fade_time = 11;
       				self.fade_counting = TRUE;
        			fadeMusicPlay ();
			}
			self.musicstate = fade;
                        DynamicMusic();
                }                
                else
                {
                        return;
                }
        }
        else if (self.musicstate == fadeplaying)
        {
                if (self.fade_time <= 0) // works if negative
                {
                        self.musicstate = idle;
                        DynamicMusic();
                }                
                else
                {
                        return;
                }
        }
        else if (self.musicstate == deadplaying)
        {
                return;
        }
        else
        {
                self.musicstate = idle;
                DynamicMusic();
        }                
};