#!/usr/local/bin/python2.6
"""maploadtool.py

   A command line map data editor

   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 cPickle, sys, string

class mapbase:

    filename = None

    width = None
    height = None
    tilewidth = None
    tileheight = None
    tilefile = None
    maplist = None
    mapdata = None
    chardata = None
    nextmaplist = None
    bgtag = None
    musicfile = None

    def load(self, filename):
        self.filename = filename

        f = file(self.filename, 'r')
        self.width = cPickle.load(f)
        self.height = cPickle.load(f)
        self.tilewidth = cPickle.load(f)
        self.tileheight = cPickle.load(f)
        self.tilefile = cPickle.load(f)
        self.maplist = cPickle.load(f)
        self.mapdata = cPickle.load(f)
        self.chardata = cPickle.load(f)
        self.nextmaplist = cPickle.load(f)
        self.bgtag = cPickle.load(f)
        self.musicfile = cPickle.load(f)
        f.close()

    def list(self):
        print "width  ", self.width
        print "height  ", self.height
        print "tilewidth  ", self.tilewidth
        print "tileheight  ", self.tileheight
        print "tilefile  ", self.tilefile
        #print "maplist  ", self.maplist
        #print "mapdata  ", self.mapdata
        print "chardata  ", self.chardata
        print "nextmaplist  ", self.nextmaplist
        print "bgtag  ", self.bgtag
        print "musicfile  ", self.musicfile


    def save(self):
        f = file(self.filename, 'w')
        cPickle.dump(self.width, f)
        cPickle.dump(self.height, f)
        cPickle.dump(self.tilewidth, f)
        cPickle.dump(self.tileheight, f)
        cPickle.dump(self.tilefile, f)
        cPickle.dump(self.maplist, f)
        cPickle.dump(self.mapdata, f)
        cPickle.dump(self.chardata, f)
        cPickle.dump(self.nextmaplist, f)
        cPickle.dump(self.bgtag, f)
        cPickle.dump(self.musicfile, f)
        f.close() 

map = mapbase()

show = 0
save = 0

width = None
height = None
tilewidth = None
tileheight = None
tilefile = None
chardata = None
nextmaplist = None
bgtag = None
musicfile = None

mapname = None

for item in sys.argv:
    if item == "--show":
        show = 1
    elif item == "--save":
        save = 1
    elif string.find(item, "--width=") > -1:
        array = string.split(item, "=")
        width =  string.atoi(array[1])
    elif string.find(item, "--height=") > -1:
        array = string.split(item, "=")
        height =  string.atoi(array[1])
    elif string.find(item, "--tilewidth=") > -1:
        array = string.split(item, "=")
        tilewidth =  string.atoi(array[1])
    elif string.find(item, "--tileheight=") > -1:
        array = string.split(item, "=")
        tileheight =  string.atoi(array[1])
    elif string.find(item, "--tilefile=") > -1:
        array = string.split(item, "=")
        tilefile = array[1]
    elif string.find(item, "--chardata=") > -1:
        array = string.split(item, "=")
        chardata = eval(array[1])
    elif string.find(item, "--nextmaplist=") > -1:
        array = string.split(item, "=")
        nextmaplist = eval(array[1])
    elif string.find(item, "--bgtag=") > -1:
        array = string.split(item, "=")
        bgtag = array[1]
    elif string.find(item, "--musicfile=") > -1:
        array = string.split(item, "=")
        musicfile = array[1]
    elif string.find(item, "--map=") > -1:
        array = string.split(item, "=")
        mapname = array[1]

if mapname != None:
    map.load(mapname)

    if show:
        map.list()

    if width != None:
        map.width = width
    if height != None:
        map.height = height
    if tilewidth != None:
        map.tilewidth = tilewidth
    if tileheight != None:
        map.tileheight = tileheight
    if tilefile != None:
        map.tilefile = tilefile
    if chardata != None:
        map.chardata = chardata
    if nextmaplist != None:
        map.nextmaplist = nextmaplist
    if bgtag != None:
        map.bgtag = bgtag
    if musicfile != None:
        map.musicfile = musicfile

    if save:
        map.save()
        if show:
            print """
            After save:
            """
            map.list()
else:
    print """Usage:""", sys.argv[0], """[options...]\n
            --show                    Show map info
            --save                    Save map info
            --map=<string>            Set the map to load

            --width=<string>          Set the map width
            --height=<string>         Set the map height
            --tilewidth=<string>      Set the map tilewidth
            --tileheight=<string>     Set the map tileheight
            --tilefile=<string>       Set the tilefile to use
            --chardata=<string>       Set new char data
            --nextmaplist=<string>    Set next map list
            --bgtag=<string>          Set the backgound image tag
            --musicfile=<string>      Set the music file"""
