1 /*
2  *  CVS: $Id: GameFileRunner.java,v 1.5 2004/07/16 16:22:35 marcus Exp $
3  * 
4  *  This file is part of Zuul.
5  *
6  *  Zuul is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10 *
11 *  Zuul is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with Zuul; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 * 
20 *  Copyrigth 2004 by marcus, leh
21 * 
22 */
23
24package org.jzuul.tools;
25
26import java.util.Iterator;
27import java.util.List;
28import java.util.Vector;
29
30import org.jzuul.engine.CommandList;
31import org.jzuul.engine.Engine;
32import org.jzuul.engine.exceptions.GuiNotInitializedException;
33import org.jzuul.engine.gui.GuiInterface;
34
35/**
36 * The GameFileRunner is a general purpose Engine Excecutable which is usable
37 * for any feature of  the JZuul Engine. 
38 * 
39 * 
40 * @version $Revision: 1.5 $
41 */
42public class GameFileRunner {
43    /**
44     * The Base where to find GuiInterface implementations
45     */
46    private static final String guiBase = "org.jzuul.engine.gui";
47
48    /**
49     * The filename of the inital gamefile
50     */
51    private String filename;
52    /**
53     * Soll bei dem Spiel der Spielername erfragt werden?
54     */
55    private boolean withPlayerName = true;
56    /**
57     * Soll ein History Lauf gemacht werden
58     */
59    private boolean historyRun = false;
60    /**
61     * NPCs werden von eigenem Thread bedient
62     */
63    private boolean threaded = false;
64    /**
65     * Anzahl der Spieler
66     */
67    private int numberOfPlayers = 0;
68    /**
69     * Der Name des History File Names
70     */
71    private String histFileName;
72    /**
73     * Das zu benutzende Gui (default ist TextUI)
74     */
75    private String guiName = "TextUi";
76
77    /**
78     * Das Engine Objekt das von dem GameFileRunner Benutzt wird
79     */
80    private Engine engine;
81
82    /**
83     * Startet den GameFileRunner mit den gegebenen Kommandozeilenargumenten
84     * 
85     * @param args  Kommandozeilenargumente
86     */
87    public static void main(String[] args) {
88        if (args.length == 0) {
89            GameFileRunner.usage();
90        }
91        Vector v = new Vector(args.length);
92        for (int i = 0; i < args.length; i++) {
93            v.add(args[i].replaceFirst("-+", ""));
94        }
95
96        GameFileRunner runner = new GameFileRunner(v);
97        runner.run();
98    }
99
00    /**
01     * Erstellt ein GameFileRunner Objekt
02     * 
03     * @param arguments Argumente
04     */
05    public GameFileRunner(List arguments) {
06        // handle the arguments
07        for (Iterator argIter = arguments.iterator(); argIter.hasNext();) {
08            String element = (String) argIter.next();
09            if (element.endsWith(".xml") && filename == null) {
10                if (element.startsWith("/")) {
11                    filename = element;
12                } else {
13                    filename = "/" + element;
14                }
15                argIter.remove();
16            }
17            if (element.equals("players")) {
18                argIter.remove();
19                numberOfPlayers = Integer.parseInt((String) argIter.next());
20                argIter.remove();
21            }
22            if (element.equals("history")) {
23                historyRun = true;
24                argIter.remove();
25                histFileName = (String) argIter.next();
26                argIter.remove();
27                if (!histFileName.startsWith("/")) {
28                    histFileName = "/" + histFileName;
29                }
30            }
31            if (element.equals("gui")) {
32                argIter.remove();
33                guiName = (String) argIter.next();
34                argIter.remove();
35            }
36            if (element.equals("debug")) {
37                argIter.remove();
38                Engine.DEBUG = Integer.parseInt((String) argIter.next());
39                argIter.remove();
40            }
41        }
42        withPlayerName = arguments.contains("name");
43        arguments.remove("name");
44        threaded = arguments.contains("threaded");
45        arguments.remove("threaded");
46        
47        if (!arguments.isEmpty()) {
48            System.out.println();
49            for (Iterator argIter = arguments.iterator(); argIter.hasNext();) {
50                String element = (String) argIter.next();
51                System.out.println("ERROR: No such argument: " + element);
52            }
53            System.out.println();
54            usage();
55        }
56        
57        GuiInterface gui = createGui();
58        this.engine = new Engine(filename, CommandList.defaultList(), gui, threaded, numberOfPlayers);
59    }
60
61    /**
62     * Versucht ein GuiInterface Objekt aus den angegeben Namen zu erstellen
63     * 
64     * @return  ein GuiInterface Objekt
65     */
66    protected GuiInterface createGui() {
67        Object guiObj = null;
68        try {
69            Class classDefinition = Class.forName(guiBase + "." + guiName);
70            guiObj = classDefinition.newInstance();
71        } catch (InstantiationException e) {
72            System.out.println(e);
73        } catch (IllegalAccessException e) {
74            System.out.println(e);
75        } catch (ClassNotFoundException e) {
76            System.out.println("ERROR: There is no " + guiName + " GUI");
77            System.exit(2);
78        }
79
80        if (guiObj != null) {
81            return (GuiInterface) guiObj;
82        } else {
83            throw new GuiNotInitializedException();
84        }
85    }
86
87    /**
88     * Lässt das Engine laufen.
89     *
90     */
91    protected void run() {
92        if (historyRun) {
93            engine.runHistory(histFileName);
94        } else {
95            engine.run(withPlayerName);
96        }
97    }
98
99    /**
00     * Gibt eine Hilfsmeldung aus
01     */
02    public static void usage() {
03        System.out.println(" This is the JZuul Gamefile runner $Revision: 1.5 $ ");
04        System.out.println();
05        System.out.println(" JZuul is free software, you can distribute and modify ");
06        System.out.println(" it under the terms of the GPL (see LICENSE)");
07        System.out.println();
08        System.out.println(
09            " Usage:  [java] GameFileRunner gamefile.xml [-name] [-history <file.xml>]");
10        System.out.println("\t [-players <number>] [-threaded] [-gui GuiImplementation] ");
11        System.out.println("\t [-debug value]");
12        System.out.println();
13        System.out.println("\t-name\t\tdoes do a named Player game");
14        System.out.println("\t-history\tdoes a history run on the given file");
15        System.out.println("\t-players\tcreates a game with number of players");
16        System.out.println("\t-threaded\tlet the NPCs actions run in an own Thread");
17        System.out.println("\t-debug\t\tsets the Engine debug level to Value");
18        System.out.println();
19        System.exit(1);
20    }
21
22}
23