From 5dcdbb1e744e2960e26555e5ee99623fa35abad5 Mon Sep 17 00:00:00 2001 From: Matei Date: Sun, 8 Jul 2018 22:31:26 -0700 Subject: [PATCH] Tanks v0.3.5 - Improved the tank registry to support custom tanks. This commit also cleans up some registry code. --- src/tanks/Game.java | 56 +++++++++---- src/tanks/Panel.java | 2 +- src/tanks/Registry.java | 173 +++++++++++++++++++--------------------- 3 files changed, 127 insertions(+), 104 deletions(-) diff --git a/src/tanks/Game.java b/src/tanks/Game.java index b9a00507..c9cf4024 100644 --- a/src/tanks/Game.java +++ b/src/tanks/Game.java @@ -63,27 +63,55 @@ public enum Menu {none, paused, title, options, interlevel} public static PrintStream logger = System.err; - public static void initScript () { - System.out.println(System.getProperty("java.class.path")); + public static final String directoryPath = "/.tanks.d"; + public static final String logPath = directoryPath + "/logfile.txt"; + public static final String registryPath = directoryPath + "/tank-registry.txt"; + + public static ArrayList defaultTanks = new ArrayList(); + + public static void initScript() + { + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankBrown.class, "brown", 1)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankGray.class, "gray", 1)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankMint.class, "mint", 1.0 / 2)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankYellow.class, "yellow", 1.0 / 2)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankMagenta.class, "magenta", 1.0 / 3)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankRed.class, "red", 1.0 / 3)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankGreen.class, "green", 1.0 / 4)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankPurple.class, "purple", 1.0 / 4)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankWhite.class, "white", 1.0 / 4)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankOrange.class, "orange", 1.0 / 6)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankDarkGreen.class, "darkgreen", 1.0 / 9)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankBlack.class, "black", 1.0 / 10)); + defaultTanks.add(new Registry.DefaultTankEntry(EnemyTankPink.class, "pink", 1.0 / 15)); + String homedir = System.getProperty("user.home"); - if (!Files.exists(Paths.get(homedir+"/.tanks.d"))) { - new File (homedir+"/.tanks.d").mkdir(); - try { - new File (homedir+"/.tanks.d/logfile.txt").createNewFile(); - Game.logger = new PrintStream (new FileOutputStream (homedir+"/.tanks.d/logfile.lmao", true)); - } catch (IOException e) { + if (!Files.exists(Paths.get(homedir + directoryPath))) + { + new File (homedir + directoryPath).mkdir(); + try + { + new File (homedir + logPath).createNewFile(); + Game.logger = new PrintStream (new FileOutputStream (homedir + logPath, true)); + } + catch (IOException e) + { e.printStackTrace(); System.exit(1); } - Registry.initRegistry (homedir); + Registry.initRegistry(homedir); } - try { - Game.logger = new PrintStream (new FileOutputStream (homedir+"/.tanks.d/logfile.lmao", true)); - } catch (FileNotFoundException e) { + try + { + Game.logger = new PrintStream(new FileOutputStream (homedir + logPath, true)); + } + catch (FileNotFoundException e) + { Game.logger = System.err; - Game.logger.println(new Date().toString() + " (syswarn) logfile not found despite existence of tanks.d! using stderr instead."); + Game.logger.println(new Date().toString() + " (syswarn) logfile not found despite existence of tanks directory! using stderr instead."); } - Registry.loadRegistry (homedir); + + Registry.loadRegistry(homedir); } public static void main(String[] args) diff --git a/src/tanks/Panel.java b/src/tanks/Panel.java index acd829c3..39788906 100644 --- a/src/tanks/Panel.java +++ b/src/tanks/Panel.java @@ -577,7 +577,7 @@ else if (Game.menu.equals(Game.Menu.interlevel)) g.setFont(g.getFont().deriveFont(Font.BOLD, 12)); - g.drawString("Tanks v0.3.4", 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 12 - Screen.yOffset)); + g.drawString("Tanks v0.3.5", 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 12 - Screen.yOffset)); g.drawString("FPS: " + lastFPS, 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 24 - Screen.yOffset)); g.drawString("Coins: " + Game.coins, 2, (int) (Game.gamescreen.getSize().getHeight() - 40 + 36 - Screen.yOffset)); diff --git a/src/tanks/Registry.java b/src/tanks/Registry.java index e097b564..0a6b9665 100644 --- a/src/tanks/Registry.java +++ b/src/tanks/Registry.java @@ -16,114 +16,84 @@ public class Registry public ArrayList tankRegistries = new ArrayList(); protected double maxTankWeight = 0; - public static void loadRegistry (String homedir) { - String path = homedir + "/.tanks.d/tank-registry.lmao"; - try { + public static void loadRegistry (String homedir) + { + String path = homedir + Game.registryPath; + try + { Scanner in = new Scanner (new File (path)); - while (in.hasNextLine()) { + while (in.hasNextLine()) + { String[] tankLine = in.nextLine().split(","); - if (tankLine[0].charAt(0) == '#') { continue; } - if (tankLine[2].toLowerCase().equals("default")) { - switch (tankLine[0]) { - case "brown": - new Registry.TankEntry(Game.registry, EnemyTankBrown.class, "brown", Double.parseDouble(tankLine[1])); - break; - case "gray": - new Registry.TankEntry(Game.registry, EnemyTankGray.class, "gray", Double.parseDouble(tankLine[1])); - break; - case "mint": - new Registry.TankEntry(Game.registry, EnemyTankMint.class, "mint", Double.parseDouble(tankLine[1])); - break; - case "yellow": - new Registry.TankEntry(Game.registry, EnemyTankYellow.class, "yellow", Double.parseDouble(tankLine[1])); - break; - case "magenta": - new Registry.TankEntry(Game.registry, EnemyTankMagenta.class, "magenta", Double.parseDouble(tankLine[1])); - break; - case "red": - new Registry.TankEntry(Game.registry, EnemyTankRed.class, "red", Double.parseDouble(tankLine[1])); - break; - case "green": - new Registry.TankEntry(Game.registry, EnemyTankGreen.class, "green", Double.parseDouble(tankLine[1])); - break; - case "purple": - new Registry.TankEntry(Game.registry, EnemyTankPurple.class, "purple", Double.parseDouble(tankLine[1])); - break; - case "white": - new Registry.TankEntry(Game.registry, EnemyTankWhite.class, "white", Double.parseDouble(tankLine[1])); - break; - case "orange": - new Registry.TankEntry(Game.registry, EnemyTankOrange.class, "orange", Double.parseDouble(tankLine[1])); - break; - case "darkgreen": - new Registry.TankEntry(Game.registry, EnemyTankDarkGreen.class, "darkgreen", Double.parseDouble(tankLine[1])); - break; - case "black": - new Registry.TankEntry(Game.registry, EnemyTankBlack.class, "black", Double.parseDouble(tankLine[1])); - break; - case "pink": - new Registry.TankEntry(Game.registry, EnemyTankPink.class, "pink", Double.parseDouble(tankLine[1])); - break; - default: - Game.logger.println (new Date().toString() + " (syswarn) no such default tank '" + tankLine[0] + "' exists. ignoring."); + if (tankLine[0].charAt(0) == '#') + { + continue; + } + if (tankLine[2].toLowerCase().equals("default")) + { + for (int i = 0; i < Game.defaultTanks.size(); i++) + { + if (tankLine[0].equals(Game.defaultTanks.get(i).name)) + { + Game.defaultTanks.get(i).registerEntry(Game.registry); break; + } + Game.logger.println (new Date().toString() + " (syswarn) the default tank [" + tankLine[0] + "] does not exist!"); } - } else { - try { + } + else + { + try + { @SuppressWarnings("resource") ClassLoader loader = new URLClassLoader ( new URL[] { new File(tankLine[3]).toURI().toURL() }); // super messy @SuppressWarnings("unchecked") Class clasz = (Class) loader.loadClass(tankLine[4]); new Registry.TankEntry(Game.registry, clasz, tankLine[0], Double.parseDouble(tankLine[1])); - } catch (Exception e) { + } + catch (Exception e) + { e.printStackTrace(); Game.logger.println (new Date().toString() + " (syswarn) error loading custom tank '" + tankLine[3] + "'. try adding the path to your jvm classpath. ignoring."); } } } in.close(); - } catch (Exception e) { - Game.logger.println (new Date().toString() + " (syswarn) tank-registry.lmao nonexistent or broken, using default."); - new Registry.TankEntry(Game.registry, EnemyTankBrown.class, "brown", 1); - new Registry.TankEntry(Game.registry, EnemyTankGray.class, "gray", 1); - new Registry.TankEntry(Game.registry, EnemyTankMint.class, "mint", 1.0 / 2); - new Registry.TankEntry(Game.registry, EnemyTankYellow.class, "yellow", 1.0 / 2); - new Registry.TankEntry(Game.registry, EnemyTankMagenta.class, "magenta", 1.0 / 3); - new Registry.TankEntry(Game.registry, EnemyTankRed.class, "red", 1.0 / 3); - new Registry.TankEntry(Game.registry, EnemyTankGreen.class, "green", 1.0 / 4); - new Registry.TankEntry(Game.registry, EnemyTankPurple.class, "purple", 1.0 / 4); - new Registry.TankEntry(Game.registry, EnemyTankWhite.class, "white", 1.0 / 4); - new Registry.TankEntry(Game.registry, EnemyTankOrange.class, "orange", 1.0 / 6); - new Registry.TankEntry(Game.registry, EnemyTankDarkGreen.class, "darkgreen", 1.0 / 9); - new Registry.TankEntry(Game.registry, EnemyTankBlack.class, "black", 1.0 / 10); - new Registry.TankEntry(Game.registry, EnemyTankPink.class, "pink", 1.0 / 15); + } + catch (Exception e) + { + Game.logger.println (new Date().toString() + " (syswarn) tank-registry file is nonexistent or broken, using default."); + + for (int i = 0; i < Game.defaultTanks.size(); i++) + { + Game.defaultTanks.get(i).registerEntry(Game.registry); + } } } - public static void initRegistry (String homedir) { - String path = homedir + "/.tanks.d/tank-registry.lmao"; - try { + public static void initRegistry (String homedir) + { + String path = homedir + Game.registryPath; + try + { new File (path).createNewFile(); - } catch (IOException e) { - Game.logger.println (new Date().toString() + " (syserr) file permissions are screwed up! cannot initialize tank registry."); + } + catch (IOException e) + { + Game.logger.println (new Date().toString() + " (syserr) file permissions are broken! Cannot initialize tank registry."); System.exit(1); } - try { + try + { PrintStream writer = new PrintStream (new File (path)); - writer.println("brown,"+String.valueOf(1)+",default"); - writer.println("gray,"+String.valueOf(1)+",default"); - writer.println("mint,"+String.valueOf(1.0/2)+",default"); - writer.println("yellow,"+String.valueOf(1.0/2)+",default"); - writer.println("magenta,"+String.valueOf(1.0/3)+",default"); - writer.println("red,"+String.valueOf(1.0/3)+",default"); - writer.println("green,"+String.valueOf(1.0/4)+",default"); - writer.println("purple,"+String.valueOf(1.0/4)+",default"); - writer.println("white,"+String.valueOf(1.0/4)+",default"); - writer.println("orange,"+String.valueOf(1.0/6)+",default"); - writer.println("darkgreen,"+String.valueOf(1.0/9)+",default"); - writer.println("black,"+String.valueOf(1.0/10)+",default"); - writer.println("pink,"+String.valueOf(1.0/15)+",default"); - } catch (Exception e) { + + for (int i = 0; i < Game.defaultTanks.size(); i++) + { + writer.println(Game.defaultTanks.get(i).getString()); + } + } + catch (Exception e) + { Game.logger.println (new Date().toString() + " (syserr) something broke. cannot initialize tank registry."); System.exit(1); } @@ -166,15 +136,40 @@ public Tank getTank(double x, double y, double a) } } + static class DefaultTankEntry + { + public final Class tank; + public final String name; + public final double weight; + + protected double startWeight; + protected double endWeight; + + public DefaultTankEntry(Class tank, String name, double weight) + { + this.tank = tank; + this.name = name; + this.weight = weight; + } + + public TankEntry registerEntry(Registry r) + { + return new TankEntry(r, this.tank, this.name, this.weight); + } + + public String getString() + { + return this.name + "," + this.weight + ",default"; + } + } + public TankEntry getRandomTank() { double random = Math.random() * maxTankWeight; - System.out.println(random); for (int i = 0; i < tankRegistries.size(); i++) { TankEntry r = tankRegistries.get(i); - System.out.println(r.name); - System.out.println(r.startWeight + " " + r.endWeight); + if (random >= r.startWeight && random < r.endWeight) { return r;