"""game_sound.py

   Basic sound module using pygame
   Adventures on Planet Zephulor by Aaron Hachez. 

   All code, artwork, and sound copyright 2003, Aaron Hachez.

   ----------------------------------------------------------------------------------------

   This file is part of "Adventures on Planet Zephulor".

   A copy of the GNU General Public License should be included with any
   redistribution of this program in a file named "COPYING.txt".

   "Adventures on Planet Zephulor" is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   "Adventures on Planet Zephulor" is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with "Adventures on Planet Zephulor"; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   ----------------------------------------------------------------------------------------

   "Adventures on Planet Zephulor" uses the following libraries:

   PyGame
   SDL

   Both are at the time of this writing, licensed under the GNU LGPL. Please see
   the included file LGPL for details 
"""

import pygame
import cPickle
import os

import game_config

class sfx:
    
    # The sound item
    sound = None

    def __init__(self, filename):
       # If the nosfx flag is set
       if game_config.config.nosfx == 1:
           return(None)

       self.load(filename)

    def load(self, filename):
        fullname = os.path.join(game_config.config.datadir, filename)

        try:
            self.sound = pygame.mixer.Sound(fullname)
        except:
            print 'Cannot load sound:', fullname
            raise SystemExit

    def play(self):
        self.sound.play()

    def stop(self):
        self.sound.stop()

class sfxlist:

    filelist = []
    sfxlist = []
    index = []
    indexcount = 0

    def __init__(self, filename):
        """Load sounds from a list provided by a file"""

        # If the nosfx flag is set
        if game_config.config.nosfx == 1:
            return(None)

        fullname = os.path.join(game_config.config.datadir, filename)

        try:
            f = file(fullname,'r')

            numentries = cPickle.load(f)
            
            for i in range(numentries):
                self.filelist.append(cPickle.load(f))

            f.close()
        except:
            print "Failed to load sound list file", fullname
            game_config.config.nosfx = 1
            return()
            #raise SystemExit

        for list in self.filelist:

            # Append the char name to our index, with it's index number
            self.index.append( (list[0], self.indexcount) )
            # Append a new list for sounds for this char
            self.sfxlist.append([])

            # Load the sounds into this new list
            for item in (list[1]):

                # If the item is none, there is no sound for this event
                if item != None:
                    soundname = os.path.join(game_config.config.datadir, item)
                    self.sfxlist[self.indexcount].append(pygame.mixer.Sound(soundname))
                else:
                    self.sfxlist[self.indexcount].append(None)
           
            # Increment to the next index 
            self.indexcount += 1

    def setvol(self, level):
        """Set the volume level for all sound effects"""

        # If there are no sound effects, return
        if game_config.config.nosfx:
            return()

        for sf in self.sfxlist:
            for item in sf:
                if item != None:
                    item.set_volume(level)

    def getsndid(self, charname):
        """Take the char name, and return the uniq index"""

        # If there are no sound effects, return
        if game_config.config.nosfx:
            return()

        for (name, index) in self.index:
            if name == charname:
                return (index)

    def play(self, sndid, sndindex):
        """Play the sound at sndid, with index sndindex"""

        # If there are no sound effects, return
        if game_config.config.nosfx:
            return()

        if self.sfxlist[sndid][sndindex] != None:
            self.sfxlist[sndid][sndindex].play()

class music_base:
    def load(self, filename):
        # If the nomusic flag is set
        if game_config.config.nomusic == 1:
            return(None)

        fullname = os.path.join(game_config.config.datadir, filename)

        try:
            pygame.mixer.music.load(fullname)
        except:
            print 'Cannot load music:', fullname
            #raise SystemExit
            game_config.config.nomusic = 1

    def getvol(self):
        """Return the current music volume"""

        if game_config.config.nomusic:
            return()

        return(pygame.mixer.music.get_volume())

    def setvol(self, level):
        """Set the volume level"""

        if game_config.config.nomusic:
            return()

        pygame.mixer.music.set_volume(level)

    def getstate(self):
        """Return whether or not busy"""

        if game_config.config.nomusic:
            return()

        return(pygame.mixer.music.get_busy())

    def loop(self):
        """When called checks to see if music is still playing, if not, restarts it"""

        if game_config.config.nomusic:
            return()

        if not pygame.mixer.music.get_busy():
            pygame.mixer.music.rewind()
            pygame.mixer.music.play()

    def play(self):

        if game_config.config.nomusic:
            return()

        pygame.mixer.music.play()

    def stop(self):

        if game_config.config.nomusic:
            return()

        pygame.mixer.music.stop()

# Setup a global sound list
entsfxlist = sfxlist('entsndlist.data')
music = music_base()
