#!/usr/local/bin/python2.7
"""scnloadtool.py

   A command line editor for scene data

   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 scnbase:

    filename = None

    frames = None
    nextmap = None
    wait = None
    framelist = []

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

        f = file(self.filename, 'r')
        self.frames = cPickle.load(f)
        self.nextmap = cPickle.load(f)
        self.wait = cPickle.load(f)

        for i in range(self.frames):
            self.framelist.append(cPickle.load(f))

        f.close()

    def list(self):
        print "frames ", self.frames
        print "nextmap ", self.nextmap
        print "wait ", self.wait
        print "framelist ", self.framelist

    def save(self):
        f = file(self.filename, 'w')
        cPickle.dump(self.frames, f)
        cPickle.dump(self.nextmap, f)
        cPickle.dump(self.wait, f)

        for i in range(self.frames):
            cPickle.dump(self.framelist[i], f)

        f.close() 

scn = scnbase()

show = 0
save = 0

frames = None
nextmap = None
wait = None
framelist = []

filename = None

for item in sys.argv:
    if item == "--show":
        show = 1
    elif item == "--save":
        save = 1
    elif string.find(item, "--nextmap=") > -1:
        array = string.split(item, "=")
        nextmap = array[1]
    elif string.find(item, "--wait=") > -1:
        array = string.split(item, "=")
        wait =  string.atoi(array[1])
    elif string.find(item, "--framelist=") > -1:
        array = string.split(item, "=")
        framelist = string.split(array[1], ",")
    elif string.find(item, "--scene=") > -1:
        array = string.split(item, "=")
        filename = array[1]

if filename != None:
    scn.load(filename)

    if show:
        scn.list()

    if frames != None:
        scn.frames = frames
    if nextmap != None:
        scn.nextmap = nextmap
    if wait != None:
        scn.wait = wait
    if framelist:
        scn.framelist = framelist[:]

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

            --nextmap=<string>          Set the map to load after this scene has been played
            --wait=<1 or 0>             0 for wait between frames, 1 for not.
            --framelist=<file>,<file>   The image files to use for each frame.

            Note: The number of frames is automatically calculated based upon the number of entries passed to --framelist="""
