"""game_config.py

   This loads the game configuration

   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 
"""



from pygame.locals import *
import cPickle, os

class config_base:

    # These are set to default so that if there is no config file, we still can load up and play
    screenwidth = 640
    screenheight = 480
    musicvol = 0.5
    soundvol = 0.5
    fullscreen = 0

                #['Up', K_UP], -- Un-needed
    keymap = [  ['Kneel', K_DOWN], 
                ['Left', K_LEFT], 
                ['Right', K_RIGHT], 
                ['Jump', K_z], 
                ['Fire', K_x], 
                ['Pause', K_p], 
                ['QuickQuit', K_q],
                ['QuickSave', K_s],
                ['QuickLoad', K_l]  ]

    configfile = None

    # These items are not saved permanently
    nomusic = 0
    nosfx = 0
    noparallax = 0
    gamespeed = 20
    confdir = None
    datadir = None
    startlevel = None
    firstrun = 0

    def __init__(self):
        pass

    def load(self, configfile):
        """Load config file"""

        # Join the configfile name to the full path to the config dir
        configfile = os.path.join(self.confdir, configfile)

        # Save the config filename for later use
        self.configfile = configfile

        # Try to open the config file for read, return 0 on fail
        try:
            f = file(configfile, 'r')
        except:
            print "Unable to load config file for read, using default values"
            self.firstrun = 1
            return(0)

        # Load config data
        self.screenwidth = cPickle.load(f)
        self.screenheight = cPickle.load(f)
        self.musicvol = cPickle.load(f)
        self.soundvol = cPickle.load(f)
        self.fullscreen = cPickle.load(f)
        self.keymap = cPickle.load(f)

        f.close()

    def save(self):
        """Save out the configuration, to the config file passed to the load function, error if none"""

        if self.configfile:
            configfile = self.configfile
        else:
            print "Error, no config filename specified"

        try:
            f = file(configfile, 'w')
        except:
            print "Unable to open config file for write."
            return(0)

        cPickle.dump(self.screenwidth, f)
        cPickle.dump(self.screenheight, f)
        cPickle.dump(self.musicvol, f)
        cPickle.dump(self.soundvol, f)
        cPickle.dump(self.fullscreen, f)
        cPickle.dump(self.keymap, f)

        f.close()

    def key(self, name):
        """Take a keyname and return the value"""

        for (keyname, keyval) in self.keymap:
            if keyname == name:
                return(keyval)

    def changekey(self, name, value):
        """Change the value of key"""

        newmap = []

        for (keyname, keyval) in self.keymap:
            # If this is they key to change, replace it in our new list
            if keyname == name:
                newmap.append([name, value])
            # Else add the original value
            else:
                newmap.append([keyname, keyval])

        self.keymap = newmap[:]

config = config_base()
